Amalgams¶
Internal documentation for the amalgam.amalgams
module.
-
class
amalgam.amalgams.
Located
[source]¶ The base dataclass for encapsulating location data of nodes.
Provides an API similar to Lark’s
Token
class for convenience.-
line_span
¶ Lines spanned by a node
- Type
Tuple[int, int]
-
column_span
¶ Columns spanned by a node
- Type
Tuple[int, int]
-
located_on
(*, lines: Tuple[int, int] = (- 1, - 1), columns: Tuple[int, int] = (- 1, - 1)) → amalgam.amalgams.Located[source]¶ Helper method for setting
Located.line_span
andLocated.column_span
.
-
property
column
¶ The starting column number of a node.
-
property
end_column
¶ The ending column number of a node.
-
property
end_line
¶ The ending line number of a node.
-
property
line
¶ The starting line number of a node.
-
-
class
amalgam.amalgams.
Amalgam
[source]¶ The abstract base class for language constructs.
-
bind
(environment: Environment) → Amalgam[source]¶ Protocol for implementing environment binding for
Function
.This base implementation is responsible for allowing bind to be called on other
Amalgam
subclasses by performing no operation aside from returningself
.
-
call
(environment: Environment, *arguments: Amalgam) → Amalgam[source]¶ Protocol for implementing function calls for
Function
.This base implementation is responsible for making the type signature of
SExpression.func
to properly type check whenSExpression.evaluate()
is called, as well as returning a fatalNotification
for non-callable types.
-
-
class
amalgam.amalgams.
Atom
(value: str)[source]¶ An
Amalgam
that represents different atoms.-
value
¶ The name of the atom.
- Type
str
-
-
class
amalgam.amalgams.
Numeric
(value: N)[source]¶ An
Amalgam
that wraps around numeric types.Parameterized as a
Generic
by:N = TypeVar("N", int, float, Fraction)
-
value
¶ The numeric value being wrapped.
- Type
N
-
-
class
amalgam.amalgams.
Symbol
(value: str)[source]¶ An
Amalgam
that wraps around symbols.-
value
¶ The name of the symbol.
- Type
str
-
evaluate
(environment: Environment) → Amalgam[source]¶ Searches the provided environment fully with
Symbol.value
. Returns theAmalgam
object bound to theSymbol.value
in the environment. Returns a fatalNotification
if a binding is not found.
-
-
class
amalgam.amalgams.
Function
(name: str, fn: Callable[[…], amalgam.amalgams.Amalgam], defer: bool = False, contextual: bool = False)[source]¶ An
Amalgam
that wraps around functions.-
name
¶ The name of the function.
- Type
str
-
fn
¶ The function being wrapped. Must have the signature: (env, amalgams…) -> amalgam.
- Type
Callable[..., Amalgam]
-
defer
¶ If set to
False
, arguments are evaluated before being passed toFunction.fn
.- Type
bool
-
contextual
¶ If set to
True
, disallows function calls whenFunction.in_context
is set toFalse
.- Type
bool
-
env
¶ The
environment.Environment
instance bound to the function. Overrides the environment parameter passed to theFunction.call()
method.
-
in_context
¶ Predicate that disallows functions to be called outside of specific contexts. Makes
Function.call()
return a fatalNotification
when set toFalse
andFunction.contextual
is set toTrue
.- Type
bool
-
bind
(environment: Environment) → Function[source]¶ Sets the
Function.env
attribute and returns the sameFunction
reference.
-
call
(environment: Environment, *arguments: Amalgam) → Amalgam[source]¶ Performs the call to the
Function.fn
attribute.Performs pre-processing depending on the values of
Function.defer
,Function.contextual
, andFunction.in_context
,
-
with_name
(name: str) → amalgam.amalgams.Function[source]¶ Sets the
Function.name
attribute and returns the sameFunction
reference.
-
-
class
amalgam.amalgams.
SExpression
(*vals: amalgam.amalgams.Amalgam)[source]¶ An
Amalgam
that wraps around S-Expressions.-
vals
¶ Entities contained by the S-Expression.
- Type
Tuple[Amalgam, ...]
-
evaluate
(environment: Environment) → Amalgam[source]¶ Evaluates
func
using environment before invoking thecall()
method with environment andSExpression.args
.
-
property
args
¶ The rest of the
SExpression.vals
.
-
property
func
¶ The head of the
SExpression.vals
.
-
-
class
amalgam.amalgams.
Vector
(*vals: T)[source]¶ An
Amalgam
that wraps around a homogenous vector.Parameterized as a
Generic
by:T = TypeVar("T", bound=Amalgam)
-
vals
¶ Entities contained by the vector
- Type
Tuple[T, ...]
-
mapping
¶ Mapping representing vectors with
Atom
s for odd indices andAmalgam
s for even indices.- Type
Mapping[str, Amalgam]
-
_as_mapping
() → Mapping[str, amalgam.amalgams.Amalgam][source]¶ Attemps to create a
Mapping[str, Amalgam]
fromVector.vals
.Odd indices must be
Atom
s and even indices must beAmalgam
s. Returns an empty mapping if this form is not met.
-
evaluate
(environment: Environment) → Amalgam[source]¶ Creates a new
Vector
by evaluating every value inVector.vals
.
-
-
class
amalgam.amalgams.
Quoted
(value: T)[source]¶ An
Amalgam
that defers evaluation of otherAmalgam
s.Parameterized as a
Generic
by:T = TypeVar("T", bound=Amalgam)
-
class
amalgam.amalgams.
Internal
(value: P)[source]¶ An
Amalgam
that holds Pythonobject
s.Parameterized as a
Generic
by:P = TypeVar("P", bound=object)
-
value
¶ The Python
object
being wrapped.- Type
P
-
-
class
amalgam.amalgams.
Trace
(amalgam: Amalgam, environment: Environment, message: str)[source]¶ Encapsulates information for tracking notifications.
-
property
amalgam
¶ Alias for field number 0
-
property
environment
¶ Alias for field number 1
-
property
message
¶ Alias for field number 2
-
property
-
class
amalgam.amalgams.
Notification
(*, fatal: bool = True, payload: amalgam.amalgams.Amalgam = <Atom 'NIL' @ 0x7f42075dd810>)[source]¶ An
Amalgam
that encapsulates and tracks notifications.-
fatal
¶ Specifies whether the notification should unconditionally propagate and halt evaluation.
- Type
bool
-
evaluate
(_environment: Environment) → Notification[source]¶ Evaluates to the same
Notification
reference.
-
make_report
(text: str, source: str = '<unknown>') → str[source]¶ Generates a report to be printed to
sys.stderr
.Accepts
text
andsource
for prettified output.
-
pop
() → amalgam.amalgams.Trace[source]¶ Pops a
Trace
fromNotification.trace
.
-
push
(amalgam: Amalgam, environment: Environment, message: str) → None[source]¶ Pushes a
Trace
intoNotification.trace
.
-