# Build Expression Tree

> For symbolic computation: ASTs, mathematical expressions, code that manipulates code structure, expression transformations.

- **Type:** Skill
- **Install:** `agentstack add skill-jimmc414-claude-code-plugin-marketplace-build-expression-tree`
- **Verified:** Pending review
- **Seller:** [jimmc414](https://agentstack.voostack.com/s/jimmc414)
- **Installs:** 0
- **Category:** [AI & ML](https://agentstack.voostack.com/c/ai-and-ml)
- **Latest version:** 0.1.0
- **License:** MIT
- **Upstream author:** [jimmc414](https://github.com/jimmc414)
- **Source:** https://github.com/jimmc414/claude-code-plugin-marketplace/tree/master/plugins/norvig-patterns/skills/build-expression-tree

## Install

```sh
agentstack add skill-jimmc414-claude-code-plugin-marketplace-build-expression-tree
```

Requires the [AgentStack CLI](https://agentstack.voostack.com/docs/cli). Works with Claude Code, Cursor, and any MCP-compatible agent.

## About

# build-expression-tree

## When to Use
- Symbolic math (differentiation, simplification)
- Building ASTs for interpreters
- Query builders (SQL, API)
- Code generation
- Expression pattern matching

## When NOT to Use
- Just need to evaluate once (use direct computation)
- No transformation needed
- Structure too complex (use existing parser)

## The Pattern

Represent expressions as nested data structures (tuples, classes, or trees).

```python
# Tuple representation
expr = ('+', ('*', 'x', 2), 1)  # (x * 2) + 1

# Class representation
class Expr:
    def __init__(self, op, *args):
        self.op, self.args = op, args

    def __add__(self, other):
        return Expr('+', self, other)

    def __mul__(self, other):
        return Expr('*', self, other)

x = Expr('x')
expr = x * 2 + 1  # Builds expression tree

# Recursive evaluation
def evaluate(expr, env):
    if isinstance(expr, str):
        return env[expr]  # Variable lookup
    if isinstance(expr, (int, float)):
        return expr
    op, *args = expr if isinstance(expr, tuple) else (expr.op, *expr.args)
    values = [evaluate(a, env) for a in args]
    return {'+': lambda a,b: a+b, '*': lambda a,b: a*b}[op](*values)
```

## Example (from pytudes Differentiation.ipynb)

```python
class Expression:
    """A symbolic mathematical expression."""
    def __init__(self, op, *args):
        self.op, self.args = op, args

    def __add__(self, other):  return Expression('+', self, other)
    def __radd__(self, other): return Expression('+', other, self)
    def __mul__(self, other):  return Expression('*', self, other)
    def __rmul__(self, other): return Expression('*', other, self)
    def __neg__(self):         return Expression('-', self)

    def __repr__(self):
        if len(self.args) == 1:
            return f"({self.op}{self.args[0]})"
        return f"({self.args[0]} {self.op} {self.args[1]})"

class Function(Expression):
    """A function like sin or cos."""
    def __call__(self, x):
        return Expression(self, x)

# Create symbols and functions
x = Expression('x')
sin, cos = Function('sin'), Function('cos')

# Build expressions naturally
expr = sin(x) + cos(x) * 2
# Expression tree: (+ (sin x) (* (cos x) 2))

# Symbolic differentiation
def D(y, x=x):
    """Differentiate y with respect to x."""
    if y == x: return 1
    if not isinstance(y, Expression): return 0
    op, args = y.op, y.args
    if op == '+': return D(args[0], x) + D(args[1], x)
    if op == '*': return D(args[0], x) * args[1] + args[0] * D(args[1], x)
    if op == sin: return cos(args[0]) * D(args[0], x)
    # ... more rules
```

## Key Principles
1. **Operator overloading**: Natural syntax for building trees
2. **radd/rmul for commutativity**: Handle `2 + x` not just `x + 2`
3. **Recursive processing**: Walk tree to evaluate/transform
4. **Pattern matching on op**: Different behavior per operation
5. **Simplification rules**: Reduce `0 + x` to `x`, etc.

## Source & license

This open-source skill is cataloged on AgentStack and links to its original source — we do not rehost the code.

- **Author:** [jimmc414](https://github.com/jimmc414)
- **Source:** [jimmc414/claude-code-plugin-marketplace](https://github.com/jimmc414/claude-code-plugin-marketplace)
- **License:** MIT

Install and usage instructions live in the source repository linked above.

## Pricing

- **Free** — Free

## Security capabilities

Automated source analysis of v0.1.0 — what this tool can access:

- **Network access:** no
- **Filesystem access:** no
- **Shell / process execution:** no
- **Environment & secrets:** no
- **Dynamic code execution:** yes

*"Yes" means the capability is present in the source — more access means more to trust, not that it is unsafe.*


## Versions

- **0.1.0** — security scan: flagged — Imported from the upstream source.

## Links

- Listing page: https://agentstack.voostack.com/l/skill-jimmc414-claude-code-plugin-marketplace-build-expression-tree
- Seller: https://agentstack.voostack.com/s/jimmc414
- Browse the marketplace: https://agentstack.voostack.com/browse

---
Listed on AgentStack — the marketplace for AI agent skills and MCP servers. Every listing is security-reviewed. Creators keep 70%.
