Browse Source

Parser tweaking

Bepis 6 years ago
parent
commit
85f942d7e1

+ 1 - 1
NTERA.Core/IConsole.cs

@@ -34,7 +34,7 @@ namespace NTERA.Core
 		bool Enabled { get; }
 		void ThrowError(bool playSound);
 		void PrintErrorButton(string message);
-		void Write(string message, PrintFlags? flags = null);
+		void Write(string message, PrintFlags flags = null);
 		bool UseSetColorStyle { get; set; }
 		void PrintC(string message, bool something);
 		DisplayLineAlignment Alignment { get; set; }

+ 9 - 8
NTERA.Core/PrintFlags.cs

@@ -1,10 +1,11 @@
 namespace NTERA.Core
 {
-    public struct PrintFlags {
-        public bool NewLine;
-        public bool Wait;
-        public bool ForceKana;
-        public bool NoColor;
-        public bool SingleLine; // No word wrap
-    }
-}
+	public class PrintFlags
+	{
+		public bool NewLine;
+		public bool Wait;
+		public bool ForceKana;
+		public bool NoColor;
+		public bool SingleLine; // No word wrap
+	}
+}

+ 5 - 1
NTERA.Engine/Compiler/Lexer.cs

@@ -293,7 +293,11 @@ namespace NTERA.Engine.Compiler
 					if (peek)
 						GetNextChar();
 
-					if (GetNextChar(true) == '&')
+					adv = !peek && !IsPeeking
+						? GetNextChar(true)
+						: source[sourceMarker.Pointer + 2];
+
+					if (adv == '&')
 						GetNextChar();
 
 					return Token.And;

+ 9 - 8
NTERA.Engine/Compiler/Parser.cs

@@ -686,12 +686,13 @@ namespace NTERA.Engine.Compiler
 
 		private static readonly Dictionary<Token, int> OrderOfOps = new Dictionary<Token, int>
 		{
-			{ Token.Or, 0 }, { Token.And, 0 }, { Token.Not, 0 },
+			{ Token.Or, 0 }, { Token.And, 0 },
 			{ Token.Equal, 1 }, { Token.NotEqual, 1 },
 			{ Token.Less, 1 }, { Token.More, 1 }, { Token.LessEqual, 1 }, { Token.MoreEqual, 1 },
 			{ Token.Plus, 2 }, { Token.Minus, 2 },
 			{ Token.Asterisk, 3 }, { Token.Slash, 3 }, { Token.Modulo, 3 },
-			{ Token.Caret, 4 }, { Token.ShiftLeft, 4 }, { Token.ShiftRight, 4 }
+			{ Token.Caret, 4 }, { Token.ShiftLeft, 4 }, { Token.ShiftRight, 4 },
+			{ Token.Not, 5 },
 		};
 
 		protected ExecutionNode Expression(out ParserError error, bool useModulo = true, bool ternaryString = false)
@@ -707,7 +708,7 @@ namespace NTERA.Engine.Compiler
 
 				Token op = operators.Pop();
 
-				if (op.IsUnary() && operands.Count == 1)
+				if (op.IsUnary() && operands.Count >= 1)
 				{
 					var operand = operands.Pop();
 
@@ -725,7 +726,7 @@ namespace NTERA.Engine.Compiler
 						}
 					});
 				}
-				else if (operands.Count >= 2)
+				else if (!op.IsUnary() && operands.Count >= 2)
 				{
 					ExecutionNode right = operands.Pop();
 					ExecutionNode left = operands.Pop();
@@ -790,15 +791,15 @@ namespace NTERA.Engine.Compiler
 				}
 				else if (token == Token.Identifer)
 				{
-					if (FunctionDefinitions.Any(x => x.Name == Lexer.Identifier))
+					if (IsVariable(Lexer.Identifier))
 					{
-						operands.Push(GetFunction(out error));
+						operands.Push(GetVariable(out error));
 						if (error != null)
 							return null;
 					}
-					else if (IsVariable(Lexer.Identifier))
+					else if (FunctionDefinitions.Any(x => x.Name == Lexer.Identifier))
 					{
-						operands.Push(GetVariable(out error));
+						operands.Push(GetFunction(out error));
 						if (error != null)
 							return null;
 					}

+ 16 - 1
NTERA.Engine/Runtime/Base/Keywords.cs

@@ -53,11 +53,26 @@ namespace NTERA.Engine.Runtime.Base
 		[Keyword("RESTART")]
 		public static void Restart(EraRuntime runtime, StackFrame context, ExecutionNode node)
 		{
-			runtime.Initialize(runtime.Console);
+			runtime.Reset();
 
 			runtime.Call(runtime.ExecutionProvider.DefinedProcedures.First(x => x.Name == "SYSTEM_TITLE"));
 		}
 
+		[Keyword("BEGIN", true, false)]
+		public static void Begin(EraRuntime runtime, StackFrame context, ExecutionNode node)
+		{
+			string target = runtime.ComputeExpression(context, node[0]).String;
+
+			if (target.Equals("FIRST", StringComparison.OrdinalIgnoreCase))
+			{
+				runtime.Call(runtime.ExecutionProvider.DefinedProcedures.First(x => x.Name == "EVENTFIRST"));
+			}
+			else
+			{
+				throw new EraRuntimeException($"Unknown BEGIN target '{target}'");
+			}
+		}
+
 		[Keyword("GOTO", true)]
 		public static void Goto(EraRuntime runtime, StackFrame context, ExecutionNode node)
 		{

+ 3 - 0
NTERA.Engine/Runtime/BaseDefinitions.cs

@@ -64,6 +64,7 @@ namespace NTERA.Engine.Runtime
 			new FunctionVariable("GOTJUEL", ValueType.Real),
 			new FunctionVariable("EJAC", ValueType.Real),
 			new FunctionVariable("BOUGHT", ValueType.Real),
+			new FunctionVariable("PBAND", ValueType.Real),
 			new FunctionVariable("ITEMSALES", ValueType.Real),
 			new FunctionVariable("FORWARD", ValueType.Real), //special casing for SORTCHARA
 			new FunctionVariable("BACK", ValueType.Real), //special casing for SORTCHARA
@@ -230,6 +231,7 @@ namespace NTERA.Engine.Runtime
 			new Keyword("DEBUGPRINTFORML", true, true),
 			new Keyword("THROW", true, true),
 
+			new Keyword("DATA", true),
 			new Keyword("PRINT", true),
 			new Keyword("PRINTD", true),
 			new Keyword("PRINTDW", true),
@@ -242,6 +244,7 @@ namespace NTERA.Engine.Runtime
 			new Keyword("ALIGNMENT", true),
 			new Keyword("CALL", true, true),
 			new Keyword("CUSTOMDRAWLINE", true),
+			new Keyword("BEGIN", true),
 			new Keyword("GOTO", true),
 			new Keyword("DEBUGPRINTL", true),
 			new Keyword("REUSELASTLINE", true),

+ 8 - 1
NTERA.Engine/Runtime/EraRuntime.cs

@@ -40,11 +40,11 @@ namespace NTERA.Engine.Runtime
 
 			ExecutionStack.Clear();
 			ExecutionResultStack.Clear();
-			TotalProcedureDefinitions.Clear();
 			GlobalVariables.Clear();
 
 			ExecutionProvider.Initialize(console);
 
+			TotalProcedureDefinitions.Clear();
 			TotalProcedureDefinitions.AddRange(ExecutionProvider.DefinedProcedures);
 			TotalProcedureDefinitions.AddRange(BaseDefinitions.DefaultGlobalFunctions);
 
@@ -77,6 +77,13 @@ namespace NTERA.Engine.Runtime
 			return true;
 		}
 
+		public void Reset()
+		{
+			ExecutionStack.Clear();
+			ExecutionResultStack.Clear();
+			GlobalVariables.Clear();
+		}
+
 		private void PrintStackTrace(bool toSystemConsoleOnly)
 		{
 			if (!toSystemConsoleOnly)

+ 2 - 3
NTERA.Engine/Value.cs

@@ -25,12 +25,11 @@ namespace NTERA.Engine
 			String = real.ToString("0.##");
 		}
 
-		public Value(string str)
-			: this()
+		public Value(string str) : this()
 		{
 			Type = ValueType.String;
 			String = str;
-			Real = Double.NaN;
+			Real = double.NaN;
 		}
 
 		public Value Operate(Value b, Token tok)

+ 2 - 2
NTERA/Console/EraConsoleInstance.cs

@@ -18,7 +18,7 @@ namespace NTERA.Console
 		public Color ForeColor = Color.White;
 		public Font Font;
 
-		public void AddText(string text, PrintFlags? flags = null, Color? color = null)
+		public void AddText(string text, PrintFlags flags = null, Color? color = null)
 		{
 			var flags_ = flags ?? new PrintFlags { NewLine = true };
 			var item = new TextRenderItem(text, Font)
@@ -156,7 +156,7 @@ namespace NTERA.Console
 
 		protected StringBuilder bodyBuilder = new StringBuilder();
 
-		public void Write(string message, PrintFlags? flags = null)
+		public void Write(string message, PrintFlags flags = null)
 		{
 			//message.Replace(' ', ' ');
 			//bodyBuilder.Append(message);