This commit is contained in:
nub31
2025-09-20 02:01:53 +02:00
parent 0afd4545f5
commit 73c9a10e8f
4 changed files with 19 additions and 11 deletions

View File

@@ -501,8 +501,11 @@ public class QBEGenerator
case ReturnNode @return:
EmitReturn(@return);
break;
case StatementExpressionNode statementExpression:
EmitExpression(statementExpression.Expression);
case StatementFuncCallNode statementFuncCall:
EmitFuncCall(statementFuncCall.FuncCall);
break;
case StatementStructFuncCallNode statementStructFuncCall:
EmitStructFuncCall(statementStructFuncCall.StructFuncCall);
break;
case VariableDeclarationNode variableDeclaration:
EmitVariableDeclaration(variableDeclaration);

View File

@@ -211,6 +211,8 @@ public sealed class Parser
private StatementSyntax ParseStatement()
{
var startIndex = _tokenIndex;
if (CurrentToken is SymbolToken symbol)
{
switch (symbol.Symbol)
@@ -236,12 +238,6 @@ public sealed class Parser
}
}
return ParseStatementExpression();
}
private StatementSyntax ParseStatementExpression()
{
var startIndex = _tokenIndex;
var expr = ParseExpression();
if (TryExpectSymbol(Symbol.Assign))

View File

@@ -6,7 +6,9 @@ public abstract record TerminalStatementNode : StatementNode;
public record BlockNode(List<StatementNode> Statements) : StatementNode;
public record StatementExpressionNode(ExpressionNode Expression) : StatementNode;
public record StatementFuncCallNode(FuncCallNode FuncCall) : StatementNode;
public record StatementStructFuncCallNode(StructFuncCallNode StructFuncCall) : StatementNode;
public record ReturnNode(Optional<ExpressionNode> Value) : TerminalStatementNode;

View File

@@ -203,9 +203,16 @@ public sealed class TypeChecker
return new ReturnNode(value);
}
private StatementExpressionNode CheckStatementExpression(StatementExpressionSyntax statement)
private StatementNode CheckStatementExpression(StatementExpressionSyntax statement)
{
return new StatementExpressionNode(CheckExpression(statement.Expression));
var expression = CheckExpression(statement.Expression);
return expression switch
{
FuncCallNode funcCall => new StatementFuncCallNode(funcCall),
StructFuncCallNode structFuncCall => new StatementStructFuncCallNode(structFuncCall),
_ => throw new TypeCheckerException(Diagnostic.Error("Expressions statements can only be function calls").At(statement).Build())
};
}
private VariableDeclarationNode CheckVariableDeclaration(VariableDeclarationSyntax statement)