diff --git a/src/jse/org/luaj/vm2/ast/Name.java b/src/jse/org/luaj/vm2/ast/Name.java index 200ce4a3..11b4acc2 100644 --- a/src/jse/org/luaj/vm2/ast/Name.java +++ b/src/jse/org/luaj/vm2/ast/Name.java @@ -21,11 +21,10 @@ ******************************************************************************/ package org.luaj.vm2.ast; -import org.luaj.vm2.ast.NameScope.NamedVariable; public class Name { public final String name; - public NamedVariable variable; + public Variable variable; public Name(String name) { this.name = name; } diff --git a/src/jse/org/luaj/vm2/ast/NameResolver.java b/src/jse/org/luaj/vm2/ast/NameResolver.java index ae5dab63..fb19a05c 100644 --- a/src/jse/org/luaj/vm2/ast/NameResolver.java +++ b/src/jse/org/luaj/vm2/ast/NameResolver.java @@ -6,7 +6,6 @@ import org.luaj.vm2.LuaValue; import org.luaj.vm2.ast.Exp.Constant; import org.luaj.vm2.ast.Exp.NameExp; import org.luaj.vm2.ast.Exp.VarExp; -import org.luaj.vm2.ast.NameScope.NamedVariable; import org.luaj.vm2.ast.Stat.Assign; import org.luaj.vm2.ast.Stat.FuncDef; import org.luaj.vm2.ast.Stat.GenericFor; @@ -117,8 +116,8 @@ public class NameResolver extends Visitor { name.variable = scope.define(name.name); } - protected NamedVariable resolveNameReference(Name name) { - NamedVariable v = scope.find(name.name); + protected Variable resolveNameReference(Name name) { + Variable v = scope.find(name.name); if ( v.isLocal() && scope.functionNestingCount != v.definingScope.functionNestingCount ) v.isupvalue = true; return v; diff --git a/src/jse/org/luaj/vm2/ast/NameScope.java b/src/jse/org/luaj/vm2/ast/NameScope.java index d999ac2f..12c14c12 100644 --- a/src/jse/org/luaj/vm2/ast/NameScope.java +++ b/src/jse/org/luaj/vm2/ast/NameScope.java @@ -1,3 +1,24 @@ +/******************************************************************************* +* 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.HashMap; @@ -5,7 +26,6 @@ import java.util.HashSet; import java.util.Map; import java.util.Set; -import org.luaj.vm2.LuaValue; public class NameScope { @@ -21,7 +41,7 @@ public class NameScope { LUA_KEYWORDS.add( k[i] ); } - public final Map namedVariables = new HashMap(); + public final Map namedVariables = new HashMap(); public final NameScope outerScope; @@ -40,20 +60,20 @@ public class NameScope { } /** Look up a name. If it is a global name, then throw IllegalArgumentException. */ - public NamedVariable find( String name ) throws IllegalArgumentException { + public Variable find( String name ) throws IllegalArgumentException { validateIsNotKeyword(name); for ( NameScope n = this; n!=null; n=n.outerScope ) if ( n.namedVariables.containsKey(name) ) return n.namedVariables.get(name); - NamedVariable value = new NamedVariable(name); + Variable value = new Variable(name); this.namedVariables.put(name, value); return value; } /** Define a name in this scope. If it is a global name, then throw IllegalArgumentException. */ - public NamedVariable define( String name ) throws IllegalStateException, IllegalArgumentException { + public Variable define( String name ) throws IllegalStateException, IllegalArgumentException { validateIsNotKeyword(name); - NamedVariable value = new NamedVariable(name, this); + Variable value = new Variable(name, this); this.namedVariables.put(name, value); return value; } @@ -62,29 +82,4 @@ public class NameScope { if ( LUA_KEYWORDS.contains(name) ) throw new IllegalArgumentException("name is a keyword: '"+name+"'"); } - - /** Named variable is load, global, or upvalue. */ - public static class NamedVariable { - public final String name; - public final NameScope definingScope; - public boolean isupvalue; - public boolean hasassignments; - public LuaValue initialValue; - /** Global is named variable not associated with a defining scope */ - public NamedVariable(String name) { - this.name = name; - this.definingScope = null; - } - public NamedVariable(String name, NameScope definingScope) { - /** Local variable is defined in a particular scope. */ - this.name = name; - this.definingScope = definingScope; - } - public boolean isLocal() { - return this.definingScope != null; - } - public boolean isConstant() { - return ! hasassignments && initialValue != null; - } - } } diff --git a/src/jse/org/luaj/vm2/ast/Variable.java b/src/jse/org/luaj/vm2/ast/Variable.java new file mode 100644 index 00000000..7405be42 --- /dev/null +++ b/src/jse/org/luaj/vm2/ast/Variable.java @@ -0,0 +1,62 @@ +/******************************************************************************* +* 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 org.luaj.vm2.LuaValue; + +/** Variable is created lua name scopes, and is a named, lua variable that + * either refers to a lua local, global, or upvalue storage location. + */ +public class Variable { + + /** The name as it appears in lua source code */ + public final String name; + + /** The lua scope in which this variable is defined. */ + public final NameScope definingScope; + + /** true if this variable is an upvalue */ + public boolean isupvalue; + + /** true if there are assignments made to this variable */ + public boolean hasassignments; + + /** When hasassignments == false, and the initial value is a constant, this is the initial value */ + public LuaValue initialValue; + + /** Global is named variable not associated with a defining scope */ + public Variable(String name) { + this.name = name; + this.definingScope = null; + } + public Variable(String name, NameScope definingScope) { + /** Local variable is defined in a particular scope. */ + this.name = name; + this.definingScope = definingScope; + } + public boolean isLocal() { + return this.definingScope != null; + } + public boolean isConstant() { + return ! hasassignments && initialValue != null; + } +} \ No newline at end of file diff --git a/src/jse/org/luaj/vm2/ast/Visitor.java b/src/jse/org/luaj/vm2/ast/Visitor.java index 532ec696..adf8a962 100644 --- a/src/jse/org/luaj/vm2/ast/Visitor.java +++ b/src/jse/org/luaj/vm2/ast/Visitor.java @@ -1,3 +1,24 @@ +/******************************************************************************* +* 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; diff --git a/src/jse/org/luaj/vm2/lua2java/JavaCodeGen.java b/src/jse/org/luaj/vm2/lua2java/JavaCodeGen.java index b8e8401d..5350d3bf 100644 --- a/src/jse/org/luaj/vm2/lua2java/JavaCodeGen.java +++ b/src/jse/org/luaj/vm2/lua2java/JavaCodeGen.java @@ -39,6 +39,7 @@ import org.luaj.vm2.ast.FuncArgs; import org.luaj.vm2.ast.FuncBody; import org.luaj.vm2.ast.Name; import org.luaj.vm2.ast.NameResolver; +import org.luaj.vm2.ast.Variable; import org.luaj.vm2.ast.ParList; import org.luaj.vm2.ast.Stat; import org.luaj.vm2.ast.TableConstructor; @@ -56,7 +57,6 @@ import org.luaj.vm2.ast.Exp.ParensExp; import org.luaj.vm2.ast.Exp.UnopExp; import org.luaj.vm2.ast.Exp.VarExp; import org.luaj.vm2.ast.Exp.VarargsExp; -import org.luaj.vm2.ast.NameScope.NamedVariable; import org.luaj.vm2.ast.Stat.Assign; import org.luaj.vm2.ast.Stat.Break; import org.luaj.vm2.ast.Stat.FuncCallStat; @@ -352,7 +352,7 @@ public class JavaCodeGen { singleLocalDeclareAssign( name.variable, value ); } - private void singleLocalDeclareAssign(NamedVariable variable, String value) { + private void singleLocalDeclareAssign(Variable variable, String value) { String javaname = javascope.getJavaName(variable); if ( variable.isConstant() ) return; @@ -808,7 +808,7 @@ public class JavaCodeGen { singleLocalDeclareAssign( name, value ); } if ( body.parlist.isvararg ) { - NamedVariable arg = body.scope.find("arg"); + Variable arg = body.scope.find("arg"); javascope.setJavaName(arg,"arg"); if ( m > 0 ) outl( "$arg = $arg.subargs("+(m+1)+");" ); diff --git a/src/jse/org/luaj/vm2/lua2java/JavaScope.java b/src/jse/org/luaj/vm2/lua2java/JavaScope.java index d6d4fdbf..bc59c3e6 100644 --- a/src/jse/org/luaj/vm2/lua2java/JavaScope.java +++ b/src/jse/org/luaj/vm2/lua2java/JavaScope.java @@ -30,6 +30,7 @@ import org.luaj.vm2.ast.Block; import org.luaj.vm2.ast.Chunk; import org.luaj.vm2.ast.FuncBody; import org.luaj.vm2.ast.NameScope; +import org.luaj.vm2.ast.Variable; import org.luaj.vm2.ast.Visitor; import org.luaj.vm2.ast.Exp.BinopExp; import org.luaj.vm2.ast.Exp.VarargsExp; @@ -96,7 +97,7 @@ public class JavaScope extends NameScope { return (JavaScope) outerScope; } - final String getJavaName(NamedVariable nv) { + final String getJavaName(Variable nv) { for ( JavaScope s = this; s != null; s = (JavaScope) s.outerScope ) if ( s.astele2javaname.containsKey(nv) ) return s.astele2javaname.get(nv); @@ -114,7 +115,7 @@ public class JavaScope extends NameScope { } } - public void setJavaName(NamedVariable astele, String javaname) { + public void setJavaName(Variable astele, String javaname) { javanames.add(javaname); astele2javaname.put(astele,javaname); }