From: nishi Date: Tue, 4 Jun 2024 02:57:18 +0000 (+0000) Subject: fix fgetc X-Git-Url: http://10.10.0.4:5575/?a=commitdiff_plain;h=a6bdc9fbd9acf8d69cb7e99ef1668b97a0d7eeb5;p=dataworks.git fix fgetc git-svn-id: file:///raid/svn-main/nishi-dataworks/trunk@140 d4a5a174-5a4a-5b4b-b672-37683c10d7d5 --- diff --git a/Client/main.c b/Client/main.c index 8b81c6f..1f5479f 100644 --- a/Client/main.c +++ b/Client/main.c @@ -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) { diff --git a/Grammar/dw.l b/Grammar/dw.l index 99c5217..fb8cebd 100644 --- a/Grammar/dw.l +++ b/Grammar/dw.l @@ -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); } [\(\),] { diff --git a/Grammar/dw.y b/Grammar/dw.y index fe279a9..2f1a505 100644 --- a/Grammar/dw.y +++ b/Grammar/dw.y @@ -35,15 +35,17 @@ %start command %union { - char* string; - char* ident; + struct Node { + char* string; + char* ident; + } node; } %% argument : STRING { - printf("%s\n", $1); + printf("%s\n", $1.string); } ; @@ -61,10 +63,10 @@ arguments command : IDENTIFIER SPACE '(' arguments ')' { - printf("%s\n", $1); + printf("%s\n", $1.ident); } | IDENTIFIER '(' arguments ')' { - printf("%s\n", $1); + printf("%s\n", $1.ident); } ; diff --git a/Library/dw_parser.h b/Library/dw_parser.h index a588b8b..0cf57d2 100644 --- a/Library/dw_parser.h +++ b/Library/dw_parser.h @@ -42,19 +42,7 @@ extern "C" { #include -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 } diff --git a/Library/exec.c b/Library/exec.c index 508b517..4509696 100644 --- a/Library/exec.c +++ b/Library/exec.c @@ -35,241 +35,13 @@ #include #include -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; } diff --git a/Library/parser.c b/Library/parser.c index c366348..866dcea 100644 --- a/Library/parser.c +++ b/Library/parser.c @@ -36,177 +36,13 @@ #include #include -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); } diff --git a/common-decl.mk b/common-decl.mk index 173847b..723aa2b 100644 --- a/common-decl.mk +++ b/common-decl.mk @@ -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