source: to-imperative/trunk/compiler/rfp_asail.rf @ 750

Last change on this file since 750 was 750, checked in by orlov, 18 years ago
  • Work towards clashes compilation.
  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 12.0 KB
Line 
1// $Source$
2// $Revision: 750 $
3// $Date: 2003-05-22 09:41:13 +0000 (Thu, 22 May 2003) $
4
5$use Apply Box Class Compare Convert StdIO Table;
6$use "rfpc";
7$use "rfp_helper";
8$use "rfp_list";
9$use "rfp_mangle";
10
11$box Module-Name;
12
13$box Current-Namespace;
14
15$box Entry;
16
17$box Entry-Name;
18
19$func ASAIL-To-CPP e.body = e.cpp-code;
20
21$func Namespace-Control e.qualifiers = e.namespace-control;
22
23$func Expr-To-CPP (e.ASAIL-Expr-init ) e.ASAIL-Expr-rest = e.ASAIL-Expr;
24
25$func Expr-Ref-To-CPP e.ASAIL-Expr-Ref = e.CPP-Expr-Ref;
26
27$func Expr-Int-To-CPP e.ASAIL-Expr-Int = e.CPP-Expr-Int;
28
29$func Step-To-CPP e.step-operators = e.cpp-step-operators;
30
31$func Const-Expr-To-CPP e.ASAIL-const-expr = e.CPP-const-expr;
32
33$func Args-To-CPP (e.prefix) s.Arg-Res-Tag e.ASAIL-Args = e.CPP-Args;
34
35$func Symbol-To-CPP s.RFP-Symbol = e.CPP-String;
36
37$func Name-To-CPP t.name = e.CPP-Name;
38
39$func Cond-To-CPP e.cond = e.CPP-Cond;
40
41$func Infix-To-CPP s.func-for-converting-args-to-cpp s.op e.args = e.cpp-expr;
42
43$func Op-Arg-To-CPP s.op = s.func-for-converting-args-to-cpp;
44
45RFP-ASAIL-To-CPP (e.ModuleName) e.asail =
46  <Store &Module-Name e.ModuleName>,
47  <Store &Current-Namespace /*empty*/>,
48  <Store &Entry (e.ModuleName Main)>,
49  <Store &Entry-Name /*empty*/>,
50  {
51    <ASAIL-To-CPP e.asail> : v.cpp,
52      {
53        <? &Current-Namespace> : v = ('}');;  // close last namespace
54      } :: e.close-namespace,
55      <Store &Current-Namespace /*empty*/>,
56      {
57        <? &Entry-Name> : v.name = ('rfrt::Entry rf_entry (' v.name ');');;
58      } :: e.entry,
59      ('namespace refal\n{')
60      ('using namespace rfrt;')
61      v.cpp e.close-namespace e.entry
62      ('}');;
63  };
64
65ASAIL-To-CPP e.asail, {
66  e.asail : t.item e.rest, t.item : {
67    (FUNC t.name (e.args) (e.ress) e.body) =
68      { <? &Entry> : t.name = <Store &Entry-Name <Rfp2Cpp t.name>>;; },
69      <RFP-Extract-Qualifiers t.name> :: (e.qualifiers) e.name,
70      <Namespace-Control e.qualifiers>
71      ('RF_FUNC (' <Rfp2Cpp e.name> ', '
72        '(' <Args-To-CPP ('RF_ARG ') Vars e.args> '), '
73        '(' <Args-To-CPP ('RF_RES ') Vars e.ress> '))'
74          (<ASAIL-To-CPP e.body>)
75       'RF_END');
76    (IF (e.cond) e.body) =
77      ('if (' <Cond-To-CPP e.cond> ')')
78      ('{' (<ASAIL-To-CPP e.body>) '}');
79    (FOR (e.label) (e.cond) (e.step) e.body) =
80      {
81        e.label : t = (LABEL <Rfp2Cpp (LABEL e.label)> ': {}');;
82      } :: e.label,
83      ('for ( ; ' <Cond-To-CPP e.cond> '; ' <Step-To-CPP e.step> ')')
84      ('{' ('{' (<ASAIL-To-CPP e.body>) '}') e.label '}');
85    (LABEL (e.label) e.body) =
86      {
87        e.label : /*empty*/ =
88          ('{' (<ASAIL-To-CPP e.body>) '}' );
89        ('{' (<ASAIL-To-CPP e.body>) '}')
90        (LABEL <Rfp2Cpp (LABEL e.label)> ': {}');
91      };
92    (TRY e.body) =
93      ('trap {' (<ASAIL-To-CPP e.body>) '}');
94    (CATCH-ERROR e.body) =
95      ('with {' (<ASAIL-To-CPP e.body>) '}');
96    RETURN = ('return true;');
97    RETFAIL = ('retfail;');
98    FATAL = ('error ("Unexpected fail");');
99    (LSPLIT e.expr (e.min) t.var1 t.var2) =
100      ('lsplit (' <Expr-Ref-To-CPP e.expr> ', ' <Expr-Int-To-CPP e.min> ', '
101      <Rfp2Cpp t.var1> ', ' <Rfp2Cpp t.var2> ');');
102    (RSPLIT e.expr (e.min) t.var1 t.var2) =
103      ('rsplit (' <Expr-Ref-To-CPP e.expr> ', ' <Expr-Int-To-CPP e.min> ', '
104      <Rfp2Cpp t.var1> ', ' <Rfp2Cpp t.var2 > ');');
105    (ASSIGN t.var e.expr) =
106      (<Rfp2Cpp t.var> ' = ' <Expr-To-CPP () e.expr> ';');
107    (DECL s.type t.var) =
108      (s.type ' ' <Rfp2Cpp t.var> ';');
109    (INT  t.var e.expr) =
110      ('uintptr_t ' <Rfp2Cpp t.var> ' = ' <Expr-Int-To-CPP e.expr> ';');
111    (EXPR t.var e.expr) =
112      ('Expr ' <Rfp2Cpp t.var> ' (' <Expr-Ref-To-CPP e.expr> ');');
113    (DEREF t.var e.expr (e.pos)) =
114      ('Expr ' <Rfp2Cpp t.var> ' (' <Expr-Ref-To-CPP e.expr> ', '
115      <Expr-Int-To-CPP e.pos> ');');
116    (SUBEXPR t.var e.expr (e.pos) (e.len)) =
117      ('Expr ' <Rfp2Cpp t.var> ' ('
118      <Expr-Ref-To-CPP e.expr> ', ' <Expr-Int-To-CPP e.pos> ', '
119      <Expr-Int-To-CPP e.len> ');');
120    (DROP t.var) =
121      (<Rfp2Cpp t.var> '.drop ();');
122    (CONTINUE t.label) =
123      ('goto ' <Rfp2Cpp (LABEL t.label)> ';');
124    (BREAK t.label) =
125      ('goto ' <Rfp2Cpp (LABEL t.label)> ';');
126    (ERROR e.expr) =
127      ('error (' <Expr-Ref-To-CPP e.expr> ');');
128    (CONSTEXPR s.linkage t.name (e.comment) e.expr) =
129      { s.linkage : LOCAL = 'static ';; } :: e.linkage,
130      {
131        t.name : (STATIC e) = (<? &Module-Name>) t.name;
132        <RFP-Extract-Qualifiers t.name>;
133      } :: (e.qualifiers) e.name,
134      <Namespace-Control e.qualifiers>
135      (e.linkage 'const Expr ' <Rfp2Cpp e.name> ' = '
136        <Const-Expr-To-CPP e.expr> ';');
137    (DECL-OBJ s.linkage s.tag t.name) =
138      { s.linkage : LOCAL = 'static ';; } :: e.linkage,
139      <To-Chars s.tag> : s1 e2,
140      <RFP-Extract-Qualifiers t.name> :: (e.qualifiers) e.name,
141      <Namespace-Control e.qualifiers>
142      (e.linkage 'const Expr ' <Rfp2Cpp e.name>
143        ' = new rftype::' s1 <To-Lower e2> ' ();');
144    (DECL-FUNC s.linkage t.name) =
145      <RFP-Extract-Qualifiers t.name> :: (e.qualifiers) e.name,
146      <Namespace-Control e.qualifiers>
147      ('RF_DECL (' <Rfp2Cpp e.name> ');');
148    /*
149     * s.call can be CALL or TAILCALL
150     */
151    (s.call t.name (e.exprs) (e.ress)) =
152      ('RF_' s.call ' (' <Name-To-CPP t.name> ', '
153      '(' <Args-To-CPP () Exprs e.exprs> '), (' <Args-To-CPP () Vars e.ress> '));');
154  } :: e.cpp-item,
155    e.cpp-item <ASAIL-To-CPP e.rest>;
156  /*empty*/;
157};
158
159/*
160 * Determine type of e.expr - int or Refal.
161 */
162Expr-To-CPP  (e.init) e.expr-all, e.expr-all : {
163  /*empty*/ = <Expr-Ref-To-CPP e.init>;
164//  s.ObjectSymbol e.rest, {
165//    <Int? s.ObjectSymbol> = <Expr-Int-To-CPP e.init e.expr-all>;
166//    <Expr-Ref-To-CPP e.expr-all>;
167//  };   
168  (PAREN e.expr) e.rest = <Expr-Ref-To-CPP e.init e.expr-all>;
169  (EXPR e.expr) e.rest = <Expr-Ref-To-CPP e.init e.expr-all>;
170  (DEREF e.expr) e.rest = <Expr-Ref-To-CPP e.init e.expr-all>;
171  (SUBEXPR e.expr) e.rest = <Expr-Ref-To-CPP e.init e.expr-all>;
172  (LENGTH e.expr) e.rest = <Expr-Int-To-CPP e.init e.expr-all> ;
173  (MAX e.args) e.rest = <Expr-Int-To-CPP e.init e.expr-all>;         
174  (MIN e.args) e.rest = <Expr-Int-To-CPP e.init e.expr-all>;
175  (INFIX s.op e.args) e.rest = <Expr-Int-To-CPP e.init e.expr-all>;
176  (s.var-tag (e.QualifiedName)) e.rest =
177    <Expr-To-CPP (e.init (s.var-tag (e.QualifiedName))) e.rest>;
178};
179
180
181$func Term-Ref-To-CPP e = e;
182
183Expr-Ref-To-CPP {
184  /*empty*/ = 'empty';
185  term = <Term-Ref-To-CPP term>;
186  expr = '(' <Infix-To-CPP &Term-Ref-To-CPP "+" <Paren expr>> ')';
187};
188
189Term-Ref-To-CPP {
190  (PAREN e.expr) =
191    <Expr-Ref-To-CPP e.expr> ' ()';
192  (EXPR e.expr) =
193    'Expr (' <Expr-Ref-To-CPP e.expr> ')';
194  (DEREF e.expr (e.pos)) =
195    'Expr (' <Expr-Ref-To-CPP e.expr> ', ' <Expr-Int-To-CPP e.pos> ')';
196  (SUBEXPR e.expr (e.pos) (e.len)) =
197    'Expr (' <Expr-Ref-To-CPP e.expr> ', '
198        <Expr-Int-To-CPP e.pos>   ', ' <Expr-Int-To-CPP e.len> ')';
199  (REF t.name) = <Name-To-CPP t.name>;
200  (s.var-tag t.name) = <Rfp2Cpp (s.var-tag t.name)>;
201};
202
203Expr-Int-To-CPP {
204  /*empty*/ = /*empty*/;
205  s.ObjectSymbol =
206    {
207      <Int? s.ObjectSymbol> = s.ObjectSymbol;
208      $error ("Illegal type int-symbol: " s.ObjectSymbol);
209    };
210  (LENGTH e.expr) =
211    <Expr-Ref-To-CPP e.expr> '.get_len ()';
212  (MAX e.args) =
213    'pxx_max (' <Args-To-CPP () Ints e.args> ')';
214  (MIN e.args) =
215    'pxx_min (' <Args-To-CPP () Ints e.args> ')';
216  (INFIX s.op e.args) =
217    '(' <Infix-To-CPP &Expr-Int-To-CPP s.op e.args> ')';
218  (REF t.name) = <Name-To-CPP t.name>;
219  (s.var-tag t.name) = <Rfp2Cpp (s.var-tag t.name)>;
220  expr = '(' <Infix-To-CPP &Expr-Int-To-CPP "+" <Paren expr>> ')';
221};
222
223Cond-To-CPP {
224  /*empty*/ = /*empty*/;
225  (CALL t.name (e.exprs) (e.ress)) =
226    'RF_CALL (' <Name-To-CPP t.name> ', '
227    '(' <Args-To-CPP () Exprs e.exprs> '), '
228    '(' <Args-To-CPP () Vars e.ress> '))';
229  (SYMBOL? e.expr (e.pos)) =
230    <Expr-Ref-To-CPP e.expr> '.symbol_at (' <Expr-Int-To-CPP e.pos> ')';
231  (FLAT-SUBEXPR? e.expr (e.pos) (e.len)) =
232    <Expr-Ref-To-CPP e.expr> '.flat_at ('
233      <Expr-Int-To-CPP e.pos> ', ' <Expr-Int-To-CPP e.len> ')';
234  /*
235   * EQ -> to function eq() with 6 arg.
236   */
237  (EQ (e.expr1) (e.pos1) (e.len1) (e.expr2) (e.pos2) (e.len2)) =
238    'Expr::eq ('<Expr-Ref-To-CPP e.expr1> ', '
239          <Expr-Int-To-CPP e.pos1>  ', ' <Expr-Int-To-CPP e.len1> ', '
240          <Expr-Ref-To-CPP e.expr2> ', '
241          <Expr-Int-To-CPP e.pos2>  ', ' <Expr-Int-To-CPP e.len2> ')';
242  /*
243   * FLAT-EQ -> to function flat_eq() with 5 arg.
244   */
245  (FLAT-EQ (e.expr1) (e.pos1) (e.expr2) (e.pos2) (e.len)) =
246    'Expr::flat_eq ('<Expr-Ref-To-CPP e.expr1> ', ' <Expr-Int-To-CPP e.pos1>
247          ', ' <Expr-Ref-To-CPP e.expr2> ', ' <Expr-Int-To-CPP e.pos2>
248          ', ' <Expr-Int-To-CPP e.len> ')';
249  (NOT e.cond) =
250    '!' <Cond-To-CPP e.cond>;
251  (INFIX s.op e.args) =
252    '(' <Infix-To-CPP <Op-Arg-To-CPP s.op> s.op e.args> ')';
253  expr = '(' <Infix-To-CPP &Cond-To-CPP "&&" <Paren expr>> ')';
254};
255
256Infix-To-CPP s.arg2cpp s.op e.args, {
257  e.args : (e.arg) e.rest =
258    <Apply s.arg2cpp e.arg> :: e.arg,
259    <Infix-To-CPP s.arg2cpp s.op e.rest> :: e.rest,
260    {
261      e.arg : v, e.rest : v = e.arg ' ' s.op ' ' e.rest;
262      e.arg e.rest;
263    };;
264};
265
266Op-Arg-To-CPP s.op, {
267  s.op : \{ "&&"; "||"; } =
268    &Cond-To-CPP;
269  s.op : \{ "<"; ">"; "<="; ">="; "=="; "!="; "+"; "-"; "%"; "*"; "/"; } =
270    &Expr-Int-To-CPP;
271};
272 
273Step-To-CPP {
274  /*empty*/ = /*empty*/;
275  (INC-ITER t.var) = 'iter(' <Rfp2Cpp t.var> ')++';
276};
277
278
279
280$func Const-Expr-Aux e.expr = e.cpp-expr;
281
282Const-Expr-To-CPP {
283  /*empty*/ = 'empty';
284  (SUBEXPR t.name s.pos s.len) = 'Expr (' <Rfp2Cpp t.name> ', ' s.pos ', ' s.len ')';
285                  //FIXME: надо проверять, что s.pos и s.len
286                  //       не превышают допустимых величин.
287                  //       Задавать эти величины опциями.
288  e.expr =
289    <Const-Expr-Aux () e.expr> : {
290      ' + ' e.cpp-expr = e.cpp-expr;
291      e.cpp-expr = e.cpp-expr;
292    };
293};
294
295Const-Expr-Aux (e.accum) e.expr, {
296  e.expr : s.sym e.rest, <Char? s.sym> =
297    <Const-Expr-Aux (e.accum <Symbol-To-CPP s.sym>) e.rest>;
298  e.accum : v =
299    ' + Char::create_expr ("' e.accum '")' <Const-Expr-Aux () e.expr>;
300  e.expr : t.item e.rest, t.item : {
301    (PAREN e.paren-expr) =
302      ' + (' <Const-Expr-To-CPP e.paren-expr> ') ()';
303    (REF t.name) =
304      ' + ' <Name-To-CPP t.name>;
305    (STATIC e) =
306      ' + ' <Rfp2Cpp t.item>;
307    s.sym, {
308      <Int? s.sym>, {
309        <"<" (<Abs s.sym>) (2147483648)> =              //FIXME: значение должно
310                            //       задаваться опцией.
311          ' + ShortInt::create_expr (' s.sym ')';
312        ' + Int::create_expr (' s.sym ')';
313      };
314      <Word? s.sym> =
315        ' + Word::create_expr ("' <Symbol-To-CPP s.sym> '")';
316    };
317  } :: e.cpp-item =
318    e.cpp-item <Const-Expr-Aux () e.rest>;
319  = /*empty*/;
320};
321
322Symbol-To-CPP s.ObjectSymbol, {
323  <To-Chars s.ObjectSymbol> () $iter {
324    e.symbol : s.char e.rest, s.char : {
325      '\\' = '\\\\';
326      '\n' = '\\n';
327      '\t' = '\\t';
328//        '\v' = '\\v';
329//        '\b' = '\\b';
330      '\r' = '\\r';
331//        '\f' = '\\f';
332      '\"' = '\\"';
333      '\'' = '\\\'';
334      s = s.char;
335    } :: e.cpp-char,
336    e.rest (e.cpp-symbol e.cpp-char);
337  } :: e.symbol (e.cpp-symbol),
338    e.symbol : /*empty*/ =
339    e.cpp-symbol;
340};
341
342
343
344Args-To-CPP {
345  (e.prefix) Vars /*empty*/ = /*empty*/;
346  (e.prefix) s.tag e.args =
347    e.args () $iter {
348      e.args : (e.arg) e.rest =
349        {
350          e.rest : v = ', ';
351          /*empty*/;
352        } :: e.comma,
353        s.tag : {
354          Vars = e.rest (e.cpp-args <Rfp2Cpp (e.arg)> e.comma);
355          Exprs = e.rest (e.cpp-args <Expr-Ref-To-CPP e.arg> e.comma);
356          Ints = e.rest (e.cpp-args <Expr-Int-To-CPP e.arg> e.comma);
357        };
358    } :: e.args (e.cpp-args),
359    e.args : /*empty*/,
360    e.prefix e.cpp-args;
361};
362
363Name-To-CPP t.obj-name =
364  <RFP-Extract-Qualifiers t.obj-name> :: (e.qualifiers) e.name,
365  <? &Current-Namespace> :: e.namespace,
366  {
367    e.qualifiers : e.namespace e.cont = <Rfp2Cpp e.cont e.name>;
368    <Rfp2Cpp t.obj-name>;
369  };
370
371Namespace-Control e.qualifiers, {
372  <? &Current-Namespace> : e.qualifiers;
373  {
374    <? &Current-Namespace> : v = ('}');;
375  } :: e.close-namespace,
376    <Store &Current-Namespace e.qualifiers>,
377    e.close-namespace ('namespace ' <Rfp2Cpp e.qualifiers> '\n{');
378};
379
Note: See TracBrowser for help on using the repository browser.