Skip to content

Commit c6083ae

Browse files
committed
Added Grammar Description and Parser functions
1 parent e88a883 commit c6083ae

File tree

3 files changed

+172
-11
lines changed

3 files changed

+172
-11
lines changed

src/AST.hs

Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ module AST (
3333
-- Call : Name ( [Args] )
3434

3535
-- Op : + | - | * | /
36-
-- Args : Name[, Args]
36+
-- Args : Expr[, Args]
3737

3838
-- LiteralStmt : StrLiteral | IntLiteral
3939
-- IntLiteral : integer
@@ -42,6 +42,17 @@ module AST (
4242
-- Name : ident
4343
-- ArgList : Type Name[, ArgList]
4444

45+
data Module
46+
= Command Expr
47+
| Method Func
48+
49+
data Func
50+
= Func {
51+
fname :: Name ,
52+
argList:: ArgList,
53+
retType:: Type ,
54+
body :: [Expr]
55+
}
4556

4657
data Expr
4758
= DeclarationStmt Declaration
@@ -58,13 +69,13 @@ data Op
5869
deriving (Show)
5970

6071
data Type
61-
= Int
62-
| String
72+
= IntC
73+
| StringC
6374
deriving (Show)
6475

6576
data Literal
6677
= StrLiteral String
67-
| IntLiteral Int
78+
| IntLiteral Integer
6879
deriving (Show)
6980

7081

@@ -77,7 +88,8 @@ type Args = [Expr]
7788
data Declaration
7889
= ExternDecl {
7990
efName :: Name ,
80-
efArgsList :: ArgList
91+
efArgsList :: ArgList,
92+
retT :: Type
8193
}
8294
| VarDecl {
8395
vType :: Type,
@@ -93,7 +105,7 @@ data FuncCall
93105
}
94106
| BinOpCall {
95107
op :: Op ,
96-
lhs :: Expr ,
108+
lhs :: Expr,
97109
rhs :: Expr
98110
}
99111
deriving (Show)
@@ -127,16 +139,16 @@ argsPrint = show
127139
showExpr :: Expr -> String
128140
showExpr = show
129141

130-
externDeclPrint (ExternDecl f a) = "extern " ++ f ++ " -> " ++ (argListPrint a)
142+
externDeclPrint (ExternDecl f a _) = "extern " ++ f ++ " -> " ++ (argListPrint a)
131143
varDeclPrint (VarDecl t l) = (show t) ++ " " ++ (vListPrint l)
132144

133145

134146
callPrint (Call c a) = "call " ++ (show c) ++ " -> " ++ (argsPrint a)
135147
binOpCallPrint (BinOpCall op l r) = (show op) ++ " -> " ++ (showExpr l) ++ " " ++ (showExpr r)
136148

137149
-- Tests
138-
externDecl = ExternDecl "sin" [(Int, "arg1")]
139-
varDecl = VarDecl String ["arg1", "arg2"]
150+
externDecl = ExternDecl "sin" [(IntC, "arg1")]
151+
varDecl = VarDecl StringC ["arg1", "arg2"]
140152
literal = StrLiteral "adf"
141153
literalStmt = LiteralStmt literal
142154
call = Call "func" [literalStmt]

src/Language.hs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,6 @@ haskullstyle = emptyDef {
2929
-- | The language definition for the Haskull language.
3030
haskulldef = haskullstyle {
3131
reservedOpNames = ["+", "/", "-", "*", ";"],
32-
reservedNames = ["int", "char", "def", "extern"]
32+
reservedNames = ["int", "char", "def", "extern", "string"]
3333
}
3434

src/Parser.hs

Lines changed: 150 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,154 @@ module Parser where
22

33
import Lexer
44
import Text.Parsec.String (Parser)
5+
import Text.Parsec.Combinator
6+
import Text.Parsec.Char
7+
import Text.Parsec
8+
import AST
59

6-
import AST
10+
-- literalParser :: Type -> Parser Literal
11+
-- literalParser IntC = do
12+
-- val <- integer
13+
-- return (IntLiteral val)
14+
-- literalParser StringC = do
15+
-- val <- stringLiteral
16+
-- return (StrLiteral val)
17+
18+
-- strP = literalParser StringC
19+
20+
-- topParser :: Parser Module
21+
-- topParser = commandParser <|> funcParser
22+
23+
-- commandParser :: Parser Module
24+
-- commandParser = do
25+
-- spaces
26+
-- exp <- exprParser
27+
-- char ';'
28+
-- spaces
29+
-- return $ Command exp
30+
31+
exprParser :: Parser Expr
32+
exprParser = literalStmtParser
33+
-- <|> funcCallParser
34+
-- <|> literalParser
35+
-- <|> parens exprParser
36+
37+
-- declStmtParser :: Parser Expr
38+
-- declStmtParser = do
39+
-- decl <- declParser
40+
-- return $ DeclarationStmt decl
41+
42+
-- declParser :: Parser Declaration
43+
-- declParser = externDeclParser <|> varDeclParser
44+
45+
-- externDeclParser :: Parser Declaration
46+
-- externDeclParser = do
47+
-- spaces
48+
-- string "extern"
49+
-- name <- nameParser
50+
-- argList <- argListParser
51+
-- ret <- typeParser
52+
-- spaces
53+
-- return $ ExternDecl name argList ret
54+
55+
-- varDeclParser :: Parser Declaration
56+
-- varDeclParser = do
57+
-- spaces
58+
-- type <- typeParser
59+
-- nameList <- vListParser
60+
-- spaces
61+
-- return $ VarDecl type nameList
62+
63+
64+
-- | Parsing type related stuffs. ----
65+
-- NSS
66+
typeParser :: Parser Type
67+
typeParser = intTParser <|> stringTParser
68+
69+
-- NSS
70+
intTParser = do
71+
reserved "int"
72+
spaces
73+
return IntC
74+
75+
-- NSS
76+
stringTParser = do
77+
reserved "string"
78+
spaces
79+
return StringC
80+
-------------------------------------
81+
82+
-- | Variable Names and identifier -----
83+
-- NSS
84+
nameParser :: Parser Name
85+
nameParser = ident
86+
----------------------------------------
87+
88+
-- | VList: Name[, VList] -----------------
89+
-- NSS
90+
vListParser :: Parser VList
91+
vListParser = nameParser `sepBy1` (spaces >> (char ',') >> spaces)
92+
-------------------------------------------
93+
94+
-- ArgList : Type Name[, ArgList] ----------
95+
-- NSS
96+
argListParer :: Parser ArgList
97+
argListParer = unit `sepBy1` (spaces >> (char ',') >> spaces)
98+
where unit = do
99+
tp <- typeParser
100+
name <- nameParser
101+
return (tp, name)
102+
---------------------------------------------
103+
104+
-- | Args : Expr[, Args]
105+
-- NSS
106+
argsParser :: Parser Args
107+
argsParser = exprParser `sepBy1` (spaces >> (char ',') >> spaces)
108+
-----------------------------------------------
109+
110+
-- delim :: Parser ()
111+
-- delim p = (spaces >> (char p) >> spaces)
112+
113+
---------------------------------------------------
114+
-- | LiteralStmt : StrLiteral | IntLiteral
115+
-- NSS
116+
literalStmtParser :: Parser Expr
117+
literalStmtParser = do
118+
res <- literalParser
119+
return $ LiteralStmt res
120+
-- NSS
121+
literalParser :: Parser Literal
122+
literalParser = strLiteralP <|> intLiteralP
123+
-- NSS
124+
strLiteralP :: Parser Literal
125+
strLiteralP = do
126+
res <- stringLiteral
127+
return (StrLiteral res)
128+
-- NSS
129+
intLiteralP :: Parser Literal
130+
intLiteralP = do
131+
res <- integer
132+
return (IntLiteral res)
133+
-----------------------------------------------------
134+
135+
-- funcCallParser :: Parser
136+
137+
mainTest = do
138+
str <- getLine
139+
if str == "quit"
140+
then
141+
return ()
142+
else do
143+
print (parse argListParer "sdf" str)
144+
mainTest
145+
146+
147+
148+
-- --------------Done
149+
-- Type
150+
-- Literal
151+
-- Name
152+
-- VList
153+
-- ArgList
154+
-- Args
155+
-- LiteralStmt

0 commit comments

Comments
 (0)