crepl

An intuitive calculator REPL.
git clone git://git.knutsen.co/crepl
Log | Files | Refs | README | LICENSE

commit c03f80677c5cbd132894a512172ce54548ed8cc9
parent 0f0ef3b11642e230165fafb8686ea0addf869485
Author: Demonstrandum <moi@knutsen.co>
Date:   Fri, 26 Jun 2020 12:31:26 +0100

Prevent some segfaults.

Diffstat:
M.gitignore | 1+
MMakefile | 1+
Msrc/execute.c | 4++++
Msrc/parse.c | 13++++++++++---
4 files changed, 16 insertions(+), 3 deletions(-)

diff --git a/.gitignore b/.gitignore @@ -9,3 +9,4 @@ crepl *.log desktop.ini .DS_Store +IDEAS.md diff --git a/Makefile b/Makefile @@ -13,6 +13,7 @@ endif all: clean $(TARGET) @printf "\033[1mBuilt \`$(TARGET)' successfully.\033[0m\n" +debug: CFLAGS := $(WARN) -Og debug: $(OBJS) $(CC) -Og -o $(TARGET) $(LINKS) $(OBJS) diff --git a/src/execute.c b/src/execute.c @@ -119,7 +119,11 @@ finished_search: // How to evaluate specific operators. DataValue *lhs = execute(ctx, stmt->node.binary.left); + if (lhs == NULL) + return NULL; DataValue *rhs = execute(ctx, stmt->node.binary.right); + if (rhs == NULL) + return NULL; // Numerical binary operations. if (strcmp(op, "+") == 0) { diff --git a/src/parse.c b/src/parse.c @@ -385,7 +385,10 @@ ParseNode *parse_infix(const ParseNode *left, unary->callee = node; unary->operand = left; unary->is_postfix = true; - } else { // Function call. + } else { // Function call, probably. + if (token->type == TT_OPERATOR) + return NULL; // Not a real operator, not a function. + unary->callee = left; // The minus one (- 1) makes function application right // associative, this is unconventional, and makes applications @@ -394,6 +397,8 @@ ParseNode *parse_infix(const ParseNode *left, // by juxtaposition. // e.g. 3 sin 2 => (3 (sin 2)) vs ((3 sin) 2) [<- error] unary->operand = parse_expr(rest, FUNCTION_PRECEDENCE - 1); + if (unary->operand == NULL) + return NULL; unary->is_postfix = false; } @@ -498,8 +503,10 @@ ParseNode *parse_expr(char **slice, u16 precedence) current_precedence = token_precedence(token_ahead); } - free_token((Token *)token_ahead); - free_token((Token *)token); + if (token_ahead != NULL) + free_token((Token *)token_ahead); + if (token != NULL) + free_token((Token *)token); return left; }