x_act_lib.html
001| '''BOOL Native Action Implementations -- Native Library Actions.
001|
001| classes:
001| ActionImplIf (ActionImpl)
001| ActionImplWhile (ActionImpl)
001| ActionImplUntil (ActionImpl)
001| ActionImplIncr (ActionImpl)
001| ActionImplDecr (ActionImpl)
001| ActionImplAbs (ActionImpl)
001|
001| '''
002| from sys import stdout, stderr, argv
003| from logger import logger
004| from x_act import ActionImpl
005|
006| Log = logger('ACT/LIB')
007|
008| WatchDogLoopMax = 10
009|
010|
011| class ActionImplIf (ActionImpl):
012| '''Native If-Else (@if..@elif..@else) Action Implementation.'''
013|
014| def __init__ (self):
015| '''Create new @if Native Action Implementation object.'''
016| super(ActionImplIf,self).__init__('if')
017| self.clause_list = [('if',2,None), ('elif',2,None), ('else',1,None)]
018|
019| def invoke (self, env, cf, act_addr, cp_list):
020| '''Invoke @if Action!'''
021| super(ActionImplIf,self).invoke(env, cf, act_addr, cp_list)
022| px = 0
023| p_list = cp_list[px]
024| if_expr = p_list[1]
025| if_list = p_list[2]
026| Log.trace('@%s %s, %s' % (str(p_list[0]), str(if_expr), str(if_list)))
027|
028| dat_obj = env.meta['MDL'].native['bool'].new_object()
029| env.stack_push_obj(dat_obj)
030|
031|
032| class ActionImplWhile (ActionImpl):
033| '''Native While-Loop (@while) Action Implementation.'''
034|
035| def __init__ (self):
036| '''Create new @while Native Action Implementation object.'''
037| super(ActionImplWhile,self).__init__('while')
038| self.clause_list = [(self.name, 2, None)]
039|
040| def invoke (self, env, cf, act_addr, cp_list):
041| '''Invoke @while Action!'''
042| params_0 = super(ActionImplWhile,self).invoke(env, cf, act_addr, cp_list)
043| loop_expr = params_0[1]
044| loop_list = params_0[2]
045| Log.trace('@%s %s, %s' % (self.name, str(loop_expr), str(loop_list)))
046|
047| flag = self._eval_boolean_expression(env, cf, loop_expr)
048| watchdog = WatchDogLoopMax
049| while flag:
050| watchdog -= 1
051| if watchdog < 0:
052| break
053|
054| env.send_message(cf, loop_list, 'x')
055| flag = self._eval_boolean_expression(env, cf, loop_expr)
056|
057| dat_obj = env.meta['MDL'].native['bool'].new_object(flag)
058| env.stack_push_obj(dat_obj)
059|
060|
061| class ActionImplUntil (ActionImpl):
062| '''Native Until-Loop (@until) Action Implementation.'''
063|
064| def __init__ (self):
065| '''Create new @until Native Action Implementation object.'''
066| super(ActionImplUntil,self).__init__('until')
067| self.clause_list = [(self.name, 2, None)]
068|
069| def invoke (self, env, cf, act_addr, cp_list):
070| '''Invoke @until Action!'''
071| params_0 = super(ActionImplUntil,self).invoke(env, cf, act_addr, cp_list)
072| loop_expr = params_0[1]
073| loop_list = params_0[2]
074| Log.trace('@%s %s, %s' % (self.name, str(loop_expr), str(loop_list)))
075|
076| flag = self._eval_boolean_expression(env, cf, loop_expr)
077| watchdog = WatchDogLoopMax
078| while not flag:
079| watchdog -= 1
080| if watchdog < 0:
081| break
082|
083| env.send_message(cf, loop_list, 'x')
084| flag = self._eval_boolean_expression(env, cf, loop_expr)
085|
086| dat_obj = env.meta['MDL'].native['bool'].new_object(flag)
087| env.stack_push_obj(dat_obj)
088|
089| '''Generic Action -- Semi-Model-Aware!'''
090|
091| class ActionImplIncr (ActionImpl):
092| '''Native Increment (@incr) Action Implementation.'''
093|
094| def __init__ (self):
095| '''Create new @incr Native Action Implementation object.'''
096| super(ActionImplIncr,self).__init__('incr')
097| self.clause_list = [(self.name, 1, None)]
098|
099| def invoke (self, env, cf, act_addr, cp_list):
100| '''Invoke @incr Action!'''
101| params_0 = super(ActionImplIncr,self).invoke(env, cf, act_addr, cp_list)
102| param_addr = params_0[1]
103| param_code = env.get_object(cf, param_addr)
104| Log.trace('@%s %s' % (self.name, str(param_code)))
105|
106|
107| mtype = param_code[0]
108| if mtype != 'INST':
109| raise Exception('@%s: Invalid object for Increment (%s).' % (self.name, mtype))
110|
111| dtype = param_code[1]
112| if dtype not in ['int', 'float']:
113| raise Exception('@%s: Invalid type for Increment (%s).' % (self.name, dtype))
114|
115| Impl = env.meta['MDL'].native[dtype]
116|
117|
118| dx = param_code[2]
119| param_data = cf.get_data_object(param_addr, dx)
120| dkey = param_data[2]
121|
122|
123| val_0 = Impl.get_value(dkey)
124| val_1 = val_0 + 1
125| Impl.set_value(dkey, val_1)
126|
127|
128| Log.trace('INCR(%s) = %s' % (val_0, val_1))
129| env.stack_push_addr(param_addr)
130|
131|
132| class ActionImplDecr (ActionImpl):
133| '''Native Decrement (@decr) Action Implementation.'''
134|
135| def __init__ (self):
136| '''Create new @decr Native Action Implementation object.'''
137| super(ActionImplDecr,self).__init__('decr')
138| self.clause_list = [(self.name, 1, None)]
139|
140| def invoke (self, env, cf, act_addr, cp_list):
141| '''Invoke @decr Action!'''
142| params_0 = super(ActionImplDecr,self).invoke(env, cf, act_addr, cp_list)
143| param_addr = params_0[1]
144| param_code = env.get_object(cf, param_addr)
145| Log.trace('@%s %s' % (self.name, str(param_code)))
146|
147|
148| mtype = param_code[0]
149| if mtype != 'INST':
150| raise Exception('@%s: Invalid object for Decrement (%s).' % (self.name, mtype))
151|
152| dtype = param_code[1]
153| if dtype not in ['int', 'float']:
154| raise Exception('@%s: Invalid type for Decrement (%s).' % (self.name, dtype))
155|
156| Impl = env.meta['MDL'].native[dtype]
157|
158|
159| dx = param_code[2]
160| param_data = cf.get_data_object(param_addr, dx)
161| dkey = param_data[2]
162|
163|
164| val_0 = Impl.get_value(dkey)
165| val_1 = val_0 - 1
166| Impl.set_value(dkey, val_1)
167|
168|
169| Log.trace('DECR(%s) = %s' % (val_0, val_1))
170| env.stack_push_addr(param_addr)
171|
172|
173| class ActionImplAbs (ActionImpl):
174| '''Native Absolute-Value (@abs) Action Implementation.'''
175|
176| def __init__ (self):
177| '''Create new @abs Native Action Implementation object.'''
178| super(ActionImplAbs,self).__init__('abs')
179| self.clause_list = [(self.name, 1, None)]
180|
181| def invoke (self, env, cf, act_addr, cp_list):
182| '''Invoke @abs Action!'''
183| params_0 = super(ActionImplAbs,self).invoke(env, cf, act_addr, cp_list)
184| param_addr = params_0[1]
185| Log.trace('@%s %s' % (self.name, str(param_addr)))
186|
187|
188| dtype, val, dx, dkey = self._eval_object(env, cf, param_addr)
189| Impl = env.meta['MDL'].native[dtype]
190|
191|
192| val_0 = Impl.get_value(dkey)
193| val_1 = (-val_0) if val_0 < 0 else val_0
194| Impl.set_value(dkey, val_1)
195|
196|
197| Log.trace('ABS(%s) = %s' % (val_0, val_1))
198| env.stack_push_addr(param_addr)
199|
200|
201|
202|
203|
204|
205| def main (args):
206| retv = []
207| retv.append( ActionImplIf() )
208| retv.append( ActionImplWhile() )
209| retv.append( ActionImplUntil() )
210| retv.append( ActionImplIncr() )
211| retv.append( ActionImplDecr() )
212| retv.append( ActionImplAbs() )
213| for r in retv:
214| print(r, r.emit_code_object())
215| return retv
216|
217| if __name__ == '__main__':
218| print('autorun',argv[0])
219| try:
220| retv = main(argv[1:])
221| except Exception as e:
222| print(e)
223|
224| '''eof'''
225|