mercredi 15 avril 2020

Parsing expressions based on reflection

I need to execute functions to calculate expressions, simimlar to Excel but in PHP (OOP). (This question is probably a peanut for anyone with language parser knowledge.)

The core engine analyzes the functions and their requirements (reflection), and should run appropriate functions, and in optimal sequence in order to:

  • Resolve unknown values
  • On set new value, update any dependent values

Scenario

a = b * c
b = c + d
c = 5
d = 8
e = f * 2
f = b + 3 + a

Get any calculated value

  • Resolving a requires resolving b.
  • Resolving e requires resolving f (which in turn requires resolving a and b).

Set known value

If c is changed, functions should run to recalculate values.

  • [a = b * c] should be held back since it would be based on trashed b value.
  • [b = c + d] should run since there are no unknown dependencies.
  • Any functions dependent to b should now also run so [a = b * c] could run.
  • By updating a function [f = b + 3 + a] should now run.
  • Likewise, function e should now run.

Question

My question is about the algorithm for resolving and setting values.

  • Allthough it appears to be a subject for recursive parsing, is it more wise iterate over an array of functions and put any unresolved requirements to the end?

For example: [Update C] should not run in source order [a=..], [b=..], but [b=], [a=].

  • Should the SET logic be used already when resolving values, and if so, wouldn't that make the parsing of looking up values above redundant? Upon class initialisation, constants would be set and dependent functions would run.

Recursive exection example:

[Update C] =>
  [Update B] =>
    [Update A] =>
      [Update F] =>
        [Update E] =>

Once again, should this execution order be called recursivly or similar to above, by iterating over a flat array.

Thankful for any references or principles in the matter.





Aucun commentaire:

Enregistrer un commentaire