This commit is contained in:
nub31
2025-01-28 17:37:09 +01:00
parent cddf4c4f78
commit 6356e37f77
45 changed files with 59 additions and 57 deletions

View File

@@ -1,9 +1,9 @@
using System.Text; using System.Text;
using Nub.Lang.Parsing; using Nub.Lang.Frontend.Parsing;
namespace Nub.Lang.Generation; namespace Nub.Lang.Backend.Custom;
public class Generator public class CustomGenerator
{ {
private const string Entrypoint = "main"; private const string Entrypoint = "main";
@@ -15,7 +15,7 @@ public class Generator
private int _stringIndex; private int _stringIndex;
private int _labelIndex; private int _labelIndex;
public Generator(IReadOnlyCollection<DefinitionNode> definitions) public CustomGenerator(IReadOnlyCollection<DefinitionNode> definitions)
{ {
_strings = []; _strings = [];
_definitions = definitions; _definitions = definitions;

View File

@@ -1,7 +1,7 @@
using Nub.Lang.Parsing; using Nub.Core;
using Nub.Core; using Nub.Lang.Frontend.Parsing;
namespace Nub.Lang.Generation; namespace Nub.Lang.Backend.Custom;
public class SymbolTable public class SymbolTable
{ {

View File

@@ -1,4 +1,4 @@
namespace Nub.Lang.Lexing; namespace Nub.Lang.Frontend.Lexing;
public class IdentifierToken(string value) : Token public class IdentifierToken(string value) : Token
{ {

View File

@@ -1,6 +1,6 @@
using Nub.Core; using Nub.Core;
namespace Nub.Lang.Lexing; namespace Nub.Lang.Frontend.Lexing;
public class Lexer public class Lexer
{ {

View File

@@ -1,4 +1,4 @@
namespace Nub.Lang.Lexing; namespace Nub.Lang.Frontend.Lexing;
public class LiteralToken(Type type, string value) : Token public class LiteralToken(Type type, string value) : Token
{ {

View File

@@ -1,4 +1,4 @@
namespace Nub.Lang.Lexing; namespace Nub.Lang.Frontend.Lexing;
public class SymbolToken(Symbol symbol) : Token public class SymbolToken(Symbol symbol) : Token
{ {

View File

@@ -0,0 +1,3 @@
namespace Nub.Lang.Frontend.Lexing;
public abstract class Token;

View File

@@ -1,4 +1,4 @@
namespace Nub.Lang.Parsing; namespace Nub.Lang.Frontend.Parsing;
public class BinaryExpressionNode(ExpressionNode left, BinaryExpressionOperator @operator, ExpressionNode right) : ExpressionNode public class BinaryExpressionNode(ExpressionNode left, BinaryExpressionOperator @operator, ExpressionNode right) : ExpressionNode
{ {

View File

@@ -1,4 +1,4 @@
namespace Nub.Lang.Parsing; namespace Nub.Lang.Frontend.Parsing;
public class BlockNode(IReadOnlyCollection<StatementNode> statements) : Node public class BlockNode(IReadOnlyCollection<StatementNode> statements) : Node
{ {

View File

@@ -0,0 +1,3 @@
namespace Nub.Lang.Frontend.Parsing;
public abstract class DefinitionNode : Node;

View File

@@ -1,4 +1,4 @@
namespace Nub.Lang.Parsing; namespace Nub.Lang.Frontend.Parsing;
public abstract class ExpressionNode : Node public abstract class ExpressionNode : Node
{ {

View File

@@ -1,6 +1,6 @@
using Nub.Core; using Nub.Core;
namespace Nub.Lang.Parsing; namespace Nub.Lang.Frontend.Parsing;
public class ExternFuncDefinitionNode(string name, IReadOnlyCollection<FuncParameter> parameters, Optional<Type> returnType) : DefinitionNode public class ExternFuncDefinitionNode(string name, IReadOnlyCollection<FuncParameter> parameters, Optional<Type> returnType) : DefinitionNode
{ {

View File

@@ -1,4 +1,4 @@
namespace Nub.Lang.Parsing; namespace Nub.Lang.Frontend.Parsing;
public class FuncCall(string name, IReadOnlyCollection<ExpressionNode> parameters) public class FuncCall(string name, IReadOnlyCollection<ExpressionNode> parameters)
{ {

View File

@@ -1,4 +1,4 @@
namespace Nub.Lang.Parsing; namespace Nub.Lang.Frontend.Parsing;
public class FuncCallExpressionNode(FuncCall funcCall) : ExpressionNode public class FuncCallExpressionNode(FuncCall funcCall) : ExpressionNode
{ {

View File

@@ -1,4 +1,4 @@
namespace Nub.Lang.Parsing; namespace Nub.Lang.Frontend.Parsing;
public class FuncCallStatementNode(FuncCall funcCall) : StatementNode public class FuncCallStatementNode(FuncCall funcCall) : StatementNode
{ {

View File

@@ -1,4 +1,4 @@
namespace Nub.Lang.Parsing; namespace Nub.Lang.Frontend.Parsing;
public class GlobalVariableDefinitionNode(string name, ExpressionNode value) : DefinitionNode public class GlobalVariableDefinitionNode(string name, ExpressionNode value) : DefinitionNode
{ {

View File

@@ -1,4 +1,4 @@
namespace Nub.Lang.Parsing; namespace Nub.Lang.Frontend.Parsing;
public class IdentifierNode(string identifier) : ExpressionNode public class IdentifierNode(string identifier) : ExpressionNode
{ {

View File

@@ -1,6 +1,6 @@
using Nub.Core; using Nub.Core;
namespace Nub.Lang.Parsing; namespace Nub.Lang.Frontend.Parsing;
public class IfNode(ExpressionNode condition, BlockNode body, Optional<Variant<IfNode, BlockNode>> @else) : StatementNode public class IfNode(ExpressionNode condition, BlockNode body, Optional<Variant<IfNode, BlockNode>> @else) : StatementNode
{ {

View File

@@ -1,4 +1,4 @@
namespace Nub.Lang.Parsing; namespace Nub.Lang.Frontend.Parsing;
public class LiteralNode(string literal, Type type) : ExpressionNode public class LiteralNode(string literal, Type type) : ExpressionNode
{ {

View File

@@ -1,6 +1,6 @@
using Nub.Core; using Nub.Core;
namespace Nub.Lang.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, IReadOnlyCollection<FuncParameter> parameters, BlockNode body, Optional<Type> returnType) : DefinitionNode
{ {

View File

@@ -0,0 +1,3 @@
namespace Nub.Lang.Frontend.Parsing;
public abstract class Node;

View File

@@ -1,8 +1,8 @@
using System.Diagnostics.CodeAnalysis; using System.Diagnostics.CodeAnalysis;
using Nub.Lang.Lexing;
using Nub.Core; using Nub.Core;
using Nub.Lang.Frontend.Lexing;
namespace Nub.Lang.Parsing; namespace Nub.Lang.Frontend.Parsing;
public class Parser public class Parser
{ {

View File

@@ -1,6 +1,6 @@
using Nub.Core; using Nub.Core;
namespace Nub.Lang.Parsing; namespace Nub.Lang.Frontend.Parsing;
public class ReturnNode(Optional<ExpressionNode> value) : StatementNode public class ReturnNode(Optional<ExpressionNode> value) : StatementNode
{ {

View File

@@ -0,0 +1,3 @@
namespace Nub.Lang.Frontend.Parsing;
public abstract class StatementNode : Node;

View File

@@ -1,4 +1,4 @@
namespace Nub.Lang.Parsing; namespace Nub.Lang.Frontend.Parsing;
public class Syscall(IReadOnlyCollection<ExpressionNode> parameters) public class Syscall(IReadOnlyCollection<ExpressionNode> parameters)
{ {

View File

@@ -1,4 +1,4 @@
namespace Nub.Lang.Parsing; namespace Nub.Lang.Frontend.Parsing;
public class SyscallExpressionNode(Syscall syscall) : ExpressionNode public class SyscallExpressionNode(Syscall syscall) : ExpressionNode
{ {

View File

@@ -1,4 +1,4 @@
namespace Nub.Lang.Parsing; namespace Nub.Lang.Frontend.Parsing;
public class SyscallStatementNode(Syscall syscall) : StatementNode public class SyscallStatementNode(Syscall syscall) : StatementNode
{ {

View File

@@ -1,4 +1,4 @@
namespace Nub.Lang.Parsing; namespace Nub.Lang.Frontend.Parsing;
public class VariableAssignmentNode(string name, ExpressionNode value) : StatementNode public class VariableAssignmentNode(string name, ExpressionNode value) : StatementNode
{ {

View File

@@ -1,4 +1,4 @@
namespace Nub.Lang.Parsing; namespace Nub.Lang.Frontend.Parsing;
public class VariableReassignmentNode(string name, ExpressionNode value) : StatementNode public class VariableReassignmentNode(string name, ExpressionNode value) : StatementNode
{ {

View File

@@ -1,7 +1,7 @@
using Nub.Core; using Nub.Core;
using Nub.Lang.Parsing; using Nub.Lang.Frontend.Parsing;
namespace Nub.Lang.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, IReadOnlyCollection<FuncParameter> parameters, Optional<BlockNode> body, Optional<Type> returnType)
{ {

View File

@@ -1,3 +0,0 @@
namespace Nub.Lang.Lexing;
public abstract class Token;

View File

@@ -1,6 +0,0 @@
#!/bin/sh
nasm -g -felf64 out.asm -o out.o
nasm -g -felf64 core/strlen.asm -o strlen.o
nasm -g -felf64 core/strcmp.asm -o strcmp.o
ld -o out out.o strlen.o strcmp.o

View File

@@ -1,3 +0,0 @@
namespace Nub.Lang.Parsing;
public abstract class DefinitionNode : Node;

View File

@@ -1,3 +0,0 @@
namespace Nub.Lang.Parsing;
public abstract class Node;

View File

@@ -1,3 +0,0 @@
namespace Nub.Lang.Parsing;
public abstract class StatementNode : Node;

View File

@@ -1,7 +1,7 @@
using Nub.Lang.Generation; using Nub.Lang.Backend.Custom;
using Nub.Lang.Lexing; using Nub.Lang.Frontend.Lexing;
using Nub.Lang.Parsing; using Nub.Lang.Frontend.Parsing;
using Nub.Lang.Typing; using Nub.Lang.Frontend.Typing;
var src = File.ReadAllText(args[0]); var src = File.ReadAllText(args[0]);
@@ -14,7 +14,7 @@ var definitions = parser.Parse();
var typer = new ExpressionTyper(definitions); var typer = new ExpressionTyper(definitions);
typer.Populate(); typer.Populate();
var generator = new Generator(definitions); var generator = new CustomGenerator(definitions);
var asm = generator.Generate(); var asm = generator.Generate();
Console.WriteLine(asm); Console.WriteLine(asm);

View File

@@ -1,4 +1,3 @@
namespace core; namespace core;
extern func strlen(msg: String): int64;
extern func strcmp(a: String, b: String): bool; extern func strcmp(a: String, b: String): bool;

View File

@@ -0,0 +1,3 @@
namespace core;
extern func strlen(msg: String): int64;

View File

@@ -0,0 +1,6 @@
#!/bin/sh
nasm -g -felf64 out.asm -o out.o
nasm -g -felf64 ../input/core/string/strlen.asm -o strlen.o
nasm -g -felf64 ../input/core/string/strcmp.asm -o strcmp.o
ld -o out out.o strlen.o strcmp.o