From 03b933bc7a20f22d22c0d62bc49b3b9d5fb8a03d Mon Sep 17 00:00:00 2001 From: Dale Weiler Date: Fri, 1 Feb 2013 12:56:01 +0000 Subject: [PATCH] Work in progress ~ operator implemented as -1-x. --- lexer.c | 11 ++++++----- parser.c | 16 ++++++++++++++++ 2 files changed, 22 insertions(+), 5 deletions(-) diff --git a/lexer.c b/lexer.c index f345e47..e1cff47 100644 --- a/lexer.c +++ b/lexer.c @@ -1288,10 +1288,11 @@ int lex_do(lex_file *lex) } if (ch == '+' || ch == '-' || /* ++, --, +=, -= and -> as well! */ - ch == '>' || ch == '<' || /* <<, >>, <=, >= */ - ch == '=' || ch == '!' || /* ==, != */ - ch == '&' || ch == '|') /* &&, ||, &=, |= */ - { + ch == '>' || ch == '<' || /* <<, >>, <=, >= */ + ch == '=' || ch == '!' || /* ==, != */ + ch == '&' || ch == '|' || /* &&, ||, &=, |= */ + ch == '~' /* ~=, ~ */ + ) { lex_tokench(lex, ch); nextch = lex_getch(lex); @@ -1484,6 +1485,6 @@ int lex_do(lex_file *lex) return (lex->tok.ttype = ch); } - lexerror(lex, "unknown token"); + lexerror(lex, "unknown token: %c", lex->tok); return (lex->tok.ttype = TOKEN_ERROR); } diff --git a/parser.c b/parser.c index bfd8e84..1de2459 100644 --- a/parser.c +++ b/parser.c @@ -951,6 +951,7 @@ static bool parser_sy_apply_operator(parser_t *parser, shunt *sy) return false; case opid1('|'): case opid1('&'): + case opid1('~'): if (NotSameType(TYPE_FLOAT)) { compile_error(ctx, "invalid types used in expression: cannot perform bit operations between types %s and %s", type_name[exprs[0]->expression.vtype], @@ -1346,6 +1347,21 @@ static bool parser_sy_apply_operator(parser_t *parser, shunt *sy) asbinstore->keep_dest = true; out = (ast_expression*)asbinstore; break; + + case opid2('~', 'P'): + if (exprs[0]->expression.vtype != TYPE_FLOAT) { + ast_type_to_string(exprs[0], ty1, sizeof(ty1)); + compile_error(ast_ctx(exprs[0]), "invalid type for bit not: %s", ty1); + return false; + } + + if (ast_istype(exprs[0], ast_value) && asvalue[0]->cvq == CV_CONST) { + compile_error(ast_ctx(exprs[0]), "assignment to constant `%s`", asvalue[0]->name); + } + + out = (ast_expression*)ast_binary_new(ctx, INSTR_SUB_F, (ast_expression*)parser_const_float(parser, -1), exprs[0]); + break; + } #undef NotSameType -- 2.39.2