paul@261 | 1 | #!/usr/bin/env python |
paul@261 | 2 | |
paul@261 | 3 | """ |
paul@261 | 4 | A native function library for a really simple virtual processor. |
paul@261 | 5 | |
paul@354 | 6 | Copyright (C) 2007, 2008, 2009, 2010 Paul Boddie <paul@boddie.org.uk> |
paul@261 | 7 | |
paul@261 | 8 | This program is free software; you can redistribute it and/or modify it under |
paul@261 | 9 | the terms of the GNU General Public License as published by the Free Software |
paul@261 | 10 | Foundation; either version 3 of the License, or (at your option) any later |
paul@261 | 11 | version. |
paul@261 | 12 | |
paul@261 | 13 | This program is distributed in the hope that it will be useful, but WITHOUT |
paul@261 | 14 | ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS |
paul@261 | 15 | FOR A PARTICULAR PURPOSE. See the GNU General Public License for more |
paul@261 | 16 | details. |
paul@261 | 17 | |
paul@261 | 18 | You should have received a copy of the GNU General Public License along with |
paul@261 | 19 | this program. If not, see <http://www.gnu.org/licenses/>. |
paul@261 | 20 | """ |
paul@261 | 21 | |
paul@264 | 22 | from micropython.program import DataValue |
paul@261 | 23 | import operator |
paul@261 | 24 | |
paul@261 | 25 | class Library: |
paul@261 | 26 | |
paul@261 | 27 | "Native function implementations." |
paul@261 | 28 | |
paul@354 | 29 | def __init__(self, machine, constants): |
paul@261 | 30 | |
paul@261 | 31 | """ |
paul@354 | 32 | Initialise the library with the 'machine' and the 'constants' addresses |
paul@354 | 33 | dictionary. |
paul@261 | 34 | """ |
paul@261 | 35 | |
paul@261 | 36 | self.machine = machine |
paul@354 | 37 | self.constants = constants |
paul@261 | 38 | |
paul@261 | 39 | # Native class constants. |
paul@261 | 40 | |
paul@261 | 41 | cls = self.machine._get_class("__builtins__", "int") |
paul@261 | 42 | self.int_class = cls.location |
paul@261 | 43 | self.int_instance = cls.instance_template_location |
paul@261 | 44 | cls = self.machine._get_class("__builtins__", "list") |
paul@346 | 45 | if cls is not None: |
paul@346 | 46 | self.list_class = cls.location |
paul@346 | 47 | self.list_instance = cls.instance_template_location |
paul@261 | 48 | cls = self.machine._get_class("__builtins__", "IndexError") |
paul@346 | 49 | if cls is not None: |
paul@346 | 50 | self.index_error = cls.location |
paul@346 | 51 | self.index_error_instance = cls.instance_template_location |
paul@262 | 52 | cls = self.machine._get_class("__builtins__", "basestring") |
paul@346 | 53 | if cls is not None: |
paul@346 | 54 | self.str_class = cls.location |
paul@346 | 55 | self.str_instance = cls.instance_template_location |
paul@261 | 56 | |
paul@261 | 57 | self.tuple_class = self.machine.tuple_class |
paul@261 | 58 | self.type_error_instance = self.machine.type_error_instance |
paul@261 | 59 | |
paul@261 | 60 | self.frame_stack = self.machine.frame_stack |
paul@261 | 61 | self.local_sp_stack = self.machine.local_sp_stack |
paul@261 | 62 | |
paul@261 | 63 | def builtins_int_arithmetic_op(self, op): |
paul@261 | 64 | frame = self.local_sp_stack[-1] |
paul@261 | 65 | |
paul@261 | 66 | # Get operands addresses. |
paul@261 | 67 | |
paul@264 | 68 | left_value = self.frame_stack[frame] |
paul@264 | 69 | right_value = self.frame_stack[frame + 1] |
paul@261 | 70 | |
paul@261 | 71 | # Test operand suitability. |
paul@261 | 72 | # NOTE: Support other types. |
paul@261 | 73 | |
paul@264 | 74 | if not (self.machine._CheckInstance(left_value.ref, self.int_class) and |
paul@264 | 75 | self.machine._CheckInstance(right_value.ref, self.int_class)): |
paul@264 | 76 | |
paul@261 | 77 | self.machine.exception = self.machine._MakeObject(2, self.type_error_instance) |
paul@261 | 78 | return self.machine.RaiseException() |
paul@261 | 79 | |
paul@261 | 80 | # NOTE: Assume single location for data. |
paul@261 | 81 | |
paul@264 | 82 | left_data = left_value.ref + 1 |
paul@264 | 83 | right_data = right_value.ref + 1 |
paul@261 | 84 | |
paul@261 | 85 | # Make a new object. |
paul@261 | 86 | |
paul@261 | 87 | addr = self.machine._MakeObject(2, self.int_instance) |
paul@261 | 88 | |
paul@261 | 89 | # Store the result. |
paul@261 | 90 | # NOTE: The data is considered ready to use. |
paul@261 | 91 | |
paul@261 | 92 | self.machine.save(addr + 1, op(self.machine.load(left_data), self.machine.load(right_data))) |
paul@261 | 93 | |
paul@261 | 94 | # Return the new object. |
paul@261 | 95 | # Introduce object as context for the new object. |
paul@261 | 96 | |
paul@264 | 97 | self.machine.result = DataValue(addr, addr) |
paul@261 | 98 | |
paul@354 | 99 | def builtins_logical_op(self, operand_class, op): |
paul@262 | 100 | frame = self.local_sp_stack[-1] |
paul@262 | 101 | |
paul@262 | 102 | # Get operands addresses. |
paul@262 | 103 | |
paul@264 | 104 | left_value = self.frame_stack[frame] |
paul@264 | 105 | right_value = self.frame_stack[frame + 1] |
paul@262 | 106 | |
paul@262 | 107 | # Test operand suitability. |
paul@262 | 108 | # NOTE: Handle comparisons of incompatible types more appropriately. |
paul@354 | 109 | # NOTE: Return NotImplemented. |
paul@262 | 110 | |
paul@264 | 111 | if not (self.machine._CheckInstance(left_value.ref, operand_class) and |
paul@264 | 112 | self.machine._CheckInstance(right_value.ref, operand_class)): |
paul@264 | 113 | |
paul@354 | 114 | notimpl = self.constants[NotImplemented] |
paul@354 | 115 | self.machine.result = DataValue(notimpl, notimpl) |
paul@262 | 116 | return |
paul@262 | 117 | |
paul@262 | 118 | # NOTE: Assume single location for data. |
paul@262 | 119 | |
paul@264 | 120 | left_data = left_value.ref + 1 |
paul@264 | 121 | right_data = right_value.ref + 1 |
paul@262 | 122 | |
paul@262 | 123 | # Test the data. |
paul@262 | 124 | # NOTE: The data is considered ready to use. |
paul@262 | 125 | |
paul@262 | 126 | if op(self.machine.load(left_data), self.machine.load(right_data)): |
paul@354 | 127 | self.machine.result = DataValue(self.constants[True], self.constants[True]) |
paul@262 | 128 | else: |
paul@354 | 129 | self.machine.result = DataValue(self.constants[False], self.constants[False]) |
paul@262 | 130 | |
paul@262 | 131 | # Operators. |
paul@262 | 132 | # Although this takes a short-cut by using the operator module, testing is |
paul@262 | 133 | # still performed on the operands to ensure that they qualify for these |
paul@262 | 134 | # native operations. |
paul@262 | 135 | |
paul@262 | 136 | def builtins_int_add(self): |
paul@262 | 137 | return self.builtins_int_arithmetic_op(operator.add) |
paul@262 | 138 | |
paul@262 | 139 | def builtins_int_sub(self): |
paul@262 | 140 | return self.builtins_int_arithmetic_op(operator.sub) |
paul@262 | 141 | |
paul@262 | 142 | def builtins_int_pow(self): |
paul@262 | 143 | return self.builtins_int_arithmetic_op(operator.pow) |
paul@262 | 144 | |
paul@262 | 145 | def builtins_int_lt(self): |
paul@354 | 146 | return self.builtins_logical_op(self.int_class, operator.lt) |
paul@262 | 147 | |
paul@262 | 148 | def builtins_int_le(self): |
paul@354 | 149 | return self.builtins_logical_op(self.int_class, operator.le) |
paul@262 | 150 | |
paul@262 | 151 | def builtins_int_gt(self): |
paul@354 | 152 | return self.builtins_logical_op(self.int_class, operator.gt) |
paul@262 | 153 | |
paul@262 | 154 | def builtins_int_ge(self): |
paul@354 | 155 | return self.builtins_logical_op(self.int_class, operator.ge) |
paul@262 | 156 | |
paul@262 | 157 | def builtins_int_eq(self): |
paul@354 | 158 | return self.builtins_logical_op(self.int_class, operator.eq) |
paul@262 | 159 | |
paul@262 | 160 | def builtins_int_ne(self): |
paul@354 | 161 | return self.builtins_logical_op(self.int_class, operator.ne) |
paul@262 | 162 | |
paul@262 | 163 | def builtins_str_lt(self): |
paul@354 | 164 | return self.builtins_logical_op(self.str_class, operator.lt) |
paul@262 | 165 | |
paul@262 | 166 | def builtins_str_le(self): |
paul@354 | 167 | return self.builtins_logical_op(self.str_class, operator.le) |
paul@262 | 168 | |
paul@262 | 169 | def builtins_str_gt(self): |
paul@354 | 170 | return self.builtins_logical_op(self.str_class, operator.gt) |
paul@262 | 171 | |
paul@262 | 172 | def builtins_str_ge(self): |
paul@354 | 173 | return self.builtins_logical_op(self.str_class, operator.ge) |
paul@262 | 174 | |
paul@262 | 175 | def builtins_str_eq(self): |
paul@354 | 176 | return self.builtins_logical_op(self.str_class, operator.eq) |
paul@262 | 177 | |
paul@262 | 178 | def builtins_str_ne(self): |
paul@354 | 179 | return self.builtins_logical_op(self.str_class, operator.ne) |
paul@262 | 180 | |
paul@262 | 181 | def builtins_int_and(self): |
paul@262 | 182 | return self.builtins_int_arithmetic_op(operator.and_) |
paul@262 | 183 | |
paul@262 | 184 | def builtins_int_or(self): |
paul@262 | 185 | return self.builtins_int_arithmetic_op(operator.or_) |
paul@262 | 186 | |
paul@262 | 187 | # Specific operator methods. |
paul@262 | 188 | |
paul@261 | 189 | def builtins_int_bool(self): |
paul@261 | 190 | frame = self.local_sp_stack[-1] |
paul@261 | 191 | |
paul@261 | 192 | # Get operands addresses. |
paul@261 | 193 | |
paul@264 | 194 | left_value = self.frame_stack[frame] |
paul@261 | 195 | |
paul@261 | 196 | # Test operand suitability. |
paul@261 | 197 | |
paul@264 | 198 | if not self.machine._CheckInstance(left_value.ref, self.int_class): |
paul@261 | 199 | self.machine.exception = self.machine._MakeObject(2, self.type_error_instance) |
paul@261 | 200 | return self.machine.RaiseException() |
paul@261 | 201 | |
paul@261 | 202 | # NOTE: Assume single location for data. |
paul@261 | 203 | |
paul@264 | 204 | left_data = left_value.ref + 1 |
paul@261 | 205 | |
paul@261 | 206 | # Test the data. |
paul@261 | 207 | # NOTE: The data is considered ready to use. |
paul@261 | 208 | |
paul@261 | 209 | if self.machine.load(left_data) != 0: |
paul@354 | 210 | self.machine.result = DataValue(self.constants[True], self.constants[True]) |
paul@261 | 211 | else: |
paul@354 | 212 | self.machine.result = DataValue(self.constants[False], self.constants[False]) |
paul@261 | 213 | |
paul@261 | 214 | def builtins_int_neg(self): |
paul@261 | 215 | frame = self.local_sp_stack[-1] |
paul@261 | 216 | |
paul@261 | 217 | # Get operands addresses. |
paul@261 | 218 | |
paul@264 | 219 | left_value = self.frame_stack[frame] |
paul@261 | 220 | |
paul@261 | 221 | # Test operand suitability. |
paul@261 | 222 | |
paul@264 | 223 | if not self.machine._CheckInstance(left_value.ref, self.int_class): |
paul@261 | 224 | self.machine.exception = self.machine._MakeObject(2, self.type_error_instance) |
paul@261 | 225 | return self.machine.RaiseException() |
paul@261 | 226 | |
paul@261 | 227 | # NOTE: Assume single location for data. |
paul@261 | 228 | |
paul@264 | 229 | left_data = left_value.ref + 1 |
paul@261 | 230 | |
paul@261 | 231 | # Make a new object. |
paul@261 | 232 | |
paul@261 | 233 | addr = self.machine._MakeObject(2, self.int_instance) |
paul@261 | 234 | |
paul@261 | 235 | # Store the result. |
paul@261 | 236 | # NOTE: The data is considered ready to use. |
paul@261 | 237 | |
paul@261 | 238 | self.machine.save(addr + 1, -self.machine.load(left_data)) |
paul@261 | 239 | |
paul@261 | 240 | # Return the new object. |
paul@261 | 241 | # Introduce object as context for the new object. |
paul@261 | 242 | |
paul@264 | 243 | self.machine.result = DataValue(addr, addr) |
paul@261 | 244 | |
paul@262 | 245 | # Various built-in methods. |
paul@261 | 246 | |
paul@261 | 247 | def builtins_bool_bool(self): |
paul@261 | 248 | frame = self.local_sp_stack[-1] |
paul@261 | 249 | |
paul@261 | 250 | # Get operands addresses. |
paul@261 | 251 | |
paul@264 | 252 | left_value = self.frame_stack[frame] |
paul@264 | 253 | self.machine.result = DataValue(left_value.ref, left_value.ref) |
paul@261 | 254 | |
paul@261 | 255 | def builtins_list_new(self): |
paul@261 | 256 | frame = self.local_sp_stack[-1] |
paul@261 | 257 | |
paul@332 | 258 | # The first parameter should be the instance. |
paul@261 | 259 | |
paul@332 | 260 | list_value = self.frame_stack[frame] |
paul@261 | 261 | |
paul@332 | 262 | # Make a new sequence. |
paul@332 | 263 | # NOTE: Using an arbitrary size. |
paul@261 | 264 | |
paul@336 | 265 | new_fragment = self.machine._MakeFragment(1, 5) # include the header |
paul@261 | 266 | |
paul@332 | 267 | # Complete the list instance by saving the fragment reference. |
paul@336 | 268 | # NOTE: This requires an attribute in the list structure. |
paul@261 | 269 | |
paul@332 | 270 | addr = list_value.ref + 1 |
paul@332 | 271 | self.machine.save(addr, DataValue(None, new_fragment)) |
paul@261 | 272 | |
paul@261 | 273 | def builtins_list_getitem(self): |
paul@261 | 274 | frame = self.local_sp_stack[-1] |
paul@261 | 275 | |
paul@261 | 276 | # Get the operand address. |
paul@261 | 277 | |
paul@264 | 278 | item_value = self.frame_stack[frame + 1] |
paul@261 | 279 | |
paul@261 | 280 | # Get the list address. |
paul@261 | 281 | |
paul@264 | 282 | obj_value = self.frame_stack[frame] |
paul@261 | 283 | |
paul@261 | 284 | # Get the fragment address. |
paul@261 | 285 | # NOTE: Assume single location for header. |
paul@261 | 286 | |
paul@264 | 287 | fragment = self.machine.load(obj_value.ref + 1) |
paul@261 | 288 | |
paul@261 | 289 | # Get the fragment header. |
paul@261 | 290 | |
paul@264 | 291 | header = self.machine.load(fragment.ref) |
paul@261 | 292 | nelements = header.occupied_size - 1 |
paul@261 | 293 | |
paul@372 | 294 | # Test operand suitability. |
paul@372 | 295 | |
paul@372 | 296 | if self.machine._CheckInstance(item_value.ref, self.int_class): |
paul@261 | 297 | |
paul@372 | 298 | # NOTE: Assume single location for data and header. |
paul@372 | 299 | |
paul@372 | 300 | item_pos = self.machine.load(item_value.ref + 1) |
paul@261 | 301 | |
paul@372 | 302 | if item_pos >= 0 and item_pos < nelements: |
paul@372 | 303 | pass |
paul@372 | 304 | elif item_pos < 0 and item_pos >= -nelements: |
paul@372 | 305 | item_pos = nelements + item_pos |
paul@372 | 306 | else: |
paul@372 | 307 | self.machine.exception = self.machine._MakeObject(2, self.index_error_instance) |
paul@372 | 308 | return self.machine.RaiseException() |
paul@372 | 309 | |
paul@261 | 310 | else: |
paul@372 | 311 | self.machine.exception = self.machine._MakeObject(2, self.type_error_instance) |
paul@261 | 312 | return self.machine.RaiseException() |
paul@261 | 313 | |
paul@261 | 314 | # NOTE: Assume single location for header. |
paul@261 | 315 | |
paul@264 | 316 | self.machine.result = self.machine.load(fragment.ref + 1 + item_pos) |
paul@261 | 317 | |
paul@261 | 318 | def builtins_list_len(self): |
paul@261 | 319 | frame = self.local_sp_stack[-1] |
paul@261 | 320 | |
paul@261 | 321 | # Get the list address. |
paul@261 | 322 | |
paul@264 | 323 | obj_value = self.frame_stack[frame] |
paul@261 | 324 | |
paul@261 | 325 | # Get the fragment address. |
paul@261 | 326 | # NOTE: Assume single location for header. |
paul@261 | 327 | |
paul@264 | 328 | fragment = self.machine.load(obj_value.ref + 1) |
paul@261 | 329 | |
paul@261 | 330 | # Get the fragment header. |
paul@261 | 331 | |
paul@264 | 332 | header = self.machine.load(fragment.ref) |
paul@261 | 333 | nelements = header.occupied_size - 1 |
paul@261 | 334 | |
paul@261 | 335 | # Make a new object. |
paul@261 | 336 | |
paul@261 | 337 | addr = self.machine._MakeObject(2, self.int_instance) |
paul@261 | 338 | |
paul@261 | 339 | # Store the result. |
paul@261 | 340 | # NOTE: The data is considered ready to use. |
paul@261 | 341 | |
paul@261 | 342 | self.machine.save(addr + 1, nelements) |
paul@261 | 343 | |
paul@261 | 344 | # Return the new object. |
paul@261 | 345 | # Introduce object as context for the new object. |
paul@261 | 346 | |
paul@264 | 347 | self.machine.result = DataValue(addr, addr) |
paul@261 | 348 | |
paul@261 | 349 | def builtins_list_append(self): |
paul@261 | 350 | frame = self.local_sp_stack[-1] |
paul@261 | 351 | |
paul@261 | 352 | # Get operand address. |
paul@261 | 353 | |
paul@264 | 354 | arg_value = self.frame_stack[frame + 1] |
paul@261 | 355 | |
paul@261 | 356 | # Get the list address. |
paul@261 | 357 | |
paul@264 | 358 | obj_value = self.frame_stack[frame] |
paul@261 | 359 | |
paul@261 | 360 | # Get the fragment address. |
paul@261 | 361 | # NOTE: Assume single location for header. |
paul@261 | 362 | |
paul@264 | 363 | fragment = self.machine.load(obj_value.ref + 1) |
paul@261 | 364 | |
paul@261 | 365 | # Get the fragment header. |
paul@261 | 366 | |
paul@264 | 367 | header = self.machine.load(fragment.ref) |
paul@261 | 368 | |
paul@261 | 369 | # Attempt to add the reference. |
paul@261 | 370 | |
paul@261 | 371 | if header.occupied_size < header.allocated_size: |
paul@264 | 372 | self.machine.save(fragment.ref + header.occupied_size, arg_value) |
paul@261 | 373 | header.occupied_size += 1 |
paul@261 | 374 | else: |
paul@261 | 375 | |
paul@261 | 376 | # Make a new fragment, maintaining more space than currently |
paul@261 | 377 | # occupied in order to avoid reallocation. |
paul@261 | 378 | |
paul@337 | 379 | new_fragment = self.machine._MakeFragment(header.occupied_size + 1, header.occupied_size * 2) |
paul@261 | 380 | |
paul@261 | 381 | # Copy existing elements. |
paul@261 | 382 | |
paul@336 | 383 | for i in range(1, header.occupied_size): |
paul@264 | 384 | self.machine.save(new_fragment + i, self.machine.load(fragment.ref + i)) |
paul@261 | 385 | |
paul@336 | 386 | self.machine.save(new_fragment + header.occupied_size, arg_value) |
paul@261 | 387 | |
paul@261 | 388 | # Set the new fragment in the object. |
paul@261 | 389 | # NOTE: The old fragment could be deallocated. |
paul@261 | 390 | |
paul@264 | 391 | self.machine.save(obj_value.ref + 1, DataValue(None, new_fragment)) |
paul@261 | 392 | |
paul@261 | 393 | def builtins_tuple_len(self): |
paul@261 | 394 | frame = self.local_sp_stack[-1] |
paul@261 | 395 | |
paul@261 | 396 | # Get the tuple address. |
paul@261 | 397 | |
paul@264 | 398 | obj_value = self.frame_stack[frame] |
paul@261 | 399 | |
paul@261 | 400 | # Get the header. |
paul@261 | 401 | # NOTE: Assume single location for header. |
paul@261 | 402 | |
paul@264 | 403 | header = self.machine.load(obj_value.ref) |
paul@261 | 404 | nelements = header.size - 1 |
paul@261 | 405 | |
paul@261 | 406 | # Make a new object. |
paul@261 | 407 | |
paul@261 | 408 | addr = self.machine._MakeObject(2, self.int_instance) |
paul@261 | 409 | |
paul@261 | 410 | # Store the result. |
paul@261 | 411 | # NOTE: The data is considered ready to use. |
paul@261 | 412 | |
paul@261 | 413 | self.machine.save(addr + 1, nelements) |
paul@261 | 414 | |
paul@261 | 415 | # Return the new object. |
paul@261 | 416 | # Introduce object as context for the new object. |
paul@261 | 417 | |
paul@264 | 418 | self.machine.result = DataValue(addr, addr) |
paul@261 | 419 | |
paul@261 | 420 | def builtins_tuple_getitem(self): |
paul@261 | 421 | frame = self.local_sp_stack[-1] |
paul@261 | 422 | |
paul@261 | 423 | # Get the operand address. |
paul@261 | 424 | |
paul@264 | 425 | item_value = self.frame_stack[frame + 1] |
paul@261 | 426 | |
paul@261 | 427 | # Get the tuple address. |
paul@261 | 428 | |
paul@264 | 429 | obj_value = self.frame_stack[frame] |
paul@261 | 430 | |
paul@261 | 431 | # Get the header. |
paul@261 | 432 | # NOTE: Assume single location for header. |
paul@261 | 433 | |
paul@264 | 434 | header = self.machine.load(obj_value.ref) |
paul@261 | 435 | nelements = header.size - 1 |
paul@261 | 436 | |
paul@261 | 437 | # NOTE: Assume single location for data and header. |
paul@261 | 438 | |
paul@264 | 439 | item_pos = self.machine.load(item_value.ref + 1) |
paul@261 | 440 | |
paul@261 | 441 | if item_pos >= 0 and item_pos < nelements: |
paul@261 | 442 | pass |
paul@261 | 443 | elif item_pos < 0 and item_pos >= -nelements: |
paul@261 | 444 | item_pos = nelements + item_pos |
paul@261 | 445 | else: |
paul@261 | 446 | self.machine.exception = self.machine._MakeObject(2, self.index_error_instance) |
paul@261 | 447 | return self.machine.RaiseException() |
paul@261 | 448 | |
paul@261 | 449 | # NOTE: Assume single location for header. |
paul@261 | 450 | |
paul@264 | 451 | self.machine.result = self.machine.load(obj_value.ref + 1 + item_pos) |
paul@261 | 452 | |
paul@261 | 453 | def builtins_object_init(self): |
paul@261 | 454 | pass |
paul@261 | 455 | |
paul@377 | 456 | def builtins_isinstance(self): |
paul@377 | 457 | frame = self.local_sp_stack[-1] |
paul@377 | 458 | |
paul@377 | 459 | # Get the operand addresses. |
paul@377 | 460 | |
paul@377 | 461 | obj_value = self.frame_stack[frame] |
paul@377 | 462 | cls_value = self.frame_stack[frame + 1] |
paul@377 | 463 | |
paul@377 | 464 | if self.machine._CheckInstance(obj_value.ref, cls_value.ref): |
paul@377 | 465 | self.machine.result = DataValue(self.constants[True], self.constants[True]) |
paul@377 | 466 | else: |
paul@377 | 467 | self.machine.result = DataValue(self.constants[False], self.constants[False]) |
paul@377 | 468 | |
paul@261 | 469 | native_functions = { |
paul@261 | 470 | |
paul@261 | 471 | # Native method implementations: |
paul@261 | 472 | |
paul@261 | 473 | "__builtins__.int.__add__" : builtins_int_add, |
paul@261 | 474 | "__builtins__.int.__radd__" : builtins_int_add, # NOTE: To be made distinct. |
paul@261 | 475 | "__builtins__.int.__sub__" : builtins_int_sub, |
paul@262 | 476 | "__builtins__.int.__pow__" : builtins_int_pow, |
paul@261 | 477 | "__builtins__.int.__iadd__" : builtins_int_add, |
paul@261 | 478 | "__builtins__.int.__bool__" : builtins_int_bool, |
paul@261 | 479 | "__builtins__.int.__neg__" : builtins_int_neg, |
paul@261 | 480 | "__builtins__.int.__lt__" : builtins_int_lt, |
paul@261 | 481 | "__builtins__.int.__le__" : builtins_int_le, |
paul@261 | 482 | "__builtins__.int.__gt__" : builtins_int_gt, |
paul@261 | 483 | "__builtins__.int.__ge__" : builtins_int_ge, |
paul@261 | 484 | "__builtins__.int.__eq__" : builtins_int_eq, |
paul@261 | 485 | "__builtins__.int.__ne__" : builtins_int_ne, |
paul@261 | 486 | "__builtins__.int.__and__" : builtins_int_and, |
paul@261 | 487 | "__builtins__.int.__rand__" : builtins_int_and, |
paul@261 | 488 | "__builtins__.int.__or__" : builtins_int_or, |
paul@261 | 489 | "__builtins__.int.__ror__" : builtins_int_or, |
paul@261 | 490 | "__builtins__.bool.__bool__" : builtins_bool_bool, |
paul@261 | 491 | "__builtins__.list.__getitem__" : builtins_list_getitem, |
paul@261 | 492 | "__builtins__.list.__len__" : builtins_list_len, |
paul@261 | 493 | "__builtins__.list.append" : builtins_list_append, |
paul@261 | 494 | "__builtins__.tuple.__len__" : builtins_tuple_len, |
paul@261 | 495 | "__builtins__.tuple.__getitem__" : builtins_tuple_getitem, |
paul@262 | 496 | "__builtins__.basestring.__lt__" : builtins_str_lt, |
paul@262 | 497 | "__builtins__.basestring.__le__" : builtins_str_le, |
paul@262 | 498 | "__builtins__.basestring.__gt__" : builtins_str_gt, |
paul@262 | 499 | "__builtins__.basestring.__ge__" : builtins_str_ge, |
paul@262 | 500 | "__builtins__.basestring.__eq__" : builtins_str_eq, |
paul@262 | 501 | "__builtins__.basestring.__ne__" : builtins_str_ne, |
paul@261 | 502 | |
paul@261 | 503 | # Native initialisers: |
paul@261 | 504 | |
paul@261 | 505 | "__builtins__.object.__init__" : builtins_object_init, # NOTE: A no-operation. |
paul@261 | 506 | "__builtins__.BaseException.__init__" : builtins_object_init, # NOTE: To be made distinct, potentially in the builtins module. |
paul@261 | 507 | |
paul@332 | 508 | # Native instantiator helpers: |
paul@261 | 509 | |
paul@332 | 510 | "__builtins__.list.__new__" : builtins_list_new, |
paul@377 | 511 | |
paul@377 | 512 | # Native helper functions: |
paul@377 | 513 | |
paul@377 | 514 | "__builtins__._isinstance" : builtins_isinstance, |
paul@261 | 515 | } |
paul@261 | 516 | |
paul@261 | 517 | # vim: tabstop=4 expandtab shiftwidth=4 |