On 25/10/2012, at 5:00 PM, john skaller wrote: > It is now possible, though still a bit ugly, to define grammars with > the action codes written as Felix templates, instead of Scheme. > > Here's a simple example: > > ///////////////////// > syntax XXX { > x[sbor_pri] := x[>sbor_pri] "OVER" x[>sbor_pri] =># ( ?1 / ?3 ); > stmt := "WILL" sstatements "DO" ";" =># { println$ "Hello"; ??2; println$ > "Bye"; }; > } > open syntax XXX; > > println$ (100.0 OVER (22.0 OVER 7.0)); > > WILL println$ "One"; WILL println "INNER"; DO; println$ "Two"; DO ; > //////////////////
Unfortunately this isn't enough. I tried to do this: //////////////// syntax LOOPS { stmt := "FOR" sname "in" sinteger "to" sinteger "DO" sstatements "DONE" =># { for ?2 in ?4 upto ?6 do ??8; done }; } open syntax LOOPS; var i:int; FOR i in 10 to 20 DO println$ i; DONE ///////////////////////// The problem is that for ?2 in ... doesn't parse as Felix because ?2 is an expression, but a sname is required. If I make ?<digits> an sname, then the ?4 and ?6 are wrong te wrong type (it parses, the substitutions get done, but sex2flex barfs: Parsed open of syntax extensions XXX [sex2flx] ERROR in identifier in ( ast_literal ( "/Users/johnskaller/felix/./m.flx" 7 11 7 15 ) "double" "100.0" "100.0" ) Fatal error: exception Flx_sex2flx.Sex2FlxTypeError("identifier", _) because 100.0 (in the quoted code) isn't an sname. One solution to this is to write: for ?2:sname in ?4:sexpr upto ?6:sexpr ... i.e. to name the nonterminal you want to parse. To make that work we just add sname := "?" sinteger ":" "sname" =># "`(PARSER_ARGUMENT ,_2)"; and so on (for every non-terminal of interest). So now this works: ///////////////////////////// syntax XXX { x[sbor_pri] := x[>sbor_pri] "OVER" x[>sbor_pri] =># ( ?1 / ?3 ); stmt := "WILL" sstatements "DO" ";" =># { println$ "Hello"; ??2; println$ "Bye"; }; sname := "?" sinteger ":" "sname" =># "`(PARSER_ARGUMENT ,_2)"; } open syntax XXX; println$ (100.0 OVER (22.0 OVER 7.0)); WILL println$ "One"; WILL println "INNER"; DO; println$ "Two"; DO ; syntax LOOPS { stmt := "FOR" sname "in" sinteger "to" sinteger "DO" sstatements "DONE" =># { for ?2:sname in ?4 upto ?6 do ??8; done }; } open syntax LOOPS; var i:int; FOR i in 10 to 20 DO println$ i; DONE ///////////////////////////// Notice I trickily added the sname thing into DSSL XXX, so it was open when DSSL LOOPS got processed. -- john skaller skal...@users.sourceforge.net http://felix-lang.org ------------------------------------------------------------------------------ Everyone hates slow websites. So do we. Make your web apps faster with AppDynamics Download AppDynamics Lite for free today: http://p.sf.net/sfu/appdyn_sfd2d_oct _______________________________________________ Felix-language mailing list Felix-language@lists.sourceforge.net https://lists.sourceforge.net/lists/listinfo/felix-language