expression function calls

This commit is contained in:
nub31
2026-02-09 22:30:25 +01:00
parent 88fe03c048
commit d3e2dcede8
5 changed files with 63 additions and 21 deletions

View File

@@ -66,7 +66,7 @@ public sealed class TypeChecker(string fileName, NodeDefinitionFunc function, Mo
{
NodeStatementAssignment statement => CheckStatementAssignment(statement),
NodeStatementBlock statement => CheckStatementBlock(statement),
NodeStatementFuncCall statement => CheckStatementFuncCall(statement),
NodeStatementExpression statement => CheckStatementExpression(statement),
NodeStatementIf statement => CheckStatementIf(statement),
NodeStatementReturn statement => CheckStatementReturn(statement),
NodeStatementVariableDeclaration statement => CheckStatementVariableDeclaration(statement),
@@ -85,9 +85,12 @@ public sealed class TypeChecker(string fileName, NodeDefinitionFunc function, Mo
return new TypedNodeStatementBlock(statement.Tokens, statement.Statements.Select(CheckStatement).ToList());
}
private TypedNodeStatementFuncCall CheckStatementFuncCall(NodeStatementFuncCall statement)
private TypedNodeStatementFuncCall CheckStatementExpression(NodeStatementExpression statement)
{
return new TypedNodeStatementFuncCall(statement.Tokens, CheckExpression(statement.Target), statement.Parameters.Select(CheckExpression).ToList());
if (statement.Expression is not NodeExpressionFuncCall funcCall)
throw new CompileException(Diagnostic.Error("Expected statement or function call").At(fileName, statement).Build());
return new TypedNodeStatementFuncCall(statement.Tokens, CheckExpression(funcCall.Target), funcCall.Parameters.Select(CheckExpression).ToList());
}
private TypedNodeStatementIf CheckStatementIf(NodeStatementIf statement)
@@ -129,6 +132,7 @@ public sealed class TypeChecker(string fileName, NodeDefinitionFunc function, Mo
NodeExpressionModuleIdent expression => CheckExpressionModuleIdent(expression),
NodeExpressionIntLiteral expression => CheckExpressionIntLiteral(expression),
NodeExpressionMemberAccess expression => CheckExpressionMemberAccess(expression),
NodeExpressionFuncCall expression => CheckExpressionFuncCall(expression),
NodeExpressionStringLiteral expression => CheckExpressionStringLiteral(expression),
NodeExpressionStructLiteral expression => CheckExpressionStructLiteral(expression),
_ => throw new ArgumentOutOfRangeException(nameof(node))
@@ -311,6 +315,17 @@ public sealed class TypeChecker(string fileName, NodeDefinitionFunc function, Mo
return new TypedNodeExpressionMemberAccess(expression.Tokens, field.Type, target, expression.Name);
}
private TypedNodeExpressionFuncCall CheckExpressionFuncCall(NodeExpressionFuncCall expression)
{
var target = CheckExpression(expression.Target);
if (target.Type is not NubTypeFunc funcType)
throw new CompileException(Diagnostic.Error($"Cannot invoke function call on type '{target.Type}'").At(fileName, target).Build());
var parameters = expression.Parameters.Select(CheckExpression).ToList();
return new TypedNodeExpressionFuncCall(expression.Tokens, funcType.ReturnType, target, parameters);
}
private TypedNodeExpressionStringLiteral CheckExpressionStringLiteral(NodeExpressionStringLiteral expression)
{
return new TypedNodeExpressionStringLiteral(expression.Tokens, NubTypeString.Instance, expression.Value);
@@ -491,6 +506,12 @@ public sealed class TypedNodeExpressionMemberAccess(List<Token> tokens, NubType
public TokenIdent Name { get; } = name;
}
public sealed class TypedNodeExpressionFuncCall(List<Token> tokens, NubType type, TypedNodeExpression target, List<TypedNodeExpression> parameters) : TypedNodeExpression(tokens, type)
{
public TypedNodeExpression Target { get; } = target;
public List<TypedNodeExpression> Parameters { get; } = parameters;
}
public sealed class TypedNodeExpressionLocalIdent(List<Token> tokens, NubType type, TokenIdent value) : TypedNodeExpression(tokens, type)
{
public TokenIdent Value { get; } = value;