@@ -12,37 +12,8 @@ import polemy.value; import std.typecons; import std.stdio; -// [todo] move to value.d - -FunValue nativef(Value delegate(immutable LexPosition pos, Layer lay, Value[] args) dg) -{ - return new FunValue(dg); -} - -FunValue native(R,T...)(R delegate (T) dg) -{ - return nativef( delegate Value(immutable LexPosition pos, Layer lay, Value[] args) { - if( lay != "@v" ) - throw genex!RuntimeException(pos, "only @v layer can call native function"); - if( T.length != args.length ) - throw genex!RuntimeException(pos, "argument number mismatch!"); - T typed_args; - foreach(i, Ti; T) - { - typed_args[i] = cast(Ti) args[i]; - if( typed_args[i] is null ) - throw genex!RuntimeException(pos, sprintf!"type mismatch on the argument %d"(i+1)); - } - try { - return dg(typed_args); - } catch( RuntimeException e ) { - throw e.pos is null ? new RuntimeException(pos, e.msg, e.file, e.line) : e; - } - }); -} - /// Table createGlobalContext() { auto ctx = new Table; @@ -77,8 +48,23 @@ ctx.set("_isint", "@v", native( (Value v){return new IntValue(BigInt(cast(IntValue)v is null ? 0 : 1));} )); ctx.set("_isstr", "@v", native( (Value v){return new IntValue(BigInt(cast(StrValue)v is null ? 0 : 1));} )); ctx.set("_isfun", "@v", native( (Value v){return new IntValue(BigInt(cast(FunValue)v is null ? 0 : 1));} )); ctx.set("_isundefined", "@v", native( (Value v){return new IntValue(BigInt(cast(UndValue)v is null ? 0 : 1));} )); + ctx.set("_istable", "@v", native( (Value v){return new IntValue(BigInt(cast(Table)v is null ? 0 : 1));} )); + ctx.set(".", "@v", native( (Table t, StrValue s){ + return (t.has(s.data, "@v") ? t.get(s.data, "@v") : new UndValue); + }) ); + ctx.set(".?", "@v", native( (Table t, StrValue s){ + return new IntValue(BigInt(t.has(s.data, "@v") ? 1 : 0)); + }) ); + ctx.set(".=", "@v", native( (Table t, StrValue s, Value v){ + auto t2 = new Table(t, Table.Kind.NotPropagateSet); + t2.set(s.data, "@v", v); + return t2; + }) ); + ctx.set("{}", "@v", native( (){ + return new Table; + }) ); return ctx; } /// Entry point of this module @@ -260,5 +246,5 @@ unittest { assert_eq( evalString(`@@t = fun(x){x+1}; @t(123)`).val, new IntValue(BigInt(124)) ); -} +}