Engine.cs 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Drawing;
  4. using System.IO;
  5. using NTERA.Core;
  6. using NTERA.EmuEra.Game.EraEmu.Content;
  7. namespace NTERA.Interpreter
  8. {
  9. public class Engine : IScriptEngine
  10. {
  11. public IConsole Console { get; protected set; }
  12. public string EntrypointPath { get; protected set; }
  13. public Dictionary<string, Interpreter> Procedures = new Dictionary<string, Interpreter>();
  14. public Stack<Interpreter> CallStack = new Stack<Interpreter>();
  15. public Interpreter CurrentParser => CallStack.Count > 0 ? CallStack.Peek() : null;
  16. public bool Initialize(IConsole console)
  17. {
  18. Console = console;
  19. EntrypointPath = @"M:\era\eraSemifullTest\erb\SYSTEM_TITLE.ERB"; //@"M:\era\eraSemifullTest\erb\TWTITLE.txt";
  20. Preprocess(File.ReadAllText(EntrypointPath));
  21. CallStack.Push(new Interpreter(console, File.ReadAllText(EntrypointPath)));
  22. return true;
  23. }
  24. public void Start()
  25. {
  26. CurrentParser.Exec();
  27. }
  28. public void InputString(string input)
  29. {
  30. throw new NotImplementedException();
  31. }
  32. public void InputInteger(long input)
  33. {
  34. throw new NotImplementedException();
  35. }
  36. public void InputSystemInteger(long input)
  37. {
  38. throw new NotImplementedException();
  39. }
  40. public CroppedImage GetImage(string name)
  41. {
  42. var bitmap = new Bitmap(@"M:\era\eraSemifullTest\resources\bbb.png");
  43. return new CroppedImage(name, bitmap, new Rectangle(Point.Empty, bitmap.Size), false);
  44. }
  45. public Dictionary<string, Interpreter> Preprocess(string contents)
  46. {
  47. Dictionary<string, Interpreter> procs = new Dictionary<string, Interpreter>();
  48. Lexer lexer = new Lexer(contents);
  49. Marker startMarker = lexer.TokenMarker;
  50. string currentProc = null;
  51. VariableDictionary locals = new VariableDictionary();
  52. void Commit()
  53. {
  54. if (currentProc != null)
  55. {
  56. string procBody = contents.Substring(startMarker.Pointer,
  57. lexer.TokenMarker.Pointer - startMarker.Pointer);
  58. var interpreter = new Interpreter(Console, procBody)
  59. {
  60. Variables = locals
  61. };
  62. procs.Add(currentProc, interpreter);
  63. }
  64. }
  65. using (var enumerator = lexer.GetEnumerator())
  66. do
  67. {
  68. if (enumerator.Current == Token.Function)
  69. {
  70. Commit();
  71. enumerator.MoveNext();
  72. if (enumerator.Current != Token.Identifer)
  73. throw new InvalidOperationException();
  74. currentProc = lexer.Identifer;
  75. }
  76. else if (enumerator.Current == Token.Sharp)
  77. {
  78. enumerator.MoveNext();
  79. switch (enumerator.Current)
  80. {
  81. case Token.Dim:
  82. {
  83. bool isString = enumerator.Current != Token.Dim;
  84. enumerator.MoveNext();
  85. while (enumerator.MoveNext() && lexer.Identifer == "CONST")
  86. {
  87. }
  88. string variable = lexer.Identifer;
  89. enumerator.MoveNext();
  90. enumerator.MoveNext();
  91. if (isString)
  92. lexer.Type = LexerType.String;
  93. locals[variable] = lexer.Expression();
  94. lexer.Type = LexerType.Both;
  95. break;
  96. }
  97. case Token.ReturnFunction:
  98. {
  99. break;
  100. }
  101. }
  102. }
  103. } while (enumerator.MoveNext());
  104. Commit();
  105. return procs;
  106. }
  107. }
  108. }