#include <stdlib.h>
#include <string.h>
-struct dataworks_db_result* __dataworks_database_execute_code(struct dataworks_db* db, struct __dw_token* token, bool dolog) {
- struct dataworks_db_result* r = malloc(sizeof(*r));
- r->value = NULL;
- r->error = false;
- if(token->type == __DW_METHOD) {
- int i;
- struct dataworks_db_result** results = malloc(sizeof(*results));
- results[0] = NULL;
- int argc = 0;
- if(token->token != NULL) {
- for(i = 0; token->token[i] != NULL; i++) {
- argc++;
- if(token->token[i]->type == __DW_METHOD) {
- struct dataworks_db_result* r2 = __dataworks_database_execute_code(db, token->token[i], dolog);
- if(r2->error) {
- int j;
- for(j = 0; results[j] != NULL; j++) {
- dataworks_database_free_result(results[j]);
- }
- free(results);
- dataworks_database_free_result(r);
- r = r2;
- break;
- }
- int j;
- for(j = 0; results[j] != NULL; j++)
- ;
- struct dataworks_db_result** newresults = malloc(sizeof(*newresults) * (j + 2));
- for(j = 0; results[j] != NULL; j++) {
- newresults[j] = results[j];
- }
- free(results);
- results = newresults;
- results[j] = r2;
- results[j + 1] = NULL;
- } else {
- struct dataworks_db_result* r2 = malloc(sizeof(*r2));
- r2->error = false;
- r2->value = __dw_strdup(token->token[i]->name);
- int j;
- for(j = 0; results[j] != NULL; j++)
- ;
- struct dataworks_db_result** newresults = malloc(sizeof(*newresults) * (j + 2));
- for(j = 0; results[j] != NULL; j++) {
- newresults[j] = results[j];
- }
- free(results);
- results = newresults;
- results[j] = r2;
- results[j + 1] = NULL;
- }
- }
- }
- if(r->error) return r;
- if(__dw_strcaseequ(token->name, "create_table")) {
- argc = 0;
- int first = -1;
- int j;
- for(j = 0; results[j] != NULL; j++) {
- if(results[j]->value != NULL) {
- argc++;
- if(first == -1) first = j;
- }
- }
- if(argc < 2) {
- r->error = true;
- r->errnum = DW_ERR_EXEC_INSUFFICIENT_ARGUMENTS;
- } else {
- int j;
- char* fieldtypes = malloc(argc);
- fieldtypes[argc - 1] = 0;
- char** fields = malloc(sizeof(*fields) * (argc));
- fields[argc - 1] = NULL;
- argc = 0;
- for(j = first + 1; results[j] != NULL; j++) {
- if(results[j]->value != NULL) {
- char* val = __dw_strdup(results[j]->value);
- int k;
- for(k = 0; val[k] != 0; k++) {
- if(val[k] == ':') {
- val[k] = 0;
- if(__dw_strcaseequ(val, "string")) {
- fieldtypes[argc] = DW_RECORD_STRING;
- } else if(__dw_strcaseequ(val, "integer")) {
- fieldtypes[argc] = DW_RECORD_INTEGER;
- } else if(__dw_strcaseequ(val, "floating")) {
- fieldtypes[argc] = DW_RECORD_FLOATING;
- } else if(__dw_strcaseequ(val, "logical")) {
- fieldtypes[argc] = DW_RECORD_LOGICAL;
- } else if(__dw_strcaseequ(val, "help")) {
- fieldtypes[argc] = DW_RECORD_HELP;
- }
- fields[argc] = __dw_strdup(val + k + 1);
- argc++;
- break;
- }
- }
- free(val);
- }
- }
- int er;
- if((er = dataworks_database_create_table(db, results[first]->value, fields, fieldtypes)) != DW_ERR_SUCCESS) {
- r->error = true;
- r->errnum = er;
- }
- for(j = 0; fields[j] != NULL; j++) free(fields[j]);
- free(fields);
- free(fieldtypes);
- }
- } else if(__dw_strcaseequ(token->name, "delete_table")) {
- argc = 0;
- int j;
- for(j = 0; results[j] != NULL; j++) {
- if(results[j]->value != NULL) {
- argc++;
- }
- }
- if(argc < 1) {
- r->error = true;
- r->errnum = DW_ERR_EXEC_INSUFFICIENT_ARGUMENTS;
- } else {
- for(j = 0; results[j] != NULL; j++) {
- if(results[j]->value != NULL) {
- int er = dataworks_database_delete_table(db, results[j]->value);
- if(er != DW_ERR_SUCCESS) {
- r->error = true;
- r->errnum = er;
- break;
- }
- }
- }
- }
- } else if(__dw_strcaseequ(token->name, "concat")) {
- r->value = malloc(1);
- r->value[0] = 0;
- int j;
- for(j = 0; results[j] != NULL; j++) {
- if(results[j]->value != NULL) {
- char* tmp = r->value;
- r->value = __dw_strcat(tmp, results[j]->value);
- free(tmp);
- }
- }
- } else if(__dw_strcaseequ(token->name, "print")) {
- int j;
- for(j = 0; results[j] != NULL; j++) {
- if(results[j]->value != NULL) {
- printf("%s\n", results[j]->value);
- }
- }
- } else if(__dw_strcaseequ(token->name, "use")) {
- int j;
- bool set = false;
- for(j = 0; results[j] != NULL; j++) {
- if(results[j]->value != NULL) {
- if(set) {
- r->error = true;
- r->errnum = DW_ERR_EXEC_TOO_MANY_ARGUMENTS;
- break;
- }
- r->errnum = dataworks_database_use_table(db, results[j]->value);
- if(r->errnum != DW_ERR_SUCCESS) {
- r->error = true;
- } else if(dolog) {
- printf("Using table `%s`.\n", results[j]->value);
- }
- }
- }
- } else if(__dw_strcaseequ(token->name, "insert")) {
- if(db->name == NULL) {
- r->error = true;
- r->errnum = DW_ERR_DATABASE_NOT_SELECTED;
- } else {
- argc = 0;
- int first = -1;
- int j;
- for(j = 0; results[j] != NULL; j++) {
- if(results[j]->value != NULL) {
- argc++;
- if(first == -1) first = j;
- }
- }
- if(argc == 0) {
- r->error = true;
- r->errnum = DW_ERR_EXEC_INSUFFICIENT_ARGUMENTS;
- } else {
- char* f = dataworks_database_get_table_field_types(db, db->name);
- void** data = malloc(sizeof(*data) * strlen(f));
- char* fields = malloc(sizeof(*data) * (strlen(f) + 1));
- for(j = 0; f[j] != 0; j++) fields[j] = 'U';
- fields[strlen(f)] = 0;
- for(j = 0; f[j] != 0; j++) data[j] = "";
- free(f);
- struct dataworks_db_result* dbr = dataworks_database_insert_record(db, data, fields);
- if(dbr->error) {
- r->error = true;
- r->errnum = dbr->errnum;
- }
- dataworks_database_free_result(dbr);
- }
- }
- } else {
- r->error = true;
- r->errnum = DW_ERR_EXEC_UNKNOWN_METHOD;
- }
- int j;
- for(j = 0; results[j] != NULL; j++) {
- dataworks_database_free_result(results[j]);
- }
- free(results);
- } 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, bool dolog) {
struct dataworks_db_result* r = malloc(sizeof(*r));
r->error = false;
- r->value = NULL;
- struct __dw_token* token = __dw_parser_parse(code, dolog);
- if(token != NULL) {
- if(token->error) {
- r->error = true;
- r->errnum = token->errnum;
- } else {
- dataworks_database_free_result(r);
- r = __dataworks_database_execute_code(db, token, dolog);
- }
- __dw_parser_free(token);
- } else {
- r->error = true;
- r->errnum = DW_ERR_PARSER_NULL;
- }
+ r->value = NULL;
+
+ __dw_parser_parse(code, dolog);
+
return r;
}
#include <stdlib.h>
#include <string.h>
-struct __dw_token* __dw_parser_parse(const char* str, bool top) {
- struct __dw_token* token = malloc(sizeof(*token));
- token->error = false;
- token->errnum = DW_ERR_SUCCESS;
- token->token = NULL;
- token->name = NULL;
- token->type = __DW_METHOD;
- char* buf = malloc(1);
- buf[0] = 0;
- int i;
- bool dq = false;
- char cbuf[2];
- cbuf[1] = 0;
- int brace = 0;
- char* br = malloc(1);
- bool has_brace = false;
- br[0] = 0;
- int start = 0;
- for(i = 0; str[i] != 0; i++) {
- cbuf[0] = str[i];
- if(brace > 0) {
- if(str[i] == '(') brace++;
- if(str[i] == ')') brace--;
- if(brace > 0) {
- if(str[i] == ',') {
- i++;
- for(; str[i] != 0 && (str[i] == '\t' || str[i] == ' '); i++)
- ;
- i--;
- }
- char* tmp = br;
- br = __dw_strcat(tmp, cbuf);
- free(tmp);
- } else {
- int j;
- for(j = 0; buf[j] != 0; j++) {
- if(buf[j] != ' ' && buf[j] != '\t') {
- char* newbuf = __dw_strdup(buf + j);
- free(buf);
- buf = newbuf;
- break;
- }
- }
- for(j = 0; br[j] != 0; j++) {
- if(br[j] != ' ' && br[j] != '\t') {
- char* newbr = __dw_strdup(br + j);
- free(br);
- br = newbr;
- break;
- }
- }
- for(j = strlen(buf) - 1; j >= 0; j--) {
- if(buf[j] != ' ' && buf[j] != '\t') {
- buf[j + 1] = 0;
- break;
- }
- }
- for(j = strlen(br) - 1; j >= 0; j--) {
- if(br[j] != ' ' && br[j] != '\t') {
- br[j + 1] = 0;
- break;
- }
- }
- if(buf[0] == ',') {
- char* newbuf = __dw_strdup(buf + 1);
- free(buf);
- buf = newbuf;
- }
- token->name = __dw_strdup(buf);
- int k;
- char* comma = malloc(1);
- comma[0] = 0;
- int intbrace = 0;
- for(k = 0;; k++) {
- char c = br[k];
- if(c == '(' || c == ')' || intbrace > 0) {
- if(c == '(') intbrace++;
- if(c == ')') intbrace--;
- cbuf[0] = c;
- char* tmp = comma;
- comma = __dw_strcat(tmp, cbuf);
- free(tmp);
- } else if(c == 0 || c == ',') {
- if(strlen(comma) > 0) {
- j = 0;
- if(token->token != NULL) {
- for(j = 0; token->token[j] != NULL; j++)
- ;
- }
- struct __dw_token** newtokens = malloc(sizeof(*newtokens) * (j + 2));
- if(token->token != NULL) {
- for(j = 0; token->token[j] != NULL; j++) {
- newtokens[j] = token->token[j];
- }
- free(token->token);
- }
- token->token = newtokens;
- token->token[j] = __dw_parser_parse(comma, false);
- token->token[j + 1] = NULL;
- free(comma);
- comma = malloc(1);
- comma[0] = 0;
- }
- if(c == 0) break;
- } else {
- cbuf[0] = c;
- char* tmp = comma;
- comma = __dw_strcat(tmp, cbuf);
- free(tmp);
- }
- }
- if(top) printf("%s(%s)\n", buf, br);
- free(comma);
- free(br);
- br = malloc(1);
- br[0] = 0;
- free(buf);
- buf = malloc(1);
- buf[0] = 0;
- }
- } else if(dq) {
- if(str[i] == '"') {
- dq = !dq;
- } else {
- char* tmp = buf;
- buf = __dw_strcat(tmp, cbuf);
- free(tmp);
- }
- } else if(str[i] == '(') {
- has_brace = true;
- brace++;
- } else if(str[i] == ')') {
- brace--;
- } else if(str[i] == '"') {
- dq = !dq;
- } else if(str[i] == ',') {
- free(buf);
- buf = malloc(1);
- buf[0] = 0;
- } else {
- char* tmp = buf;
- buf = __dw_strcat(tmp, cbuf);
- free(tmp);
- }
- }
- if(!has_brace) {
- token->type = __DW_VALUE;
- token->name = __dw_strdup(buf);
- }
- free(br);
- free(buf);
- return token;
-}
-
-void __dw_parser_free(struct __dw_token* token) {
- if(token->name != NULL) free(token->name);
- if(token->type == __DW_METHOD) {
- if(token->token != NULL) {
- int i;
- for(i = 0; token->token[i] != NULL; i++) __dw_parser_free(token->token[i]);
- free(token->token);
- }
- }
- free(token);
-}
+extern int yydebug;
+extern int yyparse(void);
+extern void* yy_scan_string(const char* str);
+extern void yy_delete_buffer(void* buffer);
-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);
- }
+int __dw_parser_parse(const char* str, bool top) {
+ void* buf = yy_scan_string(str);
+ yyparse();
+ yy_delete_buffer(buf);
}