Module system

This commit is contained in:
nub31
2025-09-11 23:03:44 +02:00
parent 9c2975d45f
commit 0e442a8c4a
15 changed files with 265 additions and 167 deletions

View File

@@ -474,7 +474,7 @@ public sealed class Parser
var expr = token switch
{
LiteralToken literal => new LiteralSyntax(GetTokens(startIndex), literal.Value, literal.Kind),
IdentifierToken identifier => new LocalIdentifierSyntax(GetTokens(startIndex), identifier.Value),
IdentifierToken identifier => ParseIdentifier(startIndex, identifier),
SymbolToken symbolToken => symbolToken.Symbol switch
{
Symbol.OpenParen => ParseParenthesizedExpression(),
@@ -499,6 +499,17 @@ public sealed class Parser
return ParsePostfixOperators(expr);
}
private ExpressionSyntax ParseIdentifier(int startIndex, IdentifierToken identifier)
{
if (TryExpectSymbol(Symbol.DoubleColon))
{
var name = ExpectIdentifier();
return new ModuleIdentifierSyntax(GetTokens(startIndex), identifier.Value, name.Value);
}
return new LocalIdentifierSyntax(GetTokens(startIndex), identifier.Value);
}
private ExpressionSyntax ParseParenthesizedExpression()
{
var expression = ParseExpression();
@@ -690,14 +701,27 @@ public sealed class Parser
return new FloatTypeSyntax(GetTokens(startIndex), size);
}
return name.Value switch
switch (name.Value)
{
"void" => new VoidTypeSyntax(GetTokens(startIndex)),
"string" => new StringTypeSyntax(GetTokens(startIndex)),
"cstring" => new CStringTypeSyntax(GetTokens(startIndex)),
"bool" => new BoolTypeSyntax(GetTokens(startIndex)),
_ => new CustomTypeSyntax(GetTokens(startIndex), _moduleName, name.Value)
};
case "void":
return new VoidTypeSyntax(GetTokens(startIndex));
case "string":
return new StringTypeSyntax(GetTokens(startIndex));
case "cstring":
return new CStringTypeSyntax(GetTokens(startIndex));
case "bool":
return new BoolTypeSyntax(GetTokens(startIndex));
default:
{
if (TryExpectSymbol(Symbol.DoubleColon))
{
var customTypeName = ExpectIdentifier().Value;
return new CustomTypeSyntax(GetTokens(startIndex), name.Value, customTypeName);
}
return new CustomTypeSyntax(GetTokens(startIndex), _moduleName, name.Value);
}
}
}
if (TryExpectSymbol(Symbol.Caret))