This commit is contained in:
Mattia Mascarello 2021-09-29 20:29:13 +02:00
parent 8f6770013c
commit beaf429cbb
3 changed files with 80 additions and 11 deletions

50
examples/fizzbuzz.mga Normal file
View File

@ -0,0 +1,50 @@
#Fizzbuzz
INT n
INT i
INT nfizz
INT nbuzz
INT modfizz
INT modbuzz
SET n 0
SET nfizz 3
SET nbuzz 5
SET modfizz 0
SET modbuzz 0
SET i 0
IN INT n How many?\n
:loop
OUT \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
JE loop modfizz 0
DEC i 1
OUT $i$
INC i 1
JL loop i n
OUT \n
END
:fizzdo
OUT fizz
JMP fizzdone
:buzzdo
OUT buzz
JMP buzzdone

View File

@ -5,14 +5,19 @@ INT te
IN INT tot numero massimo IN INT tot numero massimo
SET n 1 SET n 1
:scn :scn
SET j 1 SET j 2
OUT $n$\n OUT $n$\n
:sieve :sieve
MOD n j te MOD n j te
OUT j: $j$ \n #OUT \n- $te$ -\n
OUT te: $te$ \n JME scr j n
INC j INC j 1
JME scn j n
JM sieve te 0 JM sieve te 0
INC n INC n
JLE scn n tot JLE scn n tot
END
:scr
#OUT $j$ sas\n
SET j 2
INC n
JMP sieve

16
gg.py
View File

@ -13,12 +13,17 @@ def sostT(text,vars):
return text return text
try: try:
for i in range(len(lines)): for i in range(len(lines)):
if len(lines[i].strip()) == 0:
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:
i+=1
continue
#print(lines[i]) #print(lines[i])
if lines[i][0]=="#": if lines[i][0]=="#":
i+=1 i+=1
@ -27,7 +32,7 @@ try:
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]]=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():
@ -58,6 +63,8 @@ try:
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":
i = jumps[ag[1]]
elif ag[0]=="JLE": elif ag[0]=="JLE":
if str(ag[3]).isnumeric(): if str(ag[3]).isnumeric():
athr=float(ag[3]) athr=float(ag[3])
@ -72,6 +79,13 @@ try:
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":
if str(ag[3]).isnumeric():
athr=float(ag[3])
else:
athr=float(vars[ag[3]])
if vars[ag[2]]==athr:
i = jumps[ag[1]]
elif ag[0]=="JL": elif ag[0]=="JL":
if str(ag[3]).isnumeric(): if str(ag[3]).isnumeric():
athr=float(ag[3]) athr=float(ag[3])