From 53945b92b23bf5ba1c8ca2ad66ad5a02472ec09f Mon Sep 17 00:00:00 2001 From: nishi Date: Tue, 4 Jun 2024 11:22:52 +0000 Subject: [PATCH] print stuff git-svn-id: file:///raid/svn-main/nishi-dataworks/trunk@146 d4a5a174-5a4a-5b4b-b672-37683c10d7d5 --- Grammar/dw.l | 3 +++ Grammar/dw.y | 23 +++++++++++++++++++++-- Library/dataworks.c | 5 ++++- Library/exec.c | 4 ++-- Library/parser.c | 31 ++++++++++++++++++++++++++++++- 5 files changed, 60 insertions(+), 6 deletions(-) diff --git a/Grammar/dw.l b/Grammar/dw.l index c8f22c2..c109358 100644 --- a/Grammar/dw.l +++ b/Grammar/dw.l @@ -39,17 +39,20 @@ extern YYSTYPE yylval; yylval.node.string = strdup(yytext + 1); yylval.node.string[strlen(yylval.node.string) - 1] = 0; yylval.node.nodes = NULL; + yylval.node.ident = NULL; return (STRING); } ['][^']*['] { yylval.node.string = strdup(yytext + 1); yylval.node.string[strlen(yylval.node.string) - 1] = 0; yylval.node.nodes = NULL; + yylval.node.ident = NULL; return (STRING); } [a-zA-Z0-9_\-]+ { yylval.node.ident = strdup(yytext); yylval.node.nodes = NULL; + yylval.node.string = NULL; return (IDENTIFIER); } [\(\),] { diff --git a/Grammar/dw.y b/Grammar/dw.y index 7b54d30..8026ad4 100644 --- a/Grammar/dw.y +++ b/Grammar/dw.y @@ -42,6 +42,10 @@ } node; } +%{ +void parser_process(struct Node* node); +%} + %% argument @@ -50,6 +54,9 @@ argument $$.nodes = $1.nodes; $$.ident = $1.ident; } + | command { + $$ = $1; + } ; single_argument @@ -70,7 +77,13 @@ single_argument arguments : single_argument { $$.nodes = malloc(sizeof(*$$.nodes) * 2); - $$.nodes[0] = &$1; + $$.nodes[0] = malloc(sizeof($$)); + $$.nodes[0]->nodes = NULL; + $$.nodes[0]->ident = NULL; + $$.nodes[0]->string = NULL; + if($1.ident != NULL) $$.nodes[0]->ident = strdup($1.ident); + if($1.string != NULL) $$.nodes[0]->string = strdup($1.string); + if($1.nodes != NULL) $$.nodes[0]->nodes = $1.nodes; $$.nodes[1] = NULL; } | arguments ',' single_argument { @@ -79,7 +92,13 @@ arguments for(i = 0; old_nodes[i] != NULL; i++); $$.nodes = malloc(sizeof(*$$.nodes) * (i + 2)); for(i = 0; old_nodes[i] != NULL; i++) $$.nodes[i] = old_nodes[i]; - $$.nodes[i] = &$3; + $$.nodes[i] = malloc(sizeof($$)); + $$.nodes[i]->nodes = NULL; + $$.nodes[i]->ident = NULL; + $$.nodes[i]->string = NULL; + if($3.ident != NULL) $$.nodes[i]->ident = strdup($3.ident); + if($3.string != NULL) $$.nodes[i]->string = strdup($3.string); + if($3.nodes != NULL) $$.nodes[i]->nodes = $3.nodes; $$.nodes[i + 1] = NULL; free(old_nodes); } diff --git a/Library/dataworks.c b/Library/dataworks.c index dd6e7a0..dad8ea6 100644 --- a/Library/dataworks.c +++ b/Library/dataworks.c @@ -71,7 +71,10 @@ const char* dataworks_get_copyright(void) { return dataworks_copyright; } int yywrap() { return 1; } -int yyerror(const char* err) { fprintf(stderr, "Parser error: %s\n", err); return 0; } +int yyerror(const char* err) { + fprintf(stderr, "Parser error: %s\n", err); + return 0; +} bool dataworks_get_if_supported(void) { #ifdef SUPPORTED diff --git a/Library/exec.c b/Library/exec.c index deb1e51..2b49b09 100644 --- a/Library/exec.c +++ b/Library/exec.c @@ -38,10 +38,10 @@ struct dataworks_db_result* dataworks_database_execute_code(struct dataworks_db* db, const char* code, bool dolog) { struct dataworks_db_result* r = malloc(sizeof(*r)); r->error = false; - r->value = NULL; + r->value = NULL; int err; - if((err = __dw_parser_parse(code, dolog)) != DW_ERR_SUCCESS){ + if((err = __dw_parser_parse(code, dolog)) != DW_ERR_SUCCESS) { r->error = true; r->errnum = err; } diff --git a/Library/parser.c b/Library/parser.c index 99f6f70..cba1b29 100644 --- a/Library/parser.c +++ b/Library/parser.c @@ -31,6 +31,8 @@ #include "dw_database.h" #include "dw_util.h" +#include "../Grammar/dw.tab.h" + #include #include #include @@ -41,15 +43,42 @@ extern int yyparse(void); extern void* yy_scan_string(const char* str); extern void yy_delete_buffer(void* buffer); +void print_node(struct Node* node, bool top) { + printf("%s(", node->ident); + if(node->nodes != NULL) { + int i; + for(i = 0; node->nodes[i] != NULL; i++) { + if(i > 0) printf(", "); + if(node->nodes[i]->ident != NULL) { + print_node(node->nodes[i], false); + } else { + printf("\"%s\"", node->nodes[i]->string); + } + } + } + printf(")"); + if(top) printf("\n"); +} + +void parser_process(struct Node* node) { + if(node->ident != NULL) { + int i; + print_node(node, true); + } +} + int __dw_parser_parse(const char* str, bool top) { #ifdef PARSER_DEBUG yydebug = 1; #endif void* buf = yy_scan_string(str); - if(yyparse() != 0){ + if(yyparse() != 0) { yy_delete_buffer(buf); return DW_ERR_PARSER_FAIL; + } else { + extern YYSTYPE yyval; + parser_process(&yyval.node); } yy_delete_buffer(buf); -- 2.43.0