Skip to content

Commit

Permalink
see ref values
Browse files Browse the repository at this point in the history
  • Loading branch information
chrisbeamable committed Feb 27, 2025
1 parent c3c4cf1 commit ffc1720
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 3 deletions.
2 changes: 2 additions & 0 deletions FadeBasic/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Debugger would emit exit event during partial budget runs.
- Debugger can render `byte` variables.
- Debugger can set non `int` and non `float` values.
- Debugger can discovery variables declared through `ref` commands, such as
`INC` or `INPUT`
- Commands with `ref` parameters can access globally scoped variables.

### Added
Expand Down
5 changes: 4 additions & 1 deletion FadeBasic/FadeBasic/Ast/ExpressionNode.cs
Original file line number Diff line number Diff line change
Expand Up @@ -360,9 +360,12 @@ public class LiteralStringExpression : AstNode, ILiteralNode

public LiteralStringExpression(Token token) : base(token)
{

value = token.raw.Substring(1, token.raw.Length - 2); // account for quotes
}
public LiteralStringExpression(Token token, string value) : base(token)
{
this.value = value;
}


protected override string GetString()
Expand Down
36 changes: 34 additions & 2 deletions FadeBasic/FadeBasic/Virtual/Compiler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1360,7 +1360,7 @@ private void Compile(AddressExpression expression)
variable = refNode.variableName,
type = new TypeReferenceNode(refNode.DefaultTypeByName, refNode.startToken)
};
Compile(fakeDeclStatement);
Compile(fakeDeclStatement, includeDefaultInitializer: true);
if (!scope.TryGetVariable(refNode.variableName, out variable))
{
throw new Exception(
Expand Down Expand Up @@ -1575,7 +1575,7 @@ public void Compile(CommandStatement commandStatement)

}

public void Compile(DeclarationStatement declaration)
public void Compile(DeclarationStatement declaration, bool includeDefaultInitializer=false)
{
/*
* the declaration tells us that we need a register
Expand Down Expand Up @@ -1757,6 +1757,38 @@ public void Compile(DeclarationStatement declaration)
};
Compile(fakeAssignment);
}
else if (includeDefaultInitializer)
{

if (declaration.ranks == null)
{
switch (tc)
{
case TypeCodes.STRUCT:
// TODO: it isn't possible to pass structs as refs atm, but if it was, this would be a problem.
break;
case TypeCodes.STRING: // TODO: handle the empty string?
// if the variable is a string, then always assign it to the empty string, which will get interned.
var fadeStrAssignment = new AssignmentStatement
{
expression = new LiteralStringExpression(declaration.startToken, ""),
variable = new VariableRefNode(declaration.startToken, declaration.variable)
};
Compile(fadeStrAssignment);
break;
default:
// if the variable is a primitive, then always assign it to a default value.
var fakeAssignment = new AssignmentStatement
{
expression = new LiteralIntExpression(declaration.startToken, 0),
variable = new VariableRefNode(declaration.startToken, declaration.variable)
};
Compile(fakeAssignment);
break;
}

}
}
}

public void PushAddress(ArrayIndexReference arrayRefNode)
Expand Down
2 changes: 2 additions & 0 deletions FadeBasic/Tests/DebuggerTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,8 @@ dim x(3,5) as vec
}

[TestCase("x# = 4.2", "x#+1", "5.2")]
[TestCase("inc x", "x", "1")]
[TestCase("tuna x$", "x$", "tuna")]
[TestCase("x = 4", "x+1", "5")]
[TestCase(@"
x = 1
Expand Down

0 comments on commit ffc1720

Please sign in to comment.