I want to be able to write a J-like language using the mathjs math.parser()
function result.
Let's say I want to define an operator #
that returns the length of an array. Ideally it would work like this:
a = [1, 2, 3]
#a // yields 3
Then, for fun, an operator $
that takes two arrays and combines them.
a = [1, 2, 3, 4]
b = [4, 5, 6]
a $ b // yields [1, 2, 3, 4, 4, 5, 6]
How might I do these things with mathjs? If I cannot do them, what tool might I use instead?
I want to be able to use it like this:
var parser = math.parser();
parser.eval("a = [1,2,3]; #a");
The expression parser of math.js doesn't support adding custom operators, so you will have to clone the project and adjust the code of the parser for that:
https://github.com/josdejong/mathjs/blob/master/src/expression/parse.js
Note that right now the #
character is used as start of a comment in the parser.
If you want to create your own parser for your own specific syntax, you could do so using a parser generator like jison or peg.