-
-
Notifications
You must be signed in to change notification settings - Fork 44
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add demo of how to make a domain specific language
for arbitrary term construction.
- Loading branch information
Showing
1 changed file
with
74 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
SCHEME """(define (mklist es)`(ast_apply ,_sr (,(nos "list") (ast_tuple ,_sr ,es))))"""; | ||
|
||
syntax grammar { | ||
x[let_pri]:= "grammar" xproduction* "endgrammar" =># | ||
"""`(ast_variant ("grammar" ,(mklist _2)))""" | ||
; | ||
|
||
xproduction := sname ":=" (xnonterminal | xterminal)* ";" =># | ||
""" | ||
(let* | ||
( | ||
(cast (lambda (sym)`(ast_coercion ,_sr (,sym ,(nos "sym_t"))))) | ||
(mapcast (map cast _3)) | ||
) | ||
`(ast_variant ("production" (ast_tuple ,_sr (,(stringof _1) ,(mklist mapcast))))) | ||
) | ||
""" | ||
; | ||
|
||
xnonterminal := sname =># | ||
"""`(ast_variant ("nonterminal" ,(stringof _1)))""" | ||
; | ||
|
||
xterminal := sstring =># // a string, to be interpreted as a regexp | ||
"""`(ast_variant ("terminal" ,(stringof _1))))"""; | ||
} | ||
|
||
open syntax grammar; | ||
println$ "Grammar test"; | ||
var s = grammar | ||
start := x y; | ||
x := "Jello"; | ||
y := "world"; | ||
endgrammar; | ||
println$ "Grammar spec parsed"; | ||
typedef gram_t = ( | ||
| `grammar of list[prod_t] | ||
); | ||
typedef prod_t = ( | ||
| `production of string * list[sym_t] | ||
); | ||
typedef sym_t = ( | ||
| `terminal of string | ||
| `nonterminal of string | ||
); | ||
|
||
instance Str[sym_t] { | ||
fun str(x:sym_t):string => | ||
match x with | ||
| `terminal s => "(terminal " + s + ")" | ||
| `nonterminal s => "(nonterminal " + s + ")" | ||
endmatch | ||
; | ||
} | ||
instance Str[prod_t] { | ||
fun str(x:prod_t):string => | ||
match x with | ||
| `production (name, ls) => " production " name + " := " + List::cat "," (List::map (str of sym_t) ls) + ";" | ||
endmatch | ||
; | ||
} | ||
instance Str[gram_t] { | ||
fun str(x:gram_t):string => | ||
match x with | ||
| `grammar ls => "grammar" + List::cat "\n" (List::map (str of prod_t) ls) +"\nendgrammar\n" | ||
endmatch | ||
; | ||
} | ||
|
||
|
||
println$ s.str; | ||
|
||
|
||
|