Skip to content

Commit c2aed4d

Browse files
committed
Added Binary Operation Parser and fixed precedence issues
1 parent 446787b commit c2aed4d

File tree

3 files changed

+84
-91
lines changed

3 files changed

+84
-91
lines changed

src/AST.hs

Lines changed: 32 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ module AST (
88
ArgList,
99
Args,
1010
Declaration(..),
11+
BinOpCall(..),
1112
FuncCall(..)
1213
) where
1314

@@ -57,6 +58,7 @@ data Func
5758
data Expr
5859
= DeclarationStmt Declaration
5960
| FuncCallStmt FuncCall
61+
| BinOpCallStmt BinOpCall
6062
| LiteralStmt Literal
6163
deriving (Show)
6264

@@ -99,16 +101,18 @@ data Declaration
99101

100102

101103
data FuncCall
102-
= Call {
104+
= FuncCall {
103105
callee :: Name ,
104106
args :: Args
105107
}
106-
| BinOpCall {
108+
deriving (Show)
109+
110+
data BinOpCall
111+
= BinOpCall {
107112
op :: Op ,
108113
lhs :: Expr,
109114
rhs :: Expr
110-
}
111-
deriving (Show)
115+
} deriving (Show)
112116

113117

114118
-- | For Debugging Purposes
@@ -118,37 +122,37 @@ data FuncCall
118122
-- instance Show Declaration where
119123
-- show c@(ExternDecl a) = show
120124

121-
argListPrint :: ArgList -> String
122-
argListPrint [] = ""
123-
argListPrint (x:farr) = (show $ fst x) ++ " " ++ (snd x) ++ ", " ++ (argListPrint farr)
125+
-- argListPrint :: ArgList -> String
126+
-- argListPrint [] = ""
127+
-- argListPrint (x:farr) = (show $ fst x) ++ " " ++ (snd x) ++ ", " ++ (argListPrint farr)
124128

125129

126-
vListPrint :: VList -> String
127-
vListPrint [] = ""
128-
vListPrint (x:fx) = x ++ ", " ++ (vListPrint fx)
130+
-- vListPrint :: VList -> String
131+
-- vListPrint [] = ""
132+
-- vListPrint (x:fx) = x ++ ", " ++ (vListPrint fx)
129133

130134

131-
-- instance Show
135+
-- -- instance Show
132136

133-
-- argsPrint :: Args -> String
134-
-- argsPrint [] = ""
135-
-- argsPrint (x:fx) = (exprPrint x) ++ ", " ++ (argsPrint fx)
137+
-- -- argsPrint :: Args -> String
138+
-- -- argsPrint [] = ""
139+
-- -- argsPrint (x:fx) = (exprPrint x) ++ ", " ++ (argsPrint fx)
136140

137-
argsPrint :: Args -> String
138-
argsPrint = show
139-
showExpr :: Expr -> String
140-
showExpr = show
141+
-- argsPrint :: Args -> String
142+
-- argsPrint = show
143+
-- showExpr :: Expr -> String
144+
-- showExpr = show
141145

142-
externDeclPrint (ExternDecl f a _) = "extern " ++ f ++ " -> " ++ (argListPrint a)
143-
varDeclPrint (VarDecl t l) = (show t) ++ " " ++ (vListPrint l)
146+
-- externDeclPrint (ExternDecl f a _) = "extern " ++ f ++ " -> " ++ (argListPrint a)
147+
-- varDeclPrint (VarDecl t l) = (show t) ++ " " ++ (vListPrint l)
144148

145149

146-
callPrint (Call c a) = "call " ++ (show c) ++ " -> " ++ (argsPrint a)
147-
binOpCallPrint (BinOpCall op l r) = (show op) ++ " -> " ++ (showExpr l) ++ " " ++ (showExpr r)
150+
-- callPrint (Call c a) = "call " ++ (show c) ++ " -> " ++ (argsPrint a)
151+
-- binOpCallPrint (BinOpCall op l r) = (show op) ++ " -> " ++ (showExpr l) ++ " " ++ (showExpr r)
148152

149-
-- Tests
150-
externDecl = ExternDecl "sin" [(IntC, "arg1")]
151-
varDecl = VarDecl StringC ["arg1", "arg2"]
152-
literal = StrLiteral "adf"
153-
literalStmt = LiteralStmt literal
154-
call = Call "func" [literalStmt]
153+
-- -- Tests
154+
-- externDecl = ExternDecl "sin" [(IntC, "arg1")]
155+
-- varDecl = VarDecl StringC ["arg1", "arg2"]
156+
-- literal = StrLiteral "adf"
157+
-- literalStmt = LiteralStmt literal
158+
-- call = Call "func" [literalStmt]

src/Lexer.hs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ module Lexer (
55
reservedOp,
66
stringLiteral,
77
integer,
8+
paren,
89
Parser(..),
910
parse
1011
) where
@@ -38,3 +39,6 @@ stringLiteral = Tok.stringLiteral lexer
3839
-- Returns the value of the number.
3940
integer :: Parser Integer
4041
integer = Tok.integer lexer
42+
43+
paren :: Parser a -> Parser a
44+
paren = Tok.parens lexer

src/Parser.hs

Lines changed: 48 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -6,82 +6,60 @@ import Text.Parsec.Combinator
66
import Text.Parsec.Char
77
import Text.Parsec
88
import qualified Text.Parsec.Token as Tok
9+
import qualified Data.Map as Map
910
import AST
1011

11-
-- literalParser :: Type -> Parser Literal
12-
-- literalParser IntC = do
13-
-- val <- integer
14-
-- return (IntLiteral val)
15-
-- literalParser StringC = do
16-
-- val <- stringLiteral
17-
-- return (StrLiteral val)
18-
19-
-- strP = literalParser StringC
20-
21-
-- topParser :: Parser Module
22-
-- topParser = commandParser <|> funcParser
23-
24-
-- commandParser :: Parser Module
25-
-- commandParser = do
26-
-- spaces
27-
-- exp <- exprParser
28-
-- char ';'
29-
-- spaces
30-
-- return $ Command exp
31-
3212

13+
precedenceTable = Map.fromList[("Plus",20),("Minus",20),("Mul",40),("Divide",40)]
3314

3415
exprParser :: Parser Expr
35-
exprParser = literalStmtParser
36-
<|> declarationStmtParser
37-
<|> funcCallStmtParser
38-
<|> (Tok.parens lexer exprParser)
39-
40-
-- declStmtParser :: Parser Expr
41-
-- declStmtParser = do
42-
-- decl <- declParser
43-
-- return $ DeclarationStmt decl
44-
45-
-- declParser :: Parser Declaration
46-
-- declParser = externDeclParser <|> varDeclParser
47-
48-
-- externDeclParser :: Parser Declaration
49-
-- externDeclParser = do
50-
-- spaces
51-
-- string "extern"
52-
-- name <- nameParser
53-
-- argList <- argListParser
54-
-- ret <- typeParser
55-
-- spaces
56-
-- return $ ExternDecl name argList ret
57-
58-
-- varDeclParser :: Parser Declaration
59-
-- varDeclParser = do
60-
-- spaces
61-
-- type <- typeParser
62-
-- nameList <- vListParser
63-
-- spaces
64-
-- return $ VarDecl type nameList
16+
exprParser = try binOpCallStmtParser
17+
<|> try factor
18+
<|> try declarationStmtParser
6519

20+
factor :: Parser Expr
21+
factor = try (paren exprParser)
22+
<|> try funcCallStmtParser
23+
<|> literalStmtParser
6624

6725
-- | Parsing type related stuffs. ----
6826
-- NSS
6927
typeParser :: Parser Type
70-
typeParser = intTParser <|> stringTParser
28+
typeParser = try intTParser <|> stringTParser
7129

7230
-- NSS
7331
intTParser = do
7432
reserved "int"
75-
spaces
7633
return IntC
7734

7835
-- NSS
7936
stringTParser = do
8037
reserved "string"
81-
spaces
8238
return StringC
8339
-------------------------------------
8440

41+
opParser :: Parser Op
42+
opParser = try plusParser
43+
<|> try minusParser
44+
<|> try mulParser
45+
<|> divideParser
46+
47+
plusParser = do
48+
reservedOp "+"
49+
return Plus
50+
51+
minusParser = do
52+
reservedOp "-"
53+
return Minus
54+
55+
mulParser = do
56+
reservedOp "*"
57+
return Mul
58+
59+
divideParser = do
60+
reservedOp "/"
61+
return Divide
62+
8563
-- | Variable Names and identifier -----
8664
-- NSS
8765
nameParser :: Parser Name
@@ -144,18 +122,28 @@ funcCallStmtParser = do
144122
res <- funcCallParser
145123
return $ FuncCallStmt res
146124

147-
funcCallParser :: Parser FuncCall
148-
funcCallParser = callParser {-<|> binOpCall-}
125+
binOpCallStmtParser :: Parser Expr
126+
binOpCallStmtParser = do
127+
res <- binOpCallParser
128+
return $ BinOpCallStmt res
149129

150-
callParser :: Parser FuncCall
151-
callParser = do
130+
131+
funcCallParser :: Parser FuncCall
132+
funcCallParser = do
152133
callee <- nameParser
153134
(spaces >> (char '(') >> spaces)
154135
args <- argsParser
155136
(spaces >> (char ')') >> spaces)
156-
return $ Call callee args
157-
137+
return $ FuncCall callee args
158138

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
159147

160148
-------------------------------------------------------
161149
-- | Declaration Stuff
@@ -190,9 +178,6 @@ varDeclParser = do
190178

191179
---------------------------------------------------------
192180

193-
-- funcCallParser :: Parser
194-
195-
196181
mainTest = do
197182
str <- getLine
198183
if str == "quit"

0 commit comments

Comments
 (0)