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 |
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 |
rulesets |
Object.<Numbas.jme.rules.Ruleset> | Dictionary of rulesets defined at this level in the scope. To resolve a ruleset in the scope, use |
deleted |
Numbas.jme.scope_deletions | Names of deleted variables/functions/rulesets. |
question |
Numbas.Question | The question this scope belongs to. |
Members
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 |
|
addRuleset(name, set)
Add a ruleset to the scope.
Parameters:
Name | Type | Description |
---|---|---|
name |
string | |
set |
Numbas.jme.rules.Ruleset |
allConstants() → {Object.<Numbas.jme.token>}
Gather all variables defined in this scope.
Returns:
Type | Description |
---|---|
Object.<Numbas.jme.token> |
A dictionary of variables. |
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}. |
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. |
allVariables() → {Object.<Numbas.jme.token>}
Gather all variables defined in this scope.
Returns:
Type | Description |
---|---|
Object.<Numbas.jme.token> |
A dictionary of variables. |
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 |
Returns:
Type | Description |
---|---|
object |
a dictionary of names to values |
deleteConstant(name)
Mark the given constant name as deleted from the scope.
Parameters:
Name | Type | Description |
---|---|---|
name |
string |
deleteFunction(name)
Mark the given function name as deleted from the scope.
Parameters:
Name | Type | Description |
---|---|---|
name |
string |
deleteRuleset(name)
Mark the given ruleset name as deleted from the scope.
Parameters:
Name | Type | Description |
---|---|---|
name |
string |
deleteVariable(name)
Mark the given variable name as deleted from the scope.
Parameters:
Name | Type | Description |
---|---|---|
name |
string |
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 |
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 |
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.
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 |
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. |
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 |
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 |
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 |
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 |
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 |
name |
string | The name of the object to retrieve. |
Returns:
Type | Description |
---|---|
object |
setConstant(name, data)
Set the given constant name.
Parameters:
Name | Type | Description |
---|---|---|
name |
string | |
data |
Numbas.jme.constant_definition |
setRuleset(name, rules)
Set the given ruleset name.
Parameters:
Name | Type | Description |
---|---|---|
name |
string | |
rules |
Array.<Numbas.jme.rules.Ruleset> |
setVariable(name, value)
Set the given variable name.
Parameters:
Name | Type | Description |
---|---|---|
name |
string | |
value |
Numbas.jme.token |
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 |
Returns:
Type | Description |
---|---|
Numbas.jme.Scope |