-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathUI.swift
73 lines (73 loc) · 2.94 KB
/
UI.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
import Foundation
class UI {
let helpStr = "Welcome to SAP-2.0!\nCommands:\nasm </path/to/file> NO .txt: assemble a program\nrun <file.bin>: run a program\nprintlst </path/to/file.txt>: print listing file for program\nprintbin </path/to/file.txt>: print binary file for program\nprintsym </path/to/file.txt>: print symbol file for program\nhelp: print this help text\nquit: quit SAP-2.0"
static func ssip(_ expression: String) -> [String]{ //split String into parts
return expression.characters.split{$0 == " " }.map{String($0)}
}
func wtf(_ path: String, _ data: String) -> String? {
let url = NSURL.fileURL(withPathComponents: [path])
do {
try data.write(to: url!, atomically: true, encoding: String.Encoding.utf8)
} catch let error as NSError {
return "Failed writing to URL: \(url), Error: " + error.localizedDescription
}
catch {
return "Failed writing to URL for an unknown reason."
}
return nil
}
func run() {
var command = ""
var commandArray = [String]()
print(helpStr)
while command != "quit" {
print("SAP> ", terminator: "")
command = readLine()!
commandArray = UI.ssip(command)
if command == "help" {
print(helpStr)
}
if commandArray[0] == "asm" {
let asm = Assembler(program: "\(commandArray[1]).txt")
let assembled = asm.assemble()
if asm.numberErrors > 0 {
print("\(asm.numberErrors) errors encountered in program, see listing file for details")
}
else {
var binary = ""
for value in assembled {
binary += String(describing: value!)
binary += "\n"
}
let temp = commandArray[1]
_ = wtf("\(temp).bin", binary)
var s = ""
for (key,value) in asm.symbolTable{
s += "\(key) \(value)\n"
}
_ = wtf("\(temp).sym", String(describing: s))
}
}
if commandArray[0] == "run" {
let vm = VM(fileName: commandArray[1])
vm.run()
}
if commandArray[0] == "printbin" {
let asm = Assembler(program: "\(commandArray[1]).txt")
let assembled = asm.assemble()
for i in assembled {
print(i!)
}
}
if commandArray[0] == "printsym" {
let asm = Assembler(program: "\(commandArray[1]).txt")
let assembled = asm.assemble()
var s = ""
for (key,value) in asm.symbolTable {
s += "\(key)\t\(value)\n"
}
print(s)
}
}
}
}