From 9222cf13bd920ab847a849918a58ccd200b857cc Mon Sep 17 00:00:00 2001 From: nishi Date: Thu, 23 May 2024 11:29:38 +0000 Subject: [PATCH] writing exec routine git-svn-id: file:///raid/svn-main/nishi-dataworks/trunk@96 d4a5a174-5a4a-5b4b-b672-37683c10d7d5 --- Client/main.c | 24 +++----------- Library/Makefile | 2 +- Library/database.c | 2 +- Library/database_exec.c | 71 +++++++++++++++++++++++++++++++++++++++++ Library/dw_database.h | 52 ++++++++++++++++++++++++++++++ Library/dw_parser.h | 1 + Library/parser.c | 11 ++++++- 7 files changed, 141 insertions(+), 22 deletions(-) create mode 100644 Library/database_exec.c diff --git a/Client/main.c b/Client/main.c index 6f0a1cc..2aabcb3 100644 --- a/Client/main.c +++ b/Client/main.c @@ -51,15 +51,6 @@ void padleft(int leftpad, const char* str) { free(spaces); } -void print_recursive(struct __dw_token* token, int depth) { - int i; - for(i = 0; i < depth; i++) printf(" "); - printf("%d:[%s]\n", token->type, token->name == NULL ? "(null)" : token->name); - if(token->token != NULL) { - for(i = 0; token->token[i] != NULL; i++) print_recursive(token->token[i], depth + 1); - } -} - int main(int argc, char** argv) { int i; bool clear = true; @@ -233,17 +224,12 @@ int main(int argc, char** argv) { char* line = malloc(i + 1); line[i] = 0; memcpy(line, linebuf, i); - struct __dw_token* token = __dw_parser_parse(line); - if(token != NULL) { - if(token->error) { - printf("%s\n", dataworks_database_strerror(token->errnum)); - } else { - print_recursive(token, 0); - } - __dw_parser_free(token); - } else { - printf("Parser returned NULL. Help!\n"); + + struct dataworks_db_result* r = dataworks_database_execute_code(db, line); + if(r->error) { + printf("%s\n", dataworks_database_strerror(r->errnum)); } + free(line); if(strlen(linebuf) > 0) { char* newbuf = malloc(strlen(linebuf) - i); diff --git a/Library/Makefile b/Library/Makefile index e11800a..357c336 100644 --- a/Library/Makefile +++ b/Library/Makefile @@ -3,7 +3,7 @@ .PHONY: all clean .SUFFIXES: .c .o -OBJS = parser.o database.o util.o dataworks.o database_table.o +OBJS = parser.o database.o util.o dataworks.o database_table.o database_exec.o all: $(LIB_PREFIX)dataworks$(LIB_SUFFIX) $(STATICLIB_PREFIX)dataworks$(STATICLIB_SUFFIX) diff --git a/Library/database.c b/Library/database.c index fecb2ca..cd07b5e 100644 --- a/Library/database.c +++ b/Library/database.c @@ -134,7 +134,7 @@ uint64_t dataworks_database_get_mtime(struct dataworks_db* db) { return db->mtim int dataworks_database_get_error_number(struct dataworks_db* db) { return db->errnum; } -const char* dw_errors[] = {"Success", "Used already", "File open fail", "Invalid signature", "Invalid version"}; +const char* dw_errors[] = {"Success", "Used already", "File open fail", "Invalid signature", "Invalid version", "Parser returned NULL", "Cannot call non-method", "Unknown method"}; const char* dataworks_database_strerror(int n) { return dw_errors[n]; } diff --git a/Library/database_exec.c b/Library/database_exec.c new file mode 100644 index 0000000..5447ccd --- /dev/null +++ b/Library/database_exec.c @@ -0,0 +1,71 @@ +/* $Id$ */ +/* --- START LICENSE --- */ +/* -------------------------------------------------------------------------- */ +/* Copyright (c) 2024 Crabware. */ +/* Redistribution and use in source and binary forms, with or without modific */ +/* ation, are permitted provided that the following conditions are met: */ +/* 1. Redistributions of source code must retain the above copyright noti */ +/* ce, this list of conditions and the following disclaimer. */ +/* 2. Redistributions in binary form must reproduce the above copyright n */ +/* otice, this list of conditions and the following disclaimer in the documen */ +/* tation and/or other materials provided with the distribution. */ +/* 3. Neither the name of the copyright holder nor the names of its contr */ +/* ibutors may be used to endorse or promote products derived from this softw */ +/* are without specific prior written permission. */ +/* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS */ +/* " AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, TH */ +/* E IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPO */ +/* SE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS */ +/* BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CON */ +/* SEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITU */ +/* TE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPT */ +/* ION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, S */ +/* TRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN AN */ +/* Y WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY */ +/* OF SUCH DAMAGE. */ +/* -------------------------------------------------------------------------- */ +/* --- END LICENSE --- */ + +#include "dw_database.h" + +#include "dw_parser.h" +#include "dw_util.h" + +#include +#include + +struct dataworks_db_result* __dataworks_database_execute_code(struct dataworks_db* db, struct __dw_token* token) { + struct dataworks_db_result* r = malloc(sizeof(*r)); + r->error = false; + if(token->type == __DW_METHOD){ + if(__dw_strcaseequ(token->name, "create_table")){ + }else{ + r->error = true; + r->errnum = DW_ERR_EXEC_UNKNOWN_METHOD; + } + }else{ + r->error = true; + r->errnum = DW_ERR_EXEC_NON_METHOD; + } + return r; +} + +struct dataworks_db_result* dataworks_database_execute_code(struct dataworks_db* db, const char* code) { + struct dataworks_db_result* r = malloc(sizeof(*r)); + r->error = false; + struct __dw_token* token = __dw_parser_parse(code); + if(token != NULL){ + if(token->error){ + r->error = true; + r->errnum = token->errnum; + }else{ + free(r); + r = __dataworks_database_execute_code(db, token); + } + __dw_parser_free(token); + }else{ + r->error = true; + r->errnum = DW_ERR_PARSER_NULL; + } + return r; +} diff --git a/Library/dw_database.h b/Library/dw_database.h index fe18991..0e05775 100644 --- a/Library/dw_database.h +++ b/Library/dw_database.h @@ -93,6 +93,27 @@ enum DW_ERRORS { * */ DW_ERR_INVALID_VERSION, + + /** + * @~english + * @brief Parser returned NULL + * + */ + DW_ERR_PARSER_NULL, + + /** + * @~english + * @brief Cannot call non-method + * + */ + DW_ERR_EXEC_NON_METHOD, + + /** + * @~english + * @brief Unknown method + * + */ + DW_ERR_EXEC_UNKNOWN_METHOD, }; /** @@ -137,6 +158,27 @@ struct dataworks_db { int errnum; }; +/** + * @~english + * @brief Database result struct + * + */ +struct dataworks_db_result { + /** + * @~english + * @brief True if this is an error. + * + */ + bool error; + + /** + * @~english + * @brief Error number. + * + */ + int errnum; +}; + /** * @~english * @brief `indexentry` for v1 database. @@ -275,6 +317,16 @@ const char* dataworks_database_strerror(int n); */ void dataworks_database_update_mtime(struct dataworks_db* db); +/** + * @~english + * @brief Executes the code. + * @param db Database + * @param code Code + * @return Result + * + */ +struct dataworks_db_result* dataworks_database_execute_code(struct dataworks_db* db, const char* code); + #ifdef __cplusplus } #endif diff --git a/Library/dw_parser.h b/Library/dw_parser.h index bca3f57..04ccf4f 100644 --- a/Library/dw_parser.h +++ b/Library/dw_parser.h @@ -54,6 +54,7 @@ struct __dw_token { struct __dw_token* __dw_parser_parse(const char* str); void __dw_parser_free(struct __dw_token* token); +void __dw_parser_print(struct __dw_token* token, int depth); #ifdef __cplusplus } diff --git a/Library/parser.c b/Library/parser.c index d97a768..922eef1 100644 --- a/Library/parser.c +++ b/Library/parser.c @@ -189,7 +189,7 @@ struct __dw_token* __dw_parser_parse(const char* str) { } void __dw_parser_free(struct __dw_token* token) { - free(token->name); + if(token->name != NULL) free(token->name); if(token->type == __DW_METHOD) { if(token->token != NULL) { int i; @@ -199,3 +199,12 @@ void __dw_parser_free(struct __dw_token* token) { } free(token); } + +void __dw_parser_print(struct __dw_token* token, int depth) { + int i; + for(i = 0; i < depth; i++) printf(" "); + printf("%d:[%s]\n", token->type, token->name == NULL ? "(null)" : token->name); + if(token->token != NULL) { + for(i = 0; token->token[i] != NULL; i++) __dw_parser_print(token->token[i], depth + 1); + } +} -- 2.43.0