aboutsummaryrefslogtreecommitdiff
path: root/src/parser.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/parser.c')
-rw-r--r--src/parser.c90
1 files changed, 49 insertions, 41 deletions
diff --git a/src/parser.c b/src/parser.c
index 654cda4..1458018 100644
--- a/src/parser.c
+++ b/src/parser.c
@@ -21,15 +21,16 @@ static void quote_stack_push(struct quote_node **head, struct sexp *cur_sexp);
static struct sexp *quote_stack_pop(struct quote_node **head, int peek);
// returns 0 on success
-static int on_paren(parser_t parser, int paren_type); // 0 is open, 1 is close
+enum paren_type { P_OPEN, P_CLOSE };
+static int on_paren(parser_t parser, enum paren_type type); // 0 is open, 1 is close
static int on_quote(parser_t parent);
static int on_value(parser_t parent, value_t value);
-#define TOKEN_CALLBACK_TBL(X, parser, token) \
-/* X(test, execute on succes) */ \
- X(TOKEN_PARENTHS_OPEN, on_paren(parser, 0)) \
- X(TOKEN_PARENTHS_CLOSE, on_paren(parser, 1)) \
- X(TOKEN_SPECIAL_QUOTE, on_quote(parser)) \
+#define TOKEN_CALLBACK_TBL(X, parser, token) \
+/* X(test, execute on succes) */ \
+ X(TOKEN_PARENTHS_OPEN, on_paren(parser, P_OPEN)) \
+ X(TOKEN_PARENTHS_CLOSE, on_paren(parser, P_CLOSE)) \
+ X(TOKEN_SPECIAL_QUOTE, on_quote(parser)) \
X(TOKEN_VALUE, on_value(parser, token->value))
#define FN(fn, arg) "%s", fn(arg, buf, buf_sz)
@@ -47,30 +48,38 @@ static int on_value(parser_t parent, value_t value);
return -1; \
} break;
-int parser_parse_lexer(parser_t parser, lexer_t lexer)
-{
- for(size_t i = 0; i < lexer->ntokens; i++) {
- struct token *token = &lexer->tokens[i];
+int parser_parse_toklist(parser_t parser, toklist_t *toklist, ast_t *ast)
+{
+ if(parser->cur_sexp == NULL) {
+ size_t index = sexp_add(&ast->sexp, AST_VALUE);
+ ast->sexp.children[index].value = value_copy(parser->begin_symbol_value);
+
+ parser->cur_sexp = &ast->sexp;
+ }
+
+
+ for(size_t i = 0; i < toklist->ntokens; i++) {
+ struct token *token = &toklist->tokens[i];
- switch(token->type) {
+ switch(token->type) {
TOKEN_CALLBACK_TBL(CASE_TYPE, parser, token);
default:
err("parser_parse_lexer: Unknown token type given");
break;
}
- if((token->type != TOKEN_SPECIAL_QUOTE) &&
- (parser->cur_sexp == quote_stack_pop(&parser->quote_head, 1))) {
- if(on_paren(parser, 1));
- quote_stack_pop(&parser->quote_head, 0);
- }
+ if(token->type != TOKEN_SPECIAL_QUOTE)
+ while(parser->cur_sexp == quote_stack_pop(&parser->quote_head, 1)) {
+ on_paren(parser, P_CLOSE);
+ quote_stack_pop(&parser->quote_head, 0);
+ }
}
- if(&parser->root == parser->cur_sexp) {
+ if(&ast->sexp == parser->cur_sexp) {
return 0;
} else {
return 1;
- }
+ }
}
parser_t parser_create()
@@ -88,7 +97,6 @@ parser_t parser_create()
parser->begin_symbol_value = VALUE_CREATE("begin");
parser->quote_symbol_value = VALUE_CREATE("quote");
- parser->root.nchildren = 0;
parser->quote_head = NULL;
parser_reset(parser);
@@ -102,40 +110,35 @@ void parser_destroy(parser_t parser)
value_destroy(parser->begin_symbol_value);
value_destroy(parser->quote_symbol_value);
- sexp_free(&parser->root);
+ parser_reset(parser);
free(parser);
}
void parser_reset(parser_t parser)
{
- struct sexp *root = &parser->root;
-
- for(size_t i = 0; i < root->nchildren; i++) {
- ast_free(&root->children[i]);
- }
-
- sexp_init(root);
- size_t index = sexp_add(root, AST_VALUE);
-
- root->children[index].value = value_copy(parser->begin_symbol_value);
- parser->cur_sexp = &parser->root;
-
+ parser->cur_sexp = NULL;
while(quote_stack_pop(&parser->quote_head, 0) != NULL);
}
+void ast_reset(ast_t *ast)
+{
+ sexp_free(&ast->sexp);
+ sexp_init(&ast->sexp);
+}
+
-void parser_print_ast(parser_t parser)
+void ast_print(ast_t *ast)
{
- sexp_print(&parser->root, 0);
+ sexp_print(&ast->sexp, 0);
}
// ---------- Callback Functions ---------- //
-static int on_paren(parser_t parser, int paren_type)
+static int on_paren(parser_t parser, enum paren_type type)
{
- if(paren_type) { // !0 closing paren
+ if(type == P_CLOSE) {
parser->cur_sexp = parser->cur_sexp->prev;
- } else { // 0 opening paren
+ } else if(type == P_OPEN) {
size_t index = sexp_add(parser->cur_sexp, AST_SEXP);
struct sexp *prev = parser->cur_sexp;
@@ -153,7 +156,7 @@ static int on_paren(parser_t parser, int paren_type)
static int on_quote(parser_t parser)
{
// new sexp
- on_paren(parser, 0);
+ on_paren(parser, P_OPEN);
// add symbol to the sexp
on_value(parser, parser->quote_symbol_value);
@@ -192,6 +195,8 @@ static void sexp_init(struct sexp *sexp)
static void sexp_print(struct sexp *sexp, int indent)
{
+ // (void)indent;
+ // printf("( ");
for(size_t i = 0; i < sexp->nchildren; i++) {
struct ast *child = &sexp->children[i];
@@ -202,9 +207,11 @@ static void sexp_print(struct sexp *sexp, int indent)
char buf[LEXER_IDEN_CAP];
size_t buf_sz = LEXER_IDEN_CAP;
-
- info("%d %s", indent, value_string(child->value, buf, buf_sz));
+
+ for(int i = 0; i < indent; i++) printf(" ");
+ printf("%s\n", value_string(child->value, buf, buf_sz));
}
+ // printf(")");
}
static void sexp_free(struct sexp *sexp)
@@ -212,7 +219,8 @@ static void sexp_free(struct sexp *sexp)
for(size_t i = 0; i < sexp->nchildren; i++){
ast_free(&sexp->children[i]);
}
- free(sexp->children);
+
+ if(sexp->children) free(sexp->children);
}
#define CASE_FREE(type, free_func, print_func) \