| 1 | /** |
|---|
| 2 | * obj_integer.c -> Part of Crules Programming language |
|---|
| 3 | * |
|---|
| 4 | * Crules is the legal property of its developers. Please refer to the |
|---|
| 5 | * COPYRIGHT file distributed with this source distribution. |
|---|
| 6 | * |
|---|
| 7 | * This program is free software: you can redistribute it and/or modify |
|---|
| 8 | * it under the terms of the GNU General Public License as published by |
|---|
| 9 | * the Free Software Foundation, either version 3 of the License, or |
|---|
| 10 | * (at your option) any later version. |
|---|
| 11 | * |
|---|
| 12 | * This program is distributed in the hope that it will be useful, |
|---|
| 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
|---|
| 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|---|
| 15 | * GNU General Public License for more details. |
|---|
| 16 | * |
|---|
| 17 | * You should have received a copy of the GNU General Public License |
|---|
| 18 | * along with this program. If not, see <http://www.gnu.org/licenses/>. |
|---|
| 19 | **/ |
|---|
| 20 | |
|---|
| 21 | #ifdef HAVE_CONFIG_H |
|---|
| 22 | # include "config.h" |
|---|
| 23 | #else |
|---|
| 24 | # define CMAKE 1 |
|---|
| 25 | # include "config.h.cmake" |
|---|
| 26 | #endif |
|---|
| 27 | |
|---|
| 28 | #include <stdio.h> |
|---|
| 29 | #include <stdlib.h> |
|---|
| 30 | #include <string.h> |
|---|
| 31 | |
|---|
| 32 | #include <gmp.h> |
|---|
| 33 | #include <mpfr.h> |
|---|
| 34 | #include <math.h> |
|---|
| 35 | |
|---|
| 36 | #include <crules/crules.h> |
|---|
| 37 | #include <crules/opcodes.h> |
|---|
| 38 | #include <crules/symbols.h> |
|---|
| 39 | #include <crules/objects.h> |
|---|
| 40 | #include <crules/backend.h> |
|---|
| 41 | #include <crules/runtime.h> |
|---|
| 42 | #include <crules/garbage.h> |
|---|
| 43 | #include <crules/operators.h> |
|---|
| 44 | #include <crules/math.h> |
|---|
| 45 | |
|---|
| 46 | static crl_type_integer_t * |
|---|
| 47 | crl_obj_integer_init( crl_symbol_obj * args, |
|---|
| 48 | crl_type_obj_def_t ** def ) |
|---|
| 49 | { |
|---|
| 50 | crl_type_integer_t *s = crl_malloc( ((*def)->builtin_type_size) ); |
|---|
| 51 | crl_assert( args->op_a_t == TYPE_INTEGER ); |
|---|
| 52 | memcpy( &(s->integer), &(args->op_a.integer), |
|---|
| 53 | sizeof(long int) ); |
|---|
| 54 | return s; |
|---|
| 55 | } |
|---|
| 56 | |
|---|
| 57 | crl_symbol_obj * |
|---|
| 58 | crl_obj_integer_to_string( crl_symbol_obj * caller, crl_symbol_obj * self, |
|---|
| 59 | crl_context_table * caller_ctx, |
|---|
| 60 | crl_context_table * function_ctx ) |
|---|
| 61 | { |
|---|
| 62 | printf("\nHELLLLLLLLO\n\n"); |
|---|
| 63 | |
|---|
| 64 | return NULL; |
|---|
| 65 | } |
|---|
| 66 | |
|---|
| 67 | bool crl_obj_integer_print( crl_type_integer_t * self, FILE * fd, |
|---|
| 68 | bool newline ) |
|---|
| 69 | { |
|---|
| 70 | bool retval = true; |
|---|
| 71 | fprintf( fd, "%li", self->integer ); |
|---|
| 72 | if( newline ) |
|---|
| 73 | { |
|---|
| 74 | fprintf( fd, "\n" ); |
|---|
| 75 | } |
|---|
| 76 | return retval; |
|---|
| 77 | } |
|---|
| 78 | |
|---|
| 79 | void crl_obj_integer_destroy( crl_type_integer_t * self ) |
|---|
| 80 | { |
|---|
| 81 | if( self ) |
|---|
| 82 | crl_free( self ) |
|---|
| 83 | } |
|---|
| 84 | |
|---|
| 85 | crl_symbol_obj * |
|---|
| 86 | crl_obj_integer_add( crl_symbol_obj * opa, crl_symbol_obj * opb, |
|---|
| 87 | crl_context_table * context ) |
|---|
| 88 | { |
|---|
| 89 | crl_symbol_obj * retval = NULL; |
|---|
| 90 | crl_debug("Integer addition!\n"); |
|---|
| 91 | |
|---|
| 92 | crl_obj_state_t *o1, *o2; |
|---|
| 93 | crl_assert( opa->op_a_t == TYPE_OBJECT ); |
|---|
| 94 | crl_assert( opb->op_a_t == TYPE_OBJECT ); |
|---|
| 95 | |
|---|
| 96 | o1 = opa->op_a.object_state; |
|---|
| 97 | o2 = opb->op_a.object_state; |
|---|
| 98 | |
|---|
| 99 | if( !strcmp( o1->identifier, "Int" ) ) |
|---|
| 100 | { |
|---|
| 101 | crl_type_integer_t *t1 = (crl_type_integer_t *) o1->self; |
|---|
| 102 | if( !strcmp(o2->identifier, "Int") ) |
|---|
| 103 | { |
|---|
| 104 | crl_type_integer_t *t2 = (crl_type_integer_t *) o2->self; |
|---|
| 105 | |
|---|
| 106 | mpfr_t x,y,z; |
|---|
| 107 | mpfr_init( z ); |
|---|
| 108 | mpfr_init_set_si( x, t1->integer, GMP_RNDU ); |
|---|
| 109 | mpfr_init_set_si( y, t2->integer, GMP_RNDU ); |
|---|
| 110 | |
|---|
| 111 | if( mpfr_add( z, x, y, GMP_RNDU ) ) |
|---|
| 112 | { |
|---|
| 113 | crl_fatal("overflow in integer addition!\n"); |
|---|
| 114 | } |
|---|
| 115 | |
|---|
| 116 | crl_symbol_obj * init; |
|---|
| 117 | Crl_Symbol_Init( init ); |
|---|
| 118 | init->type = SYMBOL_PRIMARY; |
|---|
| 119 | init->op_a_t = TYPE_INTEGER; |
|---|
| 120 | init->op_a.integer = mpfr_get_si( z, GMP_RNDU ); |
|---|
| 121 | |
|---|
| 122 | mpfr_clears( x, y, z, (mpfr_ptr)0 ); |
|---|
| 123 | |
|---|
| 124 | retval = crl_rr_literal_fold( init, context ); |
|---|
| 125 | crl_garbage_mark_obj( &init ); |
|---|
| 126 | } |
|---|
| 127 | else |
|---|
| 128 | { |
|---|
| 129 | crl_fatal("invalid object type <%s>!\n", o2->identifier ); |
|---|
| 130 | } |
|---|
| 131 | } |
|---|
| 132 | else |
|---|
| 133 | { |
|---|
| 134 | crl_fatal("invalid object type <%s>!\n", o1->identifier ); |
|---|
| 135 | } |
|---|
| 136 | |
|---|
| 137 | return retval; |
|---|
| 138 | } |
|---|
| 139 | |
|---|
| 140 | struct crl_number_prot_t integer_module_binary_ops = { |
|---|
| 141 | true, |
|---|
| 142 | &crl_obj_integer_add, |
|---|
| 143 | NULL, |
|---|
| 144 | NULL, |
|---|
| 145 | NULL, |
|---|
| 146 | NULL, |
|---|
| 147 | NULL, |
|---|
| 148 | NULL, |
|---|
| 149 | NULL, |
|---|
| 150 | NULL, |
|---|
| 151 | NULL, |
|---|
| 152 | NULL, |
|---|
| 153 | NULL, |
|---|
| 154 | NULL, |
|---|
| 155 | }; |
|---|
| 156 | |
|---|
| 157 | const struct crl_builtin_function_def_t integer_module_function_table[] = { |
|---|
| 158 | { "to_string", 0, &crl_obj_integer_to_string }, |
|---|
| 159 | { NULL, 0, NULL }, |
|---|
| 160 | } ; |
|---|
| 161 | |
|---|
| 162 | const struct crl_builtin_member_def_t integer_module_member_table[] = { |
|---|
| 163 | { 0, NULL }, |
|---|
| 164 | } ; |
|---|
| 165 | |
|---|
| 166 | const struct crl_type_obj_def_t integer_object = { |
|---|
| 167 | "Int", |
|---|
| 168 | sizeof( crl_type_integer_t ), |
|---|
| 169 | &crl_obj_integer_init, |
|---|
| 170 | &crl_obj_integer_destroy, |
|---|
| 171 | &crl_obj_integer_print, |
|---|
| 172 | NULL, |
|---|
| 173 | &integer_module_binary_ops, |
|---|
| 174 | integer_module_member_table, |
|---|
| 175 | integer_module_function_table, |
|---|
| 176 | true, |
|---|
| 177 | } ; |
|---|
| 178 | |
|---|
| 179 | bool crl_obj_integer_module_init( crl_context_table * context ) |
|---|
| 180 | { |
|---|
| 181 | bool retval = false; |
|---|
| 182 | retval = crl_rr_context_intilize_module( &integer_object, context ); |
|---|
| 183 | return retval; |
|---|
| 184 | } |
|---|