This commit is contained in:
nub31
2026-02-27 22:20:32 +01:00
parent e512650440
commit 73ecbf8e9b
10 changed files with 163 additions and 90 deletions

View File

@@ -65,6 +65,10 @@ public class Parser
Next();
modifiers[Keyword.Packed] = keyword;
break;
case Keyword.Extern:
Next();
modifiers[Keyword.Extern] = keyword;
break;
default:
goto modifier_done;
}
@@ -75,6 +79,7 @@ public class Parser
if (TryExpectKeyword(Keyword.Func))
{
var exported = modifiers.Remove(Keyword.Export);
var @extern = modifiers.Remove(Keyword.Extern);
foreach (var modifier in modifiers)
// todo(nub31): Add to diagnostics instead of throwing
@@ -93,12 +98,19 @@ public class Parser
parameters.Add(new NodeDefinitionFunc.Param(TokensFrom(paramStartIndex), parameterName, parameterType));
}
ExpectSymbol(Symbol.Colon);
var returnType = ParseType();
NodeType? returnType = null;
if (TryExpectSymbol(Symbol.Colon))
returnType = ParseType();
var body = ParseStatement();
return new NodeDefinitionFunc(TokensFrom(startIndex), exported, name, parameters, body, returnType);
if (@extern)
{
return new NodeDefinitionExternFunc(TokensFrom(startIndex), exported, name, parameters, returnType);
}
else
{
var body = ParseStatement();
return new NodeDefinitionFunc(TokensFrom(startIndex), exported, name, parameters, body, returnType);
}
}
if (TryExpectKeyword(Keyword.Struct))
@@ -704,13 +716,21 @@ public abstract class Node(List<Token> tokens)
public abstract class NodeDefinition(List<Token> tokens) : Node(tokens);
public class NodeDefinitionFunc(List<Token> tokens, bool exported, TokenIdent name, List<NodeDefinitionFunc.Param> parameters, NodeStatement body, NodeType returnType) : NodeDefinition(tokens)
public class NodeDefinitionExternFunc(List<Token> tokens, bool exported, TokenIdent name, List<NodeDefinitionFunc.Param> parameters, NodeType? returnType) : NodeDefinition(tokens)
{
public bool Exported { get; } = exported;
public TokenIdent Name { get; } = name;
public List<NodeDefinitionFunc.Param> Parameters { get; } = parameters;
public NodeType? ReturnType { get; } = returnType;
}
public class NodeDefinitionFunc(List<Token> tokens, bool exported, TokenIdent name, List<NodeDefinitionFunc.Param> parameters, NodeStatement body, NodeType? returnType) : NodeDefinition(tokens)
{
public bool Exported { get; } = exported;
public TokenIdent Name { get; } = name;
public List<Param> Parameters { get; } = parameters;
public NodeStatement Body { get; } = body;
public NodeType ReturnType { get; } = returnType;
public NodeType? ReturnType { get; } = returnType;
public class Param(List<Token> tokens, TokenIdent name, NodeType type) : Node(tokens)
{
@@ -746,6 +766,13 @@ public class NodeDefinitionEnum(List<Token> tokens, bool exported, TokenIdent na
}
}
public class NodeDefinitionExternGlobalVariable(List<Token> tokens, bool exported, TokenIdent name, NodeType type) : NodeDefinition(tokens)
{
public bool Exported { get; } = exported;
public TokenIdent Name { get; } = name;
public NodeType Type { get; } = type;
}
public class NodeDefinitionGlobalVariable(List<Token> tokens, bool exported, TokenIdent name, NodeType type) : NodeDefinition(tokens)
{
public bool Exported { get; } = exported;