Skip to content

Instantly share code, notes, and snippets.

@soegaard
Created June 21, 2014 18:56
Show Gist options
  • Save soegaard/35f48b58709f81754424 to your computer and use it in GitHub Desktop.
Save soegaard/35f48b58709f81754424 to your computer and use it in GitHub Desktop.
TypeError: Cannot read property 'type' of undefined
/*** CONTROL CONSTUCTS ***/
/* rif expr expr1 expr2
equivalent to (expr ? expr1 : expr2)
Note: The Javascript if is a "statement-if", so this is the "expression-if" */
macro rif {
case {_ ($e:expr, $e1:expr, $e2:expr)}
=> {return #{(($e) ? ($e1) : ($e2))}}}
/* cond [expr body] ...
cond [expr body] ... [else default]
Racket cond, except body here is a single expression */
macro cond {
case { _ [$e:expr => $result:expr] $more ... }
=> { return #{ rif($e, $result, (cond $more ...))}}
case { _ [$e:expr $result:expr] $more ... }
=> { return #{ rif($e, $result, (cond $more ...))}}
case { _ [$[else] $e:expr]}
=> {return #{$e}}
case { _ }
=> {return #{ void(0) }} }
/*** BINDING CONSTRUCTS ***/
/* rlet (id=expr, ...) body
Racket let binding
Evaluate the expressions, bind the results to the identifiers id... in body.
The scope of the identifiers does not include the expressions.
*/
macro rlet {
case { _ ( ) {$body:expr}}
=> { return #{ (function () {return $body} )()} }
case { _ ( $($id:ident = $val:expr) (,) ... ) {$e:expr (;) ... $body:expr} }
=> { return #{(function ($id (,) ...) {$e (;) ... ; return $body})($val (,) ...)} }
case { _ ( ) $body}
=> { return #{ (function () $body )()} }
case { _ ( $($id:ident = $val:expr) (,) ... ) $body }
=> { return #{(function ($id (,) ...) $body)($val (,) ...)} } }
/* rlets (id=expr, ...) body
Racket let* binding.
Evaluate the first expression and bind it to the first identifier.
Evaluate the second expression and bind it to the second identifier. Etc.
The scope of an identifier includes the following expressions and the body.
rlets (id=expr, ...) {body ...}
is equivalent to rlet (id=0, ...) { id=expr; ... ; body ...}
*/
macro rlets {
case { _ () {$body:expr} }
=> { return #{ rlet (){$body} } }
case { _ ( $($id:ident = $val:expr) (,) ... ) {$e:expr (;)... $body:expr} }
=> { return #{ rlet ($($id=0)(,)...) {$($id=$val) (;) ... ; $e (;) ... $body } } }
case { _ ( ) $body}
=> { return #{ (function () $body )()} }
case { _ ( $($id:ident = $val:expr) (,) ... ) {$body ...} } /* HERE */
=> { return #{ rlet ($($id=0)(,)...) {$($id=$val) (;) ... ; $body ...} } }
}
/*** TESTS ***/
function displayln(x) { console.log(x); }
displayln("--- cond tests ---")
/* displayln( cond ); */
displayln( cond [43 "ok"] );
var x=1; displayln( cond [x===1 "ok"] [x===2 "2"] [x===3 "3"] );
var x=2; displayln( cond [x===1 1] [x===2 "ok"] [x===3 "3"] );
var x=3; displayln( cond [x===1 1] [x===2 "2"] [else "ok"] );
displayln("--- when and unless ---");
when( 2>3 ) displayln("fail");
when( 2<3 ) { "mumble"; displayln("ok")}
unless (1===2) { displayln("ok"); }
unless (1===1) { displayln("fail"); }
displayln("--- rif ---");
var x=1;
displayln( rif(x==1,"ok","fail"));
displayln( rif(x==2,"fail","ok") );
displayln("--- rlet ---");
rlet( a=1, b=2) { displayln(rif(a+b==3,"ok","fail")) };
displayln( rif( (rlet(){var y=43; return y})==43,"ok","fail" ));
displayln( rif( (rlet(a=1){ return a;})==1, "ok", "fail"));
displayln( cond [(rlet(a=1,b=2){a+b})==3 "ok"] [else "fail"] );
displayln( cond [(rlet(a=1,b=2){return a+b})==3 "ok"] [else "fail"] );
displayln( cond [(rlet(a=1,b=2){42; a+b})==3 "ok"] [else "fail"] ); /* this fails bc return */
displayln( cond [(rlet(a=1,b=2){42; return a+b})==3 "ok"] [else "fail"] );
displayln( "--- rlets ---");
displayln( rlets(){"ok"} );
displayln( rlets(a=7,b=a+1){rif(a+b==15,"ok","fail")} );
displayln( rlets(){var x=2 ; return "ok"} );
displayln( rlets(a=7,b=a+1){var x=3; return rif(a+b==18,"ok","fail")} ); /* EXAMPLE */
---------------------
The expander gives this error:
soegaard$ sjs -c -o rlet.js rlet.sjs && less rlet.js
/usr/local/lib/node_modules/sweet.js/lib/sweet.js:101
throw err;
^
TypeError: Cannot read property 'type' of undefined
at takeLine (/usr/local/lib/node_modules/sweet.js/lib/patterns.js:73:21)
at /usr/local/lib/node_modules/sweet.js/lib/patterns.js:67:20
at Array.map (native)
at Function._.map._.collect (/usr/local/lib/node_modules/sweet.js/node_modules/underscore/underscore.js:95:56)
at takeLineContext (/usr/local/lib/node_modules/sweet.js/lib/patterns.js:66:18)
at takeLine (/usr/local/lib/node_modules/sweet.js/lib/patterns.js:102:28)
at /usr/local/lib/node_modules/sweet.js/lib/patterns.js:67:20
at Array.map (native)
at Function._.map._.collect (/usr/local/lib/node_modules/sweet.js/node_modules/underscore/underscore.js:95:56)
at takeLineContext (/usr/local/lib/node_modules/sweet.js/lib/patterns.js:66:18)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment