Browse Source

Add name to Function struct to print them nicely

pull/8/head
mtardy 4 years ago
parent
commit
fafec91e1e
2 changed files with 16 additions and 10 deletions
  1. +9
    -3
      object.c
  2. +7
    -7
      parse.leg

+ 9
- 3
object.c View File

@ -91,6 +91,7 @@ typedef oop (*primitive_t)(oop params);
struct Function { struct Function {
type_t type; type_t type;
primitive_t primitive; primitive_t primitive;
char *name;
oop body; oop body;
oop param; oop param;
oop parentScope; oop parentScope;
@ -158,7 +159,7 @@ oop _checkType(oop ptr, type_t type, char *file, int line)
return ptr; return ptr;
} }
// added parens around expansion to protect assignment
// added parens around e/makexpansion to protect assignment
#define get(PTR, TYPE, FIELD) (_checkType(PTR, TYPE, __FILE__, __LINE__)->TYPE.FIELD) #define get(PTR, TYPE, FIELD) (_checkType(PTR, TYPE, __FILE__, __LINE__)->TYPE.FIELD)
#define set(PTR, TYPE, FIELD, VALUE) (_checkType(PTR, TYPE, __FILE__, __LINE__)->TYPE.FIELD = VALUE) #define set(PTR, TYPE, FIELD, VALUE) (_checkType(PTR, TYPE, __FILE__, __LINE__)->TYPE.FIELD = VALUE)
@ -218,11 +219,12 @@ oop makeSymbol(char *name)
return newSymb; return newSymb;
} }
oop makeFunction(primitive_t primitive, oop param, oop body, oop parentScope)
oop makeFunction(primitive_t primitive, char * name, oop param, oop body, oop parentScope)
{ {
oop newFunc = memcheck(malloc(sizeof(union object))); oop newFunc = memcheck(malloc(sizeof(union object)));
newFunc->type = Function; newFunc->type = Function;
newFunc->Function.primitive = primitive; newFunc->Function.primitive = primitive;
newFunc->Function.name = memcheck(strdup(name));
newFunc->Function.param = param; newFunc->Function.param = param;
newFunc->Function.body = body; newFunc->Function.body = body;
newFunc->Function.parentScope = parentScope; newFunc->Function.parentScope = parentScope;
@ -464,7 +466,11 @@ void print(oop ast)
printf("%s", get(ast, Symbol, name)); printf("%s", get(ast, Symbol, name));
return; return;
case Function: case Function:
printf("Function@%p", get(ast, Function, primitive));
if (get(ast, Function, primitive) == NULL) {
printf("Function:%s", get(ast, Function, name));
} else {
printf("Primitive:%s@%p", get(ast, Function, name), get(ast, Function, primitive));
}
return; return;
case Map: case Map:
map_print(ast, 0); map_print(ast, 0);

+ 7
- 7
parse.leg View File

@ -1084,7 +1084,7 @@ oop eval(oop scope, oop ast)
oop name = map_get(ast, name_symbol); oop name = map_get(ast, name_symbol);
oop param = map_get(ast, param_symbol); oop param = map_get(ast, param_symbol);
oop body = map_get(ast, body_symbol); oop body = map_get(ast, body_symbol);
oop func = makeFunction(NULL, param, body, scope);
oop func = makeFunction(NULL, get(name, Symbol, name), param, body, scope);
if (opt_v) { if (opt_v) {
printf("funcscope: "); printf("funcscope: ");
println(scope); println(scope);
@ -1558,12 +1558,12 @@ int main(int argc, char **argv)
symbol_table = makeMap(); symbol_table = makeMap();
globals = makeMap(); globals = makeMap();
map_set(globals, intern("exit") , makeFunction(prim_exit, null, null, globals));
map_set(globals, intern("keys") , makeFunction(prim_keys, null, null, globals));
map_set(globals, intern("length"), makeFunction(prim_length, null, null, globals));
map_set(globals, intern("print") , makeFunction(prim_print, null, null, globals));
map_set(globals, intern("invoke"), makeFunction(prim_invoke, null, null, globals));
map_set(globals, intern("clone") , makeFunction(prim_clone, null, null, globals));
map_set(globals, intern("exit") , makeFunction(prim_exit, "exit", null, null, globals));
map_set(globals, intern("keys") , makeFunction(prim_keys, "keys", null, null, globals));
map_set(globals, intern("length"), makeFunction(prim_length, "length", null, null, globals));
map_set(globals, intern("print") , makeFunction(prim_print, "print", null, null, globals));
map_set(globals, intern("invoke"), makeFunction(prim_invoke, "invoke", null, null, globals));
map_set(globals, intern("clone") , makeFunction(prim_clone, "clone", null, null, globals));
#define _DO(NAME) NAME##_symbol=intern(#NAME); #define _DO(NAME) NAME##_symbol=intern(#NAME);
DO_SYMBOLS() DO_SYMBOLS()

Loading…
Cancel
Save