From 5dba9ab64f648e809cec6175c7e7e6d399418cfc Mon Sep 17 00:00:00 2001 From: luca farah Date: Wed, 12 May 2021 17:26:10 -0300 Subject: [PATCH 1/8] Update Readme --- README.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 796b366..ab78d3b 100644 --- a/README.md +++ b/README.md @@ -1,3 +1,3 @@ # Compilador-Lógica -![DS1](https://i.imgur.com/84VICwh.png) -![DS2](https://i.imgur.com/NiTrVAL.png) +![DS1](https://i.imgur.com/4mg9GHX.png) +![DS2](https://i.imgur.com/GeV76oI.png) From 3800f32a2a3a2c7506da491e2304c20d0193b2be Mon Sep 17 00:00:00 2001 From: luca farah Date: Sun, 16 May 2021 19:29:45 -0300 Subject: [PATCH 2/8] final 2.3 --- main.py | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/main.py b/main.py index b9c9c00..d7f8854 100644 --- a/main.py +++ b/main.py @@ -71,7 +71,7 @@ def getter(self, name): try: return self.sym[name] except: - raise Exception("Symbol "+name+ " not declared") + return "error" class Node(): @@ -126,7 +126,9 @@ def Evaluate(self,ST): class FirstAssign(Node): def Evaluate(self, ST): type = self.children[0] - name = self.children[1] + name = self.children[1] + if(ST.getter(name) != "error"): + raise Exception ("Variable " + name + " aready declared") ST.setter(name, None ,type) class Assign(Node): @@ -134,8 +136,10 @@ def Evaluate(self, ST): name = self.children[0].value expression = self.children[1].Evaluate(ST) type = ST.getter(name)[1] + if(type == "error"): + raise Exception ("Symbol "+name+ " not declared") if(expression[1] != type ): - raise Exception("Can't cast this variable") + raise Exception("Can't cast the "+ name +" variable") ST.setter(name, expression[0],type) class Identifier(Node): From 79bb967c7d0884a047576940736fd53b97bff3f2 Mon Sep 17 00:00:00 2001 From: luca farah Date: Sun, 16 May 2021 20:31:16 -0300 Subject: [PATCH 3/8] update readme --- README.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index ab78d3b..ad3523a 100644 --- a/README.md +++ b/README.md @@ -1,3 +1,3 @@ # Compilador-Lógica -![DS1](https://i.imgur.com/4mg9GHX.png) -![DS2](https://i.imgur.com/GeV76oI.png) +![DS1](https://i.imgur.com/XC7Qrwm.png) +![DS2](https://i.imgur.com/G29Aidx.png) From 1f17577511f867bc38d1503c10d7292655c27f09 Mon Sep 17 00:00:00 2001 From: luca farah Date: Wed, 26 May 2021 16:46:57 -0300 Subject: [PATCH 4/8] add tests to README --- README.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/README.md b/README.md index ad3523a..406435e 100644 --- a/README.md +++ b/README.md @@ -1,3 +1,5 @@ +![git status](http://3.129.230.99/svg/lucafs/Compilador-L-gica/) + # Compilador-Lógica ![DS1](https://i.imgur.com/XC7Qrwm.png) ![DS2](https://i.imgur.com/G29Aidx.png) From b3489b1deecdbbb5e7122ec5d67f9c07001fdb74 Mon Sep 17 00:00:00 2001 From: luca farah Date: Wed, 26 May 2021 17:31:31 -0300 Subject: [PATCH 5/8] final 2.3 --- main.py | 40 ++++++++++++++++++++++++++++------------ teste.c | 26 ++++++++++++++------------ 2 files changed, 42 insertions(+), 24 deletions(-) diff --git a/main.py b/main.py index d7f8854..6d69859 100644 --- a/main.py +++ b/main.py @@ -65,6 +65,11 @@ def __init__(self): self.sym = {} def setter(self, name, value, type): + if(value != None): + if(type == "bool"): + value = bool(value) + elif(type == "int"): + value = int(value) self.sym[name] = (value,type) def getter(self, name): @@ -138,8 +143,11 @@ def Evaluate(self, ST): type = ST.getter(name)[1] if(type == "error"): raise Exception ("Symbol "+name+ " not declared") - if(expression[1] != type ): - raise Exception("Can't cast the "+ name +" variable") + if(expression[1] != type): + if((expression[1] == "bool" and type == "int") or (expression[1] == "int" and type == "bool")): + pass + else: + raise Exception("Can't cast the "+ name +" variable") ST.setter(name, expression[0],type) class Identifier(Node): @@ -149,6 +157,8 @@ def Evaluate(self, ST): class Println(Node): def Evaluate(self, ST): print_value = self.children[0].Evaluate(ST) + if(print_value == "error"): + raise Exception ("Symbol "+self.children[0].value+" not declared") if(print_value[1] == "string"): print(print_value[0][1:-1]) else: @@ -159,10 +169,11 @@ def Evaluate(self, ST): return (int(input()),"int") class LogOp(Node): - - def Evaluate(self, ST): - if(self.children[0].Evaluate(ST)[1] == "string" or self.children[0].Evaluate(ST)[1] == "string"): - raise Exception ("Can't make Boolean Operations with strings") + def Evaluate(self, ST): + variableType1 = self.children[0].Evaluate(ST)[1] + variableType2 = self.children[1].Evaluate(ST)[1] + if((variableType1 == "string" and variableType2 != "string") or (variableType1 != "string" and variableType2 == "string")): + raise Exception ("Not valid boolean operation") if self.value == "<": return (self.children[0].Evaluate(ST)[0] < self.children[1].Evaluate(ST)[0],"bool") elif self.value == ">": @@ -170,9 +181,15 @@ def Evaluate(self, ST): elif self.value == "==": return (self.children[0].Evaluate(ST)[0] == self.children[1].Evaluate(ST)[0],"bool") elif self.value == "&&": - return (self.children[0].Evaluate(ST)[0] and self.children[1].Evaluate(ST)[0],"bool") + if(bool(self.children[0].Evaluate(ST)[0] & self.children[1].Evaluate(ST)[0])): + return(True,"bool") + else: + return (False,"bool") elif self.value == "||": - return (self.children[0].Evaluate(ST)[0] or self.children[1].Evaluate(ST)[0],"bool") + if(self.children[0].Evaluate(ST)[0] | self.children[1].Evaluate(ST)[0]): + return (True,"bool") + else: + return(False,"bool") elif self.value == "!": return (not self.children[0].Evaluate(ST)[0],"bool")#Não sei se é o certo @@ -186,6 +203,8 @@ def Evaluate(self, ST): class IfOp(Node): def Evaluate(self, ST): + if(self.children[0].Evaluate(ST)[1] == "string"): + raise Exception("Can't use string as condition") if (self.children[0].Evaluate(ST)[0]): self.children[1].Evaluate(ST) else: @@ -232,7 +251,7 @@ def selectNext(self): return self.actual if(self.position < len(self.origin)): #passa enquanto for espaço - while(self.origin[self.position] == " "): + while(self.origin[self.position] == " " or self.origin[self.position] == "\t"): self.position += 1 #se for o ultimo acaba if (self.position >= len(self.origin)): @@ -295,7 +314,6 @@ def selectNext(self): raise Exception("AND INCOMPLETE") self.position +=1 self.actual.value += self.origin[self.position] - return self.actual @@ -444,7 +462,6 @@ def parseFactor(): raise Exception("Read error") return node else: - print(Parser.tokens.actual.value) raise Exception ("Factor error") @@ -461,7 +478,6 @@ def parseBlock(): raise Exception("} not found") return commands else: - raise Exception("{ not found") @staticmethod diff --git a/teste.c b/teste.c index a03db51..f1dd9ba 100644 --- a/teste.c +++ b/teste.c @@ -1,13 +1,15 @@ -{ - int x; - bool y; - bool h; - string hu; - hu = "dogdog"; - h = false; - y = false; - if (h){ - x = readln(); +{ + bool a; + int b; + int c; + + b = 32; + c = 32; + a = true; + + if ((b && c) == a) { + println(1); + }else{ + println(2); } - println(hu); -} +} \ No newline at end of file From b9832e2c7b19e9d23e0b92c0fe260fc8198665b3 Mon Sep 17 00:00:00 2001 From: luca farah Date: Thu, 27 May 2021 11:23:57 -0300 Subject: [PATCH 6/8] Fixed ! issue --- main.py | 11 ++++++++--- teste.c | 19 ++++++------------- 2 files changed, 14 insertions(+), 16 deletions(-) diff --git a/main.py b/main.py index 6d69859..194dbd5 100644 --- a/main.py +++ b/main.py @@ -171,9 +171,14 @@ def Evaluate(self, ST): class LogOp(Node): def Evaluate(self, ST): variableType1 = self.children[0].Evaluate(ST)[1] - variableType2 = self.children[1].Evaluate(ST)[1] - if((variableType1 == "string" and variableType2 != "string") or (variableType1 != "string" and variableType2 == "string")): - raise Exception ("Not valid boolean operation") + try: + variableType2 = self.children[1].Evaluate(ST)[1] + if((variableType1 == "string" and variableType2 != "string") or (variableType1 != "string" and variableType2 == "string")): + raise Exception ("Not valid boolean operation") + except: + if(variableType1 == "string" and self.value == "!"): + raise Exception ("Not valid ! operation") + if self.value == "<": return (self.children[0].Evaluate(ST)[0] < self.children[1].Evaluate(ST)[0],"bool") elif self.value == ">": diff --git a/teste.c b/teste.c index f1dd9ba..f26e783 100644 --- a/teste.c +++ b/teste.c @@ -1,15 +1,8 @@ { - bool a; - int b; - int c; - - b = 32; - c = 32; - a = true; - - if ((b && c) == a) { - println(1); - }else{ - println(2); + if (true && (!(1==1))){ + println(1); } -} \ No newline at end of file + else{ + println(2); + } +} From 91353a054f89881155609ce297af493ac3516d89 Mon Sep 17 00:00:00 2001 From: lucafs Date: Fri, 10 Sep 2021 23:27:44 -0300 Subject: [PATCH 7/8] att README --- README.md | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 406435e..beba72d 100644 --- a/README.md +++ b/README.md @@ -1,5 +1,19 @@ ![git status](http://3.129.230.99/svg/lucafs/Compilador-L-gica/) -# Compilador-Lógica +# Compilador Lógica + + ### Este projeto se resume em um compilador feito em python com o objetivo de compilar códigos de C básicos. Este compilador suporta as seguintes funcionalidades. + + - Println + - Readln + - Declarações de funções + - Declarações de variáveis + - While + - Chamada de função + - Operações lógicas + - If / Else + + +# Estrutura do código: ![DS1](https://i.imgur.com/XC7Qrwm.png) ![DS2](https://i.imgur.com/G29Aidx.png) From b79f14a1739be6979acfa8f09e123ac44892bf1b Mon Sep 17 00:00:00 2001 From: lucafs Date: Fri, 10 Sep 2021 23:32:53 -0300 Subject: [PATCH 8/8] att readme --- README.md | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index beba72d..ec27937 100644 --- a/README.md +++ b/README.md @@ -13,7 +13,10 @@ - Operações lógicas - If / Else - +# Como rodar: +``` +python main.py teste.c +``` # Estrutura do código: ![DS1](https://i.imgur.com/XC7Qrwm.png) ![DS2](https://i.imgur.com/G29Aidx.png)