AST
This commit is contained in:
@@ -25,18 +25,10 @@ struct Token {
|
|||||||
typealias Construct = [Element]
|
typealias Construct = [Element]
|
||||||
|
|
||||||
enum Element {
|
enum Element {
|
||||||
case Construct(type: ConstructDefinitions);
|
case Construct(type: ConstructType)
|
||||||
case Token(type: TokenType)
|
case Token(type: TokenType)
|
||||||
}
|
}
|
||||||
|
|
||||||
enum ConstructType {
|
|
||||||
case UnaryOperation
|
|
||||||
case Expression
|
|
||||||
case Statement
|
|
||||||
case Function
|
|
||||||
case Program
|
|
||||||
}
|
|
||||||
|
|
||||||
enum ConstructVariant {
|
enum ConstructVariant {
|
||||||
// Unary operation variants
|
// Unary operation variants
|
||||||
case Negation
|
case Negation
|
||||||
@@ -107,14 +99,21 @@ class SyntaxTreeNode {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
enum ConstructType {
|
||||||
|
case UnaryOperation
|
||||||
|
case Expression
|
||||||
|
case Statement
|
||||||
|
case Function
|
||||||
|
case Program
|
||||||
|
}
|
||||||
|
|
||||||
struct ConstructDefinitions {
|
struct ConstructDefinitions {
|
||||||
var type: ConstructType
|
var type: ConstructType
|
||||||
var variants: Dictionary<ConstructVariant, Construct>
|
var variants: Dictionary<ConstructVariant, Construct>
|
||||||
}
|
}
|
||||||
|
|
||||||
let unaryOperation: ConstructDefinitions = ConstructDefinitions(
|
let constructDefinitions: Dictionary<ConstructType, Dictionary<ConstructVariant, Construct>> = [
|
||||||
type: .UnaryOperation,
|
.UnaryOperation: [
|
||||||
variants: [
|
|
||||||
.Negation: [
|
.Negation: [
|
||||||
.Token(type: .NEGATION),
|
.Token(type: .NEGATION),
|
||||||
],
|
],
|
||||||
@@ -123,54 +122,51 @@ let unaryOperation: ConstructDefinitions = ConstructDefinitions(
|
|||||||
],
|
],
|
||||||
.LogicalNegation: [
|
.LogicalNegation: [
|
||||||
.Token(type: .LOGICAL_NEGATION),
|
.Token(type: .LOGICAL_NEGATION),
|
||||||
]
|
],
|
||||||
]
|
],
|
||||||
)
|
|
||||||
|
|
||||||
let expression: ConstructDefinitions = ConstructDefinitions(
|
.Expression: [
|
||||||
type: .Expression,
|
|
||||||
variants: [
|
|
||||||
.LiteralInteger: [
|
.LiteralInteger: [
|
||||||
.Token(type: .LITERAL_INTEGER)
|
.Token(type: .LITERAL_INTEGER)
|
||||||
],
|
],
|
||||||
.UnaryOperation: [
|
.UnaryOperation: [
|
||||||
.Construct(type: unaryOperation),
|
.Construct(type: .UnaryOperation),
|
||||||
.Construct(type: expression)
|
.Construct(type: .Expression)
|
||||||
]
|
]
|
||||||
]
|
],
|
||||||
)
|
|
||||||
|
|
||||||
let statement: ConstructDefinitions = ConstructDefinitions(
|
.Statement: [
|
||||||
type: .Statement,
|
|
||||||
variants: [
|
|
||||||
.ReturnInteger: [
|
.ReturnInteger: [
|
||||||
.Token(type: .RETURN),
|
.Token(type: .RETURN),
|
||||||
.Construct(type: expression),
|
.Construct(type: .Expression),
|
||||||
.Token(type: .SEMICOLON)
|
.Token(type: .SEMICOLON)
|
||||||
]
|
]
|
||||||
]
|
],
|
||||||
)
|
|
||||||
|
|
||||||
let function: ConstructDefinitions = ConstructDefinitions(
|
.Function: [
|
||||||
type: .Function,
|
|
||||||
variants: [
|
|
||||||
.Integer: [
|
.Integer: [
|
||||||
.Token(type: .INT),
|
.Token(type: .INT),
|
||||||
.Token(type: .IDENTIFIER),
|
.Token(type: .IDENTIFIER),
|
||||||
.Token(type: .PARENTHESIS_OPEN),
|
.Token(type: .PARENTHESIS_OPEN),
|
||||||
.Token(type: .PARENTHESIS_CLOSE),
|
.Token(type: .PARENTHESIS_CLOSE),
|
||||||
.Token(type: .BRACE_OPEN),
|
.Token(type: .BRACE_OPEN),
|
||||||
.Construct(type: statement),
|
.Construct(type: .Statement),
|
||||||
.Token(type: .BRACE_CLOSE)
|
.Token(type: .BRACE_CLOSE)
|
||||||
]
|
]
|
||||||
|
],
|
||||||
|
|
||||||
|
.Program: [
|
||||||
|
.SingleFunction: [
|
||||||
|
.Construct(type: .Function)
|
||||||
|
]
|
||||||
]
|
]
|
||||||
)
|
]
|
||||||
|
|
||||||
let program: ConstructDefinitions = ConstructDefinitions(
|
let program: ConstructDefinitions = ConstructDefinitions(
|
||||||
type: .Function,
|
type: .Function,
|
||||||
variants: [
|
variants: [
|
||||||
.SingleFunction: [
|
.SingleFunction: [
|
||||||
.Construct(type: function)
|
.Construct(type: .Function)
|
||||||
]
|
]
|
||||||
]
|
]
|
||||||
)
|
)
|
||||||
@@ -340,18 +336,15 @@ func validateConstruct(_ construct: Construct, tokens: inout [Token], node: Synt
|
|||||||
for element in construct {
|
for element in construct {
|
||||||
switch element {
|
switch element {
|
||||||
case .Construct(let type):
|
case .Construct(let type):
|
||||||
print("Begin validate subconstruct (type \"\(type.type)\")")
|
print("Begin validate subconstruct (type \"\(type)\")")
|
||||||
|
|
||||||
var valid: Bool = false
|
var valid: Bool = false
|
||||||
var validVariant: ConstructVariant = .Error
|
var validVariant: ConstructVariant = .Error
|
||||||
let tokenBackup: [Token] = tokens
|
let tokenBackup: [Token] = tokens
|
||||||
print("Before for key in type.variats.keys")
|
for key in constructDefinitions[type]!.keys {
|
||||||
print(type)
|
|
||||||
print("After test")
|
|
||||||
for key in type.variants.keys {
|
|
||||||
let childNode = node.addChild(value: key)
|
let childNode = node.addChild(value: key)
|
||||||
print("Testing variant \(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")
|
print("Fail")
|
||||||
tokens = tokenBackup
|
tokens = tokenBackup
|
||||||
_ = node.popLastChild()
|
_ = 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