Skip to content

Commit 0ae4829

Browse files
committed
Completed Binary Op parsing
1 parent c2aed4d commit 0ae4829

File tree

2 files changed

+117
-5
lines changed

2 files changed

+117
-5
lines changed

src/AST.hs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ data Op
6868
| Minus
6969
| Mul
7070
| Divide
71-
deriving (Show)
71+
deriving (Show,Eq,Ord)
7272

7373
data Type
7474
= IntC

src/Parser.hs

Lines changed: 116 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import qualified Data.Map as Map
1010
import AST
1111

1212

13-
precedenceTable = Map.fromList[("Plus",20),("Minus",20),("Mul",40),("Divide",40)]
13+
precedenceTable = Map.fromList[(Plus,200),(Minus,200),(Mul,400),(Divide,400)]
1414

1515
exprParser :: Parser Expr
1616
exprParser = try binOpCallStmtParser
@@ -136,15 +136,127 @@ funcCallParser = do
136136
(spaces >> (char ')') >> spaces)
137137
return $ FuncCall callee args
138138

139+
-- binOpCallParser :: Parser BinOpCall
140+
-- binOpCallParser = do
141+
-- lhs <- factor
142+
-- spaces
143+
-- op <- opParser
144+
-- spaces
145+
-- rhs <- exprParser
146+
-- return $ BinOpCall op lhs rhs
147+
148+
-- binOpCallParser :: Parser BinOpCall
149+
-- binOpCallParser = do
150+
-- temp <- factor
151+
-- spaces
152+
-- op <- opParser
153+
-- spaces
154+
-- let tokPrec = (Map.lookup op precedenceTable)
155+
-- lhs <- rest (Just 0) tokPrec temp op
156+
-- rhs <- exprParser
157+
-- return $ BinOpCall op lhs rhs
158+
159+
-- rest:: (Maybe Int) -> (Maybe Int) -> Expr -> Op-> Parser Expr
160+
-- rest expPrec tokPrec lhs op = (do
161+
-- if comp tokPrec expPrec
162+
-- then return lhs
163+
-- else do
164+
-- rhs <- factor
165+
-- spaces
166+
-- nextOp <- opParser
167+
-- spaces
168+
-- let nextPrec = (Map.lookup nextOp precedenceTable)
169+
-- if comp tokPrec nextPrec
170+
-- then do
171+
-- hi <- rest (Just 0) nextPrec rhs nextOp
172+
-- let out = BinOpCallStmt (BinOpCall nextOp rhs hi)
173+
-- return $BinOpCallStmt (BinOpCall op lhs out)
174+
-- else do
175+
-- hii <- return $ BinOpCallStmt (BinOpCall op lhs rhs)
176+
-- next <- rest (inc tokPrec) nextPrec hii nextOp
177+
-- return $BinOpCallStmt (BinOpCall nextOp hii next)
178+
-- )
179+
180+
139181
binOpCallParser :: Parser BinOpCall
140182
binOpCallParser = do
141-
lhs <- factor
142-
spaces
143-
op <- opParser
183+
temp <- factor
144184
spaces
185+
lhs <- rest (Just 0) temp
186+
res <- try (optionalParse lhs) <|> (singleParse lhs)
187+
return res
188+
189+
190+
zero :: Expr
191+
zero = (LiteralStmt (IntLiteral 0))
192+
193+
optionalParse :: Expr -> Parser BinOpCall
194+
optionalParse lhs = do
195+
op <- opParser
145196
rhs <- exprParser
146197
return $ BinOpCall op lhs rhs
147198

199+
singleParse :: Expr -> Parser BinOpCall
200+
singleParse lhs = return $ BinOpCall Plus lhs zero
201+
202+
-- rest:: (Maybe Int) -> Expr -> Parser Expr
203+
-- rest expPrec lhs= try (do
204+
-- lop <- (lookAhead opParser)
205+
-- let tokPrec = getTokPrec lop
206+
-- if comp tokPrec expPrec
207+
-- then return lhs
208+
-- else do
209+
-- op <- opParser
210+
-- spaces
211+
-- temp <- factor
212+
-- spaces
213+
-- try(do
214+
-- nop <-(lookAhead opParser)
215+
-- let nextPrec = getTokPrec nop
216+
-- if comp tokPrec nextPrec
217+
-- then do
218+
-- rhs <- rest (inc tokPrec) temp
219+
-- return $ BinOpCallStmt (BinOpCall op lhs rhs)
220+
-- else return $ BinOpCallStmt (BinOpCall op lhs temp)
221+
-- )<|>return $ BinOpCallStmt (BinOpCall op lhs temp)
222+
-- ) <|> return lhs
223+
224+
getTokPrec op = (Map.lookup op precedenceTable)
225+
226+
227+
rest:: (Maybe Int) -> Expr -> Parser Expr
228+
rest expPrec lhs= try (do
229+
lop <- (lookAhead opParser)
230+
let tokPrec = getTokPrec lop
231+
if comp tokPrec expPrec
232+
then return lhs
233+
else do
234+
op <- opParser
235+
spaces
236+
temp <- factor
237+
spaces
238+
res <- try (firstP tokPrec temp lhs op )
239+
<|> (return $ BinOpCallStmt (BinOpCall op lhs temp))
240+
return res
241+
) <|> return lhs
242+
243+
-- firstP :: (Maybe a) ->
244+
firstP tokPrec temp lhs op = do
245+
nop <-(lookAhead opParser)
246+
let nextPrec = getTokPrec nop
247+
if comp tokPrec nextPrec
248+
then do
249+
rhs <- rest (inc tokPrec) temp
250+
return $ BinOpCallStmt (BinOpCall op lhs rhs)
251+
else return $ BinOpCallStmt (BinOpCall op lhs temp)
252+
253+
254+
inc :: (Num a, Ord a)=> (Maybe a) -> (Maybe a)
255+
inc (Just x) = Just (x+1)
256+
257+
comp :: Ord a => (Maybe a) -> (Maybe a) -> Bool
258+
comp (Just x) (Just y) = (x < y)
259+
148260
-------------------------------------------------------
149261
-- | Declaration Stuff
150262

0 commit comments

Comments
 (0)