Browse Source

While loops

master
Ludovic 'Archivist' Lagouardette 3 years ago
parent
commit
b97134a155
5 changed files with 70 additions and 17 deletions
  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 View File

@ -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 View File

@ -230,9 +230,9 @@ namespace SuperBASIC
{ {
//Case 2: update the JZ //Case 2: update the JZ
c.bytecode[label.index + 2] = new BasicNumber(runtime, (float)c.bytecode.Count); c.bytecode[label.index + 2] = new BasicNumber(runtime, (float)c.bytecode.Count);
}
else
{
}
else
{
int lineIndex = 0; int lineIndex = 0;
foreach (int cnt in lineSpans.GetRange(0, idx + 1)) lineIndex += cnt; foreach (int cnt in lineSpans.GetRange(0, idx + 1)) lineIndex += cnt;
throw new ParseException($"Orphaned ENDIF\n\tat line {lineIndex}"); 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}"); throw new ParseException($"Orphaned ENDIF despite THEN\n\tat line {lineIndex}");
} }
break; 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: default:
ParseExpression(c, components, idx, lineSpans); ParseExpression(c, components, idx, lineSpans);
break; break;

+ 1
- 0
Program.cs View File

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

+ 4
- 0
SuperBASIC.csproj View File

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

+ 11
- 14
Test.basic View File

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

Loading…
Cancel
Save