source: to-imperative/trunk/runtime/rf_short_int.cc @ 1106

Last change on this file since 1106 was 1106, checked in by luba, 17 years ago
  • type_short_int is replaced by type_int32, class ShortInt? is renamed to Int32
  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 3.1 KB
Line 
1// $Source$
2// $Revision: 1106 $
3// $Date: 2003-08-06 13:30:09 +0000 (Wed, 06 Aug 2003) $
4// $Author: luba $
5
6#include "rf_short_int.ih"
7#include "rf_types.ih"
8#include "rf_expr.ih"
9#include "rf_char.ih"
10#include "rf_word.ih"
11#include "wchar.h"
12
13namespace rftype
14{
15
16unsigned Int32::compute_max_len ()
17{
18  intptr_t i   = INTPTR_MAX / 10000;
19  unsigned len = 4;
20  for ( ; i; i /= 10) len++;
21  return len;
22}
23
24const unsigned Int32::max_len = compute_max_len();
25
26Expr Int32::create_expr (intptr_t _n)
27{
28  Expr e = Term::create_expr(1);
29  new(e.get_first()) Int32(_n);
30  return e;
31}
32
33
34Expr Int32::create_expr (pxx::WString& _str) {
35  wchar_t const* s = _str.get_data();
36  return Int32::create_expr( wcstol(s, null, 10));
37}
38
39#if 0
40Expr ShortInt::create_expr (Expr& _expr, int _flag)
41{
42  Term* p = _expr.get_first();
43  Term* q = _expr.get_last();
44  int d = 0;
45  intptr_t n = 0;
46
47  if (p->get_type() == type_short_int) {
48    n = ((ShortInt*)p)->to_int();
49    if (n < 0) {
50      n = -n;
51      d = -1;
52      _flag = -1;
53    }
54    else {
55      _flag = 1;
56      d = 1;
57    }
58  }
59  if (p->get_type() == type_word) {
60    d = 0;
61  }
62  if (p->get_type() == type_char) {
63    d = _flag;
64  }
65
66  for (p += _flag * d; p < q; p++) {
67    if (p->get_type() == type_short_int) {
68      intptr_t n_temp = ((ShortInt*)p)->to_int() / 10;
69      intptr_t k      = 10;
70
71      while (n_temp) {
72        n_temp /= 10;
73        k *= 10;
74      }
75
76      n = n * k + ((ShortInt*)p)->to_int();
77
78    }
79    if (p->get_type() == type_char) {
80      if (((Char*)p)->is_digit())
81        n = n * 10 + (((Char*)p)->to_wchar_t() - 48);
82    }
83    if (p->get_type() == type_word) {
84      float n_word = 0;
85      float k_word = 0.1;
86
87      Word::NumIterator i = *((Word*)p);
88
89      while (i.in_bounds()) {
90        k_word *= 10;
91        i++;
92      }
93
94      Word::NumIterator j = *((Word*)p);
95
96      for (; j.in_bounds(); j++){
97        n_word += (*j) * k_word;
98        k_word /= 10;
99      }
100      n += size_t (n_word);
101    }
102  }
103  if (-1 == _flag) n = -n;
104
105  Expr e = Term::create_expr(1);
106  new(e.get_first()) ShortInt(n);
107  return e;
108}
109#endif
110
111void Int32::ctor (Int32* _to, Int32 const* _from)
112{
113  _to->data1 = _from->data1;
114  _to->uint_data2 = _from->uint_data2;
115}
116
117int Int32::compare (Int32 const* _shint1, Int32 const* _shint2)
118{
119  if (_shint1->int_data2 == _shint2->int_data2) return 0;
120  else if (_shint1->int_data2 < _shint2->int_data2) return -1;
121  else return 1;
122}
123
124uint32_t Int32::hash (Int32 const* _shint)
125{
126  return _shint->uint_data2;
127}
128
129pxx::WString Int32::to_string (Int32 const* _shint) {
130  intptr_t n = _shint->int_data2;
131  int flag = 0;
132
133  if (n < 0) {
134    n = -n;
135    flag = -1;
136  }
137
138  wchar_t str[max_len + 1];
139  int i = max_len;
140
141  if (!n) {
142    str[i] = 48;
143  }
144  else {
145    while (n) {
146      str[i] = n % 10 + 48;
147      n /= 10;
148      i--;
149    };
150
151    if (flag) {
152      str[i] = '-';
153    }
154    else i++;
155  }
156  return pxx::WString(&str[i], max_len + 1 - i);
157
158}
159
160short_type_funcs_t Int32::funcs = {
161  (void (*)(Term*, Term const*))(Int32::ctor),
162  null, // ShortInt::dtor,
163  null, // ShortInt::eq,
164  (int (*)(Term const*, Term const*))(Int32::compare),
165  (uint32_t (*)(Term const*))(Int32::hash),
166  (pxx::WString (*)(Term const*))(Int32::to_string)
167};
168
169}
170
Note: See TracBrowser for help on using the repository browser.