remove generics for now
This commit is contained in:
@@ -1,27 +1,6 @@
|
||||
struct Box<T>
|
||||
{
|
||||
value: T
|
||||
}
|
||||
|
||||
func main(args: []cstring): i64
|
||||
{
|
||||
let box = alloc Box<cstring>
|
||||
{
|
||||
value = "bob"
|
||||
}
|
||||
|
||||
let box2 = alloc Box<cstring>
|
||||
{
|
||||
value = "bob"
|
||||
}
|
||||
|
||||
let box3 = alloc Box<u8>
|
||||
{
|
||||
value = 23
|
||||
}
|
||||
|
||||
|
||||
c::puts(box.value)
|
||||
c::puts("test")
|
||||
|
||||
return 0
|
||||
}
|
||||
|
||||
@@ -7,7 +7,6 @@ using NubLang.Generation;
|
||||
using NubLang.Generation.QBE;
|
||||
using NubLang.Syntax.Binding;
|
||||
using NubLang.Syntax.Binding.Node;
|
||||
using NubLang.Syntax.Generics;
|
||||
using NubLang.Syntax.Parsing;
|
||||
using NubLang.Syntax.Parsing.Node;
|
||||
using NubLang.Syntax.Tokenization;
|
||||
@@ -95,9 +94,6 @@ foreach (var file in options.Files)
|
||||
syntaxTrees[file] = syntaxTree;
|
||||
}
|
||||
|
||||
var genericInstantiator = new GenericInstantiator(syntaxTrees.Values);
|
||||
genericInstantiator.Visit();
|
||||
|
||||
var definitionTable = new DefinitionTable(syntaxTrees.Values);
|
||||
|
||||
var boundSyntaxTrees = new Dictionary<string, BoundSyntaxTree>();
|
||||
|
||||
@@ -3,12 +3,7 @@ using NubLang.Syntax.Tokenization;
|
||||
|
||||
namespace NubLang.Syntax.Parsing.Node;
|
||||
|
||||
public record GenericParameter(IReadOnlyList<Token> Tokens, string Name) : SyntaxNode(Tokens)
|
||||
{
|
||||
public override IEnumerable<SyntaxNode> GetChildren() => [];
|
||||
}
|
||||
|
||||
public abstract record DefinitionSyntax(IReadOnlyList<Token> Tokens, string Namespace, IReadOnlyList<GenericParameter> Parameters) : SyntaxNode(Tokens);
|
||||
public abstract record DefinitionSyntax(IReadOnlyList<Token> Tokens, string Namespace) : SyntaxNode(Tokens);
|
||||
|
||||
public record FuncParameterSyntax(IReadOnlyList<Token> Tokens, string Name, TypeSyntax Type) : SyntaxNode(Tokens)
|
||||
{
|
||||
@@ -31,29 +26,19 @@ public record FuncSignatureSyntax(IReadOnlyList<Token> Tokens, IReadOnlyList<Fun
|
||||
}
|
||||
}
|
||||
|
||||
public record LocalFuncSyntax(IReadOnlyList<Token> Tokens, string Namespace, IReadOnlyList<GenericParameter> Parameters, string Name, FuncSignatureSyntax Signature, BlockSyntax Body) : DefinitionSyntax(Tokens, Namespace, Parameters)
|
||||
public record LocalFuncSyntax(IReadOnlyList<Token> Tokens, string Namespace, string Name, FuncSignatureSyntax Signature, BlockSyntax Body) : DefinitionSyntax(Tokens, Namespace)
|
||||
{
|
||||
public override IEnumerable<SyntaxNode> GetChildren()
|
||||
{
|
||||
foreach (var parameter in Parameters)
|
||||
{
|
||||
yield return parameter;
|
||||
}
|
||||
|
||||
yield return Signature;
|
||||
yield return Body;
|
||||
}
|
||||
}
|
||||
|
||||
public record ExternFuncSyntax(IReadOnlyList<Token> Tokens, string Namespace, IReadOnlyList<GenericParameter> Parameters, string Name, string CallName, FuncSignatureSyntax Signature) : DefinitionSyntax(Tokens, Namespace, Parameters)
|
||||
public record ExternFuncSyntax(IReadOnlyList<Token> Tokens, string Namespace, string Name, string CallName, FuncSignatureSyntax Signature) : DefinitionSyntax(Tokens, Namespace)
|
||||
{
|
||||
public override IEnumerable<SyntaxNode> GetChildren()
|
||||
{
|
||||
foreach (var parameter in Parameters)
|
||||
{
|
||||
yield return parameter;
|
||||
}
|
||||
|
||||
yield return Signature;
|
||||
}
|
||||
}
|
||||
@@ -79,15 +64,10 @@ public record StructFuncSyntax(IReadOnlyList<Token> Tokens, string Name, FuncSig
|
||||
}
|
||||
}
|
||||
|
||||
public record StructSyntax(IReadOnlyList<Token> Tokens, string Namespace, IReadOnlyList<GenericParameter> Parameters, string Name, IReadOnlyList<StructFieldSyntax> Fields, IReadOnlyList<StructFuncSyntax> Functions) : DefinitionSyntax(Tokens, Namespace, Parameters)
|
||||
public record StructSyntax(IReadOnlyList<Token> Tokens, string Namespace, string Name, IReadOnlyList<StructFieldSyntax> Fields, IReadOnlyList<StructFuncSyntax> Functions) : DefinitionSyntax(Tokens, Namespace)
|
||||
{
|
||||
public override IEnumerable<SyntaxNode> GetChildren()
|
||||
{
|
||||
foreach (var parameter in Parameters)
|
||||
{
|
||||
yield return parameter;
|
||||
}
|
||||
|
||||
foreach (var field in Fields)
|
||||
{
|
||||
yield return field;
|
||||
@@ -108,15 +88,10 @@ public record InterfaceFuncSyntax(IReadOnlyList<Token> Tokens, string Name, Func
|
||||
}
|
||||
}
|
||||
|
||||
public record InterfaceSyntax(IReadOnlyList<Token> Tokens, string Namespace, IReadOnlyList<GenericParameter> Parameters, string Name, IReadOnlyList<InterfaceFuncSyntax> Functions) : DefinitionSyntax(Tokens, Namespace, Parameters)
|
||||
public record InterfaceSyntax(IReadOnlyList<Token> Tokens, string Namespace, string Name, IReadOnlyList<InterfaceFuncSyntax> Functions) : DefinitionSyntax(Tokens, Namespace)
|
||||
{
|
||||
public override IEnumerable<SyntaxNode> GetChildren()
|
||||
{
|
||||
foreach (var parameter in Parameters)
|
||||
{
|
||||
yield return parameter;
|
||||
}
|
||||
|
||||
foreach (var func in Functions)
|
||||
{
|
||||
yield return func;
|
||||
|
||||
@@ -29,7 +29,7 @@ public abstract record TypeSyntax(IReadOnlyList<Token> Tokens) : SyntaxNode(Toke
|
||||
StringTypeSyntax => "string",
|
||||
PointerTypeSyntax ptr => $"ptr_{ptr.BaseType.MangledName()}",
|
||||
ArrayTypeSyntax arr => $"arr_{arr.BaseType.MangledName()}",
|
||||
CustomTypeSyntax custom => $"{custom.Namespace}_{custom.Name}_{string.Join("_", custom.Arguments.Select(x => x.MangledName()))}",
|
||||
CustomTypeSyntax custom => $"{custom.Namespace}_{custom.Name}",
|
||||
FuncTypeSyntax func => $"func_{string.Join("_", func.Parameters.Select(x => x.MangledName()))}_to_{func.ReturnType.MangledName()}",
|
||||
_ => throw new NotSupportedException($"Unknown type syntax: {GetType().Name}")
|
||||
};
|
||||
@@ -77,18 +77,7 @@ public record StringTypeSyntax(IReadOnlyList<Token> Tokens) : TypeSyntax(Tokens)
|
||||
public override IEnumerable<SyntaxNode> GetChildren() => [];
|
||||
}
|
||||
|
||||
public record CustomTypeSyntax(IReadOnlyList<Token> Tokens, string Namespace, string Name, IReadOnlyList<TypeSyntax> Arguments) : TypeSyntax(Tokens)
|
||||
{
|
||||
public override IEnumerable<SyntaxNode> GetChildren()
|
||||
{
|
||||
foreach (var argument in Arguments)
|
||||
{
|
||||
yield return argument;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public record GenericTypeSyntax(IReadOnlyList<Token> Tokens, string Name) : TypeSyntax(Tokens)
|
||||
public record CustomTypeSyntax(IReadOnlyList<Token> Tokens, string Namespace, string Name) : TypeSyntax(Tokens)
|
||||
{
|
||||
public override IEnumerable<SyntaxNode> GetChildren() => [];
|
||||
}
|
||||
|
||||
@@ -12,7 +12,6 @@ public sealed class Parser
|
||||
private readonly IReadOnlyList<Token> _tokens;
|
||||
|
||||
private readonly List<Diagnostic> _diagnostics = [];
|
||||
private List<GenericParameter> _genericParameters = [];
|
||||
private int _tokenIndex;
|
||||
|
||||
public Parser(IReadOnlyList<Token> tokens)
|
||||
@@ -24,7 +23,6 @@ public sealed class Parser
|
||||
public SyntaxTree Parse()
|
||||
{
|
||||
_diagnostics.Clear();
|
||||
_genericParameters = [];
|
||||
_tokenIndex = 0;
|
||||
|
||||
if (TryExpectSymbol(Symbol.Namespace))
|
||||
@@ -36,40 +34,18 @@ public sealed class Parser
|
||||
|
||||
while (Peek().HasValue)
|
||||
{
|
||||
_genericParameters = [];
|
||||
var startIndex = _tokenIndex;
|
||||
|
||||
try
|
||||
{
|
||||
var keyword = ExpectSymbol();
|
||||
var name = ExpectIdentifier();
|
||||
|
||||
if (TryExpectSymbol(Symbol.LessThan))
|
||||
{
|
||||
while (!TryExpectSymbol(Symbol.GreaterThan))
|
||||
{
|
||||
var argumentStartIndex = _tokenIndex;
|
||||
var identifier = ExpectIdentifier();
|
||||
if (!TryExpectSymbol(Symbol.Comma) && Peek().TryGetValue(out var token) && token is not SymbolToken { Symbol: Symbol.GreaterThan })
|
||||
{
|
||||
_diagnostics.Add(Diagnostic
|
||||
.Warning("Missing comma between template parameters")
|
||||
.WithHelp("Add a ',' to separate parameters")
|
||||
.At(token)
|
||||
.Build());
|
||||
}
|
||||
|
||||
var parameter = new GenericParameter(GetTokens(argumentStartIndex), identifier.Value);
|
||||
_genericParameters.Add(parameter);
|
||||
}
|
||||
}
|
||||
|
||||
var definition = keyword.Symbol switch
|
||||
{
|
||||
Symbol.Extern => ParseExtern(startIndex, name, _genericParameters),
|
||||
Symbol.Func => ParseFunc(startIndex, name, _genericParameters),
|
||||
Symbol.Struct => ParseStruct(startIndex, name, _genericParameters),
|
||||
Symbol.Interface => ParseInterface(startIndex, name, _genericParameters),
|
||||
Symbol.Extern => ParseExtern(startIndex),
|
||||
Symbol.Func => ParseFunc(startIndex),
|
||||
Symbol.Struct => ParseStruct(startIndex),
|
||||
Symbol.Interface => ParseInterface(startIndex),
|
||||
_ => throw new ParseException(Diagnostic
|
||||
.Error($"Expected 'extern', 'func', 'struct', 'trait' or 'impl' but found '{keyword.Symbol}'")
|
||||
.WithHelp("Valid definition keywords are 'extern', 'func', 'struct', 'trait' and 'impl'")
|
||||
@@ -140,18 +116,21 @@ public sealed class Parser
|
||||
return new FuncParameterSyntax(GetTokens(startIndex), name.Value, type);
|
||||
}
|
||||
|
||||
private DefinitionSyntax ParseExtern(int startIndex, IdentifierToken name, IReadOnlyList<GenericParameter> genericParameters)
|
||||
private DefinitionSyntax ParseExtern(int startIndex)
|
||||
{
|
||||
var keyword = ExpectSymbol();
|
||||
|
||||
return keyword.Symbol switch
|
||||
{
|
||||
Symbol.Func => ParseExternFunc(startIndex, name, genericParameters),
|
||||
Symbol.Func => ParseExternFunc(startIndex),
|
||||
_ => throw new ParseException(Diagnostic.Error($"Unexpected symbol {keyword.Symbol} after extern declaration").At(keyword).Build())
|
||||
};
|
||||
}
|
||||
|
||||
private ExternFuncSyntax ParseExternFunc(int startIndex, IdentifierToken name, IReadOnlyList<GenericParameter> genericParameters)
|
||||
private ExternFuncSyntax ParseExternFunc(int startIndex)
|
||||
{
|
||||
var name = ExpectIdentifier();
|
||||
|
||||
var callName = name.Value;
|
||||
|
||||
if (TryExpectSymbol(Symbol.Calls))
|
||||
@@ -161,19 +140,22 @@ public sealed class Parser
|
||||
|
||||
var signature = ParseFuncSignature();
|
||||
|
||||
return new ExternFuncSyntax(GetTokens(startIndex), _namespace, genericParameters, name.Value, callName, signature);
|
||||
return new ExternFuncSyntax(GetTokens(startIndex), _namespace, name.Value, callName, signature);
|
||||
}
|
||||
|
||||
private LocalFuncSyntax ParseFunc(int startIndex, IdentifierToken name, IReadOnlyList<GenericParameter> genericParameters)
|
||||
private LocalFuncSyntax ParseFunc(int startIndex)
|
||||
{
|
||||
var name = ExpectIdentifier();
|
||||
var signature = ParseFuncSignature();
|
||||
var body = ParseBlock();
|
||||
|
||||
return new LocalFuncSyntax(GetTokens(startIndex), _namespace, genericParameters, name.Value, signature, body);
|
||||
return new LocalFuncSyntax(GetTokens(startIndex), _namespace, name.Value, signature, body);
|
||||
}
|
||||
|
||||
private DefinitionSyntax ParseStruct(int startIndex, IdentifierToken name, IReadOnlyList<GenericParameter> genericParameters)
|
||||
private DefinitionSyntax ParseStruct(int startIndex)
|
||||
{
|
||||
var name = ExpectIdentifier();
|
||||
|
||||
ExpectSymbol(Symbol.OpenBrace);
|
||||
|
||||
List<StructFieldSyntax> fields = [];
|
||||
@@ -188,7 +170,7 @@ public sealed class Parser
|
||||
if (TryExpectSymbol(Symbol.Func))
|
||||
{
|
||||
var funcName = ExpectIdentifier().Value;
|
||||
var thisArg = new FuncParameterSyntax([], "this", new CustomTypeSyntax([], _namespace, name.Value, genericParameters.Select(x => new GenericTypeSyntax(x.Tokens, x.Name)).ToList()));
|
||||
var thisArg = new FuncParameterSyntax([], "this", new CustomTypeSyntax([], _namespace, name.Value));
|
||||
var funcSignature = ParseFuncSignature(thisArg);
|
||||
var funcBody = ParseBlock();
|
||||
|
||||
@@ -211,11 +193,13 @@ public sealed class Parser
|
||||
}
|
||||
}
|
||||
|
||||
return new StructSyntax(GetTokens(startIndex), _namespace, genericParameters, name.Value, fields, funcs);
|
||||
return new StructSyntax(GetTokens(startIndex), _namespace, name.Value, fields, funcs);
|
||||
}
|
||||
|
||||
private InterfaceSyntax ParseInterface(int startIndex, IdentifierToken name, IReadOnlyList<GenericParameter> genericParameters)
|
||||
private InterfaceSyntax ParseInterface(int startIndex)
|
||||
{
|
||||
var name = ExpectIdentifier();
|
||||
|
||||
ExpectSymbol(Symbol.OpenBrace);
|
||||
|
||||
List<InterfaceFuncSyntax> functions = [];
|
||||
@@ -232,7 +216,7 @@ public sealed class Parser
|
||||
functions.Add(new InterfaceFuncSyntax(GetTokens(funcStartIndex), funcName, signature));
|
||||
}
|
||||
|
||||
return new InterfaceSyntax(GetTokens(startIndex), _namespace, genericParameters, name.Value, functions);
|
||||
return new InterfaceSyntax(GetTokens(startIndex), _namespace, name.Value, functions);
|
||||
}
|
||||
|
||||
private StatementSyntax ParseStatement()
|
||||
@@ -646,12 +630,6 @@ public sealed class Parser
|
||||
|
||||
if (TryExpectIdentifier(out var name))
|
||||
{
|
||||
var genericParameter = _genericParameters.FirstOrDefault(x => x.Name == name.Value);
|
||||
if (genericParameter != null)
|
||||
{
|
||||
return new GenericTypeSyntax(GetTokens(startIndex), genericParameter.Name);
|
||||
}
|
||||
|
||||
return name.Value switch
|
||||
{
|
||||
"void" => new VoidTypeSyntax(GetTokens(startIndex)),
|
||||
@@ -679,17 +657,7 @@ public sealed class Parser
|
||||
@namespace = ExpectIdentifier().Value;
|
||||
}
|
||||
|
||||
var parameters = new List<TypeSyntax>();
|
||||
|
||||
if (TryExpectSymbol(Symbol.LessThan))
|
||||
{
|
||||
while (!TryExpectSymbol(Symbol.GreaterThan))
|
||||
{
|
||||
parameters.Add(ParseType());
|
||||
}
|
||||
}
|
||||
|
||||
return new CustomTypeSyntax(GetTokens(startIndex), @namespace, name.Value, parameters);
|
||||
return new CustomTypeSyntax(GetTokens(startIndex), @namespace, name.Value);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user