Created
October 15, 2013 05:43
-
-
Save strager/6987056 to your computer and use it in GitHub Desktop.
HTML example written in Kitten (kittenlang.org).
This file contains hidden or 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
| //////////////////////////////////////////////////////////// | |
| // Core. | |
| type Node (Element | [Char]) | |
| type Element ([Char] & [Attribute] & [Node]) | |
| type Attribute ([Char] & [Char]) | |
| def element([Char] [[Char] & [Char]] [Node] -> Element): | |
| ->{name attributes children} | |
| (name, attributes {to Attribute} map, children) to Element | |
| def node(Element -> Node): | |
| left to Node | |
| def text([Char] -> Node): | |
| right to Node | |
| def escape([Char] -> [Char]): | |
| id // TODO(strager) | |
| def nodeToString(Node -> [Char]): | |
| \choice from Node: elementToString | |
| else: escape | |
| def attributeToString(Attribute -> [Char]): | |
| from Attribute unpair ->{name value} | |
| [name, "='", value escape, "'"] concat | |
| def elementToString(Element -> [Char]): | |
| from Element unpair3 ->{name attributes children} | |
| [ ["<", name] | |
| , attributes {attributeToString} map {' ' prepend} map | |
| , [">"] | |
| , children {nodeToString} map | |
| , [ "</", name, ">" ] | |
| ] concat concat | |
| //////////////////////////////////////////////////////////// | |
| // Elements. | |
| def p((-> [Node]) -> Node): | |
| @ ->{children} | |
| "p" [] children element node | |
| def a([Char] (-> [Node]) -> Node): | |
| @ ->{href children} | |
| "a" [("href", href)] children element node | |
| //////////////////////////////////////////////////////////// | |
| // Utilities. | |
| def unpair((a & b) -> a b): | |
| ->{p} | |
| p first | |
| p rest | |
| def unpair3((a & b & c) -> a b c): | |
| unpair unpair | |
| //////////////////////////////////////////////////////////// | |
| // Entry point. | |
| \p: [ | |
| "Hello! Try out " text, | |
| \a "http://kittenlang.org": ["Kitten" text] | |
| , // Must be on a newline, because the parser sucks? | |
| ", a functional, concatenative programming language!" text | |
| ] | |
| nodeToString '\n' append | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment