]> Nishi Git Mirror - dataworks.git/commitdiff
fix fgetc
authornishi <nishi@d4a5a174-5a4a-5b4b-b672-37683c10d7d5>
Tue, 4 Jun 2024 02:57:18 +0000 (02:57 +0000)
committernishi <nishi@d4a5a174-5a4a-5b4b-b672-37683c10d7d5>
Tue, 4 Jun 2024 02:57:18 +0000 (02:57 +0000)
git-svn-id: file:///raid/svn-main/nishi-dataworks/trunk@140 d4a5a174-5a4a-5b4b-b672-37683c10d7d5

Client/main.c
Grammar/dw.l
Grammar/dw.y
Library/dw_parser.h
Library/exec.c
Library/parser.c
common-decl.mk

index 8b81c6fe5af22dad0de47747a54a335b0be11131..1f5479f4e57a01a93b4afc1bd262fc2dc9c7d1af 100644 (file)
@@ -176,7 +176,7 @@ int main(int argc, char** argv) {
        int len = 0;
        char* buf = malloc(1);
        buf[0] = 0;
-       char ch;
+       int ch;
        char prompt = '.';
        FILE* fp = stdin;
        if(fprog != NULL) {
index 99c52173705145c4a888352cba420ffa1e59360c..fb8cebd79a3d821cb225ce139b4bcd59daa2784b 100644 (file)
@@ -36,17 +36,17 @@ extern YYSTYPE yylval;
 %%
 
 ["][^"]*["]            {
-       yylval.string = strdup(yytext + 1);
-       yylval.string[strlen(yylval.string) - 1] = 0;
+       yylval.node.string = strdup(yytext + 1);
+       yylval.node.string[strlen(yylval.node.string) - 1] = 0;
        return (STRING);
 }
 ['][^']*[']            {
-       yylval.string = strdup(yytext + 1);
-       yylval.string[strlen(yylval.string) - 1] = 0;
+       yylval.node.string = strdup(yytext + 1);
+       yylval.node.string[strlen(yylval.node.string) - 1] = 0;
        return (STRING);
 }
 [a-zA-Z0-9_\-]+                {
-       yylval.ident = strdup(yytext);
+       yylval.node.ident = strdup(yytext);
        return (IDENTIFIER);
 }
 [\(\),]                        {
index fe279a9928a624e8c6cf7d666cd45d3747e09cac..2f1a5057c62702d1563eede3da7c5db95c15f7c3 100644 (file)
 %start command
 
 %union {
-       char* string;
-       char* ident;
+       struct Node {
+               char* string;
+               char* ident;
+       } node;
 }
 
 %%
 
 argument
        : STRING {
-               printf("%s\n", $<string>1);
+               printf("%s\n", $<node>1.string);
        }
        ;
 
@@ -61,10 +63,10 @@ arguments
 
 command
        : IDENTIFIER SPACE '(' arguments ')' {
-               printf("%s\n", $<ident>1);
+               printf("%s\n", $<node>1.ident);
        }
        | IDENTIFIER '(' arguments ')' {
-               printf("%s\n", $<ident>1);
+               printf("%s\n", $<node>1.ident);
        }
        ;
 
index a588b8b6a82cc03ba89994cc329cb91faa1dda5e..0cf57d2c59ca388043722578ba9371d2e2ebfd8c 100644 (file)
@@ -42,19 +42,7 @@ extern "C" {
 
 #include <stdbool.h>
 
-enum __token { __DW_METHOD = 0, __DW_VALUE };
-
-struct __dw_token {
-       char* name;
-       int type;
-       bool error;
-       int errnum;
-       struct __dw_token** token;
-};
-
-struct __dw_token* __dw_parser_parse(const char* str, bool top);
-void __dw_parser_free(struct __dw_token* token);
-void __dw_parser_print(struct __dw_token* token, int depth);
+int __dw_parser_parse(const char* str, bool top);
 
 #ifdef __cplusplus
 }
index 508b517ceb104fbe243345ad23524c339b45ea62..4509696ae760fc0bf06d01280616df75eed6e29a 100644 (file)
 #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;
 }
 
index c36634898cb8c8d6bc024913268a3ca7f664f6c4..866dceab17bf33d2fdb51d57c7ec1987ac2aaffc 100644 (file)
 #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);
 }
index 173847b3fb6f0511549cfbc987c57d804f622d2c..723aa2b682839e443afb1697f094e34d60427f33 100644 (file)
@@ -10,7 +10,7 @@ RANLIB = ranlib
 WINDRES = windres
 CFLAGS = -g -std=c99 -D_DEFAULT_SOURCE
 LDFLAGS = -L`pwd`/Library
-YFLAGS = -d
+YFLAGS = -d -t
 LFLAGS =
 LIBS =
 LIB_PREFIX = lib