Permalink
Browse files

[compiler][back] Fix a bug in which imported global variables were ca…

…ptured as free variables. Add tests.
  • Loading branch information...
ptal committed Dec 2, 2018
1 parent 9879d7a commit 0c9d40c0314fd221d3911d4c022bd16db408f8e5
@@ -21,7 +21,11 @@
#[run(UniverseT.twoPrintChild, "123456")]
#[run(UniverseT.successiveChild, "123456")]
#[run(UniverseT.successiveBinaryChild, "11a22a32b41b56")]
#[debug(UniverseT.countSpace, "123")]
#[run(UniverseT.countSpace, "012")]
#[run(UniverseT.countSpace2, "012")]
#[run(UniverseT.sequenceUniverse, "1234")]
#[run(UniverseT.sequenceUniverse2, "12345")]
#[run(UniverseT.nestedUniverse, "123456")]
package test;
@@ -152,4 +156,75 @@ public proc countSpace() =
System.out.print(read count);
end
end
public proc countSpace2() =
single_space StackLR stack = new StackLR();
universe with stack in
single_space LMax count = new LMax(0);
space readwrite count.inc() end;
System.out.print(read count);
pause;
space readwrite count.inc() end;
System.out.print(read count);
pause;
System.out.print(read count);
end
end
public proc sequenceUniverse() =
single_space StackLR stack = new StackLR();
universe with stack in
System.out.print(1);
space nothing end;
pause;
System.out.print(2);
end;
pause;
universe with stack in
System.out.print(3);
space nothing end;
pause;
System.out.print(4);
end
end
public proc sequenceUniverse2() =
single_space StackLR stack = new StackLR();
universe with stack in
System.out.print(1);
space nothing end;
pause;
System.out.print(2);
space nothing end;
space nothing end;
end;
pause;
universe with stack in
System.out.print(3);
pause;
System.out.print(4);
pause;
System.out.print(5);
end
end
public proc nestedUniverse() =
single_space StackLR stack = new StackLR();
universe with stack in
System.out.print(1);
space nothing end;
single_space StackLR stack2 = new StackLR();
universe with stack2 in
System.out.print(2);
space System.out.print(3) end;
pause;
pause;
end;
System.out.print(4);
pause;
System.out.print(5);
end
System.out.print(6)
end
}
@@ -23,7 +23,8 @@
public proc test() =
single_space StackLR stack = new StackLR();
universe with stack in
space nothing end;
single_space LMax count = new LMax(0);
space readwrite count.inc() end;
pause;
end
end
@@ -21,23 +21,24 @@ use back::compiler::statement::*;
pub fn compile_module(env: Env<Context>, module: JModule) -> Env<(Context, String)> {
env.and_next(|session, context| {
let code = ModuleCompiler::new(&session, &context).compile(module);
let mod_name = module.mod_name();
let code = ModuleCompiler::new(&session, &context, mod_name).compile(module);
Env::new(session, code.map(|code| (context, code)))
})
}
struct ModuleCompiler<'a> {
session: &'a Session,
context: &'a Context,
mod_name: Ident,
fmt: CodeFormatter
}
impl<'a> ModuleCompiler<'a>
{
fn new(session: &'a Session, context: &'a Context) -> Self {
fn new(session: &'a Session, context: &'a Context, mod_name: Ident) -> Self {
ModuleCompiler {
session: session,
context: context,
session, context, mod_name,
fmt: CodeFormatter::new()
}
}
@@ -266,7 +267,7 @@ impl<'a> ModuleCompiler<'a>
self.proc_uid(&process, proc_instance);
self.fmt.push_line("return");
self.fmt.indent();
compile_statement(self.session, self.context, &mut self.fmt, process.body);
compile_statement(self.session, self.context, &mut self.fmt, self.mod_name.clone(), process.body);
self.fmt.unindent();
self.fmt.terminate_line(";");
self.fmt.close_block();
@@ -18,23 +18,22 @@ use back::code_formatter::*;
use back::free_variables::*;
use back::compiler::expression::*;
pub fn compile_statement(session: &Session, context: &Context, fmt: &mut CodeFormatter, stmt: Stmt) {
StatementCompiler::new(session, context, fmt).compile(stmt)
pub fn compile_statement(session: &Session, context: &Context, fmt: &mut CodeFormatter, mod_name: Ident, stmt: Stmt) {
StatementCompiler::new(session, context, mod_name, fmt).compile(stmt)
}
struct StatementCompiler<'a> {
session: &'a Session,
context: &'a Context,
mod_name: Ident,
fmt: &'a mut CodeFormatter
}
impl<'a> StatementCompiler<'a>
{
pub fn new(session: &'a Session, context: &'a Context, fmt: &'a mut CodeFormatter) -> Self {
pub fn new(session: &'a Session, context: &'a Context, mod_name: Ident, fmt: &'a mut CodeFormatter) -> Self {
StatementCompiler {
session: session,
context: context,
fmt: fmt
session, context, mod_name, fmt
}
}
@@ -143,7 +142,7 @@ impl<'a> StatementCompiler<'a>
}
fn space(&mut self, branch: Box<Stmt>) {
let free_vars = free_variables((*branch).clone());
let free_vars = free_variables(self.context, self.mod_name.clone(), (*branch).clone());
self.fmt.push_line("new SpaceStmt(");
self.fmt.indent();
self.fmt.push_line("new ArrayList<>(Arrays.asList(");
@@ -15,19 +15,23 @@
use std::collections::hash_set::HashSet;
use context::*;
pub fn free_variables(program: Stmt) -> HashSet<Variable> {
let fv = FreeVariables::new();
pub fn free_variables(context: &Context, current_mod: Ident, program: Stmt) -> HashSet<Variable> {
let fv = FreeVariables::new(context, current_mod);
fv.collect(program)
}
struct FreeVariables {
struct FreeVariables<'a> {
context: &'a Context,
current_mod: Ident,
free_vars: HashSet<Variable>,
in_scope_vars: HashSet<usize>
}
impl FreeVariables {
pub fn new() -> Self {
impl<'a> FreeVariables<'a> {
pub fn new(context: &'a Context, current_mod: Ident) -> Self {
FreeVariables {
context,
current_mod,
free_vars: HashSet::new(),
in_scope_vars: HashSet::new()
}
@@ -47,10 +51,13 @@ impl FreeVariables {
}
}
impl Visitor<JClass> for FreeVariables
impl<'a> Visitor<JClass> for FreeVariables<'a>
{
fn visit_var(&mut self, var: Variable) {
if self.in_scope_vars.contains(&var.first_uid()) {
let head = var.path.first();
if !self.in_scope_vars.contains(&var.first_uid())
&& !self.context.is_imported(&self.current_mod, &head)
{
self.free_vars.insert(var);
}
}
@@ -50,7 +50,7 @@ fn test_data_directory()
test_data_dir(false);
}
#[test]
fn debug_run() {
test_data_dir(true);
}
// #[test]
// fn debug_run() {
// test_data_dir(true);
// }

0 comments on commit 0c9d40c

Please sign in to comment.