as2js: /home/snapwebsites/snapcpp/contrib/as2js/lib/node_compare.cpp Source File

as2js  0.1.14
AlexScript to JavaScript
node_compare.cpp
Go to the documentation of this file.
1 /* lib/node_compare.cpp
2 
3 Copyright (c) 2005-2019 Made to Order Software Corp. All Rights Reserved
4 
6 
7 Permission is hereby granted, free of charge, to any
8 person obtaining a copy of this software and
9 associated documentation files (the "Software"), to
10 deal in the Software without restriction, including
11 without limitation the rights to use, copy, modify,
12 merge, publish, distribute, sublicense, and/or sell
13 copies of the Software, and to permit persons to whom
14 the Software is furnished to do so, subject to the
15 following conditions:
16 
17 The above copyright notice and this permission notice
18 shall be included in all copies or substantial
19 portions of the Software.
20 
21 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF
22 ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT
23 LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
24 FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO
25 EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
26 LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
27 WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
28 ARISING FROM, OUT OF OR IN CONNECTION WITH THE
29 SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
30 SOFTWARE.
31 
32 */
33 
34 #include "as2js/node.h"
35 
36 #include "as2js/exceptions.h"
37 
38 
53 namespace as2js
54 {
55 
56 
57 
58 /**********************************************************************/
59 /**********************************************************************/
60 /*** NODE COMPARE ***************************************************/
61 /**********************************************************************/
62 /**********************************************************************/
63 
64 
95 {
96  if(!lhs->is_literal() || !rhs->is_literal())
97  {
98  // invalid left or right hand side
100  }
101 
102  // since we do not have a NODE_BOOLEAN, but instead have NODE_TRUE
103  // and NODE_FALSE, we have to handle that case separately
104  if(lhs->f_type == node_t::NODE_FALSE)
105  {
106  if(rhs->f_type == node_t::NODE_FALSE)
107  {
109  }
110  if(rhs->f_type == node_t::NODE_TRUE)
111  {
113  }
114  }
115  else if(lhs->f_type == node_t::NODE_TRUE)
116  {
117  if(rhs->f_type == node_t::NODE_FALSE)
118  {
120  }
121  if(rhs->f_type == node_t::NODE_TRUE)
122  {
124  }
125  }
126 
127  // exact same type?
128  if(lhs->f_type == rhs->f_type)
129  {
130  switch(lhs->f_type)
131  {
133  // NaN is a special case we have to take in account
134  if(mode == compare_mode_t::COMPARE_SMART && lhs->get_float64().nearly_equal(rhs->get_float64()))
135  {
137  }
138  return lhs->get_float64().compare(rhs->get_float64());
139 
140  case node_t::NODE_INT64:
141  return lhs->get_int64().compare(rhs->get_int64());
142 
143  case node_t::NODE_NULL:
145 
146  case node_t::NODE_STRING:
147  if(lhs->f_str == rhs->f_str)
148  {
150  }
151  return lhs->f_str < rhs->f_str ? compare_t::COMPARE_LESS : compare_t::COMPARE_GREATER;
152 
155 
156  default:
157  throw exception_internal_error("INTERNAL ERROR: comparing two literal nodes with an unknown type."); // LCOV_EXCL_LINE
158 
159  }
160  /*NOTREACHED*/
161  }
162 
163  // if strict mode is turned on, we cannot compare objects
164  // that are not of the same type (i.e. no conversions allowed)
166  {
168  }
169 
170  // we handle one special case here since 'undefined' is otherwise
171  // converted to NaN and it would not be equal to 'null' which is
172  // represented as being equal to zero
173  if((lhs->f_type == node_t::NODE_NULL && rhs->f_type == node_t::NODE_UNDEFINED)
174  || (lhs->f_type == node_t::NODE_UNDEFINED && rhs->f_type == node_t::NODE_NULL))
175  {
177  }
178 
179  // if we are here, we have go to convert both parameters to floating
180  // point numbers and compare the result (remember that we do not handle
181  // objects in this functon)
182  Float64 lf;
183  switch(lhs->f_type)
184  {
185  case node_t::NODE_INT64:
186  lf.set(lhs->f_int.get());
187  break;
188 
190  lf = lhs->f_float;
191  break;
192 
193  case node_t::NODE_TRUE:
194  lf.set(1.0);
195  break;
196 
197  case node_t::NODE_NULL:
198  case node_t::NODE_FALSE:
199  lf.set(0.0);
200  break;
201 
202  case node_t::NODE_STRING:
203  lf.set(lhs->f_str.to_float64());
204  break;
205 
207  lf.set_NaN();
208  break;
209 
210  default:
211  // failure (cannot convert)
212  throw exception_internal_error("INTERNAL ERROR: could not convert a literal node to a floating point (lhs)."); // LCOV_EXCL_LINE
213 
214  }
215 
216  Float64 rf;
217  switch(rhs->f_type)
218  {
219  case node_t::NODE_INT64:
220  rf.set(rhs->f_int.get());
221  break;
222 
224  rf = rhs->f_float;
225  break;
226 
227  case node_t::NODE_TRUE:
228  rf.set(1.0);
229  break;
230 
231  case node_t::NODE_NULL:
232  case node_t::NODE_FALSE:
233  rf.set(0.0);
234  break;
235 
236  case node_t::NODE_STRING:
237  rf.set(rhs->f_str.to_float64());
238  break;
239 
241  rf.set_NaN();
242  break;
243 
244  default:
245  // failure (cannot convert)
246  throw exception_internal_error("INTERNAL ERROR: could not convert a literal node to a floating point (rhs)."); // LCOV_EXCL_LINE
247 
248  }
249 
250  if(mode == compare_mode_t::COMPARE_SMART && lf.nearly_equal(rf))
251  {
253  }
254 
255  return lf.compare(rf);
256 }
257 
258 
259 }
260 // namespace as2js
261 
262 // vim: ts=4 sw=4 et
float64_type f_float
Definition: float64.h:167
void set(float64_type const new_float)
Definition: float64.h:76
void set_NaN()
Definition: float64.h:81
bool nearly_equal(Float64 const &rhs, float64_type epsilon=default_epsilon())
Definition: float64.h:143
compare_t compare(Float64 const &rhs) const
Definition: float64.h:120
static compare_t compare(Node::pointer_t const lhs, Node::pointer_t const rhs, compare_mode_t const mode)
Compare two nodes together.
std::shared_ptr< Node > pointer_t
Definition: node.h:67
The AlexScript to JavaScript namespace.
Definition: compiler.cpp:37
compare_t
Definition: compare.h:41
compare_mode_t
Definition: node.h:433

This document is part of the Snap! Websites Project.

Copyright by Made to Order Software Corp.

Syndicate content

Snap! Websites
An Open Source CMS System in C++

Contact Us Directly