AST
This commit is contained in:
@@ -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<ConstructVariant, Construct>
|
||||
}
|
||||
|
||||
let unaryOperation: ConstructDefinitions = ConstructDefinitions(
|
||||
type: .UnaryOperation,
|
||||
variants: [
|
||||
let constructDefinitions: Dictionary<ConstructType, Dictionary<ConstructVariant, Construct>> = [
|
||||
.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()
|
||||
|
||||
4
bin/bitwise.s
Normal file
4
bin/bitwise.s
Normal file
@@ -0,0 +1,4 @@
|
||||
.globl main
|
||||
main:
|
||||
movl $, %eax
|
||||
ret
|
||||
4
bin/bitwise_zero.s
Normal file
4
bin/bitwise_zero.s
Normal file
@@ -0,0 +1,4 @@
|
||||
.globl main
|
||||
main:
|
||||
movl $, %eax
|
||||
ret
|
||||
4
bin/missing_const.s
Normal file
4
bin/missing_const.s
Normal file
@@ -0,0 +1,4 @@
|
||||
.globl main
|
||||
main:
|
||||
movl $, %eax
|
||||
ret
|
||||
4
bin/missing_semicolon.s
Normal file
4
bin/missing_semicolon.s
Normal file
@@ -0,0 +1,4 @@
|
||||
.globl main
|
||||
main:
|
||||
movl $, %eax
|
||||
ret
|
||||
4
bin/nested_missing_const.s
Normal file
4
bin/nested_missing_const.s
Normal file
@@ -0,0 +1,4 @@
|
||||
.globl main
|
||||
main:
|
||||
movl $, %eax
|
||||
ret
|
||||
4
bin/nested_ops.s
Normal file
4
bin/nested_ops.s
Normal file
@@ -0,0 +1,4 @@
|
||||
.globl main
|
||||
main:
|
||||
movl $, %eax
|
||||
ret
|
||||
4
bin/nested_ops_2.s
Normal file
4
bin/nested_ops_2.s
Normal file
@@ -0,0 +1,4 @@
|
||||
.globl main
|
||||
main:
|
||||
movl $, %eax
|
||||
ret
|
||||
4
bin/not_five.s
Normal file
4
bin/not_five.s
Normal file
@@ -0,0 +1,4 @@
|
||||
.globl main
|
||||
main:
|
||||
movl $, %eax
|
||||
ret
|
||||
4
bin/not_zero.s
Normal file
4
bin/not_zero.s
Normal file
@@ -0,0 +1,4 @@
|
||||
.globl main
|
||||
main:
|
||||
movl $, %eax
|
||||
ret
|
||||
4
bin/wrong_order.s
Normal file
4
bin/wrong_order.s
Normal file
@@ -0,0 +1,4 @@
|
||||
.globl main
|
||||
main:
|
||||
movl $4, %eax
|
||||
ret
|
||||
Reference in New Issue
Block a user