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@627 | 40 | (module-level) statements.
|
paul@627 | 41 |
|
paul@636 | 42 | Imports
|
paul@636 | 43 | -------
|
paul@636 | 44 |
|
paul@627 | 45 | Imports act as invocations of module code and name assignments within a
|
paul@627 | 46 | particular scope and are defined as follows:
|
paul@627 | 47 |
|
paul@627 | 48 | # import package
|
paul@627 | 49 | package.__main__()
|
paul@627 | 50 | package = __module__(package)
|
paul@627 | 51 |
|
paul@627 | 52 | # import package.module
|
paul@627 | 53 | package.__main__()
|
paul@627 | 54 | package.module.__main__()
|
paul@627 | 55 | package = __module__(package)
|
paul@627 | 56 |
|
paul@627 | 57 | # from package.module import cls
|
paul@627 | 58 | package.__main__()
|
paul@627 | 59 | package.module.__main__()
|
paul@627 | 60 | cls = __loadattribute__(package.module, cls) # see below
|
paul@627 | 61 |
|
paul@627 | 62 | Since import statements can appear in code that may be executed more than
|
paul@627 | 63 | once, __main__ functions should test and set a flag indicating whether the
|
paul@627 | 64 | function has already been called.
|
paul@627 | 65 |
|
paul@627 | 66 | Python would arguably be more sensible as a language if imports were
|
paul@627 | 67 | processed separately, but this would then rule out logic controlling the
|
paul@627 | 68 | use of modules.
|
paul@627 | 69 |
|
paul@636 | 70 | Name and Attribute Declarations
|
paul@636 | 71 | -------------------------------
|
paul@636 | 72 |
|
paul@629 | 73 | Assignments and name usage involve locals and globals but usage is declared
|
paul@629 | 74 | explicitly:
|
paul@627 | 75 |
|
paul@627 | 76 | __localnames__(...)
|
paul@627 | 77 |
|
paul@627 | 78 | At the function level, locals are genuine local name definitions whereas
|
paul@627 | 79 | globals refer to module globals:
|
paul@627 | 80 |
|
paul@627 | 81 | __globalnames__(...)
|
paul@627 | 82 |
|
paul@629 | 83 | At the module level, locals are effectively equivalent to module globals but
|
paul@629 | 84 | are declared as follows:
|
paul@629 | 85 |
|
paul@629 | 86 | __moduleattrs__(...)
|
paul@629 | 87 |
|
paul@629 | 88 | Each module's __main__ function will declare any referenced module globals as
|
paul@629 | 89 | globals. Note that the __main__ function is not a genuine attribute of any
|
paul@629 | 90 | module but an internal construct used to initialise modules appropriately.
|
paul@627 | 91 |
|
paul@627 | 92 | Such declarations must appear first in a program unit (module, function).
|
paul@627 | 93 | For example:
|
paul@627 | 94 |
|
paul@627 | 95 | def f(a, b):
|
paul@627 | 96 | __localnames__(a, b, x, y)
|
paul@627 | 97 | __globalnames__(f, g)
|
paul@627 | 98 |
|
paul@627 | 99 | x = 1 # local
|
paul@627 | 100 | y = x # locals
|
paul@627 | 101 | a = b # locals
|
paul@627 | 102 | g = f # globals
|
paul@627 | 103 |
|
paul@636 | 104 | Names and Attributes
|
paul@636 | 105 | --------------------
|
paul@636 | 106 |
|
paul@627 | 107 | No operator usage: all operators are converted to invocations, including
|
paul@627 | 108 | all attribute access except static references to modules using the
|
paul@627 | 109 | following notation:
|
paul@627 | 110 |
|
paul@627 | 111 | __module__(package)
|
paul@627 | 112 | __module__(package.module)
|
paul@627 | 113 | package.module # shorthand where dot notation is used
|
paul@627 | 114 |
|
paul@627 | 115 | In general, attribute access must use an explicit function indicating the
|
paul@627 | 116 | kind of access operation being performed. For example:
|
paul@627 | 117 |
|
paul@627 | 118 | __loadaddress__(obj, attrname)
|
paul@627 | 119 | __loadaddresscontext__(obj, attrname)
|
paul@627 | 120 | __loadaddresscontextcond__(obj, attrname)
|
paul@627 | 121 | __loadattr__(obj, attrname)
|
paul@627 | 122 | __loadattrindex__(obj, attrname)
|
paul@627 | 123 | __loadattrindexcontext__(obj, attrname)
|
paul@627 | 124 | __loadattrindexcontextcond__(obj, attrname)
|
paul@627 | 125 |
|
paul@627 | 126 | __storeaddress__(obj, attrname, value)
|
paul@627 | 127 | __storeaddresscontext__(obj, attrname, value)
|
paul@627 | 128 | __storeattr__(obj, attrname, value)
|
paul@627 | 129 | __storeattrindex__(obj, attrname, value)
|
paul@627 | 130 |
|
paul@636 | 131 | Operators and Invocations
|
paul@636 | 132 | -------------------------
|
paul@636 | 133 |
|
paul@627 | 134 | Conventional operators use the operator functions.
|
paul@627 | 135 |
|
paul@627 | 136 | Special operators could also use the operator functions (where available)
|
paul@627 | 137 | but might as well be supported directly:
|
paul@627 | 138 |
|
paul@627 | 139 | __is__(a, b)
|
paul@627 | 140 |
|
paul@627 | 141 | Logical operators involving short-circuit evaluation could be represented
|
paul@627 | 142 | as function calls, but the evaluation semantics would be preserved:
|
paul@627 | 143 |
|
paul@627 | 144 | __and__(...) # returns the first non-true value or the final value
|
paul@627 | 145 | __not__(obj) # returns the inverse of the boolean interpretation of obj
|
paul@627 | 146 | __or__(...) # returns the first true value or the final value
|
paul@627 | 147 |
|
paul@627 | 148 | Comparisons could be rephrased in a verbose fashion:
|
paul@627 | 149 |
|
paul@627 | 150 | a < b < c becomes lt(a, b) and lt(b, c)
|
paul@627 | 151 | or __and__(lt(a, b), lt(b, c))
|
paul@627 | 152 |
|
paul@636 | 153 | Advanced Control-Flow
|
paul@636 | 154 | ---------------------
|
paul@636 | 155 |
|
paul@627 | 156 | Any statements requiring control-flow definition in terms of blocks must
|
paul@627 | 157 | be handled in the language as the notions of labels and blocks are not
|
paul@627 | 158 | introduced earlier apart from the special case of jumping to another
|
paul@627 | 159 | callable (described below).
|
paul@627 | 160 |
|
paul@627 | 161 | Special functions for low-level operations:
|
paul@627 | 162 |
|
paul@627 | 163 | __check__(obj, type)
|
paul@627 | 164 | __jump__(callable)
|
paul@627 | 165 |
|
paul@627 | 166 | Function/subroutine definition with entry points for checked and unchecked
|
paul@627 | 167 | parameters.
|
paul@627 | 168 |
|
paul@627 | 169 | def fn_checked(self, ...):
|
paul@627 | 170 | __check__(self, Type) # raises a TypeError if not isinstance(self, Type)
|
paul@627 | 171 | __jump__(fn_unchecked) # preserves the frame and return address
|
paul@627 | 172 |
|
paul@627 | 173 | def fn_unchecked(self, ...):
|
paul@627 | 174 | ...
|
paul@636 | 175 |
|
paul@636 | 176 | Exceptions must also be handled in the language.
|