Ditch the IReadonlyCollection bullshit
This commit is contained in:
@@ -12,7 +12,7 @@ public class Generator
|
|||||||
private readonly StringBuilder _builder;
|
private readonly StringBuilder _builder;
|
||||||
private readonly LabelFactory _labelFactory;
|
private readonly LabelFactory _labelFactory;
|
||||||
|
|
||||||
public Generator(IReadOnlyCollection<DefinitionNode> definitions)
|
public Generator(List<DefinitionNode> definitions)
|
||||||
{
|
{
|
||||||
_definitions = [];
|
_definitions = [];
|
||||||
_builder = new StringBuilder();
|
_builder = new StringBuilder();
|
||||||
|
|||||||
@@ -90,7 +90,7 @@ public class SymbolTable
|
|||||||
return variables;
|
return variables;
|
||||||
}
|
}
|
||||||
|
|
||||||
public Func ResolveFunc(string name, IReadOnlyCollection<Type> parameterTypes)
|
public Func ResolveFunc(string name, List<Type> parameterTypes)
|
||||||
{
|
{
|
||||||
var func = _funcDefinitions.FirstOrDefault(f => f.SignatureMatches(name, parameterTypes));
|
var func = _funcDefinitions.FirstOrDefault(f => f.SignatureMatches(name, parameterTypes));
|
||||||
if (func == null)
|
if (func == null)
|
||||||
@@ -101,7 +101,7 @@ public class SymbolTable
|
|||||||
return func;
|
return func;
|
||||||
}
|
}
|
||||||
|
|
||||||
public LocalFunc ResolveLocalFunc(string name, IReadOnlyCollection<Type> parameterTypes)
|
public LocalFunc ResolveLocalFunc(string name, List<Type> parameterTypes)
|
||||||
{
|
{
|
||||||
var func = ResolveFunc(name, parameterTypes);
|
var func = ResolveFunc(name, parameterTypes);
|
||||||
if (func is not LocalFunc localFunc)
|
if (func is not LocalFunc localFunc)
|
||||||
@@ -111,7 +111,7 @@ public class SymbolTable
|
|||||||
return localFunc;
|
return localFunc;
|
||||||
}
|
}
|
||||||
|
|
||||||
public ExternFunc ResolveExternFunc(string name, IReadOnlyCollection<Type> parameterTypes)
|
public ExternFunc ResolveExternFunc(string name, List<Type> parameterTypes)
|
||||||
{
|
{
|
||||||
var func = ResolveFunc(name, parameterTypes);
|
var func = ResolveFunc(name, parameterTypes);
|
||||||
if (func is not ExternFunc externFunc)
|
if (func is not ExternFunc externFunc)
|
||||||
@@ -151,7 +151,7 @@ public class GlobalVariable(string name, Type type, string identifier) : Variabl
|
|||||||
|
|
||||||
public abstract class Func
|
public abstract class Func
|
||||||
{
|
{
|
||||||
protected Func(string name, string startLabel, IReadOnlyCollection<FuncParameter> parameters, Optional<Type> returnType)
|
protected Func(string name, string startLabel, List<FuncParameter> parameters, Optional<Type> returnType)
|
||||||
{
|
{
|
||||||
Name = name;
|
Name = name;
|
||||||
Parameters = parameters;
|
Parameters = parameters;
|
||||||
@@ -161,10 +161,10 @@ public abstract class Func
|
|||||||
|
|
||||||
public string Name { get; }
|
public string Name { get; }
|
||||||
public string StartLabel { get; }
|
public string StartLabel { get; }
|
||||||
public IReadOnlyCollection<FuncParameter> Parameters { get; }
|
public List<FuncParameter> Parameters { get; }
|
||||||
public Optional<Type> ReturnType { get; }
|
public Optional<Type> ReturnType { get; }
|
||||||
|
|
||||||
public bool SignatureMatches(string name, IReadOnlyCollection<Type> parameterTypes)
|
public bool SignatureMatches(string name, List<Type> parameterTypes)
|
||||||
{
|
{
|
||||||
return Name == name
|
return Name == name
|
||||||
&& Parameters.Count == parameterTypes.Count
|
&& Parameters.Count == parameterTypes.Count
|
||||||
@@ -174,21 +174,21 @@ public abstract class Func
|
|||||||
|
|
||||||
public class ExternFunc : Func
|
public class ExternFunc : Func
|
||||||
{
|
{
|
||||||
public ExternFunc(string name, string startLabel, IReadOnlyCollection<FuncParameter> parameters, Optional<Type> returnType) : base(name, startLabel, parameters, returnType)
|
public ExternFunc(string name, string startLabel, List<FuncParameter> parameters, Optional<Type> returnType) : base(name, startLabel, parameters, returnType)
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public class LocalFunc : Func
|
public class LocalFunc : Func
|
||||||
{
|
{
|
||||||
public LocalFunc(string name, string startLabel, string endLabel, IReadOnlyCollection<FuncParameter> parameters, Optional<Type> returnType, IReadOnlyCollection<Variable> variables) : base(name, startLabel, parameters, returnType)
|
public LocalFunc(string name, string startLabel, string endLabel, List<FuncParameter> parameters, Optional<Type> returnType, List<Variable> variables) : base(name, startLabel, parameters, returnType)
|
||||||
{
|
{
|
||||||
EndLabel = endLabel;
|
EndLabel = endLabel;
|
||||||
Variables = variables;
|
Variables = variables;
|
||||||
}
|
}
|
||||||
|
|
||||||
public string EndLabel { get; }
|
public string EndLabel { get; }
|
||||||
public IReadOnlyCollection<Variable> Variables { get; }
|
public List<Variable> Variables { get; }
|
||||||
public int StackAllocation => Variables.OfType<LocalVariable>().Sum(variable => variable.Offset);
|
public int StackAllocation => Variables.OfType<LocalVariable>().Sum(variable => variable.Offset);
|
||||||
|
|
||||||
public Variable ResolveVariable(string name)
|
public Variable ResolveVariable(string name)
|
||||||
|
|||||||
@@ -48,7 +48,7 @@ public class Lexer
|
|||||||
private string _src = string.Empty;
|
private string _src = string.Empty;
|
||||||
private int _index;
|
private int _index;
|
||||||
|
|
||||||
public IReadOnlyCollection<Token> Lex(string src)
|
public List<Token> Lex(string src)
|
||||||
{
|
{
|
||||||
_src = src;
|
_src = src;
|
||||||
_index = 0;
|
_index = 0;
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
namespace Nub.Lang.Frontend.Parsing;
|
namespace Nub.Lang.Frontend.Parsing;
|
||||||
|
|
||||||
public class BlockNode(IReadOnlyCollection<StatementNode> statements) : Node
|
public class BlockNode(List<StatementNode> statements) : Node
|
||||||
{
|
{
|
||||||
public IReadOnlyCollection<StatementNode> Statements { get; } = statements;
|
public List<StatementNode> Statements { get; } = statements;
|
||||||
}
|
}
|
||||||
@@ -2,10 +2,10 @@
|
|||||||
|
|
||||||
namespace Nub.Lang.Frontend.Parsing;
|
namespace Nub.Lang.Frontend.Parsing;
|
||||||
|
|
||||||
public class ExternFuncDefinitionNode(string name, IReadOnlyCollection<FuncParameter> parameters, Optional<Type> returnType) : DefinitionNode
|
public class ExternFuncDefinitionNode(string name, List<FuncParameter> parameters, Optional<Type> returnType) : DefinitionNode
|
||||||
{
|
{
|
||||||
public string Name { get; } = name;
|
public string Name { get; } = name;
|
||||||
public IReadOnlyCollection<FuncParameter> Parameters { get; } = parameters;
|
public List<FuncParameter> Parameters { get; } = parameters;
|
||||||
public Optional<Type> ReturnType { get; } = returnType;
|
public Optional<Type> ReturnType { get; } = returnType;
|
||||||
|
|
||||||
public override string ToString() => $"{Name}({string.Join(", ", Parameters.Select(p => p.ToString()))}){(ReturnType.HasValue ? ": " + ReturnType.Value : "")}";
|
public override string ToString() => $"{Name}({string.Join(", ", Parameters.Select(p => p.ToString()))}){(ReturnType.HasValue ? ": " + ReturnType.Value : "")}";
|
||||||
|
|||||||
@@ -1,9 +1,9 @@
|
|||||||
namespace Nub.Lang.Frontend.Parsing;
|
namespace Nub.Lang.Frontend.Parsing;
|
||||||
|
|
||||||
public class FuncCall(string name, IReadOnlyCollection<ExpressionNode> parameters)
|
public class FuncCall(string name, List<ExpressionNode> parameters)
|
||||||
{
|
{
|
||||||
public string Name { get; } = name;
|
public string Name { get; } = name;
|
||||||
public IReadOnlyCollection<ExpressionNode> Parameters { get; } = parameters;
|
public List<ExpressionNode> Parameters { get; } = parameters;
|
||||||
|
|
||||||
public override string ToString() => $"{Name}()";
|
public override string ToString() => $"{Name}()";
|
||||||
}
|
}
|
||||||
@@ -2,10 +2,10 @@
|
|||||||
|
|
||||||
namespace Nub.Lang.Frontend.Parsing;
|
namespace Nub.Lang.Frontend.Parsing;
|
||||||
|
|
||||||
public class LocalFuncDefinitionNode(string name, IReadOnlyCollection<FuncParameter> parameters, BlockNode body, Optional<Type> returnType) : DefinitionNode
|
public class LocalFuncDefinitionNode(string name, List<FuncParameter> parameters, BlockNode body, Optional<Type> returnType) : DefinitionNode
|
||||||
{
|
{
|
||||||
public string Name { get; } = name;
|
public string Name { get; } = name;
|
||||||
public IReadOnlyCollection<FuncParameter> Parameters { get; } = parameters;
|
public List<FuncParameter> Parameters { get; } = parameters;
|
||||||
public BlockNode Body { get; } = body;
|
public BlockNode Body { get; } = body;
|
||||||
public Optional<Type> ReturnType { get; } = returnType;
|
public Optional<Type> ReturnType { get; } = returnType;
|
||||||
|
|
||||||
|
|||||||
@@ -1,8 +1,8 @@
|
|||||||
namespace Nub.Lang.Frontend.Parsing;
|
namespace Nub.Lang.Frontend.Parsing;
|
||||||
|
|
||||||
public class ModuleNode(string path, IReadOnlyCollection<string> imports, IReadOnlyCollection<DefinitionNode> definitions) : Node
|
public class ModuleNode(string path, List<string> imports, List<DefinitionNode> definitions) : Node
|
||||||
{
|
{
|
||||||
public string Path { get; } = path;
|
public string Path { get; } = path;
|
||||||
public IReadOnlyCollection<string> Imports { get; } = imports;
|
public List<string> Imports { get; } = imports;
|
||||||
public IReadOnlyCollection<DefinitionNode> Definitions { get; } = definitions;
|
public List<DefinitionNode> Definitions { get; } = definitions;
|
||||||
}
|
}
|
||||||
@@ -6,10 +6,10 @@ namespace Nub.Lang.Frontend.Parsing;
|
|||||||
|
|
||||||
public class Parser
|
public class Parser
|
||||||
{
|
{
|
||||||
private IReadOnlyCollection<Token> _tokens = [];
|
private List<Token> _tokens = [];
|
||||||
private int _index;
|
private int _index;
|
||||||
|
|
||||||
public ModuleNode ParseModule(IReadOnlyCollection<Token> tokens, string path)
|
public ModuleNode ParseModule(List<Token> tokens, string path)
|
||||||
{
|
{
|
||||||
_index = 0;
|
_index = 0;
|
||||||
_tokens = tokens;
|
_tokens = tokens;
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
namespace Nub.Lang.Frontend.Parsing;
|
namespace Nub.Lang.Frontend.Parsing;
|
||||||
|
|
||||||
public class Syscall(IReadOnlyCollection<ExpressionNode> parameters)
|
public class Syscall(List<ExpressionNode> parameters)
|
||||||
{
|
{
|
||||||
public IReadOnlyCollection<ExpressionNode> Parameters { get; } = parameters;
|
public List<ExpressionNode> Parameters { get; } = parameters;
|
||||||
}
|
}
|
||||||
@@ -3,10 +3,10 @@ using Nub.Lang.Frontend.Parsing;
|
|||||||
|
|
||||||
namespace Nub.Lang.Frontend.Typing;
|
namespace Nub.Lang.Frontend.Typing;
|
||||||
|
|
||||||
public class Func(string name, IReadOnlyCollection<FuncParameter> parameters, Optional<BlockNode> body, Optional<Type> returnType)
|
public class Func(string name, List<FuncParameter> parameters, Optional<BlockNode> body, Optional<Type> returnType)
|
||||||
{
|
{
|
||||||
public string Name { get; } = name;
|
public string Name { get; } = name;
|
||||||
public IReadOnlyCollection<FuncParameter> Parameters { get; } = parameters;
|
public List<FuncParameter> Parameters { get; } = parameters;
|
||||||
public Optional<BlockNode> Body { get; } = body;
|
public Optional<BlockNode> Body { get; } = body;
|
||||||
public Optional<Type> ReturnType { get; } = returnType;
|
public Optional<Type> ReturnType { get; } = returnType;
|
||||||
}
|
}
|
||||||
@@ -17,7 +17,7 @@ public class ExpressionTyper
|
|||||||
private readonly List<GlobalVariableDefinitionNode> _variableDefinitions;
|
private readonly List<GlobalVariableDefinitionNode> _variableDefinitions;
|
||||||
private readonly Stack<Variable> _variables;
|
private readonly Stack<Variable> _variables;
|
||||||
|
|
||||||
public ExpressionTyper(IReadOnlyCollection<DefinitionNode> definitions)
|
public ExpressionTyper(List<DefinitionNode> definitions)
|
||||||
{
|
{
|
||||||
_variables = new Stack<Variable>();
|
_variables = new Stack<Variable>();
|
||||||
_functions = [];
|
_functions = [];
|
||||||
|
|||||||
@@ -42,7 +42,7 @@ internal static class Program
|
|||||||
}
|
}
|
||||||
|
|
||||||
var modules = RunFrontend(input);
|
var modules = RunFrontend(input);
|
||||||
var definitions = modules.SelectMany(f => f.Definitions).ToArray();
|
var definitions = modules.SelectMany(f => f.Definitions).ToList();
|
||||||
|
|
||||||
var typer = new ExpressionTyper(definitions);
|
var typer = new ExpressionTyper(definitions);
|
||||||
typer.Populate();
|
typer.Populate();
|
||||||
@@ -54,7 +54,7 @@ internal static class Program
|
|||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
private static IEnumerable<ModuleNode> RunFrontend(string path)
|
private static List<ModuleNode> RunFrontend(string path)
|
||||||
{
|
{
|
||||||
List<ModuleNode> modules = [];
|
List<ModuleNode> modules = [];
|
||||||
RunFrontend(path, modules);
|
RunFrontend(path, modules);
|
||||||
|
|||||||
@@ -45,13 +45,13 @@ public record StringType : Type
|
|||||||
|
|
||||||
public record DelegateType : Type
|
public record DelegateType : Type
|
||||||
{
|
{
|
||||||
public DelegateType(IReadOnlyCollection<Type> parameters, Optional<Type> returnType)
|
public DelegateType(List<Type> parameters, Optional<Type> returnType)
|
||||||
{
|
{
|
||||||
Parameters = parameters;
|
Parameters = parameters;
|
||||||
ReturnType = returnType;
|
ReturnType = returnType;
|
||||||
}
|
}
|
||||||
|
|
||||||
public IReadOnlyCollection<Type> Parameters { get; }
|
public List<Type> Parameters { get; }
|
||||||
public Optional<Type> ReturnType { get; }
|
public Optional<Type> ReturnType { get; }
|
||||||
|
|
||||||
public override string ToString() => $"({string.Join(", ", Parameters)}): {(ReturnType.HasValue ? ReturnType.Value.ToString() : "")}";
|
public override string ToString() => $"({string.Join(", ", Parameters)}): {(ReturnType.HasValue ? ReturnType.Value.ToString() : "")}";
|
||||||
|
|||||||
Reference in New Issue
Block a user