Selaa lähdekoodia

While loops

master
Ludovic 'Archivist' Lagouardette 2 vuotta sitten
vanhempi
commit
b97134a155
5 muutettua tiedostoa jossa 70 lisäystä ja 17 poistoa
  1. +14
    -0
      Functions/Add.cs
  2. +40
    -3
      Parser.cs
  3. +1
    -0
      Program.cs
  4. +4
    -0
      SuperBASIC.csproj
  5. +11
    -14
      Test.basic

+ 14
- 0
Functions/Add.cs Näytä tiedosto

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

+ 40
- 3
Parser.cs Näytä tiedosto

@ -230,9 +230,9 @@ namespace SuperBASIC
{
//Case 2: update the JZ
c.bytecode[label.index + 2] = new BasicNumber(runtime, (float)c.bytecode.Count);
}
else
{
}
else
{
int lineIndex = 0;
foreach (int cnt in lineSpans.GetRange(0, idx + 1)) lineIndex += cnt;
throw new ParseException($"Orphaned ENDIF\n\tat line {lineIndex}");
@ -245,6 +245,43 @@ namespace SuperBASIC
throw new ParseException($"Orphaned ENDIF despite THEN\n\tat line {lineIndex}");
}
break;
case "WHILE":
// Role: manages the expression
// 1. Deal with expression
label.ctrl = Controls.While;
label.index = c.bytecode.Count;
labelStack.Push(label);
ParseExpression(c, components[1..], idx, lineSpans);
// 2. Adds the label to update the destination
label.ctrl = Controls.Wend;
label.index = c.bytecode.Count;
labelStack.Push(label);
// 3. Adds the jumper
c.bytecode.Add(new BasicNumber(runtime, library.nameResolution["JZ"]));
c.bytecode.Add(new BasicNumber(runtime));
c.bytecode.Add(new BasicNumber(runtime, 0f));
break;
case "WEND":
//Role: Destination if depending on what is skipped
var labelA = labelStack.Pop();
if (labelA.ctrl == Controls.Wend)
{
label = labelStack.Pop();
if (label.ctrl == Controls.While)
{
c.bytecode.Add(new BasicNumber(runtime, library.nameResolution["GOTO"]));
c.bytecode.Add(new BasicNumber(runtime, (float)label.index));
c.bytecode[labelA.index + 2] = new BasicNumber(runtime, (float)c.bytecode.Count);
}
}
else
{
int lineIndex = 0;
foreach (int cnt in lineSpans.GetRange(0, idx + 1)) lineIndex += cnt;
throw new ParseException($"Orphaned WEND\n\tat line {lineIndex}");
}
break;
default:
ParseExpression(c, components, idx, lineSpans);
break;

+ 1
- 0
Program.cs Näytä tiedosto

@ -16,6 +16,7 @@ namespace SuperBASIC
#endif
lib.AddFunction(new Functions.Print(), 1, "PRINT");
lib.AddFunction(new Functions.Multiply(), 2, "MULTIPLY");
lib.AddFunction(new Functions.Add(), 2, "ADD");
lib.AddFunction(new Functions.Compare(), 2, "COMPARE");
lib.AddFunction(new Functions.JumpZero(), 2, "JZ");
lib.AddFunction(new Functions.Goto(), 1, "GOTO");

+ 4
- 0
SuperBASIC.csproj Näytä tiedosto

@ -5,6 +5,10 @@
<TargetFramework>netcoreapp3.1</TargetFramework>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|AnyCPU'">
<DefineConstants>TRACE;MEMORY</DefineConstants>
</PropertyGroup>
<ItemGroup>
<None Update="Test.basic">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>

+ 11
- 14
Test.basic Näytä tiedosto

@ -1,14 +1,11 @@
EULER
MULTIPLY $ $
IF COMPARE $ 7.3890557
THEN
PRINT 25
GOTO end
ELSE
PRINT 10
ENDIF
LABEL loop
PRINT 50
GOTO loop
LABEL end
MEMSTORE 1 0
MEMSTORE 0 10
WHILE MEMLOAD 0
MEMLOAD 0
ADD $ -1
MEMSTORE 0 $
MEMLOAD 1
ADD $ 1
MEMSTORE 1 $
PRINT M1
WEND

Ladataan…
Peruuta
Tallenna