Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function prepareSwitch( stateMap, stateVar )
- // this is the frame of our switch; it is roughly switch <var>; ... end
- sw = Syn.Switch( Symbol(stateVar) )
- for state, r in stateMap
- // we then create an empty syntree for each case
- cs = SynTree()
- // the selector of the tree is a relevant expression that characterizes it,
- // for instance, in while <expr>... end, if <expr>... end etc, the <expr>
- // is the selector.
- // The switch statement accepts syntree with Case expressions as selectors only
- // (or no selector, for the default)
- cs.selector = Syn.Case(state)
- // a shortcut to use 'action' twice
- action = r.action
- if action // means, if it's not nil
- // then we append the action, that is, a function call. More specifically,
- // {[] code} it's the literal expression; it transforms a parsed expression
- // into a value. The ^~ operator is the 'unescape', which puts in the expression
- // the literal value of the given symbol (or expression) in the context where the
- // {[] ...} is being formed.
- // So, {[] ^~action(tree, text)} it is 100% equivalent to Call( action, $tree, $text), it's just
- // more readable.
- cs.append( {[] ^~action(tree, text)} )
- end
- // We then add a call to setState. The literal value of self as it is NOW is inserted via
- // the unescape operator. As said, ^~ works also with expressions (as for ^~(r.target))
- cs.append( {[] ^~self.setState(^~(r.target))} )
- sw.append(cs)
- end
- // SynTree's render method will give back a reflexively compileable source code.
- > sw.render()
- return sw
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement