6.4 KiB
Ecli
Generated by Copilot
1. Overview
ecli (Expression Calculator Interactive Tool) is an interactive REPL (Read-Eval-Print Loop) application for evaluating expressions using the Expr package. It provides a powerful command-line interface for expression evaluation with support for multiple builtin modules, file operations, and interactive scripting.
The tool combines the expression evaluation capabilities of the Expr package with an interactive shell environment, making it ideal for:
-
Interactive expression testing and prototyping
-
Batch expression evaluation from scripts
-
Data processing and transformation
-
Mathematical computations with fractions and complex operators
2. Getting Started
2.1. Installation
To build and install ecli:
cd cmd/ecli
./build.bash
The compiled binary will be available as ecli in the current directory.
2.2. Basic Usage
Start the interactive REPL:
./ecli
You’ll see the prompt `>>> ` where you can enter expressions to evaluate.
2.3. Command Line Options
| Option | Description |
|---|---|
|
Evaluate an expression directly without entering REPL mode |
|
Force REPL operation after processing all |
|
Import builtin modules (comma-separated list, glob patterns, or 'all') |
|
List all available builtin module names |
|
List all builtin modules |
|
Print expressions in prefix form |
|
Print expressions in tree form |
|
Disable printing of expression results |
|
Show help message |
|
Show program version |
3. Interactive Commands
Within the REPL, you can use the following commands:
| Command | Description |
|---|---|
|
Display available commands and command-line options |
|
Exit the REPL |
|
Toggle multi-line input mode for complex expressions |
|
Toggle TTY mode |
|
Execute expressions from a file |
4. Features
4.1. Expression Evaluation
ecli supports the full expression language provided by the Expr package, including:
-
Arithmetic Operations: Addition, subtraction, multiplication, division, modulo
-
Bitwise Operations: AND, OR, XOR, NOT, shift operations
-
Boolean Logic: AND, OR, NOT operations
-
Relational Operators: Comparison and equality operators
-
String Operations: Concatenation and string manipulation
-
Iterators: Range, list, and custom iterators
-
Functions: Builtin and user-defined functions
-
Collections: Lists, dictionaries, and linked lists
-
Fractions: Support for fractional arithmetic
4.2. Builtin Modules
ecli provides access to various builtin modules through the -b option:
| Module | Functionality |
|---|---|
|
Core expression evaluation functions |
|
String formatting and output functions |
|
String manipulation functions |
|
Mathematical and arithmetic operations |
|
Iterator-related functions |
|
File I/O operations |
|
Module import functionality |
Use -B or --list-builtins to see all available modules:
./ecli --list-builtins
5. Examples
5.1. Basic Arithmetic
>>> 2 + 3 * 4
14
>>> (2 + 3) * 4
20
5.2. String Operations
>>> "hello" + " " + "world"
hello world
5.3. Using Iterators
>>> [1, 2, 3, 4, 5] | map(. * 2)
[2, 4, 6, 8, 10]
5.4. Evaluating from Command Line
./ecli -e "2 + 2"
4
5.5. Loading Builtin Modules
./ecli -b "math-arith,string"
5.6. Loading Expressions from Files
Inside the REPL:
>>> source "expressions.expr"
Or from command line:
./ecli -e '@include "expressions.expr"'
6. Configuration
6.1. Resource Files
ecli supports startup resource files:
-
.ecli.rc- Main configuration file -
.ecli.rc.d/- Directory for modular configuration files
These files are automatically loaded at startup if they exist in the current directory or home directory.
7. Building from Source
7.1. Prerequisites
-
Go 1.18 or later
-
Make or bash shell
7.2. Build Steps
cd cmd/ecli
./build.bash
7.3. Build Artifacts
The build process generates:
-
ecli- The main executable -
version.txt- Version information -
Platform-specific binaries (e.g.,
ecli_v1.17.0_linux_amd64,ecli_v1.17.0_darwin_arm64)
8. Advanced Usage
8.1. Multi-line Input
For complex expressions, toggle multi-line mode:
>>> multiline
>>> result = [1, 2, 3, 4, 5]
... | filter(. > 2)
... | map(. * 2)
>>> result
[6, 8, 10]
8.2. Script Execution
Create a file calculations.expr:
x = 10
y = 20
result = x + y * 2
Execute it:
./ecli -e '@source "calculations.expr"' -e 'result'
8.3. Chaining Operations
>>> data = [{"name": "alice", "age": 30}, {"name": "bob", "age": 25}]
>>> data | map(.name)
[alice, bob]
9. Troubleshooting
9.1. Expression Parsing Errors
If you encounter parsing errors, check:
-
Bracket matching and quotation marks
-
Operator precedence
-
Variable and function names
9.3. File Not Found Errors
Ensure file paths are:
-
Properly quoted in expressions
-
Relative to the current working directory or absolute paths
-
Readable by the current user
10. Related Documentation
-
Expr Expression Language Syntax
-
Builtin Modules Reference
11. Version History
For version information and changes, see the version file.
12. License
Copyright (c) 2024-2026 Celestino Amoroso (celestino.amoroso@gmail.com). All rights reserved.