diff --git a/ast/src/tests.rs b/ast/src/tests.rs index a3f3b2c..6e2fed8 100755 --- a/ast/src/tests.rs +++ b/ast/src/tests.rs @@ -212,7 +212,7 @@ mod tests { name: "add".to_string(), params: FunctionParams { list: vec![param.clone()], - is_variadic: false, + variadic: None, }, body: Box::new(BlockStatement { body: vec![], diff --git a/compiler/src/funcs.rs b/compiler/src/funcs.rs index e4ee979..24730b4 100755 --- a/compiler/src/funcs.rs +++ b/compiler/src/funcs.rs @@ -3,12 +3,14 @@ use std::ptr::null_mut; use std::rc::Rc; use crate::Compiler; +use crate::scope::IdentifierMetadata; use crate::scope::ScopeRef; use ast::ast::*; use ast::token::*; use gccjit_sys::*; -use utils::compiler_error; use utils::compile_time_errors::errors::*; +use utils::compiler_error; +use utils::generate_random_hex::generate_random_hex; #[derive(Debug, Clone)] pub struct FuncMetadata { @@ -16,6 +18,7 @@ pub struct FuncMetadata { pub(crate) ptr: *mut gcc_jit_function, pub(crate) return_type: TokenKind, pub(crate) params: FunctionParams, + pub(crate) normal_params_count: usize, pub(crate) imported_from: Option, } @@ -94,7 +97,7 @@ impl Compiler { func_name.as_ptr(), params.len().try_into().unwrap(), params.as_mut_ptr(), - self.cbool(func_decl.params.is_variadic), + self.cbool(func_decl.params.variadic.is_some()), ) }; @@ -134,10 +137,13 @@ impl Compiler { if func_def.return_type.is_some() { if !return_compiled { - compiler_error!(format!( - "Explicit return statement required for the function '{}'.", - func_def.name - ), self.file_path.clone()); + compiler_error!( + format!( + "Explicit return statement required for the function '{}'.", + func_def.name + ), + self.file_path.clone() + ); } } else { let guard = self.block_func_ref.lock().unwrap(); @@ -147,7 +153,7 @@ impl Compiler { unsafe { gcc_jit_block_end_with_void_return(block, null_mut()) }; } } - } + } return func_ptr; } @@ -161,6 +167,8 @@ impl Compiler { declare_function.name }; + let normal_params_count = declare_function.params.list.len(); + self.func_table.borrow_mut().insert( func_name, FuncMetadata { @@ -169,6 +177,7 @@ impl Compiler { params: declare_function.params, return_type, imported_from: None, + normal_params_count, }, ); } @@ -249,23 +258,72 @@ impl Compiler { return func_metadata.1.clone(); } - compiler_error!(format!( - "Function '{}' not defined at this module.", - from_package.to_string() - ), self.file_path.clone()) + compiler_error!( + format!("Function '{}' not defined at this module.", from_package.to_string()), + self.file_path.clone() + ) } pub(crate) fn compile_func_call(&mut self, scope: ScopeRef, func_call: FuncCall) -> *mut gcc_jit_rvalue { let loc = self.gccjit_location(func_call.loc.clone()); - let metadata = self.get_func(func_call.func_name); - + let metadata = self.get_func(func_call.func_name.clone()); + + let func_metadata = self.get_func(func_call.func_name.clone()); + let normal_params_count = func_metadata.normal_params_count; + let mut args = self.compile_func_arguments( Rc::clone(&scope), Some(metadata.params.list.clone()), func_call.arguments, ); + // FIXME Find a better solution + // Add variadic_arguments identifier to current scope + // if let Some(variadic) = func_metadata.params.variadic { + // let vargs_type = self.token_as_data_type( + // self.context, + // TokenKind::Array( + // Box::new(variadic), + // vec![Some(TokenKind::Literal(Literal::Integer(IntegerLiteral::SizeT( + // normal_params_count, + // ))))], + // ), + // ); + // let mut vargs_values = args[0..normal_params_count - 1].to_vec(); + + // let vargs_rvalue = unsafe { + // gcc_jit_context_new_array_constructor( + // self.context, + // null_mut(), + // vargs_type, + // vargs_values.len().try_into().unwrap(), + // vargs_values.as_mut_ptr(), + // ) + // }; + + // let vargs_lvalue_name = CString::new(format!("__func_vargs__{}", generate_random_hex())).unwrap(); + // let vargs_lvalue = unsafe { + // gcc_jit_context_new_global( + // self.context, + // self.gccjit_location(func_call.loc.clone()), + // gcc_jit_global_kind::GCC_JIT_GLOBAL_INTERNAL, + // vargs_type, + // vargs_lvalue_name.as_ptr(), + // ) + // }; + + // unsafe { gcc_jit_global_set_initializer_rvalue(vargs_lvalue, vargs_rvalue) }; + + // scope.borrow_mut().insert( + // String::from("vargs"), + // IdentifierMetadata { + // lvalue_type: vargs_type, + // lvalue: vargs_lvalue, + // }, + // ); + // } + let rvalue = unsafe { gcc_jit_context_new_call( self.context, diff --git a/compiler/src/import.rs b/compiler/src/import.rs index 21bfac8..a224520 100755 --- a/compiler/src/import.rs +++ b/compiler/src/import.rs @@ -123,6 +123,7 @@ impl Compiler { params: value.params, return_type: value.return_type, imported_from: Some(package_name.clone()), + normal_params_count: value.normal_params_count }, ); } @@ -208,7 +209,7 @@ impl Compiler { func_name_cstr.as_ptr(), func_params.len().try_into().unwrap(), func_params.as_mut_ptr(), - self.cbool(params.is_variadic), + self.cbool(params.variadic.is_some()), ) }; diff --git a/compiler/src/lib.rs b/compiler/src/lib.rs index 285751f..2b6bce5 100755 --- a/compiler/src/lib.rs +++ b/compiler/src/lib.rs @@ -140,9 +140,10 @@ impl Compiler { FuncMetadata { func_type: func_def.vis_type, ptr, - params: func_def.params, + params: func_def.params.clone(), return_type, imported_from: None, + normal_params_count: func_def.params.list.len(), }, ); } diff --git a/examples/main.cyr b/examples/main.cyr index 0b58f77..a62f976 100755 --- a/examples/main.cyr +++ b/examples/main.cyr @@ -1,3 +1,7 @@ -fn sample(x: i32, y:double, ...i32) { - +fn sample(x: i32, ...i32) { + // #result = vargs; } + +fn main() { + sample(10, 20, 30); +} \ No newline at end of file diff --git a/parser/src/statements.rs b/parser/src/statements.rs index e6465de..910ee4f 100755 --- a/parser/src/statements.rs +++ b/parser/src/statements.rs @@ -284,12 +284,12 @@ impl<'a> Parser<'a> { span, }); } - TokenKind::Identifier { name: identiier } => { + TokenKind::Identifier { name: identifier } => { let span = self.current_token.span.clone(); package_paths.push(PackagePath { package_name: Identifier { - name: identiier, + name: identifier, span: span.clone(), loc: self.current_location(), }, diff --git a/stdlib/io.cyr b/stdlib/io.cyr index 55e034c..5527f61 100755 --- a/stdlib/io.cyr +++ b/stdlib/io.cyr @@ -1,4 +1,4 @@ -extern fn printf(fmt: string, ...): void as _printf; +extern fn printf(fmt: string, ...string): void as _printf; pub fn printf(fmt: string, ...) { _printf(fmt);