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
SET n 1
:scn
SET j 1
OUT $n$\n
:sieve
MOD n j te
OUT j: $j$ \n
OUT te: $te$ \n
INC j
JME scn j n
JM sieve te 0
INC n
SET j 2
OUT $n$\n
:sieve
MOD n j te
#OUT \n- $te$ -\n
JME scr j n
INC j 1
JM sieve te 0
INC n
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
try:
for i in range(len(lines)):
if len(lines[i].strip()) == 0:
continue
ag=lines[i].split(" ")
if len(ag[0]):
if ag[0][0]==":":
jumps[ag[0][1:]]=i
i=0
while lines[i].strip()!="END" or i > len(lines) :
if len(lines[i].strip()) == 0:
i+=1
continue
#print(lines[i])
if lines[i][0]=="#":
i+=1
@ -27,7 +32,7 @@ try:
if ag[0] == "INT":
vars[ag[1]]=0
elif ag[0]=="SET":
vars[ag[1]]=ag[2]
vars[ag[1]]=int(ag[2])
elif ag[0]=="DEC":
if len(ag)==3:
if ag[2].isnumeric():
@ -58,6 +63,8 @@ try:
else:
modTerm=float(vars[ag[2]])
vars[ag[3]]=m%modTerm
elif ag[0]=="JMP":
i = jumps[ag[1]]
elif ag[0]=="JLE":
if str(ag[3]).isnumeric():
athr=float(ag[3])
@ -72,6 +79,13 @@ try:
athr=float(vars[ag[3]])
if vars[ag[2]]>=athr:
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":
if str(ag[3]).isnumeric():
athr=float(ag[3])