This commit is contained in:
2026-02-08 00:53:55 +01:00
parent f2ea00b34d
commit 26d365cf4f
4 changed files with 110 additions and 44 deletions

View File

@@ -64,6 +64,12 @@ public sealed class Parser(List<Token> tokens)
return new NodeStatementBlock(TokensFrom(startIndex), statements);
}
if (TryExpectKeyword(Keyword.Return))
{
var value = ParseExpression();
return new NodeStatementReturn(TokensFrom(startIndex), value);
}
var expression = ParseExpression();
var parameters = new List<NodeExpression>();
@@ -111,14 +117,32 @@ public sealed class Parser(List<Token> tokens)
return new NodeTypePointer(TokensFrom(startIndex), to);
}
if (TryExpectKeyword(Keyword.Func))
{
var parameters = new List<NodeType>();
ExpectSymbol(Symbol.OpenParen);
while (!TryExpectSymbol(Symbol.CloseParen))
{
parameters.Add(ParseType());
}
ExpectSymbol(Symbol.Colon);
var returnType = ParseType();
return new NodeTypeFunc(TokensFrom(startIndex), parameters, returnType);
}
if (TryExpectIdent(out var ident))
{
switch (ident.Ident)
{
case "void":
return new NubTypeVoid(TokensFrom(startIndex));
return new NodeTypeVoid(TokensFrom(startIndex));
case "string":
return new NodeTypeString(TokensFrom(startIndex));
case "bool":
return new NodeTypeBool(TokensFrom(startIndex));
case "i8":
return new NodeTypeSInt(TokensFrom(startIndex), 8);
case "i16":
@@ -135,10 +159,6 @@ public sealed class Parser(List<Token> tokens)
return new NodeTypeUInt(TokensFrom(startIndex), 32);
case "u64":
return new NodeTypeUInt(TokensFrom(startIndex), 64);
case "f32":
return new NodeTypeFloat(TokensFrom(startIndex), 32);
case "f64":
return new NodeTypeFloat(TokensFrom(startIndex), 64);
default:
return new NodeTypeCustom(TokensFrom(startIndex), ident);
}
@@ -272,8 +292,7 @@ public abstract class Node(List<Token> tokens)
public abstract class NodeDefinition(List<Token> tokens) : Node(tokens);
public sealed class NodeDefinitionFunc(List<Token> tokens, TokenIdent name, List<NodeDefinitionFunc.Param> parameters, NodeStatement body, NodeType returnType)
: NodeDefinition(tokens)
public sealed class NodeDefinitionFunc(List<Token> tokens, TokenIdent name, List<NodeDefinitionFunc.Param> parameters, NodeStatement body, NodeType returnType) : NodeDefinition(tokens)
{
public readonly TokenIdent Name = name;
public readonly List<Param> Parameters = parameters;
@@ -300,6 +319,11 @@ public sealed class NodeStatementFuncCall(List<Token> tokens, NodeExpression fun
public readonly List<NodeExpression> Parameters = parameters;
}
internal class NodeStatementReturn(List<Token> tokens, NodeExpression value) : NodeStatement(tokens)
{
public readonly NodeExpression Value = value;
}
public abstract class NodeExpression(List<Token> tokens) : Node(tokens);
public sealed class NodeExpressionIntLiteral(List<Token> tokens, TokenIntLiteral value) : NodeExpression(tokens)
@@ -324,7 +348,7 @@ public sealed class NodeExpressionIdent(List<Token> tokens, TokenIdent value) :
public abstract class NodeType(List<Token> tokens) : Node(tokens);
public sealed class NubTypeVoid(List<Token> tokens) : NodeType(tokens);
public sealed class NodeTypeVoid(List<Token> tokens) : NodeType(tokens);
public sealed class NodeTypeUInt(List<Token> tokens, int width) : NodeType(tokens)
{
@@ -336,10 +360,7 @@ public sealed class NodeTypeSInt(List<Token> tokens, int width) : NodeType(token
public readonly int Width = width;
}
public sealed class NodeTypeFloat(List<Token> tokens, int width) : NodeType(tokens)
{
public readonly int Width = width;
}
public sealed class NodeTypeBool(List<Token> tokens) : NodeType(tokens);
public sealed class NodeTypeString(List<Token> tokens) : NodeType(tokens);
@@ -351,4 +372,10 @@ public sealed class NodeTypeCustom(List<Token> tokens, TokenIdent name) : NodeTy
public sealed class NodeTypePointer(List<Token> tokens, NodeType to) : NodeType(tokens)
{
public readonly NodeType To = to;
}
public sealed class NodeTypeFunc(List<Token> tokens, List<NodeType> parameters, NodeType returnType) : NodeType(tokens)
{
public readonly List<NodeType> Parameters = parameters;
public readonly NodeType ReturnType = returnType;
}