paul@627 | 1 | A Systems Programming Language Target for Micropython
|
paul@627 | 2 | =====================================================
|
paul@627 | 3 |
|
paul@627 | 4 | Python-compatible syntax for processing using the compiler module.
|
paul@627 | 5 |
|
paul@627 | 6 | The principal focus is on specific machine code generation and not
|
paul@627 | 7 | analysis. Thus, only block generation, address reference generation,
|
paul@627 | 8 | temporary storage administration and other code generation tasks are to be
|
paul@627 | 9 | left to the systems programming language compiler.
|
paul@627 | 10 |
|
paul@670 | 11 | Special Functions
|
paul@670 | 12 | -----------------
|
paul@670 | 13 |
|
paul@670 | 14 | In syspython, the function invocation notation is reserved to specify
|
paul@670 | 15 | primitive operations such as attribute access and actual function invocations,
|
paul@670 | 16 | with the latter being expressed as follows:
|
paul@670 | 17 |
|
paul@670 | 18 | fn(y) # original Python
|
paul@670 | 19 | apply(fn, y) # syspython
|
paul@670 | 20 |
|
paul@670 | 21 | Thus, in syspython, whenever the invocation notation is used, the target of
|
paul@670 | 22 | the invocation is always a special function and not a general Python function
|
paul@670 | 23 | or method. Note that the apply function resembles the Python function of the
|
paul@670 | 24 | same name but is not actually that particular function.
|
paul@670 | 25 |
|
paul@636 | 26 | Program Data and Data Structure Definition
|
paul@636 | 27 | ------------------------------------------
|
paul@636 | 28 |
|
paul@627 | 29 | Given that micropython has already deduced object and parameter details,
|
paul@627 | 30 | such information must be communicated in the systems programming language
|
paul@627 | 31 | so that the compiler does not have to deduce it again.
|
paul@627 | 32 |
|
paul@627 | 33 | Explicit constant declaration shall be done at the start of the main
|
paul@627 | 34 | module:
|
paul@627 | 35 |
|
paul@670 | 36 | constants(...)
|
paul@627 | 37 |
|
paul@675 | 38 | Each module may feature keyword arguments, and a list of such names is
|
paul@675 | 39 | provided as follows:
|
paul@675 | 40 |
|
paul@675 | 41 | keywords(...)
|
paul@675 | 42 |
|
paul@627 | 43 | Explicit structure declaration is still performed using class statements,
|
paul@627 | 44 | but base classes are omitted and attributes are declared explicitly as
|
paul@627 | 45 | follows:
|
paul@627 | 46 |
|
paul@627 | 47 | class C:
|
paul@670 | 48 | instattrs(member...)
|
paul@670 | 49 | classattrs(member...)
|
paul@627 | 50 |
|
paul@627 | 51 | Other object table information, such as inherited class attributes and
|
paul@627 | 52 | class compatibility (to support isinstance) are also declared explicitly:
|
paul@627 | 53 |
|
paul@670 | 54 | inherited(superclass, member...)
|
paul@670 | 55 | descendants(class...)
|
paul@627 | 56 |
|
paul@627 | 57 | Other than function definitions, no other code statements shall appear in
|
paul@627 | 58 | class definitions; such statements will appear after classes have been
|
paul@638 | 59 | defined.
|
paul@638 | 60 |
|
paul@638 | 61 | For classes in the module namespace or within other classes, the __main__
|
paul@638 | 62 | function collects together all "loose" (module-level) statements; class
|
paul@638 | 63 | attribute assignments will occur in the __main__ function, and where a name
|
paul@638 | 64 | is associated with a function definition and another object, the function will
|
paul@638 | 65 | also be explicitly assigned in the __main__ function using its full name.
|
paul@638 | 66 |
|
paul@638 | 67 | For classes in function namespaces, the containing function could contain the
|
paul@638 | 68 | "loose" statements at the point at which the class appears. However, such
|
paul@638 | 69 | classes are not currently supported in micropython.
|
paul@637 | 70 |
|
paul@637 | 71 | Any class or function defined once in a namespace need not be assigned to that
|
paul@637 | 72 | namespace in the __main__ function, but where multiple definitions exist and
|
paul@637 | 73 | program logic determines which definition prevails, such definitions must be
|
paul@637 | 74 | assigned in the __main__ function.
|
paul@637 | 75 |
|
paul@637 | 76 | For example:
|
paul@637 | 77 |
|
paul@637 | 78 | class C:
|
paul@637 | 79 | def method(self, ...):
|
paul@637 | 80 | ...
|
paul@637 | 81 | if something:
|
paul@637 | 82 | method = something
|
paul@637 | 83 |
|
paul@637 | 84 | This is represented as follows:
|
paul@637 | 85 |
|
paul@637 | 86 | class C:
|
paul@637 | 87 | ...
|
paul@637 | 88 | def method(self, ...):
|
paul@637 | 89 | ...
|
paul@637 | 90 |
|
paul@637 | 91 | def __main__():
|
paul@670 | 92 | globalnames(...)
|
paul@637 | 93 | ...
|
paul@637 | 94 | if something:
|
paul@670 | 95 | storeattr(module.C, method, something)
|
paul@627 | 96 |
|
paul@636 | 97 | Imports
|
paul@636 | 98 | -------
|
paul@636 | 99 |
|
paul@627 | 100 | Imports act as invocations of module code and name assignments within a
|
paul@627 | 101 | particular scope and are defined as follows:
|
paul@627 | 102 |
|
paul@627 | 103 | # import package
|
paul@627 | 104 | package.__main__()
|
paul@670 | 105 | storelocal(package, static(package))
|
paul@627 | 106 |
|
paul@627 | 107 | # import package.module
|
paul@627 | 108 | package.__main__()
|
paul@627 | 109 | package.module.__main__()
|
paul@670 | 110 | storelocal(package, static(package))
|
paul@627 | 111 |
|
paul@627 | 112 | # from package.module import cls
|
paul@627 | 113 | package.__main__()
|
paul@627 | 114 | package.module.__main__()
|
paul@670 | 115 | storelocal(cls, loadattribute(package.module, cls)) # see below
|
paul@627 | 116 |
|
paul@627 | 117 | Since import statements can appear in code that may be executed more than
|
paul@627 | 118 | once, __main__ functions should test and set a flag indicating whether the
|
paul@627 | 119 | function has already been called.
|
paul@627 | 120 |
|
paul@627 | 121 | Python would arguably be more sensible as a language if imports were
|
paul@627 | 122 | processed separately, but this would then rule out logic controlling the
|
paul@627 | 123 | use of modules.
|
paul@627 | 124 |
|
paul@636 | 125 | Name and Attribute Declarations
|
paul@636 | 126 | -------------------------------
|
paul@636 | 127 |
|
paul@629 | 128 | Assignments and name usage involve locals and globals but usage is declared
|
paul@629 | 129 | explicitly:
|
paul@627 | 130 |
|
paul@670 | 131 | localnames(...)
|
paul@627 | 132 |
|
paul@627 | 133 | At the function level, locals are genuine local name definitions whereas
|
paul@627 | 134 | globals refer to module globals:
|
paul@627 | 135 |
|
paul@670 | 136 | globalnames(...)
|
paul@627 | 137 |
|
paul@670 | 138 | At the module level, locals are effectively equivalent to module globals and
|
paul@670 | 139 | are declared as such.
|
paul@629 | 140 |
|
paul@629 | 141 | Each module's __main__ function will declare any referenced module globals as
|
paul@629 | 142 | globals. Note that the __main__ function is not a genuine attribute of any
|
paul@629 | 143 | module but an internal construct used to initialise modules appropriately.
|
paul@627 | 144 |
|
paul@627 | 145 | Such declarations must appear first in a program unit (module, function).
|
paul@627 | 146 | For example:
|
paul@627 | 147 |
|
paul@627 | 148 | def f(a, b):
|
paul@670 | 149 | localnames(a, b, x, y)
|
paul@670 | 150 | globalnames(f, g)
|
paul@627 | 151 |
|
paul@670 | 152 | storelocal(x, 1)
|
paul@670 | 153 | storelocal(y, x)
|
paul@670 | 154 | storelocal(a, b)
|
paul@670 | 155 | storeattr(module, g, f)
|
paul@627 | 156 |
|
paul@636 | 157 | Names and Attributes
|
paul@636 | 158 | --------------------
|
paul@636 | 159 |
|
paul@670 | 160 | Bare names refer to locals or globals according to the localnames and
|
paul@670 | 161 | globalnames declarations, or to constants such as None, True, False and
|
paul@638 | 162 | NotImplemented. Storage of local or global names is done using explicit
|
paul@638 | 163 | functions as follows:
|
paul@638 | 164 |
|
paul@670 | 165 | storelocal(name, value)
|
paul@670 | 166 | storeattr(module, name, value) # see below
|
paul@638 | 167 |
|
paul@627 | 168 | No operator usage: all operators are converted to invocations, including
|
paul@637 | 169 | all attribute access except static references to modules or particular class
|
paul@637 | 170 | or function definitions using the following notation:
|
paul@637 | 171 |
|
paul@670 | 172 | static(package)
|
paul@670 | 173 | static(package.module)
|
paul@670 | 174 | static(package.module.cls)
|
paul@670 | 175 | static(package.module.cls.function)
|
paul@627 | 176 |
|
paul@637 | 177 | A shorthand dot notation could be employed:
|
paul@637 | 178 |
|
paul@637 | 179 | package.module
|
paul@637 | 180 | package.module.cls
|
paul@637 | 181 | package.module.cls.function
|
paul@637 | 182 |
|
paul@637 | 183 | Where multiple definitions of static objects occur, the dot notation cannot be
|
paul@637 | 184 | used, and the full name of such definitions must be quoted. For example:
|
paul@637 | 185 |
|
paul@670 | 186 | static("package.module.cls#1.function")
|
paul@627 | 187 |
|
paul@627 | 188 | In general, attribute access must use an explicit function indicating the
|
paul@627 | 189 | kind of access operation being performed. For example:
|
paul@627 | 190 |
|
paul@676 | 191 | # Instance-related operations:
|
paul@676 | 192 |
|
paul@676 | 193 | loadattr(obj, attrname) # preserve context
|
paul@676 | 194 |
|
paul@676 | 195 | # Static attribute operations:
|
paul@675 | 196 |
|
paul@676 | 197 | loadaddress(obj, attrname) # preserve context
|
paul@676 | 198 | loadaddresscontext(parent, attrname, obj) # replace context with obj
|
paul@676 | 199 | loadaddresscontextcond(parent, attrname, obj) # run-time context decision
|
paul@676 | 200 |
|
paul@676 | 201 | # Unoptimised operations:
|
paul@675 | 202 |
|
paul@676 | 203 | loadattrindex(obj, attrname) # preserve context
|
paul@676 | 204 | loadattrindexcontextcond(obj, attrname) # run-time context decision
|
paul@676 | 205 |
|
paul@676 | 206 | # Instance-related operations:
|
paul@676 | 207 |
|
paul@676 | 208 | storeattr(obj, attrname, value) # preserve context
|
paul@627 | 209 |
|
paul@676 | 210 | # Static attribute operations:
|
paul@676 | 211 |
|
paul@676 | 212 | storeaddress(parent, attrname, value) # preserve context
|
paul@676 | 213 | storeaddresscontext(parent, attrname, value, obj) # replace context with obj
|
paul@676 | 214 |
|
paul@676 | 215 | # Unoptimised operations:
|
paul@676 | 216 |
|
paul@676 | 217 | storeattrindex(obj, attrname, value) # preserve context
|
paul@627 | 218 |
|
paul@675 | 219 | Recall that for loadattrindex family functions, the location of the attribute
|
paul@675 | 220 | is obtained from the object table and the nature of the attribute is
|
paul@675 | 221 | determined from the stored context value.
|
paul@675 | 222 |
|
paul@638 | 223 | Temporary variables could employ similar functions:
|
paul@638 | 224 |
|
paul@670 | 225 | loadtemp(0)
|
paul@670 | 226 | storetemp(0, value)
|
paul@638 | 227 |
|
paul@636 | 228 | Operators and Invocations
|
paul@636 | 229 | -------------------------
|
paul@636 | 230 |
|
paul@627 | 231 | Conventional operators use the operator functions.
|
paul@627 | 232 |
|
paul@627 | 233 | Special operators could also use the operator functions (where available)
|
paul@627 | 234 | but might as well be supported directly:
|
paul@627 | 235 |
|
paul@627 | 236 | __is__(a, b)
|
paul@670 | 237 | __is_not__(a, b)
|
paul@627 | 238 |
|
paul@627 | 239 | Logical operators involving short-circuit evaluation could be represented
|
paul@627 | 240 | as function calls, but the evaluation semantics would be preserved:
|
paul@627 | 241 |
|
paul@627 | 242 | __and__(...) # returns the first non-true value or the final value
|
paul@627 | 243 | __not__(obj) # returns the inverse of the boolean interpretation of obj
|
paul@627 | 244 | __or__(...) # returns the first true value or the final value
|
paul@627 | 245 |
|
paul@627 | 246 | Comparisons could be rephrased in a verbose fashion:
|
paul@627 | 247 |
|
paul@627 | 248 | a < b < c becomes lt(a, b) and lt(b, c)
|
paul@627 | 249 | or __and__(lt(a, b), lt(b, c))
|
paul@627 | 250 |
|
paul@636 | 251 | Advanced Control-Flow
|
paul@636 | 252 | ---------------------
|
paul@636 | 253 |
|
paul@627 | 254 | Any statements requiring control-flow definition in terms of blocks must
|
paul@627 | 255 | be handled in the language as the notions of labels and blocks are not
|
paul@627 | 256 | introduced earlier apart from the special case of jumping to another
|
paul@627 | 257 | callable (described below).
|
paul@627 | 258 |
|
paul@627 | 259 | Special functions for low-level operations:
|
paul@627 | 260 |
|
paul@670 | 261 | check(obj, type)
|
paul@670 | 262 | jump(callable)
|
paul@627 | 263 |
|
paul@627 | 264 | Function/subroutine definition with entry points for checked and unchecked
|
paul@627 | 265 | parameters.
|
paul@627 | 266 |
|
paul@627 | 267 | def fn_checked(self, ...):
|
paul@670 | 268 | check(self, Type) # raises a TypeError if not isinstance(self, Type)
|
paul@670 | 269 | jump(fn_unchecked) # preserves the frame and return address
|
paul@627 | 270 |
|
paul@627 | 271 | def fn_unchecked(self, ...):
|
paul@627 | 272 | ...
|
paul@636 | 273 |
|
paul@670 | 274 | The jump function might also be used for inlining appropriate functions.
|
paul@644 | 275 |
|
paul@636 | 276 | Exceptions must also be handled in the language.
|
paul@644 | 277 |
|
paul@644 | 278 | Object Type Detection
|
paul@644 | 279 | ---------------------
|
paul@644 | 280 |
|
paul@644 | 281 | Occasionally, the type of an object (instance of a particular class, class,
|
paul@644 | 282 | and so on) needs to be determined at run-time:
|
paul@644 | 283 |
|
paul@670 | 284 | isclass(obj)
|