* [PATCH olang v1 2/3] lexer: add tilde token support
2024-10-09 21:18 [PATCH olang v1 0/3] implement unary operator for bitwise not Johnny Richard
2024-10-09 21:18 ` [PATCH olang v1 1/3] doc: add unary expression to language spec Johnny Richard
@ 2024-10-09 21:18 ` Johnny Richard
2024-10-09 21:18 ` [PATCH olang v1 3/3] parser: codegen(x86_64): add bitwise not unary op support Johnny Richard
2024-10-09 23:00 ` [PATCH olang v1 0/3] implement unary operator for bitwise not Carlos Maniero
3 siblings, 0 replies; 6+ messages in thread
From: Johnny Richard @ 2024-10-09 21:18 UTC (permalink / raw)
To: ~johnnyrichard/olang-devel; +Cc: Johnny Richard
The tilde token will be used for parsing bitwise-not unary operator.
Signed-off-by: Johnny Richard <johnny@johnnyrichard.com>
---
src/lexer.c | 6 +++++
src/lexer.h | 1 +
tests/olc/0033_unary_operator_bitwise_not.ol | 27 ++++++++++++++++++++
3 files changed, 34 insertions(+)
create mode 100644 tests/olc/0033_unary_operator_bitwise_not.ol
diff --git a/src/lexer.c b/src/lexer.c
index 5b4bbaa..9e012bd 100644
--- a/src/lexer.c
+++ b/src/lexer.c
@@ -258,6 +258,11 @@ lexer_next_token(lexer_t *lexer, token_t *token)
lexer_skip_char(lexer);
return;
}
+ case '~': {
+ lexer_init_char_value_token(lexer, token, TOKEN_TILDE);
+ lexer_skip_char(lexer);
+ return;
+ }
case '/': {
lexer_init_char_value_token(lexer, token, TOKEN_SLASH);
lexer_skip_char(lexer);
@@ -303,6 +308,7 @@ static char *token_kind_str_table[] = {
[TOKEN_DASH] = "-",
[TOKEN_STAR] = "*",
[TOKEN_SLASH] = "/",
+ [TOKEN_TILDE] = "~",
[TOKEN_EQ] = "=",
[TOKEN_CMP_EQ] = "==",
[TOKEN_BANG] = "!",
diff --git a/src/lexer.h b/src/lexer.h
index 8beaea8..774f619 100644
--- a/src/lexer.h
+++ b/src/lexer.h
@@ -81,6 +81,7 @@ typedef enum token_kind
TOKEN_DASH,
TOKEN_SLASH,
TOKEN_STAR,
+ TOKEN_TILDE,
TOKEN_LF,
TOKEN_OPAREN,
TOKEN_CPAREN,
diff --git a/tests/olc/0033_unary_operator_bitwise_not.ol b/tests/olc/0033_unary_operator_bitwise_not.ol
new file mode 100644
index 0000000..1891549
--- /dev/null
+++ b/tests/olc/0033_unary_operator_bitwise_not.ol
@@ -0,0 +1,27 @@
+# Copyright (C) 2024 olang mantainers
+#
+# This program is free software: you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation, either version 3 of the License, or
+# (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program. If not, see <https://www.gnu.org/licenses/>.
+
+fn main(): u32 {
+ var e: u32 = 4294967295
+ return ~e
+}
+
+# XTEST test_compile(exit_code=0)
+#
+# XTEST test_run_binary(exit_code=0)
+#
+# TEST test_contains_tokens WITH
+# ./0033_unary_operator_bitwise_not.ol:18:10: <~>
+# END
--
2.46.0
^ permalink raw reply [flat|nested] 6+ messages in thread
* [PATCH olang v1 3/3] parser: codegen(x86_64): add bitwise not unary op support
2024-10-09 21:18 [PATCH olang v1 0/3] implement unary operator for bitwise not Johnny Richard
2024-10-09 21:18 ` [PATCH olang v1 1/3] doc: add unary expression to language spec Johnny Richard
2024-10-09 21:18 ` [PATCH olang v1 2/3] lexer: add tilde token support Johnny Richard
@ 2024-10-09 21:18 ` Johnny Richard
2024-10-09 19:35 ` [olang/patches/.build.yml] build success builds.sr.ht
2024-10-09 23:00 ` [PATCH olang v1 0/3] implement unary operator for bitwise not Carlos Maniero
3 siblings, 1 reply; 6+ messages in thread
From: Johnny Richard @ 2024-10-09 21:18 UTC (permalink / raw)
To: ~johnnyrichard/olang-devel; +Cc: Johnny Richard
Currently, all compiler tests rely on the exit code returned by a binary
program, which restricts our testing to 8-bit unsigned numbers.
To effectively test the bitwise-not unary operation within a main
function, we utilize UINT32_MAX (4294967295), which represents a value
with all bits set. The result of applying the bitwise-not operation to 0
is then used as the exit code.
Signed-off-by: Johnny Richard <johnny@johnnyrichard.com>
---
src/ast.c | 14 ++++++++
src/ast.h | 22 ++++++++++++
src/checker.c | 7 ++++
src/codegen_linux_x86_64.c | 18 ++++++++++
src/parser.c | 35 ++++++++++++++++++++
src/pretty_print_ast.c | 22 +++++++++++-
tests/olc/0033_unary_operator_bitwise_not.ol | 16 +++++++--
7 files changed, 131 insertions(+), 3 deletions(-)
diff --git a/src/ast.c b/src/ast.c
index 79daca6..5a36ccb 100644
--- a/src/ast.c
+++ b/src/ast.c
@@ -117,6 +117,20 @@ ast_new_node_bin_op(arena_t *arena, token_loc_t loc, ast_binary_op_kind_t kind,
return node_bin_op;
}
+ast_node_t *
+ast_new_node_unary_op(arena_t *arena, token_loc_t loc, ast_unary_op_kind_t kind, ast_node_t *expr)
+{
+ ast_node_t *node_unary_op = (ast_node_t *)arena_alloc(arena, sizeof(ast_node_t));
+ assert(node_unary_op);
+
+ node_unary_op->kind = AST_NODE_UNARY_OP;
+ node_unary_op->loc = loc;
+ node_unary_op->as_unary_op.kind = kind;
+ node_unary_op->as_unary_op.expr = expr;
+
+ return node_unary_op;
+}
+
ast_node_t *
ast_new_node_literal_u32(arena_t *arena, token_loc_t loc, uint32_t value)
{
diff --git a/src/ast.h b/src/ast.h
index fccc303..ee94f57 100644
--- a/src/ast.h
+++ b/src/ast.h
@@ -36,6 +36,7 @@ typedef enum
AST_NODE_FN_CALL,
AST_NODE_VAR_DEF,
AST_NODE_BINARY_OP,
+ AST_NODE_UNARY_OP,
AST_NODE_VAR_ASSIGN_STMT,
AST_NODE_RETURN_STMT,
AST_NODE_IF_STMT,
@@ -148,6 +149,23 @@ typedef struct ast_binary_op
ast_node_t *rhs;
} ast_binary_op_t;
+typedef enum ast_unary_op_kind
+{
+ AST_UNARY_BITWISE_NOT,
+ AST_UNARY_LOGICAL_NOT,
+ AST_UNARY_NEGATIVE,
+ AST_UNARY_POSITIVE,
+ AST_UNARY_DEREFERENCE,
+ AST_UNARY_ADDRESSOF,
+} ast_unary_op_kind_t;
+
+typedef struct ast_unary_op
+{
+ ast_node_meta_t meta;
+ ast_unary_op_kind_t kind;
+ ast_node_t *expr;
+} ast_unary_op_t;
+
typedef struct ast_var_assign_stmt
{
ast_node_meta_t meta;
@@ -189,6 +207,7 @@ typedef union ast_node
ast_fn_call_t as_fn_call;
ast_var_definition_t as_var_def;
ast_binary_op_t as_bin_op;
+ ast_unary_op_t as_unary_op;
ast_literal_t as_literal;
ast_ref_t as_ref;
ast_block_t as_block;
@@ -218,6 +237,9 @@ ast_new_node_var_def(arena_t *arena, token_loc_t loc, string_view_t id, type_t *
ast_node_t *
ast_new_node_bin_op(arena_t *arena, token_loc_t loc, ast_binary_op_kind_t kind, ast_node_t *lhs, ast_node_t *rhs);
+ast_node_t *
+ast_new_node_unary_op(arena_t *arena, token_loc_t loc, ast_unary_op_kind_t kind, ast_node_t *expr);
+
ast_node_t *
ast_new_node_literal_u32(arena_t *arena, token_loc_t loc, uint32_t value);
diff --git a/src/checker.c b/src/checker.c
index 7a9a7b6..62d612f 100644
--- a/src/checker.c
+++ b/src/checker.c
@@ -177,6 +177,13 @@ populate_scope(checker_t *checker, scope_t *scope, ast_node_t *ast)
return;
}
+ case AST_NODE_UNARY_OP: {
+ ast_unary_op_t unary_op = ast->as_unary_op;
+
+ populate_scope(checker, scope, unary_op.expr);
+ return;
+ }
+
case AST_NODE_VAR_ASSIGN_STMT: {
ast_var_assign_stmt_t var_assign_stmt = ast->as_var_assign_stmt;
diff --git a/src/codegen_linux_x86_64.c b/src/codegen_linux_x86_64.c
index ac268ad..ae28aa5 100644
--- a/src/codegen_linux_x86_64.c
+++ b/src/codegen_linux_x86_64.c
@@ -530,6 +530,24 @@ codegen_linux_x86_64_emit_expression(codegen_x86_64_t *codegen, ast_node_t *expr
}
}
}
+
+ case AST_NODE_UNARY_OP: {
+ ast_unary_op_t unary_op = expr_node->as_unary_op;
+ switch (unary_op.kind) {
+ case AST_UNARY_BITWISE_NOT: {
+ size_in_bytes_t expr_bytes = codegen_linux_x86_64_emit_expression(codegen, unary_op.expr);
+
+ fprintf(codegen->out, " not %s\n", get_reg_for(REG_ACCUMULATOR, expr_bytes));
+
+ return expr_bytes;
+ }
+ default: {
+ assert(0 && "unsupported unary operation");
+ return 0;
+ }
+ }
+ }
+
default:
assert(0 && "unsupported expression");
}
diff --git a/src/parser.c b/src/parser.c
index 0bfa282..f712bfc 100644
--- a/src/parser.c
+++ b/src/parser.c
@@ -209,6 +209,27 @@ get_binary_op_precedence(token_kind_t kind)
}
}
+static ast_unary_op_kind_t
+token_kind_to_unary_op_kind(token_kind_t token_kind)
+{
+ switch (token_kind) {
+ case TOKEN_AND:
+ return AST_UNARY_ADDRESSOF;
+ case TOKEN_STAR:
+ return AST_UNARY_DEREFERENCE;
+ case TOKEN_PLUS:
+ return AST_UNARY_POSITIVE;
+ case TOKEN_DASH:
+ return AST_UNARY_NEGATIVE;
+ case TOKEN_TILDE:
+ return AST_UNARY_BITWISE_NOT;
+ case TOKEN_BANG:
+ return AST_UNARY_LOGICAL_NOT;
+ default:
+ assert(false && "unable to covert the token_kind_t to unary_op_kind_t");
+ }
+}
+
static ast_node_t *
parser_parse_expr_1(parser_t *parser, ast_node_t *lhs, size_t prev_precedence)
{
@@ -275,6 +296,20 @@ parser_parse_factor(parser_t *parser)
return ast_new_node_ref(parser->arena, token_id.loc, token_id.value);
}
+ case TOKEN_AND:
+ case TOKEN_STAR:
+ case TOKEN_PLUS:
+ case TOKEN_DASH:
+ case TOKEN_TILDE:
+ case TOKEN_BANG: {
+ ast_node_t *expr = parser_parse_expr(parser);
+ if (expr == NULL) {
+ return NULL;
+ }
+
+ ast_unary_op_kind_t kind = token_kind_to_unary_op_kind(token.kind);
+ return ast_new_node_unary_op(parser->arena, token.loc, kind, expr);
+ }
case TOKEN_OPAREN: {
ast_node_t *expr = parser_parse_expr(parser);
diff --git a/src/pretty_print_ast.c b/src/pretty_print_ast.c
index edc6320..3a42412 100644
--- a/src/pretty_print_ast.c
+++ b/src/pretty_print_ast.c
@@ -258,7 +258,7 @@ ast_node_to_pretty_print_node(ast_node_t *ast, arena_t *arena)
char name[256];
switch (literal.kind) {
case AST_LITERAL_U32: {
- sprintf(name, "Literal <kind:u32> <value:%d>", literal.as_u32);
+ sprintf(name, "Literal <kind:u32> <value:%u>", literal.as_u32);
node->name = (char *)arena_alloc(arena, sizeof(char) * (strlen(name) + 1));
strcpy(node->name, name);
break;
@@ -383,6 +383,26 @@ ast_node_to_pretty_print_node(ast_node_t *ast, arena_t *arena)
return node;
}
+
+ case AST_NODE_UNARY_OP: {
+ pretty_print_node_t *node = pretty_print_node_new(arena);
+ ast_unary_op_t unary_op = ast->as_unary_op;
+
+ switch (unary_op.kind) {
+ case AST_UNARY_BITWISE_NOT: {
+ node->name = "Unary_Operation (~)";
+ break;
+ }
+ default:
+ assert(false && "unary operation kind not implemented");
+ }
+
+ pretty_print_node_t *expr = ast_node_to_pretty_print_node(unary_op.expr, arena);
+ list_append(node->children, expr);
+
+ return node;
+ }
+
default: {
printf("node kind = '%d' not implmented\n", ast->kind);
assert(false);
diff --git a/tests/olc/0033_unary_operator_bitwise_not.ol b/tests/olc/0033_unary_operator_bitwise_not.ol
index 1891549..7a9f59d 100644
--- a/tests/olc/0033_unary_operator_bitwise_not.ol
+++ b/tests/olc/0033_unary_operator_bitwise_not.ol
@@ -18,10 +18,22 @@ fn main(): u32 {
return ~e
}
-# XTEST test_compile(exit_code=0)
+# TEST test_compile(exit_code=0)
#
-# XTEST test_run_binary(exit_code=0)
+# TEST test_run_binary(exit_code=0)
#
# TEST test_contains_tokens WITH
# ./0033_unary_operator_bitwise_not.ol:18:10: <~>
# END
+#
+# TEST test_ast WITH
+# Translation_Unit
+# `-Function_Definition <name:main> <return:u32>
+# `-Block
+# |-Var_Definition <name:e> <kind:u32>
+# | `-Literal <kind:u32> <value:4294967295>
+# `-Return_Statement
+# `-Unary_Operation (~)
+# `-Reference <name:e>
+# END
+
--
2.46.0
^ permalink raw reply [flat|nested] 6+ messages in thread