├── .gitignore ├── LICENSE.LGPL ├── LICENSE.MIT ├── Makefile ├── README.md ├── examples ├── assign.html ├── compiler.html ├── conditional.html ├── conditions.html ├── creators.html ├── dollar.html ├── emptystring.html ├── euler.html ├── exprslider.html ├── filters.html ├── functions.html ├── gaetano_g.html ├── genTerm.html ├── import.html ├── jcbench.html ├── jessiecode.html ├── jison.html ├── log.html ├── map.html ├── merge_props.html ├── numbers.html ├── scopechain.html ├── test.html └── utf8.html ├── package.json ├── src ├── ca.js ├── ca_amd.js ├── grammar.jison ├── interpreter.js └── interpreter_amd.js └── test ├── jsTestDriver.conf ├── testArithmetic.js ├── testBuiltin.js ├── testControl.js └── testGeneric.js /.gitignore: -------------------------------------------------------------------------------- 1 | bin 2 | node_modules 3 | .idea/ 4 | -------------------------------------------------------------------------------- /LICENSE.LGPL: -------------------------------------------------------------------------------- 1 | GNU LESSER GENERAL PUBLIC LICENSE 2 | Version 3, 29 June 2007 3 | 4 | Copyright (C) 2007 Free Software Foundation, Inc. 5 | Everyone is permitted to copy and distribute verbatim copies 6 | of this license document, but changing it is not allowed. 7 | 8 | 9 | This version of the GNU Lesser General Public License incorporates 10 | the terms and conditions of version 3 of the GNU General Public 11 | License, supplemented by the additional permissions listed below. 12 | 13 | 0. Additional Definitions. 14 | 15 | As used herein, "this License" refers to version 3 of the GNU Lesser 16 | General Public License, and the "GNU GPL" refers to version 3 of the GNU 17 | General Public License. 18 | 19 | "The Library" refers to a covered work governed by this License, 20 | other than an Application or a Combined Work as defined below. 21 | 22 | An "Application" is any work that makes use of an interface provided 23 | by the Library, but which is not otherwise based on the Library. 24 | Defining a subclass of a class defined by the Library is deemed a mode 25 | of using an interface provided by the Library. 26 | 27 | A "Combined Work" is a work produced by combining or linking an 28 | Application with the Library. The particular version of the Library 29 | with which the Combined Work was made is also called the "Linked 30 | Version". 31 | 32 | The "Minimal Corresponding Source" for a Combined Work means the 33 | Corresponding Source for the Combined Work, excluding any source code 34 | for portions of the Combined Work that, considered in isolation, are 35 | based on the Application, and not on the Linked Version. 36 | 37 | The "Corresponding Application Code" for a Combined Work means the 38 | object code and/or source code for the Application, including any data 39 | and utility programs needed for reproducing the Combined Work from the 40 | Application, but excluding the System Libraries of the Combined Work. 41 | 42 | 1. Exception to Section 3 of the GNU GPL. 43 | 44 | You may convey a covered work under sections 3 and 4 of this License 45 | without being bound by section 3 of the GNU GPL. 46 | 47 | 2. Conveying Modified Versions. 48 | 49 | If you modify a copy of the Library, and, in your modifications, a 50 | facility refers to a function or data to be supplied by an Application 51 | that uses the facility (other than as an argument passed when the 52 | facility is invoked), then you may convey a copy of the modified 53 | version: 54 | 55 | a) under this License, provided that you make a good faith effort to 56 | ensure that, in the event an Application does not supply the 57 | function or data, the facility still operates, and performs 58 | whatever part of its purpose remains meaningful, or 59 | 60 | b) under the GNU GPL, with none of the additional permissions of 61 | this License applicable to that copy. 62 | 63 | 3. Object Code Incorporating Material from Library Header Files. 64 | 65 | The object code form of an Application may incorporate material from 66 | a header file that is part of the Library. You may convey such object 67 | code under terms of your choice, provided that, if the incorporated 68 | material is not limited to numerical parameters, data structure 69 | layouts and accessors, or small macros, inline functions and templates 70 | (ten or fewer lines in length), you do both of the following: 71 | 72 | a) Give prominent notice with each copy of the object code that the 73 | Library is used in it and that the Library and its use are 74 | covered by this License. 75 | 76 | b) Accompany the object code with a copy of the GNU GPL and this license 77 | document. 78 | 79 | 4. Combined Works. 80 | 81 | You may convey a Combined Work under terms of your choice that, 82 | taken together, effectively do not restrict modification of the 83 | portions of the Library contained in the Combined Work and reverse 84 | engineering for debugging such modifications, if you also do each of 85 | the following: 86 | 87 | a) Give prominent notice with each copy of the Combined Work that 88 | the Library is used in it and that the Library and its use are 89 | covered by this License. 90 | 91 | b) Accompany the Combined Work with a copy of the GNU GPL and this license 92 | document. 93 | 94 | c) For a Combined Work that displays copyright notices during 95 | execution, include the copyright notice for the Library among 96 | these notices, as well as a reference directing the user to the 97 | copies of the GNU GPL and this license document. 98 | 99 | d) Do one of the following: 100 | 101 | 0) Convey the Minimal Corresponding Source under the terms of this 102 | License, and the Corresponding Application Code in a form 103 | suitable for, and under terms that permit, the user to 104 | recombine or relink the Application with a modified version of 105 | the Linked Version to produce a modified Combined Work, in the 106 | manner specified by section 6 of the GNU GPL for conveying 107 | Corresponding Source. 108 | 109 | 1) Use a suitable shared library mechanism for linking with the 110 | Library. A suitable mechanism is one that (a) uses at run time 111 | a copy of the Library already present on the user's computer 112 | system, and (b) will operate properly with a modified version 113 | of the Library that is interface-compatible with the Linked 114 | Version. 115 | 116 | e) Provide Installation Information, but only if you would otherwise 117 | be required to provide such information under section 6 of the 118 | GNU GPL, and only to the extent that such information is 119 | necessary to install and execute a modified version of the 120 | Combined Work produced by recombining or relinking the 121 | Application with a modified version of the Linked Version. (If 122 | you use option 4d0, the Installation Information must accompany 123 | the Minimal Corresponding Source and Corresponding Application 124 | Code. If you use option 4d1, you must provide the Installation 125 | Information in the manner specified by section 6 of the GNU GPL 126 | for conveying Corresponding Source.) 127 | 128 | 5. Combined Libraries. 129 | 130 | You may place library facilities that are a work based on the 131 | Library side by side in a single library together with other library 132 | facilities that are not Applications and are not covered by this 133 | License, and convey such a combined library under terms of your 134 | choice, if you do both of the following: 135 | 136 | a) Accompany the combined library with a copy of the same work based 137 | on the Library, uncombined with any other library facilities, 138 | conveyed under the terms of this License. 139 | 140 | b) Give prominent notice with the combined library that part of it 141 | is a work based on the Library, and explaining where to find the 142 | accompanying uncombined form of the same work. 143 | 144 | 6. Revised Versions of the GNU Lesser General Public License. 145 | 146 | The Free Software Foundation may publish revised and/or new versions 147 | of the GNU Lesser General Public License from time to time. Such new 148 | versions will be similar in spirit to the present version, but may 149 | differ in detail to address new problems or concerns. 150 | 151 | Each version is given a distinguishing version number. If the 152 | Library as you received it specifies that a certain numbered version 153 | of the GNU Lesser General Public License "or any later version" 154 | applies to it, you have the option of following the terms and 155 | conditions either of that published version or of any later version 156 | published by the Free Software Foundation. If the Library as you 157 | received it does not specify a version number of the GNU Lesser 158 | General Public License, you may choose any version of the GNU Lesser 159 | General Public License ever published by the Free Software Foundation. 160 | 161 | If the Library as you received it specifies that a proxy can decide 162 | whether future versions of the GNU Lesser General Public License shall 163 | apply, that proxy's public statement of acceptance of any version is 164 | permanent authorization for you to choose that version for the 165 | Library. -------------------------------------------------------------------------------- /LICENSE.MIT: -------------------------------------------------------------------------------- 1 | Copyright (C) 2008-2019 2 | 3 | Matthias Ehmann 4 | Michael Gerhaeuser 5 | Carsten Miller 6 | Bianca Valentin 7 | Alfred Wassermann 8 | Peter Wilfahrt 9 | 10 | Permission is hereby granted, free of charge, to any person obtaining a copy 11 | of this software and associated documentation files (the "Software"), to deal 12 | in the Software without restriction, including without limitation the rights 13 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 14 | copies of the Software, and to permit persons to whom the Software is 15 | furnished to do so, subject to the following conditions: 16 | 17 | The above copyright notice and this permission notice shall be included in 18 | all copies or substantial portions of the Software. 19 | 20 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 21 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 22 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 23 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 24 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 25 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 26 | SOFTWARE. 27 | -------------------------------------------------------------------------------- /Makefile: -------------------------------------------------------------------------------- 1 | .PHONY: core deploy test test-server clean 2 | 3 | # directories 4 | OUTPUT=bin 5 | TMP=tmp 6 | BIN=./node_modules/.bin 7 | JSXGRAPH=../JSXGraph 8 | 9 | # build tools 10 | # REQUIREJS=$(BIN)/r.js 11 | # UGLIFYJS=$(BIN)/uglifyjs 12 | MINIFYER=./node_modules/terser/bin/terser 13 | LINT=$(BIN)/jslint 14 | ESLINT=./node_modules/eslint/bin/eslint.js 15 | HINT=$(BIN)/jshint 16 | JSTESTDRIVER=java -jar ./node_modules/jstestdriver/lib/jstestdriver.jar 17 | JISON=$(BIN)/jison 18 | 19 | # general tools 20 | CP=cp 21 | CAT=cat 22 | MKDIR=mkdir 23 | RM=rm 24 | CD=cd 25 | ZIP=zip 26 | SED=sed 27 | 28 | # flags 29 | MKDIRFLAGS=-p 30 | RMFLAGS=-rf 31 | JSDOC2FLAGS=-v -p -t=$(JSDOC2TPL) -d=$(TMP)/docs 32 | ZIPFLAGS=-r 33 | JSTESTPORT=4224 34 | JSTESTSERVER=localhost:4224 35 | JSTESTFLAGS=--reset --captureConsole --tests all 36 | 37 | # filelists - required for docs, linters, and to build the readers 38 | GRAMMAR=src/grammar.jison 39 | INTERPRETER=src/interpreter.js 40 | 41 | # rules 42 | all: core deploy 43 | 44 | deploy: core 45 | $(CP) $(OUTPUT)/jessiecode.js ../jsxgraph/src/parser/jessiecode.js 46 | 47 | core: parser.js $(INTERPRETER) 48 | $(SED) -e '/#include "parser\.js"/{r '"$(OUTPUT)"'/parser.js' -e 'd}' $(INTERPRETER) > $(OUTPUT)/jessiecode.js 49 | $(SED) -i -e 's/"use strict"//' $(OUTPUT)/jessiecode.js 50 | # Remove trailing whitespace: 51 | $(SED) -i -e 's/[[:space:]]$$//' $(OUTPUT)/jessiecode.js 52 | $(MINIFYER) $(OUTPUT)/jessiecode.js -c -m -o $(OUTPUT)/jessiecode.min.js 53 | 54 | parser.js: $(GRAMMAR) 55 | $(MKDIR) $(MKDIRFLAGS) $(OUTPUT) 56 | $(JISON) $^ -o $(OUTPUT)/parser.js -m js 57 | $(SED) -i 's/var parser =/\/**\n * @class\n * @ignore\n *\/\nvar parser =/g' $(OUTPUT)/parser.js 58 | $(SED) -i 's/^performAction:/\/**\n * @class\n * @ignore\n *\/\nperformAction:/g' $(OUTPUT)/parser.js 59 | $(SED) -i 's/^parse:/\/**\n * @class\n * @ignore\n *\/\nparse:/g' $(OUTPUT)/parser.js 60 | $(SED) -i 's/^function Parser/\/**\n * @class\n * @ignore\n *\/\nfunction Parser/g' $(OUTPUT)/parser.js 61 | 62 | test-server: 63 | $(JSTESTDRIVER) --port $(JSTESTPORT) 64 | 65 | test: jsxgraph 66 | $(JSTESTDRIVER) $(JSTESTSERVER) $(JSTESTFLAGS) --basePath ./ --config test/jsTestDriver.conf 67 | 68 | jsxgraph: deploy 69 | $(CD) ../JSXGraph; make OUTPUT=../JessieCode/bin core-min; git checkout -- src/parser/jessiecode.js 70 | 71 | clean: 72 | rm -f bin/* 73 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | JessieCode - A script language for JSXGraph 2 | =========================================== 3 | 4 | (c) 2011-2021 5 | Michael Gerhäuser, michael.gerhaeuser@gmail.com 6 | Alfred Wassermann, alfred.wassermann@uni-bayreuth.de 7 | 8 | 9 | About 10 | ----- 11 | 12 | JessieCode is a scripting language designed to provide a interface to JSXGraph. It is 13 | similar to JavaScript, but prevents access to the DOM. Hence, it can be used in community 14 | driven web sites which want to use JSXGraph to display interactive math graphics. 15 | 16 | JSXGraph (https://jsxgraph.org) is required to use JessieCode. 17 | 18 | 19 | Compile 20 | ------- 21 | 22 | Required npm tools: 23 | 24 | - Jison 25 | - uglify-js 26 | - jshint, jslint (optional) 27 | 28 | Use `make` to compile the .bnf file into a javascript parser and to concatenate 29 | the parser and interpreter into a single .js file, see `Makefile`. 30 | 31 | License 32 | ------- 33 | 34 | JessieCode is free software dual licensed under the GNU LGPL or MIT License. 35 | 36 | You can redistribute it and/or modify it under the terms of the 37 | 38 | * GNU Lesser General Public License as published by 39 | the Free Software Foundation, either version 3 of the License, or 40 | (at your option) any later version 41 | OR 42 | * MIT License: https://github.com/jsxgraph/jsxgraph/blob/master/LICENSE.MIT 43 | 44 | JessieCode is distributed in the hope that it will be useful, 45 | but WITHOUT ANY WARRANTY; without even the implied warranty of 46 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 47 | GNU Lesser General Public License for more details. 48 | 49 | You should have received a copy of the GNU Lesser General Public License and 50 | the MIT License along with JessieCode. If not, see 51 | and . 52 | 53 | # JessieCode language reference 54 | 55 | ## Datatypes 56 | 57 | - **Boolean**, *true* or *false* (case insensitive, *tRuE* is a valid boolean 58 | constant). 59 | 60 | - **Strings** are defined using single quote marks. Quote marks inside a string 61 | have to be escaped with a backslash. 62 | 63 | - **Number**, corresponds to the JavaScript *number* datatype. 64 | 65 | - **Objects**, can be created only via object literal notation **\<\< \>\>** and 66 | the predefined element functions (see below). To access properties and 67 | methods the operator is used. Example: 68 | 69 | ```js 70 | obj = << 71 | property: 'string', 72 | prop: 42, 73 | method: function (x) { 74 | return x*x; 75 | } 76 | >>; 77 | sixteen = obj.method(4); 78 | ``` 79 | 80 | - **Functions** are declared with the *function* operator 81 | 82 | ```js 83 | f = function (a, b, c) { 84 | return a+b+c; 85 | }; 86 | ``` 87 | 88 | ## Comments 89 | 90 | Only one line comments with // being the first non-whitespace characters are 91 | supported right now. 92 | 93 | ## Operators 94 | 95 | ### Logical operators 96 | 97 | | Operator | Description | 98 | |:---------|:------------| 99 | | \|\| | OR | 100 | | && | AND | 101 | | ! | NOT | 102 | 103 | ### Arithmetic operators 104 | 105 | | Operator | Description | 106 | |:---------|:------------------------------| 107 | | \+ | Addition | 108 | | \- | Subtraction or unary negation | 109 | | \* | Multiplication | 110 | | / | Division | 111 | | % | Modulus | 112 | | ^ | Exponentiation | 113 | 114 | ### Assignment operators 115 | 116 | | Operator | Description | 117 | |:---------|:------------| 118 | | = | Assignment | 119 | 120 | ### Comparison operators 121 | 122 | | Operator | Description | 123 | |:---------|:--------------------------------------------------------------| 124 | | == | Equals | 125 | | <= | Lesser or equal | 126 | | \>= | Greater or equal | 127 | | \< | Lesser | 128 | | \> | Greater | 129 | | != | Not equal | 130 | | \~= | Approximately equal, can be used to compare two float values. | 131 | 132 | ### Conditional operator 133 | 134 | | Operator | Description | 135 | |:---------|:------------| 136 | | bool ? expr1 : expr2 | expr1 if bool is true, expr2 otherwise | 137 | 138 | ### String operators 139 | 140 | | Operator | Description | 141 | |:---------|:---------------------| 142 | | \+ | String concatenation | 143 | 144 | ### Member operators 145 | 146 | | Operator | Description | 147 | |:---------|:--------------------------------------------| 148 | | . | Access the object\'s properties and methods | 149 | 150 | ## Control structures 151 | 152 | The control structures are exactly the same as in JavaScript. 153 | 154 | ### If 155 | 156 | ```js 157 | if ( 159 | } else if () { 160 | 161 | } else { 162 | 163 | } 164 | ``` 165 | 166 | ### While loop 167 | 168 | ```js 169 | while () { 170 | 171 | } 172 | ``` 173 | 174 | ### Do loop 175 | 176 | ```js 177 | do { 178 | 179 | } while (); 180 | ``` 181 | 182 | ### For loop 183 | 184 | ```js 185 | for (; ; ) { 186 | 187 | } 188 | ``` 189 | 190 | ## Predefined constants 191 | 192 | | Name | Description | 193 | |:---------|:-------------------------------------------------------| 194 | | \$board | Reference to the currently accessed board. | 195 | | LN2 | Natural logarithm of 2 | 196 | | LN10 | Natural logarithm of 10 | 197 | | LOG2E | Base 2 logarithm of EULER | 198 | | LOG10E | Base 10 logarithm of EULER | 199 | | PI | Ratio of the circumference of a circle to its diameter | 200 | | EULER | Euler\'s number e = 2.718281828459045 | 201 | | SQRT1\_2 | Square root of 1/2 | 202 | | SQRT2 | Square root of 2 | 203 | 204 | ## Predefined functions 205 | 206 | ### Math functions 207 | 208 | Supported are all functions from the JavaScript `Math` object, like `sin`, `cos`, `abs, `random`, ... 209 | Additionally supported are numerical JSXGraph functions from `JXG.Math`, see . 210 | 211 | | Function | Description | 212 | |:-------------------|:-----------------------------------------------------------------| 213 | | cos(x) | Cosine of x | 214 | | cosh(x) | Hyperbolic cosine of x | 215 | | pow(b, e) | e to the b | 216 | | log(x), ln(x) | Natural logarithm | 217 | | log(x, b) | Logarithm to base b | 218 | | log2(x), lb(x) | Logarithm to base 2 | 219 | | log10(x), ld(x) | Logarithm to base 10 | 220 | | tan(x) | Tangent of x | 221 | | cot(x) | Cotangent of x | 222 | | sqrt(x) | Square root of x | 223 | | cbrt(x) | Cube root of x | 224 | | nthroot(x) | n-th root of x | 225 | | ceil(x) | Get smallest integer n with n \> x. | 226 | | asin(x) | arcsine | 227 | | abs(x) | Absolute value of x | 228 | | max(a, b, c, \...) | Maximum value of all given values. | 229 | | min(a, b, c, \...) | Minimum value of all given values. | 230 | | exp(x) | EULER to the x | 231 | | atan2(y, x) | Returns the arctangent of the quotient of its arguments. | 232 | | random(max = 1) | Generate a random number between 0 and max. | 233 | | round(v) | Returns the value of a number rounded to the nearest integer. | 234 | | floor(x) | Returns the biggest integer n with n \< x. | 235 | | acos(x) | arccosine of x | 236 | | atan(x) | arctangent of x | 237 | | acot(x) | arccotangent of x | 238 | | sin(x) | sine of x | 239 | | sinh(x) | Hyperbolic sine of x | 240 | | factorial(n) | Calculates n! | 241 | | trunc(v, p = 0) | Truncate v after the p-th decimal. | 242 | | V(s) | Returns the value of the given element, e.g. sliders and angles. | 243 | | L(s) | Calculates the length of the given segment. | 244 | | X(P) Y(P) | Returns the x resp. y coordinate of the given point. | 245 | | dist(P, Q) | Compute the distance of two points. | 246 | | deg(A, B, C) | Calculate the angle of three points in degree. | 247 | | rad(A, B, C) | Calculate the angle of three points in rad. | 248 | | \$(id) | Look up the element to the given element id. | 249 | 250 | ### \$board methods 251 | 252 | | Method | Description | 253 | |:---------------------------------------------|:----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------| 254 | | update() | Update all dependencies and redraw the board. | 255 | | on(event, handler, context=board) | Register an event handler for the given event. | 256 | | off(event, handler=) | Deregister a given event handler or deregister all event handlers. | 257 | | setView(array, keepaspectratio=false) | Changes the viewport. An array with 4 numbers is expected, the four numbers represent the left, upper, right and lower bound of the viewport. If keepaspectratio is true, the viewport is adjusted to the same aspect ratio as the board container. | 258 | | setBoundingbox(array, keepaspectratio=false) | See setView. | 259 | | migratePoint(P, Q) | Exchange point P by point Q. | 260 | | colorblind(type) | Emulate color blindness. Possible types are*protanopia, tritanopia,* and*deuteranopia.* | 261 | 262 | ### Element functions 263 | 264 | Every element known to the loaded JSXGraph version is available inside Jessie 265 | by its element type, e.g. points can be created by calling point() 266 | 267 | ```js 268 | A = point(1, 2); 269 | ``` 270 | 271 | The given parameters correspond to the parents array of the JXG.Board.create() 272 | method. Attributes are given after the function call itself in an object: 273 | 274 | ```js 275 | A = point(1, 2) << strokeColor: 'red', face: '[]', size: 7, fillColor: 'black' >>; 276 | ``` 277 | 278 | For a possibly incomplete list including documentation, see [the JSXGraph 279 | docs](//jsxgraph.org/docs/); For a complete list see the 280 | **Element reference** section below. 281 | 282 | ## Accessing elements 283 | 284 | ### Variable assignment 285 | 286 | ```js 287 | A = point(1, 2); 288 | A.strokeColor = '#123456'; 289 | ``` 290 | 291 | ### \$ 292 | 293 | ```js 294 | point(1, 2) << id: 'foo', name: 'bar' >>; 295 | $('foo').strokeColor = '#654321'; 296 | ``` 297 | 298 | ### Id 299 | 300 | ```js 301 | point(1, 2) << id: 'foo', name: 'bar' >>; 302 | foo.strokeColor = '#f00f00'; 303 | ``` 304 | 305 | This is possible only if **foo** is not used as a variable. This won\'t work: 306 | 307 | ```js 308 | foo = 1; 309 | (function () { 310 | point(1, 2) << id: 'foo' >>; 311 | return foo.X(); 312 | })(); 313 | ``` 314 | 315 | ### Name 316 | 317 | ```js 318 | point(1, 2) << id: 'foo', name: 'bar' >>; 319 | bar.strokeColor = '#541541'; 320 | ``` 321 | 322 | This is possible only if there is not a variable called **bar** in the current 323 | or any higher scope. See **Id** above for an example. 324 | 325 | ## Element reference 326 | 327 | See 328 | 329 | ### Attributes 330 | 331 | Attributes are set like object properties 332 | 333 | ```js 334 | A.size = 10; 335 | A.face = '[]'; 336 | ``` 337 | 338 | See [the JSXGraph docs](//jsxgraph.uni-bayreuth.de/docs/) for available 339 | attributes. Texts and Points have two special attributes **X** and **Y** to set 340 | their coordinates. 341 | 342 | ### Subelements 343 | 344 | Subelements like labels for points or the baseline in sliders or the dot 345 | indicating an angle element is a right angle can be accessed like properties 346 | 347 | ```js 348 | A.label.strokecolor = 'red'; 349 | ``` 350 | 351 | The names used to access subelements correspond to their names used to set 352 | their attributes in`board.create`. 353 | 354 | ### Methods 355 | 356 | Not all methods of an element class are accessible in JessieCode. Currently these 357 | methods are available: 358 | 359 | - all elements 360 | - setLabelText 361 | - point 362 | - move 363 | - glide 364 | - free 365 | - X 366 | - Y 367 | - glider 368 | - all from point 369 | - setPosition 370 | - text 371 | - setText 372 | - free 373 | - move 374 | - slider 375 | - Value 376 | - angle 377 | - Value 378 | 379 | -------------------------------------------------------------------------------- /examples/assign.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | JessieCode example :: creator attr values 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 |

Euler line

14 | 15 |
16 | 17 |
18 | 19 |

20 | 21 |
22 | 23 | 27 | 28 | 57 | 58 | 59 | 60 | -------------------------------------------------------------------------------- /examples/compiler.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | JessieCode example :: creator attr values 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 |

Euler line

13 | 14 |
15 | 16 |
17 | 18 |

19 | 20 |
21 | 22 | 45 | 46 | 75 | 76 | 77 | 78 | -------------------------------------------------------------------------------- /examples/conditional.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | JessieCode example :: creator attr values 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 |

Euler line

13 | 14 |
15 | 16 |
17 | 18 |

19 | 20 |
21 | 22 | 39 | 40 | 69 | 70 | 71 | 72 | -------------------------------------------------------------------------------- /examples/conditions.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | JessieCode example :: creator attr values 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 |

Euler line

13 | 14 |
15 | 16 |
17 | 18 |

19 | 20 |
21 | 22 | 31 | 32 | 61 | 62 | 63 | 64 | -------------------------------------------------------------------------------- /examples/creators.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | JessieCode example :: creator attr values 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 |

Euler line

13 | 14 |
15 | 16 |
17 | 18 |

19 | 20 |
21 | 22 | 52 | 53 | 82 | 83 | 84 | 85 | -------------------------------------------------------------------------------- /examples/dollar.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | JessieCode example :: creator attr values 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 |

Euler line

14 | 15 |
16 | 17 |
18 | 19 |

20 | 21 |
22 | 23 | 31 | 32 | 61 | 62 | 63 | 64 | -------------------------------------------------------------------------------- /examples/emptystring.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | JessieCode example :: creator attr values 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 |

Euler line

13 | 14 |
15 | 16 |
17 | 18 |

19 | 20 |
21 | 22 | 41 | 42 | 70 | 71 | 72 | 73 | -------------------------------------------------------------------------------- /examples/euler.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | JessieCode example :: Euler line 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 |

Euler line

13 | 14 |
15 | 16 |
17 | 18 |

19 | 20 |
21 | 22 | 76 | 77 | 106 | 107 | 108 | 109 | -------------------------------------------------------------------------------- /examples/exprslider.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | JessieCode example :: creator attr values 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 |

Euler line

13 | 14 |
15 | 16 |
17 | 18 |

19 | 20 |
21 | 22 | 29 | 30 | 59 | 60 | 61 | 62 | -------------------------------------------------------------------------------- /examples/filters.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | JessieCode example :: creator attr values 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 |

Euler line

13 | 14 |
15 | 16 |
17 | 18 |

19 | 20 |
21 | 22 | 25 | 26 | 55 | 56 | 57 | 58 | -------------------------------------------------------------------------------- /examples/functions.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | JessieCode example :: creator attr values 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 |

Euler line

14 | 15 |
16 | 17 |
18 | 19 |

20 | 21 |
22 | 23 | 28 | 29 | 58 | 59 | 60 | 61 | -------------------------------------------------------------------------------- /examples/gaetano_g.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | JessieCode example :: creator attr values 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 |

Euler line

13 | 14 |
15 | 16 |
17 | 18 |

19 | 20 |
21 | 22 | 42 | 43 | 72 | 73 | 74 | 75 | -------------------------------------------------------------------------------- /examples/genTerm.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | JessieCode example :: creator attr values 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 |

Euler line

13 | 14 |
15 | 16 |
17 | 18 |

19 | 20 |
21 | 22 | 27 | 28 | 55 | 56 | 57 | 58 | -------------------------------------------------------------------------------- /examples/import.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | JessieCode example :: creator attr values 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 |

Import

13 | 14 |
15 | 16 |
17 | 18 |

19 | 20 |
21 | 22 | 36 | 37 | 66 | 67 | 68 | 69 | -------------------------------------------------------------------------------- /examples/jcbench.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 |
11 |
12 |
13 |
14 | 15 |

Sourcecode:

16 | 17 | 25 | 26 | 97 | 98 | 99 | -------------------------------------------------------------------------------- /examples/jessiecode.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 48 | 49 |
50 |
51 |
52 | 53 | 64 | 65 |

Sourcecode:

66 | 67 | 138 | 139 | 209 | 210 | 232 | 233 | 282 | 283 | 296 | 297 |
298 | 
299 | 305 | 306 | 307 | -------------------------------------------------------------------------------- /examples/jison.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 11 | 12 | 13 | -------------------------------------------------------------------------------- /examples/log.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | JessieCode example :: creator attr values 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 |

Euler line

13 | 14 |
15 | 16 |
17 | 18 |

19 | 20 |
21 | 22 | 32 | 33 | 62 | 63 | 64 | 65 | -------------------------------------------------------------------------------- /examples/map.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | JessieCode example :: creator attr values 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 |

Euler line

13 | 14 |
15 | 16 |
17 | 18 |

19 | 20 |
21 | 22 | 31 | 32 | 61 | 62 | 63 | 64 | -------------------------------------------------------------------------------- /examples/merge_props.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | JessieCode example :: Euler line 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 |

Euler line

13 | 14 |
15 | 16 |
17 | 18 |

19 | 20 |
21 | 22 | 45 | 46 | 76 | 77 | 78 | 79 | -------------------------------------------------------------------------------- /examples/numbers.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | JessieCode example :: creator attr values 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 |

Euler line

13 | 14 |
15 | 16 |
17 | 18 |

19 | 20 |
21 | 22 | 30 | 31 | 60 | 61 | 62 | 63 | -------------------------------------------------------------------------------- /examples/scopechain.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | JessieCode example :: creator attr values 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 |

Scope chain

13 | 14 |
15 | 16 |
17 | 18 |

19 | 20 |
21 | 22 | 50 | 51 | 80 | 81 | 82 | 83 | -------------------------------------------------------------------------------- /examples/test.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | JessieCode example 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 |

Test some code snippets

14 | 15 |
16 | 17 |
18 | 19 |

20 | 21 |
22 | 23 | 26 | 27 | 55 | 56 | 57 | 58 | -------------------------------------------------------------------------------- /examples/utf8.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | JessieCode example :: creator attr values 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 |

Euler line

13 | 14 |
15 | 16 |
17 | 18 |

19 | 20 |
21 | 22 | 25 | 26 | 55 | 56 | 57 | 58 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "JessieCode", 3 | "description": "JessieCode is a small DSL that acts as a middle layer between the user and JSXGraph.", 4 | "homepage": "https://jsxgraph.org", 5 | "version": "1.5.0", 6 | "author": "Michael Gerhaeuser", 7 | "maintainers": [ 8 | { 9 | "name": "Michael Gerhaeuser", 10 | "email": "michael.gerhaeuser@gmail.com" 11 | }, 12 | { 13 | "name": "Alfred Wassermann", 14 | "email": "alfred.wassermann@uni-bayreuth.de" 15 | } 16 | ], 17 | "contributors": [], 18 | "bugs": { 19 | "url": "https://github.com/jsxgraph/JessieCode/issues" 20 | }, 21 | "directories": { 22 | "lib": "src" 23 | }, 24 | "repository": { 25 | "type": "git", 26 | "url": "git@github.com:jsxgraph/JessieCode.git" 27 | }, 28 | "licenses": [ 29 | { 30 | "type": "MIT", 31 | "url": "https://opensource.org/licenses/MIT" 32 | }, 33 | { 34 | "type": "LGPL-3.0", 35 | "url": "https://opensource.org/licenses/LGPL-3.0" 36 | } 37 | ], 38 | "devDependencies": { 39 | "eslint": "^8.4.1", 40 | "jison": ">= 0.4.18", 41 | "jshint": ">=2.13.0", 42 | "jslint": ">= 0.1.x", 43 | "terser": "^5.10.0" 44 | }, 45 | "engines": { 46 | "node": ">= 0.6.0" 47 | }, 48 | "scripts": {}, 49 | "private": true 50 | } -------------------------------------------------------------------------------- /src/ca_amd.js: -------------------------------------------------------------------------------- 1 | /* 2 | JessieCode Computer algebra algorithms 3 | 4 | Copyright 2011-2019 5 | Michael Gerhaeuser, 6 | Alfred Wassermann 7 | 8 | JessieCode is free software dual licensed under the GNU LGPL or MIT License. 9 | 10 | You can redistribute it and/or modify it under the terms of the 11 | 12 | * GNU Lesser General Public License as published by 13 | the Free Software Foundation, either version 3 of the License, or 14 | (at your option) any later version 15 | OR 16 | * MIT License: https://github.com/jsxgraph/jsxgraph/blob/master/LICENSE.MIT 17 | 18 | JessieCode is distributed in the hope that it will be useful, 19 | but WITHOUT ANY WARRANTY; without even the implied warranty of 20 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 21 | GNU Lesser General Public License for more details. 22 | 23 | You should have received a copy of the GNU Lesser General Public License and 24 | the MIT License along with JessieCode. If not, see 25 | and . 26 | */ 27 | 28 | /*global JXG: true, define: true, window: true, console: true, self: true, document: true, parser: true*/ 29 | /*jslint nomen: true, plusplus: true*/ 30 | /*eslint eqeqeq: "off"*/ 31 | 32 | /* depends: 33 | jxg 34 | parser/geonext 35 | base/constants 36 | base/text 37 | math/math 38 | math/geometry 39 | math/statistics 40 | utils/type 41 | utils/uuid 42 | */ 43 | 44 | /** 45 | * @fileoverview Here, the computer algebra algorithms are implemented. 46 | */ 47 | 48 | define([ 49 | 'jxg', 'base/constants', 'base/text', 'math/math', 'math/geometry', 'math/statistics', 'utils/type', 'utils/env' 50 | ], function (JXG, Const, Text, Mat, Geometry, Statistics, Type, Env) { 51 | 52 | "use strict"; 53 | 54 | /** 55 | * A JessieCode object provides an interface to the parser and stores all variables and objects used within a JessieCode script. 56 | * The optional argument code is interpreted after initializing. To evaluate more code after initializing a JessieCode instance 57 | * please use {@link JXG.JessieCode#parse}. For code snippets like single expressions use {@link JXG.JessieCode#snippet}. 58 | * @constructor 59 | * @param {String} [code] Code to parse. 60 | * @param {Boolean} [geonext=false] Geonext compatibility mode. 61 | */ 62 | JXG.CA = function (node, createNode, parser) { 63 | this.node = node; 64 | this.createNode = createNode; 65 | this.parser = parser; 66 | }; 67 | 68 | JXG.extend(JXG.CA.prototype, /** @lends JXG.CA.prototype */ { 69 | findMapNode: function (mapname, node) { 70 | var i, len, ret; 71 | 72 | //console.log("FINDMAP", node); 73 | if (node.value === 'op_assign' && node.children[0].value === mapname) { 74 | return node.children[1]; 75 | } else if (node.children) { 76 | len = node.children.length; 77 | for (i = 0; i < len; ++i) { 78 | ret = this.findMapNode(mapname, node.children[i]); 79 | if (ret !== null) { 80 | return ret; 81 | } 82 | } 83 | } 84 | return null; 85 | }, 86 | 87 | /** 88 | * Declare all subnodes as math nodes, 89 | * i.e recursively set node.isMath = true; 90 | */ 91 | setMath: function (node) { 92 | var i, len; 93 | 94 | if ((node.type == 'node_op' && ( 95 | node.value == 'op_add' || node.value == 'op_sub' || 96 | node.value == 'op_mul' || node.value == 'op_div' || 97 | node.value == 'op_neg' || node.value == 'op_execfun' || 98 | node.value == 'op_exp')) || 99 | node.type == 'node_var' || node.type == 'node_const') { 100 | 101 | node.isMath = true; 102 | } 103 | if (node.children) { 104 | len = node.children.length; 105 | for (i = 0; i < len; ++i) { 106 | this.setMath(node.children[i]); 107 | } 108 | } 109 | }, 110 | 111 | deriveElementary: function (node, varname) { 112 | var fun = node.children[0].value, 113 | arg = node.children[1], 114 | newNode; 115 | 116 | 117 | switch (fun) { 118 | case 'abs': 119 | // x / sqrt(x * x) 120 | newNode = this.createNode('node_op', 'op_div', 121 | arg[0], 122 | this.createNode('node_op', 'op_execfun', 123 | this.createNode('node_var', 'sqrt'), 124 | [this.createNode('node_op', 'op_mul', 125 | Type.deepCopy(arg[0]), 126 | Type.deepCopy(arg[0]) 127 | )] 128 | ) 129 | ); 130 | break; 131 | 132 | case 'sqrt': 133 | newNode = this.createNode('node_op', 'op_div', 134 | this.createNode('node_const', 1.0), 135 | this.createNode('node_op', 'op_mul', 136 | this.createNode('node_const', 2.0), 137 | this.createNode(node.type, node.value, 138 | Type.deepCopy(node.children[0]), 139 | Type.deepCopy(node.children[1]) 140 | ) 141 | ) 142 | ); 143 | break; 144 | 145 | case 'sin': 146 | newNode = this.createNode('node_op', 'op_execfun', 147 | this.createNode('node_var', 'cos'), 148 | Type.deepCopy(arg) 149 | ); 150 | break; 151 | 152 | case 'cos': 153 | newNode = this.createNode('node_op', 'op_neg', 154 | this.createNode('node_op', 'op_execfun', 155 | this.createNode('node_var', 'sin'), 156 | Type.deepCopy(arg) 157 | ) 158 | ); 159 | break; 160 | 161 | case 'tan': 162 | newNode = this.createNode('node_op', 'op_div', 163 | this.createNode('node_const', 1.0), 164 | this.createNode('node_op', 'op_exp', 165 | this.createNode('node_op', 'op_execfun', 166 | this.createNode('node_var', 'cos'), 167 | Type.deepCopy(arg) 168 | ), 169 | this.createNode('node_const', 2) 170 | ) 171 | ); 172 | break; 173 | 174 | case 'cot': 175 | newNode = this.createNode('node_op', 'op_neg', 176 | this.createNode('node_op', 'op_div', 177 | this.createNode('node_const', 1.0), 178 | this.createNode('node_op', 'op_exp', 179 | this.createNode('node_op', 'op_execfun', 180 | this.createNode('node_var', 'sin'), 181 | Type.deepCopy(arg) 182 | ), 183 | this.createNode('node_const', 2) 184 | ) 185 | ) 186 | ); 187 | break; 188 | 189 | case 'exp': 190 | newNode = this.createNode(node.type, node.value, 191 | Type.deepCopy(node.children[0]), 192 | Type.deepCopy(node.children[1]) 193 | ); 194 | break; 195 | 196 | case 'pow': 197 | // (f^g)' = f^g*(f'g/f + g' log(f)) 198 | newNode = this.createNode('node_op', 'op_mul', 199 | this.createNode('node_op', 'op_execfun', 200 | Type.deepCopy(node.children[0]), 201 | Type.deepCopy(node.children[1]) 202 | ), 203 | this.createNode('node_op', 'op_add', 204 | this.createNode('node_op', 'op_mul', 205 | this.derivative(node.children[1][0], varname), 206 | this.createNode('node_op', 'op_div', 207 | Type.deepCopy(node.children[1][1]), 208 | Type.deepCopy(node.children[1][0]) 209 | ) 210 | ), 211 | this.createNode('node_op', 'op_mul', 212 | this.derivative(node.children[1][1], varname), 213 | this.createNode('node_op', 'op_execfun', 214 | this.createNode('node_var', 'log'), 215 | [Type.deepCopy(node.children[1][0])] 216 | ) 217 | ) 218 | ) 219 | ); 220 | break; 221 | 222 | case 'log': 223 | case 'ln': 224 | newNode = this.createNode('node_op', 'op_div', 225 | this.createNode('node_const', 1.0), 226 | // Attention: single variable mode 227 | Type.deepCopy(arg[0]) 228 | ); 229 | break; 230 | 231 | case 'log2': 232 | case 'lb': 233 | case 'ld': 234 | newNode = this.createNode('node_op', 'op_mul', 235 | this.createNode('node_op', 'op_div', 236 | this.createNode('node_const', 1.0), 237 | // Attention: single variable mode 238 | Type.deepCopy(arg[0]) 239 | ), 240 | this.createNode('node_const', 1.4426950408889634) // 1/log(2) 241 | ); 242 | break; 243 | 244 | case 'log10': 245 | case 'lg': 246 | newNode = this.createNode('node_op', 'op_mul', 247 | this.createNode('node_op', 'op_div', 248 | this.createNode('node_const', 1.0), 249 | // Attention: single variable mode 250 | Type.deepCopy(arg[0]) 251 | ), 252 | this.createNode('node_const', 0.43429448190325176) // 1/log(10) 253 | ); 254 | break; 255 | 256 | case 'asin': 257 | newNode = this.createNode('node_op', 'op_div', 258 | this.createNode('node_const', 1.0), 259 | this.createNode('node_op', 'op_execfun', 260 | this.createNode('node_var', 'sqrt'), 261 | [ 262 | this.createNode('node_op', 'op_sub', 263 | this.createNode('node_const', 1.0), 264 | this.createNode('node_op', 'op_mul', 265 | Type.deepCopy(arg[0]), 266 | Type.deepCopy(arg[0]) 267 | ) 268 | ) 269 | ] 270 | ) 271 | ); 272 | break; 273 | 274 | case 'acos': 275 | newNode = this.createNode('node_op', 'op_neg', 276 | this.createNode('node_op', 'op_div', 277 | this.createNode('node_const', 1.0), 278 | this.createNode('node_op', 'op_execfun', 279 | this.createNode('node_var', 'sqrt'), 280 | [ 281 | this.createNode('node_op', 'op_sub', 282 | this.createNode('node_const', 1.0), 283 | this.createNode('node_op', 'op_mul', 284 | Type.deepCopy(arg[0]), 285 | Type.deepCopy(arg[0]) 286 | ) 287 | ) 288 | ] 289 | ) 290 | ) 291 | ); 292 | break; 293 | 294 | //case 'atan2': 295 | 296 | case 'atan': 297 | newNode = this.createNode('node_op', 'op_div', 298 | this.createNode('node_const', 1.0), 299 | this.createNode('node_op', 'op_add', 300 | this.createNode('node_const', 1.0), 301 | this.createNode('node_op', 'op_mul', 302 | Type.deepCopy(arg[0]), 303 | Type.deepCopy(arg[0]) 304 | ) 305 | ) 306 | ); 307 | break; 308 | 309 | case 'acot': 310 | newNode = this.createNode('node_op', 'op_neg', 311 | this.createNode('node_op', 'op_div', 312 | this.createNode('node_const', 1.0), 313 | this.createNode('node_op', 'op_add', 314 | this.createNode('node_const', 1.0), 315 | this.createNode('node_op', 'op_mul', 316 | Type.deepCopy(arg[0]), 317 | Type.deepCopy(arg[0]) 318 | ) 319 | ) 320 | ) 321 | ); 322 | break; 323 | 324 | case 'sinh': 325 | newNode = this.createNode('node_op', 'op_execfun', 326 | this.createNode('node_var', 'cosh'), 327 | [Type.deepCopy(arg[0])] 328 | ); 329 | break; 330 | 331 | case 'cosh': 332 | newNode = this.createNode('node_op', 'op_execfun', 333 | this.createNode('node_var', 'sinh'), 334 | [Type.deepCopy(arg[0])] 335 | ); 336 | break; 337 | 338 | case 'tanh': 339 | newNode = this.createNode('node_op', 'op_sub', 340 | this.createNode('node_const', 1.0), 341 | this.createNode('node_op', 'op_exp', 342 | this.createNode('node_op', 'op_execfun', 343 | this.createNode('node_var', 'tanh'), 344 | [Type.deepCopy(arg[0])] 345 | ), 346 | this.createNode('node_const', 2.0) 347 | ) 348 | ); 349 | break; 350 | 351 | case 'asinh': 352 | newNode = this.createNode('node_op', 'op_div', 353 | this.createNode('node_const', 1.0), 354 | this.createNode('node_op', 'op_execfun', 355 | this.createNode('node_var', 'sqrt'), 356 | [ 357 | this.createNode('node_op', 'op_add', 358 | this.createNode('node_op', 'op_mul', 359 | Type.deepCopy(arg[0]), 360 | Type.deepCopy(arg[0]) 361 | ), 362 | this.createNode('node_const', 1.0) 363 | ) 364 | ] 365 | ) 366 | ); 367 | break; 368 | 369 | case 'acosh': 370 | newNode = this.createNode('node_op', 'op_div', 371 | this.createNode('node_const', 1.0), 372 | this.createNode('node_op', 'op_execfun', 373 | this.createNode('node_var', 'sqrt'), 374 | [ 375 | this.createNode('node_op', 'op_sub', 376 | this.createNode('node_op', 'op_mul', 377 | Type.deepCopy(arg[0]), 378 | Type.deepCopy(arg[0]) 379 | ), 380 | this.createNode('node_const', 1.0) 381 | ) 382 | ] 383 | ) 384 | ); 385 | break; 386 | 387 | case 'atanh': 388 | newNode = this.createNode('node_op', 'op_div', 389 | this.createNode('node_const', 1.0), 390 | this.createNode('node_op', 'op_sub', 391 | this.createNode('node_const', 1.0), 392 | this.createNode('node_op', 'op_mul', 393 | Type.deepCopy(arg[0]), 394 | Type.deepCopy(arg[0]) 395 | ) 396 | ) 397 | ); 398 | break; 399 | 400 | default: 401 | newNode = this.createNode('node_const', 0.0); 402 | console.log('Derivative of "' + fun + '" not yet implemented'); 403 | throw new Error('Error(' + this.line + '): '); 404 | // this._error('Derivative of "' + fun + '" not yet implemented'); 405 | 406 | } 407 | 408 | return newNode; 409 | }, 410 | 411 | derivative: function (node, varname) { 412 | var newNode; 413 | 414 | switch (node.type) { 415 | case 'node_op': 416 | switch (node.value) { 417 | /* 418 | case 'op_map': 419 | if (true) { 420 | newNode = this.createNode('node_op', 'op_map', 421 | Type.deepCopy(node.children[0]), 422 | this.derivative(node.children[1], varname) 423 | ); 424 | } else { 425 | newNode = this.derivative(node.children[1], varname); 426 | } 427 | break; 428 | */ 429 | case 'op_execfun': 430 | // f'(g(x))g'(x) 431 | if (node.children[0].value == 'pow') { 432 | newNode = this.deriveElementary(node, varname); 433 | } else { 434 | if (node.children[1].length === 0) { 435 | newNode = this.createNode('node_const', 0.0); 436 | } else { 437 | newNode = this.createNode('node_op', 'op_mul', 438 | this.deriveElementary(node, varname), 439 | // Warning: single variable mode 440 | this.derivative(node.children[1][0], varname) 441 | ); 442 | } 443 | } 444 | break; 445 | 446 | case 'op_div': 447 | // (f'g − g'f )/(g*g) 448 | newNode = this.createNode('node_op', 'op_div', 449 | this.createNode('node_op', 'op_sub', 450 | this.createNode('node_op', 'op_mul', 451 | this.derivative(node.children[0], varname), 452 | Type.deepCopy(node.children[1]) 453 | ), 454 | this.createNode('node_op', 'op_mul', 455 | Type.deepCopy(node.children[0]), 456 | this.derivative(node.children[1], varname) 457 | ) 458 | ), 459 | this.createNode('node_op', 'op_mul', 460 | Type.deepCopy(node.children[1]), 461 | Type.deepCopy(node.children[1]) 462 | ) 463 | ); 464 | break; 465 | 466 | case 'op_mul': 467 | // fg' + f'g 468 | newNode = this.createNode('node_op', 'op_add', 469 | this.createNode('node_op', 'op_mul', 470 | Type.deepCopy(node.children[0]), 471 | this.derivative(node.children[1], varname)), 472 | this.createNode('node_op', 'op_mul', 473 | this.derivative(node.children[0], varname), 474 | Type.deepCopy(node.children[1])) 475 | ); 476 | break; 477 | 478 | case 'op_neg': 479 | newNode = this.createNode('node_op', 'op_neg', 480 | this.derivative(node.children[0], varname) 481 | ); 482 | break; 483 | 484 | case 'op_add': 485 | case 'op_sub': 486 | newNode = this.createNode('node_op', node.value, 487 | this.derivative(node.children[0], varname), 488 | this.derivative(node.children[1], varname) 489 | ); 490 | break; 491 | 492 | case 'op_exp': 493 | // (f^g)' = f^g*(f'g/f + g' log(f)) 494 | newNode = this.createNode('node_op', 'op_mul', 495 | Type.deepCopy(node), 496 | this.createNode('node_op', 'op_add', 497 | this.createNode('node_op', 'op_mul', 498 | this.derivative(node.children[0], varname), 499 | this.createNode('node_op', 'op_div', 500 | Type.deepCopy(node.children[1]), 501 | Type.deepCopy(node.children[0]) 502 | ) 503 | ), 504 | this.createNode('node_op', 'op_mul', 505 | this.derivative(node.children[1], varname), 506 | this.createNode('node_op', 'op_execfun', 507 | this.createNode('node_var', 'log'), 508 | [Type.deepCopy(node.children[0])] 509 | ) 510 | ) 511 | ) 512 | ); 513 | break; 514 | } 515 | break; 516 | 517 | case 'node_var': 518 | //console.log('node_var', node); 519 | if (node.value === varname) { 520 | newNode = this.createNode('node_const', 1.0); 521 | } else { 522 | newNode = this.createNode('node_const', 0.0); 523 | } 524 | break; 525 | 526 | case 'node_const': 527 | newNode = this.createNode('node_const', 0.0); 528 | break; 529 | 530 | case 'node_const_bool': 531 | break; 532 | 533 | case 'node_str': 534 | break; 535 | 536 | } 537 | 538 | return newNode; 539 | }, 540 | 541 | /** 542 | * f = map (x) -> x*sin(x); 543 | * Usages: 544 | * h = D(f, x); 545 | * h = map (x) -> D(f, x); 546 | * 547 | */ 548 | expandDerivatives: function (node, parent, ast) { 549 | var len, i, j, mapNode, codeNode, ret, node2, newNode, 550 | mapName, varname, vArray, order; 551 | 552 | ret = 0; 553 | if (!node) { 554 | return ret; 555 | } 556 | 557 | this.line = node.line; 558 | this.col = node.col; 559 | 560 | // First we have to go down in the tree. 561 | // This ensures that in cases like D(D(f,x),x) the inner D is expanded first. 562 | len = node.children.length; 563 | for (i = 0; i < len; ++i) { 564 | if (node.children[i] && node.children[i].type) { 565 | node.children[i] = this.expandDerivatives(node.children[i], node, ast); 566 | } else if (Type.isArray(node.children[i])) { 567 | for (j = 0; j < node.children[i].length; ++j) { 568 | if (node.children[i][j] && node.children[i][j].type) { 569 | node.children[i][j] = this.expandDerivatives(node.children[i][j], node, ast); 570 | } 571 | } 572 | } 573 | } 574 | 575 | switch (node.type) { 576 | case 'node_op': 577 | switch (node.value) { 578 | case 'op_execfun': 579 | if (node.children[0] && node.children[0].value === 'D') { 580 | if (node.children[1][0].type == 'node_var') { 581 | /* 582 | * Derive map, that is compute D(f,x) 583 | * where e.g. f = map (x) -> x^2 584 | * 585 | * First step: find node where the map is defined 586 | */ 587 | mapName = node.children[1][0].value; 588 | mapNode = this.findMapNode(mapName, ast); 589 | vArray = mapNode.children[0]; 590 | 591 | // Variable name for differentiation 592 | if (node.children[1].length >= 2) { 593 | varname = node.children[1][1].value; 594 | } else { 595 | varname = mapNode.children[0][0]; // Usually it's 'x' 596 | } 597 | codeNode = mapNode.children[1]; 598 | } else { 599 | /* 600 | * Derive expression, e.g. 601 | * D(2*x, x) 602 | */ 603 | codeNode = node.children[1][0]; 604 | vArray = ['x']; 605 | 606 | // Variable name for differentiation and order 607 | if (node.children[1].length >= 2) { 608 | varname = node.children[1][1].value; 609 | } else { 610 | varname = 'x'; 611 | } 612 | } 613 | 614 | // Differentiation order 615 | if (node.children[1].length >= 3) { 616 | order = node.children[1][2].value; 617 | } else { 618 | order = 1; 619 | } 620 | 621 | // Create node which contains the derivative 622 | newNode = codeNode; 623 | //newNode = this.removeTrivialNodes(newNode); 624 | if (order >= 1) { 625 | while (order >= 1) { 626 | newNode = this.derivative(newNode, varname); 627 | newNode = this.removeTrivialNodes(newNode); 628 | order--; 629 | } 630 | } 631 | 632 | // Replace the node containing e.g. D(f,x) by the derivative. 633 | if (parent.type == 'node_op' && parent.value == 'op_assign') { 634 | // If D is an assignment it has to be replaced by a map 635 | // h = D(f, x) 636 | node2 = this.createNode('node_op', 'op_map', 637 | vArray, 638 | newNode 639 | ); 640 | } else { 641 | node2 = newNode; 642 | } 643 | 644 | this.setMath(node2); 645 | node.type = node2.type; 646 | node.value = node2.value; 647 | node.children[0] = node2.children[0]; 648 | node.children[1] = node2.children[1]; 649 | } 650 | } 651 | break; 652 | 653 | case 'node_var': 654 | case 'node_const': 655 | case 'node_const_bool': 656 | case 'node_str': 657 | break; 658 | } 659 | 660 | return node; 661 | }, 662 | 663 | removeTrivialNodes: function (node) { 664 | var i, len, n0, n1, swap; 665 | 666 | // In case of 'op_execfun' the children[1] node is an array. 667 | if (Type.isArray(node)) { 668 | len = node.length; 669 | for (i = 0; i < len; ++i) { 670 | node[i] = this.removeTrivialNodes(node[i]); 671 | } 672 | } 673 | if (node.type != 'node_op' || !node.children) { 674 | return node; 675 | } 676 | 677 | len = node.children.length; 678 | for (i = 0; i < len; ++i) { 679 | this.mayNotBeSimplified = false; 680 | do { 681 | node.children[i] = this.removeTrivialNodes(node.children[i]); 682 | } while (this.mayNotBeSimplified); 683 | 684 | } 685 | 686 | switch (node.value) { 687 | // Allow maps of the form 688 | // map (x) -> x; 689 | case 'op_map': 690 | n0 = node.children[0]; 691 | n1 = node.children[1]; 692 | if (n1.type == 'node_var') { 693 | for (i = 0; i < n0.length; ++i) { 694 | // Allow maps of the form map(x) -> x 695 | if (n0[i] == n1.value) { 696 | n1.isMath = true; 697 | break; 698 | } 699 | } 700 | } 701 | break; 702 | 703 | // a + 0 -> a 704 | // 0 + a -> a 705 | case 'op_add': 706 | n0 = node.children[0]; 707 | n1 = node.children[1]; 708 | if (n0.type == 'node_const' && n0.value === 0.0) { 709 | return n1; 710 | } 711 | if (n1.type == 'node_const' && n1.value === 0.0) { 712 | return n0; 713 | } 714 | 715 | // const + const -> const 716 | if (n0.type == 'node_const' && n1.type == 'node_const') { 717 | n0.value += n1.value; 718 | return n0; 719 | } 720 | break; 721 | 722 | // 1 * a = a 723 | // a * 1 = a 724 | // a * 0 = 0 725 | // 0 * a = 0 726 | // - * - = + 727 | // Order children 728 | case 'op_mul': 729 | n0 = node.children[0]; 730 | n1 = node.children[1]; 731 | if (n0.type == 'node_const' && n0.value == 1.0) { 732 | return n1; 733 | } 734 | if (n1.type == 'node_const' && n1.value == 1.0) { 735 | return n0; 736 | } 737 | if (n0.type == 'node_const' && n0.value === 0.0) { 738 | return n0; 739 | } 740 | if (n1.type == 'node_const' && n1.value === 0.0) { 741 | return n1; 742 | } 743 | if (n1.type == 'node_const' && n1.value === 0.0) { 744 | return n1; 745 | } 746 | 747 | // (-a) * (-b) -> a*b 748 | if (n0.type == 'node_op' && n0.value == 'op_neg' && 749 | n1.type == 'node_op' && n1.value == 'op_neg') { 750 | node.children = [n0.children[0], n1.children[0]]; 751 | this.mayNotBeSimplified = true; 752 | return node; 753 | } 754 | // (-a) * b -> -(a*b) 755 | if (n0.value == 'op_neg' && n1.value != 'op_neg') { 756 | node.type = 'node_op'; 757 | node.value = 'op_neg'; 758 | node.children = [this.createNode('node_op', 'op_mul', n0.children[0], n1)]; 759 | this.mayNotBeSimplified = true; 760 | return node; 761 | } 762 | // a * (-b) -> -(a*b) 763 | if (n0.value != 'op_neg' && n1.value == 'op_neg') { 764 | node.type = 'node_op'; 765 | node.value = 'op_neg'; 766 | node.children = [this.createNode('node_op', 'op_mul', n0, n1.children[0])]; 767 | this.mayNotBeSimplified = true; 768 | return node; 769 | } 770 | // (1 / a) * b -> a / b 771 | if (n0.value == 'op_div' && 772 | n0.children[0].type == 'node_const' && n0.children[0].value == 1.0) { 773 | node.type = 'node_op'; 774 | node.value = 'op_div'; 775 | node.children = [n1, n0.children[1]]; 776 | this.mayNotBeSimplified = true; 777 | return node; 778 | } 779 | // a * (1 / b) -> a / b 780 | if (n1.value == 'op_div' && 781 | n1.children[0].type == 'node_const' && n1.children[0].value == 1.0) { 782 | node.type = 'node_op'; 783 | node.value = 'op_div'; 784 | node.children = [n0, n1.children[1]]; 785 | this.mayNotBeSimplified = true; 786 | return node; 787 | } 788 | 789 | // Order children 790 | // a * const -> const * a 791 | if (n0.type != 'node_const' && n1.type == 'node_const') { 792 | node.children = [n1, n0]; 793 | this.mayNotBeSimplified = true; 794 | return node; 795 | } 796 | // a + (-const) -> -const * a 797 | if (n0.type != 'node_const' && n1.type == 'node_op' && 798 | n1.value == 'op_neg' && n1.children[0].type == 'node_const') { 799 | node.children = [n1, n0]; 800 | this.mayNotBeSimplified = true; 801 | return node; 802 | } 803 | 804 | // a * var -> var * a 805 | // a * fun -> fun * a 806 | if (n0.type == 'node_op' && n0.value != 'op_execfun' && 807 | (n1.type == 'node_var' || (n1.type == 'node_op' && n1.value == 'op_execfun'))) { 808 | node.children = [n1, n0]; 809 | this.mayNotBeSimplified = true; 810 | return node; 811 | } 812 | 813 | // a + (-var) -> -var * a 814 | if (n0.type != 'node_op' && n1.type == 'node_op' && 815 | n1.value == 'op_neg' && n1.children[0].type == 'node_var') { 816 | node.children = [n1, n0]; 817 | this.mayNotBeSimplified = true; 818 | return node; 819 | } 820 | // a * (const * b) -> const * (a*b) 821 | // a * (const / b) -> const * (a/b) 822 | if (n0.type != 'node_const' && n1.type == 'node_op' && 823 | (n1.value == 'op_mul' || n1.value == 'op_div') && 824 | n1.children[0].type == 'node_const') { 825 | swap = n1.children[0]; 826 | n1.children[0] = n0; 827 | node.children = [swap, n1]; 828 | this.mayNotBeSimplified = true; 829 | return node; 830 | } 831 | 832 | // (const * a) * b -> const * (a * b) 833 | if (n1.type != 'node_const' && n0.type == 'node_op' && 834 | n0.value == 'op_mul' && 835 | n0.children[0].type == 'node_const') { 836 | node.children = [ 837 | n0.children[0], 838 | this.createNode('node_op', 'op_mul', n0.children[1], n1) 839 | ]; 840 | this.mayNotBeSimplified = true; 841 | return node; 842 | } 843 | 844 | // const * const -> const 845 | if (n0.type == 'node_const' && n1.type == 'node_const') { 846 | n0.value *= n1.value; 847 | return n0; 848 | } 849 | 850 | // const * (const * a) -> const * a 851 | // const * (const / a) -> const / a 852 | if (n0.type == 'node_const' && n1.type == 'node_op' && 853 | (n1.value == 'op_mul' || n1.value == 'op_div') && 854 | n1.children[0].type == 'node_const') { 855 | n1.children[0].value *= n0.value; 856 | return n1; 857 | } 858 | 859 | // a * a-> a^2 860 | n0.hash = this.parser.compile(n0); 861 | n1.hash = this.parser.compile(n1); 862 | if (n0.hash === n1.hash) { 863 | node.value = 'op_exp'; 864 | node.children[1] = this.createNode('node_const', 2.0); 865 | return node; 866 | } 867 | 868 | if (n0.type == 'node_const' && n1.type == 'node_op' && 869 | (n1.value == 'op_mul' || n1.value == 'op_div') && 870 | n1.children[0].type == 'node_const') { 871 | n1.children[0].value *= n0.value; 872 | return n1; 873 | } 874 | 875 | // a * a^b -> a^(b+1) 876 | if (n1.type == 'node_op' && n1.value == 'op_exp') { 877 | if (!n0.hash) { 878 | n0.hash = this.parser.compile(n0); 879 | } 880 | if (!n1.children[0].hash) { 881 | n1.children[0].hash = this.parser.compile(n1.children[0]); 882 | } 883 | if (n0.hash === n1.children[0].hash) { 884 | n1.children[1] = this.createNode('node_op', 'op_add', 885 | n1.children[1], 886 | this.createNode('node_const', 1.0) 887 | ); 888 | this.mayNotBeSimplified = true; 889 | return n1; 890 | } 891 | } 892 | 893 | // a^b * a^c -> a^(b+c) 894 | if (n0.type == 'node_op' && n0.value == 'op_exp' && 895 | n1.type == 'node_op' && n1.value == 'op_exp') { 896 | n0.children[0].hash = this.parser.compile(n0.children[0]); 897 | n1.children[0].hash = this.parser.compile(n1.children[0]); 898 | if (n0.children[0].hash === n1.children[0].hash) { 899 | n0.children[1] = this.createNode('node_op', 'op_add', 900 | n0.children[1], 901 | n1.children[1] 902 | ); 903 | this.mayNotBeSimplified = true; 904 | return n0; 905 | } 906 | } 907 | 908 | break; 909 | 910 | // 0 - a -> -a 911 | // a - 0 -> a 912 | // a - a -> 0 913 | case 'op_sub': 914 | n0 = node.children[0]; 915 | n1 = node.children[1]; 916 | if (n0.type == 'node_const' && n0.value === 0.0) { 917 | node.value = 'op_neg'; 918 | node.children[0] = n1; 919 | return node; 920 | } 921 | if (n1.type == 'node_const' && n1.value === 0.0) { 922 | return n0; 923 | } 924 | if (n0.type == 'node_const' && n1.type == 'node_const' && 925 | n0.value == n1.value) { 926 | return this.createNode('node_const', 0.0); 927 | } 928 | if (n0.type == 'node_var' && n1.type == 'node_var' && 929 | n0.value == n1.value) { 930 | return this.createNode('node_const', 0.0); 931 | } 932 | 933 | // const - const -> const 934 | if (n0.type == 'node_const' && n1.type == 'node_const') { 935 | n0.value -= n1.value; 936 | return n0; 937 | } 938 | 939 | // const * a - const * a -> const * a 940 | if (n0.type == 'node_op' && n0.value == 'op_mul' && 941 | n1.type == 'node_op' && n1.value == 'op_mul') { 942 | 943 | n0.children[1].hash = this.parser.compile(n0.children[1]); 944 | n1.children[1].hash = this.parser.compile(n1.children[1]); 945 | if (n0.children[1].hash === n1.children[1].hash) { 946 | 947 | node.value = 'op_mul'; 948 | node.children = [ 949 | this.createNode('node_op', 'op_sub', 950 | n0.children[0], 951 | n1.children[0]), 952 | n0.children[1] 953 | ]; 954 | this.mayNotBeSimplified = true; 955 | return node; 956 | } 957 | } 958 | // const * a - a -> (const - 1) * a 959 | if (n0.type == 'node_op' && n0.value == 'op_mul') { 960 | 961 | n0.children[1].hash = this.parser.compile(n0.children[1]); 962 | n1.hash = this.parser.compile(n1); 963 | if (n0.children[1].hash === n1.hash) { 964 | 965 | node.value = 'op_mul'; 966 | node.children = [ 967 | this.createNode('node_op', 'op_sub', 968 | n0.children[0], 969 | this.createNode('node_const', 1.0)), 970 | n1 971 | ]; 972 | this.mayNotBeSimplified = true; 973 | return node; 974 | } 975 | } 976 | // a - const*a -> (const - 1) * a 977 | if (n1.type == 'node_op' && n1.value == 'op_mul') { 978 | 979 | n1.children[1].hash = this.parser.compile(n1.children[1]); 980 | n0.hash = this.parser.compile(n0); 981 | if (n1.children[1].hash === n0.hash) { 982 | 983 | node.value = 'op_mul'; 984 | node.children = [ 985 | this.createNode('node_op', 'op_sub', 986 | this.createNode('node_const', 1.0), 987 | n1.children[0]), 988 | n0 989 | ]; 990 | this.mayNotBeSimplified = true; 991 | return node; 992 | } 993 | } 994 | 995 | break; 996 | 997 | // -0 -> 0 998 | // -(-b) = b 999 | case 'op_neg': 1000 | n0 = node.children[0]; 1001 | if (n0.type == 'node_const' && n0.value === 0.0) { 1002 | return n0; 1003 | } 1004 | if (n0.type == 'node_op' && n0.value == 'op_neg') { 1005 | return n0.children[0]; 1006 | } 1007 | break; 1008 | 1009 | // a / a -> 1, a != 0 1010 | // 0 / a -> 0, a != 0 1011 | // a / 0 -> Infinity, a != 0 1012 | // 0 / 0 -> NaN, a == 0 1013 | case 'op_div': 1014 | n0 = node.children[0]; 1015 | n1 = node.children[1]; 1016 | if (n0.type == 'node_const' && n1.type == 'node_const' && 1017 | n0.value == n1.value && n0.value !== 0) { 1018 | n0.value = 1.0; 1019 | return n0; 1020 | } 1021 | if (n0.type == 'node_const' && n0.value === 0 && 1022 | n1.type == 'node_const' && n1.value !== 0) { 1023 | n0.value = 0.0; 1024 | return n0; 1025 | } 1026 | 1027 | // Risky: 0 / (something != 0) -> 0.0 1028 | if (n0.type == 'node_const' && n0.value === 0 && 1029 | (n1.type == 'node_op' || n1.type == 'node_var')) { 1030 | node.type = 'node_const'; 1031 | node.value = 0.0; 1032 | return node; 1033 | } 1034 | 1035 | if (n0.type == 'node_var' && n1.type == 'node_var' && 1036 | n0.value == n1.value) { 1037 | return this.createNode('node_const', 1.0); 1038 | } 1039 | if (n0.type == 'node_const' && n0.value !== 0 && 1040 | n1.type == 'node_const' && n1.value === 0) { 1041 | if (n0.value > 0.0) { 1042 | n0.value = Infinity; 1043 | } else { 1044 | n0.value = -Infinity; // Do we ever need this? 1045 | } 1046 | return n0; 1047 | } 1048 | 1049 | // (-a) / (-b) -> a/b 1050 | if (n0.type == 'node_op' && n0.value == 'op_neg' && 1051 | n1.type == 'node_op' && n1.value == 'op_neg') { 1052 | node.children = [n0.children[0], n1.children[0]]; 1053 | this.mayNotBeSimplified = true; 1054 | return node; 1055 | } 1056 | // (-a) / b -> -(a/b) 1057 | if (n0.value == 'op_neg' && n1.value != 'op_neg') { 1058 | node.type = 'node_op'; 1059 | node.value = 'op_neg'; 1060 | node.children = [this.createNode('node_op', 'op_div', n0.children[0], n1)]; 1061 | this.mayNotBeSimplified = true; 1062 | return node; 1063 | } 1064 | // a / (-b) -> -(a/b) 1065 | if (n0.value != 'op_neg' && n1.value == 'op_neg') { 1066 | node.type = 'node_op'; 1067 | node.value = 'op_neg'; 1068 | node.children = [this.createNode('node_op', 'op_div', n0, n1.children[0])]; 1069 | this.mayNotBeSimplified = true; 1070 | return node; 1071 | } 1072 | 1073 | // a^b / a -> a^(b-1) 1074 | if (n0.type == 'node_op' && n0.value == 'op_exp') { 1075 | if (!n1.hash) { 1076 | n1.hash = this.parser.compile(n1); 1077 | } 1078 | if (!n0.children[0].hash) { 1079 | n0.children[0].hash = this.parser.compile(n0.children[0]); 1080 | } 1081 | if (n1.hash === n0.children[0].hash) { 1082 | n0.children[1] = this.createNode('node_op', 'op_sub', 1083 | n0.children[1], 1084 | this.createNode('node_const', 1.0) 1085 | ); 1086 | this.mayNotBeSimplified = true; 1087 | return n0; 1088 | } 1089 | } 1090 | 1091 | // (const * a) / b -> const * (a / b) 1092 | if (n1.type != 'node_const' && n0.type == 'node_op' && 1093 | n0.value == 'op_mul' && 1094 | n0.children[0].type == 'node_const') { 1095 | node.value = 'op_mul'; 1096 | node.children = [ 1097 | n0.children[0], 1098 | this.createNode('node_op', 'op_div', n0.children[1], n1) 1099 | ]; 1100 | this.mayNotBeSimplified = true; 1101 | return node; 1102 | } 1103 | 1104 | // a^b / a^c -> a^(b-c) 1105 | if (n0.type == 'node_op' && n0.value == 'op_exp' && 1106 | n1.type == 'node_op' && n1.value == 'op_exp') { 1107 | n0.children[0].hash = this.parser.compile(n0.children[0]); 1108 | n1.children[0].hash = this.parser.compile(n1.children[0]); 1109 | if (n0.children[0].hash === n1.children[0].hash) { 1110 | n0.children[1] = this.createNode('node_op', 'op_sub', 1111 | n0.children[1], 1112 | n1.children[1] 1113 | ); 1114 | this.mayNotBeSimplified = true; 1115 | return n0; 1116 | } 1117 | } 1118 | 1119 | 1120 | break; 1121 | 1122 | // a^0 = 1 1123 | // a^1 -> a 1124 | // 1^a -> 1 1125 | // 0^a -> 0: a const != 0 1126 | case 'op_exp': 1127 | n0 = node.children[0]; 1128 | n1 = node.children[1]; 1129 | if (n1.type == 'node_const' && n1.value === 0.0) { 1130 | n1.value = 1.0; 1131 | return n1; 1132 | } 1133 | if (n1.type == 'node_const' && n1.value == 1.0) { 1134 | return n0; 1135 | } 1136 | if (n0.type == 'node_const' && n0.value == 1.0) { 1137 | return n0; 1138 | } 1139 | if (n0.type == 'node_const' && n0.value === 0.0 && 1140 | n1.type == 'node_const' && n1.value !== 0.0) { 1141 | return n0; 1142 | } 1143 | 1144 | // (a^b)^c -> a^(b*c) 1145 | if (n0.type == 'node_op' && n0.value == 'op_exp') { 1146 | node.children = [ 1147 | n0.children[0], 1148 | this.createNode('node_op', 'op_mul', 1149 | n0.children[1], 1150 | n1) 1151 | ]; 1152 | return node; 1153 | } 1154 | break; 1155 | } 1156 | 1157 | switch (node.value) { 1158 | // const_1 + const_2 -> (const_1 + const_2) 1159 | // a + a -> 2*a 1160 | // a + (-b) = a - b 1161 | case 'op_add': 1162 | n0 = node.children[0]; 1163 | n1 = node.children[1]; 1164 | if (n0.type == 'node_const' && n1.type == 'node_const' && 1165 | n0.value == n1.value) { 1166 | n0.value += n1.value; 1167 | return n0; 1168 | } 1169 | 1170 | if (n0.type == 'node_var' && n1.type == 'node_var' && 1171 | n0.value == n1.value) { 1172 | node.children[0] = this.createNode('node_const', 2.0); 1173 | node.value = 'op_mul'; 1174 | return node; 1175 | } 1176 | 1177 | if (n0.type == 'node_op' && n0.value == 'op_neg') { 1178 | node.value = 'op_sub'; 1179 | node.children[0] = n1; 1180 | node.children[1] = n0.children[0]; 1181 | this.mayNotBeSimplified = true; 1182 | return node; 1183 | } 1184 | 1185 | if (n1.type == 'node_op' && n1.value == 'op_neg') { 1186 | node.value = 'op_sub'; 1187 | node.children[1] = n1.children[0]; 1188 | this.mayNotBeSimplified = true; 1189 | return node; 1190 | } 1191 | 1192 | // const * a + const * a -> const * a 1193 | if (n0.type == 'node_op' && n0.value == 'op_mul' && 1194 | n1.type == 'node_op' && n1.value == 'op_mul') { 1195 | 1196 | n0.children[1].hash = this.parser.compile(n0.children[1]); 1197 | n1.children[1].hash = this.parser.compile(n1.children[1]); 1198 | if (n0.children[1].hash === n1.children[1].hash) { 1199 | 1200 | node.value = 'op_mul'; 1201 | node.children = [ 1202 | this.createNode('node_op', 'op_add', 1203 | n0.children[0], 1204 | n1.children[0]), 1205 | n0.children[1] 1206 | ]; 1207 | this.mayNotBeSimplified = true; 1208 | return node; 1209 | } 1210 | } 1211 | // const * a + a -> (const + 1) * a 1212 | if (n0.type == 'node_op' && n0.value == 'op_mul') { 1213 | 1214 | n0.children[1].hash = this.parser.compile(n0.children[1]); 1215 | n1.hash = this.parser.compile(n1); 1216 | if (n0.children[1].hash === n1.hash) { 1217 | 1218 | node.value = 'op_mul'; 1219 | node.children = [ 1220 | this.createNode('node_op', 'op_add', 1221 | n0.children[0], 1222 | this.createNode('node_const', 1.0)), 1223 | n1 1224 | ]; 1225 | this.mayNotBeSimplified = true; 1226 | return node; 1227 | } 1228 | } 1229 | // a + const*a -> (const + 1) * a 1230 | if (n1.type == 'node_op' && n1.value == 'op_mul') { 1231 | 1232 | n1.children[1].hash = this.parser.compile(n1.children[1]); 1233 | n0.hash = this.parser.compile(n0); 1234 | if (n1.children[1].hash === n0.hash) { 1235 | 1236 | node.value = 'op_mul'; 1237 | node.children = [ 1238 | this.createNode('node_op', 'op_add', 1239 | this.createNode('node_const', 1.0), 1240 | n1.children[0]), 1241 | n0 1242 | ]; 1243 | this.mayNotBeSimplified = true; 1244 | return node; 1245 | } 1246 | } 1247 | 1248 | break; 1249 | 1250 | // a - (-b) = a + b 1251 | case 'op_sub': 1252 | n0 = node.children[0]; 1253 | n1 = node.children[1]; 1254 | if (n1.type == 'node_op' && n1.value == 'op_neg') { 1255 | node.value = 'op_add'; 1256 | node.children[1] = n1.children[0]; 1257 | this.mayNotBeSimplified = true; 1258 | return node; 1259 | } 1260 | break; 1261 | 1262 | case 'op_execfun': 1263 | return this.simplifyElementary(node); 1264 | } 1265 | 1266 | return node; 1267 | }, 1268 | 1269 | simplifyElementary: function (node) { 1270 | var fun = node.children[0].value, 1271 | arg = node.children[1]; 1272 | 1273 | // Catch errors of the form sin() 1274 | if (arg.length == 0) { 1275 | return node; 1276 | } 1277 | 1278 | switch (fun) { 1279 | // sin(0) -> 0 1280 | // sin(PI) -> 0 1281 | // sin (int * PI) -> 0 1282 | // sin (PI * int) -> 0 1283 | // Same for tan() 1284 | case 'sin': 1285 | case 'tan': 1286 | if (arg[0].type == 'node_const' && arg[0].value === 0) { 1287 | node.type = 'node_const'; 1288 | node.value = 0.0; 1289 | return node; 1290 | } 1291 | if (arg[0].type == 'node_var' && arg[0].value == 'PI') { 1292 | node.type = 'node_const'; 1293 | node.value = 0.0; 1294 | return node; 1295 | } 1296 | if (arg[0].type == 'node_op' && arg[0].value == 'op_mul' && 1297 | arg[0].children[0].type == 'node_const' && arg[0].children[0].value % 1 === 0 && 1298 | arg[0].children[1].type == 'node_var' && arg[0].children[1].value == 'PI') { 1299 | node.type = 'node_const'; 1300 | node.value = 0.0; 1301 | return node; 1302 | } 1303 | break; 1304 | 1305 | // cos(0) -> 1.0 1306 | // cos(PI) -> -1.0 1307 | // cos(int * PI) -> +/- 1.0 1308 | // cos(PI * int) -> +/- 1.0 1309 | case 'cos': 1310 | if (arg[0].type == 'node_const' && arg[0].value === 0) { 1311 | node.type = 'node_const'; 1312 | node.value = 1.0; 1313 | return node; 1314 | } 1315 | if (arg[0].type == 'node_var' && arg[0].value == 'PI') { 1316 | node.type = 'node_op'; 1317 | node.value = 'op_neg'; 1318 | node.children = [this.createNode('node_const', 1.0)]; 1319 | return node; 1320 | } 1321 | /* 1322 | if (arg[0].type == 'node_op' && arg[0].value == 'op_mul' && 1323 | ((arg[0].children[0].type == 'node_const' && arg[0].children[0].value % 1 === 0 && 1324 | arg[0].children[1].type == 'node_var' && arg[0].children[1].value == 'PI') || 1325 | (arg[0].children[1].type == 'node_const' && arg[0].children[1].value % 1 === 0 && 1326 | arg[0].children[0].type == 'node_var' && arg[0].children[0].value == 'PI'))) { 1327 | node.type = 'node_const'; 1328 | node.value = 1.0; 1329 | return node; 1330 | } 1331 | */ 1332 | break; 1333 | 1334 | // exp(0) -> 1 1335 | case 'exp': 1336 | if (arg[0].type == 'node_const' && arg[0].value === 0) { 1337 | node.type = 'node_const'; 1338 | node.value = 1.0; 1339 | return node; 1340 | } 1341 | break; 1342 | 1343 | // pow(a, 0) -> 1 1344 | case 'pow': 1345 | if (arg[1].type == 'node_const' && arg[1].value === 0) { 1346 | node.type = 'node_const'; 1347 | node.value = 1.0; 1348 | return node; 1349 | } 1350 | break; 1351 | 1352 | } 1353 | 1354 | return node; 1355 | } 1356 | 1357 | }); 1358 | 1359 | return JXG.CA; 1360 | }); 1361 | -------------------------------------------------------------------------------- /src/grammar.jison: -------------------------------------------------------------------------------- 1 | /* -- JavaScript code -- */ 2 | %{ 3 | 4 | var AST = { 5 | node: function (type, value, children) { 6 | return { 7 | type: type, 8 | value: value, 9 | children: children 10 | }; 11 | }, 12 | 13 | createNode: function (pos, type, value, children) { 14 | var i, 15 | n = this.node(type, value, []); 16 | 17 | for (i = 3; i < arguments.length; i++) { 18 | n.children.push(arguments[i]); 19 | } 20 | 21 | n.line = pos[0]; 22 | n.col = pos[1]; 23 | n.eline = pos[2]; 24 | n.ecol = pos[3]; 25 | 26 | return n; 27 | } 28 | }; 29 | 30 | var lc = function (lc1) { 31 | return [lc1.first_line, lc1.first_column, lc1.last_line, lc1.last_column]; 32 | }; 33 | 34 | %} 35 | 36 | /* ----------------------------------------------------------------- */ 37 | /* Grammar definition of JessieCode */ 38 | /* ----------------------------------------------------------------- */ 39 | /* */ 40 | /* Copyright 2011-2024 */ 41 | /* Michael Gerhaeuser, */ 42 | /* Alfred Wassermann */ 43 | /* */ 44 | /* JessieCode is free software dual licensed under the GNU LGPL or */ 45 | /* MIT License. */ 46 | /* */ 47 | /* You can redistribute it and/or modify it under the terms of the */ 48 | /* */ 49 | /* * GNU Lesser General Public License as published by */ 50 | /* the Free Software Foundation, either version 3 of the License, */ 51 | /* or (at your option) any later version */ 52 | /* OR */ 53 | /* * MIT License: */ 54 | /* https://github.com/jsxgraph/jsxgraph/blob/master/LICENSE.MIT */ 55 | /* */ 56 | /* JessieCode is distributed in the hope that it will be useful, */ 57 | /* but WITHOUT ANY WARRANTY; without even the implied warranty of */ 58 | /* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the */ 59 | /* GNU Lesser General Public License for more details. */ 60 | /* */ 61 | /* You should have received a copy of the GNU Lesser General Public */ 62 | /* License and the MIT License along with JessieCode. If not, see */ 63 | /* and */ 64 | /* . */ 65 | /* */ 66 | /* ----------------------------------------------------------------- */ 67 | 68 | %lex 69 | 70 | %% 71 | 72 | \s+ /* ignore */ 73 | [0-9]*\.?[0-9]+([eE][-+]?[0-9]+) return 'NUMBER' /* New 123.1234e+-12 */ 74 | [0-9]+\.[0-9]*|[0-9]*\.[0-9]+\b return 'NUMBER' /* Old 123.1234 or .1234 */ 75 | [0-9]+ return 'NUMBER' /* Old 123 */ 76 | 77 | 78 | '"'("\\"["]|[^"])*'"' { return 'STRING'; } 79 | "'"('\\'[']|[^'])*"'" { return 'STRING'; } 80 | 81 | 82 | \/\/.* /* ignore comment */ 83 | "/*"(.|\n|\r)*?"*/" /* ignore multiline comment */ 84 | 85 | "if" return 'IF' 86 | "else" return 'ELSE' 87 | "while" return 'WHILE' 88 | "do" return 'DO' 89 | "for" return 'FOR' 90 | "function" return 'FUNCTION' 91 | "map" return 'MAP' 92 | "use" return 'USE' 93 | "return" return 'RETURN' 94 | "delete" return 'DELETE' 95 | "true" return 'TRUE' 96 | "false" return 'FALSE' 97 | "null" return 'NULL' 98 | "Infinity" return 'INFINITY' 99 | /*"\u221E" return 'INFINITY'*/ 100 | "->" return '->' 101 | "=>" return '->' 102 | "<<" return '<<' 103 | ">>" return '>>' 104 | "{" return '{' 105 | "}" return '}' 106 | ";" return ';' 107 | "#" return '#' 108 | "?" return '?' 109 | ":" return ':' 110 | "NaN" return 'NAN' 111 | "." return '.' 112 | "[" return '[' 113 | "]" return ']' 114 | "(" return '(' 115 | ")" return ')' 116 | "!" return '!' 117 | "^" return '^' 118 | "**" return '^' 119 | "*" return '*' 120 | "/" return '/' 121 | "%" return '%' 122 | "+" return '+' 123 | "-" return '-' 124 | "<=" return '<=' 125 | "<" return '<' 126 | ">=" return '>=' 127 | ">" return '>' 128 | "==" return '==' 129 | "~=" return '~=' 130 | "!=" return '!=' 131 | "&&" return '&&' 132 | "||" return '||' 133 | "=" return '=' 134 | "," return ',' 135 | <> return 'EOF' 136 | 137 | [A-Za-z_\$][A-Za-z0-9_]* return 'IDENTIFIER' 138 | 139 | . return 'INVALID' 140 | 141 | 142 | /lex 143 | 144 | 145 | /* operator association and precedence */ 146 | 147 | %nonassoc EMPTY 148 | %nonassoc ATTRLST 149 | 150 | /* list element separator */ 151 | %left ',' 152 | 153 | /* assignment */ 154 | %right '=' 155 | 156 | /* conditional */ 157 | %right '?' ':' 158 | 159 | /* logical or */ 160 | %left '||' 161 | 162 | /* logical and */ 163 | %left '&&' 164 | 165 | /* equality */ 166 | %left '==' '~=' '!=' 167 | 168 | /* relational */ 169 | %left '<=' '<' '>=' '>' 170 | 171 | /* addition arithmetic */ 172 | %left '+' '-' 173 | 174 | /* multiplication arithmetic */ 175 | %left '*' '/' 176 | 177 | %right '%' 178 | 179 | /* exponentiation */ 180 | %right '^' 181 | 182 | /* unary operators */ 183 | %right UMINUS UPLUS '!' 184 | 185 | /* function call */ 186 | %left '(' ')' 187 | 188 | /* member access */ 189 | %left '.' '[' ']' 190 | 191 | %nonassoc '{' '}' 192 | %nonassoc IFNOEELSE 193 | %nonassoc 'ELSE' 194 | 195 | %start Program 196 | %% 197 | 198 | /* language grammar */ 199 | 200 | Program 201 | : StatementList EOF { return $1; } 202 | ; 203 | 204 | IfStatement 205 | : "IF" "(" Expression ")" Statement %prec IFNOELSE { $$ = AST.createNode(lc(@1), 'node_op', 'op_if', $3, $5); } 206 | | "IF" "(" Expression ")" Statement "ELSE" Statement { $$ = AST.createNode(lc(@1), 'node_op', 'op_if_else', $3, $5, $7); } 207 | ; 208 | 209 | LoopStatement 210 | : "WHILE" "(" Expression ")" Statement { $$ = AST.createNode(lc(@1), 'node_op', 'op_while', $3, $5); } 211 | | "FOR" "(" Expression ";" Expression ";" Expression ")" Statement { $$ = AST.createNode(lc(@1), 'node_op', 'op_for', $3, $5, $7, $9); } 212 | | "DO" Statement "WHILE" "(" Expression ")" ";" { $$ = AST.createNode(lc(@1), 'node_op', 'op_do', $2, $5); } 213 | ; 214 | 215 | UnaryStatement 216 | : "USE" "IDENTIFIER" { $$ = AST.createNode(lc(@1), 'node_op', 'op_use', $2); } 217 | | "DELETE" "IDENTIFIER" { $$ = AST.createNode(lc(@1), 'node_op', 'op_delete', $2); } 218 | ; 219 | 220 | ReturnStatement 221 | : "RETURN" ";" { $$ = AST.createNode(lc(@1), 'node_op', 'op_return', undefined); } 222 | | "RETURN" Expression ";" { $$ = AST.createNode(lc(@1), 'node_op', 'op_return', $2); } 223 | ; 224 | 225 | EmptyStatement 226 | : ";" { $$ = AST.createNode(lc(@1), 'node_op', 'op_none'); } 227 | ; 228 | 229 | StatementBlock 230 | : "{" StatementList "}" { $$ = AST.createNode(lc(@1), 'node_op', 'op_block', $2); } 231 | ; 232 | 233 | StatementList 234 | : StatementList Statement { $$ = AST.createNode(lc(@1), 'node_op', 'op_none', $1, $2); } 235 | | { $$ = AST.createNode(lc(@1), 'node_op', 'op_none'); } 236 | ; 237 | 238 | Statement 239 | : StatementBlock { $$ = $1; } 240 | | IfStatement { $$ = $1; } 241 | | LoopStatement { $$ = $1; } 242 | | UnaryStatement { $$ = $1; } 243 | | ReturnStatement { $$ = $1; } 244 | | ExpressionStatement { $$ = $1; } 245 | | EmptyStatement { $$ = $1; } 246 | ; 247 | 248 | ExpressionStatement 249 | : Expression ";" { $$ = $1; } 250 | ; 251 | 252 | Expression 253 | : AssignmentExpression %prec EMPTY { $$ = $1; } 254 | /*| Expression "," AssignmentExpression { $$ = AST.createNode(lc(@1), 'node_op', 'op_none', $1, $3); }*/ 255 | ; 256 | 257 | AssignmentExpression 258 | : ConditionalExpression %prec EMPTY { $$ = $1; } 259 | | LeftHandSideExpression "=" AssignmentExpression { $$ = AST.createNode(lc(@1), 'node_op', 'op_assign', $1, $3); $$.isMath = false; } 260 | /* TODO: Implement special assignment operators like +=, -=, ... */ 261 | ; 262 | 263 | ConditionalExpression 264 | : LogicalORExpression %prec EMPTY { $$ = $1; } 265 | | LogicalORExpression "?" AssignmentExpression ":" AssignmentExpression { $$ = AST.createNode(lc(@1), 'node_op', 'op_conditional', $1, $3, $5); $$.isMath = false; } 266 | ; 267 | 268 | LogicalORExpression 269 | : LogicalANDExpression %prec EMPTY { $$ = $1; } 270 | | LogicalORExpression "||" LogicalANDExpression { $$ = AST.createNode(lc(@1), 'node_op', 'op_or', $1, $3); $$.isMath = false; } 271 | ; 272 | 273 | LogicalANDExpression 274 | : EqualityExpression %prec EMPTY { $$ = $1; } 275 | | LogicalANDExpression "&&" EqualityExpression { $$ = AST.createNode(lc(@1), 'node_op', 'op_and', $1, $3); $$.isMath = false; } 276 | ; 277 | 278 | EqualityExpression 279 | : RelationalExpression %prec EMPTY { $$ = $1; } 280 | | EqualityExpression "==" RelationalExpression { $$ = AST.createNode(lc(@1), 'node_op', 'op_eq', $1, $3); $$.isMath = false; } 281 | | EqualityExpression "!=" RelationalExpression { $$ = AST.createNode(lc(@1), 'node_op', 'op_neq', $1, $3); $$.isMath = false; } 282 | | EqualityExpression "~=" RelationalExpression { $$ = AST.createNode(lc(@1), 'node_op', 'op_approx', $1, $3); $$.isMath = false; } 283 | ; 284 | 285 | RelationalExpression 286 | : AdditiveExpression %prec EMPTY { $$ = $1; } 287 | | RelationalExpression "<" AdditiveExpression { $$ = AST.createNode(lc(@1), 'node_op', 'op_lt', $1, $3); $$.isMath = false; } 288 | | RelationalExpression ">" AdditiveExpression { $$ = AST.createNode(lc(@1), 'node_op', 'op_gt', $1, $3); $$.isMath = false; } 289 | | RelationalExpression "<=" AdditiveExpression { $$ = AST.createNode(lc(@1), 'node_op', 'op_leq', $1, $3); $$.isMath = false; } 290 | | RelationalExpression ">=" AdditiveExpression { $$ = AST.createNode(lc(@1), 'node_op', 'op_geq', $1, $3); $$.isMath = false; } 291 | ; 292 | 293 | AdditiveExpression 294 | : MultiplicativeExpression %prec EMPTY { $$ = $1; } 295 | | AdditiveExpression "+" MultiplicativeExpression { $$ = AST.createNode(lc(@1), 'node_op', 'op_add', $1, $3); $$.isMath = true; } 296 | | AdditiveExpression "-" MultiplicativeExpression { $$ = AST.createNode(lc(@1), 'node_op', 'op_sub', $1, $3); $$.isMath = true; } 297 | ; 298 | 299 | MultiplicativeExpression 300 | : UnaryExpression %prec EMPTY { $$ = $1; } 301 | | MultiplicativeExpression "*" UnaryExpression { $$ = AST.createNode(lc(@1), 'node_op', 'op_mul', $1, $3); $$.isMath = true; } 302 | | MultiplicativeExpression "/" UnaryExpression { $$ = AST.createNode(lc(@1), 'node_op', 'op_div', $1, $3); $$.isMath = true; } 303 | | MultiplicativeExpression "%" UnaryExpression { $$ = AST.createNode(lc(@1), 'node_op', 'op_mod', $1, $3); $$.isMath = true; } 304 | ; 305 | 306 | ExponentExpression 307 | : LeftHandSideExpression %prec EMPTY { $$ = $1; } 308 | | LeftHandSideExpression "^" UnaryExpression { $$ = AST.createNode(lc(@1), 'node_op', 'op_exp', $1, $3); $$.isMath = true; } 309 | ; 310 | 311 | UnaryExpression 312 | : ExponentExpression %prec EMPTY { $$ = $1; } 313 | | "!" UnaryExpression { $$ = AST.createNode(lc(@1), 'node_op', 'op_not', $2); $$.isMath = false; } 314 | | "+" UnaryExpression %prec UPLUS { $$ = $2; } 315 | | "-" UnaryExpression %prec UMINUS { $$ = AST.createNode(lc(@1), 'node_op', 'op_neg', $2); $$.isMath = true; } 316 | ; 317 | 318 | LeftHandSideExpression 319 | : MemberExpression %prec EMPTY { $$ = $1; } 320 | | CallExpression %prec EMPTY { $$ = $1; } 321 | ; 322 | 323 | MemberExpression 324 | : PrimaryExpression %prec EMPTY { $$ = $1; } 325 | | FunctionExpression %prec EMPTY { $$ = $1; $$.isMath = false; } 326 | | MapExpression %prec EMPTY { $$ = $1; } 327 | | MemberExpression "." "IDENTIFIER" { $$ = AST.createNode(lc(@1), 'node_op', 'op_property', $1, $3); $$.isMath = true; } 328 | | MemberExpression "[" Expression "]" { $$ = AST.createNode(lc(@1), 'node_op', 'op_extvalue', $1, $3); $$.isMath = true; } 329 | ; 330 | 331 | PrimaryExpression 332 | : "IDENTIFIER" { $$ = AST.createNode(lc(@1), 'node_var', $1); } 333 | | BasicLiteral { $$ = $1; } 334 | | ObjectLiteral { $$ = $1; $$.isMath = false; } 335 | | ArrayLiteral { $$ = $1; $$.isMath = false; } 336 | | "(" Expression ")" { $$ = $2; } 337 | ; 338 | 339 | BasicLiteral 340 | : NullLiteral { $$ = $1; $$.isMath = false; } 341 | | BooleanLiteral { $$ = $1; $$.isMath = false; } 342 | | StringLiteral { $$ = $1; $$.isMath = false; } 343 | | NumberLiteral { $$ = $1; $$.isMath = true; } 344 | ; 345 | 346 | NullLiteral 347 | : "NULL" { $$ = AST.createNode(lc(@1), 'node_const', null); } 348 | ; 349 | 350 | BooleanLiteral 351 | : "TRUE" { $$ = AST.createNode(lc(@1), 'node_const_bool', true); } 352 | | "FALSE" { $$ = AST.createNode(lc(@1), 'node_const_bool', false); } 353 | ; 354 | 355 | StringLiteral 356 | : "STRING" { $$ = AST.createNode(lc(@1), 'node_str', $1.substring(1, $1.length - 1)); } 357 | ; 358 | 359 | NumberLiteral 360 | : "NUMBER" { $$ = AST.createNode(lc(@1), 'node_const', parseFloat($1)); } 361 | | "NAN" { $$ = AST.createNode(lc(@1), 'node_const', NaN); } 362 | | "INFINITY" { $$ = AST.createNode(lc(@1), 'node_const', Infinity); } 363 | ; 364 | 365 | ArrayLiteral 366 | : "[" "]" { $$ = AST.createNode(lc(@1), 'node_op', 'op_array', []); } 367 | | "[" ElementList "]" { $$ = AST.createNode(lc(@1), 'node_op', 'op_array', $2); } 368 | ; 369 | 370 | ObjectLiteral 371 | : "<<" ">>" { $$ = AST.createNode(lc(@1), 'node_op', 'op_emptyobject', {}); $$.needsAngleBrackets = true; } 372 | | "<<" PropertyList ">>" { $$ = AST.createNode(lc(@1), 'node_op', 'op_proplst_val', $2); $$.needsAngleBrackets = true; } 373 | ; 374 | 375 | PropertyList 376 | : Property { $$ = $1; } 377 | | PropertyList "," Property { $$ = AST.createNode(lc(@1), 'node_op', 'op_proplst', $1, $3); } 378 | ; 379 | 380 | Property 381 | : PropertyName ":" AssignmentExpression { $$ = AST.createNode(lc(@1), 'node_op', 'op_prop', $1, $3); } 382 | ; 383 | 384 | PropertyName 385 | : "IDENTIFIER" { $$ = $1; } 386 | | StringLiteral { $$ = $1; } 387 | | NumberLiteral { $$ = $1; } 388 | ; 389 | 390 | CallExpression 391 | : MemberExpression Arguments { $$ = AST.createNode(lc(@1), 'node_op', 'op_execfun', $1, $2); $$.isMath = true; } 392 | | MemberExpression Arguments AttributeList %prec ATTRLST { $$ = AST.createNode(lc(@1), 'node_op', 'op_execfun', $1, $2, $3, true); $$.isMath = false; } 393 | | CallExpression Arguments { $$ = AST.createNode(lc(@1), 'node_op', 'op_execfun', $1, $2); $$.isMath = true; } 394 | | CallExpression "[" Expression "]" { $$ = AST.createNode(lc(@1), 'node_op', 'op_extvalue', $1, $3); $$.isMath = true; } 395 | | CallExpression "." "IDENTIFIER" { $$ = AST.createNode(lc(@1), 'node_op', 'op_property', $1, $3); $$.isMath = true; } 396 | ; 397 | 398 | Arguments 399 | : "(" ")" { $$ = []; } 400 | | "(" ElementList ")" { $$ = $2; } 401 | ; 402 | 403 | AttributeList 404 | : Attribute { $$ = [$1]; } 405 | | AttributeList "," Attribute { $$ = $1.concat($3); } 406 | ; 407 | 408 | Attribute 409 | : "IDENTIFIER" { $$ = AST.createNode(lc(@1), 'node_var', $1); $$.isMath = true; } 410 | | ObjectLiteral { $$ = $1; $$.isMath = false; } 411 | ; 412 | 413 | ElementList 414 | : AssignmentExpression { $$ = [$1]; } 415 | | ElementList "," AssignmentExpression { $$ = $1.concat($3); } 416 | ; 417 | FunctionExpression 418 | : "FUNCTION" "(" ")" StatementBlock { $$ = AST.createNode(lc(@1), 'node_op', 'op_function', [], $4); $$.isMath = false; } 419 | | "FUNCTION" "(" ParameterDefinitionList ")" StatementBlock { $$ = AST.createNode(lc(@1), 'node_op', 'op_function', $3, $5); $$.isMath = false; } 420 | ; 421 | 422 | MapExpression 423 | : "MAP" "(" ")" "->" Expression { $$ = AST.createNode(lc(@1), 'node_op', 'op_map', [], $5); } 424 | | "MAP" "(" ParameterDefinitionList ")" "->" Expression { $$ = AST.createNode(lc(@1), 'node_op', 'op_map', $3, $6); } 425 | ; 426 | 427 | ParameterDefinitionList 428 | : "IDENTIFIER" { $$ = [$1]; } 429 | | ParameterDefinitionList "," "IDENTIFIER" { $$ = $1.concat($3); } 430 | ; 431 | 432 | /* previously the noassign node was expressions without assignment */ 433 | /* { $$ = AST.createNode(lc(@1), 'node_op', 'op_noassign', $1); } */ 434 | -------------------------------------------------------------------------------- /test/jsTestDriver.conf: -------------------------------------------------------------------------------- 1 | server: http://localhost:4224 2 | 3 | load: 4 | - bin/jsxgraphcore.js 5 | - test/*.js 6 | -------------------------------------------------------------------------------- /test/testArithmetic.js: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright 2011-2012 3 | Michael Gerhaeuser, 4 | Alfred Wassermann, 5 | 6 | */ 7 | 8 | /* 9 | * Js-Test-Driver Test Suite for Generic JavaScript language tests 10 | * http://code.google.com/p/js-test-driver 11 | */ 12 | 13 | TestCase("Arithmetic", { 14 | jc: null, 15 | 16 | setUp: function () { 17 | this.jc = new JXG.JessieCode(); 18 | }, 19 | 20 | tearDown: function () { 21 | this.jc = null; 22 | }, 23 | 24 | testFloat: function () { 25 | expectAsserts(2); 26 | 27 | try { 28 | this.jc.parse('\ 29 | a = 1; \ 30 | b = 1.5; \ 31 | '); 32 | } catch (e) { 33 | console.log(e); 34 | } 35 | 36 | assertEquals('int', 1, this.jc.scope.locals.a); 37 | assertEquals('float', 1.5, this.jc.scope.locals.b); 38 | }, 39 | 40 | testAdd: function () { 41 | expectAsserts(7); 42 | 43 | try { 44 | this.jc.parse( 45 | 'a = +1;'+ 46 | 'b = 1+1;'+ 47 | 48 | 'c = [1, 2] + [3, 4];'+ 49 | 's = \'hello\';' + 50 | 'r = s + \' world\';' + 51 | 't = s + b;' 52 | ); 53 | } catch (e) { 54 | console.log(e); 55 | } 56 | 57 | assertEquals('unary add', 1, this.jc.scope.locals.a); 58 | assertEquals('binary add', 2, this.jc.scope.locals.b); 59 | assertEquals('vector add returns array', 2, this.jc.scope.locals.c.length); 60 | assertEquals('vector add result 0', 4, this.jc.scope.locals.c[0]); 61 | assertEquals('vector add result 1', 6, this.jc.scope.locals.c[1]); 62 | assertEquals('string/string add', 'hello world', this.jc.scope.locals.r); 63 | assertEquals('string/number add', 'hello2', this.jc.scope.locals.t); 64 | }, 65 | 66 | testSub: function () { 67 | expectAsserts(7); 68 | 69 | try { 70 | this.jc.parse( 71 | 'a = -1;'+ 72 | 'b = 1-1;' + 73 | 'd = 1--1;' + 74 | 'e = 1---1;'+ 75 | 76 | 'c = [3, 4] - [1, 2];'); 77 | 78 | } catch (e) { 79 | console.log(e); 80 | } 81 | 82 | assertEquals('unary minus', -1, this.jc.scope.locals.a); 83 | assertEquals('subtraction', 0, this.jc.scope.locals.b); 84 | assertEquals('vector sub returns array', 2, this.jc.scope.locals.c.length); 85 | assertEquals('vector sub result 0', 2, this.jc.scope.locals.c[0]); 86 | assertEquals('vector sub result 1', 2, this.jc.scope.locals.c[1]); 87 | assertEquals('double minus result', 2, this.jc.scope.locals.d); 88 | assertEquals('double minus result', 0, this.jc.scope.locals.e); 89 | }, 90 | 91 | testMul: function () { 92 | expectAsserts(6); 93 | 94 | try { 95 | this.jc.parse( 96 | 'a = 1*2;'+ 97 | 'b = 3*-4;'+ 98 | 'c = -3*-2;'+ 99 | 100 | 'd = 3*[1, 2];'+ 101 | 'e = [1, 2]*[3, 4];'); 102 | } catch (e) { 103 | console.log(e); 104 | } 105 | 106 | assertEquals('multiplication', 2, this.jc.scope.locals.a); 107 | assertEquals('multiplication with sign change', -12, this.jc.scope.locals.b); 108 | assertEquals('multiplication with double sign change', 6, this.jc.scope.locals.c); 109 | assertEquals('multiplication array result 0', 3, this.jc.scope.locals.d[0]); 110 | assertEquals('multiplication array result 1', 6, this.jc.scope.locals.d[1]); 111 | assertEquals('scalar product', 11, this.jc.scope.locals.e); 112 | }, 113 | 114 | testDiv: function () { 115 | expectAsserts(6); 116 | 117 | try { 118 | this.jc.parse( 119 | 'a = 1/2;'+ 120 | 'b = 12/4;'+ 121 | 'c = 4/-2;'+ 122 | 'd = -6/-4;'+ 123 | 124 | 'e = [4, 6] / 2;'); 125 | } catch (e) { 126 | console.log(e); 127 | } 128 | 129 | assertEquals('float division', 0.5, this.jc.scope.locals.a); 130 | assertEquals('integer division', 3, this.jc.scope.locals.b); 131 | assertEquals('division with sign change', -2, this.jc.scope.locals.c); 132 | assertEquals('division with double sign change', 1.5, this.jc.scope.locals.d); 133 | assertEquals('div array result 0', 2, this.jc.scope.locals.e[0]); 134 | assertEquals('div array result 1', 3, this.jc.scope.locals.e[1]); 135 | }, 136 | 137 | testMod: function () { 138 | expectAsserts(5); 139 | 140 | try { 141 | this.jc.parse( 142 | 'a = 1%2;'+ 143 | 'b = 12%4;'+ 144 | 'c = 9%-4;'+ 145 | 146 | 'd = [3, 4] % 2;'); 147 | } catch (e) { 148 | console.log(e); 149 | } 150 | 151 | assertEquals('mod#1', 1, this.jc.scope.locals.a); 152 | assertEquals('mod#2', 0, this.jc.scope.locals.b); 153 | // assure the mathematical mod is used, not the symmetric 154 | assertEquals('mod#3', -3, this.jc.scope.locals.c); 155 | 156 | assertEquals('mod vector 0', 1, this.jc.scope.locals.d[0]); 157 | assertEquals('mod vector 1', 0, this.jc.scope.locals.d[1]); 158 | }, 159 | 160 | testPow: function () { 161 | expectAsserts(5); 162 | 163 | try { 164 | this.jc.parse('\ 165 | a = 2^3; \ 166 | b = -2^3; \ 167 | c = 2^-3; \ 168 | d = 2^3^2; \ 169 | e = -2^2;\ 170 | 2^3;\ 171 | '); 172 | } catch (e) { 173 | console.log(e); 174 | } 175 | 176 | assertEquals('pow', 8, this.jc.scope.locals.a); 177 | assertEquals('neg pow with base', -8, this.jc.scope.locals.b); 178 | assertEquals('pow with neg exp', 1/8, this.jc.scope.locals.c); 179 | assertEquals('pow associativity', 512, this.jc.scope.locals.d); 180 | assertEquals('pow -2^2', -4, this.jc.scope.locals.e); 181 | }, 182 | 183 | testPrecedence: function () { 184 | expectAsserts(7); 185 | 186 | try { 187 | this.jc.parse( 188 | 'a = 3*4+5;'+ 189 | 'b = 7-2*3+4;'+ 190 | 'c = 2^3*4;'+ 191 | 'd = 4^3^2;'); 192 | } catch (e) { 193 | console.log(e); 194 | } 195 | 196 | assertEquals('mul vs add', 17, this.jc.scope.locals.a); 197 | assertEquals('mul vs sub and add', 5, this.jc.scope.locals.b); 198 | assertEquals('pow vs mul', 32, this.jc.scope.locals.c); 199 | assertEquals('pow precedence order', 262144, this.jc.scope.locals.d); 200 | 201 | try { 202 | this.jc.parse(' \ 203 | a = 3*(4+5); \ 204 | b = (4^3)^2; \ 205 | c = (-2)^3; \ 206 | '); 207 | } catch (e) { 208 | console.log(e); 209 | } 210 | 211 | assertEquals('caged add', 27, this.jc.scope.locals.a); 212 | assertEquals('change precedence of pow', 4096, this.jc.scope.locals.b); 213 | assertEquals('pow with neg base', -8, this.jc.scope.locals.c); 214 | }, 215 | 216 | testUnaryMinus: function () { 217 | expectAsserts(1); 218 | 219 | try { 220 | this.jc.parse('y = -2^4;'); 221 | } catch (e) { 222 | console.log(e); 223 | } 224 | 225 | assertEquals('unary minus test -x^n', -16, this.jc.scope.locals.y); 226 | } 227 | 228 | }); 229 | -------------------------------------------------------------------------------- /test/testBuiltin.js: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright 2011-2023 3 | Michael Gerhaeuser, 4 | Alfred Wassermann, 5 | 6 | */ 7 | 8 | /* 9 | * Js-Test-Driver Test Suite for Generic JavaScript language tests 10 | * http://code.google.com/p/js-test-driver 11 | */ 12 | 13 | TestCase("Builtin", { 14 | jc: null, 15 | 16 | setUp: function () { 17 | try { 18 | document.getElementsByTagName('body')[0].innerHTML = '
'; 19 | this.board = JXG.JSXGraph.initBoard('jxgbox', {axis: false, grid: false, boundingbox: [-5, 5, 5, -5], showCopyright: false, showNavigation: false}); 20 | } catch (e) { 21 | console.log(e); 22 | } 23 | }, 24 | 25 | tearDown: function () { 26 | JXG.JSXGraph.freeBoard(this.board); 27 | }, 28 | 29 | testRemove: function () { 30 | expectAsserts(1); 31 | 32 | try { 33 | this.board.jc.parse( 34 | 'a = point(1, 1);' + 35 | 'b = point(1, 1) <>;' + 36 | 'c = point(1, 1) <>;' + 37 | 'remove(a);' + 38 | 'remove(ID);' + 39 | 'remove(NAME);' 40 | ); 41 | } catch (e) { 42 | console.log(e); 43 | } 44 | 45 | assertEquals('delete point a', 1, this.board.objectsList.length); 46 | } 47 | 48 | }); 49 | -------------------------------------------------------------------------------- /test/testControl.js: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright 2011-2012 3 | Michael Gerhaeuser, 4 | Alfred Wassermann, 5 | 6 | */ 7 | 8 | /* 9 | * Js-Test-Driver Test Suite for Generic JavaScript language tests 10 | * http://code.google.com/p/js-test-driver 11 | */ 12 | 13 | TestCase("Control", { 14 | jc: null, 15 | 16 | setUp: function () { 17 | this.jc = new JXG.JessieCode(); 18 | }, 19 | 20 | tearDown: function () { 21 | this.jc = null; 22 | }, 23 | 24 | testIf: function () { 25 | expectAsserts(3); 26 | 27 | try { 28 | this.jc.parse( 29 | 'a = 0;'+ 30 | 'b = 0;'+ 31 | 'c = 0;'+ 32 | 33 | 'if (true) {'+ 34 | ' a = 1;'+ 35 | '} else {'+ 36 | ' a = 2;'+ 37 | '}'+ 38 | 39 | 'if (false) {'+ 40 | ' b = 1;'+ 41 | '} else {'+ 42 | ' b = 2;'+ 43 | '}'+ 44 | 45 | 'if (false) {'+ 46 | ' c = 1;'+ 47 | '} else if (true) {'+ 48 | ' c = 2;'+ 49 | '} else {'+ 50 | ' c = 3;'+ 51 | '}'); 52 | } catch (e) { 53 | console.log(e); 54 | } 55 | 56 | assertEquals('if', 1, this.jc.scope.locals.a); 57 | assertEquals('else', 2, this.jc.scope.locals.b); 58 | assertEquals('else if', 2, this.jc.scope.locals.c); 59 | }, 60 | 61 | testDanglingElse: function () { 62 | expectAsserts(1); 63 | 64 | try { 65 | this.jc.parse( 66 | 'e = 1;'+ 67 | 68 | 'if (true) '+ 69 | ' if (false) '+ 70 | ' e = 0;'+ 71 | ' else' + 72 | ' e = 2;'+ 73 | 74 | ''); 75 | } catch (e) { 76 | console.log(e); 77 | } 78 | 79 | assertEquals('dangling else', 2, this.jc.scope.locals.e); 80 | }, 81 | 82 | testFor: function () { 83 | expectAsserts(1); 84 | 85 | try { 86 | this.jc.parse( 87 | 'j = 0;'+ 88 | 'for (i = 0; i < 10; i = i+1) {'+ 89 | ' j = j + 1;'+ 90 | '}'); 91 | } catch (e) { 92 | console.log(e); 93 | } 94 | 95 | assertEquals('for', 10, this.jc.scope.locals.j); 96 | }, 97 | 98 | testWhile: function () { 99 | expectAsserts(1); 100 | 101 | try { 102 | this.jc.parse( 103 | 'i = 0;'+ 104 | 'while (i < 10) {'+ 105 | ' i = i + 1;'+ 106 | '}'); 107 | } catch (e) { 108 | console.log(e); 109 | } 110 | 111 | assertEquals('while', 10, this.jc.scope.locals.i); 112 | } 113 | }); 114 | -------------------------------------------------------------------------------- /test/testGeneric.js: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright 2011-2012 3 | Michael Gerhaeuser, 4 | Alfred Wassermann, 5 | 6 | */ 7 | 8 | /* 9 | * Js-Test-Driver Test Suite for Generic JavaScript language tests 10 | * http://code.google.com/p/js-test-driver 11 | */ 12 | 13 | TestCase("Generic", { 14 | setUp: function () { 15 | abcdefg = 0 16 | }, 17 | 18 | tearDown: function () { 19 | abcdefg = 0; 20 | }, 21 | 22 | testEval: function () { 23 | expectAsserts(2); 24 | 25 | var f = (function () { 26 | var foo = 'bar'; 27 | 28 | return eval('var abcdefg = function () { return foo; }; abcdefg;'); 29 | })(); 30 | 31 | assertEquals('return value is ok', 'bar', f()); 32 | assertEquals('global variables are not introduced', 0, abcdefg); 33 | } 34 | 35 | }); 36 | --------------------------------------------------------------------------------