Skip to content

Instantly share code, notes, and snippets.

@lexruee
Last active August 29, 2015 14:19
Show Gist options
  • Save lexruee/9fc2704c2997ad22e40f to your computer and use it in GitHub Desktop.
Save lexruee/9fc2704c2997ad22e40f to your computer and use it in GitHub Desktop.
ugly code
public Variable lookupVariable(String id) {
/*
* A variable lookup can only occur inside a method or a class.
* Everything else is invalid.
*/
/*
* TODO: improve this hack!
*/
Variable variable;
Scopeable currentScope = this.currentScope();
Scopeable outerScope = this.currentScope().getOuterScope();
if((variable = currentScope.lookupVariable(id)) != null){ // check local scope
return variable;
} else if(outerScope != null) { // check class scope / outer scope
Identifier classId;
if((variable = outerScope.lookupVariable(id)) != null) {
return variable;
} else if(outerScope.isClass() && (classId = outerScope.getId()) != null) { // check parent class scope
Klass klass = this.globalScope().lookupClass(classId.toString());
Klass derivedClass;
if(klass.isDerived() && (derivedClass = this.globalScope().lookupClass(klass.getDerivedId())) != null) {
if((variable = derivedClass.lookupVariable(id)) != null) {
return variable;
}
}
}
}
return null;
}
@lexruee
Copy link
Author

lexruee commented Apr 20, 2015

After refactoring the code above becomes:

public Variable lookupVariable(String id) { 
        return this.currentScope().lookupVariable(id);
    }

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment