]> git.mdlowis.com Git - proto/sclpl.git/commitdiff
Refactored libparse to use a single header and removed unused tree_walker types
authorMike D. Lowis <mike.lowis@gentex.com>
Fri, 2 Oct 2015 18:21:37 +0000 (14:21 -0400)
committerMike D. Lowis <mike.lowis@gentex.com>
Fri, 2 Oct 2015 18:21:37 +0000 (14:21 -0400)
13 files changed:
source/libparse/grammar.c
source/libparse/grammar.h [deleted file]
source/libparse/lexer.c
source/libparse/lexer.h [deleted file]
source/libparse/libparse.h [new file with mode: 0644]
source/libparse/parser.c
source/libparse/parser.h [deleted file]
source/libparse/tree.c
source/libparse/tree.h [deleted file]
source/sclpl/codegen.c
source/sclpl/main.c
source/sclpl/ops.c
source/sclpl/pprint.h

index d12cf7a38303fc70c58338c068515ef21ce324d5..4bfcc0bfed7543816a91754d9eb9c43546c7b12e 100644 (file)
@@ -4,9 +4,7 @@
   $Revision$
   $HeadURL$
 */
-#include "grammar.h"
-#include "lexer.h"
-#include "exn.h"
+#include <libparse.h>
 
 AST* grammar_toplevel(Parser* p)
 {
diff --git a/source/libparse/grammar.h b/source/libparse/grammar.h
deleted file mode 100644 (file)
index 962f456..0000000
+++ /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 */
index 75879f35aa72a0501b9de4ec16c17ce8c53682c4..3a9b6fd01a74fee7c582d0fc6f02d67598bd88b0 100644 (file)
@@ -4,11 +4,7 @@
   $Revision$
   $HeadURL$
   */
-#include "lexer.h"
-#include "mem.h"
-#include <string.h>
-#include <stdlib.h>
-#include <errno.h>
+#include <libparse.h>
 
 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 (file)
index c80579b..0000000
+++ /dev/null
@@ -1,46 +0,0 @@
-/**
-  @file lexer.h
-  @brief TODO: Describe this file
-  $Revision$
-  $HeadURL$
-  */
-#ifndef LEXER_H
-#define LEXER_H
-
-#include <stdio.h>
-#include <stdbool.h>
-#include <string.h>
-#include <assert.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;
-
-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 (file)
index 0000000..7355c00
--- /dev/null
@@ -0,0 +1,107 @@
+/**
+  @file libparse.h
+  @brief TODO: Describe this file
+  $Revision$
+  $HeadURL$
+  */
+#ifndef LIBPARSE_H
+#define LIBPARSE_H
+
+#include <stddef.h>
+#include <stdbool.h>
+#include <stdint.h>
+#include <stdio.h>
+#include <errno.h>
+#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 */
index 44b4575640ef8d15e8ef1bc5792b7238ec5d14da..674f2d7e8f33f1f251a85d0daec60abea0ec1c11 100644 (file)
@@ -4,8 +4,7 @@
   $Revision$
   $HeadURL$
   */
-#include "parser.h"
-#include "vec.h"
+#include <libparse.h>
 
 DEFINE_EXCEPTION(ParseException, &RuntimeException);
 
diff --git a/source/libparse/parser.h b/source/libparse/parser.h
deleted file mode 100644 (file)
index 3e447ad..0000000
+++ /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 */
index 87e2a8e413ad79c8900a8decfa59a14b4d1aa68f..db3d6b70d599a69532f037b9f5302585efef9f41 100644 (file)
@@ -4,7 +4,7 @@
   $Revision$
   $HeadURL$
   */
-#include "tree.h"
+#include <libparse.h>
 
 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 (file)
index c40d64a..0000000
+++ /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 */
index 1ce965695dbeec2a604a4e5cf2dcc5c4a0eccdbb..3de8875969b53cbdfa8db329071a53a3501530ff 100644 (file)
@@ -1,4 +1,4 @@
-#include "parser.h"
+#include <libparse.h>
 #include "codegen.h"
 #include "pprint.h"
 
index 87436b004dedf217427318fea37f81a8275165e3..edfa7af69f456bdbab35e93efeb367ad48f7ca0d 100644 (file)
@@ -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 <libparse.h>
+
 
 /* Command Line Options
  *****************************************************************************/
index 253c3a674b6d19b6d9ae9756379ae61e5ee9f0a3..681a371878d84cd97bc15edc2ccf33bdcc68a00c 100644 (file)
@@ -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 <libparse.h>
 
 vec_t* ops_parse_file(str_t* in) {
     bool failed = false;
index 6b456909aff17b7e66d86d8233ce2958389b039d..e4717e9168c08cc9dfc2cc0104acf375f3655a66 100644 (file)
@@ -7,8 +7,7 @@
 #ifndef PPRINT_H
 #define PPRINT_H
 
-#include "parser.h"
-#include "lexer.h"
+#include <libparse.h>
 
 void pprint_token_type(FILE* file, Token* token);