Skip to main content

expression examples

These pages walk through the four contexts, the types that each stand for one interpreter and hold its choices, in the expression crate of the cgp-examples repository, one context per page. expression is an arithmetic interpreter built with Context-Generic Programming (CGP), which enables pluggable trait implementations: each operator is its own type, and each operation is one provider per operator. Each page runs its context, explains how the wiring produces what it computes, and names the CGP pattern it shows.

The pages are in the order they teach, and each later page changes one thing about the one before. Start with add_mult, which introduces every piece the others reuse.

tip

New to CGP?​

These pages explain the interpreter without deriving CGP from first principles. The Hello World tutorial introduces contexts, providers, and wiring in a few minutes, and Extensible variants explains how CGP takes an enum apart for a provider. Neither is required reading; each page links what it relies on.

Running an example​

Clone the repository and run the tests from its root with cargo test -p cgp-example-expression. Two contexts have tests of their own; the other two pages give a test to add and the output it prints.

The examples​

  • add_mult — evaluation and conversion to Lisp, with one provider per operator and a context that wires each operator to its provider.
  • add_mult_binary_op — one generic provider replacing the two per-operator conversion providers.
  • add_mult_code — both operations in one component, chosen by an operation marker and the input type together.
  • add_mult_neg — the language extended with subtraction and negation, with no existing provider edited.

Where to go next​


An AI agent wrote this page using the CGP knowledge base. Its content was verified against the project's source. See How AI is used in this project.