Commit Graph

285 Commits

Author SHA1 Message Date
Rachel Dowdall 721c55dced Added exception hierarchy except for OSError and UnicodeError (requires arguments). Comment out the errors that aren't needed if memory becomes an issue. 2014-03-22 15:28:16 +00:00
Rachel Dowdall 17f45d41fe Merge remote-tracking branch 'upstream/master' 2014-03-22 12:17:36 +00:00
Paul Sokolovsky 1ecea7c753 py: Make 'bytes' be a proper type, support standard constructor args. 2014-03-22 00:07:04 +02:00
Paul Sokolovsky be020c27a8 py: Make 'str' be a proper type, support standard constructor args. 2014-03-22 00:07:04 +02:00
Damien George 6e48f7fa85 py: Allow 'complex()' to take a string as first argument. 2014-03-21 11:45:46 +00:00
Damien George c06ea7abf2 py: Implement parsing of infinity and nan for floats. 2014-03-21 10:55:08 +00:00
Rachel Dowdall 300c8bd4c2 Added ZeroDivisionError to float division. 2014-03-20 22:40:38 +00:00
xbe efe3422394 py: Clean up includes.
Remove unnecessary includes. Add includes that improve portability.
2014-03-17 02:43:40 -07:00
Damien George 9d68e9ccdd py: Implement integer overflow checking for * and << ops.
If operation will overflow, a multi-precision integer is created.
2014-03-12 15:38:15 +00:00
Damien George 0ec6bd47cb py: Fix printing of type name. 2014-03-09 16:29:36 +00:00
Damien George 0c36da0b59 Implement ROMable modules. Add math module.
mp_module_obj_t can now be put in ROM.

Configuration of float type is now similar to longint: can now choose
none, float or double as the implementation.

math module has basic math functions.  For STM port, these are not yet
implemented (they are just stub functions).
2014-03-08 15:24:39 +00:00
Damien George 715101580b py: Factor and improve issubclass. 2014-03-03 22:38:13 +00:00
Paul Sokolovsky d08fd68664 Add basic collections.namedtuple implementation. 2014-03-03 11:42:53 +08:00
Damien George 41eb6086b7 py: Remove more var arg names fro macros with var args. 2014-02-26 22:40:35 +00:00
Damien George 510477557d py: Take out bitfield entries from their own structure.
Don't need to wrap bitfields in their own struct.  Compiler does the
correct thing without it.
2014-02-26 17:40:52 +00:00
Damien George 2077397118 py: Put number parsing code together in parsenum.c. 2014-02-22 18:12:43 +00:00
Paul Sokolovsky ac2e28c654 Support passing positional args as keywords to bytecode functions.
For this, record argument names along with each bytecode function. The code
still includes extensive debug logging support so far.
2014-02-16 18:36:33 +02:00
Paul Sokolovsky 44739e280e Make DEBUG_printf() a proper function, implementation is port-dependent.
In particular, unix outputs to stderr, to allow to run testsuite against
micropython built with debug output (by redirecting stderr to /dev/null).
2014-02-16 18:20:49 +02:00
Damien George 2e482cdb7b py: Implement *vargs support.
Addresses issue #295.
2014-02-16 00:01:29 +00:00
Damien George 22a0865d54 py: Improve exception bases, reduces ROM usage.
Thanks to @pfalcon for the tip!
2014-02-15 21:05:25 +00:00
Damien George 8725f8f7de py: Pass all scope flags through to runtime. 2014-02-15 19:33:11 +00:00
Damien George c5966128c7 Implement proper exception type hierarchy.
Each built-in exception is now a type, with base type BaseException.
C exceptions are created by passing a pointer to the exception type to
make an instance of.  When raising an exception from the VM, an
instance is created automatically if an exception type is raised (as
opposed to an exception instance).

Exception matching (RT_BINARY_OP_EXCEPTION_MATCH) is now proper.

Handling of parse error changed to match new exceptions.

mp_const_type renamed to mp_type_type for consistency.
2014-02-15 16:10:44 +00:00
Damien George 7ef098000a Merge pull request #288 from pfalcon/port-builtins
Allow ports to define statically builtin functions.
2014-02-14 23:05:50 +00:00
Paul Sokolovsky 910843e86d Allow ports to define statically builtin functions.
Convert unix open() to such.
2014-02-14 12:13:34 +02:00
Paul Sokolovsky da1ce93da8 Implement "from module import *" construct. 2014-02-14 00:30:36 +02:00
Paul Sokolovsky 520e2f58a5 Replace global "static" -> "STATIC", to allow "analysis builds". Part 2. 2014-02-12 18:31:30 +02:00
Damien George 8c2b333aff Merge branch 'master' of github.com:micropython/micropython 2014-02-10 21:41:14 +00:00
Paul Sokolovsky 76f06de96d Add NotImplementedError. 2014-02-09 13:17:36 +02:00
Damien George 7d0bfbedd2 py: Add some qstrs to the global table. 2014-02-08 19:01:47 +00:00
Damien George aea532ece1 py: Put builtins into ROM table.
Linear table at the moment, to eventually be replaced with a hash table
generated by a preprocessor.

Dynamic table is retained so that builtins can be overridden.
2014-02-06 22:57:51 +00:00
Damien George 64131f3215 Add staticmethod and classmethod to builtin namespace. 2014-02-06 20:31:44 +00:00
Damien George cdcb4906d4 Merge pull request #262 from pfalcon/sys-path
Implement sys.path support
2014-02-05 21:27:05 +00:00
Damien George 35e2a4e6bb py: Add built-in super. 2014-02-05 00:51:47 +00:00
Paul Sokolovsky e11b17c25f Implement support for sys.path when loading modules.
sys.path is not initialized by rt_init(), that's left for platform-specific
startup code. (For example, bare metal port may have some hardcoded defaults,
and let user change sys.path directly; while port for OS with environment
feature can take path from environment). If it's not explicitly initialized,
modules will be imported only from a current directory.
2014-02-05 01:40:41 +02:00
Paul Sokolovsky 0c59db1973 Use qstr id to create sys module. 2014-02-04 19:36:00 +02:00
Paul Sokolovsky 7cd54d79db Add ImportError. 2014-02-04 03:33:55 +02:00
Paul Sokolovsky 1d938c9503 Expose __import__() function. 2014-02-04 00:47:06 +02:00
Damien George ca4767984b py: Implement builtin exec. 2014-02-03 22:44:10 +00:00
Damien George 4acb2452b3 py: Add very basic implementation of dir() builtin.
Only works on modules and class instances.
2014-02-02 22:07:44 +00:00
Damien George 93d004f4b0 Merge branch 'xbe-master' 2014-02-02 13:38:41 +00:00
Damien George 09e1f43200 Merge branch 'master' of github.com:micropython/micropython 2014-02-02 13:13:29 +00:00
Damien George cd82e02e84 py: Partially fix native emitter to work with latest runtime.
Native emitter has been broken since stack order has changed from
reverse to standard.  This fix gets it partially working.
2014-02-02 13:11:48 +00:00
xbe 0ebf8534ab Implement and add tests for the id() builtin function. 2014-02-01 19:00:41 -08:00
Paul Sokolovsky edbdf71f5c rt_unpack_sequence(): Support generic iterables. 2014-02-02 01:34:11 +02:00
Damien George 48697f1dd2 Tidy up some comments. 2014-02-01 23:32:29 +00:00
Damien George 9aa2a527b5 py: Tidy up BINARY_OPs; negation done by special NOT bytecode.
IS_NOT and NOT_IN are now compiled to IS + NOT and IN + NOT, with a new
special NOT bytecode.
2014-02-01 23:04:09 +00:00
Paul Sokolovsky 90750029df Implement default function arguments (for Python functions).
TODO: Decide if we really need separate bytecode for creating functions
with default arguments - we would need same for closures, then there're
keywords arguments too. Having all combinations is a small exponential
explosion, likely we need just 2 cases - simplest (no defaults, no kw),
and full - defaults & kw.
2014-02-01 15:38:22 +02:00
Damien George 8b56beb1a1 py: Simplified rt_call_function_n_kw. 2014-01-31 23:49:49 +00:00
Markus Siemens 2c2a124e16 Fix SIGSEV when running "a"()
rt_call_function_n_kw did check for integers but not for strings
being called. Added a check so running "a"() won't SIGSEV but
throw an exception.
2014-01-31 22:16:23 +01:00
Damien George 3f759b71c6 py: Add MICROPY_ENABLE_FLOAT around a float specific piece of code. 2014-01-31 00:42:12 +00:00
Damien George 09a0c64bce py: Improve __bool__ and __len__ dispatch; add slots for them. 2014-01-30 10:05:33 +00:00
Paul Sokolovsky c1d9bbc345 Implement __bool__ and __len__ via unary_op virtual method for all types.
__bool__() and __len__() are just the same as __neg__() or __invert__(),
and require efficient dispatching implementation (not requiring search/lookup).
type->unary_op() is just the right choice for this short of adding
standalone virtual method(s) to already big mp_obj_type_t structure.
2014-01-30 04:37:19 +02:00
Damien George d0691ccaec py: Simplify fastn in VM; reduce size of unique code struct.
We still have FAST_[0,1,2] byte codes, but they now just access the
fastn array (before they had special local variables).  It's now
simpler, a bit faster, and uses a bit less stack space (on STM at least,
which is most important).

The only reason now to keep FAST_[0,1,2] byte codes is for compressed
byte code size.
2014-01-29 20:30:52 +00:00
Damien George 08d075592f py: Fix bug with LOAD_METHOD; fix int->machine_int_t for small int.
LOAD_METHOD bug was: emitbc did not correctly calculate the amount of
stack usage for a LOAD_METHOD operation.

small int bug was: int was being used to pass small ints, when it should
have been machine_int_t.
2014-01-29 18:58:52 +00:00
Damien George e4b6a079b3 py: Implement 'not' in compiler, and improve rt_is_true. 2014-01-28 23:27:35 +00:00
Paul Sokolovsky 0dd0467a97 Don't pre-import array module. 2014-01-28 03:18:30 +02:00
Damien George 4e8dc8c41b py: Add unary op not for NoneType, bool, tuple, list, dict; fix for int. 2014-01-27 23:15:32 +00:00
Damien George 0f5e8b151f Merge branch 'master' of github.com:micropython/micropython 2014-01-25 00:18:30 +00:00
Damien George 7c9c667633 py: Implement iterator support for object that has __getitem__.
Addresses Issue #203.
2014-01-25 00:17:36 +00:00
Paul Sokolovsky c1d200ef74 rt_deinit(): Finalize some maps. 2014-01-25 02:04:07 +02:00
Paul Sokolovsky 91fb1c9b13 Add basic implementation of bytes type, piggybacking on str.
This reuses as much str implementation as possible, from this we
can make them more separate as needed.
2014-01-24 22:56:26 +02:00
Damien George 1e708fed18 py: Implement bool unary op; tidy up unary op dispatch. 2014-01-23 18:27:51 +00:00
Damien George 0d028743aa py: Initialise loaded_module map in rt_init.
STM port crashes without this re-init.  There should not be any state in
the core py/ code that relies on pre-initialised data.
2014-01-22 23:59:20 +00:00
Damien George 5fa93b6755 Second stage of qstr revamp: uPy str object can be qstr or not. 2014-01-22 14:35:10 +00:00
Damien George 12eaccacda Merge branch 'master' of github.com:micropython/micropython
Conflicts:
	py/objstr.c
	py/py.mk
	py/stream.c
	unix/main.c
	unix/socket.c
2014-01-21 21:54:15 +00:00
Damien George 55baff4c9b Revamp qstrs: they now include length and hash.
Can now have null bytes in strings.  Can define ROM qstrs per port using
qstrdefsport.h
2014-01-21 21:40:13 +00:00
Paul Sokolovsky a80ff04fe7 Add dummy bytes() constructor.
Currently, MicroPython strings are mix between CPython byte and unicode
strings. So, conversion is null so far. This dummy implementation is
intended for compatibility with CPython (so, same code can run on both).
2014-01-20 20:37:01 +02:00
Damien George 91d457a277 py: Put micropython module init code in builtinmp.c. 2014-01-20 10:30:24 +00:00
Paul Sokolovsky 440cc3f028 Expose memory stats functions via "micropython" module.
These are micropython.mem_total(), .mem_current(), .mem_peak(). These are 3
individual functions with simple scalar return value to make sure that
calls to these functions themselves have minimal (hopefully zero) impact on
memory allocation.
2014-01-20 02:20:40 +02:00
Paul Sokolovsky 159c0f75da Don't implicitly import "sys" module. 2014-01-20 01:57:20 +02:00
Paul Sokolovsky f477bfbc75 Pre-create sys module. 2014-01-20 01:00:28 +02:00
Damien George cbd2f7482c py: Add module/function/class name to exceptions.
Exceptions know source file, line and block name.

Also tidy up some debug printing functions and provide a global
flag to enable/disable them.
2014-01-19 11:48:48 +00:00
Damien George 932bf1c48f py: Fix VM/runtime unpack sequence bug, Issue #193. 2014-01-18 23:42:49 +00:00
Damien George 6b21538e91 Merge pull request #192 from pfalcon/arrays
Add skeleton implementation of array.array and bytearray.
2014-01-18 15:31:57 -08:00
Damien George f49ba1bd9c Improve method lookup in mp_obj_class_lookup.
Now searches both locals_dict and methods.  Partly addresses Issue #145.
2014-01-18 17:52:41 +00:00
Paul Sokolovsky 427905cedd Add skeleton implementation of array.array and bytearray.
So far, only storage, initialization, repr() and buffer protocol is
implemented - alredy suitable for passing binary data around.
2014-01-18 19:27:38 +02:00
Damien George 0c4e909e76 Merge pull request #191 from pfalcon/store-item
Add store_item() virtual method to type to implement container[index] = val
2014-01-18 06:57:47 -08:00
Damien George 632cf5710c Merge branch 'master' of github.com:dpgeorge/micropython 2014-01-18 14:15:48 +00:00
Damien George 20006dbba9 Make VM stack grow upwards, and so no reversed args arrays.
Change state layout in VM so the stack starts at state[0] and grows
upwards.  Locals are at the top end of the state and number downwards.
This cleans up a lot of the interface connecting the VM to C: now all
functions that take an array of Micro Python objects are in order (ie no
longer in reverse).

Also clean up C API with keyword arguments (call_n and call_n_kw
replaced with single call method that takes keyword arguments).  And now
make_new takes keyword arguments.

emitnative.c has not yet been changed to comply with the new order of
stack layout.
2014-01-18 14:10:48 +00:00
Paul Sokolovsky 6d8edf6acf Add store_item() virtual method to type to implement container[index] = val. 2014-01-18 13:11:59 +02:00
Paul Sokolovsky 166bb40fb2 Add OverflowError and use it for small int overflow instead of assert. 2014-01-18 12:46:43 +02:00
Paul Sokolovsky 10744dd816 Add empty (false) value testing for strings, tuples, lists, dicts. 2014-01-16 23:54:17 +02:00
Paul Sokolovsky dcac88095b Add empty "micropython" module to allow more seamless CPython portability.
Implicit "micropython" module contains (at least) codegeneration decorators.
Make it explicit, so an app could have "import micropython". On MicroPython,
that will be no-op. On CPython, that will give a chance to have a module
with placeholder decorators.
2014-01-16 19:29:11 +02:00
Damien George 4899ff9470 Merge branch 'str-repr' of github.com:pfalcon/micropython into pfalcon-str-repr
Conflicts:
	tests/basics/tests/exception1.py
2014-01-15 22:39:03 +00:00
Damien George 7a9d0c4540 Merge branch 'builtins' of github.com:chipaca/micropython into chipaca-builtins
Added some checks for number of arguments.

Conflicts:
	py/mpqstrraw.h
2014-01-15 22:27:16 +00:00
Damien George d02c6d8962 Implement eval. 2014-01-15 22:14:03 +00:00
Damien George e2fb2baaa4 Implement repr. 2014-01-15 21:40:48 +00:00
John R. Lenton fca456bc3c added filter() 2014-01-15 01:37:08 +00:00
John R. Lenton 39b174e00a Added map 2014-01-15 01:10:09 +00:00
Paul Sokolovsky 36c4499d36 Implement str() and repr() builtin functions. 2014-01-15 02:15:47 +02:00
John R. Lenton ff8007c7d6 Merge remote-tracking branch 'upstream/master' into builtins 2014-01-14 23:58:05 +00:00
John R. Lenton 9daa78943e added enumerate() 2014-01-14 23:55:01 +00:00
Damien George 66a5bf681d Merge pull request #142 from chipaca/containment
Implemented support for `in` and `not in` operators.
2014-01-14 15:23:09 -08:00
Damien George 6c2401e935 Merge pull request #165 from chipaca/builtins
added zip()
2014-01-14 15:13:40 -08:00
Paul Sokolovsky 8bc96471f0 Implement "is" and "is not" operators.
So far, don't work for strings as expected.
2014-01-15 00:32:09 +02:00
John R. Lenton 93451002f0 Merge remote-tracking branch 'upstream/master' into builtins
Conflicts:
	py/builtin.c
	py/builtin.h
	py/runtime.c
2014-01-13 23:14:35 +00:00
John R. Lenton f5a0a7d2b3 Merge remote-tracking branch 'upstream/master' into containment 2014-01-13 23:09:04 +00:00
Damien George f62d33aa1d Consolidate rt_make_function_[0123] to rt_make_function_n. 2014-01-13 19:50:05 +00:00
Damien George 2300537c79 Cleanup built-ins, and fix some compiler warnings/errors. 2014-01-13 19:39:01 +00:00
John R. Lenton 5c76839559 sorted 2014-01-13 05:12:50 +00:00
John R. Lenton 07205ec323 added zip() 2014-01-13 02:31:00 +00:00
John R. Lenton 13e64f0660 Merge remote-tracking branch 'upstream/master' into containment 2014-01-13 00:41:12 +00:00
Paul Sokolovsky 757ac81a69 Add proper checks for fits-in-small-int. Make it reusable.
We likely should make mp_obj_new_int() inline, and rely on its
encapsulated check rather than inline checks everywhere explicitly.

Also, parser for big small int values is still broken.
2014-01-12 22:04:21 +02:00
Damien George c0876f7ca8 Merge pull request #146 from pfalcon/assert-exc
Add AssertionError.
2014-01-12 04:26:54 -08:00
Paul Sokolovsky 729e9cce7b rt_binary_op(): Don't fall thru in case small_int op result doesn't fit back.
Currently it would report "operation not supported" which is confusing. Overall,
this is thinko leading to undefined behavior.
2014-01-12 03:33:01 +02:00
Paul Sokolovsky b81e1fdef7 Add AssertionError. 2014-01-12 00:20:49 +02:00
Damien George eae16445d5 py: Implement staticmethod and classmethod (internally).
Still need to make built-ins by these names, and write tests.
2014-01-11 19:22:29 +00:00
John R. Lenton 813edf63a3 Merge remote-tracking branch 'upstream/master' into containment
Conflicts:
	py/runtime.c
2014-01-11 16:34:56 +00:00
John R. Lenton c1bef21920 Implemented support for `in` and `not in` operators. 2014-01-11 12:39:33 +00:00
Damien George bcbeea0a47 py: Fix bug where == and != not handled for small_ints. 2014-01-11 10:47:22 +00:00
John R. Lenton b8698fca75 unified the bops 2014-01-11 00:58:59 +00:00
Paul Sokolovsky bab5cfb34f Unsupported operand types for binary operator: dump both args' types. 2014-01-11 00:14:48 +02:00
Damien George 004cdcebfe py: Implement base class lookup, issubclass, isinstance. 2014-01-09 21:43:51 +00:00
Damien George 062478e66d Improved type/class/instance code; mp_obj_type_t now has load_attr, store_attr.
Creating of classes (types) and instances is much more like CPython now.
You can use "type('name', (), {...})" to create classes.
2014-01-09 20:57:50 +00:00
Damien George 93a9b5b64d py: Proper framework for built-in 'type'. 2014-01-08 18:48:12 +00:00
Damien George 6c73ca1e75 py: add variable argument exception constructor function.
Addresses issue #104.
2014-01-08 18:11:23 +00:00
Damien George 38a2da68c2 py: Stuff qstr in object pointer; keys for mp_map_t are now always mp_obj_t. 2014-01-08 17:33:12 +00:00
Damien George b97669ab94 py: Improve __build_class__. 2014-01-08 11:47:55 +00:00
John R. Lenton 9c83ec0eda Merge remote-tracking branch 'upstream/master' into dict_feats 2014-01-07 23:06:46 +00:00
John R. Lenton 88f3043e0a added a first pass of dict.update 2014-01-07 22:51:08 +00:00
Damien George 1a9951d5aa py: Fix up number operations and coercion. 2014-01-07 22:51:08 +00:00
Damien George 71c5181a8d Convert Python types to proper Python type hierarchy.
Now much more inline with how CPython does types.
2014-01-04 20:21:15 +00:00
Damien George e9906ac3d7 Add ellipsis object. 2014-01-04 18:44:46 +00:00
Damien George eb7bfcb286 Split qstr into pools, and put initial pool in ROM.
Qstr's are now split into a linked-list of qstr pools.  This has 2
benefits: the first pool can be in ROM (huge benefit, since we no longer
use RAM for the core qstrs), and subsequent pools use m_new for the next
pool instead of m_renew (thus avoiding a huge single table for all the
qstrs).

Still would be better to use a hash table, but this scheme takes us part
of the way (eventually convert the pools to hash tables).

Also fixed bug with import.

Also improved the way the module code is referenced (not magic number 1
anymore).
2014-01-04 15:57:35 +00:00
Damien George 14f945c2ca Add note about implementing inplace operators. 2014-01-03 14:09:31 +00:00
Damien George 66028ab6dc Basic implementation of import.
import works for simple cases.  Still work to do on finding the right
script, and setting globals/locals correctly when running an imported
function.
2014-01-03 14:03:48 +00:00
Damien George 2870862601 Add module object, to be used eventually for import. 2014-01-02 21:30:26 +00:00
Damien George 40563d56bd py: Add framework for built-in "type()" function. 2014-01-02 16:01:17 +00:00
Damien George 209d1b1835 py: add int() and float() built-ins, partially implemented. 2014-01-01 17:03:35 +00:00
Damien George 6baf76e28b py: make closures work. 2013-12-30 22:32:17 +00:00
Damien George 8cc96a35e5 Put unicode functions in unicode.c, and tidy their names. 2013-12-30 18:23:50 +00:00
Damien dae7eb7226 py: add dict length function, and fix rt_store_set. 2013-12-29 22:32:51 +00:00
Damien b86e3f9293 py: implement some basic exception matching. 2013-12-29 17:17:43 +00:00
Damien d9d6201b52 py: simplify __next__ method for generators. 2013-12-21 18:38:03 +00:00
Damien d99b05282d Change object representation from 1 big union to individual structs.
A big change.  Micro Python objects are allocated as individual structs
with the first element being a pointer to the type information (which
is itself an object).  This scheme follows CPython.  Much more flexible,
not necessarily slower, uses same heap memory, and can allocate objects
statically.

Also change name prefix, from py_ to mp_ (mp for Micro Python).
2013-12-21 18:17:45 +00:00
Damien a3dcd9e80c py: add more Python built-in functions. 2013-12-17 21:35:38 +00:00
Damien 660365e14c py: split runtime into map, obj, builtin. 2013-12-17 18:27:24 +00:00
Damien 9ecbcfff99 py: work towards working closures. 2013-12-11 00:41:43 +00:00
Damien db4c361f1c py: add skeletal import functionality. 2013-12-10 17:27:24 +00:00
Damien 033d17a633 py: built-in range now accepts variable number of arguments. 2013-11-28 19:22:56 +00:00
Damien 86c7fc7dd9 py: add list pop and sort, unpack_sequence, and keywords in method_call. 2013-11-26 15:16:41 +00:00
Damien d47f9d5a4f py: add call to __init__ when instantiating class object. 2013-11-25 23:40:02 +00:00
Damien f03001f8a6 Add function to decode and show byte code. 2013-11-17 13:19:33 +00:00
Damien 0446a0d76d Change some debugging/output messages for native code generation. 2013-11-17 13:16:36 +00:00
Damien 6b92d451b2 Add len and subscr (a hack) support for strings. 2013-11-09 20:13:55 +00:00
Damien 5bf32c3b6b Built-in print function supports variable number of arguments. 2013-11-06 17:16:35 +00:00
Damien 40fdfe3000 Improve allocation of stack for byte code. 2013-11-05 22:16:22 +00:00
Damien 03c9cfb015 Make byte code jumps relative. 2013-11-05 22:06:08 +00:00
Damien 96a0addb18 Fixup include of stdarg and va_list definition. 2013-11-03 18:30:10 +00:00
Damien 2f06c57f8a Add simple var-arg functions; add simple string.format. 2013-11-03 18:20:56 +00:00