瀏覽代碼

Merge pull request #7 from mtardy/fixes2

Various fixes
pull/8/head
mtardy 4 年之前
committed by GitHub
父節點
當前提交
ee6f0d3497
沒有發現已知的金鑰在資料庫的簽署中 GPG Key ID: 4AEE18F83AFDEB23
共有 5 個文件被更改,包括 129 次插入29 次删除
  1. +27
    -0
      TODO.txt
  2. +16
    -9
      object.c
  3. +37
    -10
      parse.leg
  4. +37
    -2
      test-object.txt
  5. +12
    -8
      test3.txt

+ 27
- 0
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) { }
}

+ 16
- 9
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));

+ 37
- 10
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()

+ 37
- 2
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()

+ 12
- 8
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)
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

Loading…
取消
儲存