Add support for global functions
This commit is contained in:
@@ -1,6 +1,6 @@
|
|||||||
import "core";
|
import "core";
|
||||||
|
|
||||||
func main() {
|
global func main() {
|
||||||
let x = "test";
|
let x = "test";
|
||||||
puts(x);
|
puts(x);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -50,7 +50,11 @@ public class Generator
|
|||||||
{
|
{
|
||||||
_variables.Clear();
|
_variables.Clear();
|
||||||
var parameters = node.Parameters.Select(p => $"{QbeTypeName(p.Type)} %{p.Name}");
|
var parameters = node.Parameters.Select(p => $"{QbeTypeName(p.Type)} %{p.Name}");
|
||||||
_builder.Append("export function ");
|
if (node.Global)
|
||||||
|
{
|
||||||
|
_builder.Append("export ");
|
||||||
|
}
|
||||||
|
_builder.Append("function ");
|
||||||
if (node.ReturnType.HasValue)
|
if (node.ReturnType.HasValue)
|
||||||
{
|
{
|
||||||
_builder.Append($"{QbeTypeName(node.ReturnType.Value)} ");
|
_builder.Append($"{QbeTypeName(node.ReturnType.Value)} ");
|
||||||
@@ -113,10 +117,12 @@ public class Generator
|
|||||||
|
|
||||||
private void GenerateBreak()
|
private void GenerateBreak()
|
||||||
{
|
{
|
||||||
|
throw new NotImplementedException();
|
||||||
}
|
}
|
||||||
|
|
||||||
private void GenerateContinue()
|
private void GenerateContinue()
|
||||||
{
|
{
|
||||||
|
throw new NotImplementedException();
|
||||||
}
|
}
|
||||||
|
|
||||||
private void GenerateStatementFuncCall(FuncCallStatementNode funcCall)
|
private void GenerateStatementFuncCall(FuncCallStatementNode funcCall)
|
||||||
@@ -134,6 +140,7 @@ public class Generator
|
|||||||
|
|
||||||
private void GenerateIf(IfNode ifStatement)
|
private void GenerateIf(IfNode ifStatement)
|
||||||
{
|
{
|
||||||
|
throw new NotImplementedException();
|
||||||
}
|
}
|
||||||
|
|
||||||
private void GenerateReturn(ReturnNode @return)
|
private void GenerateReturn(ReturnNode @return)
|
||||||
@@ -161,6 +168,7 @@ public class Generator
|
|||||||
|
|
||||||
private void GenerateWhile(WhileNode whileStatement)
|
private void GenerateWhile(WhileNode whileStatement)
|
||||||
{
|
{
|
||||||
|
throw new NotImplementedException();
|
||||||
}
|
}
|
||||||
|
|
||||||
private string GenerateExpression(ExpressionNode expression)
|
private string GenerateExpression(ExpressionNode expression)
|
||||||
|
|||||||
@@ -5,6 +5,7 @@ public class Lexer
|
|||||||
private static readonly Dictionary<string, Symbol> Keywords = new()
|
private static readonly Dictionary<string, Symbol> Keywords = new()
|
||||||
{
|
{
|
||||||
["func"] = Symbol.Func,
|
["func"] = Symbol.Func,
|
||||||
|
["global"] = Symbol.Global,
|
||||||
["extern"] = Symbol.Extern,
|
["extern"] = Symbol.Extern,
|
||||||
["import"] = Symbol.Import,
|
["import"] = Symbol.Import,
|
||||||
["let"] = Symbol.Let,
|
["let"] = Symbol.Let,
|
||||||
|
|||||||
@@ -10,6 +10,7 @@ public enum Symbol
|
|||||||
Whitespace,
|
Whitespace,
|
||||||
Import,
|
Import,
|
||||||
Extern,
|
Extern,
|
||||||
|
Global,
|
||||||
Func,
|
Func,
|
||||||
Return,
|
Return,
|
||||||
Let,
|
Let,
|
||||||
|
|||||||
@@ -1,11 +1,12 @@
|
|||||||
namespace Nub.Lang.Frontend.Parsing;
|
namespace Nub.Lang.Frontend.Parsing;
|
||||||
|
|
||||||
public class LocalFuncDefinitionNode(string name, List<FuncParameter> parameters, BlockNode body, Optional<NubType> returnType) : DefinitionNode
|
public class LocalFuncDefinitionNode(string name, List<FuncParameter> parameters, BlockNode body, Optional<NubType> returnType, bool global) : DefinitionNode
|
||||||
{
|
{
|
||||||
public string Name { get; } = name;
|
public string Name { get; } = name;
|
||||||
public List<FuncParameter> Parameters { get; } = parameters;
|
public List<FuncParameter> Parameters { get; } = parameters;
|
||||||
public BlockNode Body { get; } = body;
|
public BlockNode Body { get; } = body;
|
||||||
public Optional<NubType> ReturnType { get; } = returnType;
|
public Optional<NubType> ReturnType { get; } = returnType;
|
||||||
|
public bool Global { get; } = global;
|
||||||
|
|
||||||
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 : "")}";
|
||||||
}
|
}
|
||||||
@@ -44,6 +44,7 @@ public class Parser
|
|||||||
return keyword.Symbol switch
|
return keyword.Symbol switch
|
||||||
{
|
{
|
||||||
Symbol.Func => ParseFuncDefinition(),
|
Symbol.Func => ParseFuncDefinition(),
|
||||||
|
Symbol.Global => ParseGlobalFuncDefinition(),
|
||||||
Symbol.Extern => ParseExternFuncDefinition(),
|
Symbol.Extern => ParseExternFuncDefinition(),
|
||||||
Symbol.Struct => ParseStruct(),
|
Symbol.Struct => ParseStruct(),
|
||||||
_ => throw new Exception("Unexpected symbol: " + keyword.Symbol)
|
_ => throw new Exception("Unexpected symbol: " + keyword.Symbol)
|
||||||
@@ -72,7 +73,33 @@ public class Parser
|
|||||||
|
|
||||||
var body = ParseBlock();
|
var body = ParseBlock();
|
||||||
|
|
||||||
return new LocalFuncDefinitionNode(name.Value, parameters, body, returnType);
|
return new LocalFuncDefinitionNode(name.Value, parameters, body, returnType, false);
|
||||||
|
}
|
||||||
|
|
||||||
|
private LocalFuncDefinitionNode ParseGlobalFuncDefinition()
|
||||||
|
{
|
||||||
|
ExpectSymbol(Symbol.Func);
|
||||||
|
var name = ExpectIdentifier();
|
||||||
|
List<FuncParameter> parameters = [];
|
||||||
|
ExpectSymbol(Symbol.OpenParen);
|
||||||
|
if (!TryExpectSymbol(Symbol.CloseParen))
|
||||||
|
{
|
||||||
|
while (!TryExpectSymbol(Symbol.CloseParen))
|
||||||
|
{
|
||||||
|
parameters.Add(ParseFuncParameter());
|
||||||
|
TryExpectSymbol(Symbol.Comma);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
var returnType = Optional<NubType>.Empty();
|
||||||
|
if (TryExpectSymbol(Symbol.Colon))
|
||||||
|
{
|
||||||
|
returnType = ParseTypeInstance();
|
||||||
|
}
|
||||||
|
|
||||||
|
var body = ParseBlock();
|
||||||
|
|
||||||
|
return new LocalFuncDefinitionNode(name.Value, parameters, body, returnType, true);
|
||||||
}
|
}
|
||||||
|
|
||||||
private ExternFuncDefinitionNode ParseExternFuncDefinition()
|
private ExternFuncDefinitionNode ParseExternFuncDefinition()
|
||||||
|
|||||||
@@ -1,8 +1,9 @@
|
|||||||
global _start
|
global _start
|
||||||
extern main
|
extern main, gc_init
|
||||||
|
|
||||||
section .text
|
section .text
|
||||||
_start:
|
_start:
|
||||||
|
call gc_init
|
||||||
call main
|
call main
|
||||||
mov rax, 60
|
mov rax, 60
|
||||||
mov rdi, 0
|
mov rdi, 0
|
||||||
|
|||||||
Reference in New Issue
Block a user