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(): Stat ExprStat():
{ {
PrimaryExp pe; PrimaryExp pe;
Assign as=null; Stat as=null;
} }
{ {
pe=PrimaryExp() ( as=Assign(assertvarexp(pe)) )? 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>(); List<VarExp> vl = new ArrayList<VarExp>();
vl.add(v0); vl.add(v0);
@@ -246,7 +246,7 @@ Assign Assign(VarExp v0):
List<Exp> el; 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(): VarExp VarExp():
@@ -337,6 +337,7 @@ Exp SimpleExp():
LuaString s; LuaString s;
Exp e; Exp e;
TableConstructor tc; TableConstructor tc;
FuncBody fb;
} }
{ {
<NIL> { return Exp.constant(LuaValue.NIL); } <NIL> { return Exp.constant(LuaValue.NIL); }
@@ -346,7 +347,7 @@ Exp SimpleExp():
| s=Str() { return Exp.constant(s); } | s=Str() { return Exp.constant(s); }
| "..." { return Exp.varargs(); } | "..." { return Exp.varargs(); }
| tc=TableConstructor() { return Exp.tableconstructor(tc); } | tc=TableConstructor() { return Exp.tableconstructor(tc); }
| Function() { return null; } | fb=Function() { return Exp.anonymousfunction(fb); }
| e=PrimaryExp() { return e; } | e=PrimaryExp() { return e; }
} }
@@ -382,22 +383,21 @@ FuncBody Function():
FuncBody FuncBody(): FuncBody FuncBody():
{ {
FuncBody fb = new FuncBody(); ParList pl=null;
ParList pl;
Block b; 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 ParList():
{ {
ParList pl = new ParList(); List<Name> nl=null;
List<Name> nl; boolean v=false;
} }
{ {
nl=NameList() { pl.namelist=nl; } ( "," "..." { pl.isvararg=true; } )? { return pl; } nl=NameList() ( "," "..." { v=true; } )? { return new ParList(nl,v); }
| "..." { pl.isvararg=true; return pl; } | "..." { return new ParList(null,true); ; }
} }
TableConstructor TableConstructor(): 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.ArrayList;
import java.util.List; 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) { public void add(Stat s) {
if ( stats == null ) if ( s == null )
stats = new ArrayList<Stat>(); return;
stats.add(s); stats.add(s);
}
public void accept(Visitor visitor) {
visitor.visit(this);
} }
} }

View File

@@ -27,4 +27,8 @@ public class Chunk {
public Chunk(Block b) { public Chunk(Block b) {
this.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; import org.luaj.vm2.LuaValue;
abstract
public class Exp { public class Exp {
abstract public void accept(Visitor visitor);
public static Exp constant(LuaValue value) { public static Exp constant(LuaValue value) {
// TODO Auto-generated method stub return new Constant(value);
return null;
} }
public static Exp varargs() { public static Exp varargs() {
// TODO Auto-generated method stub return new VarargsExp();
return null;
} }
public static Exp tableconstructor(TableConstructor tc) { public static Exp tableconstructor(TableConstructor tc) {
// TODO Auto-generated method stub return tc;
return null;
} }
public static Exp unaryexp(int op, Exp s) { public static Exp unaryexp(int op, Exp rhs) {
// TODO Auto-generated method stub return new UnopExp(op, rhs);
return null;
} }
public static Exp binaryexp(Exp e, int op, Exp s) { public static Exp binaryexp(Exp lhs, int op, Exp rhs) {
// TODO Auto-generated method stub return new BinopExp(lhs, op, rhs);
return null;
} }
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) { public static Field listField(Exp rhs) {
return new Field(null, null, 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; package org.luaj.vm2.ast;
import java.util.ArrayList;
import java.util.List; import java.util.List;
import org.luaj.vm2.LuaString; import org.luaj.vm2.LuaString;
public class FuncArgs { public class FuncArgs {
public final List<Exp> exps;
/** exp1,exp2... */ /** exp1,exp2... */
public static FuncArgs explist(List<Exp> explist) { public static FuncArgs explist(List<Exp> explist) {
return null; return new FuncArgs(explist);
} }
/** {...} */ /** {...} */
public static FuncArgs tableconstructor(TableConstructor table) { public static FuncArgs tableconstructor(TableConstructor table) {
return null; return new FuncArgs(table);
} }
/** "mylib" */ /** "mylib" */
public static FuncArgs string(LuaString s) { public static FuncArgs string(LuaString string) {
return null; 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 class FuncBody {
public ParList parlist; public ParList parlist;
public Block block; 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 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 // example: a.b.c.d:e
// initial base name: "a" // initial base name: "a"
public final String base; public final String name;
// intermediate field accesses: "b", "c", "d" // intermediate field accesses: "b", "c", "d"
public List<String> dots; public List<String> dots;
@@ -36,8 +36,8 @@ public class FuncName {
// optional final method name: "e" // optional final method name: "e"
public String method; public String method;
public FuncName( String base ) { public FuncName( String name ) {
this.base = base; this.name = name;
} }
public void adddot(String dot) { public void adddot(String dot) {

View File

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

View File

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

View File

@@ -22,4 +22,7 @@
package org.luaj.vm2.ast; package org.luaj.vm2.ast;
public class VarExp extends PrimaryExp { 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) {
}
}