| 1 | /** |
|---|
| 2 | * rr_math.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 | #define Crl_Lookup_Object_DEF( x,y,z ) \ |
|---|
| 47 | x = crl_rr_context_lookup_object_def( y, z ); \ |
|---|
| 48 | if( !x ) { \ |
|---|
| 49 | crl_error("object <%s> not defined!\n", y ); \ |
|---|
| 50 | } |
|---|
| 51 | |
|---|
| 52 | #define Crl_Eval_Primary( x,y,z ) \ |
|---|
| 53 | crl_assert( x->type == SYMBOL_PRIMARY ); \ |
|---|
| 54 | if( x->op_a_t != TYPE_OBJECT ) { \ |
|---|
| 55 | y = crl_rr_literal_fold( x, z ); \ |
|---|
| 56 | y->n_ref--; \ |
|---|
| 57 | } \ |
|---|
| 58 | else { y = x; } |
|---|
| 59 | |
|---|
| 60 | |
|---|
| 61 | crl_symbol_obj * |
|---|
| 62 | crl_rr_math_dispatch( crl_symbol_obj *opa, crl_symbol_obj * opb, |
|---|
| 63 | crl_context_table * ctx, crl_opcode_t operator ) |
|---|
| 64 | { |
|---|
| 65 | crl_debug("inside math dispatch!\n"); |
|---|
| 66 | crl_symbol_obj *t1, *t2, *retval = NULL; |
|---|
| 67 | crl_debug( "opa->op_a_t = <0x%x> opb->op_a_t = <0x%x>!\n", |
|---|
| 68 | opa->op_a_t, opb->op_a_t ); |
|---|
| 69 | Crl_Eval_Primary( opa, t1, ctx ); |
|---|
| 70 | Crl_Eval_Primary( opb, t2, ctx ); |
|---|
| 71 | |
|---|
| 72 | struct crl_type_obj_def_t ** def = t1->op_a.object_state->definition; |
|---|
| 73 | struct crl_number_prot_t * p = (*def)->binary_protocol; |
|---|
| 74 | char * op_str = NULL; |
|---|
| 75 | |
|---|
| 76 | if( p->init ) |
|---|
| 77 | { |
|---|
| 78 | binary_op op = NULL; |
|---|
| 79 | switch( operator ) |
|---|
| 80 | { |
|---|
| 81 | case OP_BIN_ADDITION: |
|---|
| 82 | op = p->n_add; |
|---|
| 83 | op_str = " + "; |
|---|
| 84 | break; |
|---|
| 85 | |
|---|
| 86 | //.... |
|---|
| 87 | |
|---|
| 88 | default: |
|---|
| 89 | crl_error("un handled operator type <0x%x>!\n", operator ); |
|---|
| 90 | break; |
|---|
| 91 | } |
|---|
| 92 | |
|---|
| 93 | if( op ) |
|---|
| 94 | { |
|---|
| 95 | crl_debug( "got operator <0x%x> for type <%s>!\n", |
|---|
| 96 | operator, t1->op_a.object_state->identifier ); |
|---|
| 97 | #ifdef DEBUG |
|---|
| 98 | Crl_Math_Operation_Echo( t1, t2, op_str ); |
|---|
| 99 | #endif |
|---|
| 100 | // call the operator! |
|---|
| 101 | retval = op( t1, t2, ctx ); |
|---|
| 102 | #ifdef DEBUG |
|---|
| 103 | if( retval ) |
|---|
| 104 | { |
|---|
| 105 | fprintf( crlerr, "debug: <%s:%s:%i> -> ", __FILE__, __func__, __LINE__ ); |
|---|
| 106 | crl_runtime_object_print( retval, NULL, crlerr, false ); |
|---|
| 107 | fprintf( crlerr, "!\n" ); |
|---|
| 108 | } |
|---|
| 109 | #endif |
|---|
| 110 | } |
|---|
| 111 | } |
|---|
| 112 | else |
|---|
| 113 | { |
|---|
| 114 | crl_error( "object <%s> has no binary operator protocol!\n", |
|---|
| 115 | t1->identifier ); |
|---|
| 116 | } |
|---|
| 117 | |
|---|
| 118 | return retval; |
|---|
| 119 | } |
|---|