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 @@
INT i # 99 bottles of beer on the wall - https://en.wikipedia.org/wiki/99_Bottles_of_Beer#References_in_computer_science
SET i 99 INT i
:beg # declare counter "i" as an integer
OUT bottles of beer on the wall,$i$ bottles of beer\n SET i 99
# 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 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

178
gg.py
View File

@ -1,113 +1,149 @@
import sys import sys
import re import re
vars={} import math
lines=open(sys.argv[1],"r").read().split("\n") vars = {}
lines=list(map(lambda el: el.lstrip(),lines)) lines = open(sys.argv[1], "r").read().split("\n")
i=0 lines = list(map(lambda el: el.lstrip(), lines))
jumps={} i = 0
def sostT(text,vars): jumps = {}
matches=re.findall(r"(?<=\$)(.*)(?=\$)",text) lastBefJmp = 0
for match in matches:
if match in vars.keys():
text=text.replace("$"+match+"$",str(vars[match])) def sostT(text, vars):
return text 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: 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:
continue continue
ag=lines[i].split(" ") ag = lines[i].split(" ")
if len(ag[0]): if len(ag[0]):
if ag[0][0]==":": if ag[0][0] == ":":
jumps[ag[0][1:]]=i jumps[ag[0][1:]] = i
i=0 i = 0
while lines[i].strip()!="END" or i > len(lines) : while lines[i].strip() != "END" or i > len(lines):
if len(lines[i].strip()) == 0: if len(lines[i].strip()) == 0:
i+=1 i += 1
continue
#print(lines[i])
if lines[i][0]=="#":
i+=1
continue continue
ag=lines[i].split(" ") if lines[i][0] == "#":
i += 1
continue
ag = lines[i].split(" ")
if ag[0] == "INT": if ag[0] == "INT":
vars[ag[1]]=0 vars[ag[1]] = 0
elif ag[0]=="SET": elif ag[0] == "SET":
vars[ag[1]]=int(ag[2]) vars[ag[1]] = int(ag[2])
elif ag[0]=="DEC": elif ag[0] == "DEC":
if len(ag)==3: if len(ag) == 3:
if ag[2].isnumeric(): if ag[2].isnumeric():
n=int(ag[2]) n = int(ag[2])
else: else:
n=int(vars[ag[2]]) n = int(vars[ag[2]])
else: else:
n = 1 n = 1
vars[ag[1]]=int(vars[ag[1]])-n vars[ag[1]] = int(vars[ag[1]])-n
elif ag[0]=="INC": elif ag[0] == "INC":
if len(ag)==3: if len(ag) == 3:
if ag[2].isnumeric(): if ag[2].isnumeric():
n=int(ag[2]) n = int(ag[2])
else: else:
n=int(vars[ag[2]]) n = int(vars[ag[2]])
else: else:
n = 1 n = 1
vars[ag[1]]=int(vars[ag[1]])+n vars[ag[1]] = int(vars[ag[1]])+n
elif ag[0]=="CP": elif ag[0] == "CP":
vars[ag[1]]=vars[ag[2]] vars[ag[1]] = vars[ag[2]]
elif ag[0]=="MOD": elif ag[0] == "MOD":
if str(ag[1]).isnumeric(): if str(ag[1]).isnumeric():
m=float(ag[1]) m = float(ag[1])
else: else:
m=float(vars[ag[1]]) m = float(vars[ag[1]])
if str(ag[2]).isnumeric(): if str(ag[2]).isnumeric():
modTerm=float(ag[2]) modTerm = float(ag[2])
else: else:
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:
athr=float(vars[ag[3]]) athr = float(vars[ag[3]])
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:
athr=float(vars[ag[3]]) athr = float(vars[ag[3]])
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:
athr=float(vars[ag[3]]) athr = float(vars[ag[3]])
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:
athr=float(vars[ag[3]]) athr = float(vars[ag[3]])
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:
athr=vars[ag[3]] athr = vars[ag[3]]
if vars[ag[2]]>athr: if vars[ag[2]] > athr:
i = jumps[ag[1]] i = jumps[ag[1]]
elif ag[0]=="OUT": elif ag[0] == "OUT":
print(sostT(" ".join(ag[1:]),vars).replace("\\n","\n"),end='') print(sostT(" ".join(ag[1:]), vars).replace("\\n", "\n"), end='')
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(""))
i+=1 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: except Exception as e:
print(vars) print(vars)
print(jumps) print(jumps)
raise e raise e
#print(lines) # print(lines)