1 | // $Source$ |
---|
2 | // $Revision: 730 $ |
---|
3 | // $Date: 2003-05-16 07:37:44 +0000 (Fri, 16 May 2003) $ |
---|
4 | // $Author: luba $ |
---|
5 | |
---|
6 | #include <rf_core.hh> |
---|
7 | |
---|
8 | namespace refal |
---|
9 | { |
---|
10 | |
---|
11 | using namespace rfrt; |
---|
12 | using namespace rftype; |
---|
13 | |
---|
14 | namespace Convert |
---|
15 | { |
---|
16 | |
---|
17 | RF_FUNC (To_m_Int, (RF_ARG e_Exp), (RF_RES s_Int)) |
---|
18 | uintptr_t len = 0; |
---|
19 | int flag = 0; |
---|
20 | |
---|
21 | Term* t = e_Exp.get_first(); |
---|
22 | |
---|
23 | if (t->get_type() == type_char) { |
---|
24 | if (((Char*)t)->to_wchar_t() == '-') { |
---|
25 | flag = -1; t++; |
---|
26 | } |
---|
27 | else |
---|
28 | if (((Char*)t)->to_wchar_t() == '+') { |
---|
29 | flag = 1; t++; |
---|
30 | } |
---|
31 | }; |
---|
32 | |
---|
33 | if ((t->get_type() == type_word) ) |
---|
34 | if (((Word*)t)->is_number() == -1) |
---|
35 | flag = -1; |
---|
36 | else |
---|
37 | if(((Word*)t)->is_number() == 1) |
---|
38 | flag = 1; |
---|
39 | else |
---|
40 | retfail; |
---|
41 | |
---|
42 | |
---|
43 | if (t->get_type() == type_short_int) |
---|
44 | t++; |
---|
45 | |
---|
46 | for (; t < e_Exp.get_last(); t++) { |
---|
47 | if (t->get_type() == type_char && ((Char*)t)->is_digit()) |
---|
48 | if (len > UINTPTR_MAX - 1) |
---|
49 | RF_LIB_ERROR("Argument too large for conversion"); |
---|
50 | else |
---|
51 | len += 1; |
---|
52 | else |
---|
53 | if (t->get_type() == type_short_int) { |
---|
54 | if (((ShortInt*)t)->to_int() < 0) |
---|
55 | retfail; |
---|
56 | else { |
---|
57 | size_t num_len = ((ShortInt*)t)->get_char_len(); |
---|
58 | if (len > UINTPTR_MAX - num_len) |
---|
59 | RF_LIB_ERROR("Argument too large for conversion"); |
---|
60 | else |
---|
61 | len += num_len; |
---|
62 | } |
---|
63 | } |
---|
64 | else |
---|
65 | if (t->get_type() == type_word){ |
---|
66 | size_t word_len = ((Word*)t)->get_len(); |
---|
67 | if (len > UINTPTR_MAX - word_len) |
---|
68 | RF_LIB_ERROR("Argument too large for conversion"); |
---|
69 | else |
---|
70 | len += word_len; |
---|
71 | } |
---|
72 | else |
---|
73 | retfail; |
---|
74 | }; |
---|
75 | |
---|
76 | if (len >= ShortInt::max_len) |
---|
77 | RF_LIB_ERROR("Argument too large for conversion"); |
---|
78 | |
---|
79 | s_Int = ShortInt::create_expr(e_Exp, flag); |
---|
80 | |
---|
81 | RF_END |
---|
82 | } |
---|
83 | } |
---|
84 | |
---|