Skip to content

Commit

Permalink
improve API for retrieving bindings and prepare 0.9.1 release
Browse files Browse the repository at this point in the history
  • Loading branch information
kaikalii committed Feb 26, 2024
1 parent 0414a55 commit 8503864
Show file tree
Hide file tree
Showing 7 changed files with 59 additions and 10 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ name = "uiua"
readme = "readme.md"
repository = "https://github.com/uiua-lang/uiua"
rust-version = "1.75"
version = "0.9.0"
version = "0.9.1"

[dependencies]
# Core dependencies
Expand Down
3 changes: 3 additions & 0 deletions changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
Uiua is not yet stable.
<!-- This version is not yet released. If you are reading this on the website, then these changes are live here. -->

## 0.9.1 - 2024-02-25
- Improve Rust library API for getting bindings' values

## 0.9.0 - 2024-02-25
### Language
- **Breaking Change** - [`repeat ⍥`](https://uiua.org/docs/repeat) with [`infinity ∞`](https://uiua.org/docs/infinity) now does a fixed-point iteration rather than an infinite loop
Expand Down
2 changes: 1 addition & 1 deletion src/format.rs
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ The following configuration options are available:
let asm = Compiler::new().print_diagnostics(true).load_file(file_path)?.finish();
let mut env = Uiua::with_backend(SafeSys::default());
env.run_asm(&asm)?;
let mut bindings = env.all_values_in_scope();
let mut bindings = env.bound_values();
$(
let $name = {
let requirement = requirement!([<$name:camel>], $ty);
Expand Down
29 changes: 29 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ uiua.run_str("+").unwrap();
let res = uiua.pop_int().unwrap();
assert_eq!(res, 3);
```
Sometimes, you need to configure the compiler before running.
You can create a new compiler with [`Compiler::new`]. Strings or files can be compiled with [`Compiler::load_str`] or [`Compiler::load_file`] respectively.
Expand All @@ -52,6 +53,7 @@ uiua.run_asm(&asm).unwrap();
let res = uiua.pop_int().unwrap();
assert_eq!(res, 8);
```
This can be shortened a bit with [`Uiua::compile_run`].
```rust
use uiua::*;
Expand All @@ -61,6 +63,7 @@ uiua.compile_run(|comp| {
comp.print_diagnostics(true).load_str("+ 3 5")
});
```
You can create and bind Rust functions with [`Compiler::create_function`], [`Compiler::bind_function`], and [`Compiler::create_bind_function`]
```rust
use uiua::*;
Expand All @@ -80,6 +83,32 @@ uiua.run_asm(asm).unwrap();
let res = uiua.pop_num().unwrap();
assert_eq!(res, 5.0);
```
Bindings can be retrieved with [`Uiua::bound_values`] or [`Uiua::bound_functions`].
```rust
use uiua::*;
let mut uiua = Uiua::with_native_sys();
uiua.run_str("
X ← 5
F ← +1
").unwrap();
let x = uiua.bound_values().remove("X").unwrap();
assert_eq!(x.as_int(&uiua, "").unwrap(), 5);
let f = uiua.bound_functions().remove("F").unwrap();
let mut comp = Compiler::new().with_assembly(uiua.take_asm());
comp.create_bind_function("AddTwo", (1, 1), move |uiua| {
uiua.call(f.clone())?;
uiua.call(f.clone())
}).unwrap();
comp.load_str("AddTwo 3").unwrap();
uiua.run_asm(comp.finish()).unwrap();
let res = uiua.pop_int().unwrap();
assert_eq!(res, 5);
```
You can format Uiua code with the [`mod@format`] module.
```rust
use uiua::format::*;
Expand Down
3 changes: 2 additions & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -860,8 +860,9 @@ fn repl(mut rt: Uiua, mut compiler: Compiler, color: bool, config: FormatConfig)
.load_str(&code)
.and_then(|comp| rt.run_asm(comp.finish()));
print_stack(&rt.take_stack(), color);
let mut asm = rt.take_asm();
match res {
Ok(mut asm) => {
Ok(()) => {
asm.remove_top_level();
*compiler.assembly_mut() = asm;
Ok(true)
Expand Down
28 changes: 22 additions & 6 deletions src/run.rs
Original file line number Diff line number Diff line change
Expand Up @@ -241,6 +241,10 @@ impl Uiua {
{
self.downcast_backend_mut::<T>().map(take)
}
/// Take the assembly
pub fn take_asm(&mut self) -> Assembly {
take(&mut self.asm)
}
/// Set whether to emit the time taken to execute each instruction
pub fn time_instrs(mut self, time_instrs: bool) -> Self {
self.rt.time_instrs = time_instrs;
Expand Down Expand Up @@ -296,8 +300,8 @@ impl Uiua {
self.compile_run(|comp| comp.load_file(path))
}
/// Run a Uiua assembly
pub fn run_asm(&mut self, asm: impl Into<Assembly>) -> UiuaResult<Assembly> {
fn run_asm(env: &mut Uiua, asm: Assembly) -> UiuaResult<Assembly> {
pub fn run_asm(&mut self, asm: impl Into<Assembly>) -> UiuaResult {
fn run_asm(env: &mut Uiua, asm: Assembly) -> UiuaResult {
env.asm = asm;
env.rt.execution_start = instant::now();
let top_slices = take(&mut env.asm.top_slices);
Expand All @@ -322,8 +326,7 @@ impl Uiua {
..Runtime::default()
};
}
let asm = take(&mut env.asm);
res.map(|_| asm)
res
}
run_asm(self, asm.into())
}
Expand Down Expand Up @@ -980,8 +983,10 @@ code:
)
})
}
/// Get the values for all bindings in the current scope
pub fn all_values_in_scope(&self) -> HashMap<Ident, Value> {
/// Get all bound values in the assembly
///
/// Bindings are only given values once the assembly has been run successfully
pub fn bound_values(&self) -> HashMap<Ident, Value> {
let mut bindings = HashMap::new();
for binding in &self.asm.bindings {
if let Global::Const(Some(val)) = &binding.global {
Expand All @@ -991,6 +996,17 @@ code:
}
bindings
}
/// Get all bound functions in the assembly
pub fn bound_functions(&self) -> HashMap<Ident, Function> {
let mut bindings = HashMap::new();
for binding in &self.asm.bindings {
if let Global::Func(f) = &binding.global {
let name = binding.span.as_str(self.inputs(), |s| s.into());
bindings.insert(name, f.clone());
}
}
bindings
}
/// Clone `n` values from the top of the stack
///
/// Values are cloned in the order they were pushed
Expand Down

0 comments on commit 8503864

Please sign in to comment.