diff --git a/src/org/rascalmpl/library/lang/rascal/tests/basic/FunctionCachesAndGlobals.rsc b/src/org/rascalmpl/library/lang/rascal/tests/basic/FunctionCachesAndGlobals.rsc new file mode 100644 index 00000000000..cd36f975a47 --- /dev/null +++ b/src/org/rascalmpl/library/lang/rascal/tests/basic/FunctionCachesAndGlobals.rsc @@ -0,0 +1,9 @@ +module lang::rascal::tests::basic::FunctionCachesAndGlobals + +default int f(int _) = 42; +int x = f(0); +int f(0) = 0; + +test bool globalInitLast() = x == 0; + +// test bool noCacheDuringInit() = f(0) == 0; \ No newline at end of file diff --git a/src/org/rascalmpl/semantics/dynamic/Module.java b/src/org/rascalmpl/semantics/dynamic/Module.java index 26a606784aa..3dafc66ef4e 100644 --- a/src/org/rascalmpl/semantics/dynamic/Module.java +++ b/src/org/rascalmpl/semantics/dynamic/Module.java @@ -58,8 +58,18 @@ public Result interpret(IEvaluator> eval) { List decls = this.getBody().getToplevels(); eval.__getTypeDeclarator().evaluateDeclarations(decls, eval.getCurrentEnvt(), false); + // first everything that is not a global for (Toplevel l : decls) { - l.interpret(eval); + if (!l.getDeclaration().isVariable()) { + l.interpret(eval); + } + } + + // then the globals which may depend on the previous + for (Toplevel l : decls) { + if (l.getDeclaration().isVariable()) { + l.interpret(eval); + } } } finally {