-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathAssemblyParser.jl
99 lines (78 loc) · 2.79 KB
/
AssemblyParser.jl
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
#= This is the assembly language parser
Created by: Kino Roy for CMPT 276, Assignment 1. =#
module AssemblyParser
function Parse(arguments)
#=-----------------------------------------
Initializing data structures for holding data
------------------------------------------=#
instructions = Array{AbstractString}(2048,4)
labelDict = Dict() #Creates the dictionary for labels
global pathToInstructions = "instructions.txt"
if(size(arguments)[1]>0)
pathToInstructions = arguments[1]
end
fill!(instructions, "")
lines = readFile()
counter = 1 #Start the counter
for l in lines
if(l[1] == ";") #Allows starting the file with comment only lines
continue
end
l = split(l,r";")[1] #eliminate the comments (garbage)
#Determines type of arguments in the line
splitLine = split(l,r":| |,",keep=false) #Filters text
for i in 1:length(splitLine) #Brings all instructions to uppercase
splitLine[i] = uppercase(splitLine[i])
end
#---- Begin Parsing ----#
#CASE: "INST"
if size(splitLine)[1] == 1
instructions[counter,2] = chomp(splitLine[1])
#CASE: "LABEL + INST"
elseif size(splitLine)[1] == 2 && contains(l,":")
instructions[counter,1] = splitLine[1]
instructions[counter,2] = chomp(splitLine[2])
#ADDS LABEL TO DICTIONARY
labelDict["$(splitLine[1])"] = counter
#CASE: "INST + OP1"
elseif size(splitLine)[1] == 2
instructions[counter,2] = splitLine[1]
instructions[counter,3] = chomp(splitLine[2])
#ADDS LABEL TO DICTIONARY
labelDict["$(splitLine[1])"] = counter
#CASE: "LABEL + INST + OP1"
elseif size(splitLine)[1] == 3 && contains(l,":")
instructions[counter,1] = splitLine[1]
instructions[counter,2] = splitLine[2]
instructions[counter,3] = chomp(splitLine[3])
#ADDS LABEL TO DICTIONARY
labelDict["$(splitLine[1])"] = counter
#CASE: "INST + OP1 + OP2"
elseif size(splitLine)[1] == 3 && contains(l,",")
instructions[counter,2] = splitLine[1]
instructions[counter,3] = splitLine[2]
instructions[counter,4] = chomp(splitLine[3])
#CASE: "LABEL + INST + OP1 + OP2"
elseif size(splitLine)[1] == 4
instructions[counter,1] = splitLine[1]
instructions[counter,2] = splitLine[2]
instructions[counter,3] = splitLine[3]
instructions[counter,4] = chomp(splitLine[4])
#ADDS LABEL TO DICTIONARY
labelDict["$(splitLine[1])"] = counter
end
counter += 1
end #endfor
return instructions,labelDict
end
function readFile()
global pathToInstructions
stream = open(pathToInstructions) #Open the input file and set the input stream
lines = readlines(stream) # Create an array with each element, an instruction
close(stream)
return lines
end
end
#=------------
END ASSEMBLY PARSER
-------------=#