1 module orelang.operator.DeffunOperator;
2 import orelang.operator.DynamicOperator,
3        orelang.operator.IOperator,
4        orelang.Engine,
5        orelang.Value;
6 import std.algorithm,
7        std.array;
8 import kontainer.orderedAssocArray;
9 
10 class DeffunOperator : IOperator {
11   /**
12    * call
13    */
14   public Value call(Engine engine, Value[] args) {
15     string funcName   = args[0].getString;
16     string[] funcArgs;
17     OrderedAssocArray!(string, Value) opArgs = new OrderedAssocArray!(string, Value)();
18     
19     foreach (maybeArg; args[1].getArray) {
20       if (maybeArg.type == ValueType.String || maybeArg.type == ValueType.SymbolValue) {
21         funcArgs ~= maybeArg.getString;
22       } else if (maybeArg.type == ValueType.Array) {
23         auto pair = maybeArg.getArray;
24         string name = pair[0].getString;
25         Value value = pair[1];
26         opArgs[name] = value;
27       } else {
28         throw new Error("Invalid argument list");
29       }
30     }
31 
32     Value funcBody = args[2];
33 
34     return engine.defineVariable(funcName, new Value(cast(IOperator)(new DynamicOperator(funcArgs, funcBody, opArgs))));
35   }
36 }