Skip to main content
Kuldeep Singh
Kuldeep SinghDigital Architecture
SALESFORCE · REVENUE CLOUD · LWCRole: Principal Salesforce / Solution Architect

The CML Compiler

When evaluation becomes the constraint, build the missing runtime.

Deterministic RCA / Revenue Cloud Evaluation via an embedded TypeScript language runtime.

COMPILERTYPESCRIPTLWCREACT
01 // The Constraint

What Made the Problem Difficult

Enterprise Revenue Cloud implementations rely heavily on CML evaluation for complex pricing calculations, attribute dependencies, and configuration validation. As business rules expanded across multi-product catalog structures, the standard evaluation mechanism created severe performance friction.

Core Platform Constraint

The implementation encountered a difficult round-trip evaluation issue where sending rule expressions back and forth between client interactions and platform evaluation endpoints introduced high latency, network overhead, and evaluation non-determinism across interactive quoting sessions.

Boundary Invariants & Operational Limits:
  • !Must evaluate CML rules deterministically across both native Salesforce LWC and external React runtimes
  • !Zero compromise on rule semantic fidelity between client evaluation and server validation
  • !Eliminate repeated server round-trip network hops during interactive rule and attribute selection
  • !Strict compliance with Lightning Locker / Lightning Web Security (LWS) without dynamic eval()
  • !Must package as a standalone, zero-dependency reusable TypeScript library deployable to LWC static resources and NPM
  • !Maintain strict adherence to Revenue Cloud and RCA contracts without mutating standard platform schemas
02 // The Architecture

What System Was Designed

Designed and implemented a full language compiler pipeline and deterministic runtime in TypeScript. CML source expressions are parsed into a strongly typed Abstract Syntax Tree (AST), checked against semantic validation constraints, and evaluated synchronously in-memory. The compiled runtime is packaged as a universal library that runs identically inside Salesforce LWC (via static resources) and headless React applications.

03 // The Key Decision

Pivotal Architectural Choices

Key Architectural Decisions

ADR // 01DETERMINISTIC CHOICE

Client-Side Compiler Runtime vs. Chained Remote Server Calls

Context:

Standard evaluation models force continuous round-trips to the server for expression evaluation, causing UI lag and race conditions.

Alternatives Evaluated:
  • Server-side Apex micro-evaluators
  • Headless remote evaluation API
  • Client-side regular expression parsing
Chosen: Full compiler and deterministic runtime engine written in clean TypeScript.

Moving the evaluation runtime directly into client memory space eliminates round-trip latency entirely while ensuring identical evaluation semantics across LWC and React runtimes.

ADR // 02DETERMINISTIC CHOICE

AST Interpreter vs. Dynamic Code Generation (eval / Function)

Context:

Compiling directly to JavaScript functions via new Function() offers maximum execution speed but is strictly forbidden by Salesforce Lightning Web Security (LWS).

Alternatives Evaluated:
  • Dynamic eval() code generation
  • Server-only compilation
Chosen: Secure AST tree-walking interpreter with deterministic type coercion.

Ensures 100% security compliance with Lightning Web Security while keeping execution overhead negligible for typical CML rule depths.

04 // The Engineering

What Was Actually Built

01

Custom lexical scanner and recursive-descent parser producing strongly typed AST representations of CML expressions.

02

Semantic validation pass resolving variable bindings, operand types, and Revenue Cloud domain-specific constraints.

03

Deterministic evaluation engine executing with zero dynamic code injection (no eval or Function constructor) to satisfy LWS.

04

Cross-runtime packaging producing ESM and UMD artifacts loaded in LWC via platformResourceLoader and in React via NPM.

05

Comprehensive test matrix verifying algebraic identities, boundary conditions, and exact runtime parity between Salesforce and React environments.

05 // The Trade-offs

Deliberate Architectural Compromises

Trade-offs & Mitigations

Client Bundle Size vs. Server Network Round-Trips

Architectural Benefit:

Instantaneous, zero-network evaluation of complex configuration rules in memory.

Associated Cost:

Initial download and parsing overhead of the compiler library in browser memory.

Mitigation Strategy:

Engineered a lean parser with minimal memory footprint (<35KB uncompressed) loaded asynchronously via static resources.

Strict Grammar Subsetting vs. Unbounded Macro Recursion

Architectural Benefit:

Guaranteed bounded execution time with zero risk of UI-thread infinite loops.

Associated Cost:

Language grammar intentionally disallows unbounded recursive macros.

Mitigation Strategy:

Structured declarative constraint definitions aligned with Revenue Cloud architectural best practices.

06 // The Result

Verified Outcomes

✓ VERIFIED RESULT

Completely eliminated the round-trip evaluation bottleneck during interactive quoting sessions

✓ VERIFIED RESULT

Guaranteed 100% deterministic rule parity between native Salesforce LWC and external React surfaces

✓ VERIFIED RESULT

Established a reusable, version-controlled TypeScript library shared across multiple enterprise digital touchpoints

✓ VERIFIED RESULTRequires Benchmark Confirmation

Exact benchmark latency reduction and throughput improvements: [VERIFY WITH KULDEEP]

07 // Architecture Blueprint

System Schematic & Data Flow

System Topology Blueprint
CML Compiler & Deterministic Evaluation Architecture
INTERACTIVE SCHEMATIC
STAGE 01 · INPUTCML SourceRevenue Cloud RulesDeclarative expressionsparseSTAGE 02 · TS COMPILER RUNTIME1. Lexer & AST Generation2. Semantic Rule Validation3. Deterministic Evaluation CoreESM / LWSNPM / ReusableRUNTIME TARGET 01Salesforce Native LWCLightning Web SecurityStatic Resource bundleRUNTIME TARGET 02Modern React ClientStandalone / HeadlessZero-dependency TS library
ZERO ROUND-TRIPEvaluates complex CML rule trees synchronously in client memory.
DETERMINISTIC ASTIdentical evaluation semantics guaranteed across LWC and React runtimes.
LWS COMPLIANTEngineered without eval() to strictly satisfy Lightning Web Security.

CML expressions compiled into deterministic ASTs and evaluated across Salesforce LWC and React runtimes without network round-trips.

Text alternative for screen readers: Architecture flow: CML Source to TS Lexer & Parser via Recursive-Descent AST; TS Lexer & Parser to Semantic Validator via Type & Scope Resolution; Semantic Validator to Deterministic Engine via In-Memory Evaluation Core; Deterministic Engine to LWC & React Targets via Universal TS Library Distribution

08 // Why It Matters

Architectural Conclusion

Instead of fighting the evaluation model, build the language runtime that gives the solution the determinism it needs.

Key Lessons for Platform Scale:
  • When platform evaluation boundaries become an operational constraint, owning the compilation pipeline unlocks unprecedented responsiveness and architectural portability.
  • Building clean, standards-compliant TypeScript runtimes allows Salesforce-native solutions to seamlessly bridge into broader enterprise digital ecosystems.