free(spaces);
}
-void print_recursive(struct __dw_token* token, int depth){
+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){
+ if(token->token != NULL) {
for(i = 0; token->token[i] != NULL; i++) print_recursive(token->token[i], depth + 1);
}
}
banner = false;
} else if(strcmp(argv[i], "--nolog") == 0 || strcmp(argv[i], "-NL") == 0) {
log = false;
- } else if(strcmp(argv[i], "-f") == 0){
+ } else if(strcmp(argv[i], "-f") == 0) {
fprog = argv[i + 1];
i++;
} else {
printf("\x1b[2J\x1b[1;1H");
fflush(stdout);
}
- if(banner){
+ if(banner) {
printf("DataWorks version %s %s %s\n", dataworks_get_version(), dataworks_get_compile_date(), dataworks_get_platform());
if(dataworks_get_endian() == 'L') {
printf("This system is little-endian.\n");
printf("All rights reserved.\n");
}
if(create) {
- if(log){
+ if(log) {
printf("\n");
printf("Creating the database: %s\n", fname);
}
if(log) printf("Created successfully.\n");
}
}
- if(log){
+ if(log) {
printf("\n");
printf("Opening the database: %s\n", fname);
}
dataworks_database_close(db);
return 1;
}
- if(log){
+ if(log) {
time_t mtime = (time_t)dataworks_database_get_mtime(db);
struct tm* tm = localtime(&mtime);
char mtimestr[256];
strftime(mtimestr, 255, "%a %b %d %H:%M:%S %Z %Y", tm);
printf("Opened the database (Version %d, Modified at %s).\n", dataworks_database_get_version(db), mtimestr);
printf("\n");
- if(fprog == NULL){
+ if(fprog == NULL) {
printf("Type a command (.help) for the help.\n");
printf("\n");
}
char ch;
char prompt = '.';
FILE* fp = stdin;
- if(fprog != NULL){
+ if(fprog != NULL) {
fp = fopen(fprog, "r");
- if(fp == NULL){
+ if(fp == NULL) {
printf("Could not open the program file.\n");
return 1;
}
}
- if(fprog == NULL){
+ if(fprog == NULL) {
printf("%c ", prompt);
fflush(stdout);
}
linebuf = __dw_strcat(tmp, buf);
free(tmp);
int i;
- while(true){
- for(i = 0; linebuf[i] != 0; i++){
- if(linebuf[i] == ';'){
+ while(true) {
+ for(i = 0; linebuf[i] != 0; i++) {
+ if(linebuf[i] == ';') {
char* line = malloc(i + 1);
line[i] = 0;
memcpy(line, linebuf, i);
} else {
print_recursive(token, 0);
}
+ __dw_parser_free(token);
} else {
printf("Parser returned NULL. Help!\n");
}
free(line);
- if(strlen(linebuf) > 0){
+ if(strlen(linebuf) > 0) {
char* newbuf = malloc(strlen(linebuf) - i);
newbuf[strlen(linebuf) - i - 1] = 0;
memcpy(newbuf, linebuf + i + 1, strlen(linebuf) - i - 1);
break;
}
}
- if(fprog == NULL){
+ if(fprog == NULL) {
printf("%c ", prompt);
fflush(stdout);
}
if(str[i] == '(') brace++;
if(str[i] == ')') brace--;
if(brace > 0) {
- if(str[i] == ','){
+ if(str[i] == ',') {
i++;
- for(; str[i] != 0 && (str[i] == '\t' || str[i] == ' '); i++);
+ for(; str[i] != 0 && (str[i] == '\t' || str[i] == ' '); i++)
+ ;
i--;
}
char* tmp = br;
free(tmp);
} else {
int j;
- for(j = 0; buf[j] != 0; j++){
- if(buf[j] != ' ' && buf[j] != '\t'){
+ 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'){
+ 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'){
+ 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'){
+ for(j = strlen(br) - 1; j >= 0; j--) {
+ if(br[j] != ' ' && br[j] != '\t') {
br[j + 1] = 0;
break;
}
}
- if(buf[0] == ','){
+ if(buf[0] == ',') {
char* newbuf = __dw_strdup(buf + 1);
free(buf);
buf = newbuf;
char* comma = malloc(1);
comma[0] = 0;
int intbrace = 0;
- for(k = 0;; k++){
+ for(k = 0;; k++) {
char c = br[k];
- if(c == '(' || c == ')' || intbrace > 0){
+ 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 == ','){
+ } else if(c == 0 || c == ',') {
j = 0;
- if(token->token != NULL){
- for(j = 0; token->token[j] != NULL; j++);
+ 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++){
+ if(token->token != NULL) {
+ for(j = 0; token->token[j] != NULL; j++) {
newtokens[j] = token->token[j];
}
free(token->token);
comma = malloc(1);
comma[0] = 0;
if(c == 0) break;
- }else{
+ } else {
cbuf[0] = c;
char* tmp = comma;
comma = __dw_strcat(tmp, cbuf);
buf[0] = 0;
}
} else if(dq) {
- if(str[i] == '"'){
+ if(str[i] == '"') {
dq = !dq;
- }else{
+ } else {
char* tmp = buf;
buf = __dw_strcat(tmp, cbuf);
free(tmp);
free(tmp);
}
}
- if(!has_brace){
+ if(!has_brace) {
token->type = __DW_VALUE;
token->name = __dw_strdup(buf);
}
free(buf);
return token;
}
+
+void __dw_parser_free(struct __dw_token* token) {
+ 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);
+}