diff options
-rw-r--r-- | Makefile | 3 | ||||
-rw-r--r-- | src/lexer.c | 24 |
2 files changed, 18 insertions, 9 deletions
@@ -1,6 +1,7 @@ TARGET := pipac SRC_DIR := src BUILD_DIR := build +CFLAGS := -Wall -Wextra -pedantic -std=c11 -ggdb SRCS := $(wildcard $(SRC_DIR)/*.c) OBJS := $(patsubst $(SRC_DIR)/%.c, $(BUILD_DIR)/%.o, $(SRCS)) @@ -9,7 +10,7 @@ OBJS := $(patsubst $(SRC_DIR)/%.c, $(BUILD_DIR)/%.o, $(SRCS)) all: $(TARGET) $(TARGET): $(BUILD_DIR) $(OBJS) - $(CC) $(OBJS) -o $(TARGET) $(CFLAGS) + $(CC) $(CFLAGS) $(OBJS) -o $(TARGET) $(BUILD_DIR): @mkdir -p $@ diff --git a/src/lexer.c b/src/lexer.c index 7b0206a..14165ae 100644 --- a/src/lexer.c +++ b/src/lexer.c @@ -55,7 +55,8 @@ lexer_next_token(lexer_t *lexer, token_t *token) lexer_drop_char(lexer); } token->kind = TOKEN_NUMBER; - token->value = strndup(lexer->src + begin, lexer->cur - begin); + token->value = (char *) malloc(sizeof(char) * (lexer->cur - begin + 1)); + strncpy(token->value, lexer->src + begin, lexer->cur - begin); token->row = lexer->row; token->col = begin - lexer->bol; return; @@ -68,7 +69,8 @@ lexer_next_token(lexer_t *lexer, token_t *token) lexer_drop_char(lexer); } token->kind = TOKEN_NAME; - token->value = strndup(lexer->src + begin, lexer->cur - begin); + token->value = (char *) malloc(sizeof(char) * (lexer->cur - begin + 1)); + strncpy(token->value, lexer->src + begin, lexer->cur - begin); token->row = lexer->row; token->col = begin - lexer->bol; return; @@ -76,7 +78,8 @@ lexer_next_token(lexer_t *lexer, token_t *token) if (lexer_is_not_eof(lexer) && lexer_current_char(lexer) == '(') { token->kind = TOKEN_OPAREN; - token->value = strdup("("); + token->value = (char *) malloc(sizeof(char) + 1); + strcpy(token->value, "("); token->row = lexer->row; token->col = lexer->cur - lexer->bol; lexer_drop_char(lexer); @@ -85,7 +88,8 @@ lexer_next_token(lexer_t *lexer, token_t *token) if (lexer_is_not_eof(lexer) && lexer_current_char(lexer) == ')') { token->kind = TOKEN_CPAREN; - token->value = strdup(")"); + token->value = (char *) malloc(sizeof(char) + 1); + strcpy(token->value, ")"); token->row = lexer->row; token->col = lexer->cur - lexer->bol; lexer_drop_char(lexer); @@ -94,7 +98,8 @@ lexer_next_token(lexer_t *lexer, token_t *token) if (lexer_is_not_eof(lexer) && lexer_current_char(lexer) == ':') { token->kind = TOKEN_COLON; - token->value = strdup(":"); + token->value = (char *) malloc(sizeof(char) + 1); + strcpy(token->value, ":"); token->row = lexer->row; token->col = lexer->cur - lexer->bol; lexer_drop_char(lexer); @@ -103,7 +108,8 @@ lexer_next_token(lexer_t *lexer, token_t *token) if (lexer_is_not_eof(lexer) && lexer_current_char(lexer) == ';') { token->kind = TOKEN_SEMICOLON; - token->value = strdup(";"); + token->value = (char *) malloc(sizeof(char) + 1); + strcpy(token->value, ";"); token->row = lexer->row; token->col = lexer->cur - lexer->bol; lexer_drop_char(lexer); @@ -112,7 +118,8 @@ lexer_next_token(lexer_t *lexer, token_t *token) if (lexer_is_not_eof(lexer) && lexer_current_char(lexer) == '{') { token->kind = TOKEN_OCURLY; - token->value = strdup("{"); + token->value = (char *) malloc(sizeof(char) + 1); + strcpy(token->value, "{"); token->row = lexer->row; token->col = lexer->cur - lexer->bol; lexer_drop_char(lexer); @@ -121,7 +128,8 @@ lexer_next_token(lexer_t *lexer, token_t *token) if (lexer_is_not_eof(lexer) && lexer_current_char(lexer) == '}') { token->kind = TOKEN_CCURLY; - token->value = strdup("}"); + token->value = (char *) malloc(sizeof(char) + 1); + strcpy(token->value, "}"); token->row = lexer->row; token->col = lexer->cur - lexer->bol; lexer_drop_char(lexer); |