Fixing issues.

Using correct headers and functions for linux.
Fixed some memory allocation/free issues.
This commit is contained in:
2018-06-13 21:01:59 +01:00
parent 9af2f7bb45
commit 1c74b586fd
4 changed files with 17 additions and 10 deletions

View File

@@ -1,6 +1,8 @@
#include <stdio.h> #include <stdio.h>
#include <stdbool.h> #include <stdbool.h>
#include <mem.h> #include <stdlib.h>
#include <string.h>
#include <strings.h>
#include "InputBuffer.h" #include "InputBuffer.h"
#include "scanner.h" #include "scanner.h"
@@ -62,7 +64,7 @@ int main() {
prompt(); prompt();
read_input(buffer); read_input(buffer);
if (strcmpi(buffer->buffer, ".exit") == 0) { if (strcasecmp(buffer->buffer, ".exit") == 0) {
break; break;
} else { } else {
parse_input(buffer->buffer); parse_input(buffer->buffer);

View File

@@ -32,7 +32,7 @@ char *parser_node_type_to_str(ParserNodeType nodeType) {
} }
ParserNode *new_parser_node(ParserNodeType type, ScannerToken *token) { ParserNode *new_parser_node(ParserNodeType type, ScannerToken *token) {
ParserNode *node = malloc(sizeof(ScannerToken)); ParserNode *node = malloc(sizeof(ParserNode));
node->parent = NULL; node->parent = NULL;
node->type = type; node->type = type;
node->phase = 0; node->phase = 0;
@@ -144,7 +144,7 @@ StatementList *parser_node_convert(ParserNode *node) {
case NODE_DROP_STMT: { case NODE_DROP_STMT: {
DropStmt *drop= new_drop_stmt(); DropStmt *drop= new_drop_stmt();
append_statement_list(list, new_statement(STMT_DROP, drop)); append_statement_list(list, new_statement(STMT_DROP, drop));
drop->tableName = (child->children[0]->token->valueStr); drop->tableName = strdup(child->children[0]->token->valueStr);
} }
break; break;
default: default:
@@ -717,7 +717,7 @@ void parser_set_error(Parser *parser, char *err, ScannerToken *token) {
parser->status = PARSESTATE_ERROR; parser->status = PARSESTATE_ERROR;
if (token != NULL) { if (token != NULL) {
char *errMsg = calloc(128, sizeof(char)); char *errMsg = calloc(128, sizeof(char));
snprintf(errMsg, 128, "[%d:%d] %s", token->lineNo, token->linePos, err); snprintf(errMsg, 128, "[%ld:%ld] %s", token->lineNo, token->linePos, err);
parser->errMsg = strdup(errMsg); parser->errMsg = strdup(errMsg);
free(errMsg); free(errMsg);
} else { } else {
@@ -743,7 +743,7 @@ void parser_print_node_tree(ParserNode *node, size_t indent) {
printf("<%s>", node->token->valueStr); printf("<%s>", node->token->valueStr);
break; break;
case T_NUMBER: case T_NUMBER:
printf("<%lld>", node->token->valueInt); printf("<%ld>", node->token->valueInt);
break; break;
default: default:
break; break;

View File

@@ -8,7 +8,7 @@
#include <stddef.h> #include <stddef.h>
#include <stdbool.h> #include <stdbool.h>
#include "scanner.h" #include "scanner.h"
#include "sql.h" #include "SQL.h"
#define PARSE_STATUS_LIST \ #define PARSE_STATUS_LIST \
X(PARSESTATE_NONE) \ X(PARSESTATE_NONE) \

View File

@@ -7,6 +7,7 @@
#include <ctype.h> #include <ctype.h>
#include <stdbool.h> #include <stdbool.h>
#include <stdio.h> #include <stdio.h>
#include <stdlib.h>
#include "scanner.h" #include "scanner.h"
char* scanner_token_type_to_str(ScannerTokenType tokenType) { char* scanner_token_type_to_str(ScannerTokenType tokenType) {
@@ -21,7 +22,11 @@ char* scanner_token_type_to_str(ScannerTokenType tokenType) {
ScannerToken *new_scanner_token() { ScannerToken *new_scanner_token() {
ScannerToken *result = malloc(sizeof(ScannerToken)); ScannerToken *result = malloc(sizeof(ScannerToken));
clear_scanner_token(result); result->type = T_NONE;
result->valueInt = 0;
result->valueStr = NULL;
result->lineNo = 0;
result->linePos = 0;
return result; return result;
} }
@@ -38,7 +43,7 @@ void clear_scanner_token(ScannerToken *result) {
void free_scanner_token(ScannerToken *result) { void free_scanner_token(ScannerToken *result) {
if (result->valueStr != NULL) { if (result->valueStr != NULL) {
free(result); free(result->valueStr);
} }
free(result); free(result);
} }
@@ -368,7 +373,7 @@ 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, "[%d:%d]: %s", scanner->lineNo, scanner->linePos, errText); snprintf(errMsg, 128, "[%ld:%ld]: %s", scanner->lineNo, scanner->linePos, errText);
if (scanner->errMsg != NULL) { if (scanner->errMsg != NULL) {
free(scanner->errMsg); free(scanner->errMsg);