This commit is contained in:
2026-02-08 00:21:38 +01:00
parent cb2411a7eb
commit f2ea00b34d
3 changed files with 24 additions and 59 deletions

View File

@@ -1,5 +1,4 @@
using System.Globalization; using System.Text;
using System.Text;
namespace Compiler; namespace Compiler;
@@ -84,7 +83,6 @@ public sealed class Generator(List<NodeDefinition> nodes)
return node switch return node switch
{ {
NodeExpressionBoolLiteral expression => EmitExpressionBoolLiteral(expression), NodeExpressionBoolLiteral expression => EmitExpressionBoolLiteral(expression),
NodeExpressionFloatLiteral expression => EmitExpressionFloatLiteral(expression),
NodeExpressionIntLiteral expression => EmitExpressionIntLiteral(expression), NodeExpressionIntLiteral expression => EmitExpressionIntLiteral(expression),
NodeExpressionStringLiteral expression => EmitExpressionStringLiteral(expression), NodeExpressionStringLiteral expression => EmitExpressionStringLiteral(expression),
NodeExpressionIdent expression => EmitExpressionIdent(expression), NodeExpressionIdent expression => EmitExpressionIdent(expression),
@@ -96,11 +94,6 @@ public sealed class Generator(List<NodeDefinition> nodes)
return expression.Value.Value ? "1" : "0"; return expression.Value.Value ? "1" : "0";
} }
private string EmitExpressionFloatLiteral(NodeExpressionFloatLiteral expression)
{
return expression.Value.Value.ToString(CultureInfo.InvariantCulture);
}
private string EmitExpressionIntLiteral(NodeExpressionIntLiteral expression) private string EmitExpressionIntLiteral(NodeExpressionIntLiteral expression)
{ {
return expression.Value.Value.ToString(); return expression.Value.Value.ToString();

View File

@@ -83,11 +83,6 @@ public sealed class Parser(List<Token> tokens)
return new NodeExpressionIntLiteral(TokensFrom(startIndex), intLiteral); return new NodeExpressionIntLiteral(TokensFrom(startIndex), intLiteral);
} }
if (TryExpectFloatLiteral(out var floatLiteral))
{
return new NodeExpressionFloatLiteral(TokensFrom(startIndex), floatLiteral);
}
if (TryExpectStringLiteral(out var stringLiteral)) if (TryExpectStringLiteral(out var stringLiteral))
{ {
return new NodeExpressionStringLiteral(TokensFrom(startIndex), stringLiteral); return new NodeExpressionStringLiteral(TokensFrom(startIndex), stringLiteral);
@@ -227,19 +222,6 @@ public sealed class Parser(List<Token> tokens)
return false; return false;
} }
private bool TryExpectFloatLiteral([NotNullWhen(true)] out TokenFloatLiteral? floatLiteral)
{
if (Peek() is TokenFloatLiteral token)
{
Consume();
floatLiteral = token;
return true;
}
floatLiteral = null;
return false;
}
private bool TryExpectStringLiteral([NotNullWhen(true)] out TokenStringLiteral? stringLiteral) private bool TryExpectStringLiteral([NotNullWhen(true)] out TokenStringLiteral? stringLiteral)
{ {
if (Peek() is TokenStringLiteral token) if (Peek() is TokenStringLiteral token)
@@ -293,15 +275,15 @@ public abstract class NodeDefinition(List<Token> tokens) : Node(tokens);
public sealed class NodeDefinitionFunc(List<Token> tokens, TokenIdent name, List<NodeDefinitionFunc.Param> parameters, NodeStatement body, NodeType returnType) public sealed class NodeDefinitionFunc(List<Token> tokens, TokenIdent name, List<NodeDefinitionFunc.Param> parameters, NodeStatement body, NodeType returnType)
: NodeDefinition(tokens) : NodeDefinition(tokens)
{ {
public TokenIdent Name = name; public readonly TokenIdent Name = name;
public List<Param> Parameters = parameters; public readonly List<Param> Parameters = parameters;
public NodeStatement Body = body; public readonly NodeStatement Body = body;
public NodeType ReturnType = returnType; public readonly NodeType ReturnType = returnType;
public sealed class Param(List<Token> tokens, TokenIdent name, NodeType type) : Node(tokens) public sealed class Param(List<Token> tokens, TokenIdent name, NodeType type) : Node(tokens)
{ {
public TokenIdent Name = name; public readonly TokenIdent Name = name;
public NodeType Type = type; public readonly NodeType Type = type;
} }
} }
@@ -309,40 +291,35 @@ public abstract class NodeStatement(List<Token> tokens) : Node(tokens);
public sealed class NodeStatementBlock(List<Token> tokens, List<NodeStatement> statements) : NodeStatement(tokens) public sealed class NodeStatementBlock(List<Token> tokens, List<NodeStatement> statements) : NodeStatement(tokens)
{ {
public List<NodeStatement> Statements = statements; public readonly List<NodeStatement> Statements = statements;
} }
public sealed class NodeStatementFuncCall(List<Token> tokens, NodeExpression func, List<NodeExpression> parameters) : NodeStatement(tokens) public sealed class NodeStatementFuncCall(List<Token> tokens, NodeExpression func, List<NodeExpression> parameters) : NodeStatement(tokens)
{ {
public NodeExpression Func = func; public readonly NodeExpression Func = func;
public List<NodeExpression> Parameters = parameters; public readonly List<NodeExpression> Parameters = parameters;
} }
public abstract class NodeExpression(List<Token> tokens) : Node(tokens); public abstract class NodeExpression(List<Token> tokens) : Node(tokens);
public sealed class NodeExpressionIntLiteral(List<Token> tokens, TokenIntLiteral value) : NodeExpression(tokens) public sealed class NodeExpressionIntLiteral(List<Token> tokens, TokenIntLiteral value) : NodeExpression(tokens)
{ {
public TokenIntLiteral Value = value; public readonly TokenIntLiteral Value = value;
}
public sealed class NodeExpressionFloatLiteral(List<Token> tokens, TokenFloatLiteral value) : NodeExpression(tokens)
{
public TokenFloatLiteral Value = value;
} }
public sealed class NodeExpressionStringLiteral(List<Token> tokens, TokenStringLiteral value) : NodeExpression(tokens) public sealed class NodeExpressionStringLiteral(List<Token> tokens, TokenStringLiteral value) : NodeExpression(tokens)
{ {
public TokenStringLiteral Value = value; public readonly TokenStringLiteral Value = value;
} }
public sealed class NodeExpressionBoolLiteral(List<Token> tokens, TokenBoolLiteral value) : NodeExpression(tokens) public sealed class NodeExpressionBoolLiteral(List<Token> tokens, TokenBoolLiteral value) : NodeExpression(tokens)
{ {
public TokenBoolLiteral Value = value; public readonly TokenBoolLiteral Value = value;
} }
public sealed class NodeExpressionIdent(List<Token> tokens, TokenIdent value) : NodeExpression(tokens) public sealed class NodeExpressionIdent(List<Token> tokens, TokenIdent value) : NodeExpression(tokens)
{ {
public TokenIdent Value = value; public readonly TokenIdent Value = value;
} }
public abstract class NodeType(List<Token> tokens) : Node(tokens); public abstract class NodeType(List<Token> tokens) : Node(tokens);
@@ -351,27 +328,27 @@ public sealed class NubTypeVoid(List<Token> tokens) : NodeType(tokens);
public sealed class NodeTypeUInt(List<Token> tokens, int width) : NodeType(tokens) public sealed class NodeTypeUInt(List<Token> tokens, int width) : NodeType(tokens)
{ {
public int Width = width; public readonly int Width = width;
} }
public sealed class NodeTypeSInt(List<Token> tokens, int width) : NodeType(tokens) public sealed class NodeTypeSInt(List<Token> tokens, int width) : NodeType(tokens)
{ {
public int Width = width; public readonly int Width = width;
} }
public sealed class NodeTypeFloat(List<Token> tokens, int width) : NodeType(tokens) public sealed class NodeTypeFloat(List<Token> tokens, int width) : NodeType(tokens)
{ {
public int Width = width; public readonly int Width = width;
} }
public sealed class NodeTypeString(List<Token> tokens) : NodeType(tokens); public sealed class NodeTypeString(List<Token> tokens) : NodeType(tokens);
public sealed class NodeTypeCustom(List<Token> tokens, TokenIdent name) : NodeType(tokens) public sealed class NodeTypeCustom(List<Token> tokens, TokenIdent name) : NodeType(tokens)
{ {
public TokenIdent Name = name; public readonly TokenIdent Name = name;
} }
public sealed class NodeTypePointer(List<Token> tokens, NodeType to) : NodeType(tokens) public sealed class NodeTypePointer(List<Token> tokens, NodeType to) : NodeType(tokens)
{ {
public NodeType To = to; public readonly NodeType To = to;
} }

View File

@@ -342,7 +342,7 @@ public abstract class Token(int line, int column, int length)
public sealed class TokenIdent(int line, int column, int length, string ident) : Token(line, column, length) public sealed class TokenIdent(int line, int column, int length, string ident) : Token(line, column, length)
{ {
public string Ident = ident; public readonly string Ident = ident;
} }
public sealed class TokenIntLiteral(int line, int column, int length, BigInteger value) : Token(line, column, length) public sealed class TokenIntLiteral(int line, int column, int length, BigInteger value) : Token(line, column, length)
@@ -350,19 +350,14 @@ public sealed class TokenIntLiteral(int line, int column, int length, BigInteger
public BigInteger Value = value; public BigInteger Value = value;
} }
public sealed class TokenFloatLiteral(int line, int column, int length, decimal value) : Token(line, column, length)
{
public decimal Value = value;
}
public sealed class TokenStringLiteral(int line, int column, int length, string value) : Token(line, column, length) public sealed class TokenStringLiteral(int line, int column, int length, string value) : Token(line, column, length)
{ {
public string Value = value; public readonly string Value = value;
} }
public sealed class TokenBoolLiteral(int line, int column, int length, bool value) : Token(line, column, length) public sealed class TokenBoolLiteral(int line, int column, int length, bool value) : Token(line, column, length)
{ {
public bool Value = value; public readonly bool Value = value;
} }
public enum Symbol public enum Symbol
@@ -394,7 +389,7 @@ public enum Symbol
public sealed class TokenSymbol(int line, int column, int length, Symbol symbol) : Token(line, column, length) public sealed class TokenSymbol(int line, int column, int length, Symbol symbol) : Token(line, column, length)
{ {
public Symbol Symbol = symbol; public readonly Symbol Symbol = symbol;
} }
public enum Keyword public enum Keyword
@@ -406,5 +401,5 @@ public enum Keyword
public sealed class TokenKeyword(int line, int column, int length, Keyword keyword) : Token(line, column, length) public sealed class TokenKeyword(int line, int column, int length, Keyword keyword) : Token(line, column, length)
{ {
public Keyword Keyword = keyword; public readonly Keyword Keyword = keyword;
} }