Last active
June 26, 2019 16:05
-
-
Save zelinskiy/62386ac24bf4556b3545b309ea4e6f02 to your computer and use it in GitHub Desktop.
Elm vs ANTLR javascript
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
module BoolExpr where | |
type BExpr | |
= BTrue | |
| BFalse | |
| BAnd BExpr BExpr | |
| BOr BExpr BExpr | |
-- Example of (1 /\ (0 \/ 1)) AST: | |
expr = BAnd BTrue (BOr BFalse BTrue) | |
-- Example of AST - driven view. We could simply extend this example from String to Html | |
view : BExpr -> String | |
view expression = | |
case expression of | |
BTrue -> "1" | |
BFalse -> "0" | |
BAnd a b -> "(" ++ view a ++ " /\\ " ++ view b ++ ")" | |
BOr a b -> "(" ++ view a ++ " \\/ " ++ view b ++ ")" | |
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
// naive approach | |
var expr = { _type : "BoolExpr", _const : "And" | |
, values : [ { _type : "BoolExpr", _const : "True", values : {} } | |
, { _type : "BoolExpr", _const : "Or" | |
, values : [ { _type : "BoolExpr", _const : "False", values : {} } | |
, { _type : "BoolExpr", _const : "True", values : {} } | |
] | |
} | |
] | |
} | |
function view(expr) { | |
switch(expr._const){ | |
case "True": | |
return "1"; | |
case "False": | |
return "0"; | |
case "And": | |
return "(" + view(expr.values[0]) + " /\\ " + view(expr.values[1]) + ")"; | |
case "Or": | |
return "(" + view(expr.values[0]) + " \\/ " + view(expr.values[1]) + ")"; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment