EraRuntime.cs 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  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. try
  28. {
  29. Call(ExecutionProvider.DefinedProcedures.First(x => x.Name == "SYSTEM_TITLE"));
  30. }
  31. catch (Exception ex)
  32. {
  33. Console.PrintSystemLine($"Unhandled exception: {ex.Message}");
  34. Console.PrintSystemLine("Stack trace:");
  35. foreach (var stackMember in ExecutionStack)
  36. Console.PrintSystemLine($" - @{stackMember.SelfDefinition.Name} ({stackMember.SelfDefinition.Position} > {stackMember.SelfDefinition.Filename})");
  37. throw;
  38. }
  39. System.Threading.Thread.Sleep(-1);
  40. }
  41. public void Call(FunctionDefinition function)
  42. {
  43. var executionContents = ExecutionProvider.GetExecutionNodes(function);
  44. var localVariables = new VariableDictionary();
  45. foreach (var variable in function.Variables)
  46. localVariables[variable.Name] = variable.CalculatedValue;
  47. var globalVariables = new VariableDictionary();
  48. foreach (var variable in BaseDefinitions.DefaultGlobalVariables)
  49. globalVariables[variable.Name] = variable.CalculatedValue;
  50. var executionSet = new ExecutionSet
  51. {
  52. Nodes = executionContents.ToList(),
  53. SelfDefinition = function,
  54. GlobalVariables = globalVariables,
  55. LocalVariables = localVariables
  56. };
  57. ExecutionStack.Push(executionSet);
  58. ExecuteSet(executionSet);
  59. ExecutionStack.Pop();
  60. }
  61. public void ExecuteSet(ExecutionSet set)
  62. {
  63. for (; set.Position < set.Nodes.Count; set.Position++)
  64. {
  65. ExecutionNode node = set.Nodes[set.Position];
  66. ExecuteNode(set, node);
  67. }
  68. }
  69. public void ExecuteNode(ExecutionSet set, ExecutionNode node)
  70. {
  71. switch (node.Type)
  72. {
  73. case "statement":
  74. string statement = node.Metadata["name"];
  75. if (!Base.Keywords.StaticKeywords.TryGetValue(statement, out var keywordAction))
  76. throw new Exception($"Unknown statement: '{statement}'");
  77. keywordAction(this, set, node);
  78. break;
  79. case "assignment":
  80. string variableName = node["variable"].Metadata["name"];
  81. if (set.LocalVariables.ContainsKey(variableName))
  82. set.LocalVariables[variableName] = ComputeExpression(set, node["value"].SubNodes.Single());
  83. else if (set.GlobalVariables.ContainsKey(variableName))
  84. set.GlobalVariables[variableName] = ComputeExpression(set, node["value"].SubNodes.Single());
  85. else
  86. throw new Exception($"Unknown variable: '{variableName}'");
  87. break;
  88. default:
  89. throw new Exception($"Unknown node type: '{node.Type}'");
  90. }
  91. }
  92. public Value ComputeExpression(ExecutionSet set, ExecutionNode expressionNode)
  93. {
  94. switch (expressionNode.Type)
  95. {
  96. case "constant":
  97. ValueType type = (ValueType)Enum.Parse(typeof(ValueType), expressionNode.Metadata["type"]);
  98. string strValue = expressionNode.Metadata["value"];
  99. return type == ValueType.String ? (Value)strValue : (Value)double.Parse(strValue);
  100. default:
  101. throw new Exception($"Unknown expression type: '{expressionNode.Type}'");
  102. }
  103. }
  104. public void InputString(string input)
  105. {
  106. throw new NotImplementedException();
  107. }
  108. public void InputInteger(long input)
  109. {
  110. throw new NotImplementedException();
  111. }
  112. public void InputSystemInteger(long input)
  113. {
  114. throw new NotImplementedException();
  115. }
  116. public CroppedImage GetImage(string name)
  117. {
  118. throw new NotImplementedException();
  119. }
  120. }
  121. }