def readline(args): prompt = args[0] os.write(1, prompt.to_string()) res = '' while True: buf = os.read(0, 16) if not buf: return objects.String(res) res += buf if res[-1] == '\n': # print res[:-1] == "" return objects.String(res[:-1])
def read_macro_constant(node): extent = node.extent start, end = extent.start, extent.end with open(start.file.name) as fd: a = start.offset + len(node.displayname) b = end.offset data = fd.read()[a:b].strip() if data.isdigit(): return objects.Integer(int(data)) if data.startswith('0x'): return objects.Integer(int(data, 16)) if data.startswith('"') and data.endswith('"'): return objects.String(data[1:-1]) raise Exception("no interpretation found for %r" % data)
def expr(self, expr): if expr.type == 'identifier': var = self.function.lookup(expr.string) assert var is not None, repr(expr.string) return var if expr.type == 'string': return objects.String(expr.string) if expr.type == 'number': if "." in expr.string: return objects.Float(float(expr.string)) elif expr.string.startswith('0x'): return objects.Integer(int(expr.string, 16)) else: return objects.Integer(int(expr.string)) if expr.type == 'member': arg = self.expr(expr[0]) return self.append(member(arg, expr.string)) if expr.type == 'call': args = [self.expr(sub_expr) for sub_expr in expr] callee = args.pop(0) return self.append(call(callee, args)) if expr.type == 'def': argv = [arg.string for arg in expr[0]] body = expr[1:] return self.new_function(argv, body) if expr.type == 'op': lhs, rhs = expr opname = '' if expr.string == '<': opname = 'lt' if expr.string == '>': opname = 'gt' if expr.string == '<=': opname = 'le' if expr.string == '>=': opname = 'ge' if expr.string == '!=': opname = 'ne' if expr.string == '==': opname = 'eq' if len(opname) > 0: res = call(self.function.lookup(opname), [self.expr(lhs), self.expr(rhs)]) self.append(res) return res raise Exception("%s not built" % expr.repr())
def compile_string(context, ast): assert(isinstance(ast,ast_objects.String)) value = objects.String(ast.value) index = context.register_constant(value) context.emit(bytecode.LOAD_CONST,index)
def eval_string_literal(self, ast_node, env): value = str(ast_node.literal) return objects.String(value)