Skip to content

Implement operator registry system with configurable runtime and codegen classes #4

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

Copilot
Copy link

@Copilot Copilot AI commented Jul 27, 2025

This PR implements a flexible operator registry system that allows users to create custom operator sets for different use cases. The new system provides:

New Classes

OperatorRegistry

A class to manage collections of operators with methods to:

  • Add/remove operators dynamically
  • Check operator existence (including aliases)
  • Clone and merge registries
  • Convert to efficient lookup maps

ExpressionRuntime

A runtime class that evaluates JSON expressions using a specified operator registry:

const runtime = new ExpressionRuntime(customRegistry);
const result = runtime.evaluate(['+', 1, 2], {vars: new Vars(null)});

ExpressionCodegen

A code generation class that compiles expressions to JavaScript using custom operator sets:

const codegen = new ExpressionCodegen(registry, {expression: ['+', 1, 2]});
const compiledFn = codegen.run().compile();

Built-in Registries

Default Registry

Contains the most commonly used operators for typical JSON expression needs:

  • Arithmetic (+, -, *, /, etc.)
  • Comparison (==, >, <, etc.)
  • Logical (and, or, not)
  • Type checking (type, bool?, str?, etc.)
  • Container operations (len, member)
  • String operations (cat, contains, starts)
  • Array operations (concat, filter, map)
  • Object operations (keys, values, entries)
  • Control flow (if, throw)
  • Variable access (get, $)

Extended Registry

Includes everything from the default registry plus advanced operators:

  • Binary operations (u8, i8, f32, etc.)
  • Bitwise operations (&, |, ^, ~)
  • JSON Patch operations (jp.add)

Usage Examples

import { 
  defaultOperatorRegistry, 
  extendedOperatorRegistry,
  OperatorRegistry,
  ExpressionRuntime 
} from '@jsonjoy.com/json-expression';

// Use default operators for common cases
const runtime = new ExpressionRuntime(defaultOperatorRegistry);
const result = runtime.evaluate(['+', 1, 2], {vars: new Vars(null)}); // 3

// Use extended registry for advanced operations  
const extendedRuntime = new ExpressionRuntime(extendedOperatorRegistry);
const bitwiseResult = extendedRuntime.evaluate(['&', 5, 3], {vars: new Vars(null)}); // 1

// Create custom registries
const customRegistry = new OperatorRegistry()
  .add(...arithmeticOperators)
  .add(...comparisonOperators);

Backward Compatibility

All existing APIs remain unchanged. The original evaluate function and JsonExpressionCodegen class continue to work exactly as before, using the full operator set by default.

Fixes #3.


💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.

Co-authored-by: streamich <9773803+streamich@users.noreply.github.com>
@Copilot Copilot AI changed the title [WIP] Operator registry Implement operator registry system with configurable runtime and codegen classes Jul 27, 2025
@Copilot Copilot AI requested a review from streamich July 27, 2025 16:11
Copilot finished work on behalf of streamich July 27, 2025 16:11
@streamich streamich marked this pull request as ready for review July 27, 2025 19:44
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Operator registry
2 participants