Unary Operations

This commit is contained in:
Trevor Maze
2026-01-29 09:56:27 -05:00
parent e1548a9ff0
commit ebd466119e
2 changed files with 34 additions and 1 deletions

View File

@@ -5,6 +5,9 @@ import PackageDescription
let package = Package( let package = Package(
name: "rxcc", name: "rxcc",
platforms: [
.macOS(.v13)
],
targets: [ targets: [
// Targets are the basic building blocks of a package, defining a module or a test suite. // Targets are the basic building blocks of a package, defining a module or a test suite.
// Targets can depend on other targets in this package and products from dependencies. // Targets can depend on other targets in this package and products from dependencies.

View File

@@ -234,7 +234,7 @@ func getTestFiles() -> [TestFile] {
var testFiles: [TestFile] = [TestFile]() var testFiles: [TestFile] = [TestFile]()
let fileManager = FileManager.default let fileManager = FileManager.default
let path = "c/tests/stage_1" let path = "c/tests/stage_2"
do { do {
let validItems = try fileManager.contentsOfDirectory(atPath: path + "/valid") let validItems = try fileManager.contentsOfDirectory(atPath: path + "/valid")
@@ -358,6 +358,36 @@ func generateOutput(_ node: SyntaxTreeNode) -> String {
text += " movl $\(returnValue), %eax\n ret\n" text += " movl $\(returnValue), %eax\n ret\n"
break break
case .UnaryOperation:
if node.children.count != 2 {
print("\(node.variant) must have two child nodes")
}
let operation: SyntaxTreeNode = node.children[0]
let expression: SyntaxTreeNode = node.children[1]
switch operation.variant {
case .BitwiseCompliment:
text += String(~Int(generateOutput(expression))!)
break
case .Negation:
text += String(-Int(generateOutput(expression))!)
break
case .LogicalNegation:
text += String(Int(generateOutput(expression))! == 0 ? "1" : "0")
break
default:
print("\(node.variant): Unknown unary operation \"\(operation.variant)\"")
}
break
case .Negation, .BitwiseCompliment, .LogicalNegation:
text += node.value
break
case .LiteralInteger: case .LiteralInteger:
text += node.value text += node.value
break break