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@636 | 11 | Program Data and Data Structure Definition
|
paul@636 | 12 | ------------------------------------------
|
paul@636 | 13 |
|
paul@627 | 14 | Given that micropython has already deduced object and parameter details,
|
paul@627 | 15 | such information must be communicated in the systems programming language
|
paul@627 | 16 | so that the compiler does not have to deduce it again.
|
paul@627 | 17 |
|
paul@627 | 18 | Explicit constant declaration shall be done at the start of the main
|
paul@627 | 19 | module:
|
paul@627 | 20 |
|
paul@627 | 21 | __constants__(...)
|
paul@627 | 22 |
|
paul@627 | 23 | Explicit structure declaration is still performed using class statements,
|
paul@627 | 24 | but base classes are omitted and attributes are declared explicitly as
|
paul@627 | 25 | follows:
|
paul@627 | 26 |
|
paul@627 | 27 | class C:
|
paul@627 | 28 | __instattrs__(member...)
|
paul@627 | 29 | __classattrs__(member...)
|
paul@627 | 30 |
|
paul@627 | 31 | Other object table information, such as inherited class attributes and
|
paul@627 | 32 | class compatibility (to support isinstance) are also declared explicitly:
|
paul@627 | 33 |
|
paul@627 | 34 | __inherited__(superclass, member...)
|
paul@627 | 35 | __descendants__(class...)
|
paul@627 | 36 |
|
paul@627 | 37 | Other than function definitions, no other code statements shall appear in
|
paul@627 | 38 | class definitions; such statements will appear after classes have been
|
paul@627 | 39 | defined in a __main__ function collecting together all "loose"
|
paul@637 | 40 | (module-level) statements; class attribute assignments will occur in the
|
paul@637 | 41 | __main__ function, and where a name is associated with a function definition
|
paul@637 | 42 | and another object, the function will also be explicitly assigned in the
|
paul@637 | 43 | __main__ function using its full name.
|
paul@637 | 44 |
|
paul@637 | 45 | Any class or function defined once in a namespace need not be assigned to that
|
paul@637 | 46 | namespace in the __main__ function, but where multiple definitions exist and
|
paul@637 | 47 | program logic determines which definition prevails, such definitions must be
|
paul@637 | 48 | assigned in the __main__ function.
|
paul@637 | 49 |
|
paul@637 | 50 | For example:
|
paul@637 | 51 |
|
paul@637 | 52 | class C:
|
paul@637 | 53 | def method(self, ...):
|
paul@637 | 54 | ...
|
paul@637 | 55 | if something:
|
paul@637 | 56 | method = something
|
paul@637 | 57 |
|
paul@637 | 58 | This is represented as follows:
|
paul@637 | 59 |
|
paul@637 | 60 | class C:
|
paul@637 | 61 | ...
|
paul@637 | 62 | def method(self, ...):
|
paul@637 | 63 | ...
|
paul@637 | 64 |
|
paul@637 | 65 | def __main__():
|
paul@637 | 66 | ...
|
paul@637 | 67 | method = module.C.method
|
paul@637 | 68 | if something:
|
paul@637 | 69 | __storeaddress__(module.C, something)
|
paul@627 | 70 |
|
paul@636 | 71 | Imports
|
paul@636 | 72 | -------
|
paul@636 | 73 |
|
paul@627 | 74 | Imports act as invocations of module code and name assignments within a
|
paul@627 | 75 | particular scope and are defined as follows:
|
paul@627 | 76 |
|
paul@627 | 77 | # import package
|
paul@627 | 78 | package.__main__()
|
paul@627 | 79 | package = __module__(package)
|
paul@627 | 80 |
|
paul@627 | 81 | # import package.module
|
paul@627 | 82 | package.__main__()
|
paul@627 | 83 | package.module.__main__()
|
paul@627 | 84 | package = __module__(package)
|
paul@627 | 85 |
|
paul@627 | 86 | # from package.module import cls
|
paul@627 | 87 | package.__main__()
|
paul@627 | 88 | package.module.__main__()
|
paul@627 | 89 | cls = __loadattribute__(package.module, cls) # see below
|
paul@627 | 90 |
|
paul@627 | 91 | Since import statements can appear in code that may be executed more than
|
paul@627 | 92 | once, __main__ functions should test and set a flag indicating whether the
|
paul@627 | 93 | function has already been called.
|
paul@627 | 94 |
|
paul@627 | 95 | Python would arguably be more sensible as a language if imports were
|
paul@627 | 96 | processed separately, but this would then rule out logic controlling the
|
paul@627 | 97 | use of modules.
|
paul@627 | 98 |
|
paul@636 | 99 | Name and Attribute Declarations
|
paul@636 | 100 | -------------------------------
|
paul@636 | 101 |
|
paul@629 | 102 | Assignments and name usage involve locals and globals but usage is declared
|
paul@629 | 103 | explicitly:
|
paul@627 | 104 |
|
paul@627 | 105 | __localnames__(...)
|
paul@627 | 106 |
|
paul@627 | 107 | At the function level, locals are genuine local name definitions whereas
|
paul@627 | 108 | globals refer to module globals:
|
paul@627 | 109 |
|
paul@627 | 110 | __globalnames__(...)
|
paul@627 | 111 |
|
paul@629 | 112 | At the module level, locals are effectively equivalent to module globals but
|
paul@629 | 113 | are declared as follows:
|
paul@629 | 114 |
|
paul@629 | 115 | __moduleattrs__(...)
|
paul@629 | 116 |
|
paul@629 | 117 | Each module's __main__ function will declare any referenced module globals as
|
paul@629 | 118 | globals. Note that the __main__ function is not a genuine attribute of any
|
paul@629 | 119 | module but an internal construct used to initialise modules appropriately.
|
paul@627 | 120 |
|
paul@627 | 121 | Such declarations must appear first in a program unit (module, function).
|
paul@627 | 122 | For example:
|
paul@627 | 123 |
|
paul@627 | 124 | def f(a, b):
|
paul@627 | 125 | __localnames__(a, b, x, y)
|
paul@627 | 126 | __globalnames__(f, g)
|
paul@627 | 127 |
|
paul@627 | 128 | x = 1 # local
|
paul@627 | 129 | y = x # locals
|
paul@627 | 130 | a = b # locals
|
paul@627 | 131 | g = f # globals
|
paul@627 | 132 |
|
paul@636 | 133 | Names and Attributes
|
paul@636 | 134 | --------------------
|
paul@636 | 135 |
|
paul@627 | 136 | No operator usage: all operators are converted to invocations, including
|
paul@637 | 137 | all attribute access except static references to modules or particular class
|
paul@637 | 138 | or function definitions using the following notation:
|
paul@637 | 139 |
|
paul@637 | 140 | __static__(package)
|
paul@637 | 141 | __static__(package.module)
|
paul@637 | 142 | __static__(package.module.cls)
|
paul@637 | 143 | __static__(package.module.cls.function)
|
paul@627 | 144 |
|
paul@637 | 145 | A shorthand dot notation could be employed:
|
paul@637 | 146 |
|
paul@637 | 147 | package.module
|
paul@637 | 148 | package.module.cls
|
paul@637 | 149 | package.module.cls.function
|
paul@637 | 150 |
|
paul@637 | 151 | Where multiple definitions of static objects occur, the dot notation cannot be
|
paul@637 | 152 | used, and the full name of such definitions must be quoted. For example:
|
paul@637 | 153 |
|
paul@637 | 154 | __static__("package.module.cls#1.function")
|
paul@627 | 155 |
|
paul@627 | 156 | In general, attribute access must use an explicit function indicating the
|
paul@627 | 157 | kind of access operation being performed. For example:
|
paul@627 | 158 |
|
paul@627 | 159 | __loadaddress__(obj, attrname)
|
paul@627 | 160 | __loadaddresscontext__(obj, attrname)
|
paul@627 | 161 | __loadaddresscontextcond__(obj, attrname)
|
paul@627 | 162 | __loadattr__(obj, attrname)
|
paul@627 | 163 | __loadattrindex__(obj, attrname)
|
paul@627 | 164 | __loadattrindexcontext__(obj, attrname)
|
paul@627 | 165 | __loadattrindexcontextcond__(obj, attrname)
|
paul@627 | 166 |
|
paul@627 | 167 | __storeaddress__(obj, attrname, value)
|
paul@627 | 168 | __storeaddresscontext__(obj, attrname, value)
|
paul@627 | 169 | __storeattr__(obj, attrname, value)
|
paul@627 | 170 | __storeattrindex__(obj, attrname, value)
|
paul@627 | 171 |
|
paul@636 | 172 | Operators and Invocations
|
paul@636 | 173 | -------------------------
|
paul@636 | 174 |
|
paul@627 | 175 | Conventional operators use the operator functions.
|
paul@627 | 176 |
|
paul@627 | 177 | Special operators could also use the operator functions (where available)
|
paul@627 | 178 | but might as well be supported directly:
|
paul@627 | 179 |
|
paul@627 | 180 | __is__(a, b)
|
paul@627 | 181 |
|
paul@627 | 182 | Logical operators involving short-circuit evaluation could be represented
|
paul@627 | 183 | as function calls, but the evaluation semantics would be preserved:
|
paul@627 | 184 |
|
paul@627 | 185 | __and__(...) # returns the first non-true value or the final value
|
paul@627 | 186 | __not__(obj) # returns the inverse of the boolean interpretation of obj
|
paul@627 | 187 | __or__(...) # returns the first true value or the final value
|
paul@627 | 188 |
|
paul@627 | 189 | Comparisons could be rephrased in a verbose fashion:
|
paul@627 | 190 |
|
paul@627 | 191 | a < b < c becomes lt(a, b) and lt(b, c)
|
paul@627 | 192 | or __and__(lt(a, b), lt(b, c))
|
paul@627 | 193 |
|
paul@636 | 194 | Advanced Control-Flow
|
paul@636 | 195 | ---------------------
|
paul@636 | 196 |
|
paul@627 | 197 | Any statements requiring control-flow definition in terms of blocks must
|
paul@627 | 198 | be handled in the language as the notions of labels and blocks are not
|
paul@627 | 199 | introduced earlier apart from the special case of jumping to another
|
paul@627 | 200 | callable (described below).
|
paul@627 | 201 |
|
paul@627 | 202 | Special functions for low-level operations:
|
paul@627 | 203 |
|
paul@627 | 204 | __check__(obj, type)
|
paul@627 | 205 | __jump__(callable)
|
paul@627 | 206 |
|
paul@627 | 207 | Function/subroutine definition with entry points for checked and unchecked
|
paul@627 | 208 | parameters.
|
paul@627 | 209 |
|
paul@627 | 210 | def fn_checked(self, ...):
|
paul@627 | 211 | __check__(self, Type) # raises a TypeError if not isinstance(self, Type)
|
paul@627 | 212 | __jump__(fn_unchecked) # preserves the frame and return address
|
paul@627 | 213 |
|
paul@627 | 214 | def fn_unchecked(self, ...):
|
paul@627 | 215 | ...
|
paul@636 | 216 |
|
paul@636 | 217 | Exceptions must also be handled in the language.
|