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
# declare counter "i" as an integer
SET i 99
:beg
# set i to 99
:beering
# main loop
OUT bottles of beer on the wall, $i$ bottles of beer\n
# Print line 1
DEC i 1
# decrease i
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

View File

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

View File

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

38
gg.py
View File

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