-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSyntax.cf
134 lines (93 loc) · 5.31 KB
/
Syntax.cf
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
entrypoints [Dec], Typ, Exp ;
-- Comments
comment "--" ;
comment "(*" "*)" ;
-- Identifiers
token Id ('_'* lower (letter | digit | '_' | '\'')*) ;
token IdCap ('_'* upper (letter | digit | '_'| '\'')*);
token IdVar ('\'' '_'* (letter | digit | '_')*) ;
-- Constants
CInt. Con ::= Integer ; -- 42
CString. Con ::= String ; -- "abc"
CUnit. Con ::= "(" ")" ; -- ()
-- Expressions
ECon. Exp11 ::= Con ; -- 42
EObjCon. Exp11 ::= IdCap ; -- Empty
EId. Exp11 ::= Id ; -- x
ETup. Exp11 ::= "(" Exp "," [Exp] ")" ; -- (a, b)
ELst. Exp11 ::= "[" [Exp] "]" ; -- [a, b]
separator Exp "," ;
-- Odpowiada też za konstruowanie obiektów, np. "Some x" będzie zinterpretowane
-- jako EApp (EObjCon "Some") (EId "x").
EApp. Exp10 ::= Exp10 Exp11 ; -- f x
ENeg. Exp9 ::= "-" Exp10 ; -- -10
EMul. Exp8 ::= Exp8 "*" Exp9 ; -- 2 * 2
EDiv. Exp8 ::= Exp8 "/" Exp9 ; -- 8 / 4
EAdd. Exp7 ::= Exp7 "+" Exp8 ; -- 2 + 2
ESub. Exp7 ::= Exp7 "-" Exp8 ; -- 3 - 2
ECons. Exp6 ::= Exp7 "::" Exp6 ; -- 1 :: [2, 3]
EAppend. Exp5 ::= Exp6 "@" Exp5 ; -- [1, 2] @ [2, 3]
ECat. Exp5 ::= Exp6 "^" Exp5 ; -- "ab" ^ "c"
-- Niedozwolone np. a < b < c
ERel. Exp4 ::= Exp5 ERelOp Exp5 ;
EREq. ERelOp ::= "==" ;
ERNe. ERelOp ::= "!=" ;
ERLt. ERelOp ::= "<" ;
ERLe. ERelOp ::= "<=" ;
ERGt. ERelOp ::= ">" ;
ERGe. ERelOp ::= ">=" ;
EAnd. Exp3 ::= Exp3 "&&" Exp4 ; -- a && b
EOr. Exp2 ::= Exp2 "||" Exp3 ; -- a || b
EIf. Exp1 ::= "if" Exp "then" Exp "else" Exp1 ; -- if a then b else c
ELet. Exp ::= "let" [LetBind] "in" Exp ; -- let ... in ...
ECase. Exp ::= "case" Exp "of" [ECaseBind] ; -- case x of ...
eCaseAlt. Exp ::= "case" Exp "of" "|" [ECaseBind] ; -- case x of ...
define eCaseAlt a b = ECase a b ;
ECBJust. ECaseBind ::= Pat "->" Exp ;
separator nonempty ECaseBind "|" ;
EFn. Exp ::= "fn" [Id] "->" Exp ;
separator nonempty Id "" ;
coercions Exp 11 ;
-- Patterns
PCon. Pat3 ::= Con ; -- 0
PId. Pat3 ::= Id ; -- x
PWild. Pat3 ::= "_" ; -- _
PTup. Pat3 ::= "(" Pat "," [Pat] ")" ; -- (a, b)
PLst. Pat3 ::= "[" [Pat] "]" ; -- [a]
separator Pat "," ;
PObjCon. Pat3 ::= IdCap ; -- Empty
PObj. Pat1 ::= IdCap Pat2 ; -- Some x
PCons. Pat ::= Pat1 "::" Pat ; -- 1 :: [2, 3]
coercions Pat 3 ;
-- Types
TIdVar. Typ3 ::= IdVar ; -- 'a
TId. Typ3 ::= TypLst Id ; -- int list
TLEmpty. TypLst ::= ;
TLOne. TypLst ::= Typ3 ; -- int
TLMany. TypLst ::= "(" Typ "," [Typ] ")" ; -- (int, string)
separator nonempty Typ "," ;
TTup. Typ1 ::= Typ2 "*" [TTupElem] ; -- int * string
TTupJust. TTupElem ::= Typ2 ;
separator nonempty TTupElem "*" ;
TFn. Typ ::= Typ1 "->" Typ ; -- int -> string
coercions Typ 3 ;
-- Declarations
DLet. Dec ::= "let" [LetBind] ; -- let a = 0 and f x = a
DType. Dec ::= "type" [TypBind] ; -- type x = A | B of c
DExn. Dec ::= "exception" [ExnBind] ; -- exception A of string
DOpen. Dec ::= "open" [IdCap] ; -- open List
separator nonempty IdCap "and" ;
separator Dec "" ;
LBJust. LetBind ::= Id "=" Exp ; -- let x = 4
LBAnon. LetBind ::= "_" "=" Exp ; -- let _ = x
separator nonempty LetBind "and" ;
TBJust. TypBind ::= TypLst Id "=" [DTag] ; -- 'a t = A | B
tBJust. TypBind ::= TypLst Id "=" "|" [DTag] ; -- 'a t = | A | B
define tBJust a b c = TBJust a b c ;
separator nonempty TypBind "and" ;
DTCon. DTag ::= IdCap ; -- A
DTArg. DTag ::= IdCap "of" Typ ; -- A of int
separator nonempty DTag "|" ;
EBCon. ExnBind ::= IdCap ; -- A
EBArg. ExnBind ::= IdCap "of" Typ ; -- A of int
separator nonempty ExnBind "and" ;