Changeset 127
- Timestamp:
- 08/09/07 22:51:33
- Files:
-
- branches/ast/geniusql/astwalk.py (modified) (9 diffs)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
branches/ast/geniusql/astwalk.py
r126 r127 192 192 terms = self.targets.get(ip) 193 193 if terms: 194 clause = self.stack .pop()194 clause = self.stack[-1] 195 195 while terms: 196 196 term, oper = terms.pop() … … 201 201 else: 202 202 clause = oper((term, clause)) 203 self.stack .append(clause)203 self.stack[-1] = clause 204 204 if self.verbose: 205 205 self.debug("clause:", clause, "\n") … … 217 217 218 218 def visit_BUILD_LIST(self, lo, hi): 219 terms = [self.stack.pop() for i in range(lo + (hi << 8))]220 terms.reverse()221 self.stack.append(ast.List(terms))219 numterms = lo + (hi << 8) 220 if numterms: 221 self.stack[-numterms:] = [ast.List(self.stack[-numterms:])] 222 222 223 223 def visit_BUILD_MAP(self, lo, hi): … … 226 226 227 227 def visit_BUILD_TUPLE(self, lo, hi): 228 terms = [self.stack.pop() for i in range(lo + (hi << 8))]229 terms.reverse()230 self.stack.append(ast.Tuple(terms))228 numterms = lo + (hi << 8) 229 if numterms: 230 self.stack[-numterms:] = [ast.Tuple(self.stack[-numterms:])] 231 231 232 232 def visit_CALL_FUNCTION(self, lo, hi): … … 237 237 kwargs.append(ast.Keyword(key, val)) 238 238 239 args = [self.stack.pop() for i in xrange(lo)] 240 args.reverse() 241 242 func = self.stack.pop() 239 if lo: 240 args = self.stack[-lo:] 241 self.stack[-lo:] = [] 242 else: 243 args = [] 244 245 func = self.stack[-1] 243 246 244 247 if self.reduce and isinstance(func, ast.Const): 245 248 if func.value is getattr and not isinstance(args[0], ast.Const): 246 self.stack .append(ast.Getattr(args[0], args[1].value))249 self.stack[-1] = ast.Getattr(args[0], args[1].value) 247 250 return 248 251 else: … … 266 269 if kwargvals is not None: 267 270 retval = func.value(*tuple(argvals), **kwargvals) 268 self.stack .append(ast.Const(retval))271 self.stack[-1] = ast.Const(retval) 269 272 return 270 273 271 274 if kwargs: 272 275 args += kwargs 273 self.stack .append(ast.CallFunc(func, args))276 self.stack[-1] = ast.CallFunc(func, args) 274 277 275 278 def visit_COMPARE_OP(self, lo, hi): 276 term 2, term1 = self.stack.pop(), self.stack.pop()279 term1, term2 = self.stack[-2:] 277 280 op = cmp_op[lo + (hi << 8)] 278 281 if self.reduce and (isinstance(term1, ast.Const) and 279 282 isinstance(term2, ast.Const)): 280 283 oper = codewalk.comparisons[op] 281 self.stack .append(ast.Const(oper(term1.value, term2.value)))282 else: 283 self.stack .append(ast.Compare(term1, [(op, term2)]))284 self.stack[-2:] = [ast.Const(oper(term1.value, term2.value))] 285 else: 286 self.stack[-2:] = [ast.Compare(term1, [(op, term2)])] 284 287 if self.verbose: 285 288 self.debug(op) … … 310 313 def visit_LOAD_ATTR(self, lo, hi): 311 314 term = self.co_names[lo + (hi << 8)] 312 obj = self.stack .pop()315 obj = self.stack[-1] 313 316 if self.reduce and isinstance(obj, ast.Const): 314 self.stack .append(ast.Const(getattr(obj.value, term)))315 else: 316 self.stack .append(ast.Getattr(obj, term))317 self.stack[-1] = ast.Const(getattr(obj.value, term)) 318 else: 319 self.stack[-1] = ast.Getattr(obj, term) 317 320 if self.verbose: 318 321 self.debug(term) … … 351 354 352 355 def visit_ROT_TWO(self): 353 v = self.stack.pop() 354 k = self.stack.pop() 355 self.stack.extend([v, k]) 356 k, v = self.stack[-2:] 357 self.stack[-2:] = [v, k] 356 358 357 359 def visit_ROT_THREE(self): 358 v = self.stack.pop() 359 k = self.stack.pop() 360 x = self.stack.pop() 361 self.stack.extend([v, x, k]) 360 x, k, v = self.stack[-3:] 361 self.stack[-3:] = [v, x, k] 362 362 363 363 def visit_SLICE_PLUS_0(self): 364 obj = self.stack .pop()364 obj = self.stack[-1] 365 365 if self.reduce and isinstance(obj, ast.Const): 366 self.stack .append(ast.Const(obj.value[:]))367 else: 368 self.stack .append(ast.Slice(obj, 'OP_APPLY', None, None))366 self.stack[-1] = ast.Const(obj.value[:]) 367 else: 368 self.stack[-1] = ast.Slice(obj, 'OP_APPLY', None, None) 369 369 370 370 def visit_SLICE_PLUS_1(self): 371 arg = self.stack.pop() 372 obj = self.stack.pop() 371 obj, arg = self.stack[-2:] 373 372 if self.reduce and (isinstance(obj, ast.Const) and 374 373 isinstance(arg, ast.Const)): 375 self.stack .append(ast.Const(obj.value[arg.value:]))376 else: 377 self.stack .append(ast.Slice(obj, 'OP_APPLY', arg, None))374 self.stack[-2:] = [ast.Const(obj.value[arg.value:])] 375 else: 376 self.stack[-2:] = [ast.Slice(obj, 'OP_APPLY', arg, None)] 378 377 379 378 def visit_SLICE_PLUS_2(self): 380 arg = self.stack.pop() 381 obj = self.stack.pop() 379 obj, arg = self.stack[-2:] 382 380 if self.reduce and (isinstance(obj, ast.Const) and 383 381 isinstance(arg, ast.Const)): 384 self.stack .append(ast.Const(obj.value[:arg.value]))385 else: 386 self.stack .append(ast.Slice(obj, 'OP_APPLY', None, arg))382 self.stack[-2:] = ast.Const(obj.value[:arg.value]) 383 else: 384 self.stack[-2:] = ast.Slice(obj, 'OP_APPLY', None, arg) 387 385 388 386 def visit_SLICE_PLUS_3(self): 389 arg2 = self.stack.pop() 390 arg1 = self.stack.pop() 391 obj = self.stack.pop() 387 obj, arg1, arg2 = self.stack[-3:] 392 388 if self.reduce and (isinstance(obj, ast.Const) and 393 389 isinstance(arg1, ast.Const) and 394 390 isinstance(arg2, ast.Const)): 395 self.stack .append(ast.Const(obj.value[arg1.value:arg2.value]))396 else: 397 self.stack .append(ast.Slice(obj, 'OP_APPLY', arg1, arg2))391 self.stack[-3:] = ast.Const(obj.value[arg1.value:arg2.value]) 392 else: 393 self.stack[-3:] = ast.Slice(obj, 'OP_APPLY', arg1, arg2) 398 394 399 395 def visit_STORE_SUBSCR(self): 400 k = self.stack.pop()401 396 # 'x' should be an ast.Dict 402 x = self.stack.pop()403 v = self.stack.pop()397 v, x, k = self.stack[-3:] 398 self.stack[-3:] = [] 404 399 x.items.append((k, v)) 405 400 ## … … 409 404 410 405 def visit_UNARY_INVERT(self): 411 term = self.stack .pop()406 term = self.stack[-1] 412 407 if self.reduce and isinstance(term, ast.Const): 413 self.stack .append(ast.Const(~term.value))414 else: 415 self.stack .append(ast.Invert(term))408 self.stack[-1] = ast.Const(~term.value) 409 else: 410 self.stack[-1] = ast.Invert(term) 416 411 417 412 def visit_UNARY_NEGATIVE(self): 418 term = self.stack .pop()413 term = self.stack[-1] 419 414 if self.reduce and isinstance(term, ast.Const): 420 self.stack .append(ast.Const(-term.value))421 else: 422 self.stack .append(ast.UnarySub(term))415 self.stack[-1] = ast.Const(-term.value) 416 else: 417 self.stack[-1] = ast.UnarySub(term) 423 418 424 419 def visit_UNARY_NOT(self): 425 term = self.stack .pop()420 term = self.stack[-1] 426 421 if self.reduce and isinstance(term, ast.Const): 427 self.stack .append(ast.Const(not term.value))428 else: 429 self.stack .append(ast.Not(term))422 self.stack[-1] = ast.Const(not term.value) 423 else: 424 self.stack[-1] = ast.Not(term) 430 425 431 426 def visit_UNARY_POSITIVE(self): 432 term = self.stack .pop()427 term = self.stack[-1] 433 428 if self.reduce and isinstance(term, ast.Const): 434 self.stack .append(ast.Const(+term.value))435 else: 436 self.stack .append(ast.UnaryAdd(term))429 self.stack[-1] = ast.Const(+term.value) 430 else: 431 self.stack[-1] = ast.UnaryAdd(term) 437 432 438 433 def visit_BINARY_SUBSCR(self): 439 op 2, op1 = self.stack.pop(), self.stack.pop()434 op1, op2 = self.stack[-2:] 440 435 if self.reduce and (isinstance(op1, ast.Const) and 441 436 isinstance(op2, ast.Const)): 442 self.stack .append(ast.Const(op1.value[op2.value]))443 else: 444 self.stack .append(ast.Subscript(op1, 'OP_APPLY', op2))437 self.stack[-2:] = [ast.Const(op1.value[op2.value])] 438 else: 439 self.stack[-2:] = [ast.Subscript(op1, 'OP_APPLY', op2)] 445 440 446 441 def binary_op(self, op): 447 op 2, op1 = self.stack.pop(), self.stack.pop()442 op1, op2 = self.stack[-2:] 448 443 if self.reduce and (isinstance(op1, ast.Const) and 449 444 isinstance(op2, ast.Const)): 450 self.stack .append(ast.Const(ast_to_op[op](op1.value, op2.value)))445 self.stack[-2:] = [ast.Const(ast_to_op[op](op1.value, op2.value))] 451 446 else: 452 447 # Binary ops like ast.Add take a single tuple as a first arg 453 self.stack .append(op((op1, op2)))448 self.stack[-2:] = [op((op1, op2))] 454 449 455 450 def bit_op(self, op): 456 op 2, op1 = self.stack.pop(), self.stack.pop()451 op1, op2 = self.stack[-2:] 457 452 if self.reduce and (isinstance(op1, ast.Const) and 458 453 isinstance(op2, ast.Const)): 459 self.stack .append(ast.Const(ast_to_op[op](op1.value, op2.value)))460 else: 461 self.stack .append(op(op1, op2))454 self.stack[-2:] = [ast.Const(ast_to_op[op](op1.value, op2.value))] 455 else: 456 self.stack[-2:] = [op(op1, op2)] 462 457 463 458
