source: to-imperative/trunk/samples/Rational/Rational.rf @ 2303

Last change on this file since 2303 was 2218, checked in by yura, 14 years ago

Return fail in QToSring if empty string is given.

File size: 1.7 KB
Line 
1// vim: set et ts=2 sw=2 :
2
3$use Access;
4$use Arithm;
5$use Convert;
6$use Compare;
7
8$func QSimplify s s = t;
9$func QConvert t = s s;
10
11QAdd tX tY, <QConvert tX> :: sX1 sX2, <QConvert tY> :: sY1 sY2,
12  <QSimplify <"+" <"*" sX1 sY2> <"*" sX2 sY1>> <"*" sX2 sY2>>;
13
14QSub tX tY, <QConvert tX> :: sX1 sX2, <QConvert tY> :: sY1 sY2,
15  <QSimplify <"-" <"*" sX1 sY2> <"*" sX2 sY1>> <"*" sX2 sY2>>;
16
17QMul tX tY, <QConvert tX> :: sX1 sX2, <QConvert tY> :: sY1 sY2,
18  <QSimplify <"*" sX1 sY1> <"*" sX2 sY2>>;
19
20QDiv tX tY, <QConvert tX> :: sX1 sX2, <QConvert tY> :: sY1 sY2,
21  {
22    <Compare (sY1) (0)> : '<', <"*" sY1 -1> <"*" sY2 -1>;
23    sY1 sY2;
24  } :: sY1 sY2,
25  <QSimplify <"*" sX1 sY2> <"*" sX2 sY1>>;
26
27QCompare tX tY, <QConvert tX> :: sX1 sX2, <QConvert tY> :: sY1 sY2,
28  <Compare (<"*" sX1 sY2>) (<"*" sX2 sY1>)>;
29
30QToInt tX, <Div-Rem <QConvert tX>> :: sQ sR, sQ;
31
32QToString sN tX, <QConvert tX> :: s1 s2,
33  <Div-Rem s1 s2> :: sQ sR,
34  {
35    <Compare (s1) (0)> :'<', { sQ : 0, '-'; /*empty*/;} :: eSing, eSing sQ <"*" sR -1>;
36    sQ sR;
37  } :: eOut sR,
38  sN sR /*empty*/
39  $iter {
40     <Div-Rem <"*" sR 10> s2> :: sQ sR,
41     <"-" sN 1> sR eOut2 sQ;
42  } :: sN sR eOut2, sN : 0 =
43  'T' eOut2
44  $iter {
45    eOut2 : eOut3 0, 'T' eOut3;
46    'F' eOut2;
47  } :: sP eOut2, sP : 'F' =
48  {
49    eOut2 : /*empty*/, eOut;
50    eOut '.' eOut2;
51  } :: eOut, <To-Chars eOut>;
52
53QFromString \{
54  eIn1 '.' eIn2 =
55  {eIn1 : /*empty*/, '0'; eIn1;} :: eIn1,
56  <Length eIn2> 1
57  $iter <"-" sN 1> <"*" sM 10> ::
58  sN sM, sN : 0 =
59  \{
60    eIn1 : e '-' e = <QSub <To-Int eIn1> <QDiv <To-Int eIn2> sM>>;
61    <QAdd <To-Int eIn1> <QDiv <To-Int eIn2> sM>>;
62  };
63
64  eIn1 = (<To-Int eIn1> 1);
65};
66
67
68QSimplify s1 s2, <GCD s1 s2> :: s3, (<Div s1 s3> <Div s2 s3>);
69
70QConvert {
71  (s1 s2) = s1 s2;
72  s1 = s1 1;
73};
Note: See TracBrowser for help on using the repository browser.