File: complex_ext.cpp

package info (click to toggle)
falconpl 0.9.6.9-git20120606-2
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 46,176 kB
  • sloc: cpp: 181,389; ansic: 109,025; yacc: 2,310; xml: 1,218; sh: 403; objc: 245; makefile: 82; sql: 20
file content (228 lines) | stat: -rw-r--r-- 5,151 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
/*
   FALCON - The Falcon Programming Language.
   FILE: complex_ext.cpp

   Complex class for Falcon
   Interface extension functions
   -------------------------------------------------------------------
   Author: Enrico Lumetti
   Begin: Sat, 05 Sep 2009 21:04:31 +0000

   -------------------------------------------------------------------
   (C) Copyright 2009: the FALCON developers (see list in AUTHORS file)

   See LICENSE file for licensing details.
*/

/** \file
   Complex class for Falcon
   Interface extension functions
*/

/*#
   @beginmodule core
*/

#include "core_module.h"

namespace Falcon {
namespace core {

CoreObject *Complex_Factory( const CoreClass *cls, void *, bool )
{
    return new CoreComplex ( cls );
}

CoreComplex::~CoreComplex ( void )
{ }


bool CoreComplex::hasProperty( const String &key ) const
{
   uint32 pos = 0;
   return generator()->properties().findKey( key, pos );
}


bool CoreComplex::setProperty( const String &key, const Item &item )
{
   if (key == "real")
   {
      m_complex.real( item.forceNumeric() );
      return true;
   }
   if (key == "imag")
   {
      m_complex.imag( item.forceNumeric() );
      return true;
   }

   // found but read only?
   uint32 pos;
   if( generator()->properties().findKey( key, pos ) )
      throw new AccessError( ErrorParam( e_prop_ro, __LINE__ )
            .origin( e_orig_runtime )
            .extra( key ) );

   // no, not found.
   return false;
}

bool CoreComplex::getProperty( const String &key, Item &ret ) const
{
   if (key == "real")
   {
      ret = m_complex.real();
      return true;
   }

   if (key == "imag")
   {
      ret = m_complex.imag();
      return true;
   }

   return defaultProperty( key, ret );
}


FALCON_FUNC Complex_init( ::Falcon::VMachine *vm )
{
   Item *i_real = vm->param(0);
   Item *i_imag = vm->param(1);

   if ( ( i_real != 0 && ! i_real->isOrdinal() )
     || ( i_imag != 0 && ! i_imag->isOrdinal() )
    )
   {
      throw new ParamError( ErrorParam( e_inv_params, __LINE__ )
          .origin( e_orig_runtime )
          .extra( "[N,N]" ) );
   }

   CoreComplex *c = dyncast<CoreComplex *> ( vm->self().asObject() );
   c->complex() = Complex(
      ( i_real != 0 ) ? i_real->forceNumeric() : 0 ,
      ( i_imag != 0 ) ? i_imag->forceNumeric() : 0 
   );
}

FALCON_FUNC Complex_toString( ::Falcon::VMachine *vm )
{
    CoreComplex *self = dyncast<CoreComplex *>( vm->self().asObject() );
    String res,real,imag;
    Item(self->complex().real() ).toString(real);
    Item(self->complex().imag()).toString(imag);
    res=real+" , "+imag+"i";
    vm->retval(res);
}


FALCON_FUNC Complex_abs( ::Falcon::VMachine *vm )
{
    CoreComplex *self = dyncast<CoreComplex *>( vm->self().asObject() );

    vm->retval( self->complex().abs() );
}

static void s_operands( VMachine* vm, Complex* &one, Complex& two, const CoreClass* &gen )
{
   Item *i_obj = vm->param( 0 );
   bool is_ordinal = i_obj->isOrdinal();

   if ( i_obj == 0 || ! ( i_obj->isOfClass( "Complex" ) || is_ordinal ) )
   {
      throw new ParamError( ErrorParam( e_inv_params, __LINE__ )
            .extra( "Complex" ) );
   }

   CoreComplex *self = dyncast<CoreComplex *>( vm->self().asObject() );
   if ( is_ordinal )
      two.real( i_obj->forceNumeric() );
   else
      two = (dyncast<CoreComplex *>( i_obj->asObject() ))->complex();
   one = &self->complex();
   gen = self->generator();
}

FALCON_FUNC Complex_add__( ::Falcon::VMachine *vm )
{
   Complex *one, two;
   const CoreClass* gen;
   s_operands( vm, one, two, gen );
   vm->retval( new CoreComplex( (*one) + two, gen ) );
}

FALCON_FUNC Complex_sub__( ::Falcon::VMachine *vm )
{
   Complex *one, two;
   const CoreClass* gen;
   s_operands( vm, one, two, gen );
   vm->retval( new CoreComplex( (*one) - two, gen ) );
}

FALCON_FUNC Complex_mul__( ::Falcon::VMachine *vm )
{
   Complex *one, two;
   const CoreClass* gen;
   s_operands( vm, one, two, gen );
   vm->retval( new CoreComplex( (*one) * two, gen ) );
}

FALCON_FUNC Complex_div__( ::Falcon::VMachine *vm )
{
   Complex *one, two;
   const CoreClass* gen;
   s_operands( vm, one, two, gen );
   vm->retval( new CoreComplex( (*one) / two, gen ) );
}

FALCON_FUNC Complex_compare( ::Falcon::VMachine *vm )
{
   Item* i_other = vm->param( 0 );
   Item& i_self = vm->self();

   if( i_other == 0 || !( i_other->isOrdinal() || i_other->isOfClass( "Complex" ) ))
   {
       vm->retval( i_self.type() - i_other->type() );
       return;
   }

   Complex& self = dyncast<CoreComplex*>(i_self.asObject())->complex();
   Complex other;

   if( i_other->isOrdinal() )
   {
      other.real( i_other->forceNumeric() );
   }
   else
   {
       other = (dyncast<CoreComplex *>( i_other->asObject() ))->complex();
   }

   if( self < other )
   {
       vm->retval( -1 );
   }
   else if( self > other )
   {
       vm->retval( 1 );
   }
   else
   {
       vm->retval( 0 );
   }

}

FALCON_FUNC Complex_conj( ::Falcon::VMachine *vm )
{
   CoreComplex *self = dyncast<CoreComplex *>( vm->self().asObject() );
   vm->retval( new CoreComplex( self->complex().conj(), self->generator() ) );
}


}
}

/* end of complex_ext.cpp */