From fa640f13e8a231ec3a619cc360289828cc4fc114 Mon Sep 17 00:00:00 2001 From: Aryadev Chavali Date: Tue, 31 Oct 2023 20:38:03 +0000 Subject: parse_word deals with characters now Just takes the character literally as a number. --- asm/parser.c | 63 ++++++++++++++++++++++++++++++++++-------------------------- 1 file changed, 36 insertions(+), 27 deletions(-) (limited to 'asm') diff --git a/asm/parser.c b/asm/parser.c index 8292e39..9d8317d 100644 --- a/asm/parser.c +++ b/asm/parser.c @@ -72,39 +72,48 @@ data_type_t parse_data_type(const char *cstr, size_t length) perr_t parse_word(token_t token, word *ret) { - assert(token.type == TOKEN_LITERAL_NUMBER); - bool is_negative = token.str_size > 1 && token.str[0] == '-'; - word w = 0; - if (is_negative) + if (token.type == TOKEN_LITERAL_NUMBER) { - char *end = NULL; - // TODO: Make a standardised type of the same size as word in - // base.h - int64_t i = strtoll(token.str, &end, 0); - if (!(end && end[0] == '\0')) - return PERR_NOT_A_NUMBER; - else if (errno == ERANGE) + bool is_negative = token.str_size > 1 && token.str[0] == '-'; + word w = 0; + if (is_negative) { - errno = 0; - return PERR_INTEGER_OVERFLOW; + char *end = NULL; + // TODO: Make a standardised type of the same size as word in + // base.h + int64_t i = strtoll(token.str, &end, 0); + if (!(end && end[0] == '\0')) + return PERR_NOT_A_NUMBER; + else if (errno == ERANGE) + { + errno = 0; + return PERR_INTEGER_OVERFLOW; + } + // Copy bits, do not cast + memcpy(&w, &i, sizeof(w)); } - // Copy bits, do not cast - memcpy(&w, &i, sizeof(w)); - } - else - { - char *end = NULL; - w = strtoull(token.str, &end, 0); - if (!(end && end[0] == '\0')) - return PERR_NOT_A_NUMBER; - else if (errno == ERANGE) + else { - errno = 0; - return PERR_INTEGER_OVERFLOW; + char *end = NULL; + w = strtoull(token.str, &end, 0); + if (!(end && end[0] == '\0')) + return PERR_NOT_A_NUMBER; + else if (errno == ERANGE) + { + errno = 0; + return PERR_INTEGER_OVERFLOW; + } } + *ret = w; + return PERR_OK; } - *ret = w; - return PERR_OK; + else if (token.type == TOKEN_LITERAL_CHAR) + { + *ret = token.str[0]; + return PERR_OK; + } + else + return PERR_NOT_A_NUMBER; } perr_t parse_inst_with_type(token_stream_t *stream, inst_t *ret, -- cgit v1.2.3-13-gbd6f