diff --git a/calc.leg b/calc.leg index 0bb8816..e62b8aa 100644 --- a/calc.leg +++ b/calc.leg @@ -16,7 +16,8 @@ #include "object.c" -//oop globals; +// this is the global scope +oop globals; // this should stay out of object.c because it manipulates Symbol members defined in this file @@ -59,7 +60,7 @@ prefix = MINUS n:prefix { set(n, Integer, value, -get(n, Integer | PLUS n:prefix { $$ = n } | n:postfix { $$ = n } -postfix = i:value ( DOT s:IDENT p:parameterList { map_set(p, intern("this"), i); i = apply(map_get(i, s), p) } +postfix = i:value ( DOT s:IDENT p:parameterList { map_set(p, intern(globals, "this"), i); i = apply(map_get(i, s), p) } | DOT s:IDENT !EQUAL { i = map_get(i, s) } | LBRAC p:exp RBRAC !EQUAL { i = map_get(i, p) } | p:parameterList { i = apply(i, p) } @@ -84,7 +85,7 @@ string = SQUOTE < (!SQUOTE .)* > SQUOTE { $$ = makeString(yytext) } | DQUOTE < (!DQUOTE .)* > DQUOTE { $$ = makeString(yytext) } symbol = HASH ( i:IDENT { $$ = i } - | i:string { $$ = intern(get(i, String, value)) } + | i:string { $$ = intern(globals, get(i, String, value)) } ) map = LCB m:makeMap @@ -99,7 +100,7 @@ makeMap = { $$ = makeMap() } - = [ \t\n\r]* | "//" ( ![\n\r] . )* -IDENT = < [a-zA-Z][a-zA-Z0-9_]* > - { $$ = intern(yytext) } +IDENT = < [a-zA-Z][a-zA-Z0-9_]* > - { $$ = intern(globals, yytext) } NUMBER = < [0-9]+ > - { $$ = makeInteger(atoi(yytext)) } HASH = '#' - PLUS = '+' - @@ -135,7 +136,7 @@ int main(int argc, char **argv) { globals = makeMap(); - set(intern("exit"), Symbol, value, makeFunction(prim_exit)); // map_set(globals, intern("exit"), makeFunction(prim_exit)); + set(intern(globals, "exit"), Symbol, value, makeFunction(prim_exit)); // map_set(globals, intern("exit"), makeFunction(prim_exit)); while (yyparse()) { println(yylval); diff --git a/object.c b/object.c index c591621..05347d4 100644 --- a/object.c +++ b/object.c @@ -194,7 +194,7 @@ oop map_get(oop map, oop key) return get(map, Map, elements)[pos].value; } -#define MAP_CHUNK_SIZE 4 +#define MAP_CHUNK_SIZE 8 oop map_insert(oop map, oop key, oop value, size_t pos) { @@ -301,17 +301,14 @@ void println(oop ast) printf("\n"); } -oop globals; - -oop intern(char *ident) +oop intern(oop scope, char *ident) { + assert(is(Map, scope)); oop symbol = makeSymbol(memcheck(strdup(ident))); - ssize_t pos = map_search(globals, symbol); - if (pos >= 0) { - return get(globals, Map, elements)[pos].key; // So it this case symbol will be garbage collected right? - } + ssize_t pos = map_search(scope, symbol); + if (pos >= 0) return get(scope, Map, elements)[pos].key; // So it this case, symbol will be garbage collected right? pos = -1 - pos; // 'un-negate' the result by reflecting it around X=-1 - map_insert(globals, symbol, null, pos); + map_insert(scope, symbol, null, pos); return symbol; } \ No newline at end of file diff --git a/test2.txt b/test2.txt index 7328b4b..190ffd5 100644 --- a/test2.txt +++ b/test2.txt @@ -66,6 +66,6 @@ sys.id = "i am sys obj" "checkpoint 10" sys.bye = exit -"checkpoint 10" +"checkpoint 11" sys.bye(1,2,3)