Skip to content

Adição do Meta e StdLib -> Layr de Andrade Neto 222001331, Bruno Henrique Silva De Jesus 211055423, Alisson Vieira Neves 221002067 #42

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 20 commits into
base: main
Choose a base branch
from
Open
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
39 changes: 39 additions & 0 deletions src/interpreter/interpreter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ pub fn eval(exp: Expression, env: &Environment<EnvValue>) -> Result<EnvValue, Er
Expression::IsError(e) => eval_iserror_expression(*e, env),
Expression::IsNothing(e) => eval_isnothing_expression(*e, env),
Expression::FuncCall(name, args) => call(name, args, env),
Expression::MetaExp(f, args, _) => meta(f, args, env),
_ if is_constant(exp.clone()) => Ok(EnvValue::Exp(exp)),
_ => Err((String::from("Not implemented yet."), None)),
}
Expand Down Expand Up @@ -822,12 +823,29 @@ fn eval_err(exp: Expression, env: &Environment<EnvValue>) -> Result<EnvValue, Er
}
}

fn meta(
f: fn(Vec<EnvValue>) -> Result<EnvValue, String>,
args: Vec<Expression>,
env: &Environment<EnvValue>,
) -> Result<EnvValue, (String, Option<Expression>)> {
let mut args_values = Vec::new();
for expr in args {
let value = eval(expr, env)?;
args_values.push(value);
}
let result_value = f(args_values).map_err(|s| (s, None))?;

Ok(result_value)
}

#[cfg(test)]
mod tests {
use super::*;
use crate::ir::ast::Expression::*;
use crate::ir::ast::Function;
use crate::ir::ast::Statement::*;
use crate::ir::ast::Type;
use crate::stdlib::math::sqrt_impl;
use std::collections::HashMap;
//use crate::ir::ast::Type;
use crate::ir::ast::Type::*;
Expand Down Expand Up @@ -2159,4 +2177,25 @@ mod tests {
Err(s) => assert!(false, "{}", s),
}
}

#[test]
fn interpreter_metaexp_sqrt_returns_correct_value() {
let mut env = Environment::<EnvValue>::new();
env.insert_variable("x".to_string(), EnvValue::Exp(Expression::CReal(25.0)));

let meta_expr = Expression::MetaExp(
sqrt_impl,
vec![Expression::Var("x".to_string())],
Type::TReal,
);

let result_value = eval(meta_expr, &env).expect("Evaluation failed");

match result_value {
EnvValue::Exp(Expression::CReal(v)) => {
assert!((v - 5.0).abs() < 0.0001, "Expected 5.0, got {}", v)
}
_ => panic!("Expected a CReal result"),
}
}
}
7 changes: 7 additions & 0 deletions src/ir/ast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ pub type Name = String;
use nom::IResult;
use std::collections::HashMap;

use crate::interpreter::interpreter::EnvValue;

#[derive(Debug, PartialEq, Clone)]
pub struct Frame<A> {
pub parent_function: Option<Function>,
Expand Down Expand Up @@ -164,6 +166,11 @@ pub enum Expression {

/* function call */
FuncCall(Name, Vec<Expression>),
MetaExp(
fn(Vec<EnvValue>) -> Result<EnvValue, String>,
Vec<Expression>,
Type,
),

/* arithmetic expressions over numbers */
Add(Box<Expression>, Box<Expression>),
Expand Down
1 change: 1 addition & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ use std::io::Write;*/
pub mod interpreter;
pub mod ir;
pub mod parser;
pub mod stdlib;
pub mod tc;

fn main() {
Expand Down
3 changes: 3 additions & 0 deletions src/stdlib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
pub mod math;
pub mod stdlib;
pub mod string;
Loading
Loading