...
This commit is contained in:
@@ -271,19 +271,10 @@ public static class Parser
|
||||
{
|
||||
ExpectSymbol(Symbol.Let);
|
||||
var name = ExpectIdentifier().Value;
|
||||
var type = Optional<NubType>.Empty();
|
||||
if (TryExpectSymbol(Symbol.Colon))
|
||||
{
|
||||
type = ParseType();
|
||||
}
|
||||
|
||||
var value = Optional<ExpressionNode>.Empty();
|
||||
if (TryExpectSymbol(Symbol.Assign))
|
||||
{
|
||||
value = ParseExpression();
|
||||
}
|
||||
|
||||
return new VariableDeclarationNode(GetTokensForNode(startIndex), name, type, value);
|
||||
ExpectSymbol(Symbol.Colon);
|
||||
var type = ParseType();
|
||||
|
||||
return new VariableDeclarationNode(GetTokensForNode(startIndex), name, type);
|
||||
}
|
||||
|
||||
private static StatementNode ParseBreak(int startIndex)
|
||||
@@ -674,6 +665,11 @@ public static class Parser
|
||||
return new NubVoidType();
|
||||
}
|
||||
|
||||
if (name.Value == "string")
|
||||
{
|
||||
return new NubArrayType(NubPrimitiveType.U8);
|
||||
}
|
||||
|
||||
if (NubPrimitiveType.TryParse(name.Value, out var primitiveTypeKind))
|
||||
{
|
||||
return new NubPrimitiveType(primitiveTypeKind.Value);
|
||||
|
||||
@@ -1,13 +1,10 @@
|
||||
using Common;
|
||||
using Syntax.Parsing.Expressions;
|
||||
using Syntax.Tokenization;
|
||||
using Syntax.Tokenization;
|
||||
using Syntax.Typing;
|
||||
|
||||
namespace Syntax.Parsing.Statements;
|
||||
|
||||
public class VariableDeclarationNode(IReadOnlyList<Token> tokens, string name, Optional<NubType> explicitType, Optional<ExpressionNode> value) : StatementNode(tokens)
|
||||
public class VariableDeclarationNode(IReadOnlyList<Token> tokens, string name, NubType type) : StatementNode(tokens)
|
||||
{
|
||||
public string Name { get; } = name;
|
||||
public Optional<NubType> ExplicitType { get; } = explicitType;
|
||||
public Optional<ExpressionNode> Value { get; } = value;
|
||||
public NubType Type { get; } = type;
|
||||
}
|
||||
@@ -177,42 +177,12 @@ public static class TypeChecker
|
||||
|
||||
private static void CheckVariableVariableDeclaration(VariableDeclarationNode variableDeclaration)
|
||||
{
|
||||
NubType? type = null;
|
||||
|
||||
if (_variables.TryGetValue(variableDeclaration.Name, out var variable))
|
||||
{
|
||||
ReportError($"Cannot redeclare variable '{variable}'", variableDeclaration);
|
||||
}
|
||||
|
||||
if (variableDeclaration.Value.HasValue)
|
||||
{
|
||||
var valueType = CheckExpression(variableDeclaration.Value.Value, variableDeclaration.ExplicitType.Value);
|
||||
if (valueType == null) return;
|
||||
type = valueType;
|
||||
}
|
||||
|
||||
if (variableDeclaration.ExplicitType.HasValue)
|
||||
{
|
||||
type = variableDeclaration.ExplicitType.Value;
|
||||
}
|
||||
|
||||
if (variableDeclaration.ExplicitType.HasValue && variableDeclaration.Value.HasValue)
|
||||
{
|
||||
if (!NubType.IsCompatibleWith(variableDeclaration.ExplicitType.Value, variableDeclaration.Value.Value.Type))
|
||||
{
|
||||
ReportError(
|
||||
$"Cannot assign expression of type '{variableDeclaration.Value.Value.Type}' to variable '{variableDeclaration.Name}' with type '{variableDeclaration.ExplicitType.Value}'",
|
||||
variableDeclaration);
|
||||
}
|
||||
}
|
||||
|
||||
if (type == null)
|
||||
{
|
||||
ReportError($"Cannot implicitly get type of variable '{variableDeclaration.Name}'", variableDeclaration);
|
||||
return;
|
||||
}
|
||||
|
||||
_variables[variableDeclaration.Name] = type;
|
||||
_variables[variableDeclaration.Name] = variableDeclaration.Type;
|
||||
}
|
||||
|
||||
private static NubType? CheckDereference(DereferenceNode dereference)
|
||||
@@ -246,6 +216,7 @@ public static class TypeChecker
|
||||
if (funcCall.Parameters.Count != funcType.Parameters.Count)
|
||||
{
|
||||
ReportError($"{funcType} expects {funcType.Parameters.Count} arguments, but was called with {funcType.Parameters.Count} arguments", funcCall);
|
||||
return null;
|
||||
}
|
||||
|
||||
for (var i = 0; i < funcCall.Parameters.Count; i++)
|
||||
|
||||
Reference in New Issue
Block a user