123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145 |
- using System;
- using System.Collections.Generic;
- using System.Drawing;
- using System.IO;
- using NTERA.Core;
- using NTERA.EmuEra.Game.EraEmu.Content;
- namespace NTERA.Interpreter
- {
- public class Engine : IScriptEngine
- {
- public IConsole Console { get; protected set; }
- public string EntrypointPath { get; protected set; }
- public Dictionary<string, Interpreter> Procedures = new Dictionary<string, Interpreter>();
- public Stack<Interpreter> CallStack = new Stack<Interpreter>();
- public Interpreter CurrentParser => CallStack.Count > 0 ? CallStack.Peek() : null;
- public bool Initialize(IConsole console)
- {
- Console = console;
- EntrypointPath = @"M:\era\eraSemifullTest\erb\SYSTEM_TITLE.ERB"; //@"M:\era\eraSemifullTest\erb\TWTITLE.txt";
- Preprocess(File.ReadAllText(EntrypointPath));
- CallStack.Push(new Interpreter(console, File.ReadAllText(EntrypointPath)));
- return true;
- }
- public void Start()
- {
- CurrentParser.Exec();
- }
- public void InputString(string input)
- {
- throw new NotImplementedException();
- }
- public void InputInteger(long input)
- {
- throw new NotImplementedException();
- }
- public void InputSystemInteger(long input)
- {
- throw new NotImplementedException();
- }
- public CroppedImage GetImage(string name)
- {
- var bitmap = new Bitmap(@"M:\era\eraSemifullTest\resources\bbb.png");
- return new CroppedImage(name, bitmap, new Rectangle(Point.Empty, bitmap.Size), false);
- }
- public Dictionary<string, Interpreter> Preprocess(string contents)
- {
- Dictionary<string, Interpreter> procs = new Dictionary<string, Interpreter>();
- Lexer lexer = new Lexer(contents);
- Marker startMarker = lexer.TokenMarker;
- string currentProc = null;
- VariableDictionary locals = new VariableDictionary();
- void Commit()
- {
- if (currentProc != null)
- {
- string procBody = contents.Substring(startMarker.Pointer,
- lexer.TokenMarker.Pointer - startMarker.Pointer);
- var interpreter = new Interpreter(Console, procBody)
- {
- Variables = locals
- };
- procs.Add(currentProc, interpreter);
- }
- }
- using (var enumerator = lexer.GetEnumerator())
- do
- {
- if (enumerator.Current == Token.Function)
- {
- Commit();
- enumerator.MoveNext();
- if (enumerator.Current != Token.Identifer)
- throw new InvalidOperationException();
- currentProc = lexer.Identifer;
- }
- else if (enumerator.Current == Token.Sharp)
- {
- enumerator.MoveNext();
- switch (enumerator.Current)
- {
- case Token.Dim:
- {
- bool isString = enumerator.Current != Token.Dim;
- enumerator.MoveNext();
- while (enumerator.MoveNext() && lexer.Identifer == "CONST")
- {
- }
- string variable = lexer.Identifer;
- enumerator.MoveNext();
- enumerator.MoveNext();
- if (isString)
- lexer.Type = LexerType.String;
- locals[variable] = lexer.Expression();
- lexer.Type = LexerType.Both;
- break;
- }
- case Token.ReturnFunction:
- {
- break;
- }
- }
- }
-
- } while (enumerator.MoveNext());
- Commit();
- return procs;
- }
- }
- }
|