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

Last change on this file since 1208 was 1208, checked in by orlov, 17 years ago
  • Put constant declarations in cc-files instead of hh-ones.
  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 12.1 KB
Line 
1// $Source$
2// $Revision: 1208 $
3// $Date: 2003-08-14 10:00:20 +0000 (Thu, 14 Aug 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.cont-label) (e.break-label) (e.cond) (e.step) e.body) =
80      {
81        e.cont-label : t =
82          ('{'
83            ('{' (<ASAIL-To-CPP e.body>) '}')
84            (LABEL <Rfp2Cpp (LABEL e.cont-label)> ': {}')
85          '}');
86        ('{' (<ASAIL-To-CPP e.body>) '}');
87      } :: e.body,
88      {
89        e.break-label : t = (LABEL <Rfp2Cpp (LABEL e.break-label)> ': {}');;
90      } :: e.break,
91      ('for ( ; ' <Cond-To-CPP e.cond> '; ' <Step-To-CPP e.step> ')') e.body e.break;
92    (LABEL (e.label) e.body) =
93      {
94        e.label : /*empty*/ =
95          ('{' (<ASAIL-To-CPP e.body>) '}' );
96        ('{' (<ASAIL-To-CPP e.body>) '}')
97        (LABEL <Rfp2Cpp (LABEL e.label)> ': {}');
98      };
99    (TRY e.body) =
100      ('trap') ('{' (<ASAIL-To-CPP e.body>) '}');
101    (CATCH-ERROR e.body) =
102      ('with') <ASAIL-To-CPP e.body>;
103    RETURN = ('return true;');
104    RETFAIL = ('retfail;');
105    FATAL = ('error ("Unexpected fail");');
106    (LSPLIT e.expr (e.min) t.var1 t.var2) =
107      ('lsplit (' <Expr-Ref-To-CPP e.expr> ', ' <Expr-Int-To-CPP e.min> ', '
108      <Rfp2Cpp t.var1> ', ' <Rfp2Cpp t.var2> ');');
109    (RSPLIT e.expr (e.min) t.var1 t.var2) =
110      ('rsplit (' <Expr-Ref-To-CPP e.expr> ', ' <Expr-Int-To-CPP e.min> ', '
111      <Rfp2Cpp t.var1> ', ' <Rfp2Cpp t.var2 > ');');
112    (ASSIGN t.var e.expr) =
113      (<Rfp2Cpp t.var> ' = ' <Expr-To-CPP () e.expr> ';');
114    (DECL s.type t.var) =
115      (s.type ' ' <Rfp2Cpp t.var> ';');
116    (INT  t.var e.expr) =
117      ('uintptr_t ' <Rfp2Cpp t.var> ' = ' <Expr-Int-To-CPP e.expr> ';');
118    (EXPR t.var e.expr) =
119      ('Expr ' <Rfp2Cpp t.var> ' (' <Expr-Ref-To-CPP e.expr> ');');
120    (DEREF t.var e.expr (e.pos)) =
121      ('Expr ' <Rfp2Cpp t.var> ' (' <Expr-Ref-To-CPP e.expr> ', '
122      <Expr-Int-To-CPP e.pos> ');');
123    (SUBEXPR t.var e.expr (e.pos) (e.len)) =
124      ('Expr ' <Rfp2Cpp t.var> ' ('
125      <Expr-Ref-To-CPP e.expr> ', ' <Expr-Int-To-CPP e.pos> ', '
126      <Expr-Int-To-CPP e.len> ');');
127    (DROP t.var) =
128      (<Rfp2Cpp t.var> '.drop ();');
129    (CONTINUE t.label) =
130      ('goto ' <Rfp2Cpp (LABEL t.label)> ';');
131    (BREAK t.label) =
132      ('goto ' <Rfp2Cpp (LABEL t.label)> ';');
133    (ERROR e.expr) =
134      ('error (' <Expr-Ref-To-CPP e.expr> ');');
135    (CONSTEXPR s.linkage t.name (e.comment) e.expr) =
136      { s.linkage : LOCAL = 'static ';; } :: e.linkage,
137      {
138        t.name : (STATIC e) = (<? &Module-Name>) t.name;
139        <RFP-Extract-Qualifiers t.name>;
140      } :: (e.qualifiers) e.name,
141      <Namespace-Control e.qualifiers>
142      (e.linkage 'const Expr ' <Rfp2Cpp e.name> ' = '
143        <Const-Expr-To-CPP e.expr> ';');
144    (OBJ s.linkage s.tag t.name) =
145      { s.linkage : LOCAL = 'static ';; } :: e.linkage,
146      <To-Chars s.tag> : s1 e2,
147      <RFP-Extract-Qualifiers t.name> :: (e.qualifiers) e.name,
148      <Namespace-Control e.qualifiers>
149      (e.linkage 'const Expr ' <Rfp2Cpp e.name>
150        ' = new rftype::' s1 <To-Lower e2> ' ();');
151    (DECL-OBJ t.name) =
152      <RFP-Extract-Qualifiers t.name> :: (e.qualifiers) e.name,
153      <Namespace-Control e.qualifiers>
154      ('extern const Expr ' <Rfp2Cpp e.name> ';');
155    (DECL-FUNC t.name) =
156      <RFP-Extract-Qualifiers t.name> :: (e.qualifiers) e.name,
157      <Namespace-Control e.qualifiers>
158      ('RF_DECL (' <Rfp2Cpp e.name> ');');
159    /*
160     * s.call can be CALL or TAILCALL
161     */
162    (s.call t.name (e.exprs) (e.ress)) =
163      ('RF_' s.call ' (' <Name-To-CPP t.name> ', '
164      '(' <Args-To-CPP () Exprs e.exprs> '), (' <Args-To-CPP () Vars e.ress> '));');
165  } :: e.cpp-item,
166    e.cpp-item <ASAIL-To-CPP e.rest>;
167  /*empty*/;
168};
169
170/*
171 * Determine type of e.expr - int or Refal.
172 */
173Expr-To-CPP  (e.init) e.expr-all, e.expr-all : {
174  /*empty*/ = <Expr-Ref-To-CPP e.init>;
175//  s.ObjectSymbol e.rest, {
176//    <Int? s.ObjectSymbol> = <Expr-Int-To-CPP e.init e.expr-all>;
177//    <Expr-Ref-To-CPP e.expr-all>;
178//  };   
179  (PAREN e.expr) e.rest = <Expr-Ref-To-CPP e.init e.expr-all>;
180  (EXPR e.expr) e.rest = <Expr-Ref-To-CPP e.init e.expr-all>;
181  (DEREF e.expr) e.rest = <Expr-Ref-To-CPP e.init e.expr-all>;
182  (SUBEXPR e.expr) e.rest = <Expr-Ref-To-CPP e.init e.expr-all>;
183  (LENGTH e.expr) e.rest = <Expr-Int-To-CPP e.init e.expr-all> ;
184  (MAX e.args) e.rest = <Expr-Int-To-CPP e.init e.expr-all>;         
185  (MIN e.args) e.rest = <Expr-Int-To-CPP e.init e.expr-all>;
186  (INFIX s.op e.args) e.rest = <Expr-Int-To-CPP e.init e.expr-all>;
187  (s.var-tag (e.QualifiedName)) e.rest =
188    <Expr-To-CPP (e.init (s.var-tag (e.QualifiedName))) e.rest>;
189};
190
191
192$func Term-Ref-To-CPP e = e;
193
194Expr-Ref-To-CPP {
195  /*empty*/ = 'empty';
196  term = <Term-Ref-To-CPP term>;
197  expr = '(' <Infix-To-CPP &Term-Ref-To-CPP "+" <Paren expr>> ')';
198};
199
200Term-Ref-To-CPP {
201  (PAREN e.expr) =
202    <Expr-Ref-To-CPP e.expr> ' ()';
203  (EXPR e.expr) =
204    'Expr (' <Expr-Ref-To-CPP e.expr> ')';
205  (DEREF e.expr (e.pos)) =
206    'Expr (' <Expr-Ref-To-CPP e.expr> ', ' <Expr-Int-To-CPP e.pos> ')';
207  (SUBEXPR e.expr (e.pos) (e.len)) =
208    'Expr (' <Expr-Ref-To-CPP e.expr> ', '
209        <Expr-Int-To-CPP e.pos>   ', ' <Expr-Int-To-CPP e.len> ')';
210  (REF t.name) = <Name-To-CPP t.name>;
211  ERROR-EXPR = 'err';
212  (s.var-tag t.name) = <Rfp2Cpp (s.var-tag t.name)>;
213};
214
215Expr-Int-To-CPP {
216  /*empty*/ = /*empty*/;
217  s.ObjectSymbol =
218    {
219      <Int? s.ObjectSymbol> = s.ObjectSymbol;
220      $error ("Illegal type int-symbol: " s.ObjectSymbol);
221    };
222  (LENGTH e.expr) =
223    <Expr-Ref-To-CPP e.expr> '.get_len ()';
224  (MAX e.args) =
225    'pxx_max (' <Args-To-CPP () Ints e.args> ')';
226  (MIN e.args) =
227    'pxx_min (' <Args-To-CPP () Ints e.args> ')';
228  (INFIX s.op e.args) =
229    '(' <Infix-To-CPP &Expr-Int-To-CPP s.op e.args> ')';
230  (REF t.name) = <Name-To-CPP t.name>;
231  (s.var-tag t.name) = <Rfp2Cpp (s.var-tag t.name)>;
232  expr = '(' <Infix-To-CPP &Expr-Int-To-CPP "+" <Paren expr>> ')';
233};
234
235Cond-To-CPP {
236  /*empty*/ = /*empty*/;
237  (CALL t.name (e.exprs) (e.ress)) =
238    'RF_CALL (' <Name-To-CPP t.name> ', '
239    '(' <Args-To-CPP () Exprs e.exprs> '), '
240    '(' <Args-To-CPP () Vars e.ress> '))';
241  (SYMBOL? e.expr (e.pos)) =
242    <Expr-Ref-To-CPP e.expr> '.symbol_at (' <Expr-Int-To-CPP e.pos> ')';
243  (FLAT-SUBEXPR? e.expr (e.pos) (e.len)) =
244    <Expr-Ref-To-CPP e.expr> '.flat_at ('
245      <Expr-Int-To-CPP e.pos> ', ' <Expr-Int-To-CPP e.len> ')';
246  (CHECK-ITER e.expr) =
247    'iter(' <Expr-Ref-To-CPP e.expr> ')';
248  (EQ e.expr1 (e.expr2) (e.pos)) =
249    <Expr-Ref-To-CPP e.expr1> '.eq ('
250      <Expr-Ref-To-CPP e.expr2> ', ' <Expr-Int-To-CPP e.pos> ')';
251  (TERM-EQ e.expr1 (e.expr2) (e.pos)) =
252    <Expr-Ref-To-CPP e.expr1> '.term_eq ('
253      <Expr-Ref-To-CPP e.expr2> ', ' <Expr-Int-To-CPP e.pos> ')';
254  (NOT e.cond) =
255    '!' <Cond-To-CPP e.cond>;
256  (INFIX s.op e.args) =
257    '(' <Infix-To-CPP <Op-Arg-To-CPP s.op> s.op e.args> ')';
258  expr = '(' <Infix-To-CPP &Cond-To-CPP "&&" <Paren expr>> ')';
259};
260
261Infix-To-CPP s.arg2cpp s.op e.args, {
262  e.args : (e.arg) e.rest =
263    <Apply s.arg2cpp e.arg> :: e.arg,
264    <Infix-To-CPP s.arg2cpp s.op e.rest> :: e.rest,
265    {
266      e.arg : v, e.rest : v = e.arg ' ' s.op ' ' e.rest;
267      e.arg e.rest;
268    };;
269};
270
271Op-Arg-To-CPP s.op, {
272  s.op : \{ "&&"; "||"; } =
273    &Cond-To-CPP;
274  s.op : \{ "<"; ">"; "<="; ">="; "=="; "!="; "+"; "-"; "%"; "*"; "/"; } =
275    &Expr-Int-To-CPP;
276};
277 
278Step-To-CPP {
279  /*empty*/ = /*empty*/;
280  (INC-ITER e.expr) = 'iter(' <Expr-Ref-To-CPP e.expr> ')++';
281  (DEC-ITER e.expr) = 'iter(' <Expr-Ref-To-CPP e.expr> ')--';
282};
283
284
285
286$func Const-Expr-Aux e.expr = e.cpp-expr;
287
288Const-Expr-To-CPP {
289  /*empty*/ = 'empty';
290  (SUBEXPR t.name s.pos s.len) = 'Expr (' <Rfp2Cpp t.name> ', ' s.pos ', ' s.len ')';
291                  //FIXME: надо проверять, что s.pos и s.len
292                  //       не превышают допустимых величин.
293                  //       Задавать эти величины опциями.
294  e.expr =
295    <Const-Expr-Aux () e.expr> : {
296      ' + ' e.cpp-expr = e.cpp-expr;
297      e.cpp-expr = e.cpp-expr;
298    };
299};
300
301Const-Expr-Aux (e.accum) e.expr, {
302  e.expr : s.sym e.rest, <Char? s.sym> =
303    <Const-Expr-Aux (e.accum <Symbol-To-CPP s.sym>) e.rest>;
304  e.accum : v =
305    ' + Char::create_expr ("' e.accum '")' <Const-Expr-Aux () e.expr>;
306  e.expr : t.item e.rest, t.item : {
307    (PAREN e.paren-expr) =
308      ' + (' <Const-Expr-To-CPP e.paren-expr> ') ()';
309    (REF t.name) =
310      ' + ' <Name-To-CPP t.name>;
311    (STATIC e) =
312      ' + ' <Rfp2Cpp t.item>;
313    (FUNC t.name) =
314      ' + Func::create_expr (' <Name-To-CPP t.name> ')';
315    s.sym, {
316      <Int? s.sym> =
317        ' + Integer::create_expr ("' s.sym '")';
318      <Word? s.sym> =
319        ' + Word::create_expr ("' <Symbol-To-CPP s.sym> '")';
320    };
321  } :: e.cpp-item =
322    e.cpp-item <Const-Expr-Aux () e.rest>;
323  = /*empty*/;
324};
325
326Symbol-To-CPP s.ObjectSymbol, {
327  <To-Chars s.ObjectSymbol> () $iter {
328    e.symbol : s.char e.rest, s.char : {
329      '\\' = '\\\\';
330      '\n' = '\\n';
331      '\t' = '\\t';
332//        '\v' = '\\v';
333//        '\b' = '\\b';
334      '\r' = '\\r';
335//        '\f' = '\\f';
336      '\"' = '\\"';
337//      '\'' = '\\\'';
338      s = s.char;
339    } :: e.cpp-char,
340    e.rest (e.cpp-symbol e.cpp-char);
341  } :: e.symbol (e.cpp-symbol),
342    e.symbol : /*empty*/ =
343    e.cpp-symbol;
344};
345
346
347
348Args-To-CPP {
349  (e.prefix) Vars /*empty*/ = /*empty*/;
350  (e.prefix) s.tag e.args =
351    e.args () $iter {
352      e.args : (e.arg) e.rest =
353        {
354          e.rest : v = ', ';
355          /*empty*/;
356        } :: e.comma,
357        s.tag : {
358          Vars = e.rest (e.cpp-args <Rfp2Cpp (e.arg)> e.comma);
359          Exprs = e.rest (e.cpp-args <Expr-Ref-To-CPP e.arg> e.comma);
360          Ints = e.rest (e.cpp-args <Expr-Int-To-CPP e.arg> e.comma);
361        };
362    } :: e.args (e.cpp-args),
363    e.args : /*empty*/,
364    e.prefix e.cpp-args;
365};
366
367Name-To-CPP t.obj-name =
368  <RFP-Extract-Qualifiers t.obj-name> :: (e.qualifiers) e.name,
369  <? &Current-Namespace> :: e.namespace,
370  {
371    e.qualifiers : e.namespace e.cont = <Rfp2Cpp e.cont e.name>;
372    <Rfp2Cpp t.obj-name>;
373  };
374
375Namespace-Control e.qualifiers, {
376  <? &Current-Namespace> : e.qualifiers;
377  {
378    <? &Current-Namespace> : v = ('}');;
379  } :: e.close-namespace,
380    <Store &Current-Namespace e.qualifiers>,
381    e.close-namespace ('namespace ' <Rfp2Cpp e.qualifiers> '\n{');
382};
383
Note: See TracBrowser for help on using the repository browser.