Skip to main content

module demo::lang::MissGrant::ToSwitch

rascal-0.40.17

Usage

import demo::lang::MissGrant::ToSwitch;

Dependencies

import demo::lang::MissGrant::ToMethods;
import demo::lang::MissGrant::AST;

function controller2switch

public str controller2switch(str name, Controller ctl) =
"public class <name> {
' <states2consts(ctl.states)>
' <controller2run(ctl)>
' <for (e <- ctl.events) {>
' <event2java(e)>
' <}>
' <for (c <- ctl.commands) {>
' <command2java(c)>
' <}>
'}";

function states2consts

public str states2consts(list[State] states) {
i = 0;
return "<for (s <- states) {>
'private static final int <stateName(s)> = <i>;
'<i += 1;}>";
}

function controller2run

public str controller2run(Controller ctl) =
"public void run(Scanner input, Writer output) {
' int state = <stateName(initial(ctl))>;
' while (true) {
' String token = input.nextLine();
' switch (state) {
' <for (s <- ctl.states) {>
' <state2case(s)>
' <}>
' }
'}";

function state2case

public str state2case(State s) =
"case <stateName(s)>: {
' <for (a <- s.actions) {>
' <a>(output);
' <}>
' <for (transition(e, s2) <- s.transitions) {>
' if (<e>(token)) {
' state = <stateName(s2)>;
' }
' <}>
' break;
'}";