Add arrays
This commit is contained in:
@@ -14,6 +14,7 @@ public class Lexer
|
||||
["else"] = Symbol.Else,
|
||||
["while"] = Symbol.While,
|
||||
["return"] = Symbol.Return,
|
||||
["new"] = Symbol.New,
|
||||
};
|
||||
|
||||
private static readonly Dictionary<char[], Symbol> Chians = new()
|
||||
|
||||
@@ -38,4 +38,5 @@ public enum Symbol
|
||||
Minus,
|
||||
Star,
|
||||
ForwardSlash,
|
||||
New,
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
namespace Nub.Lang.Frontend.Parsing;
|
||||
|
||||
public class ArrayIndexAccessNode(IdentifierNode identifier, ExpressionNode index) : ExpressionNode
|
||||
{
|
||||
public IdentifierNode Identifier { get; } = identifier;
|
||||
public ExpressionNode Index { get; } = index;
|
||||
|
||||
public override string ToString() => $"{Identifier}[{Index}]";
|
||||
}
|
||||
@@ -0,0 +1,8 @@
|
||||
namespace Nub.Lang.Frontend.Parsing;
|
||||
|
||||
public class ArrayIndexAssignmentNode(IdentifierNode identifier, ExpressionNode index, ExpressionNode value) : StatementNode
|
||||
{
|
||||
public IdentifierNode Identifier { get; } = identifier;
|
||||
public ExpressionNode Index { get; } = index;
|
||||
public ExpressionNode Value { get; } = value;
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
namespace Nub.Lang.Frontend.Parsing;
|
||||
|
||||
public class ArrayInitializerNode(long length, Type innerType) : ExpressionNode
|
||||
{
|
||||
public long Length { get; } = length;
|
||||
public Type InnerType { get; } = innerType;
|
||||
}
|
||||
@@ -4,8 +4,5 @@ public class IdentifierNode(string identifier) : ExpressionNode
|
||||
{
|
||||
public string Identifier { get; } = identifier;
|
||||
|
||||
public override string ToString()
|
||||
{
|
||||
return Identifier;
|
||||
}
|
||||
public override string ToString() => Identifier;
|
||||
}
|
||||
@@ -149,6 +149,15 @@ public class Parser
|
||||
|
||||
return new FuncCallStatementNode(new FuncCall(identifier.Value, parameters));
|
||||
}
|
||||
case Symbol.OpenBracket:
|
||||
{
|
||||
var index = ParseExpression();
|
||||
ExpectSymbol(Symbol.CloseBracket);
|
||||
ExpectSymbol(Symbol.Assign);
|
||||
var value = ParseExpression();
|
||||
ExpectSymbol(Symbol.Semicolon);
|
||||
return new ArrayIndexAssignmentNode(new IdentifierNode(identifier.Value), index, value);
|
||||
}
|
||||
case Symbol.Assign:
|
||||
{
|
||||
var value = ParseExpression();
|
||||
@@ -310,10 +319,32 @@ public class Parser
|
||||
return new LiteralNode(literal.Value, literal.Type);
|
||||
case IdentifierToken identifier:
|
||||
return ParseExpressionIdentifier(identifier);
|
||||
case SymbolToken { Symbol: Symbol.OpenParen }:
|
||||
var expression = ParseExpression();
|
||||
ExpectSymbol(Symbol.CloseParen);
|
||||
return expression;
|
||||
case SymbolToken symbolToken:
|
||||
{
|
||||
switch (symbolToken.Symbol)
|
||||
{
|
||||
case Symbol.OpenParen:
|
||||
{
|
||||
var expression = ParseExpression();
|
||||
ExpectSymbol(Symbol.CloseParen);
|
||||
return expression;
|
||||
}
|
||||
case Symbol.New:
|
||||
{
|
||||
var type = ParseType();
|
||||
ExpectSymbol(Symbol.OpenParen);
|
||||
var size = ExpectLiteral();
|
||||
if (size.Type is not PrimitiveType { Kind: PrimitiveTypeKind.Int64 })
|
||||
{
|
||||
throw new Exception($"Array initializer size must be an {PrimitiveTypeKind.Int64}");
|
||||
}
|
||||
ExpectSymbol(Symbol.CloseParen);
|
||||
return new ArrayInitializerNode(long.Parse(size.Value), type);
|
||||
}
|
||||
default:
|
||||
throw new Exception($"Unknown symbol: {symbolToken.Symbol}");
|
||||
}
|
||||
}
|
||||
default:
|
||||
throw new Exception($"Unexpected token type {token.GetType().Name}");
|
||||
}
|
||||
@@ -321,6 +352,13 @@ public class Parser
|
||||
|
||||
private ExpressionNode ParseExpressionIdentifier(IdentifierToken identifier)
|
||||
{
|
||||
if (TryExpectSymbol(Symbol.OpenBracket))
|
||||
{
|
||||
var index = ParseExpression();
|
||||
ExpectSymbol(Symbol.CloseBracket);
|
||||
return new ArrayIndexAccessNode(new IdentifierNode(identifier.Value), index);
|
||||
}
|
||||
|
||||
if (TryExpectSymbol(Symbol.OpenParen))
|
||||
{
|
||||
List<ExpressionNode> parameters = [];
|
||||
@@ -356,30 +394,42 @@ public class Parser
|
||||
private Type ParseType()
|
||||
{
|
||||
var name = ExpectIdentifier().Value;
|
||||
if (name == "Func")
|
||||
|
||||
switch (name)
|
||||
{
|
||||
List<Type> typeArguments = [];
|
||||
if (TryExpectSymbol(Symbol.LessThan))
|
||||
case "Func":
|
||||
{
|
||||
while (!TryExpectSymbol(Symbol.GreaterThan))
|
||||
List<Type> typeArguments = [];
|
||||
if (TryExpectSymbol(Symbol.LessThan))
|
||||
{
|
||||
var type = ParseType();
|
||||
typeArguments.Add(type);
|
||||
TryExpectSymbol(Symbol.Comma);
|
||||
while (!TryExpectSymbol(Symbol.GreaterThan))
|
||||
{
|
||||
var type = ParseType();
|
||||
typeArguments.Add(type);
|
||||
TryExpectSymbol(Symbol.Comma);
|
||||
}
|
||||
}
|
||||
|
||||
var returnType = Optional<Type>.OfNullable(typeArguments.LastOrDefault());
|
||||
|
||||
return new DelegateType(typeArguments.Take(typeArguments.Count - 1).ToList(), returnType);
|
||||
}
|
||||
case "String":
|
||||
{
|
||||
return new StringType();
|
||||
}
|
||||
case "Array":
|
||||
{
|
||||
ExpectSymbol(Symbol.LessThan);
|
||||
var innerType = ParseType();
|
||||
ExpectSymbol(Symbol.GreaterThan);
|
||||
return new ArrayType(innerType);
|
||||
}
|
||||
default:
|
||||
{
|
||||
return PrimitiveType.Parse(name);
|
||||
}
|
||||
|
||||
var returnType = Optional<Type>.OfNullable(typeArguments.LastOrDefault());
|
||||
|
||||
return new DelegateType(typeArguments.Take(typeArguments.Count - 1).ToList(), returnType);
|
||||
}
|
||||
|
||||
if (name == "String")
|
||||
{
|
||||
return new StringType();
|
||||
}
|
||||
|
||||
return PrimitiveType.Parse(name);
|
||||
}
|
||||
|
||||
private Token ExpectToken()
|
||||
|
||||
@@ -83,6 +83,9 @@ public class ExpressionTyper
|
||||
{
|
||||
switch (statement)
|
||||
{
|
||||
case ArrayIndexAssignmentNode arrayIndexAssignment:
|
||||
PopulateArrayIndexAssignment(arrayIndexAssignment);
|
||||
break;
|
||||
case FuncCallStatementNode funcCall:
|
||||
PopulateFuncCallStatement(funcCall);
|
||||
break;
|
||||
@@ -109,6 +112,13 @@ public class ExpressionTyper
|
||||
}
|
||||
}
|
||||
|
||||
private void PopulateArrayIndexAssignment(ArrayIndexAssignmentNode arrayIndexAssignment)
|
||||
{
|
||||
PopulateIdentifier(arrayIndexAssignment.Identifier);
|
||||
PopulateExpression(arrayIndexAssignment.Index);
|
||||
PopulateExpression(arrayIndexAssignment.Value);
|
||||
}
|
||||
|
||||
private void PopulateFuncCallStatement(FuncCallStatementNode funcCall)
|
||||
{
|
||||
foreach (var parameter in funcCall.FuncCall.Parameters)
|
||||
@@ -168,6 +178,12 @@ public class ExpressionTyper
|
||||
{
|
||||
switch (expression)
|
||||
{
|
||||
case ArrayIndexAccessNode arrayIndexAccess:
|
||||
PopulateArrayIndexAccess(arrayIndexAccess);
|
||||
break;
|
||||
case ArrayInitializerNode arrayInitializer:
|
||||
PopulateArrayInitializer(arrayInitializer);
|
||||
break;
|
||||
case BinaryExpressionNode binaryExpression:
|
||||
PopulateBinaryExpression(binaryExpression);
|
||||
break;
|
||||
@@ -188,6 +204,30 @@ public class ExpressionTyper
|
||||
}
|
||||
}
|
||||
|
||||
private void PopulateArrayIndexAccess(ArrayIndexAccessNode arrayIndexAccess)
|
||||
{
|
||||
PopulateExpression(arrayIndexAccess.Index);
|
||||
PopulateIdentifier(arrayIndexAccess.Identifier);
|
||||
|
||||
var variable = _variables.FirstOrDefault(v => v.Name == arrayIndexAccess.Identifier.Identifier);
|
||||
if (variable == null)
|
||||
{
|
||||
throw new Exception($"Variable {arrayIndexAccess.Identifier} is not defined");
|
||||
}
|
||||
|
||||
if (variable.Type is not ArrayType arrayType)
|
||||
{
|
||||
throw new Exception($"Variable {arrayIndexAccess.Identifier} is not an array type");
|
||||
}
|
||||
|
||||
arrayIndexAccess.Type = arrayType.InnerType;
|
||||
}
|
||||
|
||||
private void PopulateArrayInitializer(ArrayInitializerNode arrayInitializer)
|
||||
{
|
||||
arrayInitializer.Type = arrayInitializer.InnerType;
|
||||
}
|
||||
|
||||
private void PopulateBinaryExpression(BinaryExpressionNode binaryExpression)
|
||||
{
|
||||
PopulateExpression(binaryExpression.Left);
|
||||
|
||||
Reference in New Issue
Block a user