FFFF Function Call location by CohenArthur · Pull Request #512 · jinko-core/jinko · GitHub
[go: up one dir, main page]

Skip to content
Merged
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
60 changes: 42 additions & 18 deletions src/instruction/function_call.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use crate::instruction::{FunctionDec, FunctionKind, Var};
use crate::typechecker::{CheckedType, TypeCtx, TypeId};
use crate::{
generics, log, Context, ErrKind, Error, Generic, InstrKind, Instruction, ObjectInstance,
TypeCheck,
SpanTuple, TypeCheck,
};
use std::rc::Rc;

Expand All @@ -16,6 +16,7 @@ pub struct FunctionCall {
generics: Vec<TypeId>,
args: Vec<Box<dyn Instruction>>,
cached_type: Option<CheckedType>,
location: Option<SpanTuple>,
}

impl FunctionCall {
Expand All @@ -30,6 +31,7 @@ impl FunctionCall {
generics,
args,
cached_type: None,
location: None,
}
}

Expand All @@ -53,14 +55,20 @@ impl FunctionCall {
&self.args
}

/// Set the location of a function call
pub fn set_location(&mut self, loc: SpanTuple) {
self.location = Some(loc)
}

/// Get the corresponding declaration from a context
fn get_declaration(&self, ctx: &mut Context) -> Result<Rc<FunctionDec>, Error> {
match ctx.get_function(self.name()) {
// get_function() return a Rc, so this clones the Rc, not the FunctionDec
Some(f) => Ok(f.clone()),
// FIXME: Fix Location and input
None => Err(Error::new(ErrKind::Context)
.with_msg(format!("cannot find function {}", self.name()))),
.with_msg(format!("cannot find function {}", self.name()))
.with_loc(self.location.clone())),
}
}

Expand Down Expand Up @@ -147,10 +155,14 @@ impl FunctionCall {

#[cfg(not(feature = "ffi"))]
{
ctx.error(Error::new(ErrKind::Context).with_msg(format!(
ctx.error(
Error::new(ErrKind::Context)
.with_msg(format!(
"jinko is not compiled with FFI support. Cannot call `{}` external function",
dec.name()
)));
))
.with_loc(self.location.clone()),
);
None
}
}
Expand Down Expand Up @@ -228,10 +240,14 @@ impl TypeCheck for FunctionCall {
Some(f) => f.clone(), // FIXME: Remove this clone...
// FIXME: This does not account for functions declared later in the code
None => {
ctx.error(Error::new(ErrKind::TypeChecker).with_msg(format!(
"function `{}` was not declared in this scope",
self.name()
)));
ctx.error(
Error::new(ErrKind::TypeChecker)
.with_msg(format!(
"function `{}` was not declared in this scope",
self.name()
))
.with_loc(self.location.clone()),
);
return CheckedType::Error;
}
};
Expand All @@ -249,13 +265,17 @@ impl TypeCheck for FunctionCall {
let mut args = vec![];

if self.args().len() != args_type.len() {
errors.push(Error::new(ErrKind::TypeChecker).with_msg(format!(
"wrong number of arguments \
errors.push(
Error::new(ErrKind::TypeChecker)
.with_msg(format!(
"wrong number of arguments \
for call to function `{}`: expected {}, got {}",
self.name(),
args_type.len(),
self.args().len()
)));
self.name(),
args_type.len(),
self.args().len()
))
.with_loc(self.location.clone()),
);
}

for (dec_arg, given_ty) in args_type.iter().zip(
Expand Down Expand Up @@ -309,10 +329,14 @@ impl Generic for FunctionCall {

if !self.generics.is_empty() && dec.generics().is_empty() {
// FIXME: Format generic list in error too
ctx.error(Error::new(ErrKind::Generics).with_msg(format!(
"calling non-generic function with generic arguments: `{}`",
self.name()
)));
ctx.error(
Error::new(ErrKind::Generics)
.with_msg(format!(
"calling non-generic function with generic arguments: `{}`",
self.name()
))
.with_loc(self.location.clone()),
);
return;
}

Expand Down
24 changes: 18 additions & 6 deletions src/parser/constructs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,7 @@ fn unit(input: ParseInput) -> ParseResult<ParseInput, Box<dyn Instruction>> {
} else {
let (input, id) = Token::identifier(input)?;
let input = next(input);
func_type_or_var(input, id)
func_type_or_var(input, id, start_loc.into())
}
}

Expand Down Expand Up @@ -302,12 +302,19 @@ fn unit_mut_var(input: ParseInput) -> ParseResult<ParseInput, Box<dyn Instructio

/// IDENTIFIER next '(' next args
fn unit_jk_inst(input: ParseInput) -> ParseResult<ParseInput, Box<dyn Instruction>> {
let (input, start_loc) = position(input)?;
let (input, name) = delimited(nom_next, Token::identifier, nom_next)(input)?;
let (input, _) = Token::left_parenthesis(input)?;
let (input, args) = args(next(input))?;
let (input, end_loc) = position(input)?;

// A jk_inst will never contain generics
let call = FunctionCall::new(name, vec![], args);
let mut call = FunctionCall::new(name, vec![], args);
call.set_location(SpanTuple::new(
input.extra,
start_loc.into(),
end_loc.into(),
));
match JkInst::from_function_call(&call) {
Ok(inst) => Ok((input, Box::new(inst))),
Err(err) => Err(NomError(err)),
Expand Down Expand Up @@ -425,11 +432,12 @@ fn inner_block(input: ParseInput) -> ParseResult<ParseInput, Block> {
fn func_type_or_var(
input: ParseInput,
id: String,
start_loc: Location,
) -> ParseResult<ParseInput, Box<dyn Instruction>> {
if let Ok((input, _)) = Token::left_bracket(input) {
generic_func_or_type_inst_args(next(input), id)
generic_func_or_type_inst_args(next(input), id, start_loc)
} else if let Ok((input, _)) = Token::left_parenthesis(input) {
func_or_type_inst_args(next(input), id, vec![])
func_or_type_inst_args(next(input), id, vec![], start_loc)
} else if let Ok((input, _)) = Token::equal(input) {
let (input, value) = expr(input)?;
Ok((input, Box::new(VarAssign::new(false, id, value))))
Expand All @@ -444,6 +452,7 @@ fn func_or_type_inst_args(
input: ParseInput,
id: String,
generics: Vec<TypeId>,
start_loc: Location,
) -> ParseResult<ParseInput, Box<dyn Instruction>> {
if let Ok((input, first_attr)) =
terminated(terminated(Token::identifier, nom_next), Token::colon)(input)
Expand All @@ -461,7 +470,9 @@ fn func_or_type_inst_args(
Ok((input, Box::new(type_inst)))
} else {
let (input, args) = args(input)?;
let func_call = FunctionCall::new(id, generics, args);
let (input, end_loc) = position(input)?;
let mut func_call = FunctionCall::new(id, generics, args);
func_call.set_location(SpanTuple::new(input.extra, start_loc, end_loc.into()));

Ok((input, Box::new(func_call)))
}
Expand All @@ -470,12 +481,13 @@ fn func_or_type_inst_args(
fn generic_func_or_type_inst_args(
input: ParseInput,
id: String,
start_loc: Location,
) -> ParseResult<ParseInput, Box<dyn Instruction>> {
// FIXME: Assign generics to FunctionCall and TypeInstantiation
let (input, generics) = generic_list(input)?;
let (input, _) = Token::left_parenthesis(input)?;

func_or_type_inst_args(next(input), id, generics)
func_or_type_inst_args(next(input), id, generics, start_loc)
}

///
Expand Down
0