debug = True data = 'if(hello+word+42.5==y2k+7)then;if(name=="lary")then;print(name,"age",mail2);else then;print(5.56+2.23);end;end' word = "" c = "" i = 0 wtype = 0 types = ["void","int","float","var","key","op","str","cap","dlm","trm"] line = [] lines = [[]] keywords = ["import", "if", "then", "else", "end", "for", "while", "in", "as", "of", "try", "except", "break", "print", "return"] letters = "abcdefghijklnmopqrstuvwxyz" numbers = "0123456789" operators = "+-*/^%&|!=" delimiters = ",." capsules = "[]{}()" terminators = ";\n" escape = False error = False running = True print("\n") print(" ---[ Program Code ]--- ") print("\n") print(data) print("\n") data = data + " " if debug: print("\n") print(" ---[ Parsing... ]--- ") print("\n") while running: c = data[i] if wtype == 0: if c in numbers: wtype = 1 word += c elif c in letters: wtype = 3 word += c elif c in operators: wtype = 5 word += c line.append([types[wtype],word]) word = "" wtype = 0 elif c in capsules: wtype = 7 word += c line.append([types[wtype],word]) word = "" wtype = 0 elif c in delimiters: wtype = 8 word += c line.append([types[wtype],word]) word = "" wtype = 0 elif c in terminators: wtype = 9 word += c line.append([types[wtype],word]) word = "" wtype = 0 elif c == "\"": wtype = 6 word += c elif wtype == 1: if c in numbers: word += c elif c == ".": word += c wtype = 2 else: line.append([types[wtype],word]) word = "" wtype = 0 i -= 1 elif wtype == 2: if c in numbers: word += c else: line.append([types[wtype],word]) word = "" wtype = 0 i -= 1 elif wtype == 3: if (c in letters) or (c in numbers): word += c if word in keywords: wtype = 4 line.append([types[wtype],word]) word = "" wtype = 0 else: line.append([types[wtype],word]) word = "" wtype = 0 i -= 1 elif wtype == 6: if not escape: if not c == '"': if not c == '\\': word += c else: escape == True else: word += c line.append([types[wtype],word]) word = "" wtype = 0 else: word += c escape = False else: wtype = 0 if debug: print(i,c,word,types[wtype]) i += 1 if i < len(data): running = True else: running = False print("\n") print(" ---[ Lines ]--- ") line_count = 0 print("\n") print(" ---[ "+str(line_count)+" ]--- ") for t in line: if t[0] == "trm": line_count += 1 lines.append([]) print(" ---[ "+str(line_count)+" ]--- ") else: lines[-1].append(t) print(t) print("\n") print(" ---[ Pretty Printed Code ]-- ") print("\n") indent = 0 for line in lines: if line[-1] == ["key","end"]: indent -=1 if line[0] == ["key","else"]: indent -=1 print("".join([" " for x in range(indent)])+" ".join([l[1] for l in line])) if line[-1] == ["key","then"]: indent += 1