From c544e32ee181b7d6dc68bd8f10b54ba98e9970f0 Mon Sep 17 00:00:00 2001 From: Trevor Maze Date: Wed, 28 Jan 2026 15:08:23 -0500 Subject: [PATCH] AST --- Sources/rxcc/rxcc.swift | 73 +++++++++++++++++--------------------- bin/bitwise.s | 4 +++ bin/bitwise_zero.s | 4 +++ bin/missing_const.s | 4 +++ bin/missing_semicolon.s | 4 +++ bin/neg.s | 4 +++ bin/nested_missing_const.s | 4 +++ bin/nested_ops.s | 4 +++ bin/nested_ops_2.s | 4 +++ bin/not_five.s | 4 +++ bin/not_zero.s | 4 +++ bin/wrong_order.s | 4 +++ 12 files changed, 77 insertions(+), 40 deletions(-) create mode 100644 bin/bitwise.s create mode 100644 bin/bitwise_zero.s create mode 100644 bin/missing_const.s create mode 100644 bin/missing_semicolon.s create mode 100644 bin/neg.s create mode 100644 bin/nested_missing_const.s create mode 100644 bin/nested_ops.s create mode 100644 bin/nested_ops_2.s create mode 100644 bin/not_five.s create mode 100644 bin/not_zero.s create mode 100644 bin/wrong_order.s diff --git a/Sources/rxcc/rxcc.swift b/Sources/rxcc/rxcc.swift index 525cdc8..2a20a38 100644 --- a/Sources/rxcc/rxcc.swift +++ b/Sources/rxcc/rxcc.swift @@ -25,18 +25,10 @@ struct Token { typealias Construct = [Element] enum Element { - case Construct(type: ConstructDefinitions); + case Construct(type: ConstructType) case Token(type: TokenType) } -enum ConstructType { - case UnaryOperation - case Expression - case Statement - case Function - case Program -} - enum ConstructVariant { // Unary operation variants case Negation @@ -107,14 +99,21 @@ class SyntaxTreeNode { } } +enum ConstructType { + case UnaryOperation + case Expression + case Statement + case Function + case Program +} + struct ConstructDefinitions { var type: ConstructType var variants: Dictionary } -let unaryOperation: ConstructDefinitions = ConstructDefinitions( - type: .UnaryOperation, - variants: [ +let constructDefinitions: Dictionary> = [ + .UnaryOperation: [ .Negation: [ .Token(type: .NEGATION), ], @@ -123,54 +122,51 @@ let unaryOperation: ConstructDefinitions = ConstructDefinitions( ], .LogicalNegation: [ .Token(type: .LOGICAL_NEGATION), - ] - ] -) + ], + ], -let expression: ConstructDefinitions = ConstructDefinitions( - type: .Expression, - variants: [ + .Expression: [ .LiteralInteger: [ .Token(type: .LITERAL_INTEGER) ], .UnaryOperation: [ - .Construct(type: unaryOperation), - .Construct(type: expression) + .Construct(type: .UnaryOperation), + .Construct(type: .Expression) ] - ] -) + ], -let statement: ConstructDefinitions = ConstructDefinitions( - type: .Statement, - variants: [ + .Statement: [ .ReturnInteger: [ .Token(type: .RETURN), - .Construct(type: expression), + .Construct(type: .Expression), .Token(type: .SEMICOLON) ] - ] -) + ], -let function: ConstructDefinitions = ConstructDefinitions( - type: .Function, - variants: [ + .Function: [ .Integer: [ .Token(type: .INT), .Token(type: .IDENTIFIER), .Token(type: .PARENTHESIS_OPEN), .Token(type: .PARENTHESIS_CLOSE), .Token(type: .BRACE_OPEN), - .Construct(type: statement), + .Construct(type: .Statement), .Token(type: .BRACE_CLOSE) ] + ], + + .Program: [ + .SingleFunction: [ + .Construct(type: .Function) + ] ] -) +] let program: ConstructDefinitions = ConstructDefinitions( type: .Function, variants: [ .SingleFunction: [ - .Construct(type: function) + .Construct(type: .Function) ] ] ) @@ -340,18 +336,15 @@ func validateConstruct(_ construct: Construct, tokens: inout [Token], node: Synt for element in construct { switch element { case .Construct(let type): - print("Begin validate subconstruct (type \"\(type.type)\")") + print("Begin validate subconstruct (type \"\(type)\")") var valid: Bool = false var validVariant: ConstructVariant = .Error let tokenBackup: [Token] = tokens - print("Before for key in type.variats.keys") - print(type) - print("After test") - for key in type.variants.keys { + for key in constructDefinitions[type]!.keys { let childNode = node.addChild(value: key) print("Testing variant \(key)") - if !validateConstruct(type.variants[key]!, tokens: &tokens, node: childNode) { + if !validateConstruct(constructDefinitions[type]![key]!, tokens: &tokens, node: childNode) { print("Fail") tokens = tokenBackup _ = node.popLastChild() diff --git a/bin/bitwise.s b/bin/bitwise.s new file mode 100644 index 0000000..976d781 --- /dev/null +++ b/bin/bitwise.s @@ -0,0 +1,4 @@ + .globl main +main: + movl $, %eax + ret diff --git a/bin/bitwise_zero.s b/bin/bitwise_zero.s new file mode 100644 index 0000000..976d781 --- /dev/null +++ b/bin/bitwise_zero.s @@ -0,0 +1,4 @@ + .globl main +main: + movl $, %eax + ret diff --git a/bin/missing_const.s b/bin/missing_const.s new file mode 100644 index 0000000..976d781 --- /dev/null +++ b/bin/missing_const.s @@ -0,0 +1,4 @@ + .globl main +main: + movl $, %eax + ret diff --git a/bin/missing_semicolon.s b/bin/missing_semicolon.s new file mode 100644 index 0000000..976d781 --- /dev/null +++ b/bin/missing_semicolon.s @@ -0,0 +1,4 @@ + .globl main +main: + movl $, %eax + ret diff --git a/bin/neg.s b/bin/neg.s new file mode 100644 index 0000000..976d781 --- /dev/null +++ b/bin/neg.s @@ -0,0 +1,4 @@ + .globl main +main: + movl $, %eax + ret diff --git a/bin/nested_missing_const.s b/bin/nested_missing_const.s new file mode 100644 index 0000000..976d781 --- /dev/null +++ b/bin/nested_missing_const.s @@ -0,0 +1,4 @@ + .globl main +main: + movl $, %eax + ret diff --git a/bin/nested_ops.s b/bin/nested_ops.s new file mode 100644 index 0000000..976d781 --- /dev/null +++ b/bin/nested_ops.s @@ -0,0 +1,4 @@ + .globl main +main: + movl $, %eax + ret diff --git a/bin/nested_ops_2.s b/bin/nested_ops_2.s new file mode 100644 index 0000000..976d781 --- /dev/null +++ b/bin/nested_ops_2.s @@ -0,0 +1,4 @@ + .globl main +main: + movl $, %eax + ret diff --git a/bin/not_five.s b/bin/not_five.s new file mode 100644 index 0000000..976d781 --- /dev/null +++ b/bin/not_five.s @@ -0,0 +1,4 @@ + .globl main +main: + movl $, %eax + ret diff --git a/bin/not_zero.s b/bin/not_zero.s new file mode 100644 index 0000000..976d781 --- /dev/null +++ b/bin/not_zero.s @@ -0,0 +1,4 @@ + .globl main +main: + movl $, %eax + ret diff --git a/bin/wrong_order.s b/bin/wrong_order.s new file mode 100644 index 0000000..a19f5ca --- /dev/null +++ b/bin/wrong_order.s @@ -0,0 +1,4 @@ + .globl main +main: + movl $4, %eax + ret