a)
Obj p = curScope.locals, last = null;
while (p != null) {
if (p.name.equals(name)) error(name + " declared twice");
last = p; p = p.next;
}
if (last == null) curScope.locals = obj; else last.next = obj;
b)
if (curScope.locals == null) {
curScope.locals = obj;
} else {
Obj p = curScope.locals;
while (true) {
if (p.name.equals(name))
error(name + "declared twice");
if (p.next == null)
break;
p = p.next;
}
p.next = obj;
}
Refactorings
No refactoring yet !
foo
March 1, 2010, March 01, 2010 00:38, permalink
Obj lookup(String name) {
for (Obj p = curScope.locals; p != null; p = p.next)
if (p.name.equals(name))
return p;
return null;
}
Obj insert(String name) {
Obj found = lookup(name);
if (found)
error(name + " declared twice");
// else
if (curScope.locals == null) {
curScope.locals = found;
found.next = null;
} else {
found.next = curScore.locals.next;
curScore.locals.next = found;
}
}
I got this:
a) // see below
to this:
b) // see below
do you have a better c) ?