Permalink
Browse files

Moved parser into separate file, started building the regex framework…

… for parsing
  • Loading branch information...
1 parent 490e40f commit da8b185f867054ec5df40858663795b98fcdf3b6 @Mause committed Apr 20, 2012
Showing with 95 additions and 33 deletions.
  1. +34 −17 mausembler/assembler.py
  2. +1 −1 mausembler/custom_errors.py
  3. +57 −0 mausembler/sparser.py
  4. +3 −2 test.bat
  5. +0 −13 test.dasm
View
@@ -8,6 +8,7 @@
from mausembler.custom_errors import DuplicateLabelError
from mausembler.custom_errors import FileNonExistantError
from mausembler.custom_errors import FileExistsError
+from mausembler.sparser import Sparser
class Assembler():
@@ -25,10 +26,18 @@ def __init__(self): #, data=[]):
self.dependencies = []
self.dep_path=[]
self.labels = {}
+ self.data_done=[]
self.data=''
+ self.sparser=Sparser()
print "Mausembler; self titled!\n"
def determine_dependencies(self, data):
+ print "###########################Determining dependencies..."
+ if data not in self.data_done:
+ self.data_done.append(data)
+ else:
+ print 'Give me something new!'
+ return
for line in data:
if line[0] == '.':
if line.split()[0] == '.include':
@@ -38,13 +47,14 @@ def determine_dependencies(self, data):
self.load(dep, ''.join(dep.split('.')[:-1])+'bin')
#self.dep_path.append('\\'.join(input_filename.split('\\')[:-1]))
def load(self, input_filename='null.txt', output_filename='null.bin'):
- self.dep_path.append('\\'.join(input_filename.split('\\')[:-1]))
+ #self.dep_path.append('\\'.join(input_filename.split('\\')[:-1]))
+ self.dep_path.append('\\'.join(os.path.abspath(input_filename).split('\\')[:-1]))
print 'self.dep_path:', str(self.dep_path)
cur_input_filename = input_filename
- print 'manual:',os.getcwd()+input_filename
+
if os.path.exists(os.getcwd()+'\\'+input_filename):
input_filename = os.getcwd()+'\\'+input_filename
- while not os.path.exists(cur_input_filename):
+ while not os.path.exists(input_filename):
for x in range(len(self.dep_path)):
print 'Poss'
possibles=[(self.dep_path[x]+'\\'+cur_input_filename),
@@ -55,6 +65,7 @@ def load(self, input_filename='null.txt', output_filename='null.bin'):
print 'Poss:',poss
print os.path.exists(poss)
if os.path.exists(poss):
+ print 'hurruh!'
input_filename = poss
break
# if os.path.exists(self.dep_path[x]+'\\'+cur_input_filename.split('\\')[-1]):
@@ -66,14 +77,17 @@ def load(self, input_filename='null.txt', output_filename='null.bin'):
# if os.path.exists(os.getcwd()+'\\'+self.dep_path[x]+'\\'+cur_input_filename.split('\\')[-1]):
# input_filename = self.dep_path[x]+'\\'+cur_input_filename
# break
- print 'are you sure that file exists?\n\n'
- raise FileNonExistantError(input_filename)
+ if not os.path.exists(input_filename):
+ print 'are you sure that the specified file exists?\n\n'
+ raise FileNonExistantError(input_filename)
+ print 'i should be here'
if os.path.exists(cur_input_filename):
FH = open(input_filename, 'rb')
self.data = FH.readlines()
FH.close()
if os.path.exists(output_filename):
- cont = raw_input('Output file exists. Overwrite? ')
+ #cont = raw_input('Output file exists. Overwrite? ') # commenting this line out while testing
+ cont='yes'
if cont.lower() in ['yes', 'y']:
self.output_file = io.open(output_filename, 'wb')
else:
@@ -88,16 +102,17 @@ def load(self, input_filename='null.txt', output_filename='null.bin'):
self.determine_dependencies(self.data)
print '\nThe cpu will be told to;'
for self.line_number in range(len(self.data)):
- line = self.data[self.line_number]
- line = line.rstrip()
- line = line.replace(',', ' ')
- line = line.split()
- str(self.parse(line))
- self.output_file.close()
+ opcode = self.data[self.line_number]
+ opcode = opcode.rstrip()
+ opcode = opcode.replace(',', ' ')
+ opcode = opcode.split()
+ str(self.preparse(opcode, input_filename))
+ #self.output_file.close()
print '\nDependencies:', str(self.dependencies)
- print 'Labels:', str(self.labels)
+ print 'Labels:', [label for label in self.labels]
+ #self.output_file.close()
- def parse(self, opcode):
+ def preparse(self, opcode, input_filename):
if opcode != []:
opcode[0] = opcode[0].upper()
processed = 0
@@ -106,11 +121,13 @@ def parse(self, opcode):
print '* remember line', str(self.line_number),
print 'as label "' + opcode[0][1:] + '"'
self.labels[opcode[0][1:]] = self.line_number
- else:
- raise DuplicateLabelError(opcode[0][1:])
+ if self.line_number != self.labels[opcode[0][1:]]:
+ raise DuplicateLabelError([opcode[0][1:], input_filename,
+ self.labels, self.line_number])
if opcode[0] == 'SET':
print '* set memory location', opcode[1], 'to', opcode[2]
- self.output_file.write(str(self.ops[opcode[0]]))
+ self.sparser.parse(opcode)
+ #self.output_file.write(str(self.ops[opcode[0]]))
if opcode[0] == 'ADD':
print '* set', opcode[1], 'to', opcode[1], '+', opcode[2]
self.output_file.write(str(self.ops[opcode[0]]))
@@ -3,7 +3,7 @@ class DuplicateLabelError(Exception):
def __init__(self, value):
self.value = value
def __str__(self):
- return 'Two instances of the label "'+self.value+'" were found'
+ return 'Two instances of the label "'+self.value[0]+'" were found in the file '+self.value[1]+'\n* '+str(self.value[2][self.value[0]])+'\n* '+str(self.value[3])
class FileNonExistantError(Exception):
def __init__(self, value):
View
@@ -0,0 +1,57 @@
+import re
+
+#def s_ident(self, scanner, token): return token
+#def s_operator(self, scanner, token): return "op%s" % token
+#def s_float(self, scanner, token): return float(token)
+#def s_int(self, scanner, token): return int(token)
+
+
+
+class Sparser():
+ def __init__(self, ):
+ print 'Sparser; self-titled!'
+ def parse(self, opcode):
+ scanner = re.Scanner([
+ (r"[a-zA-Z_]\w*", self.s_set),
+ (r"\d+\.\d*", self.s_add),
+ (r"\d+", self.s_sub),
+ (r"=|\+|-|\*|/", self.s_mul),
+ (r"\s+", self.s_div),
+ ])
+
+ #print scanner.scan("sum = 3*foo + 312.50 + bar")
+ print 'Scanned:', scanner.scan(' '.join(opcode))
+
+###########################################################
+## here follows individual code for the different opcodes #
+###########################################################
+ def s_set(self, scanner, token):
+ "0x1"
+ def s_add(self, scanner, token):
+ "0x2"
+ def s_sub(self, scanner, token):
+ "0x3"
+ def s_mul(self, scanner, token):
+ "0x4"
+ def s_div(self, scanner, token):
+ "0x5"
+ def s_mod(self, scanner, token):
+ "0x6"
+ def s_shl(self, scanner, token):
+ "0x7"
+ def s_shr(self, scanner, token):
+ "0x8"
+ def s_and(self, scanner, token):
+ "0x9"
+ def s_bor(self, scanner, token):
+ "0xa"
+ def s_xor(self, scanner, token):
+ "0xb"
+ def s_ife(self, scanner, token):
+ "0xc"
+ def s_ifn(self, scanner, token):
+ "0xd"
+ def s_ifg(self, scanner, token):
+ "0xe"
+ def s_ivb(self, scanner, token):
+ "0xf"
View
@@ -1,5 +1,6 @@
+@echo off
cls
-rem python Mausembler.py test.dasm test.out
+python Mausembler.py test.dasm test.out
rem python Mausembler.py ilog.dasm ilog.bin
-python Mausembler.py src\0x42c.dasm16 0x42c.bin
+rem python Mausembler.py src\0x42c.dasm16 0x42c.bin
View
@@ -1,16 +1,3 @@
set a, 1
set b, 2
set c, 3
-
-add a, b
-add a,c
-
-:hello
-:world
-
-
-
-:label
- SET A, 10
- SET B, 0x20
- ADD A, B

0 comments on commit da8b185

Please sign in to comment.