Started adding some commands (new), added RET

This commit is contained in:
MatMasIt 2021-11-15 21:21:44 +01:00
parent 529a195cbf
commit fc031dda9f
4 changed files with 125 additions and 83 deletions

View File

@ -1,8 +1,17 @@
# 99 bottles of beer on the wall - https://en.wikipedia.org/wiki/99_Bottles_of_Beer#References_in_computer_science
INT i INT i
# declare counter "i" as an integer
SET i 99 SET i 99
:beg # set i to 99
:beering
# main loop
OUT bottles of beer on the wall, $i$ bottles of beer\n OUT bottles of beer on the wall, $i$ bottles of beer\n
# Print line 1
DEC i 1 DEC i 1
# decrease i
OUT Take one down and pass it around, $i$ bottles of beer on the wall\n OUT Take one down and pass it around, $i$ bottles of beer on the wall\n
JME beg i 1 # Print line 2
JME beering i 1
# loop until 1
END END
#end

View File

@ -11,7 +11,7 @@ SET nfizz 3
SET nbuzz 5 SET nbuzz 5
SET modfizz 0 SET modfizz 0
SET modbuzz 0 SET modbuzz 0
SET i 0 SET i 1
IN INT n How many?\n IN INT n How many?\n
@ -20,12 +20,8 @@ IN INT n How many?\n
MOD i nfizz modfizz MOD i nfizz modfizz
JE fizzdo modfizz 0 JE fizzdo modfizz 0
:fizzdone
MOD i nbuzz modbuzz MOD i nbuzz modbuzz
JE buzzdo modbuzz 0 JE buzzdo modbuzz 0
:buzzdone
INC i 1 INC i 1
JE loop modbuzz 0 JE loop modbuzz 0
@ -41,10 +37,10 @@ IN INT n How many?\n
:fizzdo :fizzdo
OUT fizz OUT fizz
JMP fizzdone RET
:buzzdo :buzzdo
OUT buzz OUT buzz
JMP buzzdone RET

View File

@ -32,3 +32,4 @@ END
JMP printback JMP printback

38
gg.py
View File

@ -1,16 +1,31 @@
import sys import sys
import re import re
import math
vars = {} vars = {}
lines = open(sys.argv[1], "r").read().split("\n") lines = open(sys.argv[1], "r").read().split("\n")
lines = list(map(lambda el: el.lstrip(), lines)) lines = list(map(lambda el: el.lstrip(), lines))
i = 0 i = 0
jumps = {} jumps = {}
lastBefJmp = 0
def sostT(text, vars): def sostT(text, vars):
matches = re.findall(r"(?<=\$)(.*)(?=\$)", text) matches = re.findall(r"(?<=\$)(.*)(?=\$)", text)
for match in matches: for match in matches:
if match in vars.keys(): if match in vars.keys():
text = text.replace("$"+match+"$", str(vars[match])) text = text.replace("$"+match+"$", str(vars[match]))
return text return text
def cast(to,val):
if to == "INT":
return int(val)
elif to == "DEC":
return float(val)
elif to == "LST":
return list(val)
elif to == "STR":
return str(val)
try: try:
for i in range(len(lines)): for i in range(len(lines)):
if len(lines[i].strip()) == 0: if len(lines[i].strip()) == 0:
@ -24,7 +39,6 @@ try:
if len(lines[i].strip()) == 0: if len(lines[i].strip()) == 0:
i += 1 i += 1
continue continue
#print(lines[i])
if lines[i][0] == "#": if lines[i][0] == "#":
i += 1 i += 1
continue continue
@ -64,8 +78,10 @@ try:
modTerm = float(vars[ag[2]]) modTerm = float(vars[ag[2]])
vars[ag[3]] = m % modTerm vars[ag[3]] = m % modTerm
elif ag[0] == "JMP": elif ag[0] == "JMP":
lastBefJmp = i
i = jumps[ag[1]] i = jumps[ag[1]]
elif ag[0] == "JLE": elif ag[0] == "JLE":
lastBefJmp = i
if str(ag[3]).isnumeric(): if str(ag[3]).isnumeric():
athr = float(ag[3]) athr = float(ag[3])
else: else:
@ -73,6 +89,7 @@ try:
if vars[ag[2]] <= athr: if vars[ag[2]] <= athr:
i = jumps[ag[1]] i = jumps[ag[1]]
elif ag[0] == "JME": elif ag[0] == "JME":
lastBefJmp = i
if str(ag[3]).isnumeric(): if str(ag[3]).isnumeric():
athr = float(ag[3]) athr = float(ag[3])
else: else:
@ -80,6 +97,7 @@ try:
if vars[ag[2]] >= athr: if vars[ag[2]] >= athr:
i = jumps[ag[1]] i = jumps[ag[1]]
elif ag[0] == "JE": elif ag[0] == "JE":
lastBefJmp = i
if str(ag[3]).isnumeric(): if str(ag[3]).isnumeric():
athr = float(ag[3]) athr = float(ag[3])
else: else:
@ -87,6 +105,7 @@ try:
if vars[ag[2]] == athr: if vars[ag[2]] == athr:
i = jumps[ag[1]] i = jumps[ag[1]]
elif ag[0] == "JL": elif ag[0] == "JL":
lastBefJmp = i
if str(ag[3]).isnumeric(): if str(ag[3]).isnumeric():
athr = float(ag[3]) athr = float(ag[3])
else: else:
@ -94,6 +113,7 @@ try:
if vars[ag[2]] < athr: if vars[ag[2]] < athr:
i = jumps[ag[1]] i = jumps[ag[1]]
elif ag[0] == "JM": elif ag[0] == "JM":
lastBefJmp = i
if str(ag[3]).isnumeric(): if str(ag[3]).isnumeric():
athr = float(ag[3]) athr = float(ag[3])
else: else:
@ -105,6 +125,22 @@ try:
elif ag[0] == "IN": elif ag[0] == "IN":
print(sostT(" ".join(ag[3:]), vars).replace("\\n", "\n"), end='') print(sostT(" ".join(ag[3:]), vars).replace("\\n", "\n"), end='')
vars[ag[2]] = int(input("")) vars[ag[2]] = int(input(""))
elif ag[0] == "RET":
i = lastBefJmp
elif ag[0] == "ADD":
vars[ag[3]] = vars[ag[1]] + vars[ag[2]]
elif ag[0] == "SUB":
vars[ag[3]] = vars[ag[1]] - vars[ag[2]]
elif ag[0] == "MUL":
vars[ag[3]] = vars[ag[1]] * vars[ag[2]]
elif ag[0] == "DIV":
vars[ag[3]] = vars[ag[1]] / vars[ag[2]]
elif ag[0] == "EL":
vars[ag[3]] = vars[ag[1]] ** vars[ag[2]]
elif ag[0] == "FLO":
vars[ag[2]] = int(vars[ag[1]])
elif ag[0] == "CEL":
vars[ag[2]] = math.ceil(vars[ag[1]])
i += 1 i += 1
except Exception as e: except Exception as e:
print(vars) print(vars)