Ver código fonte

Add a scope parameter in intern function

pull/5/head
mtardy 4 anos atrás
pai
commit
24949bad35
3 arquivos alterados com 13 adições e 15 exclusões
  1. +6
    -5
      calc.leg
  2. +6
    -9
      object.c
  3. +1
    -1
      test2.txt

+ 6
- 5
calc.leg Ver arquivo

@ -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);

+ 6
- 9
object.c Ver arquivo

@ -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;
}

+ 1
- 1
test2.txt Ver arquivo

@ -66,6 +66,6 @@ sys.id = "i am sys obj"
"checkpoint 10"
sys.bye = exit
"checkpoint 10"
"checkpoint 11"
sys.bye(1,2,3)

Carregando…
Cancelar
Salvar