...
This commit is contained in:
@@ -9,6 +9,7 @@ public class Lexer
|
||||
["func"] = Symbol.Func,
|
||||
["extern"] = Symbol.Extern,
|
||||
["return"] = Symbol.Return,
|
||||
["include"] = Symbol.Include,
|
||||
["let"] = Symbol.Let,
|
||||
["if"] = Symbol.If,
|
||||
["else"] = Symbol.Else,
|
||||
|
||||
@@ -8,6 +8,7 @@ public class SymbolToken(Symbol symbol) : Token
|
||||
public enum Symbol
|
||||
{
|
||||
Whitespace,
|
||||
Include,
|
||||
Extern,
|
||||
Func,
|
||||
Return,
|
||||
|
||||
7
Nub.Lang/Nub.Lang/Frontend/Parsing/FileNode.cs
Normal file
7
Nub.Lang/Nub.Lang/Frontend/Parsing/FileNode.cs
Normal file
@@ -0,0 +1,7 @@
|
||||
namespace Nub.Lang.Frontend.Parsing;
|
||||
|
||||
public class FileNode(IReadOnlyCollection<string> includes, IReadOnlyCollection<DefinitionNode> definitions) : Node
|
||||
{
|
||||
public IReadOnlyCollection<string> Includes { get; } = includes;
|
||||
public IReadOnlyCollection<DefinitionNode> Definitions { get; } = definitions;
|
||||
}
|
||||
@@ -14,16 +14,30 @@ public class Parser
|
||||
_tokens = tokens.ToArray();
|
||||
}
|
||||
|
||||
public IReadOnlyCollection<DefinitionNode> Parse()
|
||||
public FileNode ParseFile(string relativePath)
|
||||
{
|
||||
_index = 0;
|
||||
List<DefinitionNode> definitions = [];
|
||||
List<string> includes = [];
|
||||
|
||||
while (TryExpectSymbol(Symbol.Include))
|
||||
{
|
||||
var name = ExpectLiteral();
|
||||
if (name.Type is not StringType)
|
||||
{
|
||||
throw new Exception("Using statements must have a string literal value");
|
||||
}
|
||||
|
||||
TryExpectSymbol(Symbol.Semicolon);
|
||||
includes.Add(name.Value);
|
||||
}
|
||||
|
||||
while (Peek().HasValue)
|
||||
{
|
||||
definitions.Add(ParseDefinition());
|
||||
}
|
||||
|
||||
return definitions;
|
||||
return new FileNode(includes, definitions);
|
||||
}
|
||||
|
||||
private DefinitionNode ParseDefinition()
|
||||
|
||||
@@ -13,25 +13,39 @@ public class Func(string name, IReadOnlyCollection<FuncParameter> parameters, Op
|
||||
|
||||
public class ExpressionTyper
|
||||
{
|
||||
private readonly IReadOnlyCollection<Func> _functions;
|
||||
private readonly IReadOnlyCollection<GlobalVariableDefinitionNode> _variableDefinitions;
|
||||
private readonly List<Func> _functions;
|
||||
private readonly List<GlobalVariableDefinitionNode> _variableDefinitions;
|
||||
private readonly Stack<Variable> _variables;
|
||||
|
||||
public ExpressionTyper(IReadOnlyCollection<DefinitionNode> definitions)
|
||||
public ExpressionTyper(FileNode file, Dictionary<string, FileNode> deps)
|
||||
{
|
||||
var functions = definitions
|
||||
_variables = new Stack<Variable>();
|
||||
_functions = [];
|
||||
_variableDefinitions = [];
|
||||
|
||||
ResolveFunctions(file, deps);
|
||||
}
|
||||
|
||||
private void ResolveFunctions(FileNode file, Dictionary<string, FileNode> deps)
|
||||
{
|
||||
var functions = file.Definitions
|
||||
.OfType<LocalFuncDefinitionNode>()
|
||||
.Select(f => new Func(f.Name, f.Parameters, f.Body, f.ReturnType))
|
||||
.ToList();
|
||||
|
||||
var externFunctions = definitions
|
||||
var externFunctions = file.Definitions
|
||||
.OfType<ExternFuncDefinitionNode>()
|
||||
.Select(f => new Func(f.Name, f.Parameters, Optional<BlockNode>.Empty(), f.ReturnType))
|
||||
.ToList();
|
||||
|
||||
_functions = functions.Concat(externFunctions).ToList();
|
||||
_variableDefinitions = definitions.OfType<GlobalVariableDefinitionNode>().ToList();
|
||||
_variables = new Stack<Variable>();
|
||||
_functions.AddRange(functions);
|
||||
_functions.AddRange(externFunctions);
|
||||
_variableDefinitions.AddRange(file.Definitions.OfType<GlobalVariableDefinitionNode>());
|
||||
|
||||
foreach (var include in file.Includes)
|
||||
{
|
||||
ResolveFunctions(deps[include], deps);
|
||||
}
|
||||
}
|
||||
|
||||
public void Populate()
|
||||
|
||||
Reference in New Issue
Block a user