def makeBlock( self, blockPrev, i, line, ): if -1 != line.find("("): sentence = line[:line.find("(")].split() if len(sentence) == 1: if sentence[0] == "if" or sentence[0] == "while": newCondicional = Reservada(blockPrev, sentence[0], None, None, None) condicion = line[line.find("(") + 1:line.find(")")] operadorLogico = None for e in ['>', '<', '<=', '>=', '==', '!=']: if condicion.find(e) != -1: operadorLogico = e break if operadorLogico: # A < B newCondicional.valorA = condicion[:condicion. find(operadorLogico)] newCondicional.valorB = condicion[ condicion.find(operadorLogico) + len(operadorLogico):] newCondicional.operadorLogico = operadorLogico else: self.mistakes.append( f"Error - Linea {i}: Operador logico no encontrado" ) return newCondicional else: self.mistakes.append( f"Error - Linea {i}: Declaracion de funcion invalida") else: blockTmp = blockPrev aux = None while blockTmp: aux = blockTmp.search(sentence[1]) if aux: break blockTmp = blockTmp.myBlock if aux: self.mistakes.append( f"Error - Linea {i}: la funcion '{sentence[1]}' ya se encuentra declarada" ) newFuncion = Funcion(blockPrev, sentence[0], sentence[1]) parametros = line[line.find("(") + 1:line.find(")")] if parametros != '': parametros = parametros.split(',') for var in parametros: aux = var.split() newFuncion.addParameter( Variable(newFuncion, aux[1], aux[0], None, i)) return newFuncion else: self.mistakes.append(f"Error - Linea {i}: Declaracion invalida")
def HTML(self): """ Returns the HTML representation of this sequence, which is the HTML representation of all its arguments""" result = "" for arg in self.sequence.values(): if type(arg) == Constant: result += Constant(arg).HTML() elif type(arg) == Variable: result += Variable(arg.person, arg.var).HTML() elif type(arg) == Test: result += Test(arg.condition, arg.ifTrue, arg.ifFalse).HTML() elif type(arg) == Loop: result += Loop(arg.object, arg.list, arg.todo).HTML() elif type(arg) == Iterator: result += Iterator(arg.iterator, arg.parameters).HTML() else: raise TypeError( " Your object has to be a Sequence, a Test, a Loop, a MyList, an Iterator, a Constant or a Variable" ) return result
def buildMemberfn(self): tok = self.current_token lf = len(self.compiler.functions) self.compiler.compileLine(thisp=True, thispt=self.prototypeType) self.compiler.ctidx -= 1 self.update() if (len(self.compiler.functions) - lf > 0): f = self.compiler.functions[-1] if f.name in OPERATORS: self.buildOperator(f, tok) else: self.compiler.possible_members.append(f.name) self.prototypeType.members.append( Variable(DType(f"function {f.createTypename()}", 8, function_template=f), f.name, initializer=f)) if (self.current_token.tok == T_CLSSCOPE): self.advance()
def buildMember(self): # member type starttok = self.current_token t = self.compiler.checkType() self.update() if (self.current_token.tok != T_ID): throw(ExpectedIdentifier(self.current_token)) # if member of the same type as this struct, it must be a pointer, # otherwise it is an incomplete type. if (t.name == id): nested = True if (t.ptrdepth == 0): throw(UseOfIncompleteType(self.current_token, t)) ptrhint = t.ptrdepth t = self.prototypeType else: ptrhint = None # member variable name name = self.current_token.value # check if this member variable has already been initialized exists = next( (v for v in self.prototypeType.members if v.name == name), None) is not None var = Variable(t, name, glob=False, offset=self.size, isptr=t.ptrdepth > 0, signed=t.signed) if (exists): throw(VariableRedeclaration(self.current_token, var)) # finalize data var.ptrhint = ptrhint var.t.ptrdepth = ptrhint if ptrhint is not None else t.ptrdepth self.prototypeType.members.append(var) self.prototypeType.s += var.t.csize() if not var.isptr else 8 self.compiler.possible_members.append(name) self.size += t.csize() self.advance() # uninitialized if (self.current_token.tok == T_ENDL): return if (self.current_token.tok == T_OPENIDX): self.advance() start = self.compiler.ctidx while (self.current_token.tok != T_CLSIDX): self.advance() exprtokens = self.compiler.currentTokens[start:self.compiler.ctidx] value = determineConstexpr(t.isflt(), exprtokens, self.emptyfn) self.size += t.csize() * value.accessor - 1 self.advance() return # inititialized with constexpr if (self.current_token.tok != T_EQUALS): throw( ExpectedToken(self.current_token, '; or ='), notes=[ Note(starttok, "Did you mean to include 'function' here?") ] if self.current_token.tok == "(" else []) self.advance() # determine bounds of constexpr by iterating until next endline st = self.compiler.ctidx while (self.current_token.tok != T_ENDL): self.advance() end = self.compiler.ctidx # evaluate the constexpr to assign to the member's initializer value = determineConstexpr(t.isflt(), self.compiler.currentTokens[st:end], self.emptyfn) var.initializer = value.accessor
def makeVariable(self, block, i, line): def cheakValue(block, typeData, name, value, i): if len(value.split()) == 1: value = value.split()[0] return self.getValue(block, typeData, name, value, i) else: if -1 != value.find('+') or -1 != value.find( '-') or -1 != value.find('*') or -1 != value.find('/'): values = value.split() #m + 4 if len(values) >= 3: if values[1] == '+' or values[1] == '-' or values[ 1] == '*' or values[1] == '/': A = self.getValue(block, typeData, name, values[0], i) B = self.getValue(block, typeData, name, values[2], i) if A and B: if typeData == "string" and -1 != A.find('"'): A = A[A.find('"') + 1:] if -1 != A.find('"'): A = A[:A.find('"')] if typeData == "string" and -1 != B.find('"'): B = B[B.find('"') + 1:] if -1 != B.find('"'): B = B[:B.find('"')] tipoA = str(type(A)) tipoA = tipoA[tipoA.find("\'") + 1:] tipoA = tipoA[:tipoA.find("\'")] tipoB = str(type(B)) tipoB = tipoB[tipoB.find("\'") + 1:] tipoB = tipoB[:tipoB.find("\'")] if tipoA == "str": tipoA += "ing" if tipoB == "str": tipoB += "ing" if tipoA == typeData and tipoB == typeData: result = None try: if values[1] == '+': result = A + B if values[1] == '-': result = A - B if values[1] == '*': result = A * B if values[1] == '/': result = A / B except: return None if typeData == "string": result = '\"' + result + "\"" if -1 != value.find('"') and -1 != value[value.find('"') + 1:].find('"'): return value return None if -1 != line.find(';'): line = line[:line.find(';')] #Eliminar el ;\n sentence = line.split() if len(sentence) >= 4: value = cheakValue(block, sentence[0], sentence[1], line[line.find("=") + 1:], i) blockTmp = block aux = None while blockTmp: aux = blockTmp.search(sentence[1]) if aux: break blockTmp = blockTmp.myBlock if aux: self.mistakes.append( f"Error - Linea {i}: la variable '{sentence[1]}' ya esta declarada" ) else: aux = Variable(block, sentence[1], sentence[0], value, i) self.mistakes.extend(aux.mistakes) if aux.value: return aux else: self.mistakes.append( f"Error - Linea {i}: Declaracion de variable invalida") else: self.mistakes.append( f"Error - Linea {i}: Se esperaba el simbolo ';' al final de la linea" )
def add_item(self, id, data_type, address, dimensions): if not id in self.__table: self.__table[id] = Variable(data_type, address, dimensions) return True else: return False