x_act_usr.html


001| '''BOOL Native Action Implementations -- Native User Actions.
001| 
001| classes:
001|     ActionImplSqrt  (ActionImpl)
001|     ActionImplSin   (ActionImpl)
001|     ActionImplCos   (ActionImpl)
001| 
001| '''

002| from sys import stdout, stderr, argv
003| from math import sqrt, cos, sin
004| from logger import logger
005| from x_act import ActionImpl
006| 
007| Log = logger('ACT/USR')
008| 
009| 
010| class ActionImplSqrt (ActionImpl):
011|     '''Native Square-Root (@sqrt) Action Implementation.'''
012| 
013|     def __init__ (self):
014|         '''Create new @sqrt Native Action Implementation object.'''
015|         super(ActionImplSqrt,self).__init__('sqrt')
016|         self.clause_list = [(self.name, 1, None)]
017| 
018|     def invoke (self, env, cf, act_addr, cp_list):
019|         '''Invoke @sqrt Action!'''
020|         params_0 = super(ActionImplSqrt,self).invoke(env, cf, act_addr, cp_list)
021|         param_addr = params_0[1]
022|         Log.trace('@%s %s' % (self.name, str(param_addr)))
023|         #
024|         result = self._eval_object(env, cf, param_addr)
025|         try:
026|             nbr = float(result[1])
027|             val = sqrt(nbr_val)
028|         except:
029|             Log.error('@sqrt: input is not numeric ("%s").' % str(result[1]))
030|             val = 0.0
031|         #
032|         Log.trace('SQRT(%f) = %f' % (nbr, val))
033|         dat_obj = env.meta['MDL'].native['float'].new_object(val)
034|         env.stack_push_obj(dat_obj)
035| 
036| 
037| class ActionImplSin (ActionImpl):
038|     '''Native Sine (@sin) Action Implementation.'''
039| 
040|     def __init__ (self):
041|         '''Create new @sin Native Action Implementation object.'''
042|         super(ActionImplSin,self).__init__('sin')
043|         self.clause_list = [(self.name, 1, None)]
044| 
045|     def invoke (self, env, cf, act_addr, cp_list):
046|         '''Invoke @sin Action!'''
047|         params_0 = super(ActionImplSin,self).invoke(env, cf, act_addr, cp_list)
048|         param_addr = params_0[1]
049|         Log.trace('@%s %s' % (self.name, str(param_addr)))
050|         #
051|         result = self._eval_object(env, cf, param_addr)
052|         try:
053|             nbr = float(result[1])
054|             val = sin(nbr_val)
055|         except:
056|             Log.error('@sin: input is not numeric ("%s").' % str(result[1]))
057|             val = 0.0
058|         #
059|         Log.trace('SIN(%f) = %f' % (nbr, val))
060|         dat_obj = env.meta['MDL'].native['float'].new_object(val)
061|         env.stack_push_obj(dat_obj)
062| 
063| 
064| class ActionImplCos (ActionImpl):
065|     '''Native Co-Sine (@cos) Action Implementation.'''
066| 
067|     def __init__ (self):
068|         '''Create new @cos Native Action Implementation object.'''
069|         super(ActionImplCos,self).__init__('cos')
070|         self.clause_list = [(self.name, 1, None)]
071| 
072|     def invoke (self, env, cf, act_addr, cp_list):
073|         '''Invoke @cos Action!'''
074|         params_0 = super(ActionImplCos,self).invoke(env, cf, act_addr, cp_list)
075|         param_addr = params_0[1]
076|         Log.trace('@%s %s' % (self.name, str(param_addr)))
077|         #
078|         result = self._eval_object(env, cf, param_addr)
079|         try:
080|             nbr = float(result[1])
081|             val = cos(nbr_val)
082|         except:
083|             Log.error('@cos: input is not numeric ("%s").' % str(result[1]))
084|             val = 0.0
085|         #
086|         Log.trace('COS(%f) = %f' % (nbr, val))
087|         dat_obj = env.meta['MDL'].native['float'].new_object(val)
088|         env.stack_push_obj(dat_obj)
089| 
090| 
091| 
092| 
093| 
094| ### --- auto --- ###
095| def main (args):
096|     retv = []
097|     retv.append( ActionImplSqrt() )
098|     retv.append( ActionImplSin()  )
099|     retv.append( ActionImplCos()  )
100|     for r in retv:
101|         print(r, r.emit_code_object())
102|     return retv
103| 
104| if __name__ == '__main__':
105|     print('autorun',argv[0])
106|     try:
107|         retv = main(argv[1:])
108|     except Exception as e:
109|         print(e)
110| 
111| '''eof'''
112|