From 0346030a78179e325ddad2966c49bc6501cc331f Mon Sep 17 00:00:00 2001 From: bigyihsuan Date: Sun, 27 Nov 2022 01:56:19 -0500 Subject: [PATCH] =?UTF-8?q?add=20`=CA=94`=20IN=3F=20instruction?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- sample/printipa.ipel | 1 + sample/x without x cop.ipel | 3 ++ src/instructions.py | 7 ++- src/interpreter.py | 103 +++++++++++++++++++++--------------- 4 files changed, 69 insertions(+), 45 deletions(-) create mode 100644 sample/printipa.ipel create mode 100644 sample/x without x cop.ipel diff --git a/sample/printipa.ipel b/sample/printipa.ipel new file mode 100644 index 0000000..871a791 --- /dev/null +++ b/sample/printipa.ipel @@ -0,0 +1 @@ +{3F}{2P}ɑe{2V}əɐʌɔ|i|eχu|i|e1søɒ"äæçðøħŋœǁǃ"{HW}{GG}ɑe{GQ}əe{GT}əe{HJ}əe{HO}əe{HR}əɞɞɞɞɐʌɔ|j|eχu|j|e1søɒ{IQ}{I0}ɑe{IB}əe{IE}əe{IF}əe{II}əe{IM}əe{IO}əɞɞɞɞɞɐʌɔ|h|eχu|h|e1søɒ"βθχᶑⱱ"o diff --git a/sample/x without x cop.ipel b/sample/x without x cop.ipel new file mode 100644 index 0000000..1cfb020 --- /dev/null +++ b/sample/x without x cop.ipel @@ -0,0 +1,3 @@ +{32}{15}0ɑbesχue1søɒ{65}{32}0ɑbesχue1søɒ + +(X = !"#$%&'()*+,-.ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_`) \ No newline at end of file diff --git a/src/instructions.py b/src/instructions.py index d99e948..277805f 100644 --- a/src/instructions.py +++ b/src/instructions.py @@ -1,6 +1,6 @@ import math, string, util -def executeInstruction(instruction, unvoiced, voiced, currentStack): +def executeInstruction(instruction: str, unvoiced: list, voiced: list, currentStack: list): """ This file defines the majority of 1-character instructions in IPEL. Input is an instruction, the 2 stacks, and the current stack. @@ -223,6 +223,11 @@ def executeInstruction(instruction, unvoiced, voiced, currentStack): for e in currentStack.pop(): out += str(e) currentStack.append(out) + elif instruction == "ʔ": + if len(currentStack) >= 2: + if type(currentStack[-2]) == list or type(currentStack[-2]) == str: + e = currentStack.pop() + currentStack.append(e in currentStack[-1]) # IO elif instruction == "i": currentStack.append(input().strip()) diff --git a/src/interpreter.py b/src/interpreter.py index 18300b9..6fbd0fd 100644 --- a/src/interpreter.py +++ b/src/interpreter.py @@ -9,59 +9,74 @@ from evaluator import evaluate import os -labels = {} # Maps a label to a location in code. - # Also maps the name of a function to its definition location. -lexemes = [] # List of lexemes. -parser = P() +def printUsage(): + print("Usage:") + print("python3 interpreter.py (options) (code)") + print() + print("Options:") + print(" -d: Debug mode. Print label mappings, parsed lexemes, data stack, and execution stacks.") + print(" -f filename: Use the file `filename` as the code source input.") -# Stacks -unvoiced = [] -voiced = [] -executionStack = [] +def main(): + labels = {} # Maps a label to a location in code. + # Also maps the name of a function to its definition location. + lexemes = [] # List of lexemes. + parser = P() -# Register -register = None + # Stacks + unvoiced = [] + voiced = [] + executionStack = [] -currentStack = unvoiced -otherStack = voiced + # Register + register = None -debugmode = False + currentStack = unvoiced + otherStack = voiced -if len(sys.argv) > 1: - foundFile = -1 - for i in range(1, len(sys.argv)): - if os.path.isfile(sys.argv[i]): - foundFile = i + debugmode = False + usingfile = -1 - if sys.argv[1] == "-d": - debugmode = True + if len(sys.argv) > 1: + foundFile = -1 + for i, arg in enumerate(sys.argv): + if arg == "-d": + debugmode = True + if arg == "-f": + usingfile = i - if foundFile == -1: - code = sys.argv[2] if debugmode else sys.argv[1] - else: - source = open(sys.argv[foundFile], "r") - code = source.read() + if usingfile > -1 and len(sys.argv) > usingfile + 1: + source = open(sys.argv[usingfile + 1], "r") + code = source.read() + elif usingfile != -1: + print("not enough arguments for using file") + printUsage() + return + else: + code = sys.argv[len(sys.argv)-1] - code += " " - lastlex = lexer.Lex(T.BEGIN, "") - while lastlex.token != T.END: - code, lex = parser.getNextToken(code) - if (lex.token != T.COMMENT): - lexemes.append(lex) - lastlex = lex - if lastlex.token == T.ERR: - print("LEXING ERROR:", lastlex.lexeme) - os.abort() + code += " " + lastlex = lexer.Lex(T.BEGIN, "") + while lastlex.token != T.END: + code, lex = parser.getNextToken(code) + if (lex.token != T.COMMENT): + lexemes.append(lex) + lastlex = lex + if lastlex.token == T.ERR: + print("LEXING ERROR:", lastlex.lexeme) + os.abort() - parser.mapLabels(lexemes, labels) - if debugmode: - print("Lexemes:", lexemes) - print("Label Mapping:", labels) + parser.mapLabels(lexemes, labels) + if debugmode: + print("Lexemes:", lexemes) + print("Label Mapping:", labels) - if not parser.validateLexemes(lexemes, labels): - os.abort() + if not parser.validateLexemes(lexemes, labels): + os.abort() - currentStack, otherStack = evaluate(lexemes, labels, debugmode, unvoiced, voiced, executionStack, currentStack, otherStack, register) -else: - print("Code file not found") + currentStack, otherStack = evaluate(lexemes, labels, debugmode, unvoiced, voiced, executionStack, currentStack, otherStack, register) + else: + printUsage() +if __name__ == "__main__": + main() \ No newline at end of file