diff options
Diffstat (limited to 'src/roscha.c')
-rw-r--r-- | src/roscha.c | 133 |
1 files changed, 66 insertions, 67 deletions
diff --git a/src/roscha.c b/src/roscha.c index 3f315ce..75f42a0 100644 --- a/src/roscha.c +++ b/src/roscha.c @@ -55,15 +55,15 @@ roscha_env_destroy_templates_cb(const struct slice *key, void *val) template_destroy(tmpl); } -#define eval_error(e, t, fmt, ...) \ - sds err = sdscatfmt(sdsempty(), "%s:%U:%U: "fmt, \ - e->internal->eval_tmpl->name, t.line, t.column, __VA_ARGS__); \ +#define eval_error(e, t, fmt, ...) \ + sds err = sdscatfmt(sdsempty(), "%s:%U:%U: " fmt, \ + e->internal->eval_tmpl->name, t.line, t.column, __VA_ARGS__); \ vector_push(e->errors, err) #define THERES_ERRORS env->errors->len > 0 static inline struct roscha_object *eval_expression(struct roscha_env *, - struct expression *); + struct expression *); static inline struct roscha_object * eval_prefix(struct roscha_env *env, struct prefix *pref) @@ -81,15 +81,15 @@ eval_prefix(struct roscha_env *env, struct prefix *pref) case TOKEN_MINUS: if (right->type != ROSCHA_INT) { eval_error(env, pref->token, - "operator '%s' can only be used with integer types", - token_type_print(pref->token.type)); + "operator '%s' can only be used with integer types", + token_type_print(pref->token.type)); } else { res = roscha_object_new(-right->integer); } break; - default:{ - eval_error(env, pref->token, "invalid prefix operator '%s'", - token_type_print(pref->token.type)); + default: { + eval_error(env, pref->token, "invalid prefix operator '%s'", + token_type_print(pref->token.type)); res = NULL; } } @@ -100,7 +100,7 @@ eval_prefix(struct roscha_env *env, struct prefix *pref) static inline struct roscha_object * eval_boolean_infix(struct roscha_env *env, struct token *op, - struct roscha_object *left, struct roscha_object *right) + struct roscha_object *left, struct roscha_object *right) { struct roscha_object *res = NULL; switch (op->type) { @@ -131,13 +131,13 @@ eval_boolean_infix(struct roscha_env *env, struct token *op, default: if (left->type != right->type) { eval_error(env, (*op), "types mismatch: %s %s %s", - roscha_type_print(left->type), token_type_print(op->type), - roscha_type_print(right->type)); + roscha_type_print(left->type), token_type_print(op->type), + roscha_type_print(right->type)); break; } eval_error(env, (*op), "bad operator: %s %s %s", - roscha_type_print(left->type), token_type_print(op->type), - roscha_type_print(right->type)); + roscha_type_print(left->type), token_type_print(op->type), + roscha_type_print(right->type)); break; } roscha_object_unref(left); @@ -148,7 +148,7 @@ eval_boolean_infix(struct roscha_env *env, struct token *op, static inline struct roscha_object * eval_integer_infix(struct roscha_env *env, struct token *op, - struct roscha_object *left, struct roscha_object *right) + struct roscha_object *left, struct roscha_object *right) { struct roscha_object *res; switch (op->type) { @@ -185,8 +185,7 @@ eval_infix(struct roscha_env *env, struct infix *inf) roscha_object_unref(left); return NULL; } - if (left->type == ROSCHA_INT && right->type == ROSCHA_INT) - { + if (left->type == ROSCHA_INT && right->type == ROSCHA_INT) { return eval_integer_infix(env, &inf->token, left, right); } @@ -201,13 +200,13 @@ eval_mapkey(struct roscha_env *env, struct indexkey *mkey) if (!map) return NULL; if (map->type != ROSCHA_HMAP) { eval_error(env, mkey->token, "expected %s type got %s", - roscha_type_print(ROSCHA_HMAP), - roscha_type_print(map->type)); + roscha_type_print(ROSCHA_HMAP), + roscha_type_print(map->type)); goto out; } if (mkey->key->type != EXPRESSION_IDENT) { eval_error(env, mkey->key->token, "bad map key '%s'", - token_type_print(mkey->key->token.type)); + token_type_print(mkey->key->token.type)); goto out; } res = hmap_gets(map->hmap, &mkey->key->token.literal); @@ -230,14 +229,14 @@ eval_index(struct roscha_env *env, struct indexkey *index) if (!vec) return NULL; if (vec->type != ROSCHA_VECTOR) { eval_error(env, index->token, "expected %s type got %s", - roscha_type_print(ROSCHA_VECTOR), - roscha_type_print(vec->type)); + roscha_type_print(ROSCHA_VECTOR), + roscha_type_print(vec->type)); goto out; } struct roscha_object *i = eval_expression(env, index->key); if (i->type != ROSCHA_INT) { eval_error(env, index->key->token, "bad vector key type %s", - roscha_type_print(ROSCHA_INT)); + roscha_type_print(ROSCHA_INT)); goto out2; } if (i->integer > (vec->vector->len - 1)) { @@ -307,7 +306,7 @@ eval_variable(struct roscha_env *env, sds r, struct variable *var) } static inline sds eval_subblocks(struct roscha_env *, sds r, - struct vector *blks); + struct vector *blks); static inline sds eval_branch(struct roscha_env *env, sds r, struct branch *br) @@ -329,18 +328,18 @@ eval_branch(struct roscha_env *env, sds r, struct branch *br) static inline sds eval_loop(struct roscha_env *env, sds r, struct loop *loop) { - struct roscha_object *loopv = roscha_object_new(hmap_new()); + struct roscha_object *loopv = roscha_object_new(hmap_new()); struct roscha_object *indexv = roscha_object_new(0); roscha_hmap_set(loopv, "index", indexv); - struct slice loopk = slice_whole("loop"); + struct slice loopk = slice_whole("loop"); struct roscha_object *outerloop = roscha_hmap_set(env->vars, "loop", loopv); - struct slice it = loop->item.token.literal; + struct slice it = loop->item.token.literal; struct roscha_object *outeritem = roscha_hmap_get(env->vars, &it); - struct roscha_object *seq = eval_expression(env, loop->seq); + struct roscha_object *seq = eval_expression(env, loop->seq); if (!seq) return r; if (seq->type == ROSCHA_VECTOR) { struct roscha_object *item; - vector_foreach(seq->vector, indexv->integer, item) { + vector_foreach (seq->vector, indexv->integer, item) { roscha_hmap_set(env->vars, it, item); r = eval_subblocks(env, r, loop->subblocks); roscha_hmap_unset(env->vars, &it); @@ -350,11 +349,11 @@ eval_loop(struct roscha_env *env, sds r, struct loop *loop) break; } } - } else if(seq->type == ROSCHA_HMAP) { - struct hmap_iter *iter = hmap_iter_new(seq->hmap); + } else if (seq->type == ROSCHA_HMAP) { + struct hmap_iter *iter = hmap_iter_new(seq->hmap); const struct slice *key; - void *val; - hmap_iter_foreach(iter, &key, &val) { + void *val; + hmap_iter_foreach (iter, &key, &val) { struct roscha_object *item = val; indexv->integer++; roscha_hmap_set(env->vars, it, item); @@ -368,9 +367,9 @@ eval_loop(struct roscha_env *env, sds r, struct loop *loop) } } else { eval_error(env, loop->seq->token, - "sequence should be of type %s or %s, got %s", - roscha_type_print(ROSCHA_VECTOR), - roscha_type_print(ROSCHA_HMAP), roscha_type_print(seq->type)); + "sequence should be of type %s or %s, got %s", + roscha_type_print(ROSCHA_VECTOR), + roscha_type_print(ROSCHA_HMAP), roscha_type_print(seq->type)); } if (outerloop) { @@ -393,7 +392,7 @@ eval_loop(struct roscha_env *env, sds r, struct loop *loop) static inline struct tblock * get_child_tblock(struct roscha_env *env, struct slice *name, - const struct template *tmpl) + const struct template *tmpl) { struct tblock *tblk = NULL; if (tmpl->child) { @@ -410,7 +409,7 @@ static inline sds eval_tblock(struct roscha_env *env, sds r, struct tblock *tblk) { struct tblock *child = get_child_tblock(env, &tblk->name.token.literal, - env->internal->eval_tmpl); + env->internal->eval_tmpl); if (child) { tblk = child; } @@ -428,9 +427,9 @@ eval_tag(struct roscha_env *env, sds r, struct tag *tag) return eval_loop(env, r, &tag->loop); case TAG_BLOCK: return eval_tblock(env, r, &tag->tblock); - case TAG_EXTENDS:{ + case TAG_EXTENDS: { eval_error(env, tag->token, "extends tag can only be the first tag", - tag->token); + tag->token); break; } case TAG_BREAK: @@ -447,19 +446,19 @@ static inline sds eval_block(struct roscha_env *env, sds r, struct block *blk) { switch (blk->type) { - case BLOCK_CONTENT: - return slice_string(&blk->token.literal, r); - case BLOCK_VARIABLE: - return eval_variable(env, r, &blk->variable); - case BLOCK_TAG: - return eval_tag(env, r, &blk->tag); + case BLOCK_CONTENT: + return slice_string(&blk->token.literal, r); + case BLOCK_VARIABLE: + return eval_variable(env, r, &blk->variable); + case BLOCK_TAG: + return eval_tag(env, r, &blk->tag); } } static inline sds eval_subblocks(struct roscha_env *env, sds r, struct vector *blks) { - size_t i; + size_t i; struct block *blk; vector_foreach (blks, i, blk) { r = eval_block(env, r, blk); @@ -472,18 +471,18 @@ eval_subblocks(struct roscha_env *env, sds r, struct vector *blks) static inline sds eval_template(struct roscha_env *env, const struct slice *name, - struct template *child) + struct template *child) { struct template *tmpl = hmap_gets(env->internal->templates, name); if (!tmpl) { sds errmsg = sdscat(sdsempty(), "template \""); - errmsg = slice_string(name, errmsg); - errmsg = sdscat(errmsg, "\" not found"); + errmsg = slice_string(name, errmsg); + errmsg = sdscat(errmsg, "\" not found"); vector_push(env->errors, errmsg); return NULL; } - tmpl->child = child; + tmpl->child = child; env->internal->eval_tmpl = tmpl; struct block *blk = tmpl->blocks->values[0]; @@ -493,7 +492,7 @@ eval_template(struct roscha_env *env, const struct slice *name, } sds r = sdsempty(); - r = eval_subblocks(env, r, tmpl->blocks); + r = eval_subblocks(env, r, tmpl->blocks); env->internal->eval_tmpl = NULL; @@ -515,11 +514,11 @@ roscha_deinit(void) struct roscha_env * roscha_env_new(void) { - struct roscha_env *env = calloc(1, sizeof(*env)); - env->internal = calloc(1, sizeof(*env->internal)); - env->vars = roscha_object_new(hmap_new()); + struct roscha_env *env = calloc(1, sizeof(*env)); + env->internal = calloc(1, sizeof(*env->internal)); + env->vars = roscha_object_new(hmap_new()); env->internal->templates = hmap_new(); - env->errors = vector_new(); + env->errors = vector_new(); return env; } @@ -527,8 +526,8 @@ roscha_env_new(void) bool roscha_env_add_template(struct roscha_env *env, char *name, char *body) { - struct parser *parser = parser_new(name, body); - struct template *tmpl = parser_parse_template(parser); + struct parser *parser = parser_new(name, body); + struct template *tmpl = parser_parse_template(parser); if (parser->errors->len > 0) { sds errmsg = NULL; while ((errmsg = vector_pop(parser->errors)) != NULL) { @@ -549,7 +548,7 @@ roscha_env_load_dir(struct roscha_env *env, const char *path) DIR *dir = opendir(path); if (!dir) { sds errmsg = sdscatfmt(sdsempty(), "unable to open dir %s, error %s", - path, strerror(errno)); + path, strerror(errno)); vector_push(env->errors, errmsg); return false; } @@ -560,10 +559,10 @@ roscha_env_load_dir(struct roscha_env *env, const char *path) } struct stat fstats; - sds fpath = sdscatfmt(sdsempty(), "%s/%s", path, ent->d_name); + sds fpath = sdscatfmt(sdsempty(), "%s/%s", path, ent->d_name); if (stat(fpath, &fstats)) { sds errmsg = sdscatfmt(sdsempty(), - "unable to stat file %s, error %s", fpath, strerror(errno)); + "unable to stat file %s, error %s", fpath, strerror(errno)); vector_push(env->errors, errmsg); closedir(dir); return false; @@ -573,18 +572,18 @@ roscha_env_load_dir(struct roscha_env *env, const char *path) char *name = malloc(strlen(ent->d_name) + 1); strcpy(name, ent->d_name); - int fd = open(fpath, O_RDONLY); - char buf[BUFSIZE]; - sds body = sdsempty(); + int fd = open(fpath, O_RDONLY); + char buf[BUFSIZE]; + sds body = sdsempty(); size_t nread; while ((nread = read(fd, buf, BUFSIZE)) > 0) { buf[nread] = '\0'; - body = sdscat(body, buf); + body = sdscat(body, buf); } close(fd); if (nread < 0) { sds errmsg = sdscatfmt(sdsempty(), - "unable to read file %s, error %s", fpath, strerror(errno)); + "unable to read file %s, error %s", fpath, strerror(errno)); vector_push(env->errors, errmsg); goto fileerr; } @@ -620,7 +619,7 @@ void roscha_env_destroy(struct roscha_env *env) { size_t i; - sds errmsg; + sds errmsg; vector_foreach (env->errors, i, errmsg) { sdsfree(errmsg); } |