crepl

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

commit c12ed3e0974d52dd93162ce5ba0538a292840d1d
parent 2dc0d6830530017506ff961a6a6ad2f3b530cf98
Author: Demonstrandum <moi@knutsen.co>
Date:   Wed, 24 Jun 2020 17:34:08 +0100

Correct type checking.

Diffstat:
Msrc/displays.c | 5+++++
Msrc/execute.c | 10++++++----
2 files changed, 11 insertions(+), 4 deletions(-)

diff --git a/src/displays.c b/src/displays.c @@ -87,8 +87,13 @@ char *display_datavalue(const DataValue *data) // Safe bet. char *string = malloc(sizeof(char) * 512); + if (data == NULL) + return "internal-null-pointer"; + switch (data->type) { case T_NUMBER: { + if (data->value == NULL) + return "number-with-null-value"; NumberNode *num = data->value; // TODO: Handle more than just INT type. sprintf(string, "%ld", num->value.i); diff --git a/src/execute.c b/src/execute.c @@ -68,12 +68,14 @@ DataValue *wrap_data(DataType type, void *value) void *type_check(char *function_name, ParamPos pos, DataType type, DataValue *value) { - if (value->type == type) { + if (value != NULL + && value->value != NULL + && value->type == type) return value->value; - } + ERROR_TYPE = TYPE_ERROR; - sprintf(ERROR_MSG, "Wrong type for %s of `%s' operation," - " needed type of `%s'.", + sprintf(ERROR_MSG, "Wrong type for %s of `%s' operation,\n" + " needed type of `%s'.", display_parampos(pos), function_name, display_datatype(type));