Colin Hogben / micropython

Dependents:   micropython-repl

Committer:
Colin Hogben
Date:
Wed Apr 27 22:11:29 2016 +0100
Revision:
10:33521d742af1
Parent:
2:c89e95946844
Update README and version

Who changed what in which revision?

UserRevisionLine numberNew contents of line
pythontech 0:5868e8752d44 1 /*
pythontech 0:5868e8752d44 2 * This file is part of the Micro Python project, http://micropython.org/
pythontech 0:5868e8752d44 3 *
pythontech 0:5868e8752d44 4 * The MIT License (MIT)
pythontech 0:5868e8752d44 5 *
pythontech 0:5868e8752d44 6 * Copyright (c) 2015 Paul Sokolovsky
Colin Hogben 2:c89e95946844 7 * Copyright (c) 2016 Damien P. George
pythontech 0:5868e8752d44 8 *
pythontech 0:5868e8752d44 9 * Permission is hereby granted, free of charge, to any person obtaining a copy
pythontech 0:5868e8752d44 10 * of this software and associated documentation files (the "Software"), to deal
pythontech 0:5868e8752d44 11 * in the Software without restriction, including without limitation the rights
pythontech 0:5868e8752d44 12 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
pythontech 0:5868e8752d44 13 * copies of the Software, and to permit persons to whom the Software is
pythontech 0:5868e8752d44 14 * furnished to do so, subject to the following conditions:
pythontech 0:5868e8752d44 15 *
pythontech 0:5868e8752d44 16 * The above copyright notice and this permission notice shall be included in
pythontech 0:5868e8752d44 17 * all copies or substantial portions of the Software.
pythontech 0:5868e8752d44 18 *
pythontech 0:5868e8752d44 19 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
pythontech 0:5868e8752d44 20 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
pythontech 0:5868e8752d44 21 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
pythontech 0:5868e8752d44 22 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
pythontech 0:5868e8752d44 23 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
pythontech 0:5868e8752d44 24 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
pythontech 0:5868e8752d44 25 * THE SOFTWARE.
pythontech 0:5868e8752d44 26 */
pythontech 0:5868e8752d44 27
pythontech 0:5868e8752d44 28 #include <string.h>
pythontech 0:5868e8752d44 29 #include <stdint.h>
pythontech 0:5868e8752d44 30
pythontech 0:5868e8752d44 31 #include "py/lexer.h"
pythontech 0:5868e8752d44 32 #include "py/frozenmod.h"
pythontech 0:5868e8752d44 33
Colin Hogben 2:c89e95946844 34 #if MICROPY_MODULE_FROZEN_STR
pythontech 0:5868e8752d44 35
pythontech 0:5868e8752d44 36 #ifndef MICROPY_MODULE_FROZEN_LEXER
pythontech 0:5868e8752d44 37 #define MICROPY_MODULE_FROZEN_LEXER mp_lexer_new_from_str_len
pythontech 0:5868e8752d44 38 #else
pythontech 0:5868e8752d44 39 mp_lexer_t *MICROPY_MODULE_FROZEN_LEXER(qstr src_name, const char *str, mp_uint_t len, mp_uint_t free_len);
pythontech 0:5868e8752d44 40 #endif
pythontech 0:5868e8752d44 41
Colin Hogben 2:c89e95946844 42 extern const char mp_frozen_str_names[];
Colin Hogben 2:c89e95946844 43 extern const uint32_t mp_frozen_str_sizes[];
Colin Hogben 2:c89e95946844 44 extern const char mp_frozen_str_content[];
pythontech 0:5868e8752d44 45
Colin Hogben 2:c89e95946844 46 STATIC mp_lexer_t *mp_find_frozen_str(const char *str, size_t len) {
Colin Hogben 2:c89e95946844 47 const char *name = mp_frozen_str_names;
pythontech 0:5868e8752d44 48
pythontech 0:5868e8752d44 49 size_t offset = 0;
pythontech 0:5868e8752d44 50 for (int i = 0; *name != 0; i++) {
Colin Hogben 2:c89e95946844 51 size_t l = strlen(name);
pythontech 0:5868e8752d44 52 if (l == len && !memcmp(str, name, l)) {
Colin Hogben 2:c89e95946844 53 mp_lexer_t *lex = MICROPY_MODULE_FROZEN_LEXER(MP_QSTR_, mp_frozen_str_content + offset, mp_frozen_str_sizes[i], 0);
pythontech 0:5868e8752d44 54 return lex;
pythontech 0:5868e8752d44 55 }
pythontech 0:5868e8752d44 56 name += l + 1;
Colin Hogben 2:c89e95946844 57 offset += mp_frozen_str_sizes[i] + 1;
pythontech 0:5868e8752d44 58 }
pythontech 0:5868e8752d44 59 return NULL;
pythontech 0:5868e8752d44 60 }
pythontech 0:5868e8752d44 61
Colin Hogben 2:c89e95946844 62 #endif
Colin Hogben 2:c89e95946844 63
Colin Hogben 2:c89e95946844 64 #if MICROPY_MODULE_FROZEN_MPY
Colin Hogben 2:c89e95946844 65
Colin Hogben 2:c89e95946844 66 #include "py/emitglue.h"
Colin Hogben 2:c89e95946844 67
Colin Hogben 2:c89e95946844 68 extern const char mp_frozen_mpy_names[];
Colin Hogben 2:c89e95946844 69 extern const mp_raw_code_t *const mp_frozen_mpy_content[];
Colin Hogben 2:c89e95946844 70
Colin Hogben 2:c89e95946844 71 STATIC const mp_raw_code_t *mp_find_frozen_mpy(const char *str, size_t len) {
Colin Hogben 2:c89e95946844 72 const char *name = mp_frozen_mpy_names;
Colin Hogben 2:c89e95946844 73 for (size_t i = 0; *name != 0; i++) {
Colin Hogben 2:c89e95946844 74 size_t l = strlen(name);
Colin Hogben 2:c89e95946844 75 if (l == len && !memcmp(str, name, l)) {
Colin Hogben 2:c89e95946844 76 return mp_frozen_mpy_content[i];
Colin Hogben 2:c89e95946844 77 }
Colin Hogben 2:c89e95946844 78 name += l + 1;
Colin Hogben 2:c89e95946844 79 }
Colin Hogben 2:c89e95946844 80 return NULL;
Colin Hogben 2:c89e95946844 81 }
Colin Hogben 2:c89e95946844 82
Colin Hogben 2:c89e95946844 83 #endif
Colin Hogben 2:c89e95946844 84
Colin Hogben 2:c89e95946844 85 #if MICROPY_MODULE_FROZEN
Colin Hogben 2:c89e95946844 86
Colin Hogben 2:c89e95946844 87 int mp_find_frozen_module(const char *str, size_t len, void **data) {
Colin Hogben 2:c89e95946844 88 #if MICROPY_MODULE_FROZEN_STR
Colin Hogben 2:c89e95946844 89 mp_lexer_t *lex = mp_find_frozen_str(str, len);
Colin Hogben 2:c89e95946844 90 if (lex != NULL) {
Colin Hogben 2:c89e95946844 91 *data = lex;
Colin Hogben 2:c89e95946844 92 return MP_FROZEN_STR;
Colin Hogben 2:c89e95946844 93 }
Colin Hogben 2:c89e95946844 94 #endif
Colin Hogben 2:c89e95946844 95 #if MICROPY_MODULE_FROZEN_MPY
Colin Hogben 2:c89e95946844 96 const mp_raw_code_t *rc = mp_find_frozen_mpy(str, len);
Colin Hogben 2:c89e95946844 97 if (rc != NULL) {
Colin Hogben 2:c89e95946844 98 *data = (void*)rc;
Colin Hogben 2:c89e95946844 99 return MP_FROZEN_MPY;
Colin Hogben 2:c89e95946844 100 }
Colin Hogben 2:c89e95946844 101 #endif
Colin Hogben 2:c89e95946844 102 return MP_FROZEN_NONE;
Colin Hogben 2:c89e95946844 103 }
Colin Hogben 2:c89e95946844 104
Colin Hogben 2:c89e95946844 105 #endif