JXG.JessieCode

Class

A JessieCode object provides an interface to the parser and stores all variables and objects used within a JessieCode script. The optional argument code is interpreted after initializing. To evaluate more code after initializing a JessieCode instance please use JXG.JessieCode#parse. For code snippets like single expressions use JXG.JessieCode#snippet.

Members

Own

$log : Array

Description

Store $log messages in case there's no console.

Type
Array
Source
parser/jessiecode.js, line 163

board : JXG.Board

Description

The board which currently is used to create and look up elements.

Type
JXG.Board
Source
parser/jessiecode.js, line 181

builtIn : Object

Description

Built-in functions and constants

Type
Object
Source
parser/jessiecode.js, line 169

creator

Description

Returns a element creator function which takes two parameters: the parents array and the attributes object.

Source
parser/jessiecode.js, line 312

(private) dpstack : Array

Description

A stack to store debug information (like line and column where it was defined) of a parameter

Type
Array
Source
parser/jessiecode.js, line 114

forceValueCall : Boolean

Description

Force slider names to return value instead of node

Type
Boolean
Source
parser/jessiecode.js, line 187

isLHS : Boolean

Description

lhs flag, used by JXG.JessieCode#replaceNames

Type
Boolean
Default Value
false
Source
parser/jessiecode.js, line 150

(private) lhs : Array

Description

Store the left hand side of an assignment. If an element is constructed and no attributes are given, this is used as the element's name.

Type
Array
Source
parser/jessiecode.js, line 143

lineToElement : Object

Description

Keep track of which element is created in which line.

Type
Object
Source
parser/jessiecode.js, line 193

operands : Object

Description

List of all possible operands in JessieCode (except of JSXGraph objects).

Type
Object
Source
parser/jessiecode.js, line 175

(private) propscope : Number

Description

The current scope of the object literal stack JXG.JessieCode#propstack.

Type
Number
Source
parser/jessiecode.js, line 135

(private) propstack : Array

Description

Used to store the property-value definition while parsing an object literal.

Type
Array
Source
parser/jessiecode.js, line 128

(private) pscope : Number

Description

Determines the parameter stack scope.

Type
Number
Source
parser/jessiecode.js, line 121

scope : Object

Description

The global scope.

Type
Object
Source
parser/jessiecode.js, line 93

scopes : Array

Description

Keeps track of all possible scopes every required.

Type
Array
Source
parser/jessiecode.js, line 106

warnLog : String

Description

The id of an HTML node in which innerText all warnings are stored (if no console object is available).

Type
String
Default Value
'jcwarn'
Source
parser/jessiecode.js, line 157

Inherited

none

Methods

Own

L(e) → {Number}

This is used as the global L() function.

Parameters

Name Type Description
e Line

Returns

Type
Number

Details

Source
parser/jessiecode.js, line 2272

V(e) → {Number}

This is used as the global V() function.

Parameters

Name Type Description
e Glider | Slider

Returns

Type
Number

Details

Source
parser/jessiecode.js, line 2263

X(e) → {Number}

This is used as the global X() function.

Parameters

Name Type Description
e Point | Text

Returns

Type
Number

Details

Source
parser/jessiecode.js, line 2245

Y(e) → {Number}

This is used as the global Y() function.

Parameters

Name Type Description
e Point | Text

Returns

Type
Number

Details

Source
parser/jessiecode.js, line 2254

(private) _debug(log)

Output a debugging message. Uses debug console, if available. Otherwise an HTML element with the id "debug" and an innerText property is used.

Parameters

Name Type Description
log String

Details

Source
parser/jessiecode.js, line 2966

_error(msg)

Throws an exception with the given error message.

Parameters

Name Type Description
msg String

Error message

Details

Source
parser/jessiecode.js, line 2978

_genericParse(code, cmd, optionsopt) → {Object}

Generic method to parse JessieCode. This consists of generating an AST with parser.parse, apply simplifying rules from CA and manipulate the AST according to the second parameter "cmd".

Parameters

Name Type Attributes Description
code String

JessieCode code to be parsed

cmd String

Type of manipulation to be done with AST

options Object <optional>

Object with attributes

  • {Boolean} [geonext=false] Geonext compatibility mode.
  • {Boolean} [dontstore=false] If false, the code string is stored in this.code, i.e. in the JessieCode object, e.g. in board.jc.

Returns

Returns result of computation as directed in cmd.

Type
Object

Details

Source
parser/jessiecode.js, line 825

_warn(msg)

Output a warning message using JXG#debug and precedes the message with "Warning: ".

Parameters

Name Type Description
msg String

Details

Source
parser/jessiecode.js, line 2988

add(a, b) → {Number|Array}

  • operator implementation

Parameters

Name Type Description
a Number | Array | Point
b Number | Array | Point

Returns

Type
Number | Array

Details

Source
parser/jessiecode.js, line 2348

area(obj) → {Number}

This is used as the global area() function.

Parameters

Name Type Description
obj Circle | Polygon

Returns

Type
Number

Details

Source
parser/jessiecode.js, line 2281

collectDependencies(node, varnames, result)

Search the parse tree below node for stationary dependencies, i.e. dependencies hard coded into the function.

Parameters

Name Type Description
node Object
varnames Array

List of variable names of the function

result Object

An object where the referenced elements will be stored. Access key is their id.

Details

Source
parser/jessiecode.js, line 1150

(private) compile(ast, jsopt, formatopt)

Compiles a parse tree back to JessieCode.

Parameters

Name Type Attributes Default Description
ast Object
js Boolean <optional>
false

Compile either to JavaScript or back to JessieCode (required for the UI).

format Object <optional>

Options for formatting the output. Depending on some options, the function might return a not re-parsable string. This format options have only effect on JessieCode output.

  • {Boolean} [minParentheses=false] Use minimal amount of parentheses?
  • {Boolean|Number|Function} [constToFixed=false] Use this number or function to format constant values.
  • {Boolean} [printable=false] Adds additional signs or parentheses, e.g. x^0.5 --> x^{0.5}.

Returns

Something

Details

Source
parser/jessiecode.js, line 1731

createNode(type, value, children)

Create a new parse tree node. Basically the same as node(), but this builds the children part out of an arbitrary number of parameters, instead of one array parameter.

Parameters

Name Type Description
type String

Type of node, e.g. node_op, node_var, or node_const

value

The nodes value, e.g. a variables value or a functions body.

children

Arbitrary number of parameters; define the child nodes.

Details

Source
parser/jessiecode.js, line 239

createReplacementNode(node, callValueopt) → {Object}

Replaces node_var nodes with node_op>op_execfun nodes, calling the internal $() function with the id of the element accessed by the node_var node.

Parameters

Name Type Attributes Description
node Object
callValue Boolean <optional>

if true, uses $value() instead of $()

Returns

op_execfun node

Type
Object

Details

Source
parser/jessiecode.js, line 1128

defineBuiltIn() → {Object}

Defines built in methods and constants.

Returns

BuiltIn control object

Type
Object

Details

Source
parser/jessiecode.js, line 2691

defineFunction(node) → {function}

Converts a node type node_op and value op_map or op_function into a executable function. Does a simple type inspection.

Parameters

Name Type Description
node Object

Returns

Type
function

Details

See
Source
parser/jessiecode.js, line 644

del(element)

Implementation of the delete() builtin function

Parameters

Name Type Description
element JXG.GeometryElement

Details

Source
parser/jessiecode.js, line 2603

dist(p1, p2) → {Number}

This is used as the global dist() function.

Parameters

Name Type Description
p1 Point
p2 Point

Returns

Type
Number

Details

Source
parser/jessiecode.js, line 2308

div(a, b) → {Number|Array}

Implementation of the / operator.

Parameters

Name Type Description
a Number | Array
b Number

Returns

Type
Number | Array

Details

Source
parser/jessiecode.js, line 2478

eval(v)

Implementation of the eval() builtin function. Calls JXG.evaluate().

Parameters

Name Type Description
v String | Number | function

Details

Source
parser/jessiecode.js, line 2613

(private) execute(node) → {Number|String|Object|Boolean}

Executes a parse subtree.

Parameters

Name Type Description
node Object

Returns

Something

Type
Number | String | Object | Boolean

Details

Source
parser/jessiecode.js, line 1366

findSymbol(v, scopeopt) → {Array}

Find the first symbol to the given value from the given scope upwards.

Parameters

Name Type Attributes Default Description
v

Value

scope Number <optional>
-1

The scope, default is to start with current scope (-1).

Returns

An array containing the symbol and the scope if a symbol could be found, an empty array otherwise;

Type
Array

Details

Source
parser/jessiecode.js, line 2655

format(code, options) → {String}

Format JessieCode. This consists of generating an AST with parser.parse, and compile the AST back to JessieCode with options.

Parameters

Name Type Description
code String

JessieCode code to be parsed.

options Object

For possible options see param "format" in JXG.JessieCode#compile.

Returns

Manipulated JessieCode string. This is no necessarily a parsable JessieCode code!

Type
String

Details

Source
parser/jessiecode.js, line 962

getAST(code, geonextopt, dontstoreopt) → {Node}

Get abstract syntax tree (AST) from JessieCode code. This consists of generating an AST with parser.parse.

Parameters

Name Type Attributes Default Description
code String
geonext Boolean <optional>
false

Geonext compatibility mode.

dontstore Boolean <optional>
false

If false, the code string is stored in this.code.

Returns

AST

Type
Node

Details

Source
parser/jessiecode.js, line 975

getElementById(id) → {JXG.GeometryElement}

Looks up an JXG.GeometryElement by its id.

Parameters

Name Type Description
id String

Returns

Type
JXG.GeometryElement

Details

Source
parser/jessiecode.js, line 295

getLHS(node) → {Object}

Resolves the lefthand side of an assignment operation

Parameters

Name Type Description
node

Returns

An object with two properties. o which contains the object, and a string what which contains the property name.

Type
Object

Details

Source
parser/jessiecode.js, line 1313

getName(obj, useId) → {String}

This is used as the global getName() function.

Parameters

Name Type Description
obj JXG.GeometryElement
useId Boolean

Returns

Type
String

Details

Source
parser/jessiecode.js, line 2225

getPossibleOperands() → {Object}

Returns information about the possible functions and constants.

Returns

Type
Object

Details

Source
parser/jessiecode.js, line 2865

getvar(vname, localopt, isFunctionNameopt)

Looks up the value of the given variable. We use a simple type inspection.

Parameters

Name Type Attributes Default Description
vname String

Name of the variable

local Boolean <optional>
false

Only look up the internal symbol table and don't look for the vname in Math or the element list.

isFunctionName Boolean <optional>
false

Lookup function of type builtIn, Math.*, creator.

Details

See
Source
parser/jessiecode.js, line 445

getvarJS(vname, localopt, withPropsopt)

TODO this needs to be called from JS and should not generate JS code Looks up a variable identifier in various tables and generates JavaScript code that could be eval'd to get the value.

Parameters

Name Type Attributes Default Description
vname String

Identifier

local Boolean <optional>
false

Don't resolve ids and names of elements

withProps Boolean <optional>
false

Details

Source
parser/jessiecode.js, line 509

ifthen(cond, v1, v2) → {*}

Implementation of the ?: operator

Parameters

Name Type Description
cond Boolean

Condition

v1 *
v2 *

Returns

Either v1 or v2.

Type
*

Details

Source
parser/jessiecode.js, line 2591

importModule(module)

Import modules into a JessieCode script.

Parameters

Name Type Description
module String

Details

Source
parser/jessiecode.js, line 2683

isBuiltIn(vname) → {Boolean}

Returns true if the given identifier is a builtIn variable/function.

Parameters

Name Type Description
vname String

Returns

Type
Boolean

Details

Source
parser/jessiecode.js, line 431

isCreator(vname) → {Boolean}

Checks if the given variable name is a valid creator method.

Parameters

Name Type Description
vname String

Returns

Type
Boolean

Details

Source
parser/jessiecode.js, line 412

isLocalVariable(vname) → {Object}

Checks if the given variable name can be found in the current scope chain.

Parameters

Name Type Description
vname String

Returns

A reference to the scope object the variable can be found in or null if it can't be found.

Type
Object

Details

Source
parser/jessiecode.js, line 374

isMathMethod(vname) → {Boolean}

Checks if the given variable identifier is a valid member of the JavaScript Math Object.

Parameters

Name Type Description
vname String

Returns

Type
Boolean

Details

Source
parser/jessiecode.js, line 422

isParameter(vname) → {Object}

Checks if the given variable name is a parameter in any scope from the current to the global scope.

Parameters

Name Type Description
vname String

Returns

A reference to the scope object that contains the variable in its arg list.

Type
Object

Details

Source
parser/jessiecode.js, line 393

letvar(vname, value)

Assigns a value to a variable in the current scope.

Parameters

Name Type Description
vname String

Variable name

value

Anything

Details

See
Source
parser/jessiecode.js, line 361

makeMap(f) → {function}

Adds the property isMap to a function and sets it to true.

Parameters

Name Type Description
f function

Returns

Type
function

Details

Source
parser/jessiecode.js, line 612

manipulate(code, geonextopt, dontstoreopt) → {String}

Manipulate JessieCode. This consists of generating an AST with parser.parse, apply simplifying rules from CA and compile the AST back to JessieCode.

Parameters

Name Type Attributes Default Description
code String

JessieCode code to be parsed

geonext Boolean <optional>
false

Geonext compatibility mode.

dontstore Boolean <optional>
false

If false, the code string is stored in this.code.

Returns

Simplified JessieCode code

Type
String

Details

Source
parser/jessiecode.js, line 936

mergeAttributes(o) → {Object}

Merge all attribute values given with an element creator into one object.

Parameters

Name Type Description
o Object

An arbitrary number of objects

Returns

All given objects merged into one. If properties appear in more (case sensitive) than one object the last value is taken.

Type
Object

Details

Source
parser/jessiecode.js, line 734

mod(a, b) → {Number|Array}

Implementation of the % operator.

Parameters

Name Type Description
a Number | Array
b Number

Returns

Type
Number | Array

Details

Source
parser/jessiecode.js, line 2508

mul(a, b) → {Number|Array}

Multiplication of vectors and numbers

Parameters

Name Type Description
a Number | Array
b Number | Array

Returns

(Inner) product of the given input values.

Type
Number | Array

Details

Source
parser/jessiecode.js, line 2438

neg(a) → {Number|Array}

unary - operator implementation

Parameters

Name Type Description
a Number | Array | Point

Returns

Type
Number | Array

Details

Source
parser/jessiecode.js, line 2409

node(type, value, children)

Create a new parse tree node.

Parameters

Name Type Description
type String

Type of node, e.g. node_op, node_var, or node_const

value

The nodes value, e.g. a variables value or a functions body.

children Array

Arbitrary number of child nodes.

Details

Source
parser/jessiecode.js, line 223

parse(code, geonextopt, dontstoreopt) → {Object}

Parses JessieCode. This consists of generating an AST with parser.parse, apply simplifying rules from CA and executing the ast by calling this.execute(ast).

Parameters

Name Type Attributes Default Description
code String

JessieCode code to be parsed

geonext Boolean <optional>
false

Geonext compatibility mode.

dontstore Boolean <optional>
false

If false, the code string is stored in this.code.

Returns

Parse JessieCode code and execute it.

Type
Object

Details

Source
parser/jessiecode.js, line 921

perimeter(obj) → {Number}

This is used as the global perimeter() function.

Parameters

Name Type Description
obj Circle | Polygon

Returns

Type
Number

Details

Source
parser/jessiecode.js, line 2294

popScope() → {Object}

Remove the current scope and reinstate the previous scope

Returns

Type
Object

Details

Source
parser/jessiecode.js, line 281

pow(a, b) → {Number}

Pow function wrapper to allow direct usage of sliders.

Parameters

Name Type Description
a Number | Slider
b Number | Slider

Returns

Type
Number

Details

Source
parser/jessiecode.js, line 2538

pushScope(args) → {Object}

Create a new scope.

Parameters

Name Type Description
args Array

Returns

Type
Object

Details

Source
parser/jessiecode.js, line 262

radius(obj) → {Number}

This is used as the global radius() function.

Parameters

Name Type Description
obj Circle | Sector

Returns

Type
Number

Details

Source
parser/jessiecode.js, line 2321

replaceIDs(node)

Traverses through the given subtree and changes all values of nodes with the replaced flag set by JXG.JessieCode#replaceNames to the name of the element (if not empty).

Parameters

Name Type Description
node Object

Details

Source
parser/jessiecode.js, line 1007

replaceNames(node, callValueParopt)

Traverses through the given subtree and changes all elements referenced by names through referencing them by ID. An identifier is only replaced if it is not found in all scopes above the current scope and if it has not been blacklisted within the codeblock determined by the given subtree.

Parameters

Name Type Attributes Default Description
node Object
callValuePar Boolean <optional>
false

if true, uses $value() instead of $() in createReplacementNode

Details

Source
parser/jessiecode.js, line 1051

resolve(vname) → {*}

Look up the value of a local variable.

Parameters

Name Type Description
vname string

Returns

Type
*

Details

Source
parser/jessiecode.js, line 490

(private) resolveType(vname, node)

Type inspection: check if the string vname appears as function name in the AST node. Used in "op_execfun". This allows the JessieCode examples below.

Examples

var p = board.create('point', [2, 0], {name: 'X'});
 var txt = 'X(X)';
 console.log(board.jc.parse(txt));
var p = board.create('point', [2, 0], {name: 'X'});
 var txt = 'f = function(el, X) { return X(el); }; f(X, X);';
 console.log(board.jc.parse(txt));
var p = board.create('point', [2, 0], {name: 'point'});
 var txt = 'B = point(1,3); X(point);';
 console.log(board.jc.parse(txt));
var p = board.create('point', [2, 0], {name: 'A'});
 var q = board.create('point', [-2, 0], {name: 'X'});
 var txt = 'getCoord=function(p, f){ return f(p); }; getCoord(A, X);';
 console.log(board.jc.parse(txt));

Parameters

Name Type Description
vname String
node Object

Returns

'any' or 'function'

Details

See
Source
parser/jessiecode.js, line 1265

setProp(o, what, value)

Sets the property what of o to value

Parameters

Name Type Description
o Point | Text
what String
value

Details

Source
parser/jessiecode.js, line 750

simplify(code) → {String}

Manipulate JessieCode. This consists of generating an AST with parser.parse, apply simplifying rules from CAS and compile the AST back to JessieCode with minimal number of parentheses.

Parameters

Name Type Description
code String

JessieCode code to be parsed

Returns

Simplified JessieCode code

Type
String

Details

Source
parser/jessiecode.js, line 949

slope(obj) → {Number}

This is used as the global slope() function.

Parameters

Name Type Description
obj Line

Returns

Type
Number

Details

Source
parser/jessiecode.js, line 2334

snippet(code, funwrapopt, varnameopt, geonextopt, forceValueCallopt)

Parses a JessieCode snippet, e.g. "3+4", and wraps it into a function, if desired.

Parameters

Name Type Attributes Default Description
code String

A small snippet of JessieCode. Must not be an assignment.

funwrap Boolean <optional>
true

If true, the code is wrapped in a function.

varname String <optional>
''

Name of the parameter(s)

geonext Boolean <optional>
false

Geonext compatibility mode.

forceValueCall Boolean <optional>
true

Force evaluation of value method of sliders.

Details

Source
parser/jessiecode.js, line 987

sub(a, b) → {Number|Array}

  • operator implementation

Parameters

Name Type Description
a Number | Array | Point
b Number | Array | Point

Returns

Type
Number | Array

Details

Source
parser/jessiecode.js, line 2380

use(board)

Implementation of the use() builtin function

Parameters

Name Type Description
board String

Details

Source
parser/jessiecode.js, line 2621

Inherited

none

Parameters

Name Type Attributes Default Description
code String <optional>

Code to parse.

geonext Boolean <optional>
false

Geonext compatibility mode.

Details

See
Source
parser/jessiecode.js, line 86