Remove alloc keyword for struct
This commit is contained in:
@@ -23,7 +23,7 @@ struct Human : Test
|
||||
|
||||
func main(args: []cstring): i64
|
||||
{
|
||||
let human: Test = alloc Human {
|
||||
let human: Human = struct {
|
||||
name = "oliver"
|
||||
}
|
||||
|
||||
|
||||
@@ -270,7 +270,6 @@ public class Diagnostic
|
||||
case Symbol.Continue:
|
||||
case Symbol.Struct:
|
||||
case Symbol.Let:
|
||||
case Symbol.Alloc:
|
||||
case Symbol.Calls:
|
||||
case Symbol.Interface:
|
||||
case Symbol.For:
|
||||
|
||||
@@ -416,10 +416,10 @@ public sealed class Parser
|
||||
Symbol.Minus => new UnaryExpressionSyntax(GetTokens(startIndex), UnaryOperatorSyntax.Negate, ParsePrimaryExpression()),
|
||||
Symbol.Bang => new UnaryExpressionSyntax(GetTokens(startIndex), UnaryOperatorSyntax.Invert, ParsePrimaryExpression()),
|
||||
Symbol.OpenBracket => ParseArrayInitializer(startIndex),
|
||||
Symbol.Alloc => ParseStructInitializer(startIndex),
|
||||
Symbol.Struct => ParseStructInitializer(startIndex),
|
||||
_ => throw new ParseException(Diagnostic
|
||||
.Error($"Unexpected symbol '{symbolToken.Symbol}' in expression")
|
||||
.WithHelp("Expected literal, identifier, or '(' to start expression")
|
||||
.WithHelp("Expected '(', '-', '!', '[' or '{'")
|
||||
.At(symbolToken)
|
||||
.Build())
|
||||
},
|
||||
@@ -440,30 +440,6 @@ public sealed class Parser
|
||||
return expression;
|
||||
}
|
||||
|
||||
private ExpressionSyntax ParseArrayInitializer(int startIndex)
|
||||
{
|
||||
var capacity = ParseExpression();
|
||||
ExpectSymbol(Symbol.CloseBracket);
|
||||
var type = ParseType();
|
||||
return new ArrayInitializerSyntax(GetTokens(startIndex), capacity, type);
|
||||
}
|
||||
|
||||
private ExpressionSyntax ParseStructInitializer(int startIndex)
|
||||
{
|
||||
var type = ParseType();
|
||||
Dictionary<string, ExpressionSyntax> initializers = [];
|
||||
ExpectSymbol(Symbol.OpenBrace);
|
||||
while (!TryExpectSymbol(Symbol.CloseBrace))
|
||||
{
|
||||
var name = ExpectIdentifier().Value;
|
||||
ExpectSymbol(Symbol.Assign);
|
||||
var value = ParseExpression();
|
||||
initializers.Add(name, value);
|
||||
}
|
||||
|
||||
return new StructInitializerSyntax(GetTokens(startIndex), type, initializers);
|
||||
}
|
||||
|
||||
private ExpressionSyntax ParsePostfixOperators(ExpressionSyntax expr)
|
||||
{
|
||||
var startIndex = _tokenIndex;
|
||||
@@ -538,6 +514,35 @@ public sealed class Parser
|
||||
return expr;
|
||||
}
|
||||
|
||||
private ArrayInitializerSyntax ParseArrayInitializer(int startIndex)
|
||||
{
|
||||
var capacity = ParseExpression();
|
||||
ExpectSymbol(Symbol.CloseBracket);
|
||||
var type = ParseType();
|
||||
return new ArrayInitializerSyntax(GetTokens(startIndex), capacity, type);
|
||||
}
|
||||
|
||||
private StructInitializerSyntax ParseStructInitializer(int startIndex)
|
||||
{
|
||||
var type = Optional.Empty<TypeSyntax>();
|
||||
if (!TryExpectSymbol(Symbol.OpenBrace))
|
||||
{
|
||||
type = ParseType();
|
||||
ExpectSymbol(Symbol.OpenBrace);
|
||||
}
|
||||
|
||||
Dictionary<string, ExpressionSyntax> initializers = [];
|
||||
while (!TryExpectSymbol(Symbol.CloseBrace))
|
||||
{
|
||||
var name = ExpectIdentifier().Value;
|
||||
ExpectSymbol(Symbol.Assign);
|
||||
var value = ParseExpression();
|
||||
initializers.Add(name, value);
|
||||
}
|
||||
|
||||
return new StructInitializerSyntax(GetTokens(startIndex), type, initializers);
|
||||
}
|
||||
|
||||
private BlockSyntax ParseBlock()
|
||||
{
|
||||
var startIndex = _tokenIndex;
|
||||
|
||||
@@ -44,6 +44,6 @@ public record LiteralSyntax(IEnumerable<Token> Tokens, string Value, LiteralKind
|
||||
|
||||
public record StructFieldAccessSyntax(IEnumerable<Token> Tokens, ExpressionSyntax Target, string Member) : ExpressionSyntax(Tokens);
|
||||
|
||||
public record StructInitializerSyntax(IEnumerable<Token> Tokens, TypeSyntax StructType, Dictionary<string, ExpressionSyntax> Initializers) : ExpressionSyntax(Tokens);
|
||||
public record StructInitializerSyntax(IEnumerable<Token> Tokens, Optional<TypeSyntax> StructType, Dictionary<string, ExpressionSyntax> Initializers) : ExpressionSyntax(Tokens);
|
||||
|
||||
public record DereferenceSyntax(IEnumerable<Token> Tokens, ExpressionSyntax Expression) : ExpressionSyntax(Tokens);
|
||||
@@ -65,7 +65,6 @@ public enum Symbol
|
||||
Caret,
|
||||
Ampersand,
|
||||
Let,
|
||||
Alloc,
|
||||
Calls,
|
||||
Interface,
|
||||
For,
|
||||
|
||||
@@ -14,7 +14,6 @@ public sealed class Tokenizer
|
||||
["break"] = Symbol.Break,
|
||||
["continue"] = Symbol.Continue,
|
||||
["return"] = Symbol.Return,
|
||||
["alloc"] = Symbol.Alloc,
|
||||
["struct"] = Symbol.Struct,
|
||||
["let"] = Symbol.Let,
|
||||
["calls"] = Symbol.Calls,
|
||||
|
||||
@@ -276,7 +276,7 @@ public sealed class TypeChecker
|
||||
IdentifierSyntax expression => CheckIdentifier(expression),
|
||||
LiteralSyntax expression => CheckLiteral(expression, expectedType),
|
||||
StructFieldAccessSyntax expression => CheckStructFieldAccess(expression),
|
||||
StructInitializerSyntax expression => CheckStructInitializer(expression),
|
||||
StructInitializerSyntax expression => CheckStructInitializer(expression, expectedType),
|
||||
UnaryExpressionSyntax expression => CheckUnaryExpression(expression),
|
||||
_ => throw new ArgumentOutOfRangeException(nameof(node))
|
||||
};
|
||||
@@ -475,7 +475,7 @@ public sealed class TypeChecker
|
||||
throw new TypeCheckerException(Diagnostic.Error($"No identifier with the name {expression.Name} exists").Build());
|
||||
}
|
||||
|
||||
private LiteralNode CheckLiteral(LiteralSyntax expression, TypeNode? expectedType = null)
|
||||
private LiteralNode CheckLiteral(LiteralSyntax expression, TypeNode? expectedType)
|
||||
{
|
||||
var type = expectedType ?? expression.Kind switch
|
||||
{
|
||||
@@ -523,11 +523,20 @@ public sealed class TypeChecker
|
||||
throw new TypeCheckerException(Diagnostic.Error($"{boundExpression.Type} does not have a member with the name {expression.Member}").Build());
|
||||
}
|
||||
|
||||
private StructInitializerNode CheckStructInitializer(StructInitializerSyntax expression)
|
||||
private StructInitializerNode CheckStructInitializer(StructInitializerSyntax expression, TypeNode? expectedType)
|
||||
{
|
||||
var boundType = CheckType(expression.StructType);
|
||||
var type = expectedType;
|
||||
if (expression.StructType.HasValue)
|
||||
{
|
||||
type = CheckType(expression.StructType.Value);
|
||||
}
|
||||
|
||||
if (boundType is not StructTypeNode structType)
|
||||
if (type == null)
|
||||
{
|
||||
throw new TypeCheckerException(Diagnostic.Error("Cannot determine type of struct").Build());
|
||||
}
|
||||
|
||||
if (type is not StructTypeNode structType)
|
||||
{
|
||||
throw new TypeCheckerException(Diagnostic.Error($"Cannot initialize non-struct type {expression.StructType}").Build());
|
||||
}
|
||||
@@ -598,7 +607,7 @@ public sealed class TypeChecker
|
||||
|
||||
if (type == null)
|
||||
{
|
||||
throw new NotImplementedException("Diagnostics not implemented");
|
||||
throw new TypeCheckerException(Diagnostic.Error($"Cannot perform unary operation {expression.Operand} on type {boundOperand.Type}").Build());
|
||||
}
|
||||
|
||||
return new UnaryExpressionNode(type, CheckUnaryOperator(expression.OperatorSyntax), boundOperand);
|
||||
|
||||
Reference in New Issue
Block a user