From 45afeea77cd017d2ac37cf330c595c22c90a7f87 Mon Sep 17 00:00:00 2001 From: Luke Lau Date: Sat, 18 May 2019 17:51:22 +0100 Subject: [PATCH] Parse variables in expressions --- AST.hs | 3 +++ 1 file changed, 3 insertions(+) diff --git a/AST.hs b/AST.hs index 5b22ea6..c766afc 100644 --- a/AST.hs +++ b/AST.hs @@ -4,6 +4,7 @@ import Text.Read import Text.ParserCombinators.ReadP hiding ((+++), choice) data Expr = Num Float + | Var String | BinOp BinOp Expr Expr deriving Show @@ -12,12 +13,14 @@ data BinOp = Add | Sub | Mul | Cmp Ordering instance Read Expr where readPrec = choice [ parseNum + , parseVar , parseBinOp "<" 10 (Cmp LT) , parseBinOp "+" 20 Add , parseBinOp "-" 20 Sub , parseBinOp "*" 40 Mul ] where parseNum = Num <$> readPrec + parseVar = Var <$> lift (munch1 isAlpha) -- use 'prec 1' and 'step' so that parsing 'a' -- can only go one step deep, to prevent ininfite -- recursion -- 2.30.2