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:

Methods

addFunction(fn)

Add a JME function to the scope.

Parameters:
Name Type Description
fn Numbas.jme.funcObj

function to add

Source:

addRuleset(name, set)

Add a ruleset to the scope.

Parameters:
Name Type Description
name String
set Numbas.jme.rules.Ruleset
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

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:

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:

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:

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:

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

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.funcObj
Source:

resolve(collection, name) → {Object}

Get the object with given name from the given collection

Parameters:
Name Type Description
collection String

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

name String

the name of the object to retrieve

Returns:
Type Description
Object
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: