diff --git a/README.md b/README.md index 796b366..ec27937 100644 --- a/README.md +++ b/README.md @@ -1,3 +1,22 @@ -# Compilador-Lógica -![DS1](https://i.imgur.com/84VICwh.png) -![DS2](https://i.imgur.com/NiTrVAL.png) +![git status](http://3.129.230.99/svg/lucafs/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 + +# 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) diff --git a/main.py b/main.py index b9c9c00..194dbd5 100644 --- a/main.py +++ b/main.py @@ -65,13 +65,18 @@ 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): try: return self.sym[name] except: - raise Exception("Symbol "+name+ " not declared") + return "error" class Node(): @@ -126,7 +131,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 +141,13 @@ def Evaluate(self, ST): name = self.children[0].value expression = self.children[1].Evaluate(ST) type = ST.getter(name)[1] - if(expression[1] != type ): - raise Exception("Can't cast this variable") + if(type == "error"): + raise Exception ("Symbol "+name+ " not declared") + 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): @@ -145,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: @@ -155,10 +169,16 @@ 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] + 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 == ">": @@ -166,9 +186,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 @@ -182,6 +208,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: @@ -228,7 +256,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)): @@ -291,7 +319,6 @@ def selectNext(self): raise Exception("AND INCOMPLETE") self.position +=1 self.actual.value += self.origin[self.position] - return self.actual @@ -440,7 +467,6 @@ def parseFactor(): raise Exception("Read error") return node else: - print(Parser.tokens.actual.value) raise Exception ("Factor error") @@ -457,7 +483,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..f26e783 100644 --- a/teste.c +++ b/teste.c @@ -1,13 +1,8 @@ -{ - int x; - bool y; - bool h; - string hu; - hu = "dogdog"; - h = false; - y = false; - if (h){ - x = readln(); +{ + if (true && (!(1==1))){ + println(1); + } + else{ + println(2); } - println(hu); }