Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WIP: Parameters are read-only #80

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion compiler/include/astnodes.h
Original file line number Diff line number Diff line change
Expand Up @@ -2108,7 +2108,6 @@ static inline b32 is_lval(AstNode* node) {
node = strip_aliases(node);

if ((node->kind == Ast_Kind_Local)
|| (node->kind == Ast_Kind_Param)
|| (node->kind == Ast_Kind_Global)
|| (node->kind == Ast_Kind_Dereference)
|| (node->kind == Ast_Kind_Subscript)
Expand Down
11 changes: 7 additions & 4 deletions compiler/src/checker.c
Original file line number Diff line number Diff line change
Expand Up @@ -1041,9 +1041,8 @@ CheckStatus check_binaryop_assignment(AstBinaryOp** pbinop) {
ERROR(binop->token->pos, "Assignment not valid in expression.");

if (!is_lval((AstNode *) binop->left))
ERROR_(binop->left->token->pos,
"Cannot assign to '%b'.",
binop->left->token->text, binop->left->token->length);
ERROR(binop->token->pos,
"Left-hand side is not assignable.");

if ((binop->left->flags & Ast_Flag_Const) != 0 && binop->left->type != NULL)
ERROR_(binop->token->pos,
Expand Down Expand Up @@ -1952,7 +1951,11 @@ CheckStatus check_address_of(AstAddressOf** paof) {
return Check_Yield_Macro;
}

ERROR_(aof->token->pos, "Cannot take the address of something that is not an l-value. %s", onyx_ast_node_kind_string(expr->kind));
if (expr->kind == Ast_Kind_Param) {
ERROR(aof->token->pos, "Cannot take the address of a parameter. Store it in a local variable first to take its address.");
} else {
ERROR_(aof->token->pos, "Cannot take the address of something that is not an l-value. %s", onyx_ast_node_kind_string(expr->kind));
}
}

expr->flags |= Ast_Flag_Address_Taken;
Expand Down
28 changes: 15 additions & 13 deletions core/container/heap.onyx
Original file line number Diff line number Diff line change
Expand Up @@ -38,28 +38,29 @@ remove_top :: (use heap: &Heap) -> heap.T {
heap_rchild :: macro (index) => (index * 2) + 2

shift_down :: (use heap: &Heap, idx: i32) {
index := idx;
while true {
min_index := idx;
min_index := index;

l := heap_lchild(idx);
l := heap_lchild(index);
if l < data.count {
if compare(data[l], data[min_index]) < 0 {
min_index = l;
}
}

r := heap_rchild(idx);
r := heap_rchild(index);
if r < data.count {
if compare(data[r], data[min_index]) < 0 {
min_index = r;
}
}

if idx != min_index {
tmp := data[idx];
data[idx] = data[min_index];
if index != min_index {
tmp := data[index];
data[index] = data[min_index];
data[min_index] = tmp;
idx = min_index;
index = min_index;
continue;
}

Expand All @@ -68,14 +69,15 @@ remove_top :: (use heap: &Heap) -> heap.T {
}

shift_up :: (use heap: &Heap, idx: i32) {
while idx > 0 {
parent := heap_parent(idx);
if compare(data[parent], data[idx]) <= 0 do break;
index := idx;
while index > 0 {
parent := heap_parent(index);
if compare(data[parent], data[index]) <= 0 do break;

tmp := data[parent];
data[parent] = data[idx];
data[idx] = tmp;
idx = parent;
data[parent] = data[index];
data[index] = tmp;
index = parent;
}
}
}
Expand Down
21 changes: 12 additions & 9 deletions core/container/slice.onyx
Original file line number Diff line number Diff line change
Expand Up @@ -129,10 +129,11 @@ transplant :: (arr: [] $T, old_index: i32, new_index: i32) -> bool {
get :: (arr: [] $T, idx: i32) -> T {
if arr.count == 0 do return .{};

while idx < 0 do idx += arr.count;
while idx >= arr.count do idx -= arr.count;
i := idx;
while i < 0 do i += arr.count;
while i >= arr.count do i -= arr.count;

return arr.data[idx];
return arr.data[i];
}

#doc """
Expand All @@ -141,10 +142,11 @@ get :: (arr: [] $T, idx: i32) -> T {
get_ptr :: (arr: [] $T, idx: i32) -> &T {
if arr.count == 0 do return null;

while idx < 0 do idx += arr.count;
while idx >= arr.count do idx -= arr.count;
i := idx;
while i < 0 do i += arr.count;
while i >= arr.count do i -= arr.count;

return &arr.data[idx];
return &arr.data[i];
}

#doc """
Expand All @@ -153,10 +155,11 @@ get_ptr :: (arr: [] $T, idx: i32) -> &T {
set :: (arr: [] $T, idx: i32, value: T) {
if arr.count == 0 do return;

while idx < 0 do idx += arr.count;
while idx >= arr.count do idx -= arr.count;
i := idx;
while i < 0 do i += arr.count;
while i >= arr.count do i -= arr.count;

arr.data[idx] = value;
arr.data[i] = value;
}

contains :: #match #locked {
Expand Down
9 changes: 6 additions & 3 deletions core/conv/conv.onyx
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,8 @@ str_to_f64 :: (s: &str) -> f64 {
prefixed with '0b' and hexadecimal numbers are prefixed with
'0x'.
"""
i64_to_str :: (n: i64, base: u64, buf: [] u8, min_length := 0, prefix := false) -> str {
i64_to_str :: (num: i64, base: u64, buf: [] u8, min_length := 0, prefix := false) -> str {
n := num;
is_neg := false;
if n < 0 && base == 10 {
is_neg = true;
Expand Down Expand Up @@ -215,7 +216,8 @@ i64_to_str :: (n: i64, base: u64, buf: [] u8, min_length := 0, prefix := false)
Converts an unsigned number into a string using the buffer provided.
Behaves like i64_to_str.
"""
u64_to_str :: (n: u64, base: u64, buf: [] u8, min_length := 0, prefix := false) -> str {
u64_to_str :: (num: u64, base: u64, buf: [] u8, min_length := 0, prefix := false) -> str {
n := num;
c: [&] u8 = &buf[buf.count - 1];
len := 0;

Expand Down Expand Up @@ -274,7 +276,8 @@ u64_to_str :: (n: u64, base: u64, buf: [] u8, min_length := 0, prefix := false)
This is better than what used to be, but still relies on converting the integer
part of the float to an integer, which could overflow.
"""
f64_to_str :: (f: f64, buf: [] u8, digits_after_decimal := 4) -> str {
f64_to_str :: (num: f64, buf: [] u8, digits_after_decimal := 4) -> str {
f := num;
if math.is_nan(f) {
return format(buf, "NaN");
}
Expand Down
10 changes: 6 additions & 4 deletions core/io/reader.onyx
Original file line number Diff line number Diff line change
Expand Up @@ -444,6 +444,8 @@ read_word :: (use reader: &Reader, numeric_allowed := false, allocator := contex
}

read_until :: (use reader: &Reader, until: u8, skip: u32 = 0, allocator := context.allocator, consume_end := false, inplace := false) -> str {
sk := skip;

//
// Reading in place is special. It does not make a special allocation for the data,
// instead just return a pointer to the internal data.
Expand All @@ -461,8 +463,8 @@ read_until :: (use reader: &Reader, until: u8, skip: u32 = 0, allocator := conte
if curr != until {
count += 1;
} else {
if skip == 0 do break;
else do skip -= 1;
if sk == 0 do break;
else do sk -= 1;
}
}

Expand All @@ -482,8 +484,8 @@ read_until :: (use reader: &Reader, until: u8, skip: u32 = 0, allocator := conte
if curr != until {
count += 1;
} else {
if skip == 0 do break;
else do skip -= 1;
if sk == 0 do break;
else do sk -= 1;
}
}

Expand Down
23 changes: 14 additions & 9 deletions core/math/math.onyx
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@ SQRT_2 :: 1.414213562f;
// implementations of these functions.
//

sin :: (t: f32) -> f32 {
sin :: (x: f32) -> f32 {
t := x;
while t >= PI do t -= TAU;
while t <= -PI do t += TAU;

Expand All @@ -49,7 +50,8 @@ sin :: (t: f32) -> f32 {
return res;
}

cos :: (t: f32) -> f32 {
cos :: (x: f32) -> f32 {
t := x;
while t >= PI do t -= TAU;
while t <= -PI do t += TAU;

Expand Down Expand Up @@ -151,9 +153,10 @@ pow :: #match {
if p == 0 do return 1;

a: T = 1;
while p > 0 {
if p % 2 == 1 do a *= base;
p = p >> 1;
p_ := p;
while p_ > 0 {
if p_ % 2 == 1 do a *= base;
p_ = p_ >> 1;
base *= base;
}

Expand Down Expand Up @@ -353,11 +356,13 @@ choose :: (n: $T, k: T) -> T {
}

gcd :: (a: $T, b: T) -> T {
if a < 0 do a = -a;
if b < 0 do b = -b;

if b == 0 do return a;
return gcd(b, a % b);

x := a;
y := b;
if x < 0 do x = -x;
if x < 0 do y = -y;
return gcd(y, x % y);
}

lcm :: (a: $T, b: T) -> T {
Expand Down
6 changes: 4 additions & 2 deletions core/memory/memory.onyx
Original file line number Diff line number Diff line change
Expand Up @@ -46,16 +46,18 @@ align :: (size: &u64, align: u64) {
#overload
align :: (size: u64, align: u64) -> u64 {
if size % align != 0 {
size += align - (size % align);
return size + align - (size % align);
}

return size;
}

#overload
align :: (size: u32, align: u32) -> u32 {
if size % align != 0 {
size += align - (size % align);
return size + align - (size % align);
}

return size;
}

Expand Down
7 changes: 4 additions & 3 deletions core/runtime/common.onyx
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,11 @@ use runtime.platform { __output_string }
__output_uint :: (n: u64) {
buf: [128] u8;
i := 127;
while n > 0 && i >= 0 {
buf[i] = ~~('0' + n % 10);
x := n;
while x > 0 && i >= 0 {
buf[i] = ~~('0' + x % 10);
i -= 1;
n /= 10;
x /= 10;
}

__output_string(buf[i .. 128]);
Expand Down
3 changes: 2 additions & 1 deletion core/runtime/platform/wasi/platform.onyx
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,8 @@ PollDescription :: struct {
out: core.io.PollEvent;
}

__poll :: (fds: [] PollDescription, timeout: i32) -> void {
__poll :: (fds: [] PollDescription, timeout_: i32) -> void {
timeout := timeout_;
count := fds.count;
if timeout == 0 {
timeout = 1;
Expand Down
12 changes: 6 additions & 6 deletions core/string/string.onyx
Original file line number Diff line number Diff line change
Expand Up @@ -426,19 +426,19 @@ advance :: #match #local {}

#overload
advance :: (s: &str, chars := 1) {
chars = math.min(chars, s.count);
c := math.min(chars, s.count);

s.data += chars;
s.count -= chars;
s.data += c;
s.count -= c;
}

#overload
advance :: (s: str, chars := 1) -> str {
chars = math.min(chars, s.count);
c := math.min(chars, s.count);
out := s;

out.data += chars;
out.count -= chars;
out.data += c;
out.count -= c;

return out;
}
Expand Down
3 changes: 2 additions & 1 deletion tests/aoc-2021/day17.onyx
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ ty0: i32
tx1: i32
ty1: i32

simulate :: (dx, dy: i32) -> (i32, bool) {
simulate :: (dx_, dy_: i32) -> (i32, bool) {
dx, dy := dx_, dy_;
x, y := 0, 0;
maxy := 0;
did_enter := false;
Expand Down
6 changes: 4 additions & 2 deletions tests/aoc-2021/day18.onyx
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,8 @@ SnailNum :: struct {
if new_right do new_right.parent = parent;
}

number_to_left :: (n: &SnailNum) -> &u32 {
number_to_left :: (num: &SnailNum) -> &u32 {
n := num;
while n.parent && n.parent.left == n {
n = n.parent;
}
Expand All @@ -159,7 +160,8 @@ SnailNum :: struct {
return &n.right_val;
}

number_to_right :: (n: &SnailNum) -> &u32 {
number_to_right :: (num: &SnailNum) -> &u32 {
n := num;
while n.parent && n.parent.right == n {
n = n.parent;
}
Expand Down