Jump to navigation

Class: Scope

Numbas.jme.Scope

new Scope(scopes)

A JME evaluation environment. Stores variable, function, and ruleset definitions.

A scope may have a parent; elements of the scope are resolved by searching up through the hierarchy of parents until a match is found.

Parameters:
Name Type Description
scopes Array.<Numbas.jme.Scope>

Either: nothing, in which case this scope has no parents; a parent Scope object; a list whose first element is a parent scope, and the second element is a dictionary of extra variables/functions/rulesets to store in this scope.

Properties:
Name Type Description
variables Object.<Numbas.jme.token>

Dictionary of variables defined at this level in the scope. To resolve a variable in the scope, use Numbas.jme.Scope.getVariable.

functions Object.<Array.<Numbas.jme.funcObj>>

Dictionary of functions defined at this level in the scope. Function names map to lists of functions: there can be more than one function for each name because of multiple dispatch. To resolve a function name in the scope, use Numbas.jme.Scope.getFunction.

rulesets Object.<Numbas.jme.rules.Ruleset>

Dictionary of rulesets defined at this level in the scope. To resolve a ruleset in the scope, use Numbas.jme.Scope.getRuleset.

deleted Numbas.jme.scope_deletions

Names of deleted variables/functions/rulesets.

question Numbas.Question

The question this scope belongs to.

Source:

Members

parser :Numbas.jme.Parser

Parser to use when compiling expressions.

Type:
Source:

Methods

addFunction(fn) → {Numbas.jme.funcObj}

Add a JME function to the scope.

Parameters:
Name Type Description
fn Numbas.jme.funcObj

Function to add.

Returns:
Type Description
Numbas.jme.funcObj
  • The function.
Source:

addRuleset(name, set)

Add a ruleset to the scope.

Parameters:
Name Type Description
name string
set Numbas.jme.rules.Ruleset
Source:

allConstants() → {Object.<Numbas.jme.token>}

Gather all variables defined in this scope.

Returns:
Type Description
Object.<Numbas.jme.token>

A dictionary of variables.

Source:

allFunctions() → {Object.<Array.<Numbas.jme.funcObj>>}

Gather all functions defined in this scope.

Returns:
Type Description
Object.<Array.<Numbas.jme.funcObj>>

A dictionary of function definitions: each name maps to a list of @link{Numbas.jme.funcObj}.

Source:

allRulesets() → {Object.<Numbas.jme.rules.Ruleset>}

Gather all rulesets defined in this scope.

Returns:
Type Description
Object.<Numbas.jme.rules.Ruleset>

A dictionary of rulesets.

Source:

allVariables() → {Object.<Numbas.jme.token>}

Gather all variables defined in this scope.

Returns:
Type Description
Object.<Numbas.jme.token>

A dictionary of variables.

Source:

collect(collection) → {object}

Collect together all items from the given collection.

Parameters:
Name Type Description
collection string

The name of the collection. A property of this Scope object, i.e. one of variables, functions, rulesets.

Returns:
Type Description
object

a dictionary of names to values

Source:

deleteConstant(name)

Mark the given constant name as deleted from the scope.

Parameters:
Name Type Description
name string
Source:

deleteFunction(name)

Mark the given function name as deleted from the scope.

Parameters:
Name Type Description
name string
Source:

deleteRuleset(name)

Mark the given ruleset name as deleted from the scope.

Parameters:
Name Type Description
name string
Source:

deleteVariable(name)

Mark the given variable name as deleted from the scope.

Parameters:
Name Type Description
name string
Source:

evaluate(expr, variablesopt, noSubstitutionopt) → {Numbas.jme.token}

Evaluate an expression in this scope - equivalent to Numbas.jme.evaluate(expr,this).

Parameters:
Name Type Attributes Description
expr JME
variables Object.<(Numbas.jme.token|object)> <optional>

Dictionary of variables to sub into expression. Values are automatically wrapped up as JME types, so you can pass raw JavaScript values.

noSubstitution boolean <optional>

If true, don't substitute variable values from the scope into the expression.

Returns:
Type Description
Numbas.jme.token
Source:

expandJuxtapositions(tree, options) → {Numbas.jme.tree}

Expand juxtapositions in variable and function names for implicit multiplication or composition.

Parameters:
Name Type Description
tree Numbas.jme.tree
options Numbas.jme.expand_juxtapositions_options
Returns:
Type Description
Numbas.jme.tree
Source:

flatten()

Gather all members of this scope into this scope object. A backwards-compatibility hack for questions that use question.scope.variables.x Shouldn't be applied to any scope other than the question scope.

Source:

getConstant(name) → {Numbas.jme.token}

Find the value of the variable with the given name, if it's defined.

Parameters:
Name Type Description
name string
Returns:
Type Description
Numbas.jme.token
Source:

getFunction(name) → {Array.<Numbas.jme.funcObj>}

Get all definitions of the given function name.

Parameters:
Name Type Description
name string
Returns:
Type Description
Array.<Numbas.jme.funcObj>

A list of all definitions of the given name.

Source:

getRuleset(name) → {Numbas.jme.rules.Ruleset}

Get the ruleset with the gien name.

Parameters:
Name Type Description
name string
Returns:
Type Description
Numbas.jme.rules.Ruleset
Source:

getVariable(name) → {Numbas.jme.token}

Find the value of the variable with the given name, if it's defined.

Parameters:
Name Type Description
name string
Returns:
Type Description
Numbas.jme.token
Source:

isConstant(value) → {object}

If the given value is equal to one of the constant defined in this scope, return the constant.

Parameters:
Name Type Description
value Numbas.jme.token
Returns:
Type Description
object
Source:

matchFunctionToArguments(tok, args) → {Numbas.jme.call_signature}

Get the definition of the function with the given name which matches the types of the given arguments.

Parameters:
Name Type Description
tok Numbas.jme.token

The token of the function or operator.

args Array.<Numbas.jme.token>
Returns:
Type Description
Numbas.jme.call_signature
Source:

resolve(collection, name) → {object}

Get the object with given name from the given collection.

Parameters:
Name Type Description
collection string

The name of the collection. A property of this Scope object, i.e. one of constants, variables, functions, rulesets.

name string

The name of the object to retrieve.

Returns:
Type Description
object
Source:

setConstant(name, data)

Set the given constant name.

Parameters:
Name Type Description
name string
data Numbas.jme.constant_definition
Source:

setRuleset(name, rules)

Set the given ruleset name.

Parameters:
Name Type Description
name string
rules Array.<Numbas.jme.rules.Ruleset>
Source:

setVariable(name, value)

Set the given variable name.

Parameters:
Name Type Description
name string
value Numbas.jme.token
Source:

unset(defs) → {Numbas.jme.Scope}

Return a new scope created by unsetting the members specified by the given object.

Parameters:
Name Type Description
defs object

A dictionary with elements variables, rulesets and functions, each lists of names to unset.

Returns:
Type Description
Numbas.jme.Scope
Source: