Remove alloc keyword for struct

This commit is contained in:
nub31
2025-08-18 14:13:52 +02:00
parent 456e13cc6e
commit 08cb6b9f41
7 changed files with 48 additions and 37 deletions

View File

@@ -23,7 +23,7 @@ struct Human : Test
func main(args: []cstring): i64 func main(args: []cstring): i64
{ {
let human: Test = alloc Human { let human: Human = struct {
name = "oliver" name = "oliver"
} }

View File

@@ -270,7 +270,6 @@ public class Diagnostic
case Symbol.Continue: case Symbol.Continue:
case Symbol.Struct: case Symbol.Struct:
case Symbol.Let: case Symbol.Let:
case Symbol.Alloc:
case Symbol.Calls: case Symbol.Calls:
case Symbol.Interface: case Symbol.Interface:
case Symbol.For: case Symbol.For:

View File

@@ -416,10 +416,10 @@ public sealed class Parser
Symbol.Minus => new UnaryExpressionSyntax(GetTokens(startIndex), UnaryOperatorSyntax.Negate, ParsePrimaryExpression()), Symbol.Minus => new UnaryExpressionSyntax(GetTokens(startIndex), UnaryOperatorSyntax.Negate, ParsePrimaryExpression()),
Symbol.Bang => new UnaryExpressionSyntax(GetTokens(startIndex), UnaryOperatorSyntax.Invert, ParsePrimaryExpression()), Symbol.Bang => new UnaryExpressionSyntax(GetTokens(startIndex), UnaryOperatorSyntax.Invert, ParsePrimaryExpression()),
Symbol.OpenBracket => ParseArrayInitializer(startIndex), Symbol.OpenBracket => ParseArrayInitializer(startIndex),
Symbol.Alloc => ParseStructInitializer(startIndex), Symbol.Struct => ParseStructInitializer(startIndex),
_ => throw new ParseException(Diagnostic _ => throw new ParseException(Diagnostic
.Error($"Unexpected symbol '{symbolToken.Symbol}' in expression") .Error($"Unexpected symbol '{symbolToken.Symbol}' in expression")
.WithHelp("Expected literal, identifier, or '(' to start expression") .WithHelp("Expected '(', '-', '!', '[' or '{'")
.At(symbolToken) .At(symbolToken)
.Build()) .Build())
}, },
@@ -440,30 +440,6 @@ public sealed class Parser
return expression; 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) private ExpressionSyntax ParsePostfixOperators(ExpressionSyntax expr)
{ {
var startIndex = _tokenIndex; var startIndex = _tokenIndex;
@@ -538,6 +514,35 @@ public sealed class Parser
return expr; 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() private BlockSyntax ParseBlock()
{ {
var startIndex = _tokenIndex; var startIndex = _tokenIndex;

View File

@@ -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 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); public record DereferenceSyntax(IEnumerable<Token> Tokens, ExpressionSyntax Expression) : ExpressionSyntax(Tokens);

View File

@@ -65,7 +65,6 @@ public enum Symbol
Caret, Caret,
Ampersand, Ampersand,
Let, Let,
Alloc,
Calls, Calls,
Interface, Interface,
For, For,

View File

@@ -14,7 +14,6 @@ public sealed class Tokenizer
["break"] = Symbol.Break, ["break"] = Symbol.Break,
["continue"] = Symbol.Continue, ["continue"] = Symbol.Continue,
["return"] = Symbol.Return, ["return"] = Symbol.Return,
["alloc"] = Symbol.Alloc,
["struct"] = Symbol.Struct, ["struct"] = Symbol.Struct,
["let"] = Symbol.Let, ["let"] = Symbol.Let,
["calls"] = Symbol.Calls, ["calls"] = Symbol.Calls,

View File

@@ -276,7 +276,7 @@ public sealed class TypeChecker
IdentifierSyntax expression => CheckIdentifier(expression), IdentifierSyntax expression => CheckIdentifier(expression),
LiteralSyntax expression => CheckLiteral(expression, expectedType), LiteralSyntax expression => CheckLiteral(expression, expectedType),
StructFieldAccessSyntax expression => CheckStructFieldAccess(expression), StructFieldAccessSyntax expression => CheckStructFieldAccess(expression),
StructInitializerSyntax expression => CheckStructInitializer(expression), StructInitializerSyntax expression => CheckStructInitializer(expression, expectedType),
UnaryExpressionSyntax expression => CheckUnaryExpression(expression), UnaryExpressionSyntax expression => CheckUnaryExpression(expression),
_ => throw new ArgumentOutOfRangeException(nameof(node)) _ => 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()); 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 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()); 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()); throw new TypeCheckerException(Diagnostic.Error($"Cannot initialize non-struct type {expression.StructType}").Build());
} }
@@ -598,7 +607,7 @@ public sealed class TypeChecker
if (type == null) 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); return new UnaryExpressionNode(type, CheckUnaryOperator(expression.OperatorSyntax), boundOperand);