Skip to content

Commit

Permalink
add ʔ IN? instruction
Browse files Browse the repository at this point in the history
  • Loading branch information
bigyihsuan committed Nov 27, 2022
1 parent 64b001f commit 0346030
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 45 deletions.
1 change: 1 addition & 0 deletions sample/printipa.ipel
Original file line number Diff line number Diff line change
@@ -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
3 changes: 3 additions & 0 deletions sample/x without x cop.ipel
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{32}{15}0ɑbesχue1søɒ{65}{32}0ɑbesχue1søɒ

(X = !"#$%&'()*+,-.ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_`)
7 changes: 6 additions & 1 deletion src/instructions.py
Original file line number Diff line number Diff line change
@@ -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.
Expand Down Expand Up @@ -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())
Expand Down
103 changes: 59 additions & 44 deletions src/interpreter.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()

0 comments on commit 0346030

Please sign in to comment.