Allow mutable function arguments

This commit is contained in:
Nikita Lisitsa 2026-05-07 13:47:15 +03:00
parent 79838a1bb3
commit d146ec69fd
2 changed files with 25 additions and 25 deletions

View file

@ -19,12 +19,11 @@ func print_byte(c: u8):
foreign func putchar(c: i32) -> i32
putchar(c as i32)
func print_str(str: u8*):
func print_str(mut str: u8*):
// Don't use puts() because it adds a newline
mut p = str
while *p != 0ub:
print_byte(*p)
p += 1
while *str != 0ub:
print_byte(*str)
str += 1
func print_i32(x: i32):
if x < 0:
@ -35,7 +34,7 @@ func print_i32(x: i32):
print_i32(x / 10)
print_byte('0' + (x % 10 as u8))
func print_f32(x: f32):
func print_f32(mut x: f32):
if x < 0.0:
print_byte('-')
print_f32(-x)
@ -44,14 +43,14 @@ func print_f32(x: f32):
let xfloor = floor(x)
print_i32(xfloor)
print_byte('.')
mut y = x - (xfloor as f32)
x -= (xfloor as f32)
// Print fractional part
mut i = 0
while i < 3:
y = y * 10.0
let yfloor = floor(y)
print_byte('0' + (yfloor as u8))
y = y - (yfloor as f32)
x = x * 10.0
let xfloor = floor(x)
print_byte('0' + (xfloor as u8))
x -= (xfloor as f32)
i = i + 1
func print_vec3(v: vec3):
@ -108,8 +107,8 @@ struct file:
func open(path: u8*, mode: u8) -> file*:
foreign func fopen(path: u8*, mode: u8*) -> file*
// A hack to turn a single u8 into a zero-terminated array
let mode_wide = mode as u32
return fopen(path, &mode as u8*)
let mode_wide = [mode, 0ub]
return fopen(path, &mode_wide as u8*)
func close(file: file*):
foreign func fclose(file: file*) -> i32

View file

@ -275,6 +275,7 @@ nonempty_function_declaration_argument_list
function_declaration_single_argument
: name colon type_expression { $$ = ast::function_declaration::argument{ast::value_category::constant, $1, std::make_unique<ast::type>($3), @$}; }
| mut name colon type_expression { $$ = ast::function_declaration::argument{ast::value_category::_mutable, $2, std::make_unique<ast::type>($4), @$}; }
;
function_return_type