Skip to content

Commit

Permalink
Add support for where target < initial val
Browse files Browse the repository at this point in the history
  • Loading branch information
WilliamQiufeng committed Aug 29, 2022
1 parent e8eee13 commit b281767
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 4 deletions.
6 changes: 5 additions & 1 deletion PseudoCode.Core/Runtime/Operations/ForOperation.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using PseudoCode.Core.Runtime.Instances;
using PseudoCode.Core.Runtime.Types;

namespace PseudoCode.Core.Runtime.Operations;
Expand All @@ -13,15 +14,18 @@ public ForOperation(Scope parentScope, PseudoProgram program) : base(parentScope
{
}

public delegate Instance CompareFunc(Instance i1, Instance i2);
public override void Operate()
{
base.Operate();
// Initial Stack: [ref instance id]
TargetValue.HandledOperate(); // [ref instance id, ref instance targ_val]
var targetValue = Program.RuntimeStack.Pop();
var incrementValue = Program.RuntimeStack.Pop();
var smaller = incrementValue.Type.SmallerEqual(incrementValue, targetValue).Get<bool>();
CompareFunc func = smaller ? incrementValue.Type.SmallerEqual : incrementValue.Type.GreaterEqual;
// Stack empty
while (incrementValue.Type.SmallerEqual(incrementValue, targetValue).Get<bool>())
while (func(incrementValue, targetValue).Get<bool>())
{
ForBody.HandledOperate();
Next.HandledOperate(); // [ref instance next]
Expand Down
2 changes: 1 addition & 1 deletion PseudoCode.Core/Runtime/PseudoProgram.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ namespace PseudoCode.Core.Runtime;
/// </summary>
public class PseudoProgram
{
public static readonly Version Version = new(1, 3, 2);
public static readonly Version Version = new(1, 3, 3);

/// <summary>
/// Stores feedbacks during parsing and typechecking (metaoperation)
Expand Down
2 changes: 1 addition & 1 deletion PseudoCode.Core/Runtime/Reflection/BuiltinFunctions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ public static int Find(string str, char chr)
[BuiltinNativeFunction("INSTR")]
public static int InString(int index, string str, char chr)
{
return Find(str[(index - 1)..], chr);
return Find(str[(index - 1)..], chr) + index - 1;
}

[Documentation("Returns a random number [0..x)")]
Expand Down
4 changes: 3 additions & 1 deletion PseudoCode.Core/run.pseudo
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@


FOR i <- 5 TO 1 STEP -1
OUTPUT i
NEXT i
arr <- [2]
DECLARE a : REAL
DECLARE b : ARRAY[1:3, 1:4] OF INTEGER
Expand Down

0 comments on commit b281767

Please sign in to comment.