source: applications/trunk/LFC/parser/Parser.rf @ 3893

Last change on this file since 3893 was 3893, checked in by orlov, 12 years ago
  • Draft version of compiler to ASR.
  • Property svn:eol-style set to native
File size: 9.7 KB
Line 
1$use Access Apply StdIO Stream;
2$use Lexer;
3
4$func Main = e;
5Main = <WriteLn <ParseFile "samples.lfc">>;
6
7$public $func ParseFile e.fname = e.ast;
8ParseFile e.fname =
9  <Lexer <File_Open e.fname>> :: e.tokens,
10  //<WriteLn e.tokens>,
11  <GetRAST <Program e.tokens>>;
12
13$public $func GetRAST e.ast = e.ast;
14GetRAST {
15  /*empty*/ = /*empty*/;
16  t.term e.ast = t.term : {
17    (IDENT t.name VAR) = (VAR t.name);
18    (IDENT t.name CALL e.args) = (CALL t.name <GetRAST e.args>);
19    (SENTENCE t.name (e.patterns) e.exp (WHERE e.assigns)) = (SENTENCE t.name (e.patterns) (LET (<GetRAST e.assigns>) <GetRAST e.exp>));
20    (PAREN e.exp (WHERE e.assigns)) = (PAREN (LET (<GetRAST e.assigns>) <GetRAST e.exp>));
21    (e.exp) = (<GetRAST e.exp>);
22    s.sym = s.sym;
23  } :: t.term,
24    t.term <GetRAST e.ast>;
25};
26
27$func Parse (e.tokens) e.expr = e.ast (e.tokens);
28Parse {
29  (e.tokens) /*empty*/ = (e.tokens);
30  (e.tokens) t.term e.expr = t.term : {
31    (Get s.func) =
32      <Apply s.func e.tokens> : e.ast (e.tokens_rest),
33      e.ast <Parse (e.tokens_rest) e.expr>;
34    (Skip s.tk) =
35      {
36        e.tokens : (s.tk t.pos) e.tokens_rest = <Parse (e.tokens_rest) e.expr>;
37        <Error (e.tokens) "expected " s.tk>;
38      };
39    (e.expr2) =
40      <Parse (e.tokens) e.expr2> :: e.ast (e.tokens),
41      (e.ast) <Parse (e.tokens) e.expr>;
42    s.symbol = s.symbol <Parse (e.tokens) e.expr>;
43  };
44};
45
46$func Error (e.tokens) e.message = (e.tokens);
47Error ((e.tk (e.pos)) e) e.message =
48  <Write e.pos> <Print ' --- 'e.message' while ('> <Write e.tk> <PrintLn ') have been got'>,
49  $error "Can't parse the program";
50
51$func Program e.tokens = e.ast (e.tokens);
52Program e.tokens = {
53  e.tokens : (DEC e) e = <Parse (e.tokens) (FUNC (Get &FunctionDec) (Get &VarDecAndFuncDef)) (Get &ProgramRest)>;
54  <Error (e.tokens) "program should consist of a number of function definitions">;
55};
56
57$func ProgramRest e.tokens = e.ast (e.tokens);
58ProgramRest e.tokens = {
59  e.tokens : (EOF e) = ();
60  e.tokens : (DEC e) e = <Program e.tokens>;
61  <Error (e.tokens) "expected function declaration">;
62};
63
64$func FunctionDec e.tokens = e.ast (e.tokens);
65FunctionDec (DEC e) e.tokens =
66  <Parse (e.tokens) (Get &Identifier) (Skip COLON) ((Get &FunctionType)) (Skip SC)>;
67
68$func VarDecAndFuncDef e.tokens = e.ast (e.tokens);
69VarDecAndFuncDef e.tokens = {
70  e.tokens : (VAR e) e = <Parse (e.tokens) ((Get &VariableDec)) (Get &Definition)>;
71  e.tokens : (DEF e) e = <Parse (e.tokens) () (Get &Definition)>;
72  <Error (e.tokens) "expected variable declarations or a function definition">;
73};
74
75$func VariableDec e.tokens = e.ast (e.tokens);
76VariableDec (VAR e) e.tokens =
77  <Parse (e.tokens) ((Get &VarsDecl)) (Get &VariableDecRest)>;
78
79$func VarsDecl e.tokens = e.ast (e.tokens);
80VarsDecl e.tokens = {
81  e.tokens : (IDENTIFIER e) e = <Parse (e.tokens) (Get &VariableList) (Skip COLON) (TYPE (Get &Identifier)) (Skip SC)>;
82  <Error (e.tokens) "expected the name of a variable to be declared">;
83};
84
85$func VariableDecRest e.tokens = e.ast (e.tokens);
86VariableDecRest e.tokens = {
87  e.tokens : (DEF e) e = (e.tokens);
88  e.tokens : (IDENTIFIER e) e = <Parse (e.tokens) ((Get &VarsDecl)) (Get &VariableDecRest)>;
89  <Error (e.tokens) "expected the name of a variable to be declared or 'def' and a function definition">;
90};
91
92$func VariableList e.tokens = e.ast (e.tokens);
93VariableList e.tokens = {
94  e.tokens : (IDENTIFIER e) e = <Parse (e.tokens) (VAR (Get &Identifier)) (Get &VariableListRest)>;
95  <Error (e.tokens) "expected the name of a variable to be declared">;
96};
97
98$func VariableListRest e.tokens = e.ast (e.tokens);
99VariableListRest e.tokens = {
100  e.tokens : (COLON e) e = (e.tokens);
101  e.tokens : (COMMA e) e.tokens_rest = <Parse (e.tokens_rest) (VAR (Get &Identifier)) (Get &VariableListRest)>;
102  <Error (e.tokens) "expected the name of a variable to be declared or ':' and a concept name">;
103};
104
105$func Definition e.tokens = e.ast (e.tokens);
106Definition e.tokens = {
107  e.tokens : (DEF e) e.tokens_rest = <Parse (e.tokens_rest) (Get &Equation) (Get &EquationRest)>;
108};
109
110$func Equation e.tokens = e.ast (e.tokens);
111Equation e.tokens = {
112  e.tokens : (IDENTIFIER e) e =
113    <Parse (e.tokens) (SENTENCE (Get &Identifier) (Skip LPAR) ((Get &PatternList)) (Skip RPAR) (Skip EQ) (Get &ExpWithWhereOrUndef) (Skip SC))>;
114  <Error (e.tokens) "expected the name of a function">;
115};
116
117$func EquationRest e.tokens = e.ast (e.tokens);
118EquationRest e.tokens = {
119  e.tokens : \{ (EOF e) e; (DEC e) e; } = (e.tokens);
120  e.tokens : (IDENTIFIER e) e = <Parse (e.tokens) (Get &Equation) (Get &EquationRest)>;
121  <Error (e.tokens) "expected another equation or new function declaration">;
122};
123
124$func PatternList e.tokens = e.ast (e.tokens);
125PatternList e.tokens = {
126  e.tokens : \{ (IDENTIFIER e) e; (STRING e) e; } = <Parse (e.tokens) ((Get &Pattern)) (Get &PatternListRest)>;
127  <Error (e.tokens) "expected a constant or a variable name">;
128};
129
130$func Pattern e.tokens = e.ast (e.tokens);
131Pattern e.tokens = {
132  e.tokens : (STRING e.str t.pos) e.tokens_rest = e.str <Parse (e.tokens_rest) (Get &PatConcat)>;
133  e.tokens : (IDENTIFIER e) e = <Parse (e.tokens) (VAR (Get &Identifier)) (Get &PatConcat)>;
134  <Error (e.tokens) "expected a constant or a variable name">;
135};
136
137$func PatConcat e.tokens = e.ast (e.tokens);
138PatConcat e.tokens = {
139  e.tokens : \{ (COMMA e) e; (RPAR e) e; } = (e.tokens);
140  e.tokens : (CONCAT e) e.tokens_rest = <Pattern e.tokens_rest>;
141  <Error (e.tokens) "expected '[]' or ',' or ')'">;
142};
143
144$func PatternListRest e.tokens = e.ast (e.tokens);
145PatternListRest e.tokens = {
146  e.tokens : (RPAR e) e = (e.tokens);
147  e.tokens : (COMMA e) e.tokens_rest =  <Parse (e.tokens_rest) ((Get &Pattern)) (Get &PatternListRest)>;
148  <Error (e.tokens) "expected ',' or ')'">;
149};
150
151$func FunctionType e.tokens = e.ast (e.tokens);
152FunctionType e.tokens = {
153  e.tokens : (IDENTIFIER e) e = <Parse (e.tokens) (TYPE (Get &Identifier)) (Get &ConceptListRest) (Skip TO) (TYPE (Get &Identifier))>;
154  <Error (e.tokens) "expected a type of a function argument">;
155};
156
157$func ConceptListRest e.tokens = e.ast (e.tokens);
158ConceptListRest e.tokens = {
159  e.tokens : (TO e) e = (e.tokens);
160  e.tokens : (ASTERISK e) e.tokens_rest = <Parse (e.tokens_rest) (TYPE (Get &Identifier)) (Get &ConceptListRest)>;
161  <Error (e.tokens) "expected '*' or '->'">;
162};
163
164$func Exp e.tokens = e.ast (e.tokens);
165Exp e.tokens = {
166  e.tokens : (STRING e.str t.pos) e.tokens_rest = e.str <ExpConcat e.tokens_rest>;
167  e.tokens : (IDENTIFIER e) e = <Parse (e.tokens) (IDENT (Get &Identifier) (Get &ExpVarOrFunc)) (Get &ExpConcat)>;
168  e.tokens : (LPAR e) e.tokens_rest = <Parse (e.tokens_rest) (PAREN (Get &ExpWithWhere)) (Skip RPAR) (Get &ExpConcat)>;
169  e.tokens : (IF e) e.tokens_rest = <Parse (e.tokens_rest) (IF ((Get &Exp)) (Skip THEN) ((Get &Exp)) (Skip ELSE) ((Get &Exp)))>;
170  <Error (e.tokens) "expected a constant, or an identifier, or '(', or 'if', or 'undefined'">;
171};
172
173$func ExpWithWhere e.tokens = e.ast (e.tokens);
174ExpWithWhere e.tokens = {
175  e.tokens : (STRING e.str t.pos) e.tokens_rest = e.str <ExpConcatOrWhere e.tokens_rest>;
176  e.tokens : (IDENTIFIER e) e = <Parse (e.tokens) (IDENT (Get &Identifier) (Get &ExpVarOrFunc)) (Get &ExpConcatOrWhere)>;
177  e.tokens : (LPAR e) e.tokens_rest = <Parse (e.tokens_rest) (PAREN (Get &ExpWithWhere)) (Skip RPAR) (Get &ExpConcatOrWhere)>;
178  e.tokens : (IF e) e.tokens_rest =
179     <Parse (e.tokens_rest) (IF ((Get &ExpWithWhere)) (Skip THEN) ((Get &ExpWithWhere)) (Skip ELSE) ((Get &ExpWithWhere)))>;
180  <Error (e.tokens) "expected a constant, or an identifier, or '(', or 'if', or 'undefined'">;
181};
182
183$func ExpWithWhereOrUndef e.tokens = e.ast (e.tokens);
184ExpWithWhereOrUndef e.tokens = {
185  e.tokens : (UNDEFINED e) = (UNDEFINED);
186  e.tokens : \{ (IDENTIFIER e) e;  (IF e) e; (LPAR e) e; (STRING e) e; } = <ExpWithWhere e.tokens>;
187};
188
189$func ExpVarOrFunc e.tokens = e.ast (e.tokens);
190ExpVarOrFunc e.tokens = {
191  e.tokens : \{ (COMMA e) e; (CONCAT e) e; (ELSE e) e; (RPAR e) e; (SC e) e; (THEN e) e; (WHERE e) e; } = VAR (e.tokens);
192  e.tokens : (LPAR e) e.tokens_rest = <Parse (e.tokens_rest) CALL (Get &ArgList) (Skip RPAR)>;
193  <Error (e.tokens)>;
194};
195
196$func ArgList e.tokens = e.ast (e.tokens);
197ArgList e.tokens = {
198  e.tokens : (RPAR e) e = (e.tokens);
199  e.tokens : \{ (IDENTIFIER e) e;  (IF e) e; (LPAR e) e; (STRING e) e; } =
200    <Parse (e.tokens) ((Get &Exp)) (Get &ArgListRest)>;
201  <Error (e.tokens) "expected a function argument or ')'">;
202};
203
204$func ArgListRest e.tokens = e.ast (e.tokens);
205ArgListRest e.tokens = {
206  e.tokens : (RPAR e) e = (e.tokens);
207  e.tokens : (COMMA e) e.tokens_rest = <Parse (e.tokens_rest) ((Get &Exp)) (Get &ArgListRest)>;
208  <Error (e.tokens) "expected ',' or ')'">;
209};
210
211$func ExpConcat e.tokens = e.ast (e.tokens);
212ExpConcat e.tokens = {
213  e.tokens : \{ (COMMA e) e; (ELSE e) e; (RPAR e) e; (SC e) e; (THEN e) e; } = (e.tokens);
214  e.tokens : (CONCAT e) e.tokens_rest = <Exp e.tokens_rest>;
215  <Error (e.tokens)>;
216};
217
218$func ExpConcatOrWhere e.tokens = e.ast (e.tokens);
219ExpConcatOrWhere e.tokens = {
220  e.tokens : \{ (ELSE e) e; (RPAR e) e; (SC e) e; (THEN e) e; } = (e.tokens);
221  e.tokens : (CONCAT e) e.tokens_rest = <ExpWithWhere e.tokens_rest>;
222  e.tokens : (WHERE e) e.tokens_rest = <Parse (e.tokens_rest) (WHERE (Get &VarassignList))>;
223  <Error (e.tokens)>;
224};
225
226$func VarassignList e.tokens = e.ast (e.tokens);
227VarassignList e.tokens = {
228  e.tokens : (IDENTIFIER e) e = <Parse (e.tokens) ((VAR (Get &Identifier)) (Skip EQ) (Get &Exp)) (Get &VarassignRest)>;
229  <Error (e.tokens) "expected a variable name">; 
230};
231
232$func VarassignRest e.tokens = e.ast (e.tokens);
233VarassignRest e.tokens = {
234  e.tokens : \{ (ELSE e) e; (RPAR e) e; (SC e) e; (THEN e) e; } = (e.tokens);
235  e.tokens : (COMMA e) e.tokens_rest = <VarassignList e.tokens_rest>;
236  <Error (e.tokens)>;
237};
238
239$func Identifier e.tokens = e.ast (e.tokens);
240Identifier {
241  (IDENTIFIER e.ident t.pos) e.tokens = (e.ident) (e.tokens);
242  e.tokens = <Error (e.tokens) "expected an identifier">;
243};
Note: See TracBrowser for help on using the repository browser.