From e0e54ba3a5e3242f49da607b150669fcad0f2f6e Mon Sep 17 00:00:00 2001 From: "Michael D. Lowis" Date: Wed, 1 Oct 2014 14:06:35 -0400 Subject: [PATCH] Fixed tree-building algorithm and updated clang toolchain settings --- Rakefile | 5 +++-- build-system | 2 +- modules/libcds | 2 +- source/sclpl/grammar.c | 3 ++- source/sclpl/grammar.h | 2 +- source/sclpl/lexer.c | 3 +-- source/sclpl/lexer.h | 2 ++ source/sclpl/main.c | 42 ++++++++++++++++++------------------------ source/sclpl/parser.c | 13 +++++++++---- source/sclpl/parser.h | 2 ++ 10 files changed, 40 insertions(+), 36 deletions(-) diff --git a/Rakefile b/Rakefile index 89c7d61..01a8efa 100644 --- a/Rakefile +++ b/Rakefile @@ -10,6 +10,7 @@ end # Define the compiler environment base_env = BuildEnv.new(echo: :command) do |env| env.build_dir('source','build/obj/source') + env.build_dir('modules','build/obj/modules') env.set_toolset(:clang) env["CFLAGS"] += ['-DLEAK_DETECT_LEVEL=1', '--std=c99', '-Wall', '-Wextra'] #, '-Werror'] env["CPPPATH"] += ['modules/libopts/source'] + Dir['modules/libcds/source/**/'] @@ -35,7 +36,7 @@ end file "#{CLANG_BIN_DIR}/#{CLANG_BIN_NAME}" => ["#{CLANG_BUILD_DIR}/Makefile"] + FileList["#{CLANG_SRC_DIR}/tools/clang/**/*.c"] do FileUtils.cd(CLANG_BUILD_DIR) do - sh "#{CLANG_MAKE_CMD} clang" + sh "#{CLANG_MAKE_CMD} clang llvm-ar" end end @@ -79,6 +80,6 @@ task :clean_all => [:clean] do end end -desc "Clobber the build driectory and all it's contents" +desc "Clobber the build directory and all it's contents" task(:clobber) { FileUtils.rm_rf('build/') } diff --git a/build-system b/build-system index 81f2a99..02c33d6 160000 --- a/build-system +++ b/build-system @@ -1 +1 @@ -Subproject commit 81f2a99521abae709ccdeea0640f382908d20b64 +Subproject commit 02c33d624a63c7d5fa34f116b2209c816d4564da diff --git a/modules/libcds b/modules/libcds index a70643a..20a2ea4 160000 --- a/modules/libcds +++ b/modules/libcds @@ -1 +1 @@ -Subproject commit a70643a30382932f3eda4d2a5159e81664731bea +Subproject commit 20a2ea4fb0e0154f41032c945b25f66069a45a37 diff --git a/source/sclpl/grammar.c b/source/sclpl/grammar.c index c2a0a79..9ab6523 100644 --- a/source/sclpl/grammar.c +++ b/source/sclpl/grammar.c @@ -6,7 +6,7 @@ */ #include "grammar.h" -void grammar_toplevel(parser_t* p_parser) +tree_t* grammar_toplevel(parser_t* p_parser) { if (parser_accept_str(p_parser, T_VAR, "import")) grammar_import(p_parser); @@ -16,6 +16,7 @@ void grammar_toplevel(parser_t* p_parser) grammar_expression(p_parser); else parser_error(p_parser, "Unrecognized top-level form"); + return parser_get_tree(p_parser); } void grammar_import(parser_t* p_parser) diff --git a/source/sclpl/grammar.h b/source/sclpl/grammar.h index 3ac5088..9c9749b 100644 --- a/source/sclpl/grammar.h +++ b/source/sclpl/grammar.h @@ -7,7 +7,7 @@ #include "parser.h" -void grammar_toplevel(parser_t* p_parser); +tree_t* grammar_toplevel(parser_t* p_parser); void grammar_import(parser_t* p_parser); diff --git a/source/sclpl/lexer.c b/source/sclpl/lexer.c index 37cb1fb..b0bafe5 100644 --- a/source/sclpl/lexer.c +++ b/source/sclpl/lexer.c @@ -19,7 +19,6 @@ static lex_tok_t* lexer_integer(char* text, int base); static lex_tok_t* lexer_float(char* text); static lex_tok_t* lexer_bool(char* text); static lex_tok_t* lexer_var(char* text); -static char* lexer_tok_type_str(lex_tok_t* p_tok); static bool lexer_oneof(const char* class, char c); static bool is_float(char* text); static char* lexer_dup(const char* p_old); @@ -164,7 +163,7 @@ static lex_tok_t* lexer_var(char* text) return lex_tok_new(T_VAR, lexer_dup(text)); } -static char* lexer_tok_type_str(lex_tok_t* p_tok) { +char* lexer_tok_type_str(lex_tok_t* p_tok) { switch(p_tok->type) { case T_END: return "T_END"; case T_STRING: return "T_STRING"; diff --git a/source/sclpl/lexer.h b/source/sclpl/lexer.h index 5c82acf..e0536b8 100644 --- a/source/sclpl/lexer.h +++ b/source/sclpl/lexer.h @@ -33,4 +33,6 @@ lexer_t* lexer_new(char* p_prompt, FILE* p_input); lex_tok_t* lexer_read(lexer_t* p_lexer); +char* lexer_tok_type_str(lex_tok_t* p_tok); + #endif /* LEXER_H */ diff --git a/source/sclpl/main.c b/source/sclpl/main.c index 44a681f..2aebbb3 100644 --- a/source/sclpl/main.c +++ b/source/sclpl/main.c @@ -2,6 +2,7 @@ #include #include "opts.h" #include "grammar.h" +#include "lexer.h" /* Command Line Options *****************************************************************************/ @@ -14,32 +15,25 @@ OptionConfig_T Options_Config[] = { /* SCLPL Parser *****************************************************************************/ -void print_subtree(tree_t* p_tree, int depth); - -void print_tree(vec_t* p_vec, int depth); +void print_indent(int depth) { + for(int i = 0; i < (2 * depth); i++) + printf("%c", ' '); +} -void print_subtree(tree_t* p_tree, int depth) { - puts("printing subtree"); - for(int i = 0; i < (4 * depth); i++) printf("%c", ' '); +void print_tree(tree_t* p_tree, int depth) { + print_indent(depth); if (p_tree->tag == ATOM) { - printf("ATOM (%p)\n", p_tree); + lex_tok_t* p_tok = p_tree->ptr.tok; + printf("\n", lexer_tok_type_str(p_tok)); } else { - printf("TREE (%p)\n", p_tree); - print_tree(p_tree->ptr.vec, depth+1); - } - puts("printed subtree"); -} - -void print_tree(vec_t* p_vec, int depth) { - puts("printing tree"); - for(size_t idx = 0; idx < vec_size(p_vec); idx++) { - puts("("); - tree_t* p_tree = (tree_t*)vec_at(p_vec, idx); - puts("-"); - print_subtree(p_tree, depth); + puts("(tree:"); + vec_t* p_vec = p_tree->ptr.vec; + for(size_t idx = 0; idx < vec_size(p_vec); idx++) { + print_tree((tree_t*)vec_at(p_vec, idx), depth+1); + } + print_indent(depth); puts(")"); } - puts("printed tree"); } /* TODO: @@ -55,9 +49,9 @@ int main(int argc, char **argv) { parser_t* p_parser = parser_new(":> ", stdin); while(!parser_eof(p_parser)) { - grammar_toplevel(p_parser); - print_tree(p_parser->p_tok_buf, 0); - vec_clear(p_parser->p_tok_buf); + tree_t* p_tree = grammar_toplevel(p_parser); + print_tree(p_tree, 0); + mem_release(p_tree); puts("OK."); } mem_release(p_parser); diff --git a/source/sclpl/parser.c b/source/sclpl/parser.c index c928de8..9482459 100644 --- a/source/sclpl/parser.c +++ b/source/sclpl/parser.c @@ -120,11 +120,16 @@ void parser_reduce(parser_t* p_parser, size_t mark) vec_t* p_buf = p_parser->p_tok_buf; vec_t* p_form = vec_new(0); for(size_t idx = mark; idx < vec_size(p_buf); idx++) { - tree_tag_t tag = ((tree_t*)vec_at(p_buf, idx))->tag; - tree_t* p_tree = parser_tree_new(tag, mem_retain(vec_at(p_buf, idx))); + tree_t* p_tree = mem_retain(vec_at(p_buf, idx)); vec_push_back(p_form, p_tree); } - vec_erase(p_parser->p_tok_buf, mark, vec_size(p_parser->p_tok_buf)-1); - vec_push_back(p_parser->p_tok_buf, parser_tree_new(TREE, p_form)); + vec_erase(p_buf, mark, vec_size(p_buf)-1); + vec_push_back(p_buf, parser_tree_new(TREE, p_form)); +} + +tree_t* parser_get_tree(parser_t* p_parser) { + tree_t* p_tree = parser_tree_new(TREE, p_parser->p_tok_buf); + p_parser->p_tok_buf = vec_new(0); + return p_tree; } diff --git a/source/sclpl/parser.h b/source/sclpl/parser.h index 5733542..0e62b1a 100644 --- a/source/sclpl/parser.h +++ b/source/sclpl/parser.h @@ -48,4 +48,6 @@ size_t parser_mark(parser_t* p_parser); void parser_reduce(parser_t* p_parser, size_t mark); +tree_t* parser_get_tree(parser_t* p_parser); + #endif /* PARSER_H */ -- 2.52.0