minor updates
This commit is contained in:
14
src/main.c
14
src/main.c
@@ -27,13 +27,12 @@ void parse_input(char *input) {
|
||||
Scanner_Result *result = NULL;
|
||||
|
||||
while ((result = scanner_next_token(scanner, result)) != NULL) {
|
||||
|
||||
if (result->token == T_STRING) {
|
||||
printf("Found String: %s\n", result->value_str);
|
||||
printf("Found String: '%s'\n", result->valueStr);
|
||||
} else if (result->token == T_IDENTIFIER) {
|
||||
printf("Found Identifier: %s\n", result->value_str);
|
||||
printf("Found Identifier: %s\n", result->valueStr);
|
||||
} else if (result->token == T_NUMBER) {
|
||||
printf("Found Number %lld\n", result->value_int);
|
||||
printf("Found Number %lld\n", result->valueInt);
|
||||
} else {
|
||||
printf("Found Token: %d\n", result->token);
|
||||
}
|
||||
@@ -41,9 +40,9 @@ void parse_input(char *input) {
|
||||
|
||||
if (scanner->state == SCANSTATE_ERROR) {
|
||||
if (scanner->errMsg != NULL) {
|
||||
printf("%s\n", scanner->errMsg);
|
||||
fprintf(stderr, "%s\n", scanner->errMsg);
|
||||
} else {
|
||||
printf("Parse Error!");
|
||||
fprintf(stderr, "Parse Error!\n");
|
||||
}
|
||||
}
|
||||
|
||||
@@ -52,11 +51,10 @@ void parse_input(char *input) {
|
||||
|
||||
int main() {
|
||||
#if defined(_WIN32) || defined(WIN32)
|
||||
//Fix issues with debugging on windows
|
||||
setbuf(stdout, 0);
|
||||
setbuf(stderr, 0);
|
||||
#endif
|
||||
|
||||
|
||||
InputBuffer *buffer = input_buffer_new();
|
||||
|
||||
while (true) {
|
||||
|
||||
@@ -17,15 +17,15 @@ Scanner_Result *new_scanner_result() {
|
||||
|
||||
void clear_scanner_result(Scanner_Result *result) {
|
||||
result->token = T_NONE;
|
||||
result->value_int = 0;
|
||||
if (result->value_str != NULL) {
|
||||
free(result->value_str);
|
||||
result->valueInt = 0;
|
||||
if (result->valueStr != NULL) {
|
||||
free(result->valueStr);
|
||||
}
|
||||
result->value_str = NULL;
|
||||
result->valueStr = NULL;
|
||||
}
|
||||
|
||||
void free_scanner_result(Scanner_Result *result) {
|
||||
if (result->value_str != NULL) {
|
||||
if (result->valueStr != NULL) {
|
||||
free(result);
|
||||
}
|
||||
free(result);
|
||||
@@ -146,7 +146,7 @@ Scanner_Result *scanner_next_token(Scanner *scanner, Scanner_Result *result) {
|
||||
}
|
||||
result->token = T_NUMBER;
|
||||
//convert number
|
||||
result->value_int = (uint64_t) strtol(intInput, NULL, 10);
|
||||
result->valueInt = (uint64_t) strtol(intInput, NULL, 10);
|
||||
return result;
|
||||
}
|
||||
|
||||
@@ -174,7 +174,7 @@ Scanner_Result *scanner_next_token(Scanner *scanner, Scanner_Result *result) {
|
||||
}
|
||||
} while ((isalnum(scanner_peek_char(scanner)) || scanner_peek_char(scanner) == '_'));
|
||||
result->token = T_IDENTIFIER;
|
||||
result->value_str = strdup(ident);
|
||||
result->valueStr = strdup(ident);
|
||||
free(ident);
|
||||
return result;
|
||||
}
|
||||
@@ -231,7 +231,7 @@ bool scanner_read_string(Scanner *scanner, Scanner_Result *result, char quoteTyp
|
||||
} else if (next == quoteType) {
|
||||
//end of string
|
||||
result->token = T_STRING;
|
||||
result->value_str = strdup(str);
|
||||
result->valueStr = strdup(str);
|
||||
free(str);
|
||||
return true;
|
||||
}
|
||||
@@ -295,12 +295,11 @@ void scanner_set_error(Scanner *scanner, const char *errText) {
|
||||
//Create error msg
|
||||
char *errMsg = malloc(sizeof(char) * 128);
|
||||
|
||||
snprintf(errMsg, 128, "Error [%d:%d]: %s", scanner->lineNo, scanner->linePos, errText);
|
||||
snprintf(errMsg, 128, "Error at line %d:%d %s", scanner->lineNo, scanner->linePos, errText);
|
||||
|
||||
if (scanner->errMsg != NULL) {
|
||||
free(scanner->errMsg);
|
||||
}
|
||||
scanner->state = SCANSTATE_ERROR;
|
||||
scanner->errMsg = errMsg;
|
||||
printf(scanner->errMsg);
|
||||
}
|
||||
@@ -45,8 +45,8 @@ typedef enum Scanner_Token_t Scanner_Token;
|
||||
|
||||
struct Scanner_Result_t {
|
||||
Scanner_Token token;
|
||||
char *value_str;
|
||||
uint64_t value_int;
|
||||
char *valueStr;
|
||||
uint64_t valueInt;
|
||||
};
|
||||
typedef struct Scanner_Result_t Scanner_Result;
|
||||
|
||||
|
||||
Reference in New Issue
Block a user