|
9 | 9 | import Foundation |
10 | 10 |
|
11 | 11 | public class Interpreter { |
12 | | - private var integerMemory: [String: Int] = [:] |
13 | | - private var realMemory: [String: Double] = [:] |
| 12 | + private var callStack = Stack<Frame>() |
14 | 13 | private let tree: AST |
| 14 | + private let scopes: [String: ScopedSymbolTable] |
15 | 15 |
|
16 | 16 | public init(_ text: String) { |
17 | 17 | let parser = Parser(text) |
18 | 18 | tree = parser.parse() |
19 | 19 | let semanticAnalyzer = SemanticAnalyzer() |
20 | | - semanticAnalyzer.analyze(node: tree) |
| 20 | + scopes = semanticAnalyzer.analyze(node: tree) |
21 | 21 | } |
22 | 22 |
|
23 | | - @discardableResult private func eval(_ node: AST) -> Number? { |
| 23 | + @discardableResult private func eval(node: AST) -> Number? { |
24 | 24 | switch node { |
25 | | - case let .number(value): |
26 | | - return value |
27 | | - case let .unaryOperation(operation: operation, child: child): |
28 | | - guard let result = eval(child) else { |
29 | | - fatalError("Cannot use unary \(operation) on non number") |
30 | | - } |
31 | | - switch operation { |
32 | | - case .plus: |
33 | | - return +result |
34 | | - case .minus: |
35 | | - return -result |
36 | | - } |
37 | | - case let .binaryOperation(left: left, operation: operation, right: right): |
38 | | - guard let leftResult = eval(left), let rightResult = eval(right) else { |
39 | | - fatalError("Cannot use binary \(operation) on non numbers") |
40 | | - } |
41 | | - switch operation { |
42 | | - case .plus: |
43 | | - return leftResult + rightResult |
44 | | - case .minus: |
45 | | - return leftResult - rightResult |
46 | | - case .mult: |
47 | | - return leftResult * rightResult |
48 | | - case .integerDiv: |
49 | | - return leftResult ‖ rightResult |
50 | | - case .floatDiv: |
51 | | - return leftResult / rightResult |
52 | | - } |
53 | | - case let .compound(children): |
54 | | - for chid in children { |
55 | | - eval(chid) |
56 | | - } |
| 25 | + case let number as Number: |
| 26 | + return eval(number: number) |
| 27 | + case let unaryOperation as UnaryOperation: |
| 28 | + return eval(unaryOperation: unaryOperation) |
| 29 | + case let binaryOperation as BinaryOperation: |
| 30 | + return eval(binaryOperation: binaryOperation) |
| 31 | + case let compound as Compound: |
| 32 | + return eval(compound: compound) |
| 33 | + case let assignment as Assignment: |
| 34 | + return eval(assignment: assignment) |
| 35 | + case let variable as Variable: |
| 36 | + return eval(variable: variable) |
| 37 | + case let block as Block: |
| 38 | + return eval(block: block) |
| 39 | + case let program as Program: |
| 40 | + return eval(program: program) |
| 41 | + case let call as ProcedureCall: |
| 42 | + return eval(call: call) |
| 43 | + default: |
57 | 44 | return nil |
58 | | - case let .assignment(left, right): |
59 | | - guard case let .variable(name) = left else { |
60 | | - fatalError("Assignment left side is not a variable, check Parser implementation") |
61 | | - } |
| 45 | + } |
| 46 | + } |
62 | 47 |
|
63 | | - if integerMemory.keys.contains(name) { |
64 | | - switch eval(right)! { |
65 | | - case let .integer(value): |
66 | | - integerMemory[name] = value |
67 | | - return nil |
68 | | - case .real: |
69 | | - fatalError("Cannot assign Real value to Int variable \(name)") |
70 | | - } |
71 | | - } |
| 48 | + func eval(number: Number) -> Number? { |
| 49 | + return number |
| 50 | + } |
72 | 51 |
|
73 | | - if realMemory.keys.contains(name) { |
74 | | - switch eval(right)! { |
75 | | - case let .integer(value): |
76 | | - realMemory[name] = Double(value) |
77 | | - return nil |
78 | | - case let .real(value): |
79 | | - realMemory[name] = value |
80 | | - return nil |
81 | | - } |
82 | | - } |
| 52 | + func eval(unaryOperation: UnaryOperation) -> Number? { |
| 53 | + guard let result = eval(node: unaryOperation.operand) else { |
| 54 | + fatalError("Cannot use unary \(unaryOperation.operation) on non number") |
| 55 | + } |
83 | 56 |
|
84 | | - fatalError("Variable \(name) not found, check the SemanticAnalyzer implementation") |
85 | | - case let .variable(name): |
86 | | - if let value = integerMemory[name] { |
87 | | - return .integer(value) |
88 | | - } |
89 | | - if let value = realMemory[name] { |
90 | | - return .real(value) |
91 | | - } |
92 | | - fatalError("Variable \(name) not found, check the SemanticAnalyzer implementation") |
93 | | - case .noOp: |
94 | | - return nil |
95 | | - case let .block(declarations, compound): |
96 | | - for declaration in declarations { |
97 | | - eval(declaration) |
98 | | - } |
99 | | - return eval(compound) |
100 | | - case let .variableDeclaration(name: name, type: type): |
101 | | - guard case let .type(type) = type, case let .variable(name: name) = name else { |
102 | | - fatalError("Invalid variable declaration, check Parser implementation") |
103 | | - } |
104 | | - switch type { |
105 | | - case .integer: |
106 | | - integerMemory[name] = 0 |
107 | | - case .real: |
108 | | - realMemory[name] = 0 |
| 57 | + switch unaryOperation.operation { |
| 58 | + case .plus: |
| 59 | + return +result |
| 60 | + case .minus: |
| 61 | + return -result |
| 62 | + } |
| 63 | + } |
| 64 | + func eval(binaryOperation: BinaryOperation) -> Number? { |
| 65 | + guard let leftResult = eval(node: binaryOperation.left), let rightResult = eval(node: binaryOperation.right) else { |
| 66 | + fatalError("Cannot use binary \(binaryOperation.operation) on non numbers") |
| 67 | + } |
| 68 | + |
| 69 | + switch binaryOperation.operation { |
| 70 | + case .plus: |
| 71 | + return leftResult + rightResult |
| 72 | + case .minus: |
| 73 | + return leftResult - rightResult |
| 74 | + case .mult: |
| 75 | + return leftResult * rightResult |
| 76 | + case .integerDiv: |
| 77 | + return leftResult ‖ rightResult |
| 78 | + case .floatDiv: |
| 79 | + return leftResult / rightResult |
| 80 | + } |
| 81 | + } |
| 82 | + |
| 83 | + func eval(compound: Compound) -> Number? { |
| 84 | + for child in compound.children { |
| 85 | + eval(node: child) |
| 86 | + } |
| 87 | + return nil |
| 88 | + } |
| 89 | + |
| 90 | + func eval(assignment: Assignment) -> Number? { |
| 91 | + guard let currentFrame = callStack.peek() else { |
| 92 | + fatalError("No call stack frame") |
| 93 | + } |
| 94 | + |
| 95 | + currentFrame.set(variable: assignment.left.name, value: eval(node: assignment.right)!) |
| 96 | + return nil |
| 97 | + } |
| 98 | + |
| 99 | + func eval(variable: Variable) -> Number? { |
| 100 | + guard let currentFrame = callStack.peek() else { |
| 101 | + fatalError("No call stack frame") |
| 102 | + } |
| 103 | + |
| 104 | + return currentFrame.get(variable: variable.name) |
| 105 | + } |
| 106 | + |
| 107 | + func eval(block: Block) -> Number? { |
| 108 | + for declaration in block.declarations { |
| 109 | + eval(node: declaration) |
| 110 | + } |
| 111 | + |
| 112 | + return eval(node: block.compound) |
| 113 | + } |
| 114 | + |
| 115 | + func eval(program: Program) -> Number? { |
| 116 | + let frame = Frame(scope: scopes["global"]!, previousFrame: nil) |
| 117 | + callStack.push(frame) |
| 118 | + return eval(node: program.block) |
| 119 | + } |
| 120 | + |
| 121 | + func eval(call: ProcedureCall) -> Number? { |
| 122 | + let current = callStack.peek()! |
| 123 | + let frame = Frame(scope: scopes[call.name]!, previousFrame: current) |
| 124 | + callStack.push(frame) |
| 125 | + callProcedure(procedure: call.name, params: call.actualParameters, frame: frame) |
| 126 | + callStack.pop() |
| 127 | + return nil |
| 128 | + } |
| 129 | + |
| 130 | + private func callProcedure(procedure: String, params: [Number], frame: Frame) { |
| 131 | + guard let symbol = frame.scope.lookup(procedure), let procedureSymbol = symbol as? ProcedureSymbol else { |
| 132 | + fatalError("Symbol(procedure) not found '\(procedure)'") |
| 133 | + } |
| 134 | + |
| 135 | + if procedureSymbol.params.count > 0 { |
| 136 | + |
| 137 | + for i in 0 ... procedureSymbol.params.count - 1 { |
| 138 | + frame.set(variable: procedureSymbol.params[i].name, value: params[i]) |
109 | 139 | } |
110 | | - return nil |
111 | | - case .type: |
112 | | - return nil |
113 | | - case let .program(_, block): |
114 | | - return eval(block) |
115 | | - case .procedure: |
116 | | - return nil |
117 | | - case .param: |
118 | | - return nil |
119 | 140 | } |
| 141 | + |
| 142 | + eval(node: procedureSymbol.body.block) |
120 | 143 | } |
121 | 144 |
|
122 | 145 | public func interpret() { |
123 | | - eval(tree) |
| 146 | + eval(node: tree) |
124 | 147 | } |
125 | 148 |
|
126 | 149 | func getState() -> ([String: Int], [String: Double]) { |
127 | | - return (integerMemory, realMemory) |
| 150 | + return (callStack.peek()!.integerMemory, callStack.peek()!.realMemory) |
128 | 151 | } |
129 | 152 |
|
130 | 153 | public func printState() { |
131 | | - print("Final interpreter memory state:") |
132 | | - print("Int: \(integerMemory)") |
133 | | - print("Real: \(realMemory)") |
| 154 | + print("Final interpreter memory state (\(callStack.peek()!.scope.name)):") |
| 155 | + print("Int: \(callStack.peek()!.integerMemory)") |
| 156 | + print("Real: \(callStack.peek()!.realMemory)") |
134 | 157 | } |
135 | 158 | } |
0 commit comments