Skip to content

Syntax & Operators

SmartCal integrates a rich and expressive grammar, precisely handled by its Pratt parser.

Operator & Precedence Table

Operators are listed below in descending order of priority (from highest to lowest precedence):

CategoryOperatorDescriptionAssociativityExample
Parentheses( )Priority grouping-(2 + 3) * 4
Unary+, -Positive / Unary negationRight-x, -(a + b)
Exponentiation^PowerRight2 ^ 3 ^ 2 (= $2^9 = 512$)
Multiplicative*, /, %Multiplication, Division, ModuloLeft10 % 3 * 2
Additive+, -Addition, SubtractionLeft10 + 5 - 2
Comparison<, <=, >, >=Order comparisonsLeftage >= 18
Equality==, !=Strict equality / InequalityLeftstatus == 'active'
Logical AND&&Logical conjunction (short-circuit)Lefta > 0 && b > 0
Logical OR||Logical disjunction (short-circuit)LeftisAdmin || isOwner
Conditional? :Ternary operatorRightscore > 50 ? 1 : 0

Supported Data Types

Numbers

SmartCal accepts integers and floating-point numbers:

typescript
SmartCal('3.14159 * radius ^ 2', { radius: 5 });

Strings

String literals can be delimited by single quotes '...' or double quotes "..." and can contain spaces:

typescript
SmartCal("user == 'John Doe' ? 'VIP' : 'Standard'", { user: 'John Doe' }); // 'VIP'

Identifiers & Unicode

Variable names can contain Latin letters, digits, underscores _, and all international Unicode characters:

typescript
SmartCal('café_prix + quantité_achetée', {
  café_prix: 2.5,
  quantité_achetée: 4,
}); // 6.5

Nested Ternary Operators

Thanks to the right associativity of the Pratt Parser, nested decision trees are written clearly without ambiguity:

typescript
const taxBracket = compile(
  'income < 10000 ? 0 : (income < 40000 ? 0.15 : (income < 100000 ? 0.30 : 0.45))'
);

taxBracket.evaluate({ income: 55000 }); // 0.3

Released under MIT License.