Created
November 14, 2020 08:17
-
-
Save Transfusion/6d03e6449262fd09b5b4ee5dec897cf2 to your computer and use it in GitHub Desktop.
obama's interpreter
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # Dependencies | |
| import os | |
| import sys, traceback | |
| import readline | |
| from platform import python_version | |
| version = "1.0-stable" | |
| # Inbuilt commands | |
| class commands(): | |
| def echo(arg): | |
| print(arg) | |
| def cwd(args=False): | |
| if not args: | |
| print(os.getcwd()) | |
| elif not os.path.isdir(args): | |
| print("Invalid path.") | |
| elif args: | |
| os.chdir(args) | |
| def mkdir(dir): | |
| try: | |
| os.mkdir(dir) | |
| print("Directory created.") | |
| except: | |
| print("Directory could not be created.") | |
| # Interpreter | |
| while True: | |
| rawInput = str(input("\033[92m\u001b[1minterpreter {} on Python {} {}\033[0m » ".format(version, python_version(), '\u001b[34m' + os.getcwd() + '\033[0m'))) | |
| #.split() | |
| quoted_string_stack = [] | |
| quotes_begin_end = [] # begin and end of string range that needs to be merged | |
| # cleanedInput = "" | |
| cmdArray = [] | |
| argArray = [] | |
| try: | |
| for idx, c in enumerate(rawInput): | |
| # print(idx, quoted_string_stack, quotes_begin_end) | |
| if c == '\'' or c == '"': | |
| if not quoted_string_stack or quoted_string_stack[0][1] != c: | |
| quoted_string_stack.append((idx, c)) | |
| else: | |
| # can be simplified... | |
| while quoted_string_stack[-1][1] != c: | |
| quoted_string_stack.pop() | |
| begin_idx, _ = quoted_string_stack[-1] | |
| quoted_string_stack.pop() | |
| quotes_begin_end.append((begin_idx, idx)) | |
| quoted_args_begin_end = [(begin-2, end+1) for begin, end in | |
| filter(lambda x: rawInput[x[0]-2: x[0]] == '--', quotes_begin_end)] | |
| quoted_args_begin_end.insert(0, (0,0)) | |
| quoted_args_begin_end.append((len(rawInput), len(rawInput))) | |
| for i in range(0, len(quoted_args_begin_end)-1): | |
| cleanedInput = rawInput[quoted_args_begin_end[i][1]: quoted_args_begin_end[i+1][0]] | |
| cleanedInput = cleanedInput.split() | |
| for s in cleanedInput: | |
| if s.startswith('--'): | |
| argArray.append(s[2:]) | |
| else: | |
| cmdArray.append(s) | |
| if i < len(quoted_args_begin_end) - 2: # because we appended (len(rawInput), len(rawInput)) | |
| # rawInput is echo --huehue --'hi hi' --"'ho ho' --'hee hee'" 'ha ha' --huhu | |
| # argArray is ['huehue', "--'hi hi'", '--"\'ho ho\' --\'hee hee\'"', 'huhu'], strip --" .... " | |
| argArray.append(rawInput[quoted_args_begin_end[i+1][0] + 3: quoted_args_begin_end[i+1][1] - 1]) | |
| except: | |
| print("Invalid syntax.") | |
| traceback.print_exc() | |
| execute = commands | |
| print("cmdArray: {} argArray: {}".format(cmdArray, argArray)) | |
| for o in cmdArray: | |
| execute = getattr(execute, o, "") | |
| try: | |
| execute(*argArray) | |
| except TypeError as syntaxerror: | |
| print("Invalid syntax.") | |
| print(syntaxerror) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
epic