-
Notifications
You must be signed in to change notification settings - Fork 0
/
SrcParser.y
140 lines (110 loc) · 3.11 KB
/
SrcParser.y
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
{
module SrcParser where
import Data.List
import qualified Data.Set as S
import SrcToken
import SrcLexer
import SyntaxSource
}
%name parser
%tokentype { SrcToken }
%error { parseError }
%monad { Maybe } { >>= } { return }
%token
var { SrcTokenVar $$ }
iface { SrcTokenIfaceName $$ }
int { SrcTokenInt $$ }
tint { SrcTokenTInt }
implicit { SrcTokenImplicit }
interface { SrcTokenInterface }
let { SrcTokenLet }
in { SrcTokenIn }
forall { SrcTokenForall }
'\\' { SrcTokenLambda }
'(' { SrcTokenOP }
')' { SrcTokenCP }
'{' { SrcTokenOB }
'}' { SrcTokenCB }
"=>" { SrcTokenBigArrow }
"->" { SrcTokenArrow }
'=' { SrcTokenEQ }
',' { SrcTokenComma }
'.' { SrcTokenDot }
':' { SrcTokenColon }
';' { SrcTokenSemiColon }
'?' { SrcTokenQuestion }
%left SrcTokenBigArrow
%left SrcTokenArrow
%%
Program
: Interfaces Exp {($1, $2)}
Exp
: FExp { $1 }
| '\\' var '.' Exp { SELam $2 Nothing $4 }
| '\\' var ':' Tau '.' Exp { SELam $2 (Just $4) $6 }
| implicit '{' VarList '}' in Exp { SEImplicit $3 $6 }
| let var ':' Scheme '=' Exp in Exp { SELet $2 $4 $6 $8 }
AExp
: int { SELit $1 }
| var { SEVar $1 }
| var ':' Tau { SELVar (Just $3) $1 }
| '?' { SEQuery Nothing }
| '?' Tau { SEQuery (Just $2) }
| '(' Exp ')' { $2 }
-- | '(' Exp ':' Scheme ')' { SEAnnot $2 $4 }
| iface '{' DefList '}' { SEImpl $1 $3 Nothing }
| iface '{' DefList '}' ':' iface ATaus { SEImpl $1 $3 (Just (STIface $6 $7)) }
FExp
: FExp AExp { SEApp $1 $2 }
| AExp { $1 }
VarList
: { [] }
| var VarList { $1:$2 }
Tau
: ATau { $1 }
| ATau "->" Tau { STFun $1 $3 }
ATaus
: ATau { [$1] }
| ATau ATaus { $1:$2 }
ATau
: '(' Tau ')' { $2 }
| tint { STInt }
| var { STVar $1 }
| iface ATaus { STIface $1 $2 }
Schemes
: { [] }
| AScheme { [$1] }
| AScheme ',' Schemes { $1:$3 }
AScheme
: Scheme { $1 }
SchemeSet
: '{' Schemes '}' { $2 }
Scheme
: forall VarList '.' SchemeSet "=>" Tau { Scheme $2 $4 $6 }
| forall VarList '.' Tau { Scheme $2 [] $4 }
| SchemeSet "=>" Tau { Scheme [] $1 $3 }
| Tau { lift $1 }
Interfaces
: { [] }
| Interface Interfaces { $1 : $2 }
Interface
: interface iface VarList '=' '{' DeclList '}' { Interface $2 $3 $6 }
DeclList
: Decl { [$1] }
| Decl ';' DeclList { $1:$3 }
Decl
: var ':' Tau { ($1, $3) }
DefList
: Def { [$1] }
| Def ',' DefList { $1:$3 }
Def
: var '=' Exp { ($1, $3) }
{
parseError :: [SrcToken] -> Maybe a
parseError _ = error "Parse error"
srcparse inp = do
(interfaces, pgm) <- parser (srclexer inp)
let getDecls (Interface _ _ decls) = decls
let fields = S.fromList $ map fst $ concatMap getDecls interfaces
return $ (interfaces, transformLVarToField fields $ transformVarToLVar pgm)
}