New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Parsing constant literals of type "boolean", "null", "undefined" yield SymbolNodes instead of ConstantNodes #833

balagge opened this Issue Apr 19, 2017 · 3 comments


None yet
2 participants

balagge commented Apr 19, 2017

If the input to the expression parser contains boolean, null or undefined literals, the parsed expression is built using SymbolNodes instead of ConstantNodes. However, the ConstantNode explicitly contains code that would handle these types, so I believe this is a bug.

On the other hand, string and number literals are parsed to ConstantNodes.


'use strict';

/* jshint node: true */

var math = require('mathjs');

var parsed = math.parse('a = null; b=true; c=undefined; d = "abc"; e=123');

function print (node, level) {
    console.log(" ".repeat(level),"type: ",node.type,",",node.toString());
    node.forEach(function (sub) {print(sub,level+1);});



type:  BlockNode , a = null;
b = true;
c = undefined;
d = "abc";
e = 123
  type:  AssignmentNode , a = null
   type:  SymbolNode , a
   type:  SymbolNode , null
  type:  AssignmentNode , b = true
   type:  SymbolNode , b
   type:  SymbolNode , true
  type:  AssignmentNode , c = undefined
   type:  SymbolNode , c
   type:  SymbolNode , undefined
  type:  AssignmentNode , d = "abc"
   type:  SymbolNode , d
   type:  ConstantNode , "abc"
  type:  AssignmentNode , e = 123
   type:  SymbolNode , e
   type:  ConstantNode , 123

As can be seen from the output, the first three literals create SymbolNodes, but the last two ConstantNodes.

The result is that the "symbols": "true", "false", "undefined" and "null" can be overridden from the scope when evaluating such expressions, but probably this is undesirable anyway and adds unnecessary code to the compiled expression:

(this is the compiled result of a=null):

scope["a"] = ("null" in scope ? getSafeProperty(scope, "null") : getSafeProperty(math, "null"))

Which is a lot of overhead if such literals occur frequently.

@josdejong josdejong added the feature label Apr 20, 2017


This comment has been minimized.


josdejong commented Apr 20, 2017

Thanks for bringing this up Paál. From the parsers point of view symbols like true and pi are all the same and are only evaluated at runtime, which works just fine. I agree that from a user point of view (looking at a parsed expression tree) it would be more logic to get a ConstantNode rather than a SymbolNode. Makes sense to me to change this behavior, let's schedule this for the first next breaking release (v4).

Besides true, false, null, and undefined, we should also implement NaN, Infinity, and uninitialized this way. Here, NaN and Infinity have a different value depending on whether you have configured to use numbers or bignumbers.

@josdejong josdejong referenced this issue Apr 20, 2017


Breaking changes for v4 #682

13 of 13 tasks complete

This comment has been minimized.


josdejong commented Feb 4, 2018

I've dropped constant uninitialized. This was mainly used for keeping new entries undefined when resizing matrices. Instead you can use null to indicate entries that aren't explicitly set.

@josdejong josdejong closed this in 3f3d50f Feb 25, 2018


This comment has been minimized.


josdejong commented Feb 25, 2018

This has been adressed in v4

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment