Skip to content

Commit

Permalink
feat: Add helper method to ensure conditons of while and if are of ty…
Browse files Browse the repository at this point in the history
…pe bool
  • Loading branch information
furesoft committed Feb 3, 2025
1 parent cfb2a1e commit 3134254
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ public class IfStatementListener : Listener<BodyCompilation, AstNode, IfStatemen
protected override void ListenToNode(BodyCompilation context, IfStatement node)
{
var cond = Utils.CreateValue(node.Condition, context);
cond.EnsureType<bool>(node.Condition);

cond = NegateCondition(cond);
context.Builder.ForkIf(cond, (builder, block) =>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ protected override void ListenToNode(BodyCompilation context, WhileStatement nod
body.SetBranch(condBlk);

var cond = Utils.CreateValue(node.Condition, context);
cond.EnsureType<bool>(node.Condition);
condBlk.SetBranch(new BranchInst(cond, body, after));

context.Builder.SetPosition(after);
Expand Down
13 changes: 13 additions & 0 deletions NewSource/SocordiaC/Compilation/ValueExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
namespace Socordia.Compilation;

public static class ValueExtensions
{
public static void EnsureType<T>(this Value value, AstNode node)
{
//ToDo: fix this
if (value.Type != typeof(T))
{
node.AddError($"Expected value of type {type}, but got {value.Type}");
}
}
}

0 comments on commit 3134254

Please sign in to comment.