From ccd2bb723fbfa97cff803ad38154a3194ce01690 Mon Sep 17 00:00:00 2001 From: Christian Barthel Date: Fri, 5 Jul 2019 09:08:02 +0200 Subject: [PATCH] cleanup, comments added - add comments and underscores for symbol names - remove printf --- tokenize.l | 100 ++++++++++------------------------------------------- 1 file changed, 19 insertions(+), 81 deletions(-) diff --git a/tokenize.l b/tokenize.l index 302dd9b..b640a11 100644 --- a/tokenize.l +++ b/tokenize.l @@ -97,9 +97,10 @@ char *yystr; t { yylval = 1; return BOOL;} nil { yylval = 0; return BOOL; } [0-9]+ { yylval = atoi(yytext); return NUM; } +;.* { /* comment */ } [ \t\n] { /* ignore white space */ } \"[^\"]*\" { return STR; } -[a-zA-Z][a-zA-Z0-9]* { return SYM; } +[a-zA-Z][_a-zA-Z0-9]* { return SYM; } . { err(1, "invalid symbol: %s\n", yytext); } %% @@ -306,9 +307,12 @@ lookup(struct env *e, char *name) { if (e == NULL) return NULL; - for (struct env *t = e; t != NULL; t = t->next) + for (struct env *t = e; t != NULL; t = t->next) { + fprintf(stderr, "env: %s\n", t->name); if (strcmp(t->name, name) == 0) return t->a; + } + fprintf(stderr, "====\n"); return lookup(e->parent, name); } @@ -406,7 +410,6 @@ eval_nand(struct ast *a, struct env *e) struct ast * eval_add(struct ast *a, struct env *e) { - fprintf(stderr, "add?\n"); reassure(a != NULL && a->next != NULL && a->next->next != NULL && @@ -509,19 +512,6 @@ eval_internal_sym(struct ast *a, struct env *e) l->v.list = head; l->next = a->next; head->v.token = a->v.token; - - - fprintf(stderr, "====\n"); - fprintf(stderr, "%d\n", a->v.token); - fprintf(stderr, "====\n"); - fprintf(stderr, "%d\n", a->type); - fprintf(stderr, "%s\n", a->v.token->v.str); - fprintf(stderr, "%d\n", a->v.token->next->type); - fprintf(stderr, "%d\n", a->v.token->next->next->type); - fprintf(stderr, "%s\n", a->v.token->next->next->v.str); - fprintf(stderr, "%d\n", a->v.token->next->next->next->type); - fprintf(stderr, "%d\n", a->v.token->next->next->next->next->type); - fprintf(stderr, "====\n"); return l; } else if (strcmp(a->v.token->v.str, "if") == 0) { return eval_if(a, e); @@ -562,16 +552,13 @@ bind_args(struct token *args, struct ast *a = fnargs_values; struct token *t = args; - fprintf(stderr, "args: %d\n", fnargs_values->type); - fprintf(stderr, "args: %d\n", fnargs_values->v.token->type); - struct env *newenv = (struct env*) + struct env *newenv = (struct env*) calloc(1, sizeof(struct env)); + newenv->name = strdup("__fn"); newenv->parent = e; while (t != NULL && t->type != RPAR) { - fprintf(stderr, "args assign: %d, %s [%d]\n", - t->type, t->v.str, a->type); reassure(t != NULL && t->type == SYM && t->v.str != NULL, @@ -583,7 +570,7 @@ bind_args(struct token *args, __func__, t->v.str); struct ast *new = eval(a, e); - fprintf(stderr, "evaluated: %d, %d (ast,token)\n", new->type, new->v.token->type); + append(newenv, make_env(t->v.str, new)); t = t->next; @@ -607,8 +594,6 @@ eval_fn_call(struct ast *a, struct env *e) struct ast *fn_body; fnargs_values = a->next; - fprintf(stderr, "fnargs: %d\n", fnargs_values->type); - fprintf(stderr, "fnargs: %d\n", fnargs_values->type); reassure(fnargs_values != NULL, "%s: expected RPAR or function args", __func__); @@ -617,27 +602,25 @@ eval_fn_call(struct ast *a, struct env *e) a->v.token->type == SYM, "%s: expected SYM, is: %d", __func__, a->type); - /* XXX fprintf(stderr, "eval args: %d\n", fnargs_values->v.token->type); */ char *fnstr = a->v.token->v.str; struct ast *fn = lookup(e, fnstr); - fprintf(stderr, "_lookup: %s\n", fnstr); + reassure(fn != NULL && fn->type == AST_LIST, "%s: lookup on `%s` failed", __func__, fnstr); fn_head = fn->v.list; fn_body = fn->next->next; - - fprintf(stderr, "fnbody: %d, %p\n", fn_body->type, fn_body->v.list); - fprintf(stderr, "fnbody: %d, %d\n", fn_body->v.list->type, fn_body->v.list->v.token->type); - fprintf(stderr, "fnbody: %s\n", fn_body->v.list->v.token->v.str); - } else if (a->type == AST_LIST) { /* direct lambda? */ - fprintf(stderr, "looks like a list..\n"); - - //debug_ast(a->v.list->next->next, 0); + } else if (a->type == AST_LIST + && strcmp(a->v.list->v.token->v.str, "lm") == 0) { + /* ((lm () ) args) */ fn_head = a->v.list; fn_body = a->v.list->next->next; - + } else { + /* xxx */ + struct ast *fn = eval(a, e); + fn_head = fn->v.list; + fn_body = fn->next->next; } reassure(fn_body != NULL && fn_body->type == AST_LIST, @@ -656,53 +639,13 @@ eval_fn_call(struct ast *a, struct env *e) "%s: args expected to be a list", __func__); struct env *newenv = bind_args(fnargs, fnargs_values, e); + for (struct env *e = newenv; e; e=e->next) fprintf(stderr, "e: %s\n", e->name); reassure(newenv != NULL, "%s: setup for newenv failed", __func__); - fprintf(stderr, "?hier\n"); return eval(fn_body, newenv); - /* fnargs: (), (a), (a b), (a b c), ... */ - /* fprintf(stderr, "args: %d %s\n", fnargs->type, ""); */ - /* fprintf(stderr, "args: %d %s\n", fnargs->next->type, */ - /* fnargs->next->v.str); */ - /* fprintf(stderr, "args: %d %s\n", */ - /* fnargs->next->next->type, */ - /* fnargs->next->next->v.str */ - /* ); */ - /* fprintf(stderr, "args: %d %s\n", fnargs->next->next->next->type, ""); */ - - /* ((lm (x y z) (...)) 1 2 3) */ - - /* fprintf(stderr, "now evaluate and bind parameter arguments %p\n", fnargs); */ - /* fprintf(stderr, "args: %d %p\n", fnargs->type, fnargs->v.token); */ - /* fprintf(stderr, "args: %d %p\n", fnargs->v.token->next->type, fnargs->v.list); */ - - /* fprintf(stderr, "type: %d\n", fn->type); */ - /* fprintf(stderr, "token p: %p\n", fn->v.token); */ - /* fprintf(stderr, "list p:%p\n", fn->v.list); */ - - /* fprintf(stderr, "list: nxt type: %d\n", fn->v.list->type); /\* sym *\/ */ - /* fprintf(stderr, "list: nxt token type: %d\n", fn->v.list->v.token->type); /\* sym *\/ */ - /* fprintf(stderr, "list: nxt token type: %s\n", fn->v.list->v.token->v.str); /\* sym *\/ */ - /* fprintf(stderr, "nxt tok p: %p\n", fn->list->v.token); /\* sym *\/ */ - /* fprintf(stderr, "nxt list p: %s\n", fn->list->v.list); /\* sym *\/ */ - - - /* fprintf(stderr, "nxt type: %d\n", fn->next->type); /\* sym *\/ */ - /* fprintf(stderr, "nxt tok p: %p\n", fn->next->v.token); /\* sym *\/ */ - /* fprintf(stderr, "nxt list p: %s\n", fn->next->v.list); /\* sym *\/ */ - - /* reassure(fn != NULL && */ - /* fn->type == AST_LIST && */ - /* fn->v.list != NULL && */ - /* fn->v.list->type == AST_TOK && */ - /* fn->v.list->v.token->type == SYM && */ - /* (strcmp(fn->v.list->v.token->v.str, "lm") == 0), "broken"); */ - - /* fprintf(stderr, "type %d\n", a->type); */ - /* fprintf(stderr, "type %s\n", a->v.list->v.token->v.str); */ } struct ast * @@ -731,10 +674,6 @@ eval(struct ast *a, struct env *e) else if (token_is_bool(a->v.token)) return make_ast(AST_TOK, a->v.token); case AST_LIST: - /* XXX either it's an internal function, a custom - * defined function or an lambda expression - * ((lm (x) (* x x)) 2) - */ reassure ((a->v.list->type == AST_TOK && a->v.list->v.token->type == SYM) || (a->v.list->type == AST_LIST), @@ -810,7 +749,6 @@ int main(void) return 0; } - /* * Local Variables: * mode: c;