EraRuntime.cs 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using NTERA.Core;
  5. using NTERA.EmuEra.Game.EraEmu.Content;
  6. using NTERA.Engine.Compiler;
  7. namespace NTERA.Engine.Runtime
  8. {
  9. public class EraRuntime : IScriptEngine
  10. {
  11. public IExecutionProvider ExecutionProvider { get; }
  12. public IConsole Console { get; protected set; }
  13. public Stack<ExecutionSet> ExecutionStack { get; } = new Stack<ExecutionSet>();
  14. public EraRuntime(IExecutionProvider executionProvider)
  15. {
  16. ExecutionProvider = executionProvider;
  17. }
  18. public bool Initialize(IConsole console)
  19. {
  20. Console = console;
  21. return true;
  22. }
  23. public void Start()
  24. {
  25. Console.PrintSystemLine("EraJIT x64 0.0.0.0");
  26. Console.PrintSystemLine("");
  27. Call(ExecutionProvider.DefinedProcedures.First(x => x.Name == "SYSTEM_TITLE"));
  28. System.Threading.Thread.Sleep(-1);
  29. }
  30. public void Call(FunctionDefinition function)
  31. {
  32. var executionContents = ExecutionProvider.GetExecutionNodes(function);
  33. var localVariables = new VariableDictionary();
  34. foreach (var variable in function.Variables)
  35. localVariables[variable.Name] = variable.CalculatedValue;
  36. var globalVariables = new VariableDictionary();
  37. foreach (var variable in BaseDefinitions.DefaultGlobalVariables)
  38. globalVariables[variable.Name] = variable.CalculatedValue;
  39. var executionSet = new ExecutionSet
  40. {
  41. Nodes = executionContents.ToList(),
  42. SelfDefinition = function,
  43. GlobalVariables = globalVariables,
  44. LocalVariables = localVariables
  45. };
  46. ExecutionStack.Push(executionSet);
  47. ExecuteSet(executionSet);
  48. ExecutionStack.Pop();
  49. }
  50. public void ExecuteSet(ExecutionSet set)
  51. {
  52. for (; set.Position < set.Nodes.Count; set.Position++)
  53. {
  54. ExecutionNode node = set.Nodes[set.Position];
  55. ExecuteNode(set, node);
  56. }
  57. }
  58. public void ExecuteNode(ExecutionSet set, ExecutionNode node)
  59. {
  60. switch (node.Type)
  61. {
  62. case "statement":
  63. string statement = node.Metadata["name"];
  64. if (!Base.Keywords.StaticKeywords.TryGetValue(statement, out var keywordAction))
  65. throw new Exception($"Unknown statement: '{statement}'");
  66. keywordAction(this, set, node);
  67. break;
  68. case "assignment":
  69. string variableName = node["variable"].Metadata["name"];
  70. if (set.LocalVariables.ContainsKey(variableName))
  71. set.LocalVariables[variableName] = ComputeExpression(set, node["value"].SubNodes.Single());
  72. else if (set.GlobalVariables.ContainsKey(variableName))
  73. set.GlobalVariables[variableName] = ComputeExpression(set, node["value"].SubNodes.Single());
  74. else
  75. throw new Exception($"Unknown variable: '{variableName}'");
  76. break;
  77. default:
  78. throw new Exception($"Unknown node type: '{node.Type}'");
  79. }
  80. }
  81. public Value ComputeExpression(ExecutionSet set, ExecutionNode expressionNode)
  82. {
  83. switch (expressionNode.Type)
  84. {
  85. case "constant":
  86. ValueType type = (ValueType)Enum.Parse(typeof(ValueType), expressionNode.Metadata["type"]);
  87. string strValue = expressionNode.Metadata["value"];
  88. return type == ValueType.String ? (Value)strValue : (Value)double.Parse(strValue);
  89. default:
  90. throw new Exception($"Unknown expression type: '{expressionNode.Type}'");
  91. }
  92. }
  93. public void InputString(string input)
  94. {
  95. throw new NotImplementedException();
  96. }
  97. public void InputInteger(long input)
  98. {
  99. throw new NotImplementedException();
  100. }
  101. public void InputSystemInteger(long input)
  102. {
  103. throw new NotImplementedException();
  104. }
  105. public CroppedImage GetImage(string name)
  106. {
  107. throw new NotImplementedException();
  108. }
  109. }
  110. }