minor updates

This commit is contained in:
2018-06-06 23:05:58 +01:00
parent 8b7a0fba4e
commit cb262733ed
3 changed files with 17 additions and 20 deletions

View File

@@ -27,13 +27,12 @@ void parse_input(char *input) {
Scanner_Result *result = NULL; Scanner_Result *result = NULL;
while ((result = scanner_next_token(scanner, result)) != NULL) { while ((result = scanner_next_token(scanner, result)) != NULL) {
if (result->token == T_STRING) { 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) { } 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) { } else if (result->token == T_NUMBER) {
printf("Found Number %lld\n", result->value_int); printf("Found Number %lld\n", result->valueInt);
} else { } else {
printf("Found Token: %d\n", result->token); printf("Found Token: %d\n", result->token);
} }
@@ -41,9 +40,9 @@ void parse_input(char *input) {
if (scanner->state == SCANSTATE_ERROR) { if (scanner->state == SCANSTATE_ERROR) {
if (scanner->errMsg != NULL) { if (scanner->errMsg != NULL) {
printf("%s\n", scanner->errMsg); fprintf(stderr, "%s\n", scanner->errMsg);
} else { } else {
printf("Parse Error!"); fprintf(stderr, "Parse Error!\n");
} }
} }
@@ -52,11 +51,10 @@ void parse_input(char *input) {
int main() { int main() {
#if defined(_WIN32) || defined(WIN32) #if defined(_WIN32) || defined(WIN32)
//Fix issues with debugging on windows
setbuf(stdout, 0); setbuf(stdout, 0);
setbuf(stderr, 0); setbuf(stderr, 0);
#endif #endif
InputBuffer *buffer = input_buffer_new(); InputBuffer *buffer = input_buffer_new();
while (true) { while (true) {

View File

@@ -17,15 +17,15 @@ Scanner_Result *new_scanner_result() {
void clear_scanner_result(Scanner_Result *result) { void clear_scanner_result(Scanner_Result *result) {
result->token = T_NONE; result->token = T_NONE;
result->value_int = 0; result->valueInt = 0;
if (result->value_str != NULL) { if (result->valueStr != NULL) {
free(result->value_str); free(result->valueStr);
} }
result->value_str = NULL; result->valueStr = NULL;
} }
void free_scanner_result(Scanner_Result *result) { void free_scanner_result(Scanner_Result *result) {
if (result->value_str != NULL) { if (result->valueStr != NULL) {
free(result); free(result);
} }
free(result); free(result);
@@ -146,7 +146,7 @@ Scanner_Result *scanner_next_token(Scanner *scanner, Scanner_Result *result) {
} }
result->token = T_NUMBER; result->token = T_NUMBER;
//convert number //convert number
result->value_int = (uint64_t) strtol(intInput, NULL, 10); result->valueInt = (uint64_t) strtol(intInput, NULL, 10);
return result; 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) == '_')); } while ((isalnum(scanner_peek_char(scanner)) || scanner_peek_char(scanner) == '_'));
result->token = T_IDENTIFIER; result->token = T_IDENTIFIER;
result->value_str = strdup(ident); result->valueStr = strdup(ident);
free(ident); free(ident);
return result; return result;
} }
@@ -231,7 +231,7 @@ bool scanner_read_string(Scanner *scanner, Scanner_Result *result, char quoteTyp
} else if (next == quoteType) { } else if (next == quoteType) {
//end of string //end of string
result->token = T_STRING; result->token = T_STRING;
result->value_str = strdup(str); result->valueStr = strdup(str);
free(str); free(str);
return true; return true;
} }
@@ -295,12 +295,11 @@ void scanner_set_error(Scanner *scanner, const char *errText) {
//Create error msg //Create error msg
char *errMsg = malloc(sizeof(char) * 128); 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) { if (scanner->errMsg != NULL) {
free(scanner->errMsg); free(scanner->errMsg);
} }
scanner->state = SCANSTATE_ERROR; scanner->state = SCANSTATE_ERROR;
scanner->errMsg = errMsg; scanner->errMsg = errMsg;
printf(scanner->errMsg);
} }

View File

@@ -45,8 +45,8 @@ typedef enum Scanner_Token_t Scanner_Token;
struct Scanner_Result_t { struct Scanner_Result_t {
Scanner_Token token; Scanner_Token token;
char *value_str; char *valueStr;
uint64_t value_int; uint64_t valueInt;
}; };
typedef struct Scanner_Result_t Scanner_Result; typedef struct Scanner_Result_t Scanner_Result;