def enable_while_loop(self, line_index): LOC = self.CODE[line_index][:] if len(LOC) >= 3: if LOC[-1] == ":": pass try: value = self.solve(LOC[1:-1], line_index) if value == "true": last_line = line_index + 1 temp_stack = stack() while True: temp_code = self.CODE[last_line][:] if len(temp_code) <= 0: pass elif temp_code[0] == "#": pass elif temp_code[0] in ["if", "for", "while"]: temp_stack.push(temp_code[0]) elif temp_code[0] == ";": if temp_stack.pointer == -1: break else: temp_stack.pop() last_line += 1 self.loop.push({ "type": "while", "first_line": line_index, "last_line": last_line }) # print (last_line,line_index) return line_index elif value == "false": last_line = line_index + 1 temp_stack = stack() while True: temp_code = self.CODE[last_line][:] if len(temp_code) <= 0: pass elif temp_code[0] == "#": pass elif temp_code[0] in ["if", "for", "while"]: temp_stack.push(temp_code[0]) elif temp_code[0] == ";": if temp_stack.pointer == -1: break else: temp_stack.pop() last_line += 1 return last_line else: self.make_error(line_index=line_index) except: self.make_error(line_index=line_index) else: self.make_error(line_index=line_index) else: self.make_error(line_index=line_index)
def enable_if_loop(self, line_index): LOC = self.CODE[line_index][:] if LOC[-1] == ":": # print (LOC[1:-1]) value = self.solve(LOC[1:-1], line_index) # print (value) if value == "true": pass self.loop.push({"type": "if", "line_index": line_index}) return line_index else: last_line = line_index + 1 temp_stack = stack() while True: temp_code = self.CODE[last_line][:] if len(temp_code) <= 0: pass elif temp_code[0] == "#": pass elif temp_code[0] in ["if", "for", "while"]: temp_stack.push(temp_code[0]) elif temp_code[0] == ";": if temp_stack.pointer == -1: break else: temp_stack.pop() last_line += 1 return last_line else: self.make_error(line_index=line_index)
def __init__(self, CODE): self.CODE = CODE self.database = database() self.variable_constraints = [95] + list(range(48, 58)) + list( range(97, 123)) + list(range(65, 91)) self.variable_constraints_for_initial = [95] + list(range( 97, 123)) + list(range(65, 91)) self.loop = stack() self.result_text_for_label = ""
def split_as_variables_operators_and_integers(self): final = [] item = stack() last = None inside_string = False self.CODE_divided_by_new_line = self.CODE.split("\n", ) for line in self.CODE_divided_by_new_line: result = [] for character in line + " ": if character == "\"": if len("".join(item.values)) > 0: prev_index = (line.index(character)) - 1 if len("".join(item.values)) == 1: result.append("".join(item.values)) else: result.append("\"" + "".join(item.values)) item.empty() inside_string = not inside_string continue if inside_string: item.push(character) continue if character in [" "]: if len("".join(item.values)) > 0: result.append("".join(item.values)) item.empty() elif ord(character) in self.variable_constraints: if last != None: if ord(last) in self.variable_constraints: item.push(character) else: if len("".join(item.values)) > 0: result.append("".join(item.values)) item.empty() item.push(character) else: item.push(character) else: if len("".join(item.values)) > 0: result.append("".join(item.values)) item.empty() item.push(character) last = character final.append(result) self.CODE = final[:] if "EOL" in self.CODE: self.make_error( message="EOL is a reserved keyword. Don't use it in the Code.") while "\n" in self.CODE: self.CODE[self.CODE.index("\n")] = "EOL"
def enable_for_loop(self, line_index): LOC = self.CODE[line_index][:] if len(LOC) >= 7: variable_name = LOC[1] if self.is_variable(variable_name): pass if LOC[2] == "from" and LOC[-1] == ":" and "to" in LOC: try: start_value = self.solve(LOC[3:LOC.index("to")], line_index) end_value = self.solve(LOC[LOC.index("to") + 1:-1], line_index) # print (start_value,end_value) last_line = line_index + 1 temp_stack = stack() while True: temp_code = self.CODE[last_line][:] if len(temp_code) <= 0: pass elif temp_code[0] == "#": pass elif temp_code[0] in ["if", "for", "while"]: temp_stack.push(temp_code[0]) elif temp_code[0] == ";": if temp_stack.pointer == -1: break else: temp_stack.pop() last_line += 1 self.database.variables.update( {variable_name: start_value}) self.loop.push({ "type": "for", "from": variable_name, "upto": end_value, "first_line": line_index, "last_line": last_line }) # print (last_line,line_index) except: self.make_error(line_index=line_index) else: self.make_error(line_index=line_index) else: self.make_error(line_index=line_index, message="invalid variable name") else: self.make_error(line_index=line_index)
def solve(self, expression, line_index): # print ("before",expression,line_index) for operator in self.database.operator_priority: while operator in expression: index = expression.index(operator) prev_index = index - 1 next_index = index + 1 if operator == "(": temp_stack = stack() for word in expression: if word == "(": temp_stack.empty() elif word == ")": sub_expression = temp_stack.values[:] result = self.solve(sub_expression, line_index) # print ("ooops",result) start_index = expression.index(word) - len( temp_stack.values) - 1 iterations_to_delete = len(temp_stack.values) + 2 # print ("a value",self.database.variables["a"]) # print (expression) for ___ in range(iterations_to_delete): expression.pop(start_index) expression.insert(start_index, result) # print ("brackets",expression) # return expression else: temp_stack.push(word) elif operator == ".": try: prev_value = str( self.get_value(expression[prev_index], line_index)) next_value = str( self.get_value(expression[next_index], line_index)) for ___ in range(3): expression.pop(prev_index) # print (expression) value = float(prev_value + "." + next_value) expression.insert(prev_index, value) # print ("hahahahahaha") except: # print ("ooops") self.make_error(line_index=line_index) elif operator == "/": # print ("hahahahahaha") try: prev_value = self.get_value(expression[prev_index], line_index) next_value = self.get_value(expression[next_index], line_index) # print ("ooops",prev_value,next_value) for ___ in range(3): expression.pop(prev_index) value = self.get_value(prev_value / next_value, line_index) expression.insert(prev_index, value) except: self.make_error(line_index=line_index) elif operator == "*": try: # print ("ooops",expression[prev_index],expression[next_index]) prev_value = self.get_value(expression[prev_index], line_index) next_value = self.get_value(expression[next_index], line_index) for ___ in range(3): expression.pop(prev_index) value = self.get_value(prev_value * next_value, line_index) expression.insert(prev_index, value) except: self.make_error(line_index=line_index) elif operator == "%": try: prev_value = self.get_value(expression[prev_index], line_index) next_value = self.get_value(expression[next_index], line_index) for ___ in range(3): expression.pop(prev_index) value = self.get_value(prev_value % next_value, line_index) expression.insert(prev_index, value) except: self.make_error(line_index=line_index) elif operator == "+": try: prev_value = self.get_value(expression[prev_index], line_index) next_value = self.get_value(expression[next_index], line_index) for ___ in range(3): expression.pop(prev_index) value = self.get_value(prev_value + next_value, line_index) expression.insert(prev_index, value) except: self.make_error(line_index=line_index) elif operator == "-": prev_value = str(expression[prev_index]) if 48 <= ord(prev_value[0]) <= 57 or self.is_variable( prev_value): try: # print (expression) prev_value = self.get_value( expression[prev_index], line_index) next_value = self.get_value( expression[next_index], line_index) for ___ in range(3): expression.pop(prev_index) value = self.get_value(prev_value - next_value, line_index) expression.insert(prev_index, value) # print ("ooops",prev_value,next_value) except: self.make_error(line_index=line_index) else: try: next_value = self.get_value( expression[next_index], line_index) for ___ in range(2): expression.pop(index) value = self.get_value((-1) * next_value, line_index) expression.insert(index, value) except: self.make_error(line_index=line_index) # print ("final - simplification",expression) elif operator == "is": try: prev_value = self.get_value(expression[prev_index], line_index) next_value = self.get_value(expression[next_index], line_index) for ___ in range(3): expression.pop(prev_index) value = "true" if (prev_value == next_value) else "false" value = self.get_value(value, line_index) expression.insert(prev_index, value) except: self.make_error(line_index=line_index) elif operator == "lt": try: prev_value = self.get_value(expression[prev_index], line_index) next_value = self.get_value(expression[next_index], line_index) for ___ in range(3): expression.pop(prev_index) value = "true" if ( prev_value < next_value) else "false" value = self.get_value(value, line_index) expression.insert(prev_index, value) except: self.make_error(line_index=line_index) elif operator == "le": try: prev_value = self.get_value(expression[prev_index], line_index) next_value = self.get_value(expression[next_index], line_index) for ___ in range(3): expression.pop(prev_index) value = "true" if ( prev_value <= next_value) else "false" value = self.get_value(value, line_index) expression.insert(prev_index, value) except: self.make_error(line_index=line_index) elif operator == "gt": try: prev_value = self.get_value(expression[prev_index], line_index) next_value = self.get_value(expression[next_index], line_index) for ___ in range(3): expression.pop(prev_index) value = "true" if ( prev_value > next_value) else "false" value = self.get_value(value, line_index) expression.insert(prev_index, value) except: self.make_error(line_index=line_index) elif operator == "ge": try: prev_value = self.get_value(expression[prev_index], line_index) next_value = self.get_value(expression[next_index], line_index) for ___ in range(3): expression.pop(prev_index) value = "true" if ( prev_value >= next_value) else "false" value = self.get_value(value, line_index) expression.insert(prev_index, value) except: self.make_error(line_index=line_index) elif operator == "and": try: prev_value = self.get_value(expression[prev_index], line_index) next_value = self.get_value(expression[next_index], line_index) for ___ in range(3): expression.pop(prev_index) value = self.get_value( self.and_operator(prev_value, next_value, line_index), line_index) expression.insert(prev_index, value) except: self.make_error(line_index=line_index) elif operator == "or": try: prev_value = self.get_value(expression[prev_index], line_index) next_value = self.get_value(expression[next_index], line_index) for ___ in range(3): expression.pop(prev_index) value = self.get_value( self.or_operator(prev_value, next_value, line_index), line_index) expression.insert(prev_index, value) except: self.make_error(line_index=line_index) elif operator == "not": try: next_value = self.get_value(expression[next_index], line_index) for ___ in range(2): expression.pop(index) # print ("here",next_value) value = self.get_value( self.not_operator(next_value, line_index), line_index) expression.insert(index, value) except: self.make_error(line_index=line_index) # print (expression) if len(expression) == 1: return self.get_value(expression[0], line_index) else: self.make_error(line_index=line_index)