Skip to content

Commit

Permalink
Merge pull request #147 from onyx-lang/rc-v0.1.12
Browse files Browse the repository at this point in the history
Release: 0.1.12
  • Loading branch information
brendanfh committed May 19, 2024
2 parents eed2efc + 4750349 commit 53d068b
Show file tree
Hide file tree
Showing 24 changed files with 519 additions and 210 deletions.
8 changes: 8 additions & 0 deletions CHANGELOG
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,15 @@ Not released yet
Additions:
- Ability to pipe into a method call.
- `x |> y->z() === y->z(x)`
- Ability to pipe into a try operator.
- `x |> y()?` === y(x)?`
- Ability to use `_` where `#auto` is used.
- This will be come the default soon and `#auto` will be removed.
- `return #from_proc` to return all the way from the procedure.
- Variant of `new` that accepts an r-value and places it in the heap.
- Builtin `panic` procedure that is equivalent to `assert(false, ...)`
- Format parameter "a" that unpacks an `any` and formats its internal value.
- `--generate-name-section` CLI flag

Removals:
- `Optional.try` as it is incompatible with new semantics of `?`.
Expand All @@ -18,10 +24,12 @@ Changes:
- `str.to_cstr_on_stack` is now preferred over `string.to_cstr_on_stack`
- `str.join` is now preferred over `string.join`
- Implementation of `?` for `Optional` and `Result` to return from the enclosing procedure.
- JavaScript file generated by `-r js` is no longer an ES6 module.

Bugfixes:
- Fixed WASI compilation due to syntax issue.
- Fixed WASI platform `__file_open` implementation for wasmtime.
- Weird edge case when using multiple placeholder arguments in a quick procedure.

Contributors:

Expand Down
6 changes: 5 additions & 1 deletion compiler/include/astnodes.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

#define VERSION_MAJOR 0
#define VERSION_MINOR 1
#define VERSION_PATCH 11
#define VERSION_PATCH 12

#include "stb_ds.h"
#include "lex.h"
Expand Down Expand Up @@ -1422,6 +1422,7 @@ struct AstFunction {
AstBlock *body;

char* name;
char* assembly_name;

// NOTE: This is NULL, unless this function was generated from a polymorphic
// procedure call. Then it is set to the token of the call node.
Expand Down Expand Up @@ -1952,6 +1953,7 @@ struct CompileOptions {
b32 generate_foreign_info : 1;
b32 generate_type_info : 1;
b32 generate_method_info : 1;
b32 generate_name_section : 1;
b32 no_core : 1;
b32 no_stale_code : 1;
b32 show_all_errors : 1;
Expand Down Expand Up @@ -2170,6 +2172,8 @@ char *get_expression_string_value(AstTyped* node, b32 *out_is_valid);

b32 cast_is_legal(Type* from_, Type* to_, char** err_msg);
char* get_function_name(AstFunction* func);
char* get_function_assembly_name(AstFunction* func);
char* generate_name_within_scope(Scope *scope, OnyxToken* symbol);

TypeMatch implicit_cast_to_bool(AstTyped **pnode);

Expand Down
1 change: 1 addition & 0 deletions compiler/include/wasm_emit.h
Original file line number Diff line number Diff line change
Expand Up @@ -544,6 +544,7 @@ typedef struct WasmFunc {
LocalAllocator locals;
bh_arr(WasmInstruction) code;
OnyxToken *location;
char *name;
} WasmFunc;

typedef struct WasmGlobal {
Expand Down
44 changes: 41 additions & 3 deletions compiler/src/astnodes.c
Original file line number Diff line number Diff line change
Expand Up @@ -1555,18 +1555,56 @@ TypeMatch implicit_cast_to_bool(AstTyped **pnode) {
}

char* get_function_name(AstFunction* func) {
if (func->kind != Ast_Kind_Function) return "<procedure>";
if (func->kind != Ast_Kind_Function) return "unnamed_proc";

if (func->name != NULL) return func->name;

if (func->exported_name != NULL) {
return bh_aprintf(global_scratch_allocator,
"EXPORTED:%b",
"%b",
func->exported_name->text,
func->exported_name->length);
}

return "<anonymous procedure>";
return "unnamed_proc";
}

char* get_function_assembly_name(AstFunction* func) {
if (func->kind != Ast_Kind_Function) return "unnamed_proc";

if (func->name != NULL) return func->assembly_name;

if (func->exported_name != NULL) {
return bh_aprintf(global_scratch_allocator,
"%b",
func->exported_name->text,
func->exported_name->length);
}

return "unnamed_proc";
}

char* generate_name_within_scope(Scope *scope, OnyxToken* symbol) {
char name[512];
memset(name, 0, 512);

bh_arr(char *) names=NULL;
bh_arr_new(global_heap_allocator, names, 4);

while (scope != NULL) {
bh_arr_push(names, scope->name);
scope = scope->parent;
}

bh_arr_each(char *, n, names) {
if (*n == NULL) continue;

strncat(name, *n, 511);
strncat(name, ".", 511);
}
bh_arr_free(names);

return bh_aprintf(global_heap_allocator, "%s%b", name, symbol->text, symbol->length);
}

AstNode* strip_aliases(AstNode* n) {
Expand Down
5 changes: 5 additions & 0 deletions compiler/src/checker.c
Original file line number Diff line number Diff line change
Expand Up @@ -3812,6 +3812,11 @@ CheckStatus check_process_directive(AstNode* directive) {
// Check if this line is even necessary
inject->binding->token = inject->symbol;

if (inject->binding->kind == Ast_Kind_Function || inject->binding->kind == Ast_Kind_Polymorphic_Proc) {
AstFunction *func = (void *) inject->binding;
func->name = generate_name_within_scope(scope, inject->symbol);
}

Package *pac = NULL;
if (inject->dest->kind == Ast_Kind_Package) {
pac = ((AstPackage *) inject->dest)->package;
Expand Down
7 changes: 7 additions & 0 deletions compiler/src/cli.c
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ static const char *build_docstring = DOCSTRING_HEADER
C_LBLUE " --no-type-info " C_NORM "Disables generating type information\n"
C_LBLUE " --generate-method-info " C_NORM "Populate method information in type information structures\n"
C_LBLUE " --generate-foreign-info " C_NORM "Generate information for foreign blocks\n"
C_LBLUE " --generate-name-section " C_NORM "Generate the 'name' custom section for better debugging\n"
C_LBLUE " --no-stale-code " C_NORM "Disables use of " C_YELLOW "#allow_stale_code" C_NORM " directive\n"
"\n"
C_LBLUE " --doc " C_GREY "doc_file " C_NORM "Generate a .odoc file, Onyx's documentation format used by " C_YELLOW "onyx-doc-gen\n"
Expand Down Expand Up @@ -287,6 +288,9 @@ static void cli_parse_compilation_options(CompileOptions *options, int arg_parse
else if (!strcmp(argv[i], "--generate-method-info")) {
options->generate_method_info = 1;
}
else if (!strcmp(argv[i], "--generate-name-section")) {
options->generate_name_section = 1;
}
else if (!strcmp(argv[i], "--no-type-info")) {
options->generate_type_info = 0;
}
Expand Down Expand Up @@ -353,16 +357,19 @@ static void cli_parse_compilation_options(CompileOptions *options, int arg_parse
options->debug_session = 1;
options->debug_info_enabled = 1;
options->stack_trace_enabled = 1;
options->generate_name_section = 1;
}
else if (!strcmp(argv[i], "--debug-socket")) {
options->debug_socket = argv[++i];
}
else if (!strcmp(argv[i], "--debug-info")) {
options->debug_info_enabled = 1;
options->stack_trace_enabled = 1;
options->generate_name_section = 1;
}
else if (!strcmp(argv[i], "--stack-trace")) {
options->stack_trace_enabled = 1;
options->generate_name_section = 1;
}
else if (!strcmp(argv[i], "--perf")) {
options->running_perf = 1;
Expand Down
17 changes: 10 additions & 7 deletions compiler/src/errors.c
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,14 @@ static void print_error_text(char *text) {
static void print_underline(OnyxError *err, i32 len, i32 first_non_whitespace, b32 colored_printing) {
char* pointer_str = bh_alloc_array(global_scratch_allocator, char, len);
memset(pointer_str, ' ', len);
memcpy(pointer_str - 1, err->pos.line_start, first_non_whitespace);
memset(pointer_str + first_non_whitespace - 1, ' ', err->pos.column - first_non_whitespace);
memset(pointer_str + err->pos.column - 1, '~', err->pos.length - 1);
pointer_str[err->pos.column - 2] = '^';
pointer_str[err->pos.column + err->pos.length - 1] = 0;

int c = err->pos.column - 1;
int l = err->pos.length;

memcpy(pointer_str, err->pos.line_start, first_non_whitespace);
memset(pointer_str + c + 1, '~', l - 1);
pointer_str[c] = '^';
pointer_str[c + l] = 0;

if (colored_printing) bh_printf("\033[91m");
bh_printf("%s\n", pointer_str);
Expand All @@ -60,7 +63,7 @@ static void print_detailed_message_v1(OnyxError *err, bh_file_contents* fc, b32
if (colored_printing) bh_printf("\033[94m");
bh_printf("%b\n", err->pos.line_start, linelength);

fori (i, 0, numlen) bh_printf(" ");
fori (i, 0, numlen - 1) bh_printf(" ");
print_underline(err, linelength, first_char, colored_printing);
}

Expand Down Expand Up @@ -113,7 +116,7 @@ static void print_detailed_message_v2(OnyxError* err, bh_file_contents* fc, b32

if (colored_printing) bh_printf("\033[90m");
fori (i, 0, numlen - 3) bh_printf(" ");
bh_printf("| ");
bh_printf("| ");
if (colored_printing) bh_printf("\033[94m");

print_underline(err, linelength, first_char, colored_printing);
Expand Down
2 changes: 2 additions & 0 deletions compiler/src/onyx.c
Original file line number Diff line number Diff line change
Expand Up @@ -842,6 +842,8 @@ static void onyx_watch(CompileOptions *compile_opts) {
#endif

#if defined(_BH_LINUX) || defined(_BH_DARWIN)
#include <sys/wait.h>

static void perform_self_upgrade(CompileOptions *opts, char *version) {
// TODO: cleanup

Expand Down
42 changes: 13 additions & 29 deletions compiler/src/parser.c
Original file line number Diff line number Diff line change
Expand Up @@ -3466,8 +3466,15 @@ static b32 parse_possible_quick_function_definition(OnyxParser* parser, AstTyped
char text[512];
memset(text, 0, 512);
strncat(text, "__type_", 511);

token_toggle_end(param->token);
strncat(text, param->token->text, 511);
if (!strcmp(param->token->text, "_")) {
int index = param - params;
int len = strnlen(text, 511);
snprintf(text + len, 511 - len, "%d", index);
} else {
strncat(text, param->token->text, 511);
}
token_toggle_end(param->token);

OnyxToken* new_token = bh_alloc(parser->allocator, sizeof(OnyxToken));
Expand Down Expand Up @@ -3840,30 +3847,6 @@ static AstTyped* parse_top_level_expression(OnyxParser* parser) {
return parse_expression(parser, 1);
}

static char* generate_name_within_scope(OnyxParser* parser, OnyxToken* symbol) {
char name[512];
memset(name, 0, 512);

bh_arr(char *) names=NULL;
bh_arr_new(global_heap_allocator, names, 4);

Scope* scope = parser->current_scope;
while (scope != NULL) {
bh_arr_push(names, scope->name);
scope = scope->parent;
}

bh_arr_each(char *, n, names) {
if (*n == NULL) continue;

strncat(name, *n, 511);
strncat(name, ".", 511);
}
bh_arr_free(names);

return bh_aprintf(global_heap_allocator, "%s%b", name, symbol->text, symbol->length);
}

static AstBinding* parse_top_level_binding(OnyxParser* parser, OnyxToken* symbol) {
OnyxToken *after_second_colon = expect_token(parser, ':');
if (after_second_colon) after_second_colon += 1;
Expand All @@ -3879,7 +3862,8 @@ static AstBinding* parse_top_level_binding(OnyxParser* parser, OnyxToken* symbol

if (func->intrinsic_name == NULL) func->intrinsic_name = symbol;

func->name = generate_name_within_scope(parser, symbol);
func->name = generate_name_within_scope(parser->current_scope, symbol);
func->assembly_name = func->name;
func->flags &= ~Ast_Flag_Function_Is_Lambda;
break;
}
Expand All @@ -3888,13 +3872,13 @@ static AstBinding* parse_top_level_binding(OnyxParser* parser, OnyxToken* symbol
AstMacro* macro = (AstMacro *) node;

AstFunction* func = (AstFunction *) macro->body;
func->name = generate_name_within_scope(parser, symbol);
func->name = generate_name_within_scope(parser->current_scope, symbol);
break;
}

case Ast_Kind_Directive_Init: break;

case Ast_Kind_Global: ((AstGlobal *) node)->name = generate_name_within_scope(parser, symbol);
case Ast_Kind_Global: ((AstGlobal *) node)->name = generate_name_within_scope(parser->current_scope, symbol);

case Ast_Kind_Overloaded_Function:
case Ast_Kind_StrLit:
Expand All @@ -3910,7 +3894,7 @@ static AstBinding* parse_top_level_binding(OnyxParser* parser, OnyxToken* symbol
case Ast_Kind_Distinct_Type:
case Ast_Kind_Union_Type:
case Ast_Kind_Poly_Union_Type:
((AstStructType *) node)->name = generate_name_within_scope(parser, symbol);
((AstStructType *) node)->name = generate_name_within_scope(parser->current_scope, symbol);
goto default_case;

case Ast_Kind_Type_Alias:
Expand Down
14 changes: 12 additions & 2 deletions compiler/src/polymorph.h
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ static void insert_poly_slns_into_scope(Scope* scope, bh_arr(AstPolySolution) sl
// NOTE: This might return a volatile string. Do not store it without copying it.
static char* build_poly_solution_key(AstPolySolution* sln) {
if (sln->kind == PSK_Type) {
return (char *) type_get_unique_name(sln->type);
return bh_aprintf(context.ast_alloc, "@%d", sln->type->id);

} else if (sln->kind == PSK_Value) {
static char buffer[256];
Expand Down Expand Up @@ -93,12 +93,13 @@ static char* build_poly_slns_unique_key(bh_arr(AstPolySolution) slns) {
fori (i, 0, 1024) key_buf[i] = 0;

bh_arr_each(AstPolySolution, sln, slns) {
if (sln != slns) strncat(key_buf, "$", 1023);

token_toggle_end(sln->poly_sym->token);

strncat(key_buf, sln->poly_sym->token->text, 1023);
strncat(key_buf, "=", 1023);
strncat(key_buf, build_poly_solution_key(sln), 1023);
strncat(key_buf, ";", 1023);

token_toggle_end(sln->poly_sym->token);
}
Expand Down Expand Up @@ -865,6 +866,15 @@ AstFunction* polymorphic_proc_solidify(AstFunction* pp, bh_arr(AstPolySolution)
// NOTE: Cache the function for later use, reducing duplicate functions.
shput(pp->concrete_funcs, unique_key, solidified_func);

if (solidified_func.func->name) {
solidified_func.func->assembly_name = bh_aprintf(
global_heap_allocator,
"%s$%s",
solidified_func.func->name,
unique_key
);
}

return (AstFunction *) &node_that_signals_a_yield;
}

Expand Down
1 change: 1 addition & 0 deletions compiler/src/wasm_emit.c
Original file line number Diff line number Diff line change
Expand Up @@ -4526,6 +4526,7 @@ static void emit_function(OnyxWasmModule* mod, AstFunction* fd) {
WasmFunc wasm_func = { 0 };
wasm_func.type_idx = type_idx;
wasm_func.location = fd->token;
wasm_func.name = get_function_assembly_name(fd);

bh_arr_new(mod->allocator, wasm_func.code, 16);

Expand Down

0 comments on commit 53d068b

Please sign in to comment.