old.html


001| '''\
001| BOOL (again) RUN-TIME ENVIRONMENT.
001| 
001| Code here supports BOOL at runtime.
001| 
001| Chris@Sonnack.com
001| November 2013
001| '''

002| ####################################################################################################
003| from sys import stdout, stderr, argv
004| from datetime import datetime, timedelta
005| ####################################################################################################
006| 
007| 
008| ##------------------------------------------------------------------------------------------------##
009| BoolInstanceObjectNull = _BoolAddressSpace.current.add(_BoolInstanceMetaObject.create('null', 'null'))
010| BoolMessageObjectNull  = _BoolAddressSpace.current.add(_BoolMessageMetaObject.create('null', BoolInstanceObjectNull))
011| BoolActorObjectNull    = _BoolAddressSpace.current.add(_BoolActorMetaObject.create('null', BoolEmptyList))
012| 
013| ##------------------------------------------------------------------------------------------------##
014| BoolActionObjectNull = _BoolAddressSpace.current.add(_BoolActionMetaObject.create('null', BoolEmptyList, BoolEmptyList))
015| _BoolActionMetaObject.put(BoolActionObjectNull)
016| 
017| ##------------------------------------------------------------------------------------------------##
018| BoolActionObjectIfElse = _BoolAddressSpace.current.add(_BoolActionMetaObject.create('if**else*', BoolEmptyList, BoolEmptyList, BoolEmptyList))
019| _BoolActionMetaObject.put(BoolActionObjectIfElse)
020| 
021| ##------------------------------------------------------------------------------------------------##
022| BoolModelObjectNull = _BoolAddressSpace.current.add(_BoolModelMetaObject.create('null', BoolInstanceObjectNull, BoolEmptyList))
023| _BoolModelMetaObject.put(BoolModelObjectNull)
024| 
025| ##================================================================================================##
026| def BoolResolveReference (ref):
027|     '''Resolve Reference (tuple). This function takes a (type,value) tuple and resolves the reference value.'''
028|     mtype,k = ref[0:2]
029|     # Constants...
030|     if mtype == 'const':
031|         if k not in _BoolConstantObjects:
032|             raise LookupError('Unknown Reference: "%s"'% k)
033|         return _BoolConstantObjects[k]
034|     # Globals...
035|     if mtype == 'global':
036|         if k not in _BoolGlobalObjects:
037|             raise LookupError('Unknown Reference: "%s"'% k)
038|         return _BoolGlobalObjects[k]
039|     # Locals...
040|     if mtype == 'local':
041|         if k not in _BoolLocalObjects:
042|             # (TODO: should follow a *chain* of Locals from most recent to oldest!)
043|             raise LookupError('Unknown Reference: "%s"'% k)
044|         return _BoolLocalObjects[k]
045|     # oops!
046|     raise ReferenceError('Unknown Reference Type: "%s"'% mtype)
047| 
048| ##================================================================================================##
049| def BoolCreateMessageInstance (msg, props=[], ns='user'):
050|     '''Create a Message String Object instance.'''
051|     otype = 'BOOL.Message'
052|     # Check Constants lib for known Messages...
053|     try:
054|         mx = BoolConstantIndex(otype, msg)
055|     except LookupError:
056|         mx = -1
057|     # If not in Constants; try Globals lib...
058|     if mx < 0:
059|         try:
060|             mx = BoolGlobalObjectInfo(otype, msg)
061|         except LookupError:
062|             mx = -1
063|     # Not in Constants or Globals; add to Globals lib...
064|     # (Message name strings are global and always either exist or are created ad hoc.)
065|     if mx < 0:
066|         m = BoolCreateInstance(otype, m, ns='system', props=[_prop_user_acl('R'),_prop_user_user()])
067|         _BoolGlobalObjects.append(m)
068|         mx = len(_BoolGlobalObjects)
069|         BoolGlobalIndexObjectAdd(m, mx)
070| 
071|     return BoolCreateInstance(otype, msg, value=mx, vtype='sys/constant', props=props, ns=ns)
072| 
073| 
074| ##@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@##
075| ## BOOL RT CONSTANTS LIBRARY
076| ##------------------------------------------------------------------------------------------------##
077| _BoolConstantsIndex = {}
078| _BoolConstants =[   BoolCreateBoolInstance('false', False, ns='system')
079| ,   BoolCreateBoolInstance('true' , True , ns='system')
080| 
081| ,   BoolCreateStringInstance(ns='system')
082| ,   BoolCreateStringInstance('BOOL', ns='system')
083| ,   BoolCreateStringInstance('BOOL2013', ns='system')
084| ,   BoolCreateStringInstance('Basic Object-Oriented Language [BOOL] (c) CJSonnack 1987-2013 All rights reserved.', ns='system')
085| ,   BoolCreateStringInstance('Interuption:', ns='system')
086| 
087| ,   BoolCreateFloatInstance('pi'   , float(3.14159)    , ns='system')
088| ,   BoolCreateFloatInstance('e'    , float(2.71828)    , ns='system')
089| ,   BoolCreateFloatInstance('s/hr' , float(3600)       , ns='system')
090| ,   BoolCreateFloatInstance('s/day', float(24*3600)    , ns='system')
091| ,   BoolCreateFloatInstance('s/yr' , float(365*24*3600), ns='system')
092| ,   BoolCreateFloatInstance('c'    , float(186282)     , ns='system')
093| ,   BoolCreateFloatInstance('AU'   , float(93000000)   , ns='system')
094| ]
095| ##------------------------------------------------------------------------------------------------##
096| ## Add numbers from 0-100...
097| for n in range(101):
098|     t = BoolCreateIntInstance(str(n), int(n))
099|     _BoolConstants.append(t)
100| 
101| ##------------------------------------------------------------------------------------------------##
102| ## Add some other common numbers...
103| for n in [500, 1000, 2000, 2500, 3000, 5000, 10000, 50000, 100000, 1000000]:
104|     t = BoolCreateIntInstance(str(n), int(n))
105|     _BoolConstants.append(t)
106| 
107| ##------------------------------------------------------------------------------------------------##
108| ## Add Messages from 'a' to 'z'...
109| a = ord('a')
110| for n in range(26):
111|     t = BoolCreateInstance('BOOL.Message', chr(a+n), ns='system')
112|     _BoolConstants.append(t)
113| 
114| ##------------------------------------------------------------------------------------------------##
115| ## Add standard Messages...
116| for m in _BoolStandardMessages:
117|     t = BoolCreateInstance('BOOL.Message', m, ns='system')
118|     _BoolConstants.append(t)
119| 
120| ##------------------------------------------------------------------------------------------------##
121| ## Build Index...
122| for ix,bc in enumerate(_BoolConstants):
123|     (mtype, model, name) = bc[:3]
124|     if model not in _BoolConstantsIndex:
125|         _BoolConstantsIndex[model] = {}
126|     typelib = _BoolConstantsIndex[model]
127|     if name in typelib:
128|         raise RuntimeError('Duplicate Constant: "%s [%s]"' % (name, model))
129|     typelib[name] = (bc, int(ix))
130| 
131| ##================================================================================================##
132| def BoolConstant (model, name):
133|     '''Return a(n existing) constant.'''
134|     if model not in _BoolConstantsIndex:
135|         raise NotImplementedError('Constant Model not found: "%s [%s]"' % (name, model))
136|     typelib = _BoolConstantsIndex[model]
137|     if name not in typelib:
138|         raise LookupError('Constant not found: "%s [%s]"' % (name, model))
139|     return typelib[name][0]
140| 
141| ##================================================================================================##
142| def BoolConstantIndex (model, name):
143|     '''Return a(n existing) constant's index.'''
144|     if model not in _BoolConstantsIndex:
145|         raise NotImplementedError('Constant Model not found: "%s [%s]"' % (name, model))
146|     typelib = _BoolConstantsIndex[model]
147|     if name not in typelib:
148|         raise LookupError('Constant not found: "%s [%s]"' % (name, model))
149|     return typelib[name][1]
150| 
151| 
152| ##@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@##
153| ## RUNTIME OBJECTS
154| ##------------------------------------------------------------------------------------------------##
155| _BoolGlobalObjectsIndex = {}
156| _BoolGlobalObjects = []
157| 
158| ##================================================================================================##
159| def BoolGlobalIndexObjectAdd (obj, info):
160|     '''Add an object to the Global Index.
160| (Note that the Global Index is not intimately connected with the Global Objects list,
160| which is a bit different from the Constants architecture. The Index contains a reference
160| to all global objects, whether they're on the list or not. The Global Objects list is
160| for storing global objects not otherwise owned by the user [e.g. message names].)
160| '''

161|     (mtype, model, name) = obj[:3]
162|     if model not in _BoolGlobalObjectsIndex:
163|         _BoolGlobalObjectsIndex[model] = {}
164|     typelib = _BoolGlobalObjectsIndex[model]
165|     if name in typelib:
166|         raise RuntimeError('Duplicate Object: "%s [%s]"' % (name, model))
167|     typelib[name] = (obj, info)
168| 
169| ##================================================================================================##
170| def BoolGlobalIndexObjectDelete (obj):
171|     '''Delete an object from the Global Index.'''
172|     (mtype, model, name) = obj[:3]
173|     if model not in _BoolGlobalObjectsIndex:
174|         raise LookupError('Object Model not found: "%s [%s]"' % (name, model))
175|     typelib = _BoolGlobalObjectsIndex[model]
176|     if name not in typelib:
177|         raise LookupError('Object not found: "%s [%s]"' % (name, model))
178|     del typelib[name]
179| 
180| ##================================================================================================##
181| def BoolGlobalObject (model, name):
182|     '''Return a(n existing) Global Object.'''
183|     if model not in _BoolGlobalObjectsIndex:
184|         raise NotImplementedError('Object Model not found: "%s [%s]"' % (name, model))
185|     typelib = _BoolGlobalObjectsIndex[model]
186|     if name not in typelib:
187|         raise LookupError('Object not found: "%s [%s]"' % (name, model))
188|     return typelib[name][0]
189| 
190| ##================================================================================================##
191| def BoolGlobalObjectInfo (model, name):
192|     '''Return a(n existing) Global Object's user info.'''
193|     if model not in _BoolGlobalObjectsIndex:
194|         raise NotImplementedError('Object Model not found: "%s [%s]"' % (name, model))
195|     typelib = _BoolGlobalObjectsIndex[model]
196|     if name not in typelib:
197|         raise LookupError('Object not found: "%s [%s]"' % (name, model))
198|     return typelib[name][1]
199| 
200| 
201| 
202| 
203| ##================================================================================================##
204| def main (args):
205|     print()
206|     print('Current User: %s' % str(_CurrentUser))
207|     print()
208|     print('Bool Constants: %d' % len(_BoolConstants))
209|     print('Constant(42) = "%s"' % str(BoolConstant('BOOL.int',str(42))))
210|     print()
211|     return args
212| 
213| ####################################################################################################
214| if __name__ == '__main__':
215|     print('autorun:',argv[0])
216|     obj = main(argv[1:])
217| 
218| ####################################################################################################
219| '''eof'''
220|