1 module orelang.operator.DefineOperator; 2 import orelang.operator.DynamicOperator, 3 orelang.operator.IOperator, 4 orelang.Engine, 5 orelang.Value; 6 import std.algorithm, 7 std.array; 8 9 class DefineOperator : IOperator { 10 /** 11 * call 12 */ 13 public Value call(Engine engine, Value[] args) { 14 if (args[0].type == ValueType.SymbolValue) {// variable 15 string varName = args[0].getString; 16 Value var = args[1]; 17 18 engine.defineVariable(varName, var); 19 20 return var; 21 } else if (args[0].type == ValueType.Array) {// function 22 Value[] funcSignatures = args[0].getArray; 23 string funcName = funcSignatures[0].getString; 24 string[] funcArgs = funcSignatures[1..$].map!(value => value.getString).array; 25 Value funcBody = args[1]; 26 27 return engine.defineVariable(funcName, new Value(cast(IOperator)(new DynamicOperator(funcArgs, funcBody)))); 28 } else { 29 throw new Exception("Invalid"); 30 } 31 } 32 }