Skip to content

Commit b1f302b

Browse files
author
ThroughTheVoid
committed
clean lines/parser
1 parent ad0ea0d commit b1f302b

File tree

4 files changed

+106
-58
lines changed

4 files changed

+106
-58
lines changed

.gitignore

+4-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
/build
2-
test.cpu
3-
test.cpu.asm
42
cvm
53
vm/main.o
64
vm/readfile.o
75
*.pyc
6+
*.asm
7+
*.ins
8+
test
9+
envs

assembler/__main__.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,9 @@ def parse_args():
2222
default=None
2323
)
2424

25+
2526
args = parser.parse_args()
26-
27+
2728
return args
2829

2930
def __main__():

assembler/lib/parser.py

+95-54
Original file line numberDiff line numberDiff line change
@@ -2,59 +2,100 @@
22
from instruction import str_to_name
33
from instruction import str_to_value
44

5+
def error(msg):
6+
print("[!!] " + msg)
7+
58
class Parser():
6-
# Converts a string to and Instruction
7-
def line_to_instruction(self,s):
8-
9-
# Conversion
10-
11-
# remove endline
12-
words = s.replace("\n","")
13-
14-
# shave off comments
15-
if ";" in words:
16-
words = words.split(";")[0]
17-
18-
words = words.split(" ")
19-
20-
# ignore lines with less than 2 words
21-
if len(words)<2:
22-
return None
23-
24-
str_name = words[0].lower()
25-
str_value = words[1]
26-
num_name = str_to_name(str_name)
27-
num_value = str_to_value(str_value)
28-
29-
# Error Checking
30-
if num_name is None:
31-
error("Invalid Instruction " + str_name)
32-
return None
33-
34-
if num_value is None:
35-
error("Invalid Value " + str_value)
36-
return None
37-
38-
return Instruction(num_name,num_value)
39-
40-
# Converts a list of strings
41-
# to a list of Instructions
42-
def lines_to_assembly(self,lines):
43-
instr_list = []
44-
for line in lines:
45-
instr = self.line_to_instruction(line)
46-
if instr is not None:
47-
instr_list.append(instr)
48-
return instr_list
49-
50-
# Converts a list of instructions
51-
# to a list of bytes.
52-
def assembly_to_buffer(self,assembly):
53-
buf = []
54-
for instr in assembly:
55-
name = chr(instr.name)
56-
value = chr(instr.value)
57-
buf.append(name)
58-
buf.append(value)
59-
return buf
9+
# Converts a string to and Instruction
10+
def line_to_instruction(self,s):
11+
12+
# Conversion
13+
14+
# remove endline
15+
words = s.replace("\n","")
16+
17+
# shave off comments
18+
if ";" in words:
19+
words = words.split(";")[0]
20+
21+
words = words.split(" ")
22+
23+
# ignore lines with less than 2 words
24+
if len(words)<2:
25+
return None
26+
27+
str_name = words[0].lower()
28+
str_value = words[1]
29+
num_name = str_to_name(str_name)
30+
num_value = str_to_value(str_value)
31+
32+
# Error Checking
33+
if num_name is None:
34+
error("Invalid Instruction " + str_name)
35+
return None
36+
37+
if num_value is None:
38+
error("Invalid Value " + str_value)
39+
return None
40+
41+
return Instruction(num_name,num_value)
42+
43+
def split_comma(self,lines):
44+
result = []
45+
for line in lines:
46+
if ',' in line:
47+
split_line = line.split(",")
48+
for s in split_line:
49+
result.append(s)
50+
else:
51+
result.append(line)
52+
return result
53+
54+
def remove_trailing_spaces(self,lines):
55+
result = []
56+
for line in lines:
57+
while line[0]==" ":
58+
line = line[1:]
59+
result.append(line)
60+
return result
61+
62+
def clean_lines(self, lines):
63+
result = []
64+
for line in lines:
65+
if line[0]==";":
66+
continue
67+
68+
line = line.replace("\n","")
69+
line = line.replace(" ","")
70+
71+
if len(line)>1:
72+
result.append(line)
73+
return result
74+
75+
# Converts a list of strings
76+
# to a list of Instructions
77+
def lines_to_assembly(self,lines):
78+
instr_list = []
79+
print(lines)
80+
lines = self.split_comma(lines)
81+
lines = self.remove_trailing_spaces(lines)
82+
lines = self.clean_lines(lines)
83+
print(lines)
84+
for line in lines:
85+
instr = self.line_to_instruction(line)
86+
if instr is not None:
87+
instr_list.append(instr)
88+
89+
return instr_list
90+
91+
# Converts a list of instructions
92+
# to a list of bytes.
93+
def assembly_to_buffer(self,assembly):
94+
buf = []
95+
for instr in assembly:
96+
name = chr(instr.name)
97+
value = chr(instr.value)
98+
buf.append(name)
99+
buf.append(value)
100+
return buf
60101

vm/main.c

+5-1
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,9 @@ enum CODES{
1212
char PROGRAM[256];
1313
char MEMORY[256];
1414
char POINTER = 0;
15-
char INST = 0;
15+
char INST = 0;
1616
char RUNNING = 1;
17+
int CYCLES = 0;
1718

1819
// Maths
1920
void adda(char v) { MEMORY[POINTER] += MEMORY[v];}
@@ -55,6 +56,7 @@ void interpret(){
5556

5657
void run(){
5758
while(RUNNING){
59+
CYCLES = CYCLES + 1;
5860
interpret();
5961
}
6062
}
@@ -84,6 +86,8 @@ void dump(char height){
8486

8587
printf("MEMORY = \n");
8688

89+
printf("CYCLES = %d\n",CYCLES);
90+
8791
for(x=0;x<height;x++){
8892
for(y=0;y<16;y++){
8993
printf("%d ",MEMORY[y+(x*16)]);

0 commit comments

Comments
 (0)