Skip to content

Commit

Permalink
Merge pull request #365 from andrey-terekhov/macro-fixes
Browse files Browse the repository at this point in the history
Macro fixes
  • Loading branch information
Victor-Y-Fadeev authored Feb 16, 2023
2 parents f4c012e + 60b0081 commit ef441d5
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 8 deletions.
10 changes: 8 additions & 2 deletions libs/macro/computer.c
Original file line number Diff line number Diff line change
Expand Up @@ -305,9 +305,9 @@ static inline int computer_const_number(computer *const comp, const item_t pos,
*/


computer computer_create(location *const loc, universal_io *const io)
computer computer_create(location *const loc, universal_io *const io, const char *const directive)
{
return (computer) { .loc = loc_copy(loc), .io = io
return (computer) { .loc = loc_copy(loc), .io = io, .directive = directive
, .numbers = stack_create(MAX_EXPRESSION_DAPTH)
, .operators = stack_create(MAX_EXPRESSION_DAPTH)
, .was_number = false };
Expand Down Expand Up @@ -396,6 +396,12 @@ item_t computer_pop_result(computer *const comp)
return 0;
}

if (stack_size(&comp->numbers) == 0)
{
computer_error(comp, ITEM_MAX, DIRECTIVE_NO_EXPRESSION, comp->directive);
return 0;
}

if (!comp->was_number)
{
char token[3];
Expand Down
5 changes: 4 additions & 1 deletion libs/macro/computer.h
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ typedef struct computer

location loc; /**< Directive location */
universal_io *io; /**< IO for location assembly */
const char *directive; /**< Directive name for error emitting */

bool was_number; /**< Set, if last push is number */
} computer;
Expand All @@ -83,10 +84,12 @@ typedef struct computer
* Create computer structure
*
* @param loc Default location
* @param io Location IO
* @param directive Directive name
*
* @return Computer structure
*/
computer computer_create(location *const loc, universal_io *const io);
computer computer_create(location *const loc, universal_io *const io, const char *const directive);


/**
Expand Down
18 changes: 13 additions & 5 deletions libs/macro/parser.c
Original file line number Diff line number Diff line change
Expand Up @@ -439,13 +439,17 @@ static inline char32_t skip_macro(parser *const prs, const keyword_t keyword)
if (utf8_is_letter(character))
{
const char *value = storage_get_by_index(prs->stg, storage_search(prs->stg, prs->io));
if (value != NULL)
if (value == NULL)
{
uni_printf(prs->io, MASK_ARGUMENT "%s", value);
uni_printf(prs->io, "%s", storage_last_read(prs->stg));
}
else if (keyword == KW_IFDEF || keyword == KW_IFNDEF)
{
uni_printf(prs->io, MASK_TOKEN_PASTE "%s", value);
}
else
{
uni_printf(prs->io, "%s", storage_last_read(prs->stg));
uni_printf(prs->io, MASK_ARGUMENT "%s", value);
}
}
else if (character == '\'' || character == '"' || (character == '<' && keyword == KW_INCLUDE))
Expand Down Expand Up @@ -1460,18 +1464,22 @@ static inline bool parse_token(parser *const prs, computer *const comp, const si
static item_t parse_expression(parser *const prs)
{
location loc = parse_location(prs);
char directive[MAX_KEYWORD_SIZE];
sprintf(directive, "%s", storage_last_read(prs->stg));

char32_t character = skip_until(prs, false);
if (character == '\n' || character == (char32_t)EOF)
{
parser_error(prs, &loc, DIRECTIVE_NO_EXPRESSION, storage_last_read(prs->stg));
parser_error(prs, &loc, DIRECTIVE_NO_EXPRESSION, directive);
return 0;
}

universal_io *origin_io = prs->io;
location *origin_loc = prs->loc;
location *origin_prev = prs->prev;

computer comp = prs->prev == NULL ? computer_create(&loc, prs->io) : computer_create(prs->prev, NULL);
computer comp = prs->prev == NULL ? computer_create(&loc, prs->io, directive)
: computer_create(prs->prev, NULL, directive);
size_t position = in_get_position(prs->io);
universal_io out = io_create();
char *buffer = NULL;
Expand Down

0 comments on commit ef441d5

Please sign in to comment.