Array type works with args
This commit is contained in:
7
src/compiler/Nub.Lang/Frontend/Parsing/ArrayIndexNode.cs
Normal file
7
src/compiler/Nub.Lang/Frontend/Parsing/ArrayIndexNode.cs
Normal file
@@ -0,0 +1,7 @@
|
||||
namespace Nub.Lang.Frontend.Parsing;
|
||||
|
||||
public class ArrayIndexNode(ExpressionNode expression, ExpressionNode index) : ExpressionNode
|
||||
{
|
||||
public ExpressionNode Expression { get; } = expression;
|
||||
public ExpressionNode Index { get; } = index;
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
namespace Nub.Lang.Frontend.Parsing;
|
||||
|
||||
public class MemberAccessNode(ExpressionNode expression, string member) : ExpressionNode
|
||||
{
|
||||
public ExpressionNode Expression { get; } = expression;
|
||||
public string Member { get; } = member;
|
||||
}
|
||||
@@ -427,17 +427,17 @@ public class Parser
|
||||
if (TryExpectSymbol(Symbol.Period))
|
||||
{
|
||||
var structMember = ExpectIdentifier().Value;
|
||||
expr = new StructFieldAccessorNode(expr, structMember);
|
||||
expr = new MemberAccessNode(expr, structMember);
|
||||
continue;
|
||||
}
|
||||
|
||||
// if (TryExpectSymbol(Symbol.OpenBracket))
|
||||
// {
|
||||
// var index = ParseExpression();
|
||||
// ExpectSymbol(Symbol.CloseBracket);
|
||||
// expr = new ArrayIndexNode(expr, index);
|
||||
// continue;
|
||||
// }
|
||||
if (TryExpectSymbol(Symbol.OpenBracket))
|
||||
{
|
||||
var index = ParseExpression();
|
||||
ExpectSymbol(Symbol.CloseBracket);
|
||||
expr = new ArrayIndexNode(expr, index);
|
||||
continue;
|
||||
}
|
||||
|
||||
break;
|
||||
}
|
||||
@@ -459,10 +459,25 @@ public class Parser
|
||||
|
||||
private NubType ParseType()
|
||||
{
|
||||
var pointer = TryExpectSymbol(Symbol.Caret);
|
||||
var name = ExpectIdentifier().Value;
|
||||
var type = NubType.Parse(name);
|
||||
return pointer ? new NubPointerType(type) : type;
|
||||
if (TryExpectIdentifier(out var name))
|
||||
{
|
||||
return NubType.Parse(name);
|
||||
}
|
||||
|
||||
if (TryExpectSymbol(Symbol.Caret))
|
||||
{
|
||||
var baseType = ParseType();
|
||||
return new NubPointerType(baseType);
|
||||
}
|
||||
|
||||
if (TryExpectSymbol(Symbol.OpenBracket))
|
||||
{
|
||||
ExpectSymbol(Symbol.CloseBracket);
|
||||
var baseType = ParseType();
|
||||
return new NubArrayType(baseType);
|
||||
}
|
||||
|
||||
throw new Exception($"Unexpected token {Peek()} when parsing type");
|
||||
}
|
||||
|
||||
private Token ExpectToken()
|
||||
@@ -517,6 +532,19 @@ public class Parser
|
||||
return false;
|
||||
}
|
||||
|
||||
private bool TryExpectIdentifier([NotNullWhen(true)] out string? identifier)
|
||||
{
|
||||
if (Peek() is { HasValue: true, Value: IdentifierToken identifierToken })
|
||||
{
|
||||
identifier = identifierToken.Value;
|
||||
Next();
|
||||
return true;
|
||||
}
|
||||
|
||||
identifier = null;
|
||||
return false;
|
||||
}
|
||||
|
||||
private IdentifierToken ExpectIdentifier()
|
||||
{
|
||||
var token = ExpectToken();
|
||||
|
||||
@@ -1,7 +0,0 @@
|
||||
namespace Nub.Lang.Frontend.Parsing;
|
||||
|
||||
public class StructFieldAccessorNode(ExpressionNode @struct, string field) : ExpressionNode
|
||||
{
|
||||
public ExpressionNode Struct { get; } = @struct;
|
||||
public string Field { get; } = field;
|
||||
}
|
||||
Reference in New Issue
Block a user