Skip to content

Commit ee339c2

Browse files
committed
feat: implement function call validation and add corresponding tests
1 parent eb33449 commit ee339c2

12 files changed

Lines changed: 156 additions & 34 deletions

File tree

crates/validator/src/expr.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,10 @@ pub fn validate_expr(expr: &mut Expr, ctx: &mut Validator) -> Result<(), Validat
6969
index,
7070
target_type,
7171
} => Ok(()),
72+
Expr::Return(e) => {
73+
validate_expr(&mut **e, ctx)?;
74+
Ok(())
75+
}
7276
Expr::TemplateString(chunk) => {
7377
for ch in chunk {
7478
if let TemplateChunk::Expr(expr) = ch {

crates/validator/src/lib.rs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,26 @@ impl Validator {
103103
}
104104

105105
pub fn validate(&mut self, parsed_program: &mut Program) -> Result<(), ValidatorError> {
106+
for func in parsed_program.functions.iter_mut() {
107+
for param in &func.1.params {
108+
self.global_variables.insert(
109+
param.name.clone(),
110+
Symbol {
111+
typ: param.typ.clone(),
112+
is_mutable: param.is_mutable,
113+
is_pointer: param.is_pointer,
114+
is_used: false,
115+
is_changed: false,
116+
},
117+
);
118+
}
119+
for stmt in func.1.body.iter_mut() {
120+
validate_statement(stmt, self)?;
121+
}
122+
for param in &func.1.params {
123+
self.global_variables.remove(&param.name);
124+
}
125+
}
106126
for expr in parsed_program.expressions.iter_mut() {
107127
validate_statement(expr, self)?;
108128
}
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
#[cfg(test)]
2+
mod tests {
3+
use std::collections::HashMap;
4+
5+
use parser::{
6+
ast::{Expr, FunctionDef, Parameter, Program, Statement, Symbol},
7+
shared_ast::Type,
8+
};
9+
10+
use crate::{Validator, validate};
11+
12+
#[test]
13+
fn function_call_with_argument() {
14+
let function = FunctionDef {
15+
params: vec![Parameter {
16+
name: "a".to_string(),
17+
typ: Type::Integer,
18+
is_mutable: false,
19+
is_pointer: false,
20+
}],
21+
body: vec![Statement::Expr(Expr::Return(Box::new(Expr::VariableRef {
22+
name: "a".to_string(),
23+
symbol: None,
24+
})))],
25+
return_type: Some(Type::Integer),
26+
};
27+
let mut functions = HashMap::new();
28+
functions.insert("Hello".to_string(), function);
29+
let mut program = Program {
30+
functions,
31+
expressions: vec![],
32+
};
33+
34+
let mut validator = Validator::new();
35+
validator.validate(&mut program);
36+
assert_eq!(
37+
program.functions.get("Hello"),
38+
Some(&FunctionDef {
39+
params: vec![Parameter {
40+
name: "a".to_string(),
41+
typ: Type::Integer,
42+
is_mutable: false,
43+
is_pointer: false,
44+
}],
45+
body: vec![Statement::Expr(Expr::Return(Box::new(Expr::VariableRef {
46+
name: "a".to_string(),
47+
symbol: Some(Symbol {
48+
typ: Type::Integer,
49+
is_mutable: false,
50+
is_pointer: false,
51+
is_used: true,
52+
is_changed: false,
53+
})
54+
})))],
55+
return_type: Some(Type::Integer),
56+
})
57+
)
58+
}
59+
}

crates/validator/src/tests/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
mod assignment;
2+
mod function_call;
23
mod variable_decl;

crates/validator/src/validate.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -127,9 +127,11 @@ pub fn validate_statement(stmt: &mut Statement, ctx: &mut Validator) -> Result<(
127127
_ => todo!("Bura baxmaq lazımdır {:#?}", function),
128128
}
129129
}
130-
_ => todo!("Bura baxmaq lazımdır {:#?}", expr),
130+
_ => {
131+
return validate_expr(expr, ctx);
132+
}
131133
},
132-
_ => todo!("Bura baxmaq lazımdır {:#?}", stmt),
134+
_ => todo!("Bura baxmaq lazımdır {:#?}", stmt),
133135
}
134136

135137
Ok(())

interpreter/src/runner/binary_op.rs

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,9 @@
1-
use parser::{
2-
ast::{Expr, Operation},
3-
shared_ast::Type,
4-
};
1+
use parser::{ast::Operation, shared_ast::Type};
52

63
use crate::runner::{Runner, runner::Value};
74
//TODO: Burası güncellene bilinir
85
pub fn binary_op_runner(
9-
ctx: &mut Runner,
6+
_ctx: &mut Runner,
107
left: Value,
118
right: Value,
129
op: Operation,

interpreter/src/runner/builtin/mod.rs

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,8 @@
1-
use parser::{
2-
ast::Expr,
3-
shared_ast::{BuiltInFunction, Type},
4-
};
1+
use parser::shared_ast::{BuiltInFunction, Type};
52

63
use crate::runner::{
7-
Runner,
84
builtin::{input::input, sum::sum},
9-
runner::{Value, runner_interpretator},
5+
runner::Value,
106
};
117

128
mod input;
@@ -15,7 +11,7 @@ mod sum;
1511
pub fn builthin_call_runner(
1612
function: BuiltInFunction,
1713
mut args: Vec<Value>,
18-
return_type: Type,
14+
_return_type: Type,
1915
) -> Value {
2016
match function {
2117
BuiltInFunction::Print => {

interpreter/src/runner/function_call.rs

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
use std::{os::unix::process, rc::Rc};
1+
use std::rc::Rc;
22

33
use parser::{
4-
ast::{Expr, Parameter, Statement, Symbol},
4+
ast::{Expr, Statement, Symbol},
55
shared_ast::Type,
66
};
77

@@ -12,11 +12,12 @@ use crate::runner::{
1212

1313
pub fn function_call(
1414
ctx: &mut Runner,
15-
target: Option<Box<Expr>>,
15+
_target: Option<Box<Expr>>,
1616
name: Box<Expr>,
1717
args: Vec<Expr>,
18-
returned_type: Option<Type>,
18+
_returned_type: Option<Type>,
1919
) -> Value {
20+
dbg!(&name);
2021
match *name {
2122
Expr::VariableRef {
2223
name,
@@ -33,13 +34,14 @@ pub fn function_call(
3334
param.name.clone(),
3435
Variable {
3536
value: variable,
36-
typ: Rc::new(param.typ.clone()),
37-
is_mutable: param.is_mutable,
37+
// typ: Rc::new(param.typ.clone()),
38+
// is_mutable: param.is_mutable,
3839
},
3940
);
4041
}
4142
println!("Hellooo");
4243
for stmt in function.body.clone() {
44+
dbg!(&stmt);
4345
match stmt {
4446
Statement::Expr(Expr::Return(e)) => {
4547
return get_primitive_value(ctx, *e, function.return_type);

interpreter/src/runner/helpers.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use crate::runner::{Runner, runner::runner_interpretator};
2-
use parser::ast::{Expr, Statement};
2+
use parser::ast::Statement;
33

44
/*
55
*
@@ -19,7 +19,7 @@ use parser::ast::{Expr, Statement};
1919
}
2020
*/
2121

22-
pub fn run_body<'a>(ctx: &mut Runner, body: Vec<Statement>) {
22+
pub fn run_body(ctx: &mut Runner, body: Vec<Statement>) {
2323
for expr in body {
2424
runner_interpretator(ctx, expr);
2525
}

interpreter/src/runner/mod.rs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ mod helpers;
55
mod runner;
66
mod tests;
77
use parser::{
8-
ast::{Expr, FunctionDef, Parameter, Program, Statement},
8+
ast::{Expr, FunctionDef, Statement},
99
shared_ast::Type,
1010
};
1111

@@ -16,8 +16,6 @@ mod handlers;
1616
#[derive(Debug)]
1717
pub struct Variable {
1818
value: Value,
19-
typ: Rc<Type>,
20-
is_mutable: bool,
2119
}
2220

2321
#[derive(Debug)]

0 commit comments

Comments
 (0)