* Re: [PATCH olang v1 0/3] add support to compile extern fn def
2024-10-17 2:04 [PATCH olang v1 0/3] add support to compile extern fn def Johnny Richard
@ 2024-10-17 0:11 ` Carlos Maniero
2024-10-17 2:04 ` [PATCH olang v1 1/3] lexer: spec: add extern keyword for function def Johnny Richard
` (2 subsequent siblings)
3 siblings, 0 replies; 6+ messages in thread
From: Carlos Maniero @ 2024-10-17 0:11 UTC (permalink / raw)
To: Johnny Richard, ~johnnyrichard/olang-devel
Nice! Applied!
To git.sr.ht:~johnnyrichard/olang
160969f..c01fa7c main -> main
^ permalink raw reply [flat|nested] 6+ messages in thread
* [PATCH olang v1 1/3] lexer: spec: add extern keyword for function def
2024-10-17 2:04 [PATCH olang v1 0/3] add support to compile extern fn def Johnny Richard
2024-10-17 0:11 ` Carlos Maniero
@ 2024-10-17 2:04 ` Johnny Richard
2024-10-17 0:07 ` [olang/patches/.build.yml] build success builds.sr.ht
2024-10-17 2:04 ` [PATCH olang v1 2/3] parser: support extern function definition Johnny Richard
2024-10-17 2:04 ` [PATCH olang v1 3/3] codegen: x64: compile " Johnny Richard
3 siblings, 1 reply; 6+ messages in thread
From: Johnny Richard @ 2024-10-17 2:04 UTC (permalink / raw)
To: ~johnnyrichard/olang-devel; +Cc: Johnny Richard
Signed-off-by: Johnny Richard <johnny@johnnyrichard.com>
---
docs/info/olang.ebnf | 2 +-
src/lexer.c | 5 ++++
src/lexer.h | 1 +
tests/olc/0037_variable_overflow.ol | 39 +++++++++++++++++++++++++++++
4 files changed, 46 insertions(+), 1 deletion(-)
create mode 100644 tests/olc/0037_variable_overflow.ol
diff --git a/docs/info/olang.ebnf b/docs/info/olang.ebnf
index 0bc7eb5..5bd2be6 100644
--- a/docs/info/olang.ebnf
+++ b/docs/info/olang.ebnf
@@ -11,7 +11,7 @@
<variable-initializer> ::= '=' <ows> <expression>
(* Functions *)
-<function-definition> ::= 'fn' <ws> <function-name> <ows> '(' ( <ows> | <ows> <function-params> <ows> ) ')' <ows> ':' <ows> <return-type> <ows> <function-body>
+<function-definition> ::= ('extern' <ws>)? 'fn' <ws> <function-name> <ows> '(' ( <ows> | <ows> <function-params> <ows> ) ')' <ows> ':' <ows> <return-type> <ows> <function-body>?
<function-name> ::= <identifier>
<function-params> ::= <identifier> <ows> ':' <ows> <type> ( <ows> ',' <ows> <function-params>)*
<return-type> ::= <type>
diff --git a/src/lexer.c b/src/lexer.c
index 1a6c24b..ddfe42a 100644
--- a/src/lexer.c
+++ b/src/lexer.c
@@ -313,6 +313,7 @@ static char *token_kind_str_table[] = {
[TOKEN_ELSE] = "else",
[TOKEN_WHILE] = "while",
[TOKEN_VAR] = "var",
+ [TOKEN_EXTERN] = "extern",
[TOKEN_LF] = "line_feed",
[TOKEN_OPAREN] = "(",
[TOKEN_CPAREN] = ")",
@@ -489,6 +490,10 @@ lexer_str_to_token_kind(string_view_t text)
return TOKEN_VAR;
}
+ if (string_view_eq_to_cstr(text, "extern")) {
+ return TOKEN_EXTERN;
+ }
+
if (string_view_eq_to_cstr(text, "return")) {
return TOKEN_RETURN;
}
diff --git a/src/lexer.h b/src/lexer.h
index 774f619..bba15fb 100644
--- a/src/lexer.h
+++ b/src/lexer.h
@@ -53,6 +53,7 @@ typedef enum token_kind
TOKEN_ELSE,
TOKEN_WHILE,
TOKEN_VAR,
+ TOKEN_EXTERN,
// Equality operators
TOKEN_CMP_EQ,
diff --git a/tests/olc/0037_variable_overflow.ol b/tests/olc/0037_variable_overflow.ol
new file mode 100644
index 0000000..0e3c302
--- /dev/null
+++ b/tests/olc/0037_variable_overflow.ol
@@ -0,0 +1,39 @@
+# 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/>.
+
+extern fn putchar(c: u32): u32
+
+fn main(): u32 {
+ return putchar(111)
+}
+
+# XTEST test_compile(exit_code=0)
+#
+# XTEST test_run_binary(exit_code=0)
+#
+# XTEST test_ast WITH
+# Translation_Unit
+# |-Function_Definition <name:putchar> <return:u32> <extern>
+# | `-Param_Definition <name:c> <type:u32>
+# `-Function_Definition <name:main> <return:u32>
+# `-Block
+# `-Return_Statement
+# `-Function_Call <name:putchar>
+# `-Literal <kind:u32> <value:111>
+# END
+#
+# TEST test_contains_tokens WITH
+# ./0037_variable_overflow.ol:16:1: <extern>
+# END
--
2.46.0
^ permalink raw reply [flat|nested] 6+ messages in thread
* [PATCH olang v1 2/3] parser: support extern function definition
2024-10-17 2:04 [PATCH olang v1 0/3] add support to compile extern fn def Johnny Richard
2024-10-17 0:11 ` Carlos Maniero
2024-10-17 2:04 ` [PATCH olang v1 1/3] lexer: spec: add extern keyword for function def Johnny Richard
@ 2024-10-17 2:04 ` Johnny Richard
2024-10-17 2:04 ` [PATCH olang v1 3/3] codegen: x64: compile " Johnny Richard
3 siblings, 0 replies; 6+ messages in thread
From: Johnny Richard @ 2024-10-17 2:04 UTC (permalink / raw)
To: ~johnnyrichard/olang-devel; +Cc: Johnny Richard
Signed-off-by: Johnny Richard <johnny@johnnyrichard.com>
---
src/ast.c | 3 ++-
src/ast.h | 2 ++
src/parser.c | 20 +++++++++++++++++---
src/pretty_print_ast.c | 14 +++++++++-----
tests/olc/0037_variable_overflow.ol | 2 +-
5 files changed, 31 insertions(+), 10 deletions(-)
diff --git a/src/ast.c b/src/ast.c
index 800239e..d0c6b37 100644
--- a/src/ast.c
+++ b/src/ast.c
@@ -45,11 +45,11 @@ ast_new_node_fn_def(arena_t *arena,
string_view_t id,
list_t *params,
type_t *return_type,
+ bool _extern,
ast_node_t *block)
{
assert(arena);
assert(params);
- assert(block);
ast_node_t *node_fn_def =
(ast_node_t *)arena_alloc(arena, sizeof(ast_node_t));
@@ -61,6 +61,7 @@ ast_new_node_fn_def(arena_t *arena,
fn_def->id = id;
fn_def->return_type = return_type;
+ fn_def->_extern = _extern;
fn_def->block = block;
fn_def->params = params;
diff --git a/src/ast.h b/src/ast.h
index 217a264..e7d8ed8 100644
--- a/src/ast.h
+++ b/src/ast.h
@@ -75,6 +75,7 @@ typedef struct ast_fn_definition
string_view_t id;
list_t *params;
type_t *return_type;
+ bool _extern;
ast_node_t *block;
scope_t *scope;
} ast_fn_definition_t;
@@ -226,6 +227,7 @@ ast_new_node_fn_def(arena_t *arena,
string_view_t id,
list_t *params,
type_t *return_type,
+ bool _extern,
ast_node_t *block);
ast_node_t *
diff --git a/src/parser.c b/src/parser.c
index 35cf017..d26f266 100644
--- a/src/parser.c
+++ b/src/parser.c
@@ -467,6 +467,16 @@ parser_parse_fn_params(parser_t *parser)
ast_node_t *
parser_parse_fn_definition(parser_t *parser)
{
+ bool _extern = false;
+
+ token_t _extern_token;
+ lexer_peek_next(parser->lexer, &_extern_token);
+
+ if (_extern_token.kind == TOKEN_EXTERN) {
+ _extern = true;
+ skip_next_token(parser);
+ }
+
if (!skip_expected_token(parser, TOKEN_FN)) {
return NULL;
}
@@ -494,9 +504,12 @@ parser_parse_fn_definition(parser_t *parser)
skip_line_feeds(parser->lexer);
- ast_node_t *block = parser_parse_block(parser);
- if (block == NULL) {
- return NULL;
+ ast_node_t *block = NULL;
+ if (!_extern) {
+ block = parser_parse_block(parser);
+ if (block == NULL) {
+ return NULL;
+ }
}
return ast_new_node_fn_def(parser->arena,
@@ -504,6 +517,7 @@ parser_parse_fn_definition(parser_t *parser)
fn_name_token.value,
params,
ret_type,
+ _extern,
block);
}
diff --git a/src/pretty_print_ast.c b/src/pretty_print_ast.c
index d2164eb..d476370 100644
--- a/src/pretty_print_ast.c
+++ b/src/pretty_print_ast.c
@@ -155,9 +155,11 @@ ast_node_to_pretty_print_node(ast_node_t *ast, arena_t *arena)
char name[256];
sprintf(name,
- "Function_Definition <name:" SV_FMT "> <return:" SV_FMT ">",
+ "Function_Definition <name:" SV_FMT "> <return:" SV_FMT
+ ">%s",
SV_ARG(fn_def.id),
- SV_ARG(fn_def.return_type->id));
+ SV_ARG(fn_def.return_type->id),
+ fn_def._extern ? " <extern>" : "");
node->name =
(char *)arena_alloc(arena, sizeof(char) * (strlen(name) + 1));
strcpy(node->name, name);
@@ -169,9 +171,11 @@ ast_node_to_pretty_print_node(ast_node_t *ast, arena_t *arena)
param = list_next(param);
}
- pretty_print_node_t *block =
- ast_node_to_pretty_print_node(fn_def.block, arena);
- list_append(node->children, block);
+ if (fn_def.block != NULL) {
+ pretty_print_node_t *block =
+ ast_node_to_pretty_print_node(fn_def.block, arena);
+ list_append(node->children, block);
+ }
return node;
}
case AST_NODE_FN_CALL: {
diff --git a/tests/olc/0037_variable_overflow.ol b/tests/olc/0037_variable_overflow.ol
index 0e3c302..2b7c8c2 100644
--- a/tests/olc/0037_variable_overflow.ol
+++ b/tests/olc/0037_variable_overflow.ol
@@ -23,7 +23,7 @@ fn main(): u32 {
#
# XTEST test_run_binary(exit_code=0)
#
-# XTEST test_ast WITH
+# TEST test_ast WITH
# Translation_Unit
# |-Function_Definition <name:putchar> <return:u32> <extern>
# | `-Param_Definition <name:c> <type:u32>
--
2.46.0
^ permalink raw reply [flat|nested] 6+ messages in thread
* [PATCH olang v1 3/3] codegen: x64: compile extern function definition
2024-10-17 2:04 [PATCH olang v1 0/3] add support to compile extern fn def Johnny Richard
` (2 preceding siblings ...)
2024-10-17 2:04 ` [PATCH olang v1 2/3] parser: support extern function definition Johnny Richard
@ 2024-10-17 2:04 ` Johnny Richard
3 siblings, 0 replies; 6+ messages in thread
From: Johnny Richard @ 2024-10-17 2:04 UTC (permalink / raw)
To: ~johnnyrichard/olang-devel; +Cc: Johnny Richard
Signed-off-by: Johnny Richard <johnny@johnnyrichard.com>
---
src/checker.c | 4 +++-
src/codegen_x86_64.c | 4 ++++
tests/olc/0037_variable_overflow.ol | 4 ++--
3 files changed, 9 insertions(+), 3 deletions(-)
diff --git a/src/checker.c b/src/checker.c
index 0acab13..02341cc 100644
--- a/src/checker.c
+++ b/src/checker.c
@@ -141,7 +141,9 @@ populate_scope(checker_t *checker, scope_t *scope, ast_node_t *ast)
item = list_next(item);
}
- populate_scope(checker, fn_def->scope, ast->as_fn_def.block);
+ if (ast->as_fn_def.block != NULL) {
+ populate_scope(checker, fn_def->scope, ast->as_fn_def.block);
+ }
return;
}
diff --git a/src/codegen_x86_64.c b/src/codegen_x86_64.c
index 6758fc1..1951365 100644
--- a/src/codegen_x86_64.c
+++ b/src/codegen_x86_64.c
@@ -887,6 +887,10 @@ static void
codegen_x86_64_emit_function(codegen_x86_64_t *codegen,
ast_fn_definition_t *fn_def)
{
+ if (fn_def->_extern) {
+ return;
+ }
+
fprintf(codegen->out, ".globl " SV_FMT "\n", SV_ARG(fn_def->id));
codegen->base_offset = 0;
diff --git a/tests/olc/0037_variable_overflow.ol b/tests/olc/0037_variable_overflow.ol
index 2b7c8c2..be36e77 100644
--- a/tests/olc/0037_variable_overflow.ol
+++ b/tests/olc/0037_variable_overflow.ol
@@ -19,9 +19,9 @@ fn main(): u32 {
return putchar(111)
}
-# 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=111)
#
# TEST test_ast WITH
# Translation_Unit
--
2.46.0
^ permalink raw reply [flat|nested] 6+ messages in thread