...
This commit is contained in:
@@ -185,10 +185,11 @@ public sealed class Generator(List<NodeDefinition> nodes)
|
|||||||
return node switch
|
return node switch
|
||||||
{
|
{
|
||||||
NodeExpressionBinary expression => EmitExpressionBinary(expression),
|
NodeExpressionBinary expression => EmitExpressionBinary(expression),
|
||||||
|
NodeExpressionUnary expression => EmitExpressionUnary(expression),
|
||||||
NodeExpressionBoolLiteral expression => expression.Value.Value ? "true" : "false",
|
NodeExpressionBoolLiteral expression => expression.Value.Value ? "true" : "false",
|
||||||
NodeExpressionIntLiteral expression => expression.Value.Value.ToString(),
|
NodeExpressionIntLiteral expression => expression.Value.Value.ToString(),
|
||||||
NodeExpressionStringLiteral expression => $"(struct string){{ \"{expression.Value.Value}\", {expression.Value.Value.Length} }}",
|
NodeExpressionStringLiteral expression => $"(struct string){{ \"{expression.Value.Value}\", {expression.Value.Value.Length} }}",
|
||||||
NodeExpressionStructLiteral expression => EmitStructLiteral(expression),
|
NodeExpressionStructLiteral expression => EmitExpressionStructLiteral(expression),
|
||||||
NodeExpressionMemberAccess expression => EmitExpressionMemberAccess(expression),
|
NodeExpressionMemberAccess expression => EmitExpressionMemberAccess(expression),
|
||||||
NodeExpressionIdent expression => expression.Value.Ident,
|
NodeExpressionIdent expression => expression.Value.Ident,
|
||||||
_ => throw new ArgumentOutOfRangeException(nameof(node), node, null)
|
_ => throw new ArgumentOutOfRangeException(nameof(node), node, null)
|
||||||
@@ -221,23 +222,19 @@ public sealed class Generator(List<NodeDefinition> nodes)
|
|||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
private static string CType(NodeType node, string? varName = null)
|
private string EmitExpressionUnary(NodeExpressionUnary expression)
|
||||||
{
|
{
|
||||||
return node switch
|
var target = EmitExpression(expression.Target);
|
||||||
|
|
||||||
|
return expression.Operation switch
|
||||||
{
|
{
|
||||||
NodeTypeVoid => "void" + (varName != null ? $" {varName}" : ""),
|
NodeExpressionUnary.Op.Negate => $"(-{target})",
|
||||||
NodeTypeBool => "bool" + (varName != null ? $" {varName}" : ""),
|
NodeExpressionUnary.Op.Invert => $"(!{target})",
|
||||||
NodeTypeCustom type => $"struct {type.Name.Ident}" + (varName != null ? $" {varName}" : ""),
|
_ => throw new ArgumentOutOfRangeException()
|
||||||
NodeTypeSInt type => $"int{type.Width}_t" + (varName != null ? $" {varName}" : ""),
|
|
||||||
NodeTypeUInt type => $"uint{type.Width}_t" + (varName != null ? $" {varName}" : ""),
|
|
||||||
NodeTypePointer type => CType(type.To) + (varName != null ? $" *{varName}" : "*"),
|
|
||||||
NodeTypeString => "struct string" + (varName != null ? $" {varName}" : ""),
|
|
||||||
NodeTypeFunc type => $"{CType(type.ReturnType)} (*{varName})({string.Join(", ", type.Parameters.Select(p => CType(p)))})",
|
|
||||||
_ => throw new ArgumentOutOfRangeException(nameof(node), node, null)
|
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
private string EmitStructLiteral(NodeExpressionStructLiteral expression)
|
private string EmitExpressionStructLiteral(NodeExpressionStructLiteral expression)
|
||||||
{
|
{
|
||||||
var initializerValues = new Dictionary<string, string>();
|
var initializerValues = new Dictionary<string, string>();
|
||||||
|
|
||||||
@@ -257,6 +254,22 @@ public sealed class Generator(List<NodeDefinition> nodes)
|
|||||||
var target = EmitExpression(expression.Target);
|
var target = EmitExpression(expression.Target);
|
||||||
return $"{target}.{expression.Name.Ident}";
|
return $"{target}.{expression.Name.Ident}";
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private static string CType(NodeType node, string? varName = null)
|
||||||
|
{
|
||||||
|
return node switch
|
||||||
|
{
|
||||||
|
NodeTypeVoid => "void" + (varName != null ? $" {varName}" : ""),
|
||||||
|
NodeTypeBool => "bool" + (varName != null ? $" {varName}" : ""),
|
||||||
|
NodeTypeCustom type => $"struct {type.Name.Ident}" + (varName != null ? $" {varName}" : ""),
|
||||||
|
NodeTypeSInt type => $"int{type.Width}_t" + (varName != null ? $" {varName}" : ""),
|
||||||
|
NodeTypeUInt type => $"uint{type.Width}_t" + (varName != null ? $" {varName}" : ""),
|
||||||
|
NodeTypePointer type => CType(type.To) + (varName != null ? $" *{varName}" : "*"),
|
||||||
|
NodeTypeString => "struct string" + (varName != null ? $" {varName}" : ""),
|
||||||
|
NodeTypeFunc type => $"{CType(type.ReturnType)} (*{varName})({string.Join(", ", type.Parameters.Select(p => CType(p)))})",
|
||||||
|
_ => throw new ArgumentOutOfRangeException(nameof(node), node, null)
|
||||||
|
};
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
internal class IndentedTextWriter
|
internal class IndentedTextWriter
|
||||||
|
|||||||
@@ -208,6 +208,16 @@ public sealed class Parser(string fileName, List<Token> tokens)
|
|||||||
ExpectSymbol(Symbol.CloseParen);
|
ExpectSymbol(Symbol.CloseParen);
|
||||||
expr = value;
|
expr = value;
|
||||||
}
|
}
|
||||||
|
else if (TryExpectSymbol(Symbol.Minus))
|
||||||
|
{
|
||||||
|
var target = ParseExpression();
|
||||||
|
expr = new NodeExpressionUnary(TokensFrom(startIndex), target, NodeExpressionUnary.Op.Negate);
|
||||||
|
}
|
||||||
|
else if (TryExpectSymbol(Symbol.Bang))
|
||||||
|
{
|
||||||
|
var target = ParseExpression();
|
||||||
|
expr = new NodeExpressionUnary(TokensFrom(startIndex), target, NodeExpressionUnary.Op.Invert);
|
||||||
|
}
|
||||||
else if (TryExpectIntLiteral(out var intLiteral))
|
else if (TryExpectIntLiteral(out var intLiteral))
|
||||||
{
|
{
|
||||||
expr = new NodeExpressionIntLiteral(TokensFrom(startIndex), intLiteral);
|
expr = new NodeExpressionIntLiteral(TokensFrom(startIndex), intLiteral);
|
||||||
@@ -617,7 +627,6 @@ public sealed class NodeExpressionBinary(List<Token> tokens, NodeExpression left
|
|||||||
public readonly Op Operation = operation;
|
public readonly Op Operation = operation;
|
||||||
public readonly NodeExpression Right = right;
|
public readonly NodeExpression Right = right;
|
||||||
|
|
||||||
|
|
||||||
public enum Op
|
public enum Op
|
||||||
{
|
{
|
||||||
Add,
|
Add,
|
||||||
@@ -645,6 +654,18 @@ public sealed class NodeExpressionBinary(List<Token> tokens, NodeExpression left
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public sealed class NodeExpressionUnary(List<Token> tokens, NodeExpression target, NodeExpressionUnary.Op op) : NodeExpression(tokens)
|
||||||
|
{
|
||||||
|
public NodeExpression Target { get; } = target;
|
||||||
|
public Op Operation { get; } = op;
|
||||||
|
|
||||||
|
public enum Op
|
||||||
|
{
|
||||||
|
Negate,
|
||||||
|
Invert,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
public abstract class NodeType(List<Token> tokens) : Node(tokens);
|
public abstract class NodeType(List<Token> tokens) : Node(tokens);
|
||||||
|
|
||||||
public sealed class NodeTypeVoid(List<Token> tokens) : NodeType(tokens);
|
public sealed class NodeTypeVoid(List<Token> tokens) : NodeType(tokens);
|
||||||
|
|||||||
@@ -7,8 +7,9 @@ func main(): i32 {
|
|||||||
let x: i32 = 23
|
let x: i32 = 23
|
||||||
x = 24
|
x = 24
|
||||||
|
|
||||||
if true {
|
if !true {
|
||||||
x = 49
|
x = 49
|
||||||
|
return x
|
||||||
} else {
|
} else {
|
||||||
x = 3
|
x = 3
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user