ast

package
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Sep 24, 2026 License: BSD-3-Clause Imports: 10 Imported by: 0

Documentation

Overview

Package ast defines Abstract Syntax Tree (AST) nodes for a sheet cell.

Index

Constants

This section is empty.

Variables

View Source
var ErrCalculate = errors.New("calculate error")

ErrCalculate means the AST has an error can can't be calculated.

View Source
var ErrCellRange = errors.New("invalid cell range")

ErrCellRange means the cell range is invalid.

View Source
var ErrParse = errors.New("parse error")

ErrParse means the was en error parsing data.

Functions

func Calculate

func Calculate(n Node, refcal func(string) (float64, error)) (float64, error)

Calculate evaluates a formula node and skips other types of nodes. It returns an error if evaluation fails.

func Equal

func Equal(x, y float64) bool

Equal is approximate equality of two floating numbers. The two number are considered equal, if relative absolute difference between the two is within set precision [floatEqPrecision].

Types

type BinOpNode

type BinOpNode struct {
	Op    string // binary operator
	Left  Node   // left operand
	Right Node   // right operand
}

BinOpNode is a binary operation of the form "left op right".

type CallNode

type CallNode struct {
	Name string // function name
	Args []Node // function arguments, can be empty
}

CallNode is a function call.

type CellScanner

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

CellScanner scans through cells in the inclusive range [from, to].

It supports inverted range, e.g. "A3:A1". In either case, the scanner generates a sequence of cell IDs, sorted by row and column.

func NewCellScanner

func NewCellScanner(from, to string) (*CellScanner, error)

NewCellScanner creates a cell scanner. It returns an error if the range is invalid, e.g. the cell reference uses double-letter cells (unsupported).

func (*CellScanner) Len

func (sc *CellScanner) Len() int

Len returns the number of cells in the cell range.

func (*CellScanner) Scan

func (sc *CellScanner) Scan() iter.Seq[string]

Scan generates a sequence of cell identiifiers in the range.

type Engine

type Engine struct{}

Engine uses AST for intermediate representation.

func (Engine) Calculate

func (Engine) Calculate(data any, refcal func(string) (float64, error)) (float64, error)

Calculate evaluates cell's AST node.

func (Engine) Parse

func (Engine) Parse(s string) (any, error)

Parse parses a cell value into an AST node.

func (Engine) WriteIR

func (Engine) WriteIR() bool

WriteIR disables intermediate representation in the Sheet write/read state.

type IfNode

type IfNode struct {
	Cond   *RelOpNode // a comparison binary expression
	IfPass Node       // an expression to be executed when if-condition is true
	IfFail Node       // an expression to be executed when if-condition is false
}

IfNode is if-clause, e.g. IF(Cond, IfPassExpr, IfFailExpr).

type Node

type Node any

Node represents any node.

func Parse

func Parse(s string) (Node, error)

Parse runs lexical and syntacit analysis of s. It returns an AST node upon success or a non-nil error in case of failure.

type NumberNode

type NumberNode struct {
	Number string // number text value
}

NumberNode is a number value.

type RangeNode

type RangeNode struct {
	From string // from cell
	To   string // to cell (inclusive)
}

RangeNode represents a cells rang, e.g. A1:A5

type RefNode

type RefNode struct {
	Ref string // cell reference
}

RefNode is a cell reference.

type RelOpNode

type RelOpNode struct {
	Op    string // comparison operator: ==, !=, <, <=, >, >=
	Left  Node   // left operand
	Right Node   // right operand
}

RelOpNode is a comparison binary operation, e.g. "A1 < 5".

Jump to

Keyboard shortcuts

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