...
This commit is contained in:
@@ -86,7 +86,6 @@ public sealed class Parser
|
|||||||
{
|
{
|
||||||
Symbol.Func => ParseFunc(startIndex, exported, null),
|
Symbol.Func => ParseFunc(startIndex, exported, null),
|
||||||
Symbol.Struct => ParseStruct(startIndex, exported),
|
Symbol.Struct => ParseStruct(startIndex, exported),
|
||||||
Symbol.Enum => ParseEnum(startIndex, exported),
|
|
||||||
_ => throw new ParseException(Diagnostic
|
_ => throw new ParseException(Diagnostic
|
||||||
.Error($"Expected 'func' or 'struct' but found '{keyword.Symbol}'")
|
.Error($"Expected 'func' or 'struct' but found '{keyword.Symbol}'")
|
||||||
.WithHelp("Valid definition keywords are 'func' and 'struct'")
|
.WithHelp("Valid definition keywords are 'func' and 'struct'")
|
||||||
@@ -225,36 +224,6 @@ public sealed class Parser
|
|||||||
return new StructSyntax(GetTokens(startIndex), name.Value, exported, fields, funcs);
|
return new StructSyntax(GetTokens(startIndex), name.Value, exported, fields, funcs);
|
||||||
}
|
}
|
||||||
|
|
||||||
private EnumSyntax ParseEnum(int startIndex, bool exported)
|
|
||||||
{
|
|
||||||
var name = ExpectIdentifier();
|
|
||||||
|
|
||||||
TypeSyntax? type = null;
|
|
||||||
if (TryExpectSymbol(Symbol.Colon))
|
|
||||||
{
|
|
||||||
type = ParseType();
|
|
||||||
}
|
|
||||||
|
|
||||||
var values = new List<EnumValueSyntax>();
|
|
||||||
|
|
||||||
ExpectSymbol(Symbol.OpenBrace);
|
|
||||||
while (!TryExpectSymbol(Symbol.CloseBrace))
|
|
||||||
{
|
|
||||||
var valueStartIndex = _tokenIndex;
|
|
||||||
var valueName = ExpectIdentifier().Value;
|
|
||||||
|
|
||||||
ExpressionSyntax? valueValue = null;
|
|
||||||
if (TryExpectSymbol(Symbol.Assign))
|
|
||||||
{
|
|
||||||
valueValue = ParseExpression();
|
|
||||||
}
|
|
||||||
|
|
||||||
values.Add(new EnumValueSyntax(GetTokens(valueStartIndex), valueName, valueValue));
|
|
||||||
}
|
|
||||||
|
|
||||||
return new EnumSyntax(GetTokens(startIndex), name.Value, exported, type, values);
|
|
||||||
}
|
|
||||||
|
|
||||||
private StatementSyntax ParseStatement()
|
private StatementSyntax ParseStatement()
|
||||||
{
|
{
|
||||||
var startIndex = _tokenIndex;
|
var startIndex = _tokenIndex;
|
||||||
@@ -593,11 +562,11 @@ public sealed class Parser
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
expr = new DotFuncCallSyntax(GetTokens(startIndex), member, expr, parameters);
|
expr = new MemberFuncCallSyntax(GetTokens(startIndex), member, expr, parameters);
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
expr = new StructFieldAccessSyntax(GetTokens(startIndex), expr, member);
|
expr = new MemberAccessSyntax(GetTokens(startIndex), expr, member);
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -17,7 +17,3 @@ public record StructFuncSyntax(List<Token> Tokens, string Name, string? Hook, Fu
|
|||||||
public record StructSyntax(List<Token> Tokens, string Name, bool Exported, List<StructFieldSyntax> Fields, List<StructFuncSyntax> Functions) : DefinitionSyntax(Tokens, Name, Exported);
|
public record StructSyntax(List<Token> Tokens, string Name, bool Exported, List<StructFieldSyntax> Fields, List<StructFuncSyntax> Functions) : DefinitionSyntax(Tokens, Name, Exported);
|
||||||
|
|
||||||
public record StructTemplateSyntax(List<Token> Tokens, List<string> TemplateArguments, string Name, bool Exported, List<StructFieldSyntax> Fields, List<StructFuncSyntax> Functions) : DefinitionSyntax(Tokens, Name, Exported);
|
public record StructTemplateSyntax(List<Token> Tokens, List<string> TemplateArguments, string Name, bool Exported, List<StructFieldSyntax> Fields, List<StructFuncSyntax> Functions) : DefinitionSyntax(Tokens, Name, Exported);
|
||||||
|
|
||||||
public record EnumValueSyntax(List<Token> Tokens, string Name, ExpressionSyntax? Value) : SyntaxNode(Tokens);
|
|
||||||
|
|
||||||
public record EnumSyntax(List<Token> Tokens, string Name, bool Exported, TypeSyntax? Type, List<EnumValueSyntax> Values) : DefinitionSyntax(Tokens, Name, Exported);
|
|
||||||
@@ -38,7 +38,7 @@ public record UnaryExpressionSyntax(List<Token> Tokens, UnaryOperatorSyntax Oper
|
|||||||
|
|
||||||
public record FuncCallSyntax(List<Token> Tokens, ExpressionSyntax Expression, List<ExpressionSyntax> Parameters) : ExpressionSyntax(Tokens);
|
public record FuncCallSyntax(List<Token> Tokens, ExpressionSyntax Expression, List<ExpressionSyntax> Parameters) : ExpressionSyntax(Tokens);
|
||||||
|
|
||||||
public record DotFuncCallSyntax(List<Token> Tokens, string Name, ExpressionSyntax Target, List<ExpressionSyntax> Parameters) : ExpressionSyntax(Tokens);
|
public record MemberFuncCallSyntax(List<Token> Tokens, string Name, ExpressionSyntax Target, List<ExpressionSyntax> Parameters) : ExpressionSyntax(Tokens);
|
||||||
|
|
||||||
public record LocalIdentifierSyntax(List<Token> Tokens, string Name) : ExpressionSyntax(Tokens);
|
public record LocalIdentifierSyntax(List<Token> Tokens, string Name) : ExpressionSyntax(Tokens);
|
||||||
|
|
||||||
@@ -52,7 +52,7 @@ public record AddressOfSyntax(List<Token> Tokens, ExpressionSyntax Target) : Exp
|
|||||||
|
|
||||||
public record LiteralSyntax(List<Token> Tokens, string Value, LiteralKind Kind) : ExpressionSyntax(Tokens);
|
public record LiteralSyntax(List<Token> Tokens, string Value, LiteralKind Kind) : ExpressionSyntax(Tokens);
|
||||||
|
|
||||||
public record StructFieldAccessSyntax(List<Token> Tokens, ExpressionSyntax Target, string Member) : ExpressionSyntax(Tokens);
|
public record MemberAccessSyntax(List<Token> Tokens, ExpressionSyntax Target, string Member) : ExpressionSyntax(Tokens);
|
||||||
|
|
||||||
public record StructInitializerSyntax(List<Token> Tokens, TypeSyntax? StructType, Dictionary<string, ExpressionSyntax> Initializers) : ExpressionSyntax(Tokens);
|
public record StructInitializerSyntax(List<Token> Tokens, TypeSyntax? StructType, Dictionary<string, ExpressionSyntax> Initializers) : ExpressionSyntax(Tokens);
|
||||||
|
|
||||||
|
|||||||
@@ -61,7 +61,6 @@ public enum Symbol
|
|||||||
Export,
|
Export,
|
||||||
Defer,
|
Defer,
|
||||||
At,
|
At,
|
||||||
Enum,
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public abstract record Token(string FileName, SourceSpan Span);
|
public abstract record Token(string FileName, SourceSpan Span);
|
||||||
|
|||||||
@@ -24,7 +24,6 @@ public sealed class Tokenizer
|
|||||||
["export"] = Symbol.Export,
|
["export"] = Symbol.Export,
|
||||||
["import"] = Symbol.Import,
|
["import"] = Symbol.Import,
|
||||||
["defer"] = Symbol.Defer,
|
["defer"] = Symbol.Defer,
|
||||||
["enum"] = Symbol.Enum,
|
|
||||||
};
|
};
|
||||||
|
|
||||||
private static readonly Dictionary<char[], Symbol> Symbols = new()
|
private static readonly Dictionary<char[], Symbol> Symbols = new()
|
||||||
|
|||||||
@@ -112,7 +112,7 @@ public class NubStructType(string module, string name, List<NubStructFieldType>
|
|||||||
|
|
||||||
public override string ToString() => $"{Module}.{Name}";
|
public override string ToString() => $"{Module}.{Name}";
|
||||||
public override bool Equals(NubType? other) => other is NubStructType structType && Name == structType.Name && Module == structType.Module;
|
public override bool Equals(NubType? other) => other is NubStructType structType && Name == structType.Name && Module == structType.Module;
|
||||||
public override int GetHashCode() => HashCode.Combine(typeof(NubStructType), Name);
|
public override int GetHashCode() => HashCode.Combine(typeof(NubStructType), Module, Name);
|
||||||
}
|
}
|
||||||
|
|
||||||
public class NubStructFieldType(string name, NubType type, bool hasDefaultValue)
|
public class NubStructFieldType(string name, NubType type, bool hasDefaultValue)
|
||||||
|
|||||||
@@ -298,12 +298,12 @@ public sealed class TypeChecker
|
|||||||
BinaryExpressionSyntax expression => CheckBinaryExpression(expression),
|
BinaryExpressionSyntax expression => CheckBinaryExpression(expression),
|
||||||
UnaryExpressionSyntax expression => CheckUnaryExpression(expression, expectedType),
|
UnaryExpressionSyntax expression => CheckUnaryExpression(expression, expectedType),
|
||||||
DereferenceSyntax expression => CheckDereference(expression),
|
DereferenceSyntax expression => CheckDereference(expression),
|
||||||
DotFuncCallSyntax expression => CheckDotFuncCall(expression),
|
MemberFuncCallSyntax expression => CheckMemberFuncCall(expression),
|
||||||
FuncCallSyntax expression => CheckFuncCall(expression),
|
FuncCallSyntax expression => CheckFuncCall(expression),
|
||||||
LocalIdentifierSyntax expression => CheckLocalIdentifier(expression),
|
LocalIdentifierSyntax expression => CheckLocalIdentifier(expression),
|
||||||
ModuleIdentifierSyntax expression => CheckModuleIdentifier(expression),
|
ModuleIdentifierSyntax expression => CheckModuleIdentifier(expression),
|
||||||
LiteralSyntax expression => CheckLiteral(expression, expectedType),
|
LiteralSyntax expression => CheckLiteral(expression, expectedType),
|
||||||
StructFieldAccessSyntax expression => CheckStructFieldAccess(expression),
|
MemberAccessSyntax expression => CheckMemberAccess(expression),
|
||||||
StructInitializerSyntax expression => CheckStructInitializer(expression, expectedType),
|
StructInitializerSyntax expression => CheckStructInitializer(expression, expectedType),
|
||||||
InterpretBuiltinSyntax expression => CheckExpression(expression.Target) with { Type = ResolveType(expression.Type) },
|
InterpretBuiltinSyntax expression => CheckExpression(expression.Target) with { Type = ResolveType(expression.Type) },
|
||||||
SizeBuiltinSyntax expression => new SizeBuiltinNode(new NubIntType(false, 64), ResolveType(expression.Type)),
|
SizeBuiltinSyntax expression => new SizeBuiltinNode(new NubIntType(false, 64), ResolveType(expression.Type)),
|
||||||
@@ -610,7 +610,7 @@ public sealed class TypeChecker
|
|||||||
return new FuncCallNode(funcType.ReturnType, accessor, parameters);
|
return new FuncCallNode(funcType.ReturnType, accessor, parameters);
|
||||||
}
|
}
|
||||||
|
|
||||||
private StructFuncCallNode CheckDotFuncCall(DotFuncCallSyntax expression)
|
private StructFuncCallNode CheckMemberFuncCall(MemberFuncCallSyntax expression)
|
||||||
{
|
{
|
||||||
// todo(nub31): When adding interfaces, also support other types than structs
|
// todo(nub31): When adding interfaces, also support other types than structs
|
||||||
var target = CheckExpression(expression.Target);
|
var target = CheckExpression(expression.Target);
|
||||||
@@ -769,18 +769,13 @@ public sealed class TypeChecker
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private StructFieldAccessNode CheckStructFieldAccess(StructFieldAccessSyntax expression)
|
private ExpressionNode CheckMemberAccess(MemberAccessSyntax expression)
|
||||||
{
|
{
|
||||||
var target = CheckExpression(expression.Target);
|
var target = CheckExpression(expression.Target);
|
||||||
|
switch (target.Type)
|
||||||
if (target.Type is not NubStructType structType)
|
{
|
||||||
|
case NubStructType structType:
|
||||||
{
|
{
|
||||||
throw new TypeCheckerException(Diagnostic
|
|
||||||
.Error($"Cannot access struct member on non-struct type {target.Type}")
|
|
||||||
.At(expression)
|
|
||||||
.Build());
|
|
||||||
}
|
|
||||||
|
|
||||||
var field = structType.Fields.FirstOrDefault(x => x.Name == expression.Member);
|
var field = structType.Fields.FirstOrDefault(x => x.Name == expression.Member);
|
||||||
if (field == null)
|
if (field == null)
|
||||||
{
|
{
|
||||||
@@ -792,6 +787,15 @@ public sealed class TypeChecker
|
|||||||
|
|
||||||
return new StructFieldAccessNode(field.Type, target, expression.Member);
|
return new StructFieldAccessNode(field.Type, target, expression.Member);
|
||||||
}
|
}
|
||||||
|
default:
|
||||||
|
{
|
||||||
|
throw new TypeCheckerException(Diagnostic
|
||||||
|
.Error($"Cannot access struct member on non-struct type {target.Type}")
|
||||||
|
.At(expression)
|
||||||
|
.Build());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
private StructInitializerNode CheckStructInitializer(StructInitializerSyntax expression, NubType? expectedType)
|
private StructInitializerNode CheckStructInitializer(StructInitializerSyntax expression, NubType? expectedType)
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -3,15 +3,9 @@ import "raylib"
|
|||||||
|
|
||||||
module "main"
|
module "main"
|
||||||
|
|
||||||
enum State
|
|
||||||
{
|
|
||||||
NONE
|
|
||||||
TEST
|
|
||||||
SOMEOTHERVALUE
|
|
||||||
}
|
|
||||||
|
|
||||||
extern "main" func main(args: []cstring): i64
|
extern "main" func main(args: []cstring): i64
|
||||||
{
|
{
|
||||||
|
raylib::SetConfigFlags(raylib::ConfigFlags::VSYNC_HINT)
|
||||||
raylib::InitWindow(1600, 900, "Hi from nub-lang")
|
raylib::InitWindow(1600, 900, "Hi from nub-lang")
|
||||||
raylib::SetTargetFPS(240)
|
raylib::SetTargetFPS(240)
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user