cleanup, comments added
- add comments and underscores for symbol names - remove printf
This commit is contained in:
parent
516c2f346d
commit
ccd2bb723f
100
tokenize.l
100
tokenize.l
|
@ -97,9 +97,10 @@ char *yystr;
|
||||||
t { yylval = 1; return BOOL;}
|
t { yylval = 1; return BOOL;}
|
||||||
nil { yylval = 0; return BOOL; }
|
nil { yylval = 0; return BOOL; }
|
||||||
[0-9]+ { yylval = atoi(yytext); return NUM; }
|
[0-9]+ { yylval = atoi(yytext); return NUM; }
|
||||||
|
;.* { /* comment */ }
|
||||||
[ \t\n] { /* ignore white space */ }
|
[ \t\n] { /* ignore white space */ }
|
||||||
\"[^\"]*\" { return STR; }
|
\"[^\"]*\" { 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); }
|
. { err(1, "invalid symbol: %s\n", yytext); }
|
||||||
%%
|
%%
|
||||||
|
|
||||||
|
@ -306,9 +307,12 @@ lookup(struct env *e, char *name)
|
||||||
{
|
{
|
||||||
if (e == NULL)
|
if (e == NULL)
|
||||||
return 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)
|
if (strcmp(t->name, name) == 0)
|
||||||
return t->a;
|
return t->a;
|
||||||
|
}
|
||||||
|
fprintf(stderr, "====\n");
|
||||||
return lookup(e->parent, name);
|
return lookup(e->parent, name);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -406,7 +410,6 @@ eval_nand(struct ast *a, struct env *e)
|
||||||
struct ast *
|
struct ast *
|
||||||
eval_add(struct ast *a, struct env *e)
|
eval_add(struct ast *a, struct env *e)
|
||||||
{
|
{
|
||||||
fprintf(stderr, "add?\n");
|
|
||||||
reassure(a != NULL &&
|
reassure(a != NULL &&
|
||||||
a->next != NULL &&
|
a->next != NULL &&
|
||||||
a->next->next != NULL &&
|
a->next->next != NULL &&
|
||||||
|
@ -509,19 +512,6 @@ eval_internal_sym(struct ast *a, struct env *e)
|
||||||
l->v.list = head;
|
l->v.list = head;
|
||||||
l->next = a->next;
|
l->next = a->next;
|
||||||
head->v.token = a->v.token;
|
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;
|
return l;
|
||||||
} else if (strcmp(a->v.token->v.str, "if") == 0) {
|
} else if (strcmp(a->v.token->v.str, "if") == 0) {
|
||||||
return eval_if(a, e);
|
return eval_if(a, e);
|
||||||
|
@ -562,16 +552,13 @@ bind_args(struct token *args,
|
||||||
|
|
||||||
struct ast *a = fnargs_values;
|
struct ast *a = fnargs_values;
|
||||||
struct token *t = args;
|
struct token *t = args;
|
||||||
fprintf(stderr, "args: %d\n", fnargs_values->type);
|
struct env *newenv = (struct env*)
|
||||||
fprintf(stderr, "args: %d\n", fnargs_values->v.token->type);
|
|
||||||
struct env *newenv = (struct env*)
|
|
||||||
calloc(1, sizeof(struct env));
|
calloc(1, sizeof(struct env));
|
||||||
|
|
||||||
newenv->name = strdup("__fn");
|
newenv->name = strdup("__fn");
|
||||||
newenv->parent = e;
|
newenv->parent = e;
|
||||||
|
|
||||||
while (t != NULL && t->type != RPAR) {
|
while (t != NULL && t->type != RPAR) {
|
||||||
fprintf(stderr, "args assign: %d, %s [%d]\n",
|
|
||||||
t->type, t->v.str, a->type);
|
|
||||||
reassure(t != NULL &&
|
reassure(t != NULL &&
|
||||||
t->type == SYM &&
|
t->type == SYM &&
|
||||||
t->v.str != NULL,
|
t->v.str != NULL,
|
||||||
|
@ -583,7 +570,7 @@ bind_args(struct token *args,
|
||||||
__func__, t->v.str);
|
__func__, t->v.str);
|
||||||
|
|
||||||
struct ast *new = eval(a, e);
|
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));
|
append(newenv, make_env(t->v.str, new));
|
||||||
|
|
||||||
t = t->next;
|
t = t->next;
|
||||||
|
@ -607,8 +594,6 @@ eval_fn_call(struct ast *a, struct env *e)
|
||||||
struct ast *fn_body;
|
struct ast *fn_body;
|
||||||
|
|
||||||
fnargs_values = a->next;
|
fnargs_values = a->next;
|
||||||
fprintf(stderr, "fnargs: %d\n", fnargs_values->type);
|
|
||||||
fprintf(stderr, "fnargs: %d\n", fnargs_values->type);
|
|
||||||
reassure(fnargs_values != NULL,
|
reassure(fnargs_values != NULL,
|
||||||
"%s: expected RPAR or function args", __func__);
|
"%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,
|
a->v.token->type == SYM,
|
||||||
"%s: expected SYM, is: %d",
|
"%s: expected SYM, is: %d",
|
||||||
__func__, a->type);
|
__func__, a->type);
|
||||||
/* XXX fprintf(stderr, "eval args: %d\n", fnargs_values->v.token->type); */
|
|
||||||
|
|
||||||
char *fnstr = a->v.token->v.str;
|
char *fnstr = a->v.token->v.str;
|
||||||
struct ast *fn = lookup(e, fnstr);
|
struct ast *fn = lookup(e, fnstr);
|
||||||
fprintf(stderr, "_lookup: %s\n", fnstr);
|
|
||||||
reassure(fn != NULL && fn->type == AST_LIST,
|
reassure(fn != NULL && fn->type == AST_LIST,
|
||||||
"%s: lookup on `%s` failed", __func__, fnstr);
|
"%s: lookup on `%s` failed", __func__, fnstr);
|
||||||
|
|
||||||
fn_head = fn->v.list;
|
fn_head = fn->v.list;
|
||||||
fn_body = fn->next->next;
|
fn_body = fn->next->next;
|
||||||
|
} else if (a->type == AST_LIST
|
||||||
fprintf(stderr, "fnbody: %d, %p\n", fn_body->type, fn_body->v.list);
|
&& strcmp(a->v.list->v.token->v.str, "lm") == 0) {
|
||||||
fprintf(stderr, "fnbody: %d, %d\n", fn_body->v.list->type, fn_body->v.list->v.token->type);
|
/* ((lm (<param>) <body>) args) */
|
||||||
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);
|
|
||||||
fn_head = a->v.list;
|
fn_head = a->v.list;
|
||||||
fn_body = a->v.list->next->next;
|
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,
|
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__);
|
"%s: args expected to be a list", __func__);
|
||||||
|
|
||||||
struct env *newenv = bind_args(fnargs, fnargs_values, e);
|
struct env *newenv = bind_args(fnargs, fnargs_values, e);
|
||||||
|
|
||||||
for (struct env *e = newenv; e; e=e->next)
|
for (struct env *e = newenv; e; e=e->next)
|
||||||
fprintf(stderr, "e: %s\n", e->name);
|
fprintf(stderr, "e: %s\n", e->name);
|
||||||
|
|
||||||
reassure(newenv != NULL,
|
reassure(newenv != NULL,
|
||||||
"%s: setup for newenv failed", __func__);
|
"%s: setup for newenv failed", __func__);
|
||||||
fprintf(stderr, "?hier\n");
|
|
||||||
return eval(fn_body, newenv);
|
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 *
|
struct ast *
|
||||||
|
@ -731,10 +674,6 @@ eval(struct ast *a, struct env *e)
|
||||||
else if (token_is_bool(a->v.token))
|
else if (token_is_bool(a->v.token))
|
||||||
return make_ast(AST_TOK, a->v.token);
|
return make_ast(AST_TOK, a->v.token);
|
||||||
case AST_LIST:
|
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 &&
|
reassure ((a->v.list->type == AST_TOK &&
|
||||||
a->v.list->v.token->type == SYM) ||
|
a->v.list->v.token->type == SYM) ||
|
||||||
(a->v.list->type == AST_LIST),
|
(a->v.list->type == AST_LIST),
|
||||||
|
@ -810,7 +749,6 @@ int main(void)
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Local Variables:
|
* Local Variables:
|
||||||
* mode: c;
|
* mode: c;
|
||||||
|
|
Loading…
Reference in New Issue