Abacus.Runtime.Scope (abacus v2.0.0)

Contains helper functions to work with the scope of an Abacus script at runtime

Link to this section Summary

Functions

Returns a scope with some default functions and constants

Tries to get values from a subject.

Renames first-level variables into their respective :"var#{x}" atoms

Link to this section Types

Specs

index() :: integer()

Specs

key() :: String.t() | atom()

Link to this section Functions

Link to this function

default_scope()

Returns a scope with some default functions and constants

Link to this function

get_in(subject, key_or_index, var_lookup \\ %{})

Specs

get_in(nil, key() | index(), map()) :: nil
get_in(map(), key(), map()) :: nil | term()
get_in(list(), index(), map()) :: nil | term()
get_in(list(), key(), map()) :: nil | term()

Tries to get values from a subject.

Subjects can be:

  • maps
  • keyword lists
  • lists (with integer keys)
  • nil (special case so access to undefined variables will not crash execution)

Keys can be:

  • strings
  • atoms (you can pass a resolve map that converts the given atom key into a string key)
  • positive integers (for lists only)

The Abacus parser will reference this function extensively like this:

a.b[2] will turn into

get_in(get_in(var0, :var1, var_lookup), 2, var_lookup)

in this case, var_lookup will be:

%{
  var1: "a",
  var2: "b"
}
Link to this function

prepare_scope(scope, lookup)

Renames first-level variables into their respective :"var#{x}" atoms