Diff
Not logged in

Differences From Artifact [5c2f449580dd9555]:

To Artifact [6db0ce492da06d00]:


9 9 import polemy.lex : LexPosition; 10 10 import polemy.ast; 11 11 import polemy.parse; 12 12 import polemy.value; 13 13 import std.typecons; 14 14 import std.stdio; 15 15 16 -// [todo] move to value.d 17 - 18 -FunValue nativef(Value delegate(immutable LexPosition pos, Layer lay, Value[] args) dg) 19 -{ 20 - return new FunValue(dg); 21 -} 22 - 23 -FunValue native(R,T...)(R delegate (T) dg) 24 -{ 25 - return nativef( delegate Value(immutable LexPosition pos, Layer lay, Value[] args) { 26 - if( lay != "@v" ) 27 - throw genex!RuntimeException(pos, "only @v layer can call native function"); 28 - if( T.length != args.length ) 29 - throw genex!RuntimeException(pos, "argument number mismatch!"); 30 - T typed_args; 31 - foreach(i, Ti; T) 32 - { 33 - typed_args[i] = cast(Ti) args[i]; 34 - if( typed_args[i] is null ) 35 - throw genex!RuntimeException(pos, sprintf!"type mismatch on the argument %d"(i+1)); 36 - } 37 - try { 38 - return dg(typed_args); 39 - } catch( RuntimeException e ) { 40 - throw e.pos is null ? new RuntimeException(pos, e.msg, e.file, e.line) : e; 41 - } 42 - }); 43 -} 44 - 45 16 /// 46 17 Table createGlobalContext() 47 18 { 48 19 auto ctx = new Table; 49 20 ctx.set("+", "@v", native( (IntValue lhs, IntValue rhs){return new IntValue(lhs.data + rhs.data);} )); 50 21 ctx.set("-", "@v", native( (IntValue lhs, IntValue rhs){return new IntValue(lhs.data - rhs.data);} )); 51 22 ctx.set("*", "@v", native( (IntValue lhs, IntValue rhs){return new IntValue(lhs.data * rhs.data);} )); ................................................................................ 74 45 return (x.data == 0 ? fe : ft).call(pos,lay,[]); 75 46 throw genex!RuntimeException(pos, "type mismatch in if"); 76 47 })); 77 48 ctx.set("_isint", "@v", native( (Value v){return new IntValue(BigInt(cast(IntValue)v is null ? 0 : 1));} )); 78 49 ctx.set("_isstr", "@v", native( (Value v){return new IntValue(BigInt(cast(StrValue)v is null ? 0 : 1));} )); 79 50 ctx.set("_isfun", "@v", native( (Value v){return new IntValue(BigInt(cast(FunValue)v is null ? 0 : 1));} )); 80 51 ctx.set("_isundefined", "@v", native( (Value v){return new IntValue(BigInt(cast(UndValue)v is null ? 0 : 1));} )); 52 + ctx.set("_istable", "@v", native( (Value v){return new IntValue(BigInt(cast(Table)v is null ? 0 : 1));} )); 53 + ctx.set(".", "@v", native( (Table t, StrValue s){ 54 + return (t.has(s.data, "@v") ? t.get(s.data, "@v") : new UndValue); 55 + }) ); 56 + ctx.set(".?", "@v", native( (Table t, StrValue s){ 57 + return new IntValue(BigInt(t.has(s.data, "@v") ? 1 : 0)); 58 + }) ); 59 + ctx.set(".=", "@v", native( (Table t, StrValue s, Value v){ 60 + auto t2 = new Table(t, Table.Kind.NotPropagateSet); 61 + t2.set(s.data, "@v", v); 62 + return t2; 63 + }) ); 64 + ctx.set("{}", "@v", native( (){ 65 + return new Table; 66 + }) ); 81 67 return ctx; 82 68 } 83 69 84 70 /// Entry point of this module 85 71 86 72 Tuple!(Value,"val",Table,"ctx") evalString(S,T...)(S str, T fn_ln_cn) 87 73 {