From d472caa320d12eec758635c761d1d920c9681629 Mon Sep 17 00:00:00 2001 From: MatMasIt Date: Tue, 28 Sep 2021 22:35:18 +0200 Subject: [PATCH] First commit --- examples/99.mga | 11 ++++++++ examples/rect.mga | 19 +++++++++++++ gg.py | 70 +++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 100 insertions(+) create mode 100644 examples/99.mga create mode 100644 examples/rect.mga create mode 100644 gg.py diff --git a/examples/99.mga b/examples/99.mga new file mode 100644 index 0000000..e2f2135 --- /dev/null +++ b/examples/99.mga @@ -0,0 +1,11 @@ +INT i +INT im +SET i 99 +:beg +OUT bottles of beer on the wall,$i$ bottles of beer\n +CP im i +DEC im 1 +OUT Take one down and pass it around, $im$ bottles of beer on the wall\n +DEC i 1 +JME beg i 1 +END diff --git a/examples/rect.mga b/examples/rect.mga new file mode 100644 index 0000000..f72ea05 --- /dev/null +++ b/examples/rect.mga @@ -0,0 +1,19 @@ +OUT I will draw a rectangle\n +INT w +INT h +IN INT w l +IN INT h h +INT wr +INT hr +SET wr 0 +SET hr 0 +:wrc +:wf +OUT * +INC wr +JL wf wr w +OUT \n +SET wr 0 +INC hr +JL wrc hr h +END diff --git a/gg.py b/gg.py new file mode 100644 index 0000000..fb32347 --- /dev/null +++ b/gg.py @@ -0,0 +1,70 @@ +import sys +import re +vars={} +lines=open(sys.argv[1],"r").read().split("\n") +i=0 +jumps={} +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 +try: + while lines[i].strip()!="END": + #print(lines[i]) + if lines[i][0]=="#": + i+=1 + continue + ag=lines[i].split(" ") + if ag[0] == "INT": + vars[ag[1]]=0 + elif ag[0]=="SET": + vars[ag[1]]=ag[2] + elif ag[0]=="DEC": + vars[ag[1]]=int(vars[ag[1]])-1 + elif ag[0]=="INC": + vars[ag[1]]=int(vars[ag[1]])+1 + elif ag[0]=="CP": + vars[ag[1]]=vars[ag[2]] + elif ag[0]=="JLE": + if str(ag[3]).isnumeric(): + athr=float(ag[3]) + else: + athr=vars[ag[3]] + if vars[ag[2]]<=athr: + i = jumps[ag[1]] + elif ag[0]=="JME": + if str(ag[3]).isnumeric(): + athr=float(ag[3]) + else: + athr=vars[ag[3]] + if vars[ag[2]]>=athr: + i = jumps[ag[1]] + elif ag[0]=="JL": + if str(ag[3]).isnumeric(): + athr=float(ag[3]) + else: + athr=vars[ag[3]] + if vars[ag[2]]athr: + i = jumps[ag[1]] + elif ag[0][0]==":": + jumps[ag[0][1:]]=i + elif ag[0]=="OUT": + print(sostT(" ".join(ag[1:]),vars).replace("\\n","\n"),end='') + elif ag[0]=="IN": + print(sostT(" ".join(ag[3:]),vars).replace("\\n","\n"),end='') + vars[ag[2]]=int(input("")) + i+=1 +except Exception as e: + print(vars) + print(jumps) + raise e +#print(lines)