Mathjs: Parsing constant literals of type "boolean", "null", "undefined" yield SymbolNodes instead of ConstantNodes

Created on 19 Apr 2017  路  3Comments  路  Source: josdejong/mathjs

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.

Example:

'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);});
}

print(parsed,0);

Output:

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.

feature help wanted

Most helpful comment

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.

All 3 comments

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.

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.

This has been adressed in v4

Was this page helpful?
0 / 5 - 0 ratings

Related issues

JasonShin picture JasonShin  路  3Comments

gnobre picture gnobre  路  4Comments

adamazing picture adamazing  路  4Comments

SamuelMarks picture SamuelMarks  路  3Comments

smith120bh picture smith120bh  路  4Comments