[][src]Trait litto::Interpreter

pub trait Interpreter: 'static {
    type Value: Clone;
    type Error: From<String> + From<&'static String>;
    type Env;
    type Expr;
    fn parse(code: &str) -> Result<Self::Expr, Self::Error>;
fn global_env() -> Self::Env;
fn evaluate(
        env: &Self::Env,
        expr: &Self::Expr
    ) -> Result<Self::Value, Self::Error>; fn interpret(code: &str) -> Result<Self::Value, Self::Error> { ... } }

A lightweight interpreter interface.

Associated Types

type Value: Clone

The value type the interpreter uses and returns.

It is usually a trait object that allows supporting different kinds of actual values.

Refer to the value module for example implementations.

type Error: From<String> + From<&'static String>

The error type that the interpreter raises.

Could be a string, or the same as the Value type, or a dedicated error type.

type Env

The environment type that is used to evaluate code.

Environments are complementary (mutable) structures that helps resolving static expressions.

For example, environments might include these kinds of information:

  • The state (registers, heap and the stack). Tracking local and outer variables.
  • What to do next (the program counter). Making it possible to suspend and resume execution. This would require explicit support from the stdlib so execution can be suspended and resumed.

Refer to the env module for example implementations.

type Expr

The expression type.

The type that can represent parsed code of this language. If the language wants to operate code as data, then there needs a way to convert Expr to Value. For interpreters that use bytecode, this might also include the bytecode.

Refer to the expr module for example implementations.

Loading content...

Required methods

fn parse(code: &str) -> Result<Self::Expr, Self::Error>

Parse code into an Expr.

For interpreters that use bytecode, consider compiling the code into bytecode in this function.

fn global_env() -> Self::Env

Define the default global environment.

Usually this defines the standard library.

fn evaluate(
    env: &Self::Env,
    expr: &Self::Expr
) -> Result<Self::Value, Self::Error>

Evaluate an expression in a given environment.

Loading content...

Provided methods

fn interpret(code: &str) -> Result<Self::Value, Self::Error>

Evaluate code in string. This is a shortcut for calling parse, and evaluate with global_env().

Loading content...

Implementors

impl Interpreter for MiniLang[src]

type Value = <TinyLang as Interpreter>::Value

type Error = <TinyLang as Interpreter>::Error

type Env = <TinyLang as Interpreter>::Env

type Expr = <TinyLang as Interpreter>::Expr

impl Interpreter for TinyLang[src]

type Value = Value<TinyLang>

type Error = String

type Env = KvEnv<Self::Value>

type Expr = Sexp<Self::Value>

Loading content...