x_act.html


001| '''BOOL Native Action Implementations.
001| 
001| The Python binding for an Action object is:
001| 
001|     ('ACT', action-name, clause-list, args-list, exit-list, properties)
001| 
001| An Actor object is:
001| 
001|     ('TOR', action-name, clause-parameters-list, temp, properties)
001| 
001| 
001| classes:
001|     ActionImpl      (object)
001| 
001| '''

002| from sys import stdout, stderr, argv
003| from logger import logger
004| 
005| Log = logger('ACT')
006| 
007| 
008| class ActionImpl (object):
009|     '''Native Action Base Implementation.'''
010| 
011|     def __init__ (self, _name):
012|         '''Create new Native Action Implementation object.'''
013|         self.name = _name
014|         self.clause_list = [(self.name, 0, None)]
015| 
016|     def __str__ (self):
017|         return 'Action[%s]' % self.name
018| 
019|     def emit_code_object (self):
020|         '''Emit the Python tuple BOOL code object for this Action.'''
021|         return ('ACT', self.name, self.clause_list, [], [], None, ('native',self))
022| 
023|     def invoke (self, env, cf, act_addr, cp_list):
024|         '''Invoke an Action!'''
025|         params_0 = cp_list[0]
026|         if params_0[0] != self.name:
027|             raise Exception('@%s: Illegal parameter clause ("%s").' & (self.name, params_0[0]))
028|         return params_0
029| 
030|     def _eval_object (self, env, cf, obj_addr):
031|         '''Evaluate an object and return result.'''
032|         # Evaluate the object...
033|         env.send_message(cf, obj_addr, 'q')
034|         # Get the result...
035|         retv_addr = env.stack_pop()
036|         Log.debug('@%s(obj.addr): %s' % (self.name, str(retv_addr)))
037|         # Get the code object...
038|         retv_code = env.get_object(cf, retv_addr)
039|         Log.debug('@%s(obj.code): %s' % (self.name, str(retv_code)))
040|         # We expect an Instance object from a Q: message...
041|         if retv_code[0] not in ['INST','TEMP']:
042|             raise Exception('@%s: Unexpected object type ("%s").' & (self.name, str(retv_code)))
043|         # Get the data object...
044|         retv_dx = retv_code[2]
045|         retv_data = cf.get_data_object(retv_addr, retv_dx)
046|         Log.debug('@%s(obj.data): %s' % (self.name, str(retv_data)))
047|         # Get the data value
048|         retv_mdl  = retv_data[1]
049|         retv_dkey = retv_data[2]
050|         data = env.meta['MDL'].native[retv_mdl].get_value(retv_dkey)
051|         ###TODO### ^^^ Needs to retrieve from user-def Models, too!
052|         return (retv_mdl, data, retv_dx, retv_dkey)
053| 
054|     def _eval_boolean_expression (self, env, cf, expr):
055|         '''Evaluate an expression for truthiness and return (boolean) result.'''
056|         # Evaluate the expression...
057|         env.send_message(cf, expr, 't')
058|         # Get the result...
059|         retv_addr = env.stack_pop()
060|         Log.debug('@%s(expr.addr): %s' % (self.name, str(retv_addr)))
061|         # Get the code object (a *bool Instance is expected)...
062|         retv_code = env.get_object(cf, retv_addr)
063|         Log.debug('@%s(expr.code): %s' % (self.name, str(retv_code)))
064|         # Sanity Check...
065|         if retv_code[0] not in ['INST','TEMP']:
066|             raise Exception('@%s: Unexpected expression result ("%s").' & (self.name, str(retv_code)))
067|         # Get the data object...
068|         retv_dx = retv_code[2]
069|         retv_data = cf.get_data_object(retv_addr, retv_dx)
070|         Log.debug('@%s(expr.data): %s' % (self.name, str(retv_data)))
071|         # Sanity Check...
072|         if retv_data[1] != 'bool':
073|             raise Exception('@%s: Unexpected expression type ("%s").' & (self.name, str(retv_data)))
074|         # Get the value object...
075|         retv_dkey = retv_data[2]
076|         data = env.meta['MDL'].native['bool'].get_value(retv_dkey)
077|         Log.debug('@%s(expr.value): %s' % (self.name, str(data)))
078|         return data
079| 
080| 
081| 
082| 
083| 
084| ### --- auto --- ###
085| def main (args):
086|     impl = ActionImpl('Foobar')
087|     print(impl, impl.emit_code_object())
088|     return impl
089| 
090| if __name__ == '__main__':
091|     print('autorun',argv[0])
092|     try:
093|         retv = main(argv[1:])
094|     except Exception as e:
095|         print(e)
096| 
097| '''eof'''
098|