Skip to main content
added 3 characters in body
Source Link
Erik Eidt
  • 34.8k
  • 6
  • 61
  • 95

You need an expression evaluator, as an abstract syntax tree interpreter.

What an interpreter does is: start with some known state, e.g. variables, (blank) outputs, etc.. Then it execute statements to update that state.

To execute expression statements, you might use a recursive expression evaluator, which would do something like this:

  • for a constant node, fetch the constant value and return it as the value of the evaluation
  • for a variable node, fetch the value of the variable and return it as the value of the evaluation
  • for a binary operator, evaluate left child (hold result), then evaluate right child, then based on the operator type appropriately combine the left-result with the right-result. Then return that value as value of the evaluation.

In your system a simple value has a type of: a list of inclusions maybe together with a list of exclusions (depending on your language semantics). Each operator manipulates these lists. The NOT operator, for example, might filter the current inclusion list, and depending on your semantics, also add itselfsomething to the exclusion list.

Larger interpreters (and their associated languages) will have a notion of multiple types, such as single boolean, string, integer, list, etc... but you might get away with having just one type.

You need an expression evaluator, as an abstract syntax tree interpreter.

What an interpreter does is: start with some known state, e.g. variables, (blank) outputs, etc.. Then it execute statements to update that state.

To execute expression statements, you might use a recursive expression evaluator, which would do something like this:

  • for a constant node, fetch the constant value and return it as the value of the evaluation
  • for a variable node, fetch the value of the variable and return it as the value of the evaluation
  • for a binary operator, evaluate left child (hold result), then evaluate right child, then based on the operator type appropriately combine the left-result with the right-result. Then return that value as value of the evaluation.

In your system a simple value has a type of: a list of inclusions maybe together with a list of exclusions (depending on your language semantics). Each operator manipulates these lists. The NOT operator, for example, might filter the current inclusion list, and depending on your semantics, also add itself to the exclusion list.

Larger interpreters (and their associated languages) will have a notion of multiple types, such as single boolean, string, integer, list, etc... but you might get away with having just one type.

You need an expression evaluator, as an abstract syntax tree interpreter.

What an interpreter does is: start with some known state, e.g. variables, (blank) outputs, etc.. Then it execute statements to update that state.

To execute expression statements, you might use a recursive expression evaluator, which would do something like this:

  • for a constant node, fetch the constant value and return it as the value of the evaluation
  • for a variable node, fetch the value of the variable and return it as the value of the evaluation
  • for a binary operator, evaluate left child (hold result), then evaluate right child, then based on the operator type appropriately combine the left-result with the right-result. Then return that value as value of the evaluation.

In your system a simple value has a type of: a list of inclusions maybe together with a list of exclusions (depending on your language semantics). Each operator manipulates these lists. The NOT operator, for example, might filter the current inclusion list, and depending on your semantics, also add something to the exclusion list.

Larger interpreters (and their associated languages) will have a notion of multiple types, such as single boolean, string, integer, list, etc... but you might get away with having just one type.

Source Link
Erik Eidt
  • 34.8k
  • 6
  • 61
  • 95

You need an expression evaluator, as an abstract syntax tree interpreter.

What an interpreter does is: start with some known state, e.g. variables, (blank) outputs, etc.. Then it execute statements to update that state.

To execute expression statements, you might use a recursive expression evaluator, which would do something like this:

  • for a constant node, fetch the constant value and return it as the value of the evaluation
  • for a variable node, fetch the value of the variable and return it as the value of the evaluation
  • for a binary operator, evaluate left child (hold result), then evaluate right child, then based on the operator type appropriately combine the left-result with the right-result. Then return that value as value of the evaluation.

In your system a simple value has a type of: a list of inclusions maybe together with a list of exclusions (depending on your language semantics). Each operator manipulates these lists. The NOT operator, for example, might filter the current inclusion list, and depending on your semantics, also add itself to the exclusion list.

Larger interpreters (and their associated languages) will have a notion of multiple types, such as single boolean, string, integer, list, etc... but you might get away with having just one type.