Let Variable class be a top level class.
This commit is contained in:
@@ -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;
|
||||
}
|
||||
|
||||
@@ -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;
|
||||
|
||||
@@ -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<String,NamedVariable> namedVariables = new HashMap<String,NamedVariable>();
|
||||
public final Map<String,Variable> namedVariables = new HashMap<String,Variable>();
|
||||
|
||||
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;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
62
src/jse/org/luaj/vm2/ast/Variable.java
Normal file
62
src/jse/org/luaj/vm2/ast/Variable.java
Normal file
@@ -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;
|
||||
}
|
||||
}
|
||||
@@ -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;
|
||||
|
||||
@@ -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)+");" );
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user