ANTLR + ASM = my sad attempts at SQL to java bytecode compiler.
0 posts in topic
Flat View  Flat View
TOPIC ACTIONS:
 

Posted By:   Andy_Sonnenburg
Posted On:   Tuesday, March 24, 2009 09:03 AM

I am trying to have basic support for a SQL where clause to filter a given collection of java objects. Java properties are determined through getter methods. The only thing it needs to support now are if statements (and, not, etc.) and null checks (is null, is not null). What I have working is: Given: id is not null and test is null and msg is null Produces AST: (and (and (not (is tid null)) (is test null)) (is msg null)) Now I need to generated the bytecode. My grammar is below. If anyone already knows a better way to handle binary operations (nonexistent in bytecode), please let me know how (or post a link to where). Thanks!    More>>

I am trying to have basic support for a SQL where clause to filter a given collection of java objects. Java properties are determined through getter methods. The only thing it needs to support now are if statements (and, not, etc.) and null checks (is null, is not null).

What I have working is:



Given:



			
id is not null and test is null and msg is null


Produces AST:



			
(and (and (not (is tid null)) (is test null)) (is msg null))


Now I need to generated the bytecode. My grammar is below. If anyone already knows a better way to handle binary operations (nonexistent in bytecode), please let me know how (or post a link to where).



Thanks!



			
tree grammar Eval;

options
{
language = Java;
tokenVocab = Expr;
ASTLabelType = CommonTree;
}

@header
{
package stubs;

import org.objectweb.asm.*;
import org.objectweb.asm.commons.*;
}

expression[GeneratorAdapter adapter]
scope { GeneratorAdapter g; }
:
{
$expression::g = adapter;
Label returnTrue = $expression::g.newLabel();
Label returnFalse = $expression::g.newLabel();
}
expr[returnTrue, returnFalse]
{
$expression::g.mark(returnTrue);
$expression::g.push(true);
$expression::g.returnValue();
$expression::g.mark(returnFalse);
$expression::g.push(false);
$expression::g.returnValue();
};

expr[Label returnTrue, Label returnFalse]
: ^(OR
{
Label next = $expression::g.newLabel();
}
expr[returnTrue, next]
{
$expression::g.mark(next);
}
expr[returnTrue, returnFalse])
| ^(AND
{
Label next = $expression::g.newLabel();
}
expr[next, returnFalse]
{
$expression::g.mark(next);
}
expr[returnTrue, returnFalse])
| ^(NOT expr[returnFalse, returnTrue])
| ^(IS ID NULL
{
/* invoke $ID getter property on element, then: */
$expression::g.ifNull(returnTrue);
$expression::g.ifNonNull(returnFalse);
});
   <<Less
About | Sitemap | Contact