expr

package
v0.2.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jan 6, 2026 License: MIT Imports: 5 Imported by: 0

Documentation

Overview

Package expr provides a simple expression DSL for cortex formulas.

Supported operations:

  • Arithmetic: +, -, *, /, %
  • Comparison: ==, !=, <, >, <=, >=
  • Logical: &&, ||, !
  • Functions: min, max, abs, floor, ceil, round, if, sqrt, pow

Example expressions:

"base_salary * tax_rate"
"if(age >= 65, senior_discount, 0)"
"round(total * 0.0825, 2)"
"min(calculated, max_amount)"

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type BinaryExpr

type BinaryExpr struct {
	Op    TokenType
	Left  Node
	Right Node
}

BinaryExpr represents a binary expression.

type BoolLit

type BoolLit struct {
	Value bool
}

BoolLit represents a boolean literal.

type CallExpr

type CallExpr struct {
	Name string
	Args []Node
}

CallExpr represents a function call.

type Evaluator

type Evaluator struct {
	// contains filtered or unexported fields
}

Evaluator evaluates an AST against a value getter.

func NewEvaluator

func NewEvaluator() *Evaluator

NewEvaluator creates a new evaluator with built-in functions.

func (*Evaluator) Eval

func (e *Evaluator) Eval(ctx context.Context, node Node, getter ValueGetter) (any, error)

Eval evaluates an AST node against a value getter.

func (*Evaluator) RegisterFunc

func (e *Evaluator) RegisterFunc(name string, fn Func)

RegisterFunc registers a custom function.

type Expression

type Expression struct {
	// contains filtered or unexported fields
}

Expression represents a compiled expression.

func Compile

func Compile(input string) (*Expression, error)

Compile parses and compiles an expression string.

func MustCompile

func MustCompile(input string) *Expression

MustCompile compiles an expression, panicking on error.

func (*Expression) Eval

func (e *Expression) Eval(ctx context.Context, getter ValueGetter) (any, error)

Eval evaluates the expression against a value getter.

func (*Expression) EvalBool

func (e *Expression) EvalBool(ctx context.Context, getter ValueGetter) (bool, error)

EvalBool evaluates the expression and returns a bool.

func (*Expression) EvalFloat64

func (e *Expression) EvalFloat64(ctx context.Context, getter ValueGetter) (float64, error)

EvalFloat64 evaluates the expression and returns a float64.

func (*Expression) EvalWithMap

func (e *Expression) EvalWithMap(ctx context.Context, values map[string]any) (any, error)

EvalWithMap evaluates the expression using a map as the value source.

func (*Expression) Raw

func (e *Expression) Raw() string

Raw returns the original expression string.

func (*Expression) RegisterFunc

func (e *Expression) RegisterFunc(name string, fn Func)

RegisterFunc registers a custom function for this expression.

type Func

type Func func(args ...any) (any, error)

Func is a built-in function type.

type Ident

type Ident struct {
	Name string
}

Ident represents an identifier (variable reference).

type Lexer

type Lexer struct {
	// contains filtered or unexported fields
}

Lexer tokenizes an expression string.

func NewLexer

func NewLexer(input string) *Lexer

NewLexer creates a new lexer for the given input.

func (*Lexer) NextToken

func (l *Lexer) NextToken() Token

NextToken returns the next token from the input.

type Node

type Node interface {
	// contains filtered or unexported methods
}

Node represents an AST node.

func Parse

func Parse(input string) (Node, error)

Parse parses an expression string into an AST.

type NumberLit

type NumberLit struct {
	Value float64
}

NumberLit represents a numeric literal.

type Parser

type Parser struct {
	// contains filtered or unexported fields
}

Parser parses an expression into an AST.

func NewParser

func NewParser(input string) *Parser

NewParser creates a new parser for the given input.

func (*Parser) Errors

func (p *Parser) Errors() []string

Errors returns any parsing errors.

func (*Parser) Parse

func (p *Parser) Parse() (Node, error)

Parse parses the expression and returns the AST root.

type StringLit

type StringLit struct {
	Value string
}

StringLit represents a string literal.

type Token

type Token struct {
	Type    TokenType
	Literal string
	Pos     int
}

Token represents a lexical token.

func Tokenize

func Tokenize(input string) []Token

Tokenize returns all tokens from the input.

type TokenType

type TokenType int

TokenType represents the type of a token.

const (
	TokenEOF TokenType = iota
	TokenError

	// Literals
	TokenNumber
	TokenString
	TokenIdent
	TokenBool

	// Operators
	TokenPlus    // +
	TokenMinus   // -
	TokenStar    // *
	TokenSlash   // /
	TokenPercent // %
	TokenEq      // ==
	TokenNe      // !=
	TokenLt      // <
	TokenLe      // <=
	TokenGt      // >
	TokenGe      // >=
	TokenAnd     // &&
	TokenOr      // ||
	TokenNot     // !

	// Delimiters
	TokenLParen // (
	TokenRParen // )
	TokenComma  // ,
)

func (TokenType) String

func (t TokenType) String() string

type UnaryExpr

type UnaryExpr struct {
	Op   TokenType
	Expr Node
}

UnaryExpr represents a unary expression.

type ValueGetter

type ValueGetter interface {
	Get(key string) (any, bool)
}

ValueGetter retrieves values by name (e.g., from EvalContext).

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL