-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparser.lua
142 lines (111 loc) · 2.67 KB
/
parser.lua
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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
--[[
Parse .stp8 files
]]
local parser = {}
-- parse a file as string
function parser.parse(file)
-- group
local groups = {}
local group = ""
local commentBypass = false
for i = 1, #file do
-- char
local char = string.sub(file, i, i)
-- ignore comments
if char == "#" then
commentBypass = true
elseif commentBypass == true
and char == "\n"
then
commentBypass = false
end
-- grouping
if not commentBypass then
if char == " "
or char == "\t"
or char == "\n"
then
-- push group and reset
if group ~= "" then
table.insert(groups, group)
group = ""
end
else
-- add char to group
group = group .. char
end
end
end
print("-- Groups:")
for i = 1, #groups do
print(groups[i])
end
print("")
-- tokenise
local tokens = {}
for i = 1, #groups do
-- string
local string = groups[i]
local firstChar = string.sub(string, 1, 1)
local token = {}
-- get token
if firstChar == ">" then
-- is address specifier
token.flavour = "address specifier"
token.literal = utils.newHex(string.sub(string, 3, -1))
elseif firstChar == "%" then
-- is binary number
token.flavour = "number bin"
token.literal = utils.newByte(string.sub(string, 2, -1))
elseif firstChar == "$" then
-- is hex number
token.flavour = "number hex"
token.literal = utils.newHex(string.sub(string, 2, -1))
else
-- is instruction
token.flavour = "instruction"
token.literal = string
end
-- push token
table.insert(tokens, token)
end
print("-- Tokens:")
for i = 1, #tokens do
print(tokens[i].flavour, tokens[i].literal)
end
print("")
-- load into memory
print("-- Loading into memory:")
local memoryIndex = 1152 -- start at $480
for i = 1, #tokens do
-- get token
local token = tokens[i]
-- put into memory
if token.flavour == "address specifier" then
-- accress specification
memoryIndex = utils.hexToNum(token.literal)
print("new memory index: " .. memoryIndex)
elseif token.flavour == "number bin" then
-- binary number
memory[memoryIndex] = token.literal
memoryIndex = memoryIndex + 1
print(memoryIndex, "bin")
elseif token.flavour == "number hex" then
-- hex number
local low, high = utils.numToHex(utils.hexToNum(token.literal))
memory[memoryIndex] = low
memory[memoryIndex + 1] = high
memoryIndex = memoryIndex + 2
print(memoryIndex, "hex")
elseif token.flavour == "instruction" then
-- instruction
memory[memoryIndex] = utils.instructionToByte(token.literal)
memoryIndex = memoryIndex + 1
print(memoryIndex, "instruction: " .. token.literal)
end
end
print("")
-- done!
end
-- return
return parser