From 5c4f807b6e3c90b7a76ea6369e61c9cd91842802 Mon Sep 17 00:00:00 2001 From: "Mike D. Lowis" Date: Fri, 2 Oct 2015 14:21:37 -0400 Subject: [PATCH] Refactored libparse to use a single header and removed unused tree_walker types --- source/libparse/grammar.c | 4 +- source/libparse/grammar.h | 24 --------- source/libparse/lexer.c | 6 +-- source/libparse/lexer.h | 46 ---------------- source/libparse/libparse.h | 107 +++++++++++++++++++++++++++++++++++++ source/libparse/parser.c | 3 +- source/libparse/parser.h | 51 ------------------ source/libparse/tree.c | 27 +--------- source/libparse/tree.h | 51 ------------------ source/sclpl/codegen.c | 2 +- source/sclpl/main.c | 6 +-- source/sclpl/ops.c | 4 +- source/sclpl/pprint.h | 3 +- 13 files changed, 116 insertions(+), 218 deletions(-) delete mode 100644 source/libparse/grammar.h delete mode 100644 source/libparse/lexer.h create mode 100644 source/libparse/libparse.h delete mode 100644 source/libparse/parser.h delete mode 100644 source/libparse/tree.h diff --git a/source/libparse/grammar.c b/source/libparse/grammar.c index d12cf7a..4bfcc0b 100644 --- a/source/libparse/grammar.c +++ b/source/libparse/grammar.c @@ -4,9 +4,7 @@ $Revision$ $HeadURL$ */ -#include "grammar.h" -#include "lexer.h" -#include "exn.h" +#include AST* grammar_toplevel(Parser* p) { diff --git a/source/libparse/grammar.h b/source/libparse/grammar.h deleted file mode 100644 index 962f456..0000000 --- a/source/libparse/grammar.h +++ /dev/null @@ -1,24 +0,0 @@ -/** - @file grammar.h - @brief Describes the grammar and parsing rules that form the SCLPL language. -*/ -#ifndef GRAMMAR_H -#define GRAMMAR_H - -#include "parser.h" - -AST* grammar_toplevel(Parser* p); -void grammar_require(Parser* p); -void grammar_type_annotation(Parser* p); -void grammar_type_definition(Parser* p); -void grammar_type(Parser* p); -void grammar_tuple(Parser* p); -void grammar_function(Parser* p); -void grammar_definition(Parser* p); -void grammar_expression(Parser* p); -void grammar_literal(Parser* p); -void grammar_arglist(Parser* p); -void grammar_if_stmnt(Parser* p); -void grammar_fn_stmnt(Parser* p); - -#endif /* GRAMMAR_H */ diff --git a/source/libparse/lexer.c b/source/libparse/lexer.c index 75879f3..3a9b6fd 100644 --- a/source/libparse/lexer.c +++ b/source/libparse/lexer.c @@ -4,11 +4,7 @@ $Revision$ $HeadURL$ */ -#include "lexer.h" -#include "mem.h" -#include -#include -#include +#include static char* read(Lexer* ctx, size_t* line, size_t* col); static bool eof(Lexer* ctx); diff --git a/source/libparse/lexer.h b/source/libparse/lexer.h deleted file mode 100644 index c80579b..0000000 --- a/source/libparse/lexer.h +++ /dev/null @@ -1,46 +0,0 @@ -/** - @file lexer.h - @brief TODO: Describe this file - $Revision$ - $HeadURL$ - */ -#ifndef LEXER_H -#define LEXER_H - -#include -#include -#include -#include - -typedef struct { - char* p_line; - size_t index; - size_t lineno; - FILE* p_input; - char* p_prompt; -} Lexer; - -typedef enum { - T_ID, T_CHAR, T_INT, T_FLOAT, T_BOOL, T_STRING, T_LBRACE, T_RBRACE, T_LBRACK, - T_RBRACK, T_LPAR, T_RPAR, T_COMMA, T_SQUOTE, T_DQUOTE, T_END, T_END_FILE -} TokenType; - -typedef struct { - TokenType type; - const char* file; - size_t line; - size_t col; - void* value; -} Token; - -Lexer* lexer_new(char* p_prompt, FILE* p_input); - -Token* lex_tok_new(TokenType type, void* val); - -Token* lexer_read(Lexer* p_lexer); - -void lexer_skipline(Lexer* p_lexer); - -char* lexer_dup(const char* p_old); - -#endif /* LEXER_H */ diff --git a/source/libparse/libparse.h b/source/libparse/libparse.h new file mode 100644 index 0000000..7355c00 --- /dev/null +++ b/source/libparse/libparse.h @@ -0,0 +1,107 @@ +/** + @file libparse.h + @brief TODO: Describe this file + $Revision$ + $HeadURL$ + */ +#ifndef LIBPARSE_H +#define LIBPARSE_H + +#include +#include +#include +#include +#include +#include "mem.h" +#include "vec.h" +#include "exn.h" + +typedef struct { + char* p_line; + size_t index; + size_t lineno; + FILE* p_input; + char* p_prompt; +} Lexer; + +typedef enum { + T_ID, T_CHAR, T_INT, T_FLOAT, T_BOOL, T_STRING, T_LBRACE, T_RBRACE, T_LBRACK, + T_RBRACK, T_LPAR, T_RPAR, T_COMMA, T_SQUOTE, T_DQUOTE, T_END, T_END_FILE +} TokenType; + +typedef struct { + TokenType type; + const char* file; + size_t line; + size_t col; + void* value; +} Token; + +DECLARE_EXCEPTION(ParseException); + +typedef struct { + Lexer* p_lexer; + Token* p_tok; + vec_t* p_tok_buf; +} Parser; + +typedef enum { + ATOM, + TREE +} ASTTag; + +typedef struct { + ASTTag tag; + union { + Token* tok; + vec_t* vec; + } ptr; +} AST; + +// Lexer routines +Lexer* lexer_new(char* p_prompt, FILE* p_input); +Token* lex_tok_new(TokenType type, void* val); +Token* lexer_read(Lexer* p_lexer); +void lexer_skipline(Lexer* p_lexer); +char* lexer_dup(const char* p_old); + +// Parser routines +Parser* parser_new(char* p_prompt, FILE* input); +void fetch(Parser* p_parser); +Token* peek(Parser* p_parser); +bool parser_eof(Parser* p_parser); +void parser_resume(Parser* p_parser); +void error(Parser* p_parser, const char* p_text); +bool accept(Parser* p_parser, TokenType type); +bool accept_str(Parser* p_parser, TokenType type, const char* p_text); +bool expect(Parser* p_parser, TokenType type); +bool expect_str(Parser* p_parser, TokenType type, const char* p_text); +size_t mark(Parser* p_parser); +void reduce(Parser* p_parser, size_t mark); +AST* get_tree(Parser* p_parser); +void insert(Parser* p_parser, TokenType type, void* value); + +// AST Routines +AST* tree_convert(AST* p_tree); +AST* tree_new(ASTTag tag, void* p_obj); +AST* tree_get_child(AST* p_tree, size_t idx); +void* tree_get_val(AST* p_tree); +void* tree_get_child_val(AST* p_tree, size_t idx); +bool tree_is_formtype(AST* p_tree, const char* val); + +// Grammar Routines +AST* grammar_toplevel(Parser* p); +void grammar_require(Parser* p); +void grammar_type_annotation(Parser* p); +void grammar_type_definition(Parser* p); +void grammar_type(Parser* p); +void grammar_tuple(Parser* p); +void grammar_function(Parser* p); +void grammar_definition(Parser* p); +void grammar_expression(Parser* p); +void grammar_literal(Parser* p); +void grammar_arglist(Parser* p); +void grammar_if_stmnt(Parser* p); +void grammar_fn_stmnt(Parser* p); + +#endif /* LIBPARSE_H */ diff --git a/source/libparse/parser.c b/source/libparse/parser.c index 44b4575..674f2d7 100644 --- a/source/libparse/parser.c +++ b/source/libparse/parser.c @@ -4,8 +4,7 @@ $Revision$ $HeadURL$ */ -#include "parser.h" -#include "vec.h" +#include DEFINE_EXCEPTION(ParseException, &RuntimeException); diff --git a/source/libparse/parser.h b/source/libparse/parser.h deleted file mode 100644 index 3e447ad..0000000 --- a/source/libparse/parser.h +++ /dev/null @@ -1,51 +0,0 @@ -/** - @file parser.h - @brief A collection of helper functions for implementing recursive descent parsers. - $Revision$ - $HeadURL$ -*/ -#ifndef PARSER_H -#define PARSER_H - -#include "lexer.h" -#include "vec.h" -#include "exn.h" -#include "tree.h" - -DECLARE_EXCEPTION(ParseException); - -typedef struct { - Lexer* p_lexer; - Token* p_tok; - vec_t* p_tok_buf; -} Parser; - -Parser* parser_new(char* p_prompt, FILE* input); - -void fetch(Parser* p_parser); - -Token* peek(Parser* p_parser); - -bool parser_eof(Parser* p_parser); - -void parser_resume(Parser* p_parser); - -void error(Parser* p_parser, const char* p_text); - -bool accept(Parser* p_parser, TokenType type); - -bool accept_str(Parser* p_parser, TokenType type, const char* p_text); - -bool expect(Parser* p_parser, TokenType type); - -bool expect_str(Parser* p_parser, TokenType type, const char* p_text); - -size_t mark(Parser* p_parser); - -void reduce(Parser* p_parser, size_t mark); - -AST* get_tree(Parser* p_parser); - -void insert(Parser* p_parser, TokenType type, void* value); - -#endif /* PARSER_H */ diff --git a/source/libparse/tree.c b/source/libparse/tree.c index 87e2a8e..db3d6b7 100644 --- a/source/libparse/tree.c +++ b/source/libparse/tree.c @@ -4,7 +4,7 @@ $Revision$ $HeadURL$ */ -#include "tree.h" +#include static void tree_free(void* p_obj) { AST* p_tree = ((AST*)p_obj); @@ -96,28 +96,3 @@ bool tree_is_formtype(AST* p_tree, const char* val) { return ret; } -void tree_walk(AST* tree, tree_walker_t* walker) -{ - size_t idx; - walker->fn(walker->env, tree, PRE_NODE); - if (tree->tag == TREE) { - walker->fn(walker->env, tree, PRE_CHILDREN); - for (idx = 0; idx < vec_size(tree->ptr.vec); idx++) { - AST* child = (AST*)vec_at(tree->ptr.vec, idx); - walker->fn(walker->env, tree, PRE_CHILD); - tree_walk( child, walker ); - walker->fn(walker->env, tree, POST_CHILD); - } - walker->fn(walker->env, tree, POST_CHILDREN); - } - walker->fn(walker->env, tree, POST_NODE); -} - -tree_walker_t* tree_walker(void* env, tree_walk_fn_t fn) -{ - tree_walker_t* p_walker = (tree_walker_t*)mem_allocate(sizeof(tree_walker_t),NULL); - p_walker->env = env; - p_walker->fn = fn; - return p_walker; -} - diff --git a/source/libparse/tree.h b/source/libparse/tree.h deleted file mode 100644 index c40d64a..0000000 --- a/source/libparse/tree.h +++ /dev/null @@ -1,51 +0,0 @@ -/** - @file tree.h - @brief Module containing definition and common operations on parse trees. - $Revision$ - $HeadURL$ - */ -#ifndef TREE_H -#define TREE_H - -#include "vec.h" -#include "lexer.h" - -typedef enum { - ATOM, - TREE -} ASTTag; - -typedef struct { - ASTTag tag; - union { - Token* tok; - vec_t* vec; - } ptr; -} AST; - -typedef enum { - PRE_NODE, - POST_NODE, - PRE_CHILDREN, - POST_CHILDREN, - PRE_CHILD, - POST_CHILD, -} tree_walk_pos_t; - -typedef AST* (*tree_walk_fn_t)(void* env, AST* node, tree_walk_pos_t pos); - -typedef struct { - void* env; - tree_walk_fn_t fn; -} tree_walker_t; - -AST* tree_convert(AST* p_tree); -AST* tree_new(ASTTag tag, void* p_obj); -AST* tree_get_child(AST* p_tree, size_t idx); -void* tree_get_val(AST* p_tree); -void* tree_get_child_val(AST* p_tree, size_t idx); -bool tree_is_formtype(AST* p_tree, const char* val); -void tree_walk(AST* tree, tree_walker_t* walker); -tree_walker_t* tree_walker(void* env, tree_walk_fn_t fn); - -#endif /* TREE_H */ diff --git a/source/sclpl/codegen.c b/source/sclpl/codegen.c index 1ce9656..3de8875 100644 --- a/source/sclpl/codegen.c +++ b/source/sclpl/codegen.c @@ -1,4 +1,4 @@ -#include "parser.h" +#include #include "codegen.h" #include "pprint.h" diff --git a/source/sclpl/main.c b/source/sclpl/main.c index 87436b0..edfa7af 100644 --- a/source/sclpl/main.c +++ b/source/sclpl/main.c @@ -4,15 +4,13 @@ #include "opts.h" #include "str.h" #include "list.h" -#include "grammar.h" -#include "parser.h" -#include "lexer.h" #include "pprint.h" #include "codegen.h" #include "sys.h" #include "log.h" #include "ops.h" -#include "tree.h" +#include + /* Command Line Options *****************************************************************************/ diff --git a/source/sclpl/ops.c b/source/sclpl/ops.c index 253c3a6..681a371 100644 --- a/source/sclpl/ops.c +++ b/source/sclpl/ops.c @@ -5,13 +5,11 @@ $HeadURL$ */ #include "opts.h" -#include "parser.h" #include "str.h" #include "sys.h" -#include "tree.h" -#include "grammar.h" #include "pprint.h" #include "codegen.h" +#include vec_t* ops_parse_file(str_t* in) { bool failed = false; diff --git a/source/sclpl/pprint.h b/source/sclpl/pprint.h index 6b45690..e4717e9 100644 --- a/source/sclpl/pprint.h +++ b/source/sclpl/pprint.h @@ -7,8 +7,7 @@ #ifndef PPRINT_H #define PPRINT_H -#include "parser.h" -#include "lexer.h" +#include void pprint_token_type(FILE* file, Token* token); -- 2.54.0