From: Philip C. <Phil.Cannata@Sun.COM> - 2010-01-04 17:32:24
|
At 05:40 PM 12/31/2009, Jim Baker wrote: >Here's my recommendation: > * Modify python.g to add support for the > select statement, as you've done with > query_stmt stuff you quote below. You don't > want to have the parser actually do things like > print, it's just not going to work too well. > And in particular, the parser has no easy > access to runtime values. You would have to at > least consider issues like PySystemState, etc., to find them. > * If you want to add AST > introspection/mutability, you probably need to > write an adapter in org.python.antlr.adapter. > (Frank would certainly know for certain on this > part.) This is certainly overkill for now. > * In org.python.compiler.CodeCompiler, add a > method to visit this query node. Something like visitQueryStatement. If I try to add something like @Override public Object visitQuery(Query node) throws Exception { . . . to CodeCompiler I need to have Query defined or I get compile: [javac] Compiling 526 source files to c:\MyJython\jython\jython\build\classes [javac] c:\MyJython\jython\jython\src\org\python\compiler\CodeCompiler.java:2286: cannot find symbol [javac] symbol : class Query [javac] location: class org.python.compiler.CodeCompiler [javac] public Object visitQuery(Query node) throws Exception { Where do I define the Query node? I tried adding Query.java to src\org\python\antlr\ast but that didn't do it. thanks phil > * This will be used to define the query > statement, which in Python is always a dynamic > construct. See visitClassDef for something > similar. You can add support to the Jython > runtime, or probably better just create an > appropriate class in Python. The easiest way to > do is do something like code.invokestatic(... > instantiate a QueryStatement object with slots > ...) - I'll let you figure out the signature. > One good example of this melding of Python code > into Java code is seen in > org.python.core.PyBytecode, which loads up the > dis module to support the dissassembly of ops for debugging purposes. > * Write your QueryStatement class such that > its __repr__ special method prints out as desired. > >That should do it. > >- Jim > >On Wed, Dec 30, 2009 at 10:07 PM, Philip Cannata ><<mailto:Phi...@su...>Phi...@su...> wrote: >Frank and Marc, thanks for your time. Let me explain a little bit >what I'm trying to do. For teaching purposes, I like to teach my >students to modify the grammar first before proceeding to the AST >etc. So, with the small change to Python.g below I've gotten the >following to be recognized: > >>> a=111 > >>> b=a+222 > >>> SELECT * FROM emp WHERE x = b >SELECT Statement is: SELECT *  FROM emp, WHERE x = b > >but, I'd like to get it to print out: >SELECT Statement is: SELECT *  FROM emp, WHERE x = 333 >by getting the value of "b" which I believe has already been parsed >and turned into a real object and therefore has a value when I'm >parsing the SELECT line. (i.e., I need something in place of $e.text). > >Here's the change to Python.g starting at line 568: >//small_stmt: (expr_stmt | print_stmt  | del_stmt | pass_stmt | flow_stmt | >//       import_stmt | global_stmt | exec_stmt | assert_stmt) >small_stmt : expr_stmt >      | print_stmt >      | del_stmt >      | pass_stmt >      | flow_stmt >      | import_stmt >      | global_stmt >      | exec_stmt >      | assert_stmt >      | query_stmt >      ; >//query_stmt: SELECT * FROM person WHERE x = value >query_stmt >  : SELECT STAR CAPSFROM n1 = NAME ( WHERE >n2 = NAME ASSIGN e = expr_stmt )? >    { System.out.println("SELECT Statement is: SELECT *  FROM " >+ $n1.text + ", WHERE " + $n2.text + " = " + $e.text); } >  -> ^(SELECT<Pass>[$SELECT]) >  ; > >At 03:35 PM 12/30/2009, Frank Wierzbicki wrote: > >On Wed, Dec 30, 2009 at 4:08 PM, Philip > Cannata <<mailto:Phi...@su...>Phi...@su...> wrote: > > > Yes, but if I put it in Python.g at line 639 as follows: > >Probably Python.g is not the place you want to be putting this, since > >the x in "x = a + b" is only a symbol from the viewpoint of parsing > >(which is all Python.g does).  The way you get from source to > >something at runtime goes roughly: > > > >Source --- (parse) ---> AST --- (compile) ---> Java bytecodes --- > >(class loading) ---> real objects ready to run --- (interpreter) ---> > >actual values like x = 333 > > > >-Frank > > >------------------------------------------------------------------------------ >This SF.Net email is sponsored by the Verizon Developer Community >Take advantage of Verizon's best-in-class app development support >A streamlined, 14 day to market process makes app distribution fast and easy >Join now and get one step closer to millions of Verizon customers ><http://p.sf.net/sfu/verizon-dev2dev>http://p.sf.net/sfu/verizon-dev2dev >_______________________________________________ >Jython-dev mailing list ><mailto:Jyt...@li...>Jyt...@li... >https://lists.sourceforge.net/lists/listinfo/jython-dev > > > > >-- >Jim Baker ><mailto:jb...@zy...>jb...@zy... |