EverSharp Language Reference for AI Agents¶
Version: Current
Date: November 27, 2025
Purpose: Comprehensive reference for AI agents to understand, diagnose, and work with EverSharp code
Table of Contents¶
- Language Overview
- Lexical Structure
- Grammar Specification
- Type System
- Operators
- Statements
- Expressions
- Native Functions Reference
- Error Patterns
- Integration Patterns
1. Language Overview¶
1.1 What is EverSharp?¶
EverSharp is a dynamically-typed, interpreted meta-programming language designed for business rules and calculations in insurance quoting systems. It is implemented in C# and runs on .NET 9.0.
Key Characteristics:
- Dynamic typing: Variables have no declared types; types are determined at runtime
- Expression-oriented: Almost everything returns a value
- C-family syntax: Similar to JavaScript/C# in appearance
- Functional features: First-class functions, lambda expressions, array methods
- Object-oriented: Object literals and property access
- Imperative control flow: if/else, while loops
- No classes or inheritance: Uses object literals and functions only
- Lexically scoped: Block-level and function-level scoping
1.2 Architecture¶
Execution Pipeline:
Components:
- RuleTokenizer: Lexical analysis using regex patterns
- Parser: Recursive descent parser producing Abstract Syntax Tree (AST)
- Interpreter: Tree-walking interpreter with visitor pattern
- Environment: Lexically-scoped variable storage
- Instance/InstanceArray: Runtime representations of objects and arrays
1.3 Design Principles¶
- No implicit type coercion: Operations between incompatible types throw errors
- Explicit over implicit: No automatic conversions
- Fail fast: Invalid operations throw runtime exceptions immediately
- Lexical scoping: Variables resolved at parse time
- Immutability preference: Many operations return new values rather than mutating
2. Lexical Structure¶
2.1 Tokens¶
EverSharp has 55 token types defined in TokenType.cs:
Operators:
- Logical:
||(OR),&&(AND),!(NEGATION) - Comparison:
==(EQUAL),!=(NOT_EQUAL),<(LESS),>(GREATER),<=(LESS_OR_EQUAL),>=(GREATER_OR_EQUAL) - Arithmetic:
+(ADD),-(SUBTRACT),*(MULTIPLY),/(DIVIDE),%(MODULO) - Assignment:
=(ASSIGN),+=(ADD_ASSIGN),-=(SUBTRACT_ASSIGN),*=(MULTIPLY_ASSIGN),/=(DIVIDE_ASSIGN),%=(MODULO_ASSIGN) - Postfix:
++(PLUS_PLUS),--(MINUS_MINUS)
Delimiters:
- Parentheses:
((LEFT_PARENTHESIS),)(RIGHT_PARENTHESIS) - Brackets:
[(LEFT_BRACKET),](RIGHT_BRACKET) - Braces:
{(LEFT_CURLY_BRACKET),}(RIGHT_CURLY_BRACKET) - Others:
;(SEMICOLON),,(COMMA),.(DOT),:(COLON),?(QUESTION_MARK)
Keywords:
- Control flow:
if,else,while,return,exit,throw - Declaration:
function - Literals:
true,false,null
Literals and Identifiers:
NUMBER: Decimal or integer literalSTRING: Double-quoted string with escape sequencesCHAR: Single-quoted character (rarely used)IDENTIFIER: Variable, function, or property name
Special:
LAMBDA:=>arrow for lambda expressionsWHITESPACE: Space, tab, newline (ignored)COMMENT://single-line or/* */multi-lineEOF: End of file marker
2.2 Token Patterns (Regex)¶
Numbers:
Matches: 123, 3.14, .5, 0.001
Strings:
Matches: "hello", "line\nbreak", "quote\"inside"
Identifiers:
Matches: variableName, $NativeFunction, _private, camelCase123
Comments:
Matches: // single line, /* multi line */
2.3 Whitespace and Comments¶
Whitespace: Spaces, tabs, and newlines are ignored except as token separators
Single-line comments:
Multi-line comments:
/*
* Calculate premium with adjustments
* for age and coverage amount
*/
premium = basePremium * ageFactor;
2.4 Keywords¶
Reserved keywords that cannot be used as identifiers:
if,else,while,function,returntrue,false,null
Note: Unlike JavaScript, EverSharp does NOT reserve: var, let, const, for, switch, case, class, new, etc.
2.5 Identifiers¶
Rules:
- Must start with letter (a-z, A-Z), underscore (_), or dollar sign ($)
- Can contain letters, digits, underscores, dollar signs
- Case-sensitive:
myVarâ‰MyVar - Dollar sign prefix ($) by convention indicates native function
Valid identifiers:
Invalid identifiers:
123variable // Cannot start with digit
my-variable // Hyphen not allowed
my variable // Space not allowed
if // Reserved keyword
3. Grammar Specification¶
3.1 Complete BNF Grammar¶
program → declaration* EOF ;
declaration → function_declaration | statement ;
function_declaration → "function" function ;
function → identifier "(" parameters* ")" block ;
parameters → identifier ( "," identifier )* ;
statement → expression_statement | if_statement | return_statement
| exit_statement | throw_statement | while_statement | block ;
expression_statement → expression ';' ;
if_statement → "if" "(" expression ")" statement ( "else" statement )? ;
return_statement → "return" expression? ";" ;
exit_statement → "exit" ";" ;
throw_statement → "throw" expression? ";" ;
while_statement → "while" "(" expression ")" statement ;
block → "{" declaration* "}" ;
expression → assignment ;
assignment → (call ".") identifier ( "=" | "+=" | "-=" | "*=" | "/=" | "%=" )
argument | logical_or ;
logical_or → logical_and ( "||" logical_and )* ;
logical_and → equality ( "&&" equality )* ;
equality → comparison ( ( "!=" | "==" ) comparison )* ;
comparison → term ( ( ">" | ">=" | "<" | "<=" ) term )* ;
term → factor ( ( "-" | "+" ) factor )* ;
factor → unary ( ( "/" | "*" | "%" ) unary )* ;
unary → ( "!" | "-" ) unary | postfix ;
postfix → primary ( "++" | "--" )? ;
call → "{" properties? "}"
| "[" expression ( "," expression )* "]"
| primary ( "(" arguments? ")" | "." identifier | "[" expression "]" )* ;
properties → property_initialization ( "," property_initialization )* ;
property_initialization → string ":" expression ;
arguments → argument ( "," argument )* ;
argument → lambda | expression ;
lambda → ( identifier | "(" parameters* ")" ) "=>" lambda_body ;
lambda_body → expression | block ;
primary → identifier | number | string | "true" | "false" | "null"
| "(" expression ")" ;
3.2 Grammar Notes¶
Precedence (highest to lowest):
- Primary expressions (literals, identifiers, grouping)
- Postfix (
++,--) - Unary (
!,-) - Factor (
*,/,%) - Term (
+,-) - Comparison (
<,>,<=,>=) - Equality (
==,!=) - Logical AND (
&&) - Logical OR (
||) - Assignment (
=,+=, etc.)
Associativity:
- Most binary operators: Left-to-right
- Assignment operators: Right-to-left
- Unary operators: Right-to-left
Statement vs Expression:
- Statements do not return values (if, while, function declarations)
- Expressions always return values (can be used in assignments, arguments, etc.)
- Exception:
returnstatement can include an expression
4. Type System¶
4.1 Overview¶
EverSharp is dynamically typed: variables do not have declared types, and types are checked at runtime.
Core Types:
decimal- All numeric valuesstring- Text valuesboolean-trueorfalsenull- Absence of valueDateTime- Date and time valuesDateOnly- Date-only values (no time component)Instance- Object/dictionary with propertiesInstanceArray- Array of values
4.2 Decimal (Numbers)¶
Internal representation: C# decimal type (128-bit)
Literals:
123; // Integer
3.14; // Decimal
0.5; // Leading zero optional
0.5; // Leading zero omitted
0 - // Zero
42; // Negative (unary minus)
1000000.99; // Large numbers
No support for:
- Scientific notation (1e6)
- Hexadecimal (0xFF)
- Binary (0b1010)
- Underscores (1_000_000)
Operations:
a + b; // Addition
a - b; // Subtraction
a * b; // Multiplication
a / b; // Division
(a % b) - // Modulo (remainder)
a; // Negation
a++; // Post-increment (returns old value, increments variable)
a--; // Post-decrement (returns old value, decrements variable)
Type checking:
// All numbers are decimal - no integer vs float distinction
value = 10; // 10.0 internally
value = 10 / 3; // 3.3333... (decimal precision)
4.3 String¶
Literals: Double-quoted only
Escape sequences:
No string interpolation: Must use concatenation or $Log formatting
name = "John";
message = "Hello, " + name; // Concatenation
$Log("Hello, {0}", name); // Format in $Log
Operations:
a + b; // Concatenation (if at least one is string)
a == b; // Equality comparison
a != b; // Inequality comparison
Instance methods (called with dot notation):
str.ToUpper();
str.ToLower();
str.Trim();
str.Split(separator);
str.StartsWith(prefix);
str.EndsWith(suffix);
str.Contains(substring);
str.ToString();
str.ToNumber(); // Convert to decimal
4.4 Boolean¶
Literals: true, false (lowercase only)
Operations:
a && b; // Logical AND (short-circuit)
a || b; // Logical OR (short-circuit)
!a; // Logical NOT
a == b; // Equality
a != b; // Inequality
Truthiness: EverSharp has strict boolean semantics
- Only
trueandfalseare boolean values - No implicit conversion from other types
- Comparison operators return boolean
- Logical operators require boolean operands
Common patterns:
if (age > 18) {
} // Comparison returns boolean
if (isActive && hasLicense) {
} // Both must be boolean
if (!isExpired) {
} // Negation of boolean
4.5 Null¶
Literal: null (lowercase only)
Purpose: Represents absence of value or uninitialized variable
Default value: Variables auto-initialize to null if not assigned
Null operations:
Null behavior:
- Cannot call methods on null (throws RuntimeException)
- Cannot access properties on null (throws RuntimeException)
- Can be compared with
==and!= - Can be passed as argument
- Can be returned from functions
4.6 DateTime¶
Creation: Via native functions or string parsing
now = $Now(); // Current UTC time
today = $Today(); // Today at midnight
parsed = $ToDate("2023/12/25"); // Parse from string
utc = $ToDateUTC("2023/12/25 10:30"); // Parse as UTC
Operations: Via native functions
$Year(date);
$Month(date);
$Day(date);
$AddDays(date, days);
$AddMonths(date, months);
$AddYears(date, years);
$AddHours(date, hours);
$AddMinutes(date, minutes);
$AddSeconds(date, seconds);
$DifferenceInDays(date1, date2);
$DifferenceInYears(date1, date2);
// ... more functions in section 8
String representation: ISO 8601 format when converted to string
4.7 DateOnly¶
Creation: Via native function
Purpose: Represents date without time component
Operations: Limited compared to DateTime (no time arithmetic)
4.8 Instance (Objects)¶
Creation: Object literal syntax
Property access:
person.name; // Get property
person.age = 31; // Set property
person.newProp = 5; // Add new property
Nested objects:
policy = {
number: "POL-123",
customer: {
name: "Jane",
address: {
city: "Boston",
},
},
};
city = policy.customer.address.city; // Chained access
Instance methods:
Native functions:
4.9 InstanceArray (Arrays)¶
Creation: Array literal or $Array function
numbers = [1, 2, 3, 4, 5];
mixed = ["hello", 42, true, null];
empty = [];
nested = [
[1, 2],
[3, 4],
];
// Or using $Array function
numbers = $Array(1, 2, 3, 4, 5);
Indexing: Zero-based
first = numbers[0]; // Get first element
numbers[2] = 99; // Set third element
last = numbers[numbers.Length - 1]; // Get last element
Property access:
Instance methods (extensive - see section 8.4 for details):
array.Filter(predicate);
array.Map(transform);
array.ReduceToNum(fn, initial);
array.OrderBy(selector);
array.Reverse();
array.Find(predicate);
array.Any(predicate);
array.Push(item);
array.Slice(start, end);
// ... many more
Arrays of objects:
people = [
{ name: "John", age: 30 },
{ name: "Jane", age: 25 },
];
adults = people.Filter((p) => p.age >= 18);
names = people.Map((p) => p.name);
4.10 Type Coercion Rules¶
No implicit coercion: EverSharp does NOT automatically convert types
String concatenation: Special case for + operator
"Age: " + 30; // "Age: 30" (number converts to string)
30 + " years"; // "30 years" (number converts to string)
All other operations: Require matching types
5 + "10"; // "510" (string concat, not addition)
5 * "10"; // ERROR: Cannot multiply number and string
"5" == 5; // false (different types)
true + 1; // ERROR: Cannot add boolean and number
Explicit conversion: Use conversion functions
"123".ToNumber(); // 123 (string to decimal)
$ToNumber("123"); // 123 (string to decimal)
$ToString(123); // "123" (any value to string)
5. Operators¶
5.1 Operator Precedence Table¶
| Precedence | Operator | Description | Associativity |
| ----------- | ---------------------------- | ------------------------------------- | ------------- | ---------- | ------------- |
| 1 (highest) | () [] . | Grouping, array access, member access | Left-to-right |
| 2 | ++ -- | Postfix increment/decrement | N/A |
| 3 | ! - | Logical NOT, unary minus | Right-to-left |
| 4 | * / % | Multiplication, division, modulo | Left-to-right |
| 5 | + - | Addition, subtraction | Left-to-right |
| 6 | < > <= >= | Comparison | Left-to-right |
| 7 | == != | Equality | Left-to-right |
| 8 | && | Logical AND | Left-to-right |
| 9 | | | | Logical OR | Left-to-right |
| 10 | ?: | Ternary conditional | Right-to-left |
| 11 (lowest) | = += -= *= /= %= | Assignment | Right-to-left |
5.2 Arithmetic Operators¶
Binary arithmetic:
a + b; // Addition (also string concatenation)
a - b; // Subtraction
a * b; // Multiplication
a / b; // Division
a % b; // Modulo (remainder)
Unary arithmetic:
Type requirements: Both operands must be decimal (except + with strings)
Examples:
premium = 1000 + 250; // 1250
discount = premium * 0.15; // 187.5
netPremium = premium - discount; // 1062.5
taxAmount = netPremium * 0.08; // 85
remainder = 17 % 5; // 2
5.3 Comparison Operators¶
Operators:
a < b; // Less than
a > b; // Greater than
a <= b; // Less than or equal
a >= b; // Greater than or equal
a == b; // Equal
a != b; // Not equal
Return type: Always boolean
Type requirements: Operands must be same type (decimal, string, boolean, DateTime, null)
Examples:
age > 18; // true if age is 19
premium <= 1000; // true if premium is 1000 or less
name == "John"; // true if name exactly matches
status != null; // true if status is not null
startDate < endDate; // Compare dates
"apple" < "banana"; // String comparison (lexicographic)
5.4 Logical Operators¶
Binary logical:
Unary logical:
Type requirements: Operands must be boolean
Short-circuit evaluation:
&&: If left side isfalse, right side is NOT evaluated||: If left side istrue, right side is NOT evaluated
Examples:
isAdult = age >= 18 && age <= 65;
isEligible = hasLicense || hasPermit;
isInvalid = !isValid;
// Short-circuit prevents error
if (policy != null && policy.active) {
// policy.active only accessed if policy is not null
}
5.5 Assignment Operators¶
Simple assignment:
Compound assignment:
x += 5; // Equivalent to: x = x + 5
x -= 3; // Equivalent to: x = x - 3
x *= 2; // Equivalent to: x = x * 2
x /= 4; // Equivalent to: x = x / 4
x %= 3; // Equivalent to: x = x % 3
Return value: Assignment expressions return the assigned value
Property assignment:
5.6 Postfix Operators¶
Operators:
x++; // Post-increment: returns x, then increments
x--; // Post-decrement: returns x, then decrements
Implementation: These are syntactic sugar for compound assignment
x++; // Equivalent to: temp = x; x += 1; return temp;
x--; // Equivalent to: temp = x; x -= 1; return temp;
Type requirement: Variable must be decimal
Examples:
counter = 5;
oldValue = counter++; // oldValue = 5, counter = 6
newValue = counter--; // newValue = 6, counter = 5
Note: No prefix versions (++x or --x)
5.7 Ternary Operator¶
Syntax: condition ? trueExpression : falseExpression
Evaluation:
- Evaluate
condition(must be boolean) - If
true, evaluate and returntrueExpression - If
false, evaluate and returnfalseExpression
Examples:
premium = age > 65 ? basePremium * 1.5 : basePremium;
status = isActive ? "Active" : "Inactive";
max = a > b ? a : b; // Simple max function
// Nested ternary (discouraged for readability)
rate = age < 25 ? 0.15 : age < 65 ? 0.1 : 0.2;
5.8 Member Access Operator¶
Dot notation: object.property
Usage:
// Property access
person.name;
policy.customer.address.city;
// Property assignment
person.age = 31;
// Method calls
array.Filter(predicate);
string.ToUpper();
5.9 Array Access Operator¶
Bracket notation: array[index]
Zero-based indexing:
numbers = [10, 20, 30];
first = numbers[0]; // 10
second = numbers[1]; // 20
numbers[2] = 99; // Set third element
Accessing arrays of objects:
6. Statements¶
6.1 Expression Statements¶
Syntax: expression ;
Any expression followed by semicolon becomes a statement:
6.2 If Statement¶
Syntax:
Condition: Must evaluate to boolean
Examples:
// Simple if
if (age >= 18) {
premium = basePremium;
}
// If-else
if (age < 25) {
premium = basePremium * 1.5;
} else {
premium = basePremium;
}
// If-else-if chain
if (age < 25) {
premium = basePremium * 1.5;
} else if (age < 65) {
premium = basePremium;
} else {
premium = basePremium * 1.25;
}
// Single-line (no braces)
if (isActive) status = "Active";
Block statements: Use {} for multiple statements
if (premium > 5000) {
discount = premium * 0.1;
premium = premium - discount;
$Log("Discount applied");
}
6.3 While Statement¶
Syntax: while (condition) statement
Condition: Must evaluate to boolean
Examples:
// Simple loop
counter = 0;
while (counter < 10) {
$Log(counter);
counter++;
}
// Loop through array (manual indexing)
index = 0;
while (index < items.Length) {
$Log(items[index]);
index++;
}
// Infinite loop (must have break mechanism)
while (true) {
if (condition) {
// Need external mechanism to exit
}
}
Note: No break or continue statements in EverSharp
Alternative: Use array methods instead of loops
6.4 Function Declaration¶
Syntax:
Examples:
// Simple function
function calculatePremium(baseAmount, rate) {
return baseAmount * rate;
}
// Function with multiple statements
function processPolicy(policy) {
premium = policy.baseAmount;
discount = calculateDiscount(policy);
premium = premium - discount;
return premium;
}
// Function with no parameters
function getCurrentTimestamp() {
return $Now();
}
// Function with no return
function logMessage(message) {
$Log(message);
}
Function calls:
Scope: Functions create new scope; can access outer variables
6.5 Return Statement¶
Syntax:
Examples:
function getMax(a, b) {
if (a > b) {
return a;
}
return b;
}
function checkEligibility(age) {
if (age < 18) {
return false;
}
if (age > 80) {
return false;
}
return true;
}
function earlyExit(condition) {
if (condition) {
return; // Return null
}
// Continue processing
return result;
}
Note: Return immediately exits the function
6.6 Exit Statement¶
Syntax:
Purpose: Immediately terminates entire script execution
Examples:
// Terminate on critical error
if (premium > maxAllowed) {
$Log("Premium exceeds limit");
exit;
}
// Early termination
counter = 0;
while (counter < 100) {
if (shouldStop) {
exit; // Stops entire script
}
counter++;
}
Note: Exit terminates normally without error
6.7 Throw Statement¶
Syntax:
Purpose: Immediately terminates script execution with an error
Examples:
// Basic throw with message
if (age < 0) {
throw "Age cannot be negative";
}
// Throw with dynamic message
if (premium > maxPremium) {
throw "Premium exceeds maximum: " + premium;
}
// Throw with expression
value = -10;
throw value < 0 ? "Negative value" : "Invalid value";
// Throw without message
if (criticalError) {
throw; // Throws "Runtime error"
}
Error Detection in C#:
var runner = new EverSharpRunner();
runner.Run(@"
age = -5;
throw ""Invalid age: "" + age;
");
if (runner.HasErrors) {
foreach (var error in runner.Errors) {
Console.WriteLine($"Error: {error}");
// Output: Error: Invalid age: -5
}
}
Comparison:
| Statement | Scope | Error | Value |
|---|---|---|---|
return |
Exits current function | No | Can return |
exit |
Exits entire script | No | None |
throw |
Exits entire script | Yes | Error message |
When to use:
return: Normal function exit with optional valueexit: Graceful script terminationthrow: Error conditions that need to be reported
6.8 Block Statement¶
Syntax: { declaration* }
Purpose: Group multiple statements into single statement
Creates new scope:
Common usage:
// With if
if (condition) {
statement1;
statement2;
}
// With while
while (condition) {
statement1;
statement2;
}
// With function
function name() {
statement1;
statement2;
}
// Standalone (rarely used)
{
tempValue = compute();
process(tempValue);
}
7. Expressions¶
7.1 Literal Expressions¶
Number literals:
String literals:
Boolean literals:
Null literal:
7.2 Variable Expressions¶
Declaration and initialization: No keyword required
Auto-initialization: Undefined variables are null
Usage:
7.3 Object Initialization¶
Syntax: { "key": value, "key2": value2, ... }
Examples:
// Simple object
person = {
name: "John",
age: 30,
};
// Nested objects
policy = {
number: "POL-123",
customer: {
name: "Jane",
email: "jane@example.com",
},
premium: 1500.0,
};
// Empty object
empty = {};
// Objects with expressions
calculation = {
base: 1000,
rate: 0.15,
total: 1000 * 0.15,
};
Key requirements:
- Keys must be strings (quoted)
- Values can be any expression
- Trailing comma not allowed
7.4 Array Initialization¶
Array literal syntax: [ expression, expression, ... ]
Examples:
// Numbers
numbers = [1, 2, 3, 4, 5];
// Strings
names = ["Alice", "Bob", "Charlie"];
// Mixed types
mixed = [42, "hello", true, null];
// Empty array
empty = [];
// Nested arrays
matrix = [
[1, 2, 3],
[4, 5, 6],
[7, 8, 9],
];
// Array of objects
people = [
{ name: "John", age: 30 },
{ name: "Jane", age: 25 },
];
// With expressions
computed = [1 + 1, 2 * 3, $Max(5, 10)];
Function syntax: $Array(item1, item2, ...)
7.5 Function Call Expressions¶
User-defined functions:
Native functions (prefixed with $):
Method calls (on objects/arrays/strings):
7.6 Lambda Expressions¶
Syntax:
// Single parameter (no parentheses)
x => expression
// Multiple parameters
(x, y) => expression
// No parameters
() => expression
// Block body
x => {
statement1;
statement2;
return value;
}
// Multi-parameter block
(x, y) => {
result = x + y;
return result;
}
Examples:
// Simple transform
doubled = numbers.Map((x) => x * 2);
// Filter
adults = people.Filter((p) => p.age >= 18);
// Multi-parameter
summed = numbers.ReduceToNum((acc, x) => acc + x, 0);
// With index
indexed = items.Map((item, index) => {
return {
index: index,
value: item,
};
});
// Assigned to variable
addTen = (x) => x + 10;
result = addTen(5); // 15
// Passed as argument
applyFunction = (fn, value) => fn(value);
result = applyFunction((x) => x * 2, 10); // 20
Closure: Lambdas capture variables from outer scope
7.7 Property Access Expressions¶
Dot notation: object.property
Examples:
// Simple access
name = person.name;
age = person.age;
// Nested access
city = policy.customer.address.city;
// Assignment
person.age = 31;
policy.premium = 2000;
// Chained access
finalValue = obj.nested.deep.property;
7.8 Array Access Expressions¶
Bracket notation: array[index]
Examples:
// Read
first = numbers[0];
last = numbers[numbers.Length - 1];
// Write
numbers[2] = 99;
// With variables
index = 3;
value = items[index];
// Array of objects
person = people[0];
name = people[0].name;
// Nested arrays
value = matrix[1][2];
7.9 Grouping Expressions¶
Syntax: ( expression )
Purpose: Override default precedence
Examples:
result = (a + b) * c; // Add first, then multiply
result = a + b * c; // Multiply first, then add
average = (a + b + c) / 3;
check = x > 5 && y < 10;
8. Native Functions Reference¶
8.1 Mathematics Functions¶
$Max¶
Signature: $Max(a, b) → decimal
Description: Returns the larger of two numbers
Example:
$Min¶
Signature: $Min(a, b) → decimal
Description: Returns the smaller of two numbers
Example:
$Abs¶
Signature: $Abs(n) → decimal
Description: Returns absolute value
Example:
$Round¶
Signature: $Round(n, decimals?) → decimal
Description: Rounds number to specified decimal places (default 0)
Example:
rounded = $Round(3.14159); // 3
rounded = $Round(3.14159, 2); // 3.14
premium = $Round(1234.567, 2); // 1234.57
$Sum¶
Signature: $Sum(array) → decimal or $Sum(arrayOfObjects, property) → decimal
Description: Sums array elements or property values
Example:
8.2 Date/Time Functions¶
$Now¶
Signature: $Now() → DateTime
Description: Returns current UTC date and time
Example:
$Today¶
Signature: $Today() → DateTime
Description: Returns today's date at midnight (00:00:00)
Example:
$Year¶
Signature: $Year(date) → decimal
Description: Extracts year from date
Example:
$Month¶
Signature: $Month(date) → decimal
Description: Extracts month from date (1-12)
Example:
$Day¶
Signature: $Day(date) → decimal
Description: Extracts day from date (1-31)
Example:
$AddYears¶
Signature: $AddYears(date, years) → DateTime
Description: Adds years to date
Example:
$AddMonths¶
Signature: $AddMonths(date, months) → DateTime
Description: Adds months to date
Example:
$AddDays¶
Signature: $AddDays(date, days) → DateTime
Description: Adds days to date
Example:
$AddHours¶
Signature: $AddHours(date, hours) → DateTime
Description: Adds hours to datetime
Example:
$AddMinutes¶
Signature: $AddMinutes(date, minutes) → DateTime
Description: Adds minutes to datetime
Example:
$AddSeconds¶
Signature: $AddSeconds(date, seconds) → DateTime
Description: Adds seconds to datetime
Example:
$DifferenceInYears¶
Signature: $DifferenceInYears(date1, date2) → decimal
Description: Calculates year difference (date2 - date1)
Example:
age = $DifferenceInYears(birthDate, $Today());
diff = $DifferenceInYears("2020/01/01", "2023/01/01"); // -3
$DifferenceInDays¶
Signature: $DifferenceInDays(date1, date2) → decimal
Description: Calculates day difference (date2 - date1)
Example:
$DifferenceInHours¶
Signature: $DifferenceInHours(date1, date2) → decimal
Description: Calculates hour difference (date2 - date1)
Example:
$DifferenceInMinutes¶
Signature: $DifferenceInMinutes(date1, date2) → decimal
Description: Calculates minute difference (date2 - date1)
Example:
$DifferenceInSeconds¶
Signature: $DifferenceInSeconds(date1, date2) → decimal
Description: Calculates second difference (date2 - date1)
Example:
$StartOfDay¶
Signature: $StartOfDay(date) → DateTime
Description: Returns date at 00:00:00
Example:
$EndOfDay¶
Signature: $EndOfDay(date) → DateTime
Description: Returns date at 23:59:59
Example:
$ToDate¶
Signature: $ToDate(string) → DateTime
Description: Parses string to DateTime
Example:
$ToDateOnly¶
Signature: $ToDateOnly(string) → DateOnly
Description: Parses string to DateOnly
Example:
$ToDateUTC¶
Signature: $ToDateUTC(string) → DateTime
Description: Parses string as UTC DateTime
Example:
$ToShortDateString¶
Signature: $ToShortDateString(date) → string
Description: Formats date to short string
Example:
$ConvertTimeFromUtc¶
Signature: $ConvertTimeFromUtc(date, timezoneId) → DateTime
Description: Converts UTC time to specified timezone
Example:
$GetTimeZoneAbbreviation¶
Signature: $GetTimeZoneAbbreviation(timezoneId, date) → string
Description: Gets timezone abbreviation for date
Example:
8.3 String Functions¶
$Length (for strings)¶
Signature: $Length(string) → decimal
Description: Returns string length
Example:
$ToString¶
Signature: $ToString(value) → string
Description: Converts value to string
Example:
$ToStringSafe¶
Signature: $ToStringSafe(value) → string
Description: Safely converts value to string (handles nulls)
Example:
$Stringify¶
Signature: $Stringify(value) → string
Description: Converts value to JSON representation
Example:
$Split (Instance Method)¶
Signature: string.Split(separator) → InstanceArray
Description: Splits string into array
Example:
$Replace (Instance Method)¶
Signature: string.Replace(oldValue, newValue) → string
Description: Replaces substring
Example:
$Contains (Instance Method)¶
Signature: string.Contains(substring) → boolean
Description: Checks if string contains substring
Example:
$StartsWith (Instance Method)¶
Signature: string.StartsWith(prefix) → boolean
Description: Checks if string starts with prefix
Example:
$EndsWith (Instance Method)¶
Signature: string.EndsWith(suffix) → boolean
Description: Checks if string ends with suffix
Example:
$RemoveNonAlpha (Instance Method)¶
Signature: string.RemoveNonAlpha() → string
Description: Removes non-alphabetic characters
Example:
.ToUpper() (Instance Method)¶
Signature: string.ToUpper() → string
Description: Converts to uppercase
Example:
.ToLower() (Instance Method)¶
Signature: string.ToLower() → string
Description: Converts to lowercase
Example:
.Trim() (Instance Method)¶
Signature: string.Trim() → string
Description: Removes leading/trailing whitespace
Example:
.ToNumber() (Instance Method)¶
Signature: string.ToNumber() → decimal
Description: Parses string to number
Example:
.ToString() (Instance Method)¶
Signature: string.ToString() → string
Description: Returns string (identity)
Example:
8.4 Array Functions¶
$Array¶
Signature: $Array(item1, item2, ...) → InstanceArray
Description: Creates array from arguments
Example:
$Length (for arrays)¶
Signature: $Length(array) → decimal
Description: Returns array length
Example:
$Sort¶
Signature: $Sort(array) → InstanceArray or $Sort(arrayOfObjects, property) → InstanceArray
Description: Sorts array or array of objects by property
Example:
$Reverse¶
Signature: $Reverse(array) → InstanceArray
Description: Reverses array
Example:
$Map¶
Signature: $Map(arrayOfObjects, property) → InstanceArray
Description: Extracts property from array of objects
Example:
$AddToArray¶
Signature: $AddToArray(array, item) → InstanceArray
Description: Returns new array with item added
Example:
$FindEquals¶
Signature: $FindEquals(array, value) → InstanceArray or $FindEquals(arrayOfObjects, property, value) → InstanceArray
Description: Finds elements equal to value
Example:
matches = $FindEquals($Array(1, 2, 3, 2), 2); // [2, 2]
people = $FindEquals(users, "status", "active");
$FindNotEquals¶
Signature: $FindNotEquals(array, value) → InstanceArray or $FindNotEquals(arrayOfObjects, property, value) → InstanceArray
Description: Finds elements not equal to value
Example:
$FindGreaterThan¶
Signature: $FindGreaterThan(array, value) → InstanceArray or $FindGreaterThan(arrayOfObjects, property, value) → InstanceArray
Description: Finds elements greater than value
Example:
$FindGreaterThanOrEqual¶
Signature: $FindGreaterThanOrEqual(array, value) → InstanceArray or $FindGreaterThanOrEqual(arrayOfObjects, property, value) → InstanceArray
Description: Finds elements >= value
Example:
$FindLessThan¶
Signature: $FindLessThan(array, value) → InstanceArray or $FindLessThan(arrayOfObjects, property, value) → InstanceArray
Description: Finds elements less than value
Example:
$FindLessThanOrEqual¶
Signature: $FindLessThanOrEqual(array, value) → InstanceArray or $FindLessThanOrEqual(arrayOfObjects, property, value) → InstanceArray
Description: Finds elements <= value
Example:
$ToArray¶
Signature: $ToArray(value) → InstanceArray
Description: Converts value to array
Example:
.Filter() or .Where() (Instance Method)¶
Signature: array.Filter(predicate) → InstanceArray
Description: Filters array by predicate function
Example:
.Map() or .Select() (Instance Method)¶
Signature: array.Map(transform) → InstanceArray
Description: Transforms array elements; lambda receives (item, index)
Example:
doubled = numbers.Map(x => x * 2);
names = people.Map(p => p.name);
indexed = items.Map((item, i) => { "index": i, "value": item });
.ReduceToNum() (Instance Method)¶
Signature: array.ReduceToNum(fn, initial) → decimal
Description: Reduces array to number; lambda receives (accumulator, item, index)
Example:
sum = numbers.ReduceToNum((acc, x) => acc + x, 0);
product = numbers.ReduceToNum((acc, x) => acc * x, 1);
.ReduceToStr() (Instance Method)¶
Signature: array.ReduceToStr(fn, initial) → string
Description: Reduces array to string; lambda receives (accumulator, item, index)
Example:
.ReduceToObj() (Instance Method)¶
Signature: array.ReduceToObj(fn, initial) → Instance
Description: Reduces array to object; lambda receives (accumulator, item, index)
Example:
grouped = items.ReduceToObj((acc, item) => {
acc.AddProperty(item.key, item.value);
return acc;
}, {});
.Reverse() (Instance Method)¶
Signature: array.Reverse() → InstanceArray
Description: Reverses array in place and returns it
Example:
.ForEach() (Instance Method)¶
Signature: array.ForEach(callback) → null
Description: Executes callback for each element
Example:
.IndexOf() (Instance Method)¶
Signature: array.IndexOf(item) → decimal
Description: Returns index of item (-1 if not found)
Example:
.Slice() (Instance Method)¶
Signature: array.Slice(start, end) → InstanceArray
Description: Returns subarray from start to end
Example:
.Find() (Instance Method)¶
Signature: array.Find(predicate) → object?
Description: Returns first element matching predicate
Example:
.Add() or .Push() (Instance Method)¶
Signature: array.Add(item) → null
Description: Adds item to end of array (mutates)
Example:
.AddRange() (Instance Method)¶
Signature: array.AddRange(otherArray) → null
Description: Adds all items from other array (mutates)
Example:
.Clear() (Instance Method)¶
Signature: array.Clear() → null
Description: Removes all elements (mutates)
Example:
.Remove() (Instance Method)¶
Signature: array.Remove(item) → null
Description: Removes first occurrence of item (mutates)
Example:
.RemoveAt() (Instance Method)¶
Signature: array.RemoveAt(index) → null
Description: Removes element at index (mutates)
Example:
.Insert() (Instance Method)¶
Signature: array.Insert(index, item) → null
Description: Inserts item at index (mutates)
Example:
.OrderBy() (Instance Method)¶
Signature: array.OrderBy(selector) → InstanceArray
Description: Sorts array of objects by selector function
Example:
.OrderByDescending() (Instance Method)¶
Signature: array.OrderByDescending(selector) → InstanceArray
Description: Sorts array descending by selector function
Example:
.Take() (Instance Method)¶
Signature: array.Take(count) → InstanceArray
Description: Returns first N elements
Example:
.Join() (Instance Method)¶
Signature: array.Join(separator) → string
Description: Joins array elements into string
Example:
.Any() (Instance Method)¶
Signature: array.Any(predicate) → boolean
Description: Returns true if any element matches predicate
Example:
.Distinct() (Instance Method)¶
Signature: array.Distinct() → InstanceArray
Description: Returns array with unique elements
Example:
.ToString() (Instance Method)¶
Signature: array.ToString() → string
Description: Returns JSON representation
Example:
8.5 Conversion Functions¶
$ToNumber¶
Signature: $ToNumber(string) → decimal
Description: Parses string to decimal
Example:
$ToDictionary¶
Signature: $ToDictionary(object) → EverDictionary
Description: Converts Instance to EverDictionary (C# interop)
Example:
8.6 Utility Functions¶
$GetValue¶
Signature: $GetValue(object, propertyName) → object?
Description: Gets property value from object
Example:
$SetValue¶
Signature: $SetValue(object, propertyName, value) → null
Description: Sets property value on object
Example:
.AddProperty() (Instance Method)¶
Signature: object.AddProperty(name, value) → null
Description: Adds property to object
Example:
$Log¶
Signature: $Log(message, ...args) → null
Description: Logs to standard output with optional formatting
Example:
$Fetch¶
Signature: $Fetch(url) → string
Description: Fetches content from URL
Example:
$IsInRange¶
Signature: $IsInRange(value, min, max) → boolean
Description: Checks if value is between min and max (inclusive)
Example:
$Binary¶
Signature: $Binary(condition, trueValue, falseValue) → object?
Description: Function-based ternary operator
Example:
8.7 Testing Functions¶
$AssertAreEqual¶
Signature: $AssertAreEqual(expected, actual) → null
Description: Asserts two values are equal (for unit testing)
Example:
$AssertTrue¶
Signature: $AssertTrue(condition) → null
Description: Asserts condition is true
Example:
$AssertFalse¶
Signature: $AssertFalse(condition) → null
Description: Asserts condition is false
Example:
8.8 Domain-Specific Functions¶
$GetCountryNameFromISOCode¶
Signature: $GetCountryNameFromISOCode(isoCode) → string
Description: Converts ISO country code to name
Example:
$GetOptionInfoText¶
Signature: $GetOptionInfoText(...) → string
Description: Domain-specific function for option info text
$GetOptionName¶
Signature: $GetOptionName(...) → string
Description: Domain-specific function for option names
$GetOptionValue¶
Signature: $GetOptionValue(...) → object?
Description: Domain-specific function for option values
$GetRowFromLookupTable¶
Signature: $GetRowFromLookupTable(...) → object?
Description: Domain-specific function for table lookups
9. Error Patterns¶
9.1 Common Syntax Errors¶
Missing semicolon:
Unquoted object keys:
Single-quoted strings:
Variable declaration with var/let/const:
Using keywords for, switch, class:
// ERROR - these don't exist
for (i = 0; i < 10; i++) {}
switch (value) {
}
// CORRECT - use while
i = 0;
while (i < 10) {
i++;
}
9.2 Common Runtime Errors¶
Type mismatch in operations:
// ERROR: Cannot multiply string and number
result = "5" * 10;
// CORRECT: Convert first
result = "5".ToNumber() * 10;
Null reference:
// ERROR: Cannot access property of null
person = null;
name = person.name;
// CORRECT: Check null first
if (person != null) {
name = person.name;
}
Array index out of bounds:
// ERROR: Index 10 doesn't exist
numbers = [1, 2, 3];
value = numbers[10];
// CORRECT: Check length
if (index < numbers.Length) {
value = numbers[index];
}
Wrong number of arguments:
Invalid lambda parameter count:
// ERROR: Filter expects 1 parameter lambda
filtered = numbers.Filter((x, y) => x > 10);
// CORRECT
filtered = numbers.Filter((x) => x > 10);
9.3 Truthiness Errors¶
EverSharp requires explicit booleans in conditionals:
// ERROR: Numbers/strings are not boolean
if (count) {
}
if (name) {
}
// CORRECT: Use comparisons
if (count > 0) {
}
if (name != null) {
}
if (name != "") {
}
9.4 Scope Errors¶
Accessing variable before definition:
// ERROR: result not yet defined
$Log(result);
result = 10;
// CORRECT: Define first
result = 10;
$Log(result);
Accessing local variable from outer scope:
// ERROR: x not visible outside function
function test() {
x = 10;
}
test();
$Log(x); // x is undefined here
// CORRECT: Use return or outer variable
result = 0;
function test() {
result = 10;
}
test();
$Log(result);
9.5 Common Diagnostic Patterns¶
Identifying type errors:
- Look for operations between incompatible types
- Check for missing
.ToNumber(),$ToString()conversions - Verify comparison operands are same type
Identifying null errors:
- Check for property access without null checks
- Look for method calls on potentially null values
- Verify object initialization before access
Identifying arity errors:
- Count arguments vs function parameter count
- Check native function signatures
- Verify lambda parameter counts match method expectations
Identifying scope errors:
- Trace variable definition location
- Check if variable is in outer/inner scope
- Verify function vs block scope
10. Integration Patterns¶
10.1 EverSharpRunner C# API¶
Basic usage:
using Elements.Libs.EverSharp;
var runner = new EverSharpRunner();
runner.Run("premium = 1000;");
if (!runner.HasErrors)
{
var premium = runner.GlobalVariables["premium"]; // 1000m
}
else
{
foreach (var error in runner.Errors)
{
Console.WriteLine(error);
}
}
Running expressions:
Validation only:
var runner = new EverSharpRunner();
var errors = runner.Validate("x = 10; y = 20;");
if (!errors.Any())
{
Console.WriteLine("Valid syntax");
}
10.2 Passing Data In/Out¶
Setting constants (read-only):
var constants = new EverDictionary
{
{ "TAX_RATE", 0.08m },
{ "MIN_PREMIUM", 500m }
};
var variables = new EverDictionary();
var runner = new EverSharpRunner();
runner.InitializeEnvironment(constants, variables);
runner.Run("premium = MIN_PREMIUM * (1 + TAX_RATE);");
Setting variables (mutable):
var variables = new EverDictionary
{
{ "age", 35m },
{ "coverage", 100000m }
};
runner.InitializeEnvironment(new EverDictionary(), variables);
runner.Run("premium = coverage * 0.01 * (age / 30);");
var premium = runner.GlobalVariables.GetNumber("premium");
Setting variables at runtime:
runner.SetExternalVariable("discount", 0.15m);
runner.Run("finalPremium = basePremium * (1 - discount);");
Getting results:
// Access by key
var value = runner.GlobalVariables["variableName"];
// Typed access (extension methods)
var num = runner.GlobalVariables.GetNumber("premium");
var str = runner.GlobalVariables.GetStr("name");
var bool = runner.GlobalVariables.GetBool("isActive");
var dict = runner.GlobalVariables.GetDictionary("policy");
var arr = runner.GlobalVariables.GetArray("items");
10.3 Type Conversions¶
C# to EverSharp:
// Primitives
variables["age"] = 35m; // decimal
variables["name"] = "John"; // string
variables["active"] = true; // bool
variables["date"] = DateTime.Now; // DateTime
// Objects (use EverDictionary)
variables["person"] = new EverDictionary
{
{ "name", "John" },
{ "age", 35m }
};
// Arrays
variables["numbers"] = new object[] { 1m, 2m, 3m };
EverSharp to C#:
// Primitives
var num = (decimal)runner.GlobalVariables["value"];
var str = (string)runner.GlobalVariables["name"];
var flag = (bool)runner.GlobalVariables["active"];
// Objects (returns EverDictionary)
var dict = runner.GlobalVariables.GetDictionary("person");
var name = dict.GetStr("name");
// Arrays (returns object[])
var arr = runner.GlobalVariables.GetArray("numbers");
var first = (decimal)arr[0];
10.4 Custom Extension Functions¶
Implementing Callable interface:
using Elements.Libs.EverSharp;
using Elements.Libs.EverSharp.Visitors;
public class CustomFunction : Callable
{
public object? Call(Interpreter interpreter, List<object?> arguments)
{
// Validate argument count
if (arguments.Count != 2)
throw new RuntimeException("CustomFunction requires 2 arguments");
// Extract and validate arguments
var a = (decimal)arguments[0];
var b = (decimal)arguments[1];
// Perform calculation
return a * b + 100;
}
public int? Arity() => 2; // Fixed arity
// Return null for variable arity
}
Registering extensions:
var extensions = new Dictionary<string, Callable>
{
{ "$CustomFunction", new CustomFunction() },
{ "$AnotherFunction", new AnotherFunction() }
};
var runner = new EverSharpRunner(extensions: extensions);
runner.Run("result = $CustomFunction(10, 20);"); // 300
Variable arity example:
public class VariableArityFunction : Callable
{
public object? Call(Interpreter interpreter, List<object?> arguments)
{
// Sum all arguments
decimal sum = 0;
foreach (var arg in arguments)
{
sum += (decimal)arg;
}
return sum;
}
public int? Arity() => null; // Accepts any number of arguments
}
10.5 Preloading Libraries¶
Using library scripts:
var libraries = new List<string>
{
@"
function calculatePremium(base, rate) {
return base * rate;
}
function applyDiscount(amount, discountRate) {
return amount * (1 - discountRate);
}
"
};
var runner = new EverSharpRunner(libraries: libraries);
// Now these functions are available
runner.Run("premium = calculatePremium(1000, 0.15);");
10.6 Error Handling¶
Checking for errors:
runner.Run(script);
if (runner.HasErrors)
{
foreach (var error in runner.Errors)
{
Console.WriteLine($"Error: {error}");
// Log or handle error
}
}
Exception types:
ParserException: Syntax errors during parsingRuntimeException: Runtime errors during execution- Generic
Exception: Unexpected errors
10.7 Standard Output¶
Capturing $Log output:
runner.Run(@"
$Log(""Starting calculation"");
result = 10 + 20;
$Log(""Result: {0}"", result);
");
var output = runner.StandardOutput;
// "Starting calculation\nResult: 30\n"
10.8 Best Practices¶
1. Always check for errors:
2. Use type-safe access methods:
// Preferred
var num = runner.GlobalVariables.GetNumber("premium");
// Instead of
var num = (decimal)runner.GlobalVariables["premium"];
3. Initialize environment before running:
4. Validate syntax first for user-provided scripts:
5. Use EverDictionary for complex objects:
var policy = new EverDictionary
{
{ "number", "POL-123" },
{ "premium", 1500m },
{ "customer", new EverDictionary
{
{ "name", "John" },
{ "age", 35m }
}
}
};
End of AI Reference Document¶
This comprehensive reference provides all necessary information for AI agents to understand, diagnose, and work with EverSharp code. For human-readable documentation, refer to the main documentation files organized by topic.