language

some fools attempt at an interpreted language
Log | Files | Refs | README

toil (1066B)


      1 #!/usr/bin/python3
      2 
      3 from interpreter import *
      4 
      5 def tobytearray(l, n, ba):
      6 	for i in l:
      7 		if type(i) is list:
      8 			ba = tobytearray(i, n, ba)
      9 			n += 1
     10 		else:
     11 			if type(i) is bytes:
     12 				i = int.from_bytes(i, byteorder='big')
     13 			if type(i) is int:
     14 				ba.append(i)
     15 
     16 	return(ba)
     17 
     18 def printb(l):
     19 	if type(l) is list:
     20 		for i in l:
     21 			printb(i)
     22 	else:
     23 		print(" "+hex(l), end="")
     24 
     25 def write_out(output_file):
     26 	out = open(output_file, "wb")
     27 
     28 	print("\nTO BYTES\n")
     29 
     30 	rv = []
     31 
     32 	for n, l in enumerate(itr.program):
     33 		print("{}: {} <= ".format(str(n).rjust(4),
     34 		                          l[0].name.rjust(15),
     35 		                          l[1]),
     36 		                          end="")
     37 
     38 		for e in l[2]:
     39 			t = e.action()
     40 			printb(t)
     41 			rv.append(t)
     42 
     43 		print()
     44 
     45 	program = bytearray()
     46 	program = tobytearray(rv, 0, program)
     47 
     48 	out.write(program)
     49 	out.close()
     50 
     51 if __name__ == "__main__":
     52 	import sys
     53 
     54 	if len(sys.argv) != 3:
     55 		print("useage: {} input_file output_file".format(sys.argv[0]))
     56 		sys.exit(1)
     57 
     58 	itr = Interpreter(sys.argv[1])
     59 
     60 	if itr.success:
     61 		write_out(sys.argv[2])
     62