Skip to content

Commit

Permalink
Add warning of wrong function types
Browse files Browse the repository at this point in the history
  • Loading branch information
didrikmunther committed Sep 24, 2024
1 parent a660bd8 commit 1182799
Show file tree
Hide file tree
Showing 8 changed files with 213 additions and 140 deletions.
6 changes: 5 additions & 1 deletion input.ro
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,8 @@ fn add_two_numbers(a: int, b: int) {
let a: int = 1;
let b: str = "hej";

add_two_numbers(a, b);
add_two_numbers(
a,
b,
c
);
65 changes: 6 additions & 59 deletions out.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,11 +47,8 @@ def __intrinsic__stack_pop():
# Builtin function: printf
def __userf__5():
__builtin__printf()
# Builtin function: printf
def __userf__6():
__builtin__printf()
# User function: add_two_numbers
def __userf__7():
def __userf__6():
__intrinsic__stack_pop()
_2_a = __intrinsic__stack_pop()
_3_b = __intrinsic__stack_pop()
Expand All @@ -62,81 +59,31 @@ def __userf__7():
__intrinsic__stack_push(_4_c)
__intrinsic__stack_push(_3_b)
__intrinsic__stack_push(_2_a)
__intrinsic__stack_push(__global_data[0])
__intrinsic__stack_push(4)
# Procedure call: printf
__userf__5()
__intrinsic__stack_pop()
__intrinsic__stack_push(_4_c)
__intrinsic__stack_push(_3_b)
__intrinsic__stack_push(_2_a)
__intrinsic__stack_push(__global_data[1])
__intrinsic__stack_push(4)
# Procedure call: printf
__userf__6()
__intrinsic__stack_pop()

pass

# Builtin function: printf
def __userf__11():
__builtin__printf()
# Builtin function: printf
def __userf__12():
__builtin__printf()
# User function: add_two_numbers
def __userf__13():
__intrinsic__stack_pop()
_8_a = __intrinsic__stack_pop()
_9_b = __intrinsic__stack_pop()
__intrinsic__stack_push(_9_b)
__intrinsic__stack_push(_8_a)
__intrinsic__stack_add()
_10_c = __intrinsic__stack_pop()
__intrinsic__stack_push(_10_c)
__intrinsic__stack_push(_9_b)
__intrinsic__stack_push(_8_a)
__intrinsic__stack_push(__global_data[2])
__intrinsic__stack_push(4)
# Procedure call: printf
__userf__11()
__intrinsic__stack_pop()
__intrinsic__stack_push(_10_c)
__intrinsic__stack_push(_9_b)
__intrinsic__stack_push(_8_a)
__intrinsic__stack_push(__global_data[3])
__intrinsic__stack_push(4)
# Procedure call: printf
__userf__12()
__userf__5()
__intrinsic__stack_pop()

pass


def __setup():
global __global_data
__global_data = list(range(4))
__global_data[0] = "%i + %i = %i\n"
__global_data = list(range(2))
__global_data[0] = "hej"
__global_data[1] = "%i + %i = %i\n"
__global_data[2] = "%i + %i = %i\n"
__global_data[3] = "%i + %i = %i\n"

def __main():
__intrinsic__stack_push(1)
_0_a = __intrinsic__stack_pop()
__intrinsic__stack_push(2)
__intrinsic__stack_push(__global_data[0])
_1_b = __intrinsic__stack_pop()
__intrinsic__stack_push(_1_b)
__intrinsic__stack_push(_0_a)
__intrinsic__stack_push(2)
# Procedure call: add_two_numbers
__userf__7()
__intrinsic__stack_pop()
__intrinsic__stack_push(_1_b)
__intrinsic__stack_push(_0_a)
__intrinsic__stack_push(2)
# Procedure call: add_two_numbers
__userf__13()
__userf__6()
__intrinsic__stack_pop()

pass
Expand Down
59 changes: 56 additions & 3 deletions src/compiler/error.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
use super::program::{ir::ExpressedType, Program};
use super::program::{
ir::{ExpressedType, Type},
Program,
};
use crate::{
error::{RostError, RostErrorElement},
lexer::Keyword,
Expand Down Expand Up @@ -28,13 +31,18 @@ pub use compiler_todo;

#[derive(Debug, Clone)]
pub struct WrongType {
content: String,
pub content: String,
}

impl WrongType {
pub fn from_expressed_type(_typ: ExpressedType, _program: &Program) -> Self {
Self {
content: format!("todo"),
content: _program
.types
.get(_typ.id)
.unwrap()
.format(_program)
.to_string(),
}
}
}
Expand All @@ -45,6 +53,34 @@ impl Display for WrongType {
}
}

#[derive(Debug, Clone)]
pub struct WrongFunctionArguments {
pub expected: String,
pub got: String,
pub declaration_pos: Range<usize>,
}

impl WrongFunctionArguments {
pub fn from_expressed_type(
expected: usize,
got: usize,
declaration_pos: Range<usize>,
program: &Program,
) -> Self {
Self {
got: program.types.get(got).unwrap().format(program).to_string(),
expected: program
.types
.get(expected)
.unwrap()
.clone()
.format(program)
.to_string(),
declaration_pos,
}
}
}

#[derive(Debug, Clone)]
pub enum CompilerErrorKind {
UndefinedVariable(String),
Expand All @@ -58,6 +94,7 @@ pub enum CompilerErrorKind {
expected: usize,
got: usize,
},
WrongFunctionArguments(WrongFunctionArguments),
RedeclaredVariable(String, Range<usize>),
NotSupported(String),
WrongAssignmentType {
Expand Down Expand Up @@ -136,6 +173,22 @@ impl CompilerError {
self.pos.clone(),
)]
}
CompilerErrorKind::WrongFunctionArguments(WrongFunctionArguments {
expected,
got,
declaration_pos,
}) => {
vec![
(
format!("Wrong type in function call: {got}"),
self.pos.clone(),
),
(
format!("Expected {expected} in declaration"),
declaration_pos.clone(),
),
]
}
CompilerErrorKind::WrongAssignmentType {
got,
typ,
Expand Down
6 changes: 3 additions & 3 deletions src/compiler/program/assignment.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use super::{
builder::Builder,
ir::{ExpressedType, Function, FunctionId, FunctionTemplate, Type},
ir::{ExpressedType, Function, FunctionId, FunctionTemplate, FunctionTypeKind, Type},
Program,
};
use crate::{
Expand Down Expand Up @@ -115,11 +115,11 @@ impl Program {
id: *type_id,
arguments: None,
}),
TypeKind::Function {
TypeKind::Function(FunctionTypeKind {
parameter_type_ids,
vararg_parameter_type_id,
declaration_pos,
} => todo!(),
}) => todo!(),
TypeKind::UserDefined {
identifier,
declaration_pos,
Expand Down
Loading

0 comments on commit 1182799

Please sign in to comment.