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@627 | 38 | Explicit structure declaration is still performed using class statements,
|
paul@627 | 39 | but base classes are omitted and attributes are declared explicitly as
|
paul@627 | 40 | follows:
|
paul@627 | 41 |
|
paul@627 | 42 | class C:
|
paul@670 | 43 | instattrs(member...)
|
paul@670 | 44 | classattrs(member...)
|
paul@627 | 45 |
|
paul@627 | 46 | Other object table information, such as inherited class attributes and
|
paul@627 | 47 | class compatibility (to support isinstance) are also declared explicitly:
|
paul@627 | 48 |
|
paul@670 | 49 | inherited(superclass, member...)
|
paul@670 | 50 | descendants(class...)
|
paul@627 | 51 |
|
paul@627 | 52 | Other than function definitions, no other code statements shall appear in
|
paul@627 | 53 | class definitions; such statements will appear after classes have been
|
paul@638 | 54 | defined.
|
paul@638 | 55 |
|
paul@638 | 56 | For classes in the module namespace or within other classes, the __main__
|
paul@638 | 57 | function collects together all "loose" (module-level) statements; class
|
paul@638 | 58 | attribute assignments will occur in the __main__ function, and where a name
|
paul@638 | 59 | is associated with a function definition and another object, the function will
|
paul@638 | 60 | also be explicitly assigned in the __main__ function using its full name.
|
paul@638 | 61 |
|
paul@638 | 62 | For classes in function namespaces, the containing function could contain the
|
paul@638 | 63 | "loose" statements at the point at which the class appears. However, such
|
paul@638 | 64 | classes are not currently supported in micropython.
|
paul@637 | 65 |
|
paul@637 | 66 | Any class or function defined once in a namespace need not be assigned to that
|
paul@637 | 67 | namespace in the __main__ function, but where multiple definitions exist and
|
paul@637 | 68 | program logic determines which definition prevails, such definitions must be
|
paul@637 | 69 | assigned in the __main__ function.
|
paul@637 | 70 |
|
paul@637 | 71 | For example:
|
paul@637 | 72 |
|
paul@637 | 73 | class C:
|
paul@637 | 74 | def method(self, ...):
|
paul@637 | 75 | ...
|
paul@637 | 76 | if something:
|
paul@637 | 77 | method = something
|
paul@637 | 78 |
|
paul@637 | 79 | This is represented as follows:
|
paul@637 | 80 |
|
paul@637 | 81 | class C:
|
paul@637 | 82 | ...
|
paul@637 | 83 | def method(self, ...):
|
paul@637 | 84 | ...
|
paul@637 | 85 |
|
paul@637 | 86 | def __main__():
|
paul@670 | 87 | globalnames(...)
|
paul@637 | 88 | ...
|
paul@637 | 89 | if something:
|
paul@670 | 90 | storeattr(module.C, method, something)
|
paul@627 | 91 |
|
paul@636 | 92 | Imports
|
paul@636 | 93 | -------
|
paul@636 | 94 |
|
paul@627 | 95 | Imports act as invocations of module code and name assignments within a
|
paul@627 | 96 | particular scope and are defined as follows:
|
paul@627 | 97 |
|
paul@627 | 98 | # import package
|
paul@627 | 99 | package.__main__()
|
paul@670 | 100 | storelocal(package, static(package))
|
paul@627 | 101 |
|
paul@627 | 102 | # import package.module
|
paul@627 | 103 | package.__main__()
|
paul@627 | 104 | package.module.__main__()
|
paul@670 | 105 | storelocal(package, static(package))
|
paul@627 | 106 |
|
paul@627 | 107 | # from package.module import cls
|
paul@627 | 108 | package.__main__()
|
paul@627 | 109 | package.module.__main__()
|
paul@670 | 110 | storelocal(cls, loadattribute(package.module, cls)) # see below
|
paul@627 | 111 |
|
paul@627 | 112 | Since import statements can appear in code that may be executed more than
|
paul@627 | 113 | once, __main__ functions should test and set a flag indicating whether the
|
paul@627 | 114 | function has already been called.
|
paul@627 | 115 |
|
paul@627 | 116 | Python would arguably be more sensible as a language if imports were
|
paul@627 | 117 | processed separately, but this would then rule out logic controlling the
|
paul@627 | 118 | use of modules.
|
paul@627 | 119 |
|
paul@636 | 120 | Name and Attribute Declarations
|
paul@636 | 121 | -------------------------------
|
paul@636 | 122 |
|
paul@629 | 123 | Assignments and name usage involve locals and globals but usage is declared
|
paul@629 | 124 | explicitly:
|
paul@627 | 125 |
|
paul@670 | 126 | localnames(...)
|
paul@627 | 127 |
|
paul@627 | 128 | At the function level, locals are genuine local name definitions whereas
|
paul@627 | 129 | globals refer to module globals:
|
paul@627 | 130 |
|
paul@670 | 131 | globalnames(...)
|
paul@627 | 132 |
|
paul@670 | 133 | At the module level, locals are effectively equivalent to module globals and
|
paul@670 | 134 | are declared as such.
|
paul@629 | 135 |
|
paul@629 | 136 | Each module's __main__ function will declare any referenced module globals as
|
paul@629 | 137 | globals. Note that the __main__ function is not a genuine attribute of any
|
paul@629 | 138 | module but an internal construct used to initialise modules appropriately.
|
paul@627 | 139 |
|
paul@627 | 140 | Such declarations must appear first in a program unit (module, function).
|
paul@627 | 141 | For example:
|
paul@627 | 142 |
|
paul@627 | 143 | def f(a, b):
|
paul@670 | 144 | localnames(a, b, x, y)
|
paul@670 | 145 | globalnames(f, g)
|
paul@627 | 146 |
|
paul@670 | 147 | storelocal(x, 1)
|
paul@670 | 148 | storelocal(y, x)
|
paul@670 | 149 | storelocal(a, b)
|
paul@670 | 150 | storeattr(module, g, f)
|
paul@627 | 151 |
|
paul@636 | 152 | Names and Attributes
|
paul@636 | 153 | --------------------
|
paul@636 | 154 |
|
paul@670 | 155 | Bare names refer to locals or globals according to the localnames and
|
paul@670 | 156 | globalnames declarations, or to constants such as None, True, False and
|
paul@638 | 157 | NotImplemented. Storage of local or global names is done using explicit
|
paul@638 | 158 | functions as follows:
|
paul@638 | 159 |
|
paul@670 | 160 | storelocal(name, value)
|
paul@670 | 161 | storeattr(module, name, value) # see below
|
paul@638 | 162 |
|
paul@627 | 163 | No operator usage: all operators are converted to invocations, including
|
paul@637 | 164 | all attribute access except static references to modules or particular class
|
paul@637 | 165 | or function definitions using the following notation:
|
paul@637 | 166 |
|
paul@670 | 167 | static(package)
|
paul@670 | 168 | static(package.module)
|
paul@670 | 169 | static(package.module.cls)
|
paul@670 | 170 | static(package.module.cls.function)
|
paul@627 | 171 |
|
paul@637 | 172 | A shorthand dot notation could be employed:
|
paul@637 | 173 |
|
paul@637 | 174 | package.module
|
paul@637 | 175 | package.module.cls
|
paul@637 | 176 | package.module.cls.function
|
paul@637 | 177 |
|
paul@637 | 178 | Where multiple definitions of static objects occur, the dot notation cannot be
|
paul@637 | 179 | used, and the full name of such definitions must be quoted. For example:
|
paul@637 | 180 |
|
paul@670 | 181 | static("package.module.cls#1.function")
|
paul@627 | 182 |
|
paul@627 | 183 | In general, attribute access must use an explicit function indicating the
|
paul@627 | 184 | kind of access operation being performed. For example:
|
paul@627 | 185 |
|
paul@670 | 186 | # context effect
|
paul@670 | 187 | # --------------
|
paul@670 | 188 | loadattr(obj, attrname) # preserve context
|
paul@670 | 189 | loadattrcontext(parent, attrname, obj) # replace context with obj
|
paul@670 | 190 | loadattrcontextcond(parent, attrname, obj) # run-time context decision
|
paul@670 | 191 | loadattrindex(obj, attrname) # preserve context
|
paul@670 | 192 | loadattrindexcontextcond(obj, attrname) # run-time context decision
|
paul@627 | 193 |
|
paul@670 | 194 | storeattr(obj, attrname, value) # preserve context
|
paul@670 | 195 | storeattrcontext(parent, attrname, value, obj) # replace context with obj
|
paul@670 | 196 | storeattrindex(obj, attrname, value)
|
paul@627 | 197 |
|
paul@638 | 198 | Temporary variables could employ similar functions:
|
paul@638 | 199 |
|
paul@670 | 200 | loadtemp(0)
|
paul@670 | 201 | storetemp(0, value)
|
paul@638 | 202 |
|
paul@636 | 203 | Operators and Invocations
|
paul@636 | 204 | -------------------------
|
paul@636 | 205 |
|
paul@627 | 206 | Conventional operators use the operator functions.
|
paul@627 | 207 |
|
paul@627 | 208 | Special operators could also use the operator functions (where available)
|
paul@627 | 209 | but might as well be supported directly:
|
paul@627 | 210 |
|
paul@627 | 211 | __is__(a, b)
|
paul@670 | 212 | __is_not__(a, b)
|
paul@627 | 213 |
|
paul@627 | 214 | Logical operators involving short-circuit evaluation could be represented
|
paul@627 | 215 | as function calls, but the evaluation semantics would be preserved:
|
paul@627 | 216 |
|
paul@627 | 217 | __and__(...) # returns the first non-true value or the final value
|
paul@627 | 218 | __not__(obj) # returns the inverse of the boolean interpretation of obj
|
paul@627 | 219 | __or__(...) # returns the first true value or the final value
|
paul@627 | 220 |
|
paul@627 | 221 | Comparisons could be rephrased in a verbose fashion:
|
paul@627 | 222 |
|
paul@627 | 223 | a < b < c becomes lt(a, b) and lt(b, c)
|
paul@627 | 224 | or __and__(lt(a, b), lt(b, c))
|
paul@627 | 225 |
|
paul@636 | 226 | Advanced Control-Flow
|
paul@636 | 227 | ---------------------
|
paul@636 | 228 |
|
paul@627 | 229 | Any statements requiring control-flow definition in terms of blocks must
|
paul@627 | 230 | be handled in the language as the notions of labels and blocks are not
|
paul@627 | 231 | introduced earlier apart from the special case of jumping to another
|
paul@627 | 232 | callable (described below).
|
paul@627 | 233 |
|
paul@627 | 234 | Special functions for low-level operations:
|
paul@627 | 235 |
|
paul@670 | 236 | check(obj, type)
|
paul@670 | 237 | jump(callable)
|
paul@627 | 238 |
|
paul@627 | 239 | Function/subroutine definition with entry points for checked and unchecked
|
paul@627 | 240 | parameters.
|
paul@627 | 241 |
|
paul@627 | 242 | def fn_checked(self, ...):
|
paul@670 | 243 | check(self, Type) # raises a TypeError if not isinstance(self, Type)
|
paul@670 | 244 | jump(fn_unchecked) # preserves the frame and return address
|
paul@627 | 245 |
|
paul@627 | 246 | def fn_unchecked(self, ...):
|
paul@627 | 247 | ...
|
paul@636 | 248 |
|
paul@670 | 249 | The jump function might also be used for inlining appropriate functions.
|
paul@644 | 250 |
|
paul@636 | 251 | Exceptions must also be handled in the language.
|
paul@644 | 252 |
|
paul@644 | 253 | Object Type Detection
|
paul@644 | 254 | ---------------------
|
paul@644 | 255 |
|
paul@644 | 256 | Occasionally, the type of an object (instance of a particular class, class,
|
paul@644 | 257 | and so on) needs to be determined at run-time:
|
paul@644 | 258 |
|
paul@670 | 259 | isclass(obj)
|