From beaf429cbb55a31c596ac9891f7a680ed582d9bb Mon Sep 17 00:00:00 2001 From: MatMasIt Date: Wed, 29 Sep 2021 20:29:13 +0200 Subject: [PATCH] Fizzbuzz --- examples/fizzbuzz.mga | 50 +++++++++++++++++++++++++++++++++++++++++++ examples/primes.mga | 25 +++++++++++++--------- gg.py | 16 +++++++++++++- 3 files changed, 80 insertions(+), 11 deletions(-) create mode 100644 examples/fizzbuzz.mga diff --git a/examples/fizzbuzz.mga b/examples/fizzbuzz.mga new file mode 100644 index 0000000..76a4738 --- /dev/null +++ b/examples/fizzbuzz.mga @@ -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 + + diff --git a/examples/primes.mga b/examples/primes.mga index 422aa79..49b6775 100644 --- a/examples/primes.mga +++ b/examples/primes.mga @@ -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 diff --git a/gg.py b/gg.py index 0cb6e1c..a31f839 100644 --- a/gg.py +++ b/gg.py @@ -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])