|
@@ -39,10 +39,22 @@ namespace NTERA.Engine.Runtime
|
|
|
{
|
|
|
var executionContents = ExecutionProvider.GetExecutionNodes(function);
|
|
|
|
|
|
+ var localVariables = new VariableDictionary();
|
|
|
+
|
|
|
+ foreach (var variable in function.Variables)
|
|
|
+ localVariables[variable.Name] = variable.CalculatedValue;
|
|
|
+
|
|
|
+ var globalVariables = new VariableDictionary();
|
|
|
+
|
|
|
+ foreach (var variable in BaseDefinitions.DefaultGlobalVariables)
|
|
|
+ globalVariables[variable.Name] = variable.CalculatedValue;
|
|
|
+
|
|
|
var executionSet = new ExecutionSet
|
|
|
{
|
|
|
Nodes = executionContents.ToList(),
|
|
|
- SelfDefinition = function
|
|
|
+ SelfDefinition = function,
|
|
|
+ GlobalVariables = globalVariables,
|
|
|
+ LocalVariables = localVariables
|
|
|
};
|
|
|
|
|
|
ExecutionStack.Push(executionSet);
|
|
@@ -52,28 +64,57 @@ namespace NTERA.Engine.Runtime
|
|
|
ExecutionStack.Pop();
|
|
|
}
|
|
|
|
|
|
- protected void ExecuteSet(ExecutionSet set)
|
|
|
+ public void ExecuteSet(ExecutionSet set)
|
|
|
{
|
|
|
for (; set.Position < set.Nodes.Count; set.Position++)
|
|
|
{
|
|
|
ExecutionNode node = set.Nodes[set.Position];
|
|
|
|
|
|
- switch (node.Type)
|
|
|
- {
|
|
|
- case "statement":
|
|
|
- string statement = node.Metadata["name"];
|
|
|
-
|
|
|
- if (statement == "DRAWLINE")
|
|
|
- Base.Keywords.DrawLine(this, set);
|
|
|
- else
|
|
|
- {
|
|
|
- throw new Exception($"Unknown statement: '{statement}'");
|
|
|
- }
|
|
|
-
|
|
|
- break;
|
|
|
- default:
|
|
|
- throw new Exception($"Unknown node type: '{node.Type}'");
|
|
|
- }
|
|
|
+ ExecuteNode(set, node);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ public void ExecuteNode(ExecutionSet set, ExecutionNode node)
|
|
|
+ {
|
|
|
+ switch (node.Type)
|
|
|
+ {
|
|
|
+ case "statement":
|
|
|
+ string statement = node.Metadata["name"];
|
|
|
+
|
|
|
+ if (!Base.Keywords.StaticKeywords.TryGetValue(statement, out var keywordAction))
|
|
|
+ throw new Exception($"Unknown statement: '{statement}'");
|
|
|
+
|
|
|
+ keywordAction(this, set, node);
|
|
|
+
|
|
|
+ break;
|
|
|
+ case "assignment":
|
|
|
+ string variableName = node["variable"].Metadata["name"];
|
|
|
+
|
|
|
+ if (set.LocalVariables.ContainsKey(variableName))
|
|
|
+ set.LocalVariables[variableName] = ComputeExpression(set, node["value"].SubNodes.Single());
|
|
|
+ else if (set.GlobalVariables.ContainsKey(variableName))
|
|
|
+ set.GlobalVariables[variableName] = ComputeExpression(set, node["value"].SubNodes.Single());
|
|
|
+ else
|
|
|
+ throw new Exception($"Unknown variable: '{variableName}'");
|
|
|
+
|
|
|
+ break;
|
|
|
+ default:
|
|
|
+ throw new Exception($"Unknown node type: '{node.Type}'");
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ public Value ComputeExpression(ExecutionSet set, ExecutionNode expressionNode)
|
|
|
+ {
|
|
|
+ switch (expressionNode.Type)
|
|
|
+ {
|
|
|
+ case "constant":
|
|
|
+ ValueType type = (ValueType)Enum.Parse(typeof(ValueType), expressionNode.Metadata["type"]);
|
|
|
+
|
|
|
+ string strValue = expressionNode.Metadata["value"];
|
|
|
+
|
|
|
+ return type == ValueType.String ? (Value)strValue : (Value)double.Parse(strValue);
|
|
|
+ default:
|
|
|
+ throw new Exception($"Unknown expression type: '{expressionNode.Type}'");
|
|
|
}
|
|
|
}
|
|
|
|