From 41d98c313ca5864112e7e39f11865cb972839791 Mon Sep 17 00:00:00 2001 From: Ludovic 'Archivist' Lagouardette Date: Tue, 7 Sep 2021 17:37:26 +0200 Subject: [PATCH] More little things to the parser --- Functions/Compare.cs | 14 ++++++++++++++ Functions/Constants.cs | 21 +++++++++++++++++++++ Parser.cs | 15 ++++++++++++--- Program.cs | 21 +++++++++++++++------ Test.basic | 4 +++- 5 files changed, 65 insertions(+), 10 deletions(-) create mode 100644 Functions/Compare.cs create mode 100644 Functions/Constants.cs diff --git a/Functions/Compare.cs b/Functions/Compare.cs new file mode 100644 index 0000000..fd4267e --- /dev/null +++ b/Functions/Compare.cs @@ -0,0 +1,14 @@ +using System; +using System.Collections.Generic; +using System.Text; + +namespace SuperBASIC.Functions +{ + class Compare : IFunction + { + float IFunction.Apply(List arguments) + { + return arguments[0] == arguments[1] ? 1 : 0; + } + } +} diff --git a/Functions/Constants.cs b/Functions/Constants.cs new file mode 100644 index 0000000..fc7bda7 --- /dev/null +++ b/Functions/Constants.cs @@ -0,0 +1,21 @@ +using System; +using System.Collections.Generic; +using System.Text; + +namespace SuperBASIC.Functions +{ + class Pi : IFunction + { + float IFunction.Apply(List arguments) + { + return (float)Math.PI; + } + } + class Euler : IFunction + { + float IFunction.Apply(List arguments) + { + return (float)Math.E; + } + } +} diff --git a/Parser.cs b/Parser.cs index 8cd9eb5..4dacae9 100644 --- a/Parser.cs +++ b/Parser.cs @@ -62,7 +62,7 @@ namespace SuperBASIC if(!library.nameResolution.ContainsKey(components[0])) { int lineIndex = 0; - foreach (int cnt in lineSpans.GetRange(0, idx)) lineIndex += cnt; + foreach (int cnt in lineSpans.GetRange(0, idx+1)) lineIndex += cnt; throw new ParseException($"Unknown operation \"{components[0]}\"\n\tat line {lineIndex}"); } @@ -72,7 +72,7 @@ namespace SuperBASIC if(arity != components.Length-1) { int lineIndex = 0; - foreach (int cnt in lineSpans.GetRange(0, idx)) lineIndex += cnt; + foreach (int cnt in lineSpans.GetRange(0, idx+1)) lineIndex += cnt; throw new ParseException($"Operation {components[0]} was provided with the wrong number of arguments\n\tExpected {arity} found {components.Length-1}\n\tat line {lineIndex}"); } @@ -81,7 +81,16 @@ namespace SuperBASIC { if (elem != "$") { - c.bytecode.Add(new BasicNumber(runtime, float.Parse(elem))); + try + { + float v = float.Parse(elem); + c.bytecode.Add(new BasicNumber(runtime, v)); + } + catch(Exception) { + int lineIndex = 0; + foreach (int cnt in lineSpans.GetRange(0, idx+1)) lineIndex += cnt; + throw new ParseException($"Cannot parse {elem} as argument\n\tExpected floating point number or '$'\n\tat line {lineIndex}"); + } } else { diff --git a/Program.cs b/Program.cs index feff13b..80cc1e3 100644 --- a/Program.cs +++ b/Program.cs @@ -7,12 +7,21 @@ namespace SuperBASIC { static void Main(string[] args) { - Library lib = new Library(); - lib.AddFunction(new Functions.Print(), 1, "PRINT"); - lib.AddFunction(new Functions.Multiply(), 2, "MULTIPLY"); - Runtime r = new Runtime(lib); - r.OpenFile(Directory.GetCurrentDirectory() + "\\Test.basic"); - r.Run(); + try + { + Library lib = new Library(); + lib.AddFunction(new Functions.Print(), 1, "PRINT"); + lib.AddFunction(new Functions.Multiply(), 2, "MULTIPLY"); + lib.AddFunction(new Functions.Compare(), 2, "COMPARE"); + lib.AddFunction(new Functions.Pi(), 0, "PI"); + lib.AddFunction(new Functions.Euler(), 0, "EULER"); + Runtime r = new Runtime(lib); + r.OpenFile(Directory.GetCurrentDirectory() + "\\Test.basic"); + r.Run(); + } catch (Parser.ParseException e) + { + Console.WriteLine($"Parsing failed:\n{e}"); + } } } } diff --git a/Test.basic b/Test.basic index 15dbbd5..d4bff56 100644 --- a/Test.basic +++ b/Test.basic @@ -1,2 +1,4 @@ -MULTIPLY 2 3 +EULER +MULTIPLY $ $ +COMPARE $ 7.3890557 PRINT $ \ No newline at end of file