Ver a proveniência

More little things to the parser

master
Ludovic 'Archivist' Lagouardette há 3 anos
ascendente
cometimento
41d98c313c
5 ficheiros alterados com 65 adições e 10 eliminações
  1. +14
    -0
      Functions/Compare.cs
  2. +21
    -0
      Functions/Constants.cs
  3. +12
    -3
      Parser.cs
  4. +15
    -6
      Program.cs
  5. +3
    -1
      Test.basic

+ 14
- 0
Functions/Compare.cs Ver ficheiro

@ -0,0 +1,14 @@
using System;
using System.Collections.Generic;
using System.Text;
namespace SuperBASIC.Functions
{
class Compare : IFunction
{
float IFunction.Apply(List<BasicNumber> arguments)
{
return arguments[0] == arguments[1] ? 1 : 0;
}
}
}

+ 21
- 0
Functions/Constants.cs Ver ficheiro

@ -0,0 +1,21 @@
using System;
using System.Collections.Generic;
using System.Text;
namespace SuperBASIC.Functions
{
class Pi : IFunction
{
float IFunction.Apply(List<BasicNumber> arguments)
{
return (float)Math.PI;
}
}
class Euler : IFunction
{
float IFunction.Apply(List<BasicNumber> arguments)
{
return (float)Math.E;
}
}
}

+ 12
- 3
Parser.cs Ver ficheiro

@ -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
{

+ 15
- 6
Program.cs Ver ficheiro

@ -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}");
}
}
}
}

+ 3
- 1
Test.basic Ver ficheiro

@ -1,2 +1,4 @@
MULTIPLY 2 3
EULER
MULTIPLY $ $
COMPARE $ 7.3890557
PRINT $

Carregando…
Cancelar
Guardar