diff --git a/compiler/Compiler/Generator.cs b/compiler/Compiler/Generator.cs index 4c196c7..1168f22 100644 --- a/compiler/Compiler/Generator.cs +++ b/compiler/Compiler/Generator.cs @@ -185,10 +185,11 @@ public sealed class Generator(List nodes) return node switch { NodeExpressionBinary expression => EmitExpressionBinary(expression), + NodeExpressionUnary expression => EmitExpressionUnary(expression), NodeExpressionBoolLiteral expression => expression.Value.Value ? "true" : "false", NodeExpressionIntLiteral expression => expression.Value.Value.ToString(), NodeExpressionStringLiteral expression => $"(struct string){{ \"{expression.Value.Value}\", {expression.Value.Value.Length} }}", - NodeExpressionStructLiteral expression => EmitStructLiteral(expression), + NodeExpressionStructLiteral expression => EmitExpressionStructLiteral(expression), NodeExpressionMemberAccess expression => EmitExpressionMemberAccess(expression), NodeExpressionIdent expression => expression.Value.Ident, _ => throw new ArgumentOutOfRangeException(nameof(node), node, null) @@ -221,23 +222,19 @@ public sealed class Generator(List 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}" : ""), - 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) + NodeExpressionUnary.Op.Negate => $"(-{target})", + NodeExpressionUnary.Op.Invert => $"(!{target})", + _ => throw new ArgumentOutOfRangeException() }; } - private string EmitStructLiteral(NodeExpressionStructLiteral expression) + private string EmitExpressionStructLiteral(NodeExpressionStructLiteral expression) { var initializerValues = new Dictionary(); @@ -257,6 +254,22 @@ public sealed class Generator(List nodes) var target = EmitExpression(expression.Target); 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 diff --git a/compiler/Compiler/Parser.cs b/compiler/Compiler/Parser.cs index 73daec7..8c537ad 100644 --- a/compiler/Compiler/Parser.cs +++ b/compiler/Compiler/Parser.cs @@ -208,6 +208,16 @@ public sealed class Parser(string fileName, List tokens) ExpectSymbol(Symbol.CloseParen); 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)) { expr = new NodeExpressionIntLiteral(TokensFrom(startIndex), intLiteral); @@ -617,7 +627,6 @@ public sealed class NodeExpressionBinary(List tokens, NodeExpression left public readonly Op Operation = operation; public readonly NodeExpression Right = right; - public enum Op { Add, @@ -645,6 +654,18 @@ public sealed class NodeExpressionBinary(List tokens, NodeExpression left } } +public sealed class NodeExpressionUnary(List 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 tokens) : Node(tokens); public sealed class NodeTypeVoid(List tokens) : NodeType(tokens); diff --git a/compiler/Compiler/test.nub b/compiler/Compiler/test.nub index fa400d3..8fe11bf 100644 --- a/compiler/Compiler/test.nub +++ b/compiler/Compiler/test.nub @@ -7,8 +7,9 @@ func main(): i32 { let x: i32 = 23 x = 24 - if true { + if !true { x = 49 + return x } else { x = 3 }