Skip to main content

CompilerExt

Trait CompilerExt 

Source
pub trait CompilerExt<'a> {
    // Required method
    fn undefined_variables(
        &self,
    ) -> Result<HashMap<&'a str, Spanned<'a>>, Error<'a>>;
}
Expand description

Compiler extensions defined for some AST nodes, most notably, Block.

§Examples

use arithmetic_parser::grammars::{F32Grammar, Parse, Untyped};
use arithmetic_eval::CompilerExt;

let block = "x = sin(0.5) / PI; y = x * E; (x, y)";
let block = Untyped::<F32Grammar>::parse_statements(block)?;
let undefined_vars = block.undefined_variables()?;
assert_eq!(
    undefined_vars.keys().copied().collect::<HashSet<_>>(),
    HashSet::from_iter(vec!["sin", "PI", "E"])
);
assert_eq!(undefined_vars["PI"].location_offset(), 15);

Required Methods§

Source

fn undefined_variables( &self, ) -> Result<HashMap<&'a str, Spanned<'a>>, Error<'a>>

Returns variables not defined within the AST node, together with the span of their first occurrence.

§Errors
  • Returns an error if the AST is intrinsically malformed. This may be the case if it contains destructuring with the same variable on left-hand side, such as (x, x) = ....

The fact that an error is not returned does not guarantee that the AST node will evaluate successfully if all variables are assigned.

Dyn Compatibility§

This trait is dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety".

Implementations on Foreign Types§

Source§

impl<'a, T: Grammar<'a>> CompilerExt<'a> for Block<'a, T>

Source§

fn undefined_variables( &self, ) -> Result<HashMap<&'a str, Spanned<'a>>, Error<'a>>

Source§

impl<'a, T: Grammar<'a>> CompilerExt<'a> for FnDefinition<'a, T>

Source§

fn undefined_variables( &self, ) -> Result<HashMap<&'a str, Spanned<'a>>, Error<'a>>

Implementors§