Diff
Not logged in

Differences From Artifact [6af5a0863b67aa85]:

To Artifact [641d49d7fd647519]:


> 1 1 /** 2 /** 2 * Authors: k.inaba 3 * Authors: k.inaba 3 * License: NYSL 0.9982 http://www.kmonos.net/nysl/ 4 * License: NYSL 0.9982 http://www.kmonos.net/nysl/ 4 * 5 * 5 * Runtime library for Polemy programming language. 6 * Runtime library for Polemy programming language. 6 */ 7 */ 7 module polemy.runtime; 8 module polemy.runtime; 8 import polemy._common; 9 import polemy._common; 9 import polemy.layer; 10 import polemy.layer; > 11 import polemy.failure; 10 import polemy.value; 12 import polemy.value; 11 import polemy.eval; 13 import polemy.eval; 12 import std.stdio; 14 import std.stdio; 13 15 14 /// enroll the native implementations of primitive functions 16 /// enroll the native implementations of primitive functions 15 17 16 void enrollRuntimeLibrary( Evaluator e ) 18 void enrollRuntimeLibrary( Evaluator e ) 17 { 19 { > 20 // arithmetic operations > 21 e.addPrimitive("+", ValueLayer, 18 e.addPrimitive("+", ValueLayer, (IntValue lhs, IntValue rhs){return new | 22 (IntValue lhs, IntValue rhs){return new IntValue(lhs.data + rhs. > 23 e.addPrimitive("-", ValueLayer, 19 e.addPrimitive("-", ValueLayer, (IntValue lhs, IntValue rhs){return new | 24 (IntValue lhs, IntValue rhs){return new IntValue(lhs.data - rhs. > 25 e.addPrimitive("*", ValueLayer, 20 e.addPrimitive("*", ValueLayer, (IntValue lhs, IntValue rhs){return new | 26 (IntValue lhs, IntValue rhs){return new IntValue(lhs.data * rhs. > 27 e.addPrimitive("/", ValueLayer, > 28 (IntValue lhs, IntValue rhs){ > 29 if( rhs.data == 0 ) > 30 throw genex!RuntimeException("division by 0"); 21 e.addPrimitive("/", ValueLayer, (IntValue lhs, IntValue rhs){return new | 31 return new IntValue(lhs.data / rhs.data); > 32 }); > 33 e.addPrimitive("%", ValueLayer, 22 e.addPrimitive("%", ValueLayer, (IntValue lhs, IntValue rhs){return new | 34 (IntValue lhs, IntValue rhs){return new IntValue(lhs.data % rhs. 23 e.addPrimitive("~", ValueLayer, (Value lhs, Value rhs){return new StrVa < > 35 e.addPrimitive("||", ValueLayer, 24 e.addPrimitive("||", ValueLayer, (IntValue lhs, IntValue rhs){return new | 36 (IntValue lhs, IntValue rhs){return new IntValue(lhs.data!=0 || > 37 e.addPrimitive("&&", ValueLayer, 25 e.addPrimitive("&&", ValueLayer, (IntValue lhs, IntValue rhs){return new | 38 (IntValue lhs, IntValue rhs){return new IntValue(lhs.data!=0 && > 39 // string operation(s) > 40 e.addPrimitive("~", ValueLayer, > 41 (Value lhs, Value rhs){return new StrValue(lhs.toString ~ rhs.to > 42 // comparison 26 e.addPrimitive("<", ValueLayer, (Value lhs, Value rhs){return new IntVa 43 e.addPrimitive("<", ValueLayer, (Value lhs, Value rhs){return new IntVa 27 e.addPrimitive(">", ValueLayer, (Value lhs, Value rhs){return new IntVa 44 e.addPrimitive(">", ValueLayer, (Value lhs, Value rhs){return new IntVa 28 e.addPrimitive("<=", ValueLayer, (Value lhs, Value rhs){return new IntVa 45 e.addPrimitive("<=", ValueLayer, (Value lhs, Value rhs){return new IntVa 29 e.addPrimitive(">=", ValueLayer, (Value lhs, Value rhs){return new IntVa 46 e.addPrimitive(">=", ValueLayer, (Value lhs, Value rhs){return new IntVa 30 e.addPrimitive("==", ValueLayer, (Value lhs, Value rhs){return new IntVa 47 e.addPrimitive("==", ValueLayer, (Value lhs, Value rhs){return new IntVa 31 e.addPrimitive("!=", ValueLayer, (Value lhs, Value rhs){return new IntVa 48 e.addPrimitive("!=", ValueLayer, (Value lhs, Value rhs){return new IntVa 32 e.addPrimitive("print", ValueLayer, (Value a){ | 49 // control flow 33 writeln(a); < 34 return new IntValue(0); < 35 }); < 36 e.addPrimitive("if", ValueLayer, (IntValue x, FunValue ft, FunValue fe){ 50 e.addPrimitive("if", ValueLayer, (IntValue x, FunValue ft, FunValue fe){ 37 auto toRun = (x.data==0 ? fe : ft); 51 auto toRun = (x.data==0 ? fe : ft); 38 // [TODO] fill positional information < 39 return toRun.invoke(ValueLayer, toRun.definitionContext(), null) 52 return toRun.invoke(ValueLayer, toRun.definitionContext(), null) 40 }); 53 }); > 54 // type test > 55 e.addPrimitive("_isint", ValueLayer, 41 e.addPrimitive("_isint", ValueLayer, (Value v){return new IntValue(cast( | 56 (Value v){return new IntValue(cast(IntValue)v !is null);} ); > 57 e.addPrimitive("_isstr", ValueLayer, 42 e.addPrimitive("_isstr", ValueLayer, (Value v){return new IntValue(cast( | 58 (Value v){return new IntValue(cast(StrValue)v !is null);} ); > 59 e.addPrimitive("_isfun", ValueLayer, 43 e.addPrimitive("_isfun", ValueLayer, (Value v){return new IntValue(cast( | 60 (Value v){return new IntValue(cast(FunValue)v !is null);} ); > 61 e.addPrimitive("_isundefined", ValueLayer, 44 e.addPrimitive("_isundefined", ValueLayer, (Value v){return new IntValue | 62 (Value v){return new IntValue(cast(UndefinedValue)v !is null);} > 63 e.addPrimitive("_istable", ValueLayer, 45 e.addPrimitive("_istable", ValueLayer, (Value v){return new IntValue(cas | 64 (Value v){return new IntValue(cast(Table)v !is null);} ); > 65 // table 46 e.addPrimitive(".", ValueLayer, (Table t, StrValue s){ 66 e.addPrimitive(".", ValueLayer, (Table t, StrValue s){ 47 return (t.has(s.data, ValueLayer) ? t.get(s.data, ValueLayer) : 67 return (t.has(s.data, ValueLayer) ? t.get(s.data, ValueLayer) : 48 }); 68 }); 49 e.addPrimitive(".?", ValueLayer, (Table t, StrValue s){ 69 e.addPrimitive(".?", ValueLayer, (Table t, StrValue s){ 50 return new IntValue(t.has(s.data, ValueLayer)); 70 return new IntValue(t.has(s.data, ValueLayer)); 51 }); 71 }); 52 e.addPrimitive(".=", ValueLayer, (Table t, StrValue s, Value v){ 72 e.addPrimitive(".=", ValueLayer, (Table t, StrValue s, Value v){ ................................................................................................................................................................................ 53 auto t2 = new Table(t, Table.Kind.NotPropagateSet); 73 auto t2 = new Table(t, Table.Kind.NotPropagateSet); 54 t2.set(s.data, ValueLayer, v); 74 t2.set(s.data, ValueLayer, v); 55 return t2; 75 return t2; 56 }); 76 }); 57 e.addPrimitive("{}", ValueLayer, (){ 77 e.addPrimitive("{}", ValueLayer, (){ 58 return new Table; 78 return new Table; 59 }); 79 }); > 80 // IO > 81 e.addPrimitive("print", ValueLayer, (Value a){ writeln(a); return new In 60 } 82 }