def idiv(self, args): self.args_check([ ['var'], ['int', 'float'], ['int', 'float'] ], args) a = self._typed_value(args[1]) a_type = self._type_of(args[1]) b = self._typed_value(args[2]) b_type = self._type_of(args[2]) if (b_type == 'float' or a_type == 'float'): out_type = 'float' else: out_type = 'int' if b == 0: errors.operand_value("try to div by 0") out = a // b self.symtable.set_value( self._get_typeval(args[0]), out_type, str(out) ) self.ip += 1
def exit(self, args): self.args_check([ ['int'] ], args) retcode = self._typed_value(args[0]) if retcode not in range(0,50): errors.operand_value("invalid exit code {}".format(retcode)) exit(retcode)
def read(self, args): self.args_check([ ['var'], ['type'] ], args) readedtype = self._value(args[1]).lower() if readedtype not in ['int', 'float', 'bool', 'string']: errors.operand_value("unsuported type to read {}".format(readedtype)) try: if self.infile == None: readed = input() else: readed = self.infile.readline() except: readed = "nil" readedtype = "nil" ## check type try: if readedtype == 'int': readed = int(readed) elif readedtype == 'float': readed = float(readed) elif readedtype == 'bool': readed = readed.lower() if readed == "true": readed = True elif readed == "false": readed = False else: readed = "nil" readedtype = "nil" except: readed = "nil" readedtype = "nil" self.symtable.set_value( self._get_typeval(args[0]), readedtype, str(readed) ) self.ip += 1
def _typed_value(self, arg): value = self._value(arg) typed = self._type_of(arg) if (typed == 'int'): return int(value) elif (typed == 'float'): return float(value) elif typed == 'bool': if value.lower() == "true": return True elif value.lower() == "false": return False errors.operand_value("bad value of operand type Bool") else: return value
def div(self, args): self.args_check([ ['var'], ['int', 'float'], ['int', 'float'] ], args) a = self._typed_value(args[1]) b = self._typed_value(args[2]) if b == 0: errors.operand_value("try to div by 0") out = a / b self.symtable.set_value( self._get_typeval(args[0]), 'float', str(out) ) self.ip += 1