From 3ee4fed8f3ec79c49a9244bd2c58604c470971f5 Mon Sep 17 00:00:00 2001 From: mtardy Date: Wed, 12 Aug 2020 22:29:45 +0200 Subject: [PATCH 1/3] Fix isHidden and add map_allkeys --- object.c | 25 ++++++++++++++++--------- 1 file changed, 16 insertions(+), 9 deletions(-) diff --git a/object.c b/object.c index 56696c8..3741212 100644 --- a/object.c +++ b/object.c @@ -374,14 +374,11 @@ oop map_append(oop map, oop value) } bool isHidden(oop obj) { - if (!is(Symbol, obj)) { - return false; - } - char *s = get(obj, Symbol, name); - size_t l = strlen(s); - // maybe 'l > 5' because of ____? - if (l > 4 && s[0] == '_' && s[1] == '_' && s[l-2] == '_' && s[l-1] == '_') { - return true; + if (is(Symbol, obj)) { + char *s = get(obj, Symbol, name); + size_t l = strlen(s); + // maybe 'l > 5' because of ____? + return (l > 4 && s[0] == '_' && s[1] == '_' && s[l-2] == '_' && s[l-1] == '_'); } return false; } @@ -398,6 +395,16 @@ oop map_keys(oop map) return keys; } +oop map_allKeys(oop map) +{ + assert(is(Map, map)); + oop keys = makeMap(); + for (size_t i = 0; i < get(map, Map, size); i++) { + map_append(keys, get(map, Map, elements)[i].key); + } + return keys; +} + oop map_values(oop map) { assert(is(Map, map)); @@ -451,7 +458,7 @@ void print(oop ast) printf("%i", getInteger(ast)); return; case String: - printf("'%s'", get(ast, String, value)); + printf("%s", get(ast, String, value)); return; case Symbol: printf("%s", get(ast, Symbol, name)); From 0b35fd37947d47839b09c28358863fc44655aa7e Mon Sep 17 00:00:00 2001 From: mtardy Date: Wed, 12 Aug 2020 22:30:25 +0200 Subject: [PATCH 2/3] Fix 'not' operator and add primitives keys and length --- parse.leg | 47 +++++++++++++++++++++++++++++++++++++---------- 1 file changed, 37 insertions(+), 10 deletions(-) diff --git a/parse.leg b/parse.leg index 65ef769..f28e85d 100644 --- a/parse.leg +++ b/parse.leg @@ -1318,12 +1318,15 @@ oop eval(oop scope, oop ast) BINARY(div, / ); BINARY(mod, % ); # undef BINARY + case t_not: { + oop rhs = eval(scope, map_get(ast, rhs_symbol)); + return makeInteger(isFalse(rhs)); + } # define UNARY(NAME, OPERATOR) \ case t_##NAME: { \ oop rhs = eval(scope, map_get(ast, rhs_symbol)); \ return makeInteger(OPERATOR getInteger(rhs)); \ } - UNARY(not, !); UNARY(neg, -); UNARY(com, ~); # undef UNARY @@ -1424,18 +1427,40 @@ oop prim_exit(oop params) { int status= 0; if (map_hasIntegerKey(params, 0)) { - oop arg= get(params, Map, elements)[0].value; - if (isInteger(arg)) status= getInteger(arg); + oop arg= get(params, Map, elements)[0].value; + if (isInteger(arg)) status= getInteger(arg); } exit(status); } +oop prim_keys(oop params) +{ + if (map_hasIntegerKey(params, 0)) { + oop arg= get(params, Map, elements)[0].value; + if (is(Map, arg)) return map_keys(arg); + } + return null; +} + +oop prim_length(oop params) +{ + if (map_hasIntegerKey(params, 0)) { + oop arg= get(params, Map, elements)[0].value; + switch (getType(arg)) { + case String: return makeInteger(strlen(get(arg, String, value))); + case Symbol: return makeInteger(strlen(get(arg, Symbol, name))); + case Map: return makeInteger(map_size(arg)); + default: break; + } + } + return null; +} + oop prim_invoke(oop params) { - oop scope= globals; if (map_hasIntegerKey(params, 0)) scope= get(params, Map, elements)[0].value; - oop this= null; if (map_hasIntegerKey(params, 1)) this= get(params, Map, elements)[1].value; - oop func= null; if (map_hasIntegerKey(params, 2)) func= get(params, Map, elements)[2].value; - oop args= null; if (map_hasIntegerKey(params, 3)) args= get(params, Map, elements)[3].value; + oop this= null; if (map_hasIntegerKey(params, 0)) this= get(params, Map, elements)[0].value; + oop func= null; if (map_hasIntegerKey(params, 1)) func= get(params, Map, elements)[1].value; + oop args= null; if (map_hasIntegerKey(params, 2)) args= get(params, Map, elements)[2].value; if (!is(Function, func)) { printf("cannot invoke "); println(func); @@ -1515,10 +1540,12 @@ int main(int argc, char **argv) symbol_table = makeMap(); globals = makeMap(); - map_set(globals, intern("exit") , makeFunction(prim_exit, null, null, globals)); - map_set(globals, intern("print"), makeFunction(prim_print, null, null, globals)); + 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("clone") , makeFunction(prim_clone, null, null, globals)); #define _DO(NAME) NAME##_symbol=intern(#NAME); DO_SYMBOLS() From 6905791c60a43d45fd0d3784d06120de2073bf98 Mon Sep 17 00:00:00 2001 From: mtardy Date: Wed, 12 Aug 2020 22:31:48 +0200 Subject: [PATCH 3/3] Add tests and update todo --- TODO.txt | 27 +++++++++++++++++++++++++++ test-object.txt | 39 +++++++++++++++++++++++++++++++++++++-- test3.txt | 20 ++++++++++++-------- 3 files changed, 76 insertions(+), 10 deletions(-) diff --git a/TODO.txt b/TODO.txt index 7bf42e8..ccc98a4 100644 --- a/TODO.txt +++ b/TODO.txt @@ -3,3 +3,30 @@ return break in switch, loops continue in loops + +getFunction(Object.print) +{ + __proto__ : { __name__ : Function }, + body: anAst, + parameters: aMap, + scope: aMap, +} + +makeFunction({ + __proto__ : { __name__ : Function }, + body: anAst, + parameters: aMap, + scope: aMap, +}) + + +var num = `4; // THESE ARE THE +var ast = `(3+@num); // IMPORTANT 2 LINES + +println(3 + @ast); // -> 10 +println(3 + (3+4)); // -> 10 + +syntax for () { + if (__arguments__[1] == #in) { } + else if (__arguments__[1] == #on) { } +} diff --git a/test-object.txt b/test-object.txt index b48b3db..5ebae4b 100644 --- a/test-object.txt +++ b/test-object.txt @@ -11,7 +11,7 @@ Object.new = fun () { var obj= { __proto__ : this }; var init= this.init; print("INIT is ", init); - init && invoke(null, obj, init, __arguments__); + init && invoke(obj, init, __arguments__); obj; }; @@ -26,4 +26,39 @@ Point.init = fun (x, y) { this.y = y; } -print("Point.new(3, 4) is ", Point.new(3, 4)); +var p = Point.new(3, 4); + +print("Point.new(3, 4) is ", p); + + + +Object.clone = fun () { clone(this) } + +var q = p.clone(); + +print("clone is ", q); + +Object.println = fun () { this.print(); print('\n'); this; } + +Object.print = fun () { + var proto= this.__proto__; + if (!proto) print(this); + else { + var name= proto.__name__; + if (!name) print(this); + else { + print(name, "{"); + var keys= keys(this); + for (var i= 0; i < length(keys); ++i) { + var key= keys[i]; + var val= this[key]; + if (i) print(", "); + print(" ", key, ": ", val); + } + print(" }"); + } + } + this; +} + +p.println() \ No newline at end of file diff --git a/test3.txt b/test3.txt index 7a52b60..f2bd8e1 100644 --- a/test3.txt +++ b/test3.txt @@ -1,8 +1,12 @@ -fun f(n) { - if (n < 2) { - return 1 - } else { - return 1 + f(n-1) + f(n-2) - } -} -f(27) \ No newline at end of file +var o = {} +o.a = 12 +o.b = "salut" +var c = clone(o, "12"); + +c.a = 24 +c.b = "au revoir" +c.chips = "on va manger, des chips!!!" + + +o +c \ No newline at end of file