language

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

helper.py (1731B)


      1 
      2 def fatal_error(i, string):
      3 	i.success = False
      4 	print("")
      5 	print("FATAL: {0}".format(string))
      6 	print(" On Statement Type '{}', line #{}".format(i.line[0].name, i.ln))
      7 	print(" {}: ".format(i.line[1]))
      8 	print("")
      9 
     10 # This function splits a tokenstring by split characters, along with providing
     11 # a way to preserve capturing groups via esc_chars
     12 # An example call would be as follows:
     13 #
     14 # token_split( ["Hello", " ", "(", "Wonderful", " ", "World", ")"],
     15 #              [ ["("], [")"] ],
     16 #              [" "] )
     17 #
     18 #  Would return:
     19 #    [ [ "Hello" ], ["(", "Wonderful", " ", "World", ")" ] ]
     20 def token_split(tokenstring, esc_chars, split_chars, include_splitter=True):
     21 	tokens = []
     22 	tmp = []
     23 	capturing = False
     24 	depth     = 0
     25 	for x in tokenstring:
     26 		if x in esc_chars[0]:
     27 			if capturing:
     28 				depth += 1
     29 			else:
     30 				capturing = esc_chars[0].index(x)
     31 			tmp.append(x)
     32 		elif x in esc_chars[1]:
     33 			if esc_chars[1][capturing] == x:
     34 				if depth > 0:
     35 					depth -= 1
     36 				elif depth == 0:
     37 					capturing = False
     38 				tmp.append(x)
     39 		elif include_splitter or not x in split_chars or capturing:
     40 			tmp.append(x)
     41 
     42 		if x in split_chars and not capturing:
     43 			tokens.append(tmp)
     44 			tmp = []
     45 	if len(tmp) > 0:
     46 		tokens.append(tmp)
     47 
     48 	return tokens
     49 
     50 # This here takes a number and chops it up into a series of byte-width numbers
     51 # i.e 42 -> [42], 256 -> [1, 0]
     52 #
     53 # TODO: I don't want to use this anymore but it's just for testing.
     54 def int_to_bytes(number):
     55 	rv = []
     56 	c = 0
     57 	while (number / (0xFF << (8*c))) > 1:
     58 		rv.insert(0, (number & (0xFF << (8*c))) >> (8*c))
     59 		c += 1
     60 
     61 	rv.insert(0, (number & (0xFF << (8*c))) >> (8*c))
     62 
     63 	return rv
     64 
     65 def int_to_word(number):
     66 	rv = [0, 0]
     67 
     68 	rv[0] = (number & 0xFF00) >> 8
     69 	rv[1] = number & 0x00FF
     70 
     71 	return rv