]> Nishi Git Mirror - dataworks.git/commitdiff
writing exec routine
authornishi <nishi@d4a5a174-5a4a-5b4b-b672-37683c10d7d5>
Thu, 23 May 2024 11:29:38 +0000 (11:29 +0000)
committernishi <nishi@d4a5a174-5a4a-5b4b-b672-37683c10d7d5>
Thu, 23 May 2024 11:29:38 +0000 (11:29 +0000)
git-svn-id: file:///raid/svn-main/nishi-dataworks/trunk@96 d4a5a174-5a4a-5b4b-b672-37683c10d7d5

Client/main.c
Library/Makefile
Library/database.c
Library/database_exec.c [new file with mode: 0644]
Library/dw_database.h
Library/dw_parser.h
Library/parser.c

index 6f0a1cc4ce2e839d8fcaae41ba47fc7a0fb5e9f2..2aabcb3bee12f2a57f8dd956f63b47bad99c7f4d 100644 (file)
@@ -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);
index e11800ae2196a5bd0551f44e8858678d10b7dba1..357c3364cca8e0d07c8873b6e501e6b081710145 100644 (file)
@@ -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)
 
index fecb2cabce52ff764d67fe69d633e8acb94ca166..cd07b5e6b7ff83c1a1724d161270f5b96af2a87d 100644 (file)
@@ -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 (file)
index 0000000..5447ccd
--- /dev/null
@@ -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 <stdlib.h>
+#include <stdbool.h>
+
+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;
+}
index fe18991206bc516325ef96dc3d1e350390c3f09c..0e057752d465d2e40c97b181815559c6d82868fe 100644 (file)
@@ -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
index bca3f57dddd57c4835b73fb85cae582b92cd6d4b..04ccf4fd9d4e55be5a2cf457da3dc03c3fa579ff 100644 (file)
@@ -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
 }
index d97a7683401b5cc3ef42685c5bb8aaab73eb15b8..922eef1f1a38e4a00c555469979fac01eb23b514 100644 (file)
@@ -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);
+       }
+}