diff options
author | Damien George <damien.p.george@gmail.com> | 2014-01-07 14:54:15 +0000 |
---|---|---|
committer | Damien George <damien.p.george@gmail.com> | 2014-01-07 14:54:15 +0000 |
commit | 136f67523b10d24dd65cb25e49e07a7e4e5341a1 (patch) | |
tree | 95a8e704f85c3813735d287afccc74a6df172f29 /unix | |
parent | 880ce2d7fabc127b4bca7b6f2ea8b82d0977045f (diff) | |
download | micropython-136f67523b10d24dd65cb25e49e07a7e4e5341a1.tar.gz micropython-136f67523b10d24dd65cb25e49e07a7e4e5341a1.zip |
Factor and simplify Makefile's and mpconfig.
Diffstat (limited to 'unix')
-rw-r--r-- | unix/Makefile | 99 | ||||
-rw-r--r-- | unix/main.c | 149 | ||||
-rw-r--r-- | unix/mpconfigport.h | 5 |
3 files changed, 93 insertions, 160 deletions
diff --git a/unix/Makefile b/unix/Makefile index 984f1c3bf3..a3faea290a 100644 --- a/unix/Makefile +++ b/unix/Makefile @@ -1,106 +1,39 @@ -PYSRC=../py -BUILD=build +# define main target +PROG = py +all: $(PROG) + +# include py core make definitions +include ../py/py.mk +# program for deletion +RM = /bin/rm + +# compiler settings CC = gcc -CFLAGS = -I. -I$(PYSRC) -Wall -Werror -ansi -std=gnu99 -Os #-DNDEBUG +CFLAGS = -I. -I$(PY_SRC) -Wall -Werror -ansi -std=gnu99 -Os #-DNDEBUG LDFLAGS = -lm +# source files SRC_C = \ main.c \ -PY_O = \ - nlrx86.o \ - nlrx64.o \ - nlrthumb.o \ - malloc.o \ - qstr.o \ - vstr.o \ - unicode.o \ - lexer.o \ - lexerunix.o \ - parse.o \ - scope.o \ - compile.o \ - emitcommon.o \ - emitpass1.o \ - emitcpy.o \ - emitbc.o \ - asmx64.o \ - emitnx64.o \ - asmthumb.o \ - emitnthumb.o \ - emitinlinethumb.o \ - runtime.o \ - map.o \ - obj.o \ - objbool.o \ - objboundmeth.o \ - objcell.o \ - objclass.o \ - objclosure.o \ - objcomplex.o \ - objdict.o \ - objexcept.o \ - objfloat.o \ - objfun.o \ - objgenerator.o \ - objinstance.o \ - objint.o \ - objlist.o \ - objmodule.o \ - objnone.o \ - objrange.o \ - objset.o \ - objslice.o \ - objstr.o \ - objtuple.o \ - objtype.o \ - builtin.o \ - builtinimport.o \ - vm.o \ - showbc.o \ - repl.o \ - -OBJ = $(addprefix $(BUILD)/, $(SRC_C:.c=.o) $(PY_O)) +OBJ = $(addprefix $(BUILD)/, $(SRC_C:.c=.o)) $(PY_O) LIB = -lreadline # the following is needed for BSD #LIB += -ltermcap -PROG = py $(PROG): $(BUILD) $(OBJ) $(CC) -o $@ $(OBJ) $(LIB) $(LDFLAGS) strip $(PROG) size $(PROG) -$(BUILD): - mkdir -p $@ - $(BUILD)/%.o: %.c $(CC) $(CFLAGS) -c -o $@ $< -$(BUILD)/%.o: $(PYSRC)/%.S - $(CC) $(CFLAGS) -c -o $@ $< - -$(BUILD)/%.o: $(PYSRC)/%.c mpconfigport.h - $(CC) $(CFLAGS) -c -o $@ $< - -$(BUILD)/emitnx64.o: $(PYSRC)/emitnative.c $(PYSRC)/emit.h mpconfigport.h - $(CC) $(CFLAGS) -DN_X64 -c -o $@ $< - -$(BUILD)/emitnthumb.o: $(PYSRC)/emitnative.c $(PYSRC)/emit.h mpconfigport.h - $(CC) $(CFLAGS) -DN_THUMB -c -o $@ $< - -# optimising vm for speed, adds only a small amount to code size but makes a huge difference to speed (20% faster) -$(BUILD)/vm.o: $(PYSRC)/vm.c - $(CC) $(CFLAGS) -O3 -c -o $@ $< - $(BUILD)/main.o: mpconfigport.h -$(BUILD)/parse.o: $(PYSRC)/grammar.h -$(BUILD)/compile.o: $(PYSRC)/grammar.h -$(BUILD)/emitcpy.o: $(PYSRC)/emit.h -$(BUILD)/emitbc.o: $(PYSRC)/emit.h clean: - /bin/rm -rf $(BUILD) + $(RM) -f $(PROG) + $(RM) -rf $(BUILD) -.PHONY: clean +.PHONY: all clean diff --git a/unix/main.c b/unix/main.c index a06dc36791..f7277b960c 100644 --- a/unix/main.c +++ b/unix/main.c @@ -20,6 +20,52 @@ #include <readline/history.h> #endif +static void execute_from_lexer(mp_lexer_t *lex, mp_parse_input_kind_t input_kind, bool is_repl) { + if (lex == NULL) { + return; + } + + if (0) { + // just tokenise + while (!mp_lexer_is_kind(lex, MP_TOKEN_END)) { + mp_token_show(mp_lexer_cur(lex)); + mp_lexer_to_next(lex); + } + mp_lexer_free(lex); + return; + } + + mp_parse_node_t pn = mp_parse(lex, input_kind); + mp_lexer_free(lex); + + if (pn == MP_PARSE_NODE_NULL) { + // parse error + return; + } + + //printf("----------------\n"); + //parse_node_show(pn, 0); + //printf("----------------\n"); + + mp_obj_t module_fun = mp_compile(pn, is_repl); + + if (module_fun == mp_const_none) { + // compile error + return; + } + + // execute it + nlr_buf_t nlr; + if (nlr_push(&nlr) == 0) { + rt_call_function_0(module_fun); + nlr_pop(); + } else { + // uncaught exception + mp_obj_print((mp_obj_t)nlr.ret_val); + printf("\n"); + } +} + static char *str_join(const char *s1, int sep_char, const char *s2) { int l1 = strlen(s1); int l2 = strlen(s2); @@ -80,28 +126,11 @@ static void do_repl(void) { } mp_lexer_t *lex = mp_lexer_new_from_str_len("<stdin>", line, strlen(line), false); - mp_parse_node_t pn = mp_parse(lex, MP_PARSE_SINGLE_INPUT); - mp_lexer_free(lex); - - if (pn != MP_PARSE_NODE_NULL) { - //mp_parse_node_show(pn, 0); - mp_obj_t module_fun = mp_compile(pn, true); - if (module_fun != mp_const_none) { - nlr_buf_t nlr; - if (nlr_push(&nlr) == 0) { - rt_call_function_0(module_fun); - nlr_pop(); - } else { - // uncaught exception - mp_obj_print((mp_obj_t)nlr.ret_val); - printf("\n"); - } - } - } + execute_from_lexer(lex, MP_PARSE_SINGLE_INPUT, true); } } -void do_file(const char *file) { +static void do_file(const char *file) { // hack: set dir for import based on where this file is { const char * s = strrchr(file, '/'); @@ -115,53 +144,12 @@ void do_file(const char *file) { } mp_lexer_t *lex = mp_lexer_new_from_file(file); - //const char *pysrc = "def f():\n x=x+1\n print(42)\n"; - //mp_lexer_t *lex = mp_lexer_from_str_len("<>", pysrc, strlen(pysrc), false); - if (lex == NULL) { - return; - } - - if (0) { - // just tokenise - while (!mp_lexer_is_kind(lex, MP_TOKEN_END)) { - mp_token_show(mp_lexer_cur(lex)); - mp_lexer_to_next(lex); - } - mp_lexer_free(lex); - - } else { - // compile - - mp_parse_node_t pn = mp_parse(lex, MP_PARSE_FILE_INPUT); - mp_lexer_free(lex); - - if (pn != MP_PARSE_NODE_NULL) { - //printf("----------------\n"); - //parse_node_show(pn, 0); - //printf("----------------\n"); - mp_obj_t module_fun = mp_compile(pn, false); - //printf("----------------\n"); + execute_from_lexer(lex, MP_PARSE_FILE_INPUT, false); +} -#if MICROPY_EMIT_CPYTHON - if (!comp_ok) { - printf("compile error\n"); - } -#else - if (1 && module_fun != mp_const_none) { - // execute it - nlr_buf_t nlr; - if (nlr_push(&nlr) == 0) { - rt_call_function_0(module_fun); - nlr_pop(); - } else { - // uncaught exception - mp_obj_print((mp_obj_t)nlr.ret_val); - printf("\n"); - } - } -#endif - } - } +static void do_str(const char *str) { + mp_lexer_t *lex = mp_lexer_new_from_str_len("<stdin>", str, strlen(str), false); + execute_from_lexer(lex, MP_PARSE_SINGLE_INPUT, false); } typedef struct _test_obj_t { @@ -192,12 +180,6 @@ static const mp_obj_type_t test_type = { { &mp_const_type }, "Test", .print = test_print, - .make_new = NULL, - .call_n = NULL, - .unary_op = NULL, - .binary_op = NULL, - .getiter = NULL, - .iternext = NULL, .methods = { { "get", &test_get_obj }, { "set", &test_set_obj }, @@ -212,6 +194,11 @@ mp_obj_t test_obj_new(int value) { return o; } +int usage(void) { + printf("usage: py [-c <command>] [<filename>]\n"); + return 1; +} + int main(int argc, char **argv) { qstr_init(); rt_init(); @@ -227,12 +214,24 @@ int main(int argc, char **argv) { if (argc == 1) { do_repl(); - } else if (argc == 2) { - do_file(argv[1]); } else { - printf("usage: py [<file>]\n"); - return 1; + for (int a = 1; a < argc; a++) { + if (argv[a][0] == '-') { + if (strcmp(argv[a], "-c") == 0) { + if (a + 1 >= argc) { + return usage(); + } + do_str(argv[a + 1]); + a += 1; + } else { + return usage(); + } + } else { + do_file(argv[a]); + } + } } + rt_deinit(); //printf("total bytes = %d\n", m_get_total_bytes_allocated()); diff --git a/unix/mpconfigport.h b/unix/mpconfigport.h index 7a4622b8b6..b7901069ac 100644 --- a/unix/mpconfigport.h +++ b/unix/mpconfigport.h @@ -5,11 +5,12 @@ #define MICROPY_USE_READLINE (1) #endif -#define MICROPY_ENABLE_FLOAT (1) -#define MICROPY_EMIT_CPYTHON (0) #define MICROPY_EMIT_X64 (1) #define MICROPY_EMIT_THUMB (0) #define MICROPY_EMIT_INLINE_THUMB (0) +#define MICROPY_MEM_STATS (1) +#define MICROPY_ENABLE_REPL_HELPERS (1) +#define MICROPY_ENABLE_FLOAT (1) // type definitions for the specific machine |