Permalink
Browse files

[compiler][grammar] Add support for flow statement and process by des…

…ugaring them into loop p; pause end.
  • Loading branch information...
ptal committed Jan 11, 2019
1 parent d5d6171 commit e7b9e7b55c7a6ddf06ace623e52ef5a4a35495eb
Showing with 67 additions and 7 deletions.
  1. +41 −0 data/test/run-pass/LoopT.bonsai.java
  2. +3 −2 src/back/compiler/module.rs
  3. +23 −5 src/front/grammar.rs
@@ -17,6 +17,9 @@
#[run(LoopT.loopJoinSpace, "210-1e")]
#[run(LoopT.loopScopeBug, "bot")]
#[run(LoopT.loopInnerDeclaration, "1")]
#[run(LoopT.flowOne, "1e")]
#[run(LoopT.flowTwo, "10e")]
#[run(LoopT.flowProcOneInner, "1e")]

package test;

@@ -103,4 +106,42 @@ public proc loopInnerDeclaration() =
end
end
end

public proc flowOne() =
single_space StackLR stack = new StackLR();
universe with stack in
flow System.out.print(1) end;
System.out.print("unreachable");
end;
System.out.print("e");
end

public proc flowTwo() =
single_space StackLR stack = new StackLR();
universe with stack in
single_space LMin nodes = new LMin(2);
flow
readwrite nodes.dec();
when nodes |= 0 then
nothing
else
space nothing end
end;
System.out.print(read nodes);
end;
System.out.print("unreachable");
end;
System.out.print("e")
end

public proc flowProcOneInner() =
single_space StackLR stack = new StackLR();
universe with stack in
run flowProcOne();
System.out.print("unreachable");
end;
System.out.print("e");
end

flow flowProcOne() = System.out.print(1)
}
@@ -218,9 +218,10 @@ impl<'a> ModuleCompiler<'a>
}
self.fmt.terminate_line(")");
self.fmt.open_block();
self.fmt.push_line("this.__object_instance = ++this.__num_instances;");
for field in module.fields.clone() {
if field.binding.is_module() {
unimplemented!("module field are not supported yet.");
unimplemented!("fields of type `module` are not supported yet.");
// self.fmt.push(&format!("{}.__init(__layer", binding.name));
// self.fmt.push(");");
}
@@ -263,7 +264,7 @@ impl<'a> ModuleCompiler<'a>
if field.binding.is_module() {
self.fmt.push_line(&format!("{}.__destroy(__layer);", field.binding.name));
}
else {
else if field.is_ref.is_some() {
let uid = self.fuid(&field);
self.fmt.push_line(&format!("__layer.exitScope({});", uid));
}
@@ -16,10 +16,11 @@
#![allow(non_snake_case)]
grammar! bonsai {

// #![debug_api]

/// Convention: `_os` means that the rule is not parsing trailing space (os = open space).
/// This is required to avoid reporting error with trailing space included.

// #![debug_api]
use std::str::FromStr;
use ast::*;
use syntex_pos::Span;
@@ -108,11 +109,13 @@ grammar! bonsai {

item
= module_field
/ (.. java_visibility? PROC identifier java_param_list) EQ open_sequence > make_process_item
/ (.. java_visibility? (.. proc_or_flow) identifier java_param_list) EQ open_sequence > make_process_item
/ java_field
/ java_method
/ java_constructor

proc_or_flow = PROC > make_false / FLOW > make_true

module_field = (.. java_visibility? (.. REF)? bonsai_binding) SEMI_COLON > make_module_field

fn make_module_field(span: Span, visibility: Option<JVisibility>,
@@ -122,9 +125,14 @@ grammar! bonsai {
span, visibility, binding, is_ref))
}

fn make_process_item(span: Span, visibility: Option<JVisibility>, name: Ident,
params: JParameters, body: Stmt) -> Item
fn make_process_item(span: Span, visibility: Option<JVisibility>, flow_kw_sp: Span, is_flow: bool,
name: Ident, params: JParameters, body: Stmt) -> Item
{
let body = if is_flow {
let sp = body.span.clone();
Stmt::new(sp, make_flow(flow_kw_sp, body))
}
else { body };
Item::Proc(Process::new(span, visibility, name, params, body))
}

@@ -230,6 +238,7 @@ grammar! bonsai {
/ PAUSE_OS > make_pause
/ NOTHING_OS > make_nothing
/ LOOP close_sequence END_OS > make_loop
/ (.. FLOW) close_sequence END_OS > make_flow
/ UNIVERSE close_sequence END_OS > make_qf_universe
/ UNIVERSE WITH variable IN close_sequence END_OS > make_universe
/ RUN proc_call_os > make_proc_call
@@ -320,6 +329,14 @@ grammar! bonsai {
StmtKind::Loop(Box::new(body))
}

// We desugare `flow p end` into `loop p; pause end`.
fn make_flow(flow_kw_sp: Span, body: Stmt) -> StmtKind {
let pause = Stmt::new(flow_kw_sp, make_pause());
let sp = body.span.clone();
let desugared = Stmt::new(sp, StmtKind::Seq(vec![body,pause]));
StmtKind::Loop(Box::new(desugared))
}

fn make_tell(var: Variable, expr: Expr) -> StmtKind {
StmtKind::Tell(var, expr)
}
@@ -572,7 +589,7 @@ grammar! bonsai {
keyword
= "proc" / "par" / "space" / "prune" / "end" / "pre" / "nothing"
/ "when" / "then" / "else"
/ "loop" / "pause up" / "pause" / "stop" / "in" / "world_line"
/ "loop" / "flow" / "pause up" / "pause" / "stop" / "in" / "world_line"
/ "single_time" / "single_space" / "bot" / "top" / "ref" / "module"
/ "readwrite" / "read" / "write"
/ "or" / "and" / "not"
@@ -601,6 +618,7 @@ grammar! bonsai {
SUSPEND = "suspend" kw_tail
ABORT = "abort" kw_tail
LOOP = "loop" kw_tail
FLOW = "flow" kw_tail
IN = "in" kw_tail
WITH = "with" kw_tail
WORLD_LINE = "world_line" kw_tail

0 comments on commit e7b9e7b

Please sign in to comment.