X-Git-Url: https://git.lukelau.me/?p=kaleidoscope-hs.git;a=blobdiff_plain;f=AST.hs;fp=AST.hs;h=5628e7c7cd74c8d0f04d5b4e9c908e245e5696f6;hp=0000000000000000000000000000000000000000;hb=ae80d076cba678169dbddac4a53edd2cb1e091fb;hpb=30a26b7d2b0e17ea523ee34cb5d37242a38882df diff --git a/AST.hs b/AST.hs new file mode 100644 index 0000000..5628e7c --- /dev/null +++ b/AST.hs @@ -0,0 +1,23 @@ +module AST where + +import Text.Read +import Text.ParserCombinators.ReadP hiding ((+++)) + +data Expr = Num Float + | Add Expr Expr + deriving Show + +instance Read Expr where + readPrec = parseNum +++ parseAdd + where parseNum = Num <$> readPrec + -- use 'prec 1' and 'step' so that parsing 'a' + -- can only go one step deep, to prevent ininfite + -- recursion + parseAdd = prec 1 $ do + a <- step readPrec + lift $ do + skipSpaces + char '+' + skipSpaces + b <- readPrec + return (Add a b)