[Nice-commit] Nice/src/bossa/syntax typecheck.nice,1.84,1.85
Brought to you by:
bonniot
From: <ar...@us...> - 2003-11-20 20:50:33
|
Update of /cvsroot/nice/Nice/src/bossa/syntax In directory sc8-pr-cvs1:/tmp/cvs-serv25298/F:/nice/src/bossa/syntax Modified Files: typecheck.nice Log Message: fix of type checking in anonymous functions. Index: typecheck.nice =================================================================== RCS file: /cvsroot/nice/Nice/src/bossa/syntax/typecheck.nice,v retrieving revision 1.84 retrieving revision 1.85 diff -C2 -d -r1.84 -r1.85 *** typecheck.nice 20 Nov 2003 00:32:41 -0000 1.84 --- typecheck.nice 20 Nov 2003 20:50:30 -0000 1.85 *************** *** 43,47 **** let variable = localVariable(to); if (variable != null && Node.getCurrentFunction() instanceof FunExp) ! variable.captured = true; try{ --- 43,47 ---- let variable = localVariable(to); if (variable != null && Node.getCurrentFunction() instanceof FunExp) ! setCaptured(variable); try{ *************** *** 234,238 **** ?Function saved = Node.getCurrentFunction(); Node.setCurrentFunction(e); ! typecheck(e.body); Node.setCurrentFunction(saved); } --- 234,246 ---- ?Function saved = Node.getCurrentFunction(); Node.setCurrentFunction(e); ! ! enterClosure(); ! try { ! typecheck(e.body); ! } ! finally { ! exitClosure(); ! } ! Node.setCurrentFunction(saved); } *************** *** 446,449 **** --- 454,513 ---- } return null; + } + + var Stack<(Stack<int>, Stack<(MonoSymbol, mlsub.typing.Monotype)>)> closureEnvironments = new Stack(); + var Stack<List<(MonoSymbol, mlsub.typing.Monotype)>> replacedTypes = new Stack(); + + void enterClosure() + { + Set<MonoSymbol> done = new HashSet(); + List<(MonoSymbol, mlsub.typing.Monotype)> replaced = new ArrayList(); + for (int i = 0; i < levels.size(); i++) + { + (MonoSymbol variable, mlsub.typing.Monotype type) = conditionalTypes.get(i); + if (!done.contains(variable)) + { + replaced.add((variable, variable.type)); + variable.type = type; + done.add(variable); + } + } + + replacedTypes.push(replaced); + closureEnvironments.push((levels, conditionalTypes)); + levels = new Stack(); + conditionalTypes = new Stack(); + } + + void exitClosure() + { + List<(MonoSymbol, mlsub.typing.Monotype)> replaced = replacedTypes.pop(); + for (int i = 0; i < replaced.size(); i++) + { + (MonoSymbol variable, mlsub.typing.Monotype type) = replaced.get(i); + variable.type = type; + } + + (levels, conditionalTypes) = closureEnvironments.pop(); + } + + void setCaptured(MonoSymbol variable) + { + if (!variable.captured) + { + variable.captured = true; + let origType = getOriginalType(variable) || variable.type; + variable.type = origType; + for (int i = 0; i < closureEnvironments.size(); i++) + { + (Stack<int> lvls, Stack<(MonoSymbol, mlsub.typing.Monotype)> condTypes) = closureEnvironments.get(i); + for (int k = 0; k < condTypes.size(); k++) + { + (MonoSymbol v, mlsub.typing.Monotype t) = condTypes.get(k); + if (v == variable) + condTypes.set(k, (v, origType)); + } + } + } } |