From: <fwi...@us...> - 2007-12-31 16:48:23
|
Revision: 3918 http://jython.svn.sourceforge.net/jython/?rev=3918&view=rev Author: fwierzbicki Date: 2007-12-31 08:48:20 -0800 (Mon, 31 Dec 2007) Log Message: ----------- Some handling of negatives, start of slice support, added a -n option to astlib to report the number of diff lines for each tested file. Modified Paths: -------------- trunk/sandbox/ast/astlib.py trunk/sandbox/ast/grammar/Python.g trunk/sandbox/ast/grammar/PythonWalker.g trunk/sandbox/ast/jastlib.py Modified: trunk/sandbox/ast/astlib.py =================================================================== --- trunk/sandbox/ast/astlib.py 2007-12-30 23:50:02 UTC (rev 3917) +++ trunk/sandbox/ast/astlib.py 2007-12-31 16:48:20 UTC (rev 3918) @@ -38,7 +38,7 @@ else: yield node -def main(code_path, jy_exe="jython", print_diff=True, print_fail=False, print_success=False,): +def main(code_path, jy_exe="jython", print_diff=True, print_fail=False, print_success=False, print_diff_lines=False): from pprint import pprint from popen2 import popen2 from StringIO import StringIO @@ -63,12 +63,14 @@ differs = False diffstr = [] + difflines = 0 diff = Differ() results = diff.compare(pstr, jstr) for d in results: diffstr.append(d) if d[0] in ['+', '-']: differs = True + difflines += 1 if print_success and not differs: print "SUCCESS: %s" % pyfile @@ -76,6 +78,9 @@ if print_fail and differs: print "FAIL: %s" % pyfile + if print_diff_lines: + print "%s diff lines in %s" % (difflines, pyfile) + if print_diff and differs: print "---------- ouput -------------" print "py: %s" % sio.getvalue() @@ -88,9 +93,10 @@ import getopt usage = """\ -Usage: python %s [-j jython_exe_name] [-s -f -d] code_path +Usage: python %s [-j jython_exe_name] [-s -f -d -n] code_path where -s = print success messages -f = print failure messages + -n = print number of diff lines -d = don't print diffs on failure if codepath is a file test it, if codepath is a directory test all .py files in and below that directory. @@ -98,10 +104,11 @@ jy_exe = 'jython' print_diff = True + print_diff_lines = False print_fail = False print_success = False try: - opts, args = getopt.getopt(sys.argv[1:], 'j:sfdh') + opts, args = getopt.getopt(sys.argv[1:], 'j:sfdhn') except: print usage sys.exit(1) @@ -117,8 +124,10 @@ print_fail = True if o == '-d': print_diff = False + if o == '-n': + print_diff_lines = True if len(args) < 1 or len(args) > 7: print usage sys.exit(1) - main(args[0], jy_exe, print_diff, print_fail, print_success) + main(args[0], jy_exe, print_diff, print_fail, print_success, print_diff_lines) Modified: trunk/sandbox/ast/grammar/Python.g =================================================================== --- trunk/sandbox/ast/grammar/Python.g 2007-12-30 23:50:02 UTC (rev 3917) +++ trunk/sandbox/ast/grammar/Python.g 2007-12-31 16:48:20 UTC (rev 3918) @@ -140,8 +140,9 @@ Target; Targets; Value; - Start; - End; + Lower; + Upper; + Step; SliceOp; UnaryOp; UAdd; @@ -551,7 +552,7 @@ ; //not_test: 'not' not_test | comparison -not_test : 'not'^ not_test +not_test : NOT^ not_test | comparison ; @@ -574,9 +575,9 @@ | ALT_NOTEQUAL | NOTEQUAL | 'in' - | 'not' 'in' -> NotIn + | NOT 'in' -> NotIn | 'is' - | 'is' 'not' -> IsNot + | 'is' NOT -> IsNot ; //expr: xor_expr ('|' xor_expr)* @@ -683,12 +684,12 @@ //subscript: '.' '.' '.' | test | [test] ':' [test] [sliceop] subscript : DOT DOT DOT -> Ellipsis - | t1=test (COLON (t2=test)? (sliceop)?)? -> ^(Subscript ^(Start $t1) ^(End $t2)? ^(SliceOp sliceop)?) - | COLON (test)? (sliceop)? -> ^(Subscript ^(End test)? ^(SliceOp sliceop)?) + | t1=test (COLON (t2=test)? (sliceop)?)? -> ^(Subscript ^(Lower $t1) ^(Upper $t2)? ^(Step sliceop)?) + | COLON (test)? (sliceop)? -> ^(Subscript ^(Upper test)? ^(Step sliceop)?) ; //sliceop: ':' [test] -sliceop : COLON (test)? -> test? +sliceop : COLON (test)? -> ^(SliceOp test)? ; //exprlist: expr (',' expr)* [','] @@ -879,6 +880,8 @@ OR : 'or' ; +NOT : 'not' ; + FLOAT : '.' DIGITS (Exponent)? | DIGITS ('.' (DIGITS (Exponent)?)? | Exponent) Modified: trunk/sandbox/ast/grammar/PythonWalker.g =================================================================== --- trunk/sandbox/ast/grammar/PythonWalker.g 2007-12-30 23:50:02 UTC (rev 3917) +++ trunk/sandbox/ast/grammar/PythonWalker.g 2007-12-31 16:48:20 UTC (rev 3918) @@ -22,7 +22,9 @@ import org.python.antlr.ast.keywordType; import org.python.antlr.ast.modType; import org.python.antlr.ast.operatorType; +import org.python.antlr.ast.sliceType; import org.python.antlr.ast.stmtType; +import org.python.antlr.ast.unaryopType; import org.python.antlr.ast.Assert; import org.python.antlr.ast.Assign; import org.python.antlr.ast.Attribute; @@ -42,12 +44,15 @@ import org.python.antlr.ast.FunctionDef; import org.python.antlr.ast.Global; import org.python.antlr.ast.If; +import org.python.antlr.ast.Index; import org.python.antlr.ast.Import; import org.python.antlr.ast.ImportFrom; import org.python.antlr.ast.ListComp; import org.python.antlr.ast.Module; import org.python.antlr.ast.Name; import org.python.antlr.ast.Num; +import org.python.antlr.ast.Slice; +import org.python.antlr.ast.Subscript; import org.python.antlr.ast.TryExcept; import org.python.antlr.ast.TryFinally; import org.python.antlr.ast.Tuple; @@ -56,9 +61,11 @@ import org.python.antlr.ast.Raise; import org.python.antlr.ast.Return; import org.python.antlr.ast.Str; +import org.python.antlr.ast.UnaryOp; import org.python.antlr.ast.While; import org.python.antlr.ast.Yield; +import java.math.BigInteger; import java.util.Arrays; import java.util.HashMap; import java.util.Iterator; @@ -190,7 +197,7 @@ Num makeNum(PythonTree t) { - debug("Num matched"); + debug("Num matched " + t.getText()); String s = t.getText(); int radix = 10; if (s.startsWith("0x") || s.startsWith("0X")) { @@ -201,25 +208,25 @@ } if (s.endsWith("L") || s.endsWith("l")) { s = s.substring(0, s.length()-1); - //return new Num(t, Py.newLong(new java.math.BigInteger(s, radix))); - return new Num(t, new java.math.BigInteger(s, radix)); + //return new Num(t, Py.newLong(new BigInteger(s, radix))); + return new Num(t, new BigInteger(s, radix)); } int ndigits = s.length(); int i=0; while (i < ndigits && s.charAt(i) == '0') i++; if ((ndigits - i) > 11) { - //return new Num(t, Py.newLong(new java.math.BigInteger(s, radix))); - return new Num(t, new java.math.BigInteger(s, radix)); + //return new Num(t, Py.newLong(new BigInteger(s, radix))); + return new Num(t, new BigInteger(s, radix)); } long l = Long.valueOf(s, radix).longValue(); if (l > 0xffffffffl || (radix == 10 && l > Integer.MAX_VALUE)) { - //return new Num(t, Py.newLong(new java.math.BigInteger(s, radix))); - return new Num(t, new java.math.BigInteger(s, radix)); + //return new Num(t, Py.newLong(new BigInteger(s, radix))); + return new Num(t, new BigInteger(s, radix)); } //return new Num(t, Py.newInteger((int) l)); - return new Num(t, new Long(l)); + return new Num(t, BigInteger.valueOf(l)); } private stmtType makeTryExcept(PythonTree t, List body, List handlers, List orelse, List finBody) { @@ -285,6 +292,17 @@ stmtType[] b = (stmtType[])body.toArray(new stmtType[body.size()]); return new For(t, target, iter, b, o); } + + //FIXME: only handling Num for now. + private exprType negate(exprType o) { + if (o instanceof Num) { + Num num = (Num)o; + BigInteger b = (BigInteger)num.n; + num.n = b.negate(); + return num; + } + return o; + } } module returns [modType mod] @@ -395,6 +413,7 @@ expr_stmt : test[expr_contextType.Load] { + debug("matched expr_stmt:test " + $test.etype); $stmts::statements.add(new Expr($test.start, $test.etype)); } | ^(augassign targ=test[expr_contextType.Store] value=test[expr_contextType.Load]) { @@ -878,7 +897,10 @@ //XXX: could re-use BoolOps discarded above in many cases. $etype = new BoolOp($OR, boolopType.Or, e); } - | ^('not' test[ctype]) + | ^(NOT test[ctype]) { + $etype = new UnaryOp($NOT, unaryopType.Not, $test.etype); + $etype = $test.etype; + } | ^(comp_op left=test[ctype] targs=test[ctype]) { exprType[] targets = new exprType[1]; cmpopType[] ops = new cmpopType[1]; @@ -896,10 +918,9 @@ debug("BinOp matched"); $etype = new BinOp($left.start, left.etype, $binop.op, right.etype); } - | ^(UAdd test[ctype]) - | ^(USub test[ctype]) - | ^(Invert test[ctype]) - | call_expr {$etype = $call_expr.etype;} + | call_expr { + $etype = $call_expr.etype; + } | lambdef ; @@ -992,14 +1013,38 @@ debug("matched DOT in atom: " + $test.etype + "###" + $NAME.text); $etype = new Attribute($DOT, $test.etype, $NAME.text, ctype); } - | ^(SubscriptList subscriptlist test[ctype]) - | ^(Num INT) {$etype = makeNum($INT);} + | ^(SubscriptList subscriptlist test[expr_contextType.Load]) { + //XXX: only handling one subscript for now. + sliceType s; + if ($subscriptlist.sltypes.size() == 0) { + s = null; + } else { + s = (sliceType)$subscriptlist.sltypes.get(0); + } + $etype = new Subscript($SubscriptList, $test.etype, s, ctype); + } + | ^(Num INT) { + $etype = makeNum($INT); + debug("makeNum output: " + $etype); + } | ^(Num LONGINT) {$etype = makeNum($LONGINT);} | ^(Num FLOAT) | ^(Num COMPLEX) | stringlist { $etype = new Str($stringlist.start, extractStrings($stringlist.strings)); } + | ^(USub test[ctype]) { + debug("USub matched " + $test.etype); + //FIXME: need to actually negate this + $etype = negate($test.etype); + } + | ^(UAdd test[ctype]) { + $etype = $test.etype; + } + | ^(Invert test[ctype]) { + //FIXME: need to actually invert this + $etype = $test.etype; + } ; stringlist returns [List strings] @@ -1016,12 +1061,41 @@ lambdef: ^(Lambda varargslist? ^(Body test[expr_contextType.Load])) ; -subscriptlist - : subscript+ +subscriptlist returns [List sltypes] +@init { + List subs = new ArrayList(); +} + : subscript[subs]+ { + $sltypes = subs; + } ; -subscript : Ellipsis - | ^(Subscript (^(Start test[expr_contextType.Load]))? (^(End test[expr_contextType.Load]))? (^(SliceOp test[expr_contextType.Load]?))?) +subscript [List subs] + : Ellipsis + | ^(Subscript (^(Lower start=test[expr_contextType.Load]))? + (^(Upper end=test[expr_contextType.Load]))? (^(Step (^(SliceOp op=test[expr_contextType.Load]))?))?) { + exprType s = null; + exprType e = null; + exprType o = null; + if ($Lower != null) { + s = $start.etype; + } + if ($Upper != null) { + e = $end.etype; + } + if ($Step != null) { + if ($SliceOp != null) { + o = $op.etype; + } + } + + if (o != null || e != null) { + subs.add(new Slice($Subscript, s, e, o)); + } + else if (s != null) { + subs.add(new Index($Subscript, s)); + } + } ; classdef Modified: trunk/sandbox/ast/jastlib.py =================================================================== --- trunk/sandbox/ast/jastlib.py 2007-12-30 23:50:02 UTC (rev 3917) +++ trunk/sandbox/ast/jastlib.py 2007-12-31 16:48:20 UTC (rev 3918) @@ -42,12 +42,9 @@ else: if fname in ("ctx", "ops", "op"): yield tuple([str(node)]) - #elif fname == "ops": - # yield ops[node] - #elif fname == "op": - # yield op[node] elif fname == "n": - yield int(node) + #assuming n is BigInteger -- valid for now. + yield node.intValue() else: yield node This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |