def assem_file(f_name, called_from = [], pass_token_stream = None, pass_err = None): logging.debug("Assem_file:%s" % (f_name)) # Note these don't have to be set in the recursion but are set the first time if (pass_token_stream): global token_stream token_stream = pass_token_stream if (pass_err): global err err = pass_err hl_parser.set_err_reporter(err) err.set_context(2, "reading token file: %s" % (f_name)) # open file if (not os.path.isfile(f_name) or not os.access(f_name, os.R_OK)): print "ERROR - file %s doesn't exist or isn't readable" if (called_from): print " Insert history: ", output = "" for f, l in called_from: if (output): output += ", " output += "%s:%d" % (f, l) print output return False fh = file(f_name, 'rt') # get each line but insert a file if find 'INSERT TOKENS' line_num = 1 for line in fh: # if 'INPUT TOKENS fname' push existing file insert_check = line.split() if ((len(insert_check) >= 3) and (insert_check[0] == 'INSERT') and (insert_check[1].lower() == 'tokens')): called_from.append((f_name, line_num)) assem_file(insert_check[2], called_from) called_from.pop() else: assem_line(line) line_num += 1 return True
def test(): global token_stream global err test_simp = ["incb %acc", "movb 12,@lil_count", "decw 1001/2", " # a comment line", " addb $12 #do some adding", "movb $'a', %acc", "branch :label1", "decw", "mulw 44", "subb $1000", "subw $-3000", "movb $0, @big_count", "movw @last_reading, @store_reading"] test_jump = ["brne :f1", "bra $-1", "ret", ":f1", "dbnz :f2", "dsnz $10", "suba $10", ":f2"] test_stack = ["pushb $32", "pushw @count", "popb %acc", "popw 0x39", "pushw 43"] test_data = ["DATA home, 0", "DATB *, 0, 20, 'a'", ":start", "DATW intensity, *, *, -20", 'DATA buffer, *, 32, "This is an \"interesting\" string"'] test_spec = ["LIMITS -1, -1, -1, 0, 0", "LIMITS 30, 10, 64, 0, 200", "RESERVA 0, 16", "RESERVB 31,1", "RESERVW 0, 1"] test_device = ["DEVICE bad, 1", "DEVICE motor, 0", "DEVICE motor 13", "DEVICE motor 1 **", "DEVICE digin, 1", "DEVICE motor 2, left_wheel", "DEVICE motor 4 right_wheel", "DEVICE digout, 5", "DEVICE analogin 6"] test_begin_end = ["BEGIN WTF", "BEGIN PROGRAM 1", "BEGIN CONFIG", "BEGIN EVENT 34, 43,44", "END PROGRAM CONFIG 0 0", "BEGIN CONFIG 0,0", "BEGIN PROGRAM", "BEGIN EVENT %acc 0xfe 20/16"] test_other = ["stop 1", "COMMS", "COMMS 1 2", "FINISH 2", ":start1", ":start2", "stop", "FINISH", "COMMS 1024", "COMMS 400/16", "COMMS 0x400"] test_prog1 = ["COMMS 0x400", "LIMITS 20, 10, 64, 0, 200", 'RESERVB 0, 3', 'RESERVB 6, 2', 'RESERVW 0, 2', 'RESERVW 4,1', 'RESERVA 0, 5', "BEGIN CONFIG 0,0", "BEGIN PROGRAM", "DATB count *", "DATB buffer *, 10", "DATW temps *, 5", 'DATA message * * "This is fun but long - will it work?"', "movb 5, @count", ":b1", "movb @count, %acc", "cmpb 0", "bre :f1", "decb @count", "bra :b1", ":f1", "movb 0, %acc", "END PROGRAM", "END CONFIG", "FINISH"] test_prog1 = ["COMMS 0x400", "LIMITS 200, 10, 64, 0, 200", 'RESERVB 0, 3', 'RESERVB 6, 2', 'RESERVW 0, 2', 'RESERVW 4,1', 'RESERVA 0, 5', 'DEVICE motor 1', 'DEVICE digin 2', "BEGIN CONFIG 0,0", "BEGIN MAIN", "DATB count *", "DATB buffer *, 10", "DATW temps *, 5", 'DATA message * * "This is fun but long - will it work?"', '::top', 'DATB *, 0, 200', "movb $5, @count", ":b1", "movb @count, %acc", "cmpb 0", "bre :f1", "decb @count", "bra :b1", ":f1", "movb 0, %acc", 'bra ::top', "END MAIN", "BEGIN EVENT %2C 1 1", ":f1", 'movw $-150, @temps', 'brne :f1', 'INSERT BINARY email.sig', 'bra :f1', 'END EVENT', "END CONFIG", "FINISH"] test_prog2 = ['BEGIN FIRMWARE 0, 0, 0', 'BINB 0x10 20/16 255', 'BINB "Copyright"', 'INSERT BINARY email.sig', 'END FIRMWARE', 'FINISH'] test_new_bad = ['bitset', 'bitset 3', 'bitset 3 2', 'bitset 29, f3'] # Bads test_new_good = ['BEGIN CONFIG 0,0', 'BEGIN MAIN', 'bitset 3 f3', 'bitset 0 ff', 'END MAIN', 'END CONFIG', 'FINISH'] logging.info("Starting test") err = logging_utils.Error_reporter() hl_parser.set_err_reporter(err) #err.set_exit_on_error(False) #err.set_throw_on_error(False) token_stream = tokens.Token_stream(err) token_stream.clear() test_lines = [] #test_lines.extend(test_simp) #test_lines.extend(test_jump) #test_lines.extend(test_stack) #test_lines.extend(test_data) #test_lines.extend(test_spec) #test_lines.extend(test_device) #test_lines.extend(test_begin_end) #test_lines.extend(test_other) #test_lines.extend(test_prog2) test_lines.extend(test_new_good) for t in test_lines: assem_line(t) token_stream.dump_tokens() logging_utils.dump_object(token_stream, "Token_stream") token_analysis = tokens.Token_analyser(token_stream, err) token_analysis.map_all_variables() token_analysis.dump_variable_map() token_analysis.fixup_jumps() token_analysis.fixup_sections() # Add section headers including lengths token_analysis.fixup_jumps() # Fixup globals which may change because of section headers token_analysis.fixup_crcs() # Finally the crcs -- nothing will change now token_stream.dump_tokens() logging_utils.dump_object(token_analysis, "Token_analyser")