From 45898bb95fc72008e768f8cb0c426d36a271854b Mon Sep 17 00:00:00 2001 From: Eugen Wissner Date: Thu, 30 Jan 2025 23:09:51 +0100 Subject: [PATCH] Mark @ nodes addressable --- README.md | 2 +- example.elna | 50 ++++++++++++++++++++++++++++++++++++++++++--- gcc/elna-generic.cc | 9 +++++--- source/ast.cc | 2 +- source/lexer.ll | 26 +++++++++++------------ 5 files changed, 68 insertions(+), 21 deletions(-) diff --git a/README.md b/README.md index ac33bce..bbec671 100644 --- a/README.md +++ b/README.md @@ -61,7 +61,7 @@ statement_list = statement {";" statement }; condition = "odd" expression | expression ("="|"#"|"<"|"<="|">"|">=") expression; -comparison_operator = "=", "/=", "<", ">", "<=", ">="; +comparison_operator = "=", "<>", "<", ">", "<=", ">="; unary_prefix = "not", "@"; expression = logical_operand { ("and" | "or") logical_operand }; diff --git a/example.elna b/example.elna index 5302c62..67fa872 100644 --- a/example.elna +++ b/example.elna @@ -27,7 +27,7 @@ const TOKEN_SEMICOLON = 40, TOKEN_DOT = 41, TOKEN_COMMA = 42, TOKEN_PLUS = 43, TOKEN_MINUS = 44, TOKEN_MULTIPLICATION = 45, TOKEN_DIVISION = 46, TOKEN_REMAINDER = 47, TOKEN_ASSIGNMENT = 48, TOKEN_COLON = 49, TOKEN_HAT = 50, - TOKEN_AT = 51; + TOKEN_AT = 51, TOKEN_COMMENT = 52, TOKEN_INTEGER = 53; (* External procedures. @@ -51,6 +51,8 @@ proc strncpy(dst: pointer to Char, src: pointer to Char, dsize: Word): pointer t proc strncpy(dst: pointer to Char, src: pointer to Char, dsize: Word): pointer to Char; extern; proc strlen(ptr: pointer to Char): Word; extern; +proc strtol(nptr: pointer to Char, endptr: pointer to pointer to Char, base: Int): Int; extern; + proc exit(code: Int); extern; (* @@ -185,6 +187,22 @@ begin return input end; +proc lex_comment(input: pointer to Char): pointer to Char; +var + current: pointer to Char, + next: pointer to Char; +begin + while input^ <> '\0' do + next := input + 1; + + if input^ = '*' and next^ = ')' then + return next + 1 + end; + input := input + 1 + end; + return nil +end; + proc print_tokens(tokens: pointer to Token, tokens_size: Int); var current_token: pointer to Token, @@ -300,6 +318,12 @@ begin write_s("^") elsif current_token^.kind = TOKEN_AT then write_s("@") + elsif current_token^.kind = TOKEN_COMMENT then + write_s("COMMENT") + elsif current_token^.kind = TOKEN_INTEGER then + write_c('<'); + write_i(current_token^.value.int_value); + write_c('>') else write_s("UNKNOWN<"); write_i(current_token^.kind); @@ -411,10 +435,30 @@ begin current_token^ := categorize_identifier(input_pointer, token_length); + input_pointer := token_end + elsif is_digit(input_pointer^) then + token_end := cast(nil as pointer to Char); + current_token^.value.int_value := strtol(input_pointer, @token_end, 10); + current_token^.kind := TOKEN_INTEGER; input_pointer := token_end elsif input_pointer^ = '(' then - current_token^.kind := TOKEN_LEFT_PAREN; - input_pointer := input_pointer + 1 + input_pointer := input_pointer + 1; + if input_pointer^ = '*' then + token_end := lex_comment(input_pointer + 1); + + if token_end <> cast(nil as pointer to Char) then + token_length := cast(token_end as Int) - cast(input_pointer as Int); + current_token^.value.string_value := cast(calloc(token_length + 1, 1) as pointer to Char); + strncpy(current_token^.value.string_value, input_pointer, token_length); + current_token^.kind := TOKEN_COMMENT; + + input_pointer := token_end + else + current_token^.kind := 0 + end + else + current_token^.kind := TOKEN_LEFT_PAREN + end elsif input_pointer^ = ')' then current_token^.kind := TOKEN_RIGHT_PAREN; input_pointer := input_pointer + 1 diff --git a/gcc/elna-generic.cc b/gcc/elna-generic.cc index 7e10b1b..1e68825 100644 --- a/gcc/elna-generic.cc +++ b/gcc/elna-generic.cc @@ -9,6 +9,7 @@ #include "realmpfr.h" #include "stor-layout.h" #include "varasm.h" +#include "fold-const.h" #include namespace elna @@ -393,9 +394,11 @@ namespace gcc switch (expression->operation()) { case source::unary_operator::reference: - this->current_expression = build1_loc(get_location(&expression->position()), ADDR_EXPR, - build_pointer_type_for_mode(TREE_TYPE(this->current_expression), VOIDmode, true), - this->current_expression); + TREE_ADDRESSABLE(this->current_expression) = 1; + this->current_expression = build_fold_addr_expr_with_type_loc(get_location(&expression->position()), + this->current_expression, + build_pointer_type_for_mode(TREE_TYPE(this->current_expression), VOIDmode, true)); + TREE_NO_TRAMPOLINE(this->current_expression) = 1; break; case source::unary_operator::negation: this->current_expression = build1_loc(get_location(&expression->position()), TRUTH_NOT_EXPR, diff --git a/source/ast.cc b/source/ast.cc index 5395ee3..2187a6b 100644 --- a/source/ast.cc +++ b/source/ast.cc @@ -962,7 +962,7 @@ namespace source case binary_operator::equals: return "="; case binary_operator::not_equals: - return "/="; + return "<>"; case binary_operator::less: return "<"; case binary_operator::less_equal: diff --git a/source/lexer.ll b/source/lexer.ll index 02cf0a5..66faaa5 100644 --- a/source/lexer.ll +++ b/source/lexer.ll @@ -149,7 +149,7 @@ sizeof { return yy::parser::make_CHARACTER(std::string(yytext, 1, 1), this->location); } } -'\\x[0-9a-fA-F]{1,2}' { +'\\x[0-9a-fA-F]{1,2}' { char character = static_cast(std::stoi(yytext + 3, nullptr, 16)); return yy::parser::make_CHARACTER(std::string(&character, 1), this->location); @@ -178,14 +178,14 @@ sizeof { std::size_t processed; char character = static_cast(std::stoi(current_position, &processed, 16)); if (processed == 0) - { - REJECT; - } - else - { + { + REJECT; + } + else + { current_position += processed - 1; result.push_back(character); - } + } } else if (*current_position == '\\') { @@ -193,13 +193,13 @@ sizeof { std::optional escape = source::escape_char(*current_position); if (escape.has_value()) - { + { result.push_back(escape.value()); - } - else - { - REJECT; - } + } + else + { + REJECT; + } } else {