Skip to content

Commit

Permalink
Bend has IO now
Browse files Browse the repository at this point in the history
  • Loading branch information
vkobinski committed Jun 17, 2024
1 parent b5479e2 commit d5611df
Show file tree
Hide file tree
Showing 6 changed files with 67 additions and 37 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,4 @@ __pycache__
**/*.rs.bk
# MSVC Windows builds of rustc generate these, which store debugging information
*.pdb
.vscode/
7 changes: 5 additions & 2 deletions crates/benda/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,13 @@ edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[lib]
name = "benda"
crate-type = ["cdylib"]
#crate-type = ["cdylib"]

# Debug purposes
crate-type = ["lib"]

[dependencies]
pyo3 = { version = "0.21.2", features = [] }
pyo3 = { version = "0.21.2", features = ["extension-module"] }
bend-lang = "0.2.33"
num-bigint = "0.4.5"
num-traits = "0.2.19"
Expand Down
83 changes: 53 additions & 30 deletions crates/benda/src/types/book.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,19 @@ fn new_err<T>(str: String) -> PyResult<T> {

thread_local!(static GLOBAL_BOOK: RefCell<Option<BendBook>> = const { RefCell::new(None) });

#[pyclass(name = "Term")]
#[derive(Clone, Debug)]
pub struct Term {
term: fun::Term,
}

#[pymethods]
impl Term {
fn __str__(&self) -> String {
self.term.to_string()
}
}

#[pyclass(name = "Ctr")]
#[derive(Clone, Debug)]
pub struct Ctr {
Expand Down Expand Up @@ -51,7 +64,6 @@ impl Ctr {
return Ok(PyString::new_bound(py, &self.entire_name).into_py(py));
}

// TODO : FIX THIS
if let Some(val) = self.fields.get(&object.to_string()) {
Ok(val.clone().into_py(object.py()))
} else {
Expand Down Expand Up @@ -106,39 +118,47 @@ impl Definition {

if let Some(mut b) = bend_book.clone() {
for (arg_num, arg) in args.iter().enumerate() {
let adt = UserAdt::new(arg.clone(), &b);
let arg_name = Name::new(format!("arg{}", arg_num));

let new_arg: Expr;
let mut u_type: Option<fun::Term> = None;

if let Some(adt) = adt {
new_arg = adt.to_bend().unwrap();
if let Ok(term) = arg.downcast::<Term>() {
if let Ok(new_term) = term.extract::<Term>() {
u_type = Some(new_term.term);
}
} else {
new_arg = extract_type_raw(arg.clone())
.unwrap()
.to_bend()
.unwrap();
}
let adt = UserAdt::new(arg.clone(), &b);

let arg_name = Name::new(format!("arg{}", arg_num));

let u_type = new_arg.clone().to_fun();

let def = fun::Definition {
name: arg_name.clone(),
rules: vec![Rule {
pats: vec![],
body: u_type,
}],
builtin: false,
};
let new_arg: Expr;

dbg!(def.clone());
if let Some(adt) = adt {
new_arg = adt.to_bend().unwrap();
} else {
new_arg = extract_type_raw(arg.clone())
.unwrap()
.to_bend()
.unwrap();
}

b.defs.insert(arg_name.clone(), def);
u_type = Some(new_arg.clone().to_fun());
}

new_args.push(Expr::Var {
nam: arg_name.clone(),
});
if let Some(n_type) = u_type {
let def = fun::Definition {
name: arg_name.clone(),
rules: vec![Rule {
pats: vec![],
body: n_type,
}],
builtin: false,
};

b.defs.insert(arg_name.clone(), def);

new_args.push(Expr::Var {
nam: arg_name.clone(),
});
}
}

let first = Stmt::Return {
Expand All @@ -160,14 +180,17 @@ impl Definition {
b.defs
.insert(Name::new("main"), main_def.to_fun(true).unwrap());

println!("Bend: {}", b.display_pretty());
//println!("Bend: {}", b.display_pretty());

let res = benda_ffi::run(&b.clone());

GLOBAL_BOOK.set(bend_book);

return Ok(PyString::new_bound(py, &res.unwrap().0.to_string())
.into_py(py));
let ret_term = Term {
term: res.unwrap().0,
};

return Ok(ret_term.into_py(py));
}

new_err(format!("Could not execute function {}", self.name))
Expand Down
9 changes: 6 additions & 3 deletions crates/benda/src/types/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ pub fn extract_num_raw(
t_type: BuiltinType,
) -> Box<dyn BendType> {
match t_type {
BuiltinType::U24 => Box::new(arg.to_string().parse::<i32>().unwrap()),
BuiltinType::I32 => Box::new(arg.to_string().parse::<i32>().unwrap()),
BuiltinType::F32 => Box::new(arg.to_string().parse::<f32>().unwrap()),
_ => unreachable!(),
Expand All @@ -60,7 +61,8 @@ pub fn extract_type_raw(arg: Bound<PyAny>) -> Option<Box<dyn BendType>> {
let arg_type = BuiltinType::from(name.to_string());

match arg_type {
BuiltinType::U24 => Some(Box::new(extract_inner::<U24>(arg).unwrap())),
//BuiltinType::U24 => Some(Box::new(extract_inner::<U24>(arg).unwrap())),
BuiltinType::U24 => Some(extract_num_raw(arg, BuiltinType::U24)),
BuiltinType::I32 => Some(extract_num_raw(arg, BuiltinType::I32)),
BuiltinType::F32 => Some(extract_num_raw(arg, BuiltinType::F32)),
_ => None,
Expand Down Expand Up @@ -158,7 +160,7 @@ impl From<String> for BuiltinType {
fn from(value: String) -> Self {
match value.as_str() {
"float" => BuiltinType::F32,
"int" => BuiltinType::I32,
"int" => BuiltinType::U24,
"benda.u24" => BuiltinType::U24,
"u24" => BuiltinType::U24,
"benda.Node" => BuiltinType::Node,
Expand Down Expand Up @@ -188,7 +190,8 @@ impl BendType for f32 {
impl BendType for i32 {
fn to_bend(&self) -> ToBendResult {
Ok(imp::Expr::Num {
val: Num::I24(*self),
//val: Num::I24(*self),
val: Num::U24(self.to_u32().unwrap()),
})
}
}
Expand Down
2 changes: 1 addition & 1 deletion examples/quicksort.bend
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ let state = (^ state(<< state 5))

# Sorts and sums n random numbers
# (Main) =
# (Sum (Sort (Rnd 0x100 1)))
# (Sum(Sort(Rnd 10 20)))

# Use an argument from cli
# (Main n) = (Sum (Sort (Rnd (<< 1 n) 1)))
2 changes: 1 addition & 1 deletion result

0 comments on commit d5611df

Please sign in to comment.