Create visitor for ast

This commit is contained in:
James Roseborough
2010-06-23 15:00:12 +00:00
parent bd51839b2f
commit f50d7eb7d3
18 changed files with 413 additions and 109 deletions

View File

@@ -231,14 +231,14 @@ Stat LastStat():
Stat ExprStat():
{
PrimaryExp pe;
Assign as=null;
Stat as=null;
}
{
pe=PrimaryExp() ( as=Assign(assertvarexp(pe)) )?
{ return as==null? Stat.functioncall(assertfunccall(pe)): Stat.assignment(as); }
{ return as==null? Stat.functioncall(assertfunccall(pe)): as; }
}
Assign Assign(VarExp v0):
Stat Assign(VarExp v0):
{
List<VarExp> vl = new ArrayList<VarExp>();
vl.add(v0);
@@ -246,7 +246,7 @@ Assign Assign(VarExp v0):
List<Exp> el;
}
{
( "," ve=VarExp() { vl.add(ve); } )* "=" el=ExpList() { return new Assign(vl,el); }
( "," ve=VarExp() { vl.add(ve); } )* "=" el=ExpList() { return Stat.assignment(vl,el); }
}
VarExp VarExp():
@@ -337,16 +337,17 @@ Exp SimpleExp():
LuaString s;
Exp e;
TableConstructor tc;
FuncBody fb;
}
{
<NIL> { return Exp.constant(LuaValue.NIL); }
| <TRUE> { return Exp.constant(LuaValue.TRUE); }
| <FALSE> { return Exp.constant(LuaValue.FALSE); }
| n=<NUMBER> { return Exp.constant(LuaValue.valueOf(Double.valueOf(n.image))); }
| s=Str() { return Exp.constant(s); }
| "..." { return Exp.varargs(); }
<NIL> { return Exp.constant(LuaValue.NIL); }
| <TRUE> { return Exp.constant(LuaValue.TRUE); }
| <FALSE> { return Exp.constant(LuaValue.FALSE); }
| n=<NUMBER> { return Exp.constant(LuaValue.valueOf(Double.valueOf(n.image))); }
| s=Str() { return Exp.constant(s); }
| "..." { return Exp.varargs(); }
| tc=TableConstructor() { return Exp.tableconstructor(tc); }
| Function() { return null; }
| fb=Function() { return Exp.anonymousfunction(fb); }
| e=PrimaryExp() { return e; }
}
@@ -382,22 +383,21 @@ FuncBody Function():
FuncBody FuncBody():
{
FuncBody fb = new FuncBody();
ParList pl;
ParList pl=null;
Block b;
}
{
"(" ( pl=ParList() { fb.parlist=pl; } )? ")" b=Block() { fb.block=b; } <END> { return fb; }
"(" ( pl=ParList() )? ")" b=Block() <END> { return new FuncBody(pl,b); }
}
ParList ParList():
{
ParList pl = new ParList();
List<Name> nl;
List<Name> nl=null;
boolean v=false;
}
{
nl=NameList() { pl.namelist=nl; } ( "," "..." { pl.isvararg=true; } )? { return pl; }
| "..." { pl.isvararg=true; return pl; }
nl=NameList() ( "," "..." { v=true; } )? { return new ParList(nl,v); }
| "..." { return new ParList(null,true); ; }
}
TableConstructor TableConstructor():

View File

@@ -1,32 +0,0 @@
/*******************************************************************************
* Copyright (c) 2010 Luaj.org. All rights reserved.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
******************************************************************************/
package org.luaj.vm2.ast;
import java.util.List;
public class Assign extends Exp {
public Assign(List<VarExp> vl, List<Exp> el) {
// TODO Auto-generated constructor stub
}
}

View File

@@ -24,14 +24,17 @@ package org.luaj.vm2.ast;
import java.util.ArrayList;
import java.util.List;
public class Block {
public class Block extends Stat {
public List<Stat> stats;
public List<Stat> stats = new ArrayList<Stat>();
public void add(Stat s) {
if ( stats == null )
stats = new ArrayList<Stat>();
if ( s == null )
return;
stats.add(s);
}
public void accept(Visitor visitor) {
visitor.visit(this);
}
}

View File

@@ -27,4 +27,8 @@ public class Chunk {
public Chunk(Block b) {
this.block = b;
}
public void accept( Visitor visitor ) {
visitor.visit( this );
}
}

View File

@@ -23,31 +23,87 @@ package org.luaj.vm2.ast;
import org.luaj.vm2.LuaValue;
abstract
public class Exp {
abstract public void accept(Visitor visitor);
public static Exp constant(LuaValue value) {
// TODO Auto-generated method stub
return null;
return new Constant(value);
}
public static Exp varargs() {
// TODO Auto-generated method stub
return null;
return new VarargsExp();
}
public static Exp tableconstructor(TableConstructor tc) {
// TODO Auto-generated method stub
return null;
return tc;
}
public static Exp unaryexp(int op, Exp s) {
// TODO Auto-generated method stub
return null;
public static Exp unaryexp(int op, Exp rhs) {
return new UnopExp(op, rhs);
}
public static Exp binaryexp(Exp e, int op, Exp s) {
// TODO Auto-generated method stub
return null;
public static Exp binaryexp(Exp lhs, int op, Exp rhs) {
return new BinopExp(lhs, op, rhs);
}
public static Exp anonymousfunction(FuncBody funcbody) {
return new AnonFuncDef(funcbody);
}
public static class Constant extends Exp {
public final LuaValue value;
public Constant(LuaValue value) {
this.value = value;
}
public void accept(Visitor visitor) {
visitor.visit(this);
}
}
public static class VarargsExp extends Exp {
public void accept(Visitor visitor) {
visitor.visit(this);
}
}
public static class UnopExp extends Exp {
public final int op;
public final Exp rhs;
public UnopExp(int op, Exp rhs) {
this.op = op;
this.rhs = rhs;
}
public void accept(Visitor visitor) {
visitor.visit(this);
}
}
public static class BinopExp extends Exp {
public final Exp lhs,rhs;
public final int op;
public BinopExp(Exp lhs, int op, Exp rhs) {
this.lhs = lhs;
this.op = op;
this.rhs = rhs;
}
public void accept(Visitor visitor) {
visitor.visit(this);
}
}
public static class AnonFuncDef extends Exp {
public final FuncBody funcbody;
public AnonFuncDef(FuncBody funcbody) {
this.funcbody = funcbody;
}
public void accept(Visitor visitor) {
visitor.visit(this);
}
}
}

View File

@@ -44,4 +44,8 @@ public class Field {
public static Field listField(Exp rhs) {
return new Field(null, null, rhs);
}
public void accept(Visitor visitor) {
visitor.visit(this);
}
}

View File

@@ -21,25 +21,46 @@
******************************************************************************/
package org.luaj.vm2.ast;
import java.util.ArrayList;
import java.util.List;
import org.luaj.vm2.LuaString;
public class FuncArgs {
public final List<Exp> exps;
/** exp1,exp2... */
public static FuncArgs explist(List<Exp> explist) {
return null;
return new FuncArgs(explist);
}
/** {...} */
public static FuncArgs tableconstructor(TableConstructor table) {
return null;
return new FuncArgs(table);
}
/** "mylib" */
public static FuncArgs string(LuaString s) {
return null;
public static FuncArgs string(LuaString string) {
return new FuncArgs(string);
}
public FuncArgs(List<Exp> exps) {
this.exps = exps;
}
public FuncArgs(LuaString string) {
this.exps = new ArrayList<Exp>();
this.exps.add( Exp.constant(string) );
}
public FuncArgs(TableConstructor table) {
this.exps = new ArrayList<Exp>();
this.exps.add( table );
}
public void accept(Visitor visitor) {
visitor.visit(this);
}
}

View File

@@ -24,4 +24,12 @@ package org.luaj.vm2.ast;
public class FuncBody {
public ParList parlist;
public Block block;
public FuncBody(ParList parlist, Block block) {
this.parlist = parlist!=null? parlist: ParList.EMPTY_PARLIST;
this.block = block;
}
public void accept(Visitor visitor) {
visitor.visit(this);
}
}

View File

@@ -23,4 +23,8 @@ package org.luaj.vm2.ast;
public class FuncCall extends PrimaryExp {
public void accept(Visitor visitor) {
visitor.visit(this);
}
}

View File

@@ -28,7 +28,7 @@ public class FuncName {
// example: a.b.c.d:e
// initial base name: "a"
public final String base;
public final String name;
// intermediate field accesses: "b", "c", "d"
public List<String> dots;
@@ -36,8 +36,8 @@ public class FuncName {
// optional final method name: "e"
public String method;
public FuncName( String base ) {
this.base = base;
public FuncName( String name ) {
this.name = name;
}
public void adddot(String dot) {

View File

@@ -22,8 +22,8 @@
package org.luaj.vm2.ast;
public class Name {
public final String luaname;
public Name(String luaname) {
this.luaname = luaname;
public final String name;
public Name(String name) {
this.name = name;
}
}

View File

@@ -21,9 +21,22 @@
******************************************************************************/
package org.luaj.vm2.ast;
import java.util.ArrayList;
import java.util.List;
public class ParList {
public List<Name> namelist;
public boolean isvararg;
public static final List<Name> EMPTY_NAMELIST = new ArrayList<Name>();
public static final ParList EMPTY_PARLIST = new ParList(EMPTY_NAMELIST,false);
public final List<Name> names;
public final boolean isvararg;
public ParList(List<Name> names, boolean isvararg) {
this.names = names;
this.isvararg = isvararg;
}
public void accept(Visitor visitor) {
visitor.visit(this);
}
}

View File

@@ -55,4 +55,7 @@ public class PostfixOp {
return false;
}
public void accept(Visitor visitor) {
visitor.visit(this);
}
}

View File

@@ -51,4 +51,7 @@ public class PrimaryExp extends Exp {
return ops != null && ops.get(ops.size()-1).isfunccall();
}
public void accept(Visitor visitor) {
visitor.visit(this);
}
}

View File

@@ -23,71 +23,217 @@ package org.luaj.vm2.ast;
import java.util.List;
abstract
public class Stat {
public abstract void accept(Visitor visitor);
public static Stat block(Block block) {
// TODO Auto-generated method stub
return null;
return block;
}
public static Stat whiledo(Exp exp, Block block) {
// TODO Auto-generated method stub
return null;
return new WhileDo(exp, block);
}
public static Stat repeatuntil(Block block, Exp exp) {
// TODO Auto-generated method stub
return null;
return new RepeatUntil(block, exp);
}
public static Stat breakstat() {
// TODO Auto-generated method stub
return null;
return new Break();
}
public static Stat returnstat(List<Exp> exps) {
// TODO Auto-generated method stub
return null;
return new Return(exps);
}
public static Stat assignment(Assign assign) {
// TODO Auto-generated method stub
return null;
public static Stat assignment(List<VarExp> vars, List<Exp> exps) {
return new Assign(vars,exps);
}
public static Stat functioncall(FuncCall exp) {
// TODO Auto-generated method stub
return null;
public static Stat functioncall(FuncCall funccall) {
return new FuncCallStat(funccall);
}
public static Stat localfunctiondef(String name, FuncBody funcbody) {
// TODO Auto-generated method stub
return null;
return new LocalFuncDef(name, funcbody);
}
public static Stat fornumeric(String name, Exp initial, Exp limit, Exp step, Block block) {
// TODO Auto-generated method stub
return null;
return new NumericFor(name, initial, limit, step, block);
}
public static Stat functiondef(FuncName funcname, FuncBody funcbody) {
// TODO Auto-generated method stub
return null;
return new FuncDef( funcname, funcbody );
}
public static Stat forgeneric(List<Name> names, List<Exp> exps, Block block) {
// TODO Auto-generated method stub
return null;
return new GenericFor(names, exps, block);
}
public static Stat localassignment(List<Name> names, List<Exp> values) {
// TODO Auto-generated method stub
return null;
return new LocalAssign(names, values);
}
public static Stat ifthenelse(Exp ifexp, Block ifblock, List<Exp> elseifexps, List<Block> elseifblocks, Block elseblock) {
// TODO Auto-generated method stub
return null;
return new IfThenElse(ifexp, ifblock, elseifexps, elseifblocks, elseblock);
}
public static class Assign extends Stat {
public final List<VarExp> vars;
public final List<Exp> exps;
public Assign(List<VarExp> vars, List<Exp> exps) {
this.vars = vars;
this.exps = exps;
}
public void accept(Visitor visitor) {
visitor.visit(this);
}
}
public static class WhileDo extends Stat {
public final Exp exp;
public final Block block;
public WhileDo( Exp exp, Block block ) {
this.exp = exp;
this.block = block;
}
public void accept(Visitor visitor) {
visitor.visit( this );
}
}
public static class RepeatUntil extends Stat {
public final Block block;
public final Exp exp;
public RepeatUntil( Block block, Exp exp ) {
this.block = block;
this.exp = exp;
}
public void accept(Visitor visitor) {
visitor.visit( this );
}
}
public static class Break extends Stat {
public void accept(Visitor visitor) {
visitor.visit( this );
}
}
public static class Return extends Stat {
public final List<Exp> values;
public Return(List<Exp> values) {
this.values = values;
}
public void accept(Visitor visitor) {
visitor.visit( this );
}
}
public static class FuncCallStat extends Stat {
public final FuncCall funccall;
public FuncCallStat(FuncCall funccall) {
this.funccall = funccall;
}
public void accept(Visitor visitor) {
visitor.visit( this );
}
}
public static class LocalFuncDef extends Stat {
public final Name name;
public final FuncBody body;
public LocalFuncDef(String name, FuncBody body) {
this.name = new Name(name);
this.body = body;
}
public void accept(Visitor visitor) {
visitor.visit( this );
}
}
public static class FuncDef extends Stat {
public final FuncName name;
public final FuncBody body;
public FuncDef(FuncName name, FuncBody body) {
this.name = name;
this.body = body;
}
public void accept(Visitor visitor) {
visitor.visit( this );
}
}
public static class GenericFor extends Stat {
public List<Name> names;
public List<Exp> exps;
public Block block;
public GenericFor(List<Name> names, List<Exp> exps, Block block) {
this.names = names;
this.exps = exps;
this.block = block;
}
public void accept(Visitor visitor) {
visitor.visit( this );
}
}
public static class NumericFor extends Stat {
public final Name name;
public final Exp initial,limit,step;
public final Block block;
public NumericFor(String name, Exp initial, Exp limit, Exp step, Block block) {
this.name = new Name(name);
this.initial = initial;
this.limit = limit;
this.step = step;
this.block = block;
}
public void accept(Visitor visitor) {
visitor.visit( this );
}
}
public static class LocalAssign extends Stat {
public final List<Name> names;
public final List<Exp> values;
public LocalAssign(List<Name> names, List<Exp> values) {
this.names = names;
this.values = values;
}
public void accept(Visitor visitor) {
visitor.visit( this );
}
}
public static class IfThenElse extends Stat {
public final Exp ifexp;
public final Block ifblock;
public final List<Exp> elseifexps;
public final List<Block> elseifblocks;
public final Block elseblock;
public IfThenElse(Exp ifexp, Block ifblock, List<Exp> elseifexps,
List<Block> elseifblocks, Block elseblock) {
this.ifexp = ifexp;
this.ifblock = ifblock;
this.elseifexps = elseifexps;
this.elseifblocks = elseifblocks;
this.elseblock = elseblock;
}
public void accept(Visitor visitor) {
visitor.visit( this );
}
}
}

View File

@@ -25,4 +25,8 @@ import java.util.List;
public class TableConstructor extends Exp {
public List<Field> fields;
public void accept(Visitor visitor) {
visitor.visit(this);
}
}

View File

@@ -22,4 +22,7 @@
package org.luaj.vm2.ast;
public class VarExp extends PrimaryExp {
public void accept(Visitor visitor) {
visitor.visit(this);
}
}

View File

@@ -0,0 +1,64 @@
package org.luaj.vm2.ast;
abstract public class Visitor {
public void visit(Chunk chunk) {
chunk.block.accept(this);
};
public void visit(Block block) {
if ( block.stats != null )
for ( Stat s : block.stats )
s.accept( this );
};
public void visit(Stat.Assign assign) {
}
public void visit(Stat.Break breakstat) {
}
public void visit(Stat.FuncCallStat stat) {
}
public void visit(Stat.FuncDef stat) {
}
public void visit(Stat.GenericFor stat) {
}
public void visit(Stat.IfThenElse stat) {
}
public void visit(Stat.LocalAssign stat) {
}
public void visit(Stat.LocalFuncDef stat) {
}
public void visit(Stat.NumericFor stat) {
}
public void visit(Stat.RepeatUntil stat) {
}
public void visit(Stat.Return stat) {
}
public void visit(Stat.WhileDo stat) {
}
public void visit(FuncCall funcCall) {
}
public void visit(FuncBody funcBody) {
}
public void visit(FuncArgs funcArgs) {
}
public void visit(Field field) {
}
public void visit(Exp exp) {
}
public void visit(Exp.AnonFuncDef exp) {
}
public void visit(Exp.BinopExp exp) {
}
public void visit(Exp.Constant exp) {
}
public void visit(Exp.UnopExp exp) {
}
public void visit(Exp.VarargsExp exp) {
}
public void visit(PrimaryExp exp) {
}
public void visit(ParList parList) {
}
public void visit(PostfixOp postfixOp) {
}
public void visit(VarExp exp) {
}
}