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;
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);
.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)
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]; }
--- /dev/null
+/* $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;
+}
*
*/
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,
};
/**
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.
*/
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
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
}
}
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;
}
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);
+ }
+}