Ability to get return type(s) of parsed expression #3132
TheOneTheOnlyJJ
started this conversation in
Ideas
Replies: 1 comment 10 replies
-
Good to hear that mathjs if of help to you. Determining the output type of an arbitrary expression at compile time requires a typed compiler. The expression parser of mathjs doesn't do types though. Maybe you can require the user to provide the type of the output of an expression manually? |
Beta Was this translation helpful? Give feedback.
10 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
I've come across math.js a few days ago while researching options to parse and evaluate mathematical user input safely. The library is everything I've hoped for and much more, except one feature.
My use case allows users to define custom JSON schemas for arbitrary "objects" (as in physical items) to be used as database collection schemas. I want to allow for computed fields to be defined with arbitrary user-provided mathematical expressions, using the other defined fields as the expressions' scope. For example, given a user-defined "ball" object type that has the required field
radius
, I want the user to be able to add a fieldvolume
, that is dynamically computed given the object'sradius
when a new object of that type gets added to the database.The problem I have, however, is determining the return type of the users' expression input. I need to be able to infer the return type of the mathematical expression's possible results before committing the schema to the database. For example, continuing with the example above, I want to limit the
volume
field to a number. I must not allow expressions that return booleans, matrices, objects, strings, or function declarations in this particular case.I could implement a simple type check for the expression result at object addition time, which would not be ideal, as the user would not be notified of the type mismatch while defining the object schema, leading to a very poor UX.
Thus, this requires me to implement the type check at field (expression) declaration time. Besides having no concrete idea how to approach this problem on my own as of now, I have identified a few potential problems here:
All I can do until now is check the SymbolNodes so that no undefined field names are used in the expression.
Adding to this, I will also have to check for conflicts between user-defined field names and predefined math.js constants/functions. Having a function that returns a list of all the reserved names of predefined constants, variables and functions in the library parser would be of great help, allowing me to restrict the user-defined field names, thus avoiding such conflicts.
I welcome any kind of input, suggestions, and alternatives on this topic.
Beta Was this translation helpful? Give feedback.
All reactions