Skip to content

Commit 486ad7b

Browse files
committed
Can now create simple hardcoded IFn functions and invoke them
1 parent eae0437 commit 486ad7b

File tree

1 file changed

+63
-0
lines changed

1 file changed

+63
-0
lines changed

src/main.rs

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
use std::any::{Any, TypeId};
2+
use std::collections::HashMap;
3+
use std::hash::{Hash,Hasher};
4+
use dyn_clone::DynClone;
5+
6+
#[derive(Hash,PartialEq,Eq,Copy,Clone)]
7+
struct Symbol {
8+
name: &'static str
9+
}
10+
11+
trait IFn: DynClone {
12+
fn invoke(&self,args: &[&dyn Any]) -> Box<dyn Any>;
13+
}
14+
dyn_clone::clone_trait_object!(IFn);
15+
16+
#[derive(Clone)]
17+
struct AddFn {
18+
}
19+
impl IFn for AddFn {
20+
fn invoke(&self,args: &[&dyn Any]) -> Box<dyn Any>
21+
{
22+
Box::new(args.into_iter().fold(0,|a,b| {
23+
let _a = a as i64;
24+
if let Some(_b) = b.downcast_ref::<i64>() {
25+
_a + *_b
26+
}
27+
else {
28+
_a
29+
}
30+
31+
}))
32+
}
33+
}
34+
use crate::Expr::*;
35+
#[derive(Clone)]
36+
enum Expr {
37+
SymbolExpr(Symbol),
38+
FnExpr(Box<dyn IFn>),
39+
i32Expr(i32),
40+
nilExpr
41+
}
42+
impl Expr {
43+
fn eval(&self,environment: &HashMap<Symbol,Expr>) -> Expr{
44+
match self {
45+
SymbolExpr(sym) => match environment.get(&sym) {
46+
Some(expr) => expr.clone(),
47+
_ => nilExpr
48+
},
49+
_ => nilExpr
50+
}
51+
52+
}
53+
}
54+
55+
fn main() {
56+
let add = AddFn{};
57+
let answer = add.invoke(&[&(1 as i64) as &dyn Any ,&(2 as i64) as &dyn Any]);
58+
let answer_as_num = answer.downcast_ref::<i64>();// as Box<i64>;
59+
60+
println!("Start:");
61+
println!("{:?}",answer_as_num);
62+
println!("Edn:");
63+
}

0 commit comments

Comments
 (0)