Lumesh is a modern shell and scripting language that implements complex expression parsing using a Pratt parser.
Basic Syntax Structure#
Operator Precedence System#
Lumesh uses a precisely defined operator precedence, arranged from low to high:
- Assignment Operators (Priority 1):
=,:=,+=,-=,*=,/= - Redirection and Pipeline (Priority 2):
|,|_,|>,|^,>>,>! - Error Handling (Priority 3):
?.,?+,??,?>,?! - Lambda Expressions (Priority 4):
-> - Conditional Operators (Priority 5):
?: - Logical OR (Priority 6):
|| - Logical AND (Priority 7):
&& - Comparison Operators (Priority 8):
==,!=,>,<,>=,<= - Arithmetic operations...
Data Types#
Basic Types#
Lumesh supports various basic data types:
1# Integers
2let num = 42
3let negative = -100
4
5# Floating-point numbers
6let pi = 3.14159
7let percent = 85%
8
9# Strings
10let str1 = "Double-quoted string,\nwith escape support"
11let str2 = 'Single-quoted raw string,\nno escape'
12let template = `Template string $var ${var + 1}`
13
14# Booleans
15let flag = True
16let disabled = False
17
18# Null values
19let empty = None
20
21# File sizes
22let a = 3000KCollection Types#
1# Lists (List)
2let arr = [1, 2, 3, "mixed", True]
3let nested = [[1, 2], [3, 4]]
4
5# Hash Map (HMap) - Unordered, fast lookup
6# let hmap = H{name: "Alice", age: 25}
7
8# Ordered Map (Map) - Ordered, supports range queries
9let map = {a: 1, b: 2, c: 3}
10
11# Ranges (Range)
12let range1 = 1..10 # 1 to 9 (excluding 10)
13let range2 = 1..=10 # 1 to 10 (including 10)
14let range3 = 1..10:2 # 1, 3, 5, 7, 9 (step of 2)
15
16let array = 1...10 # Create an array directly from the rangeVariables and Assignment#
Basic Assignment#
1# Variable declaration and assignment
2let x = 10
3let name = "Lumesh"
4
5# Multiple variable assignment
6let a, b, c = 1, 2, 3
7let x, y = getValue()Delayed Assignment#
Lumesh's unique delayed assignment feature:
1# Use := for delayed assignment; the expression will not execute immediately
2let cmd := ls -l /tmp
3let calculation := 2 + 3 * 4
4
5# Execute only when needed
6eval(cmd) # Executes ls -l /tmp
7eval(calculation) # Calculates to 14
8
9# If it's a command, it can also be executed directly
10cmdDestructuring Assignment#
Supports destructuring assignment for arrays and maps:
1# Array destructuring
2let [first, second, *rest] = [1, 2, 3, 4, 5]
3# first = 1, second = 2, rest = [3, 4, 5]
4
5# Map destructuring
6let {name, age} = {name: "Bob", age: 30, city: "NYC"}
7# name = "Bob", age = 30
8
9# Renaming destructuring
10let {name: username, age: userAge} = user_dataOperators#
Arithmetic Operators#
1let a = 10 + 5 # Addition: 15
2let b = 10 - 3 # Subtraction: 7
3let c = 4 * 6 # Multiplication: 24
4let d = 15 / 3 # Division: 5
5let e = 17 % 5 # Modulus: 2
6let f = 2 ^ 3 # Exponentiation: 8Comparison Operators#
1# Basic comparisons
2a == b # Equal
3a != b # Not equal
4a > b # Greater than
5a < b # Less than
6a >= b # Greater than or equal
7a <= b # Less than or equal
8
9# Pattern matching
10text ~= "pattern" # Regex match
11text ~: "substring" # Contains match
12text !~= "pattern" # Regex not match
13text !~: "substring" # Does not contain matchLogical Operators#
1condition1 && condition2 # Logical AND
2condition1 || condition2 # Logical OR
3!condition # Logical NOTPipeline Operations#
Lumesh provides various types of pipelines, which is its unique feature:
1# Standard pipeline - Pass standard output or structured data
2cmd1 | cmd2
3
4# Positional pipeline - Replace specified parameter positions
5data |_ process_func arg1 _ arg3
6
7# Loop pipeline - Dispatch one element at a time, loop execution, can specify parameter positions
8list |> transform(param1, param2, _)
9
10# PTY pipeline - For interactive programs
11cmd1 |^ interactive_programError Handling#
Lumesh has a powerful built-in error handling mechanism:
1# Ignore errors and continue execution
2risky_command ?.
3
4# Print error to standard output
5command ?+
6
7# Print error to standard error output
8command ??
9
10# Print error and override result
11command ?>
12
13# Terminate the program on error
14command ?!
15
16# Custom error handling, can be used to provide default values
17command ?: error_handlerControl Flow#
Conditional Statements#
1# if-else expression
2let result = if condition {
3 "true branch"
4} else {
5 "false branch"
6}
7
8# Ternary operator, supports nesting
9let value = condition ? true_value : false_valueLoop Structures#
1# while loop
2while condition {
3 # Loop body
4 update_condition()
5}
6
7# for loop, can be used for strings, string splitting follows IFS settings; can use * wildcard
8for item in collection {
9 process(item)
10}
11
12# Range loop
13for i in 0..10 {
14 println(i)
15}
16
17# Infinite loop
18loop {
19 if break_condition {
20 break
21 }
22}
23
24# Simple repetition
25repeat 10 {a += 1}Pattern Matching#
Supports regex
1match value {
2 1 => "one",
3 2, 3 => "two or three",
4 xx => "is symbol/string xx",
5 '\w' => "is word",
6 '\d+' => "is digit",
7 _ => "default case"
8}Functions#
Function Definition#
1# Basic function
2fn greet(name) {
3 "Hello, " + name
4}
5
6# Function with default parameters
7fn add(a, b = 0) {
8 a + b
9}
10
11# Variable parameter function
12fn sum(a, *numbers) {
13 numbers | List.fold(0, (acc, x) -> acc + x)
14}Lambda Expressions#
1# Single-parameter lambda
2let square = x -> x * x
3
4# Multi-parameter lambda
5let add = (a, b) -> a + b
6
7# Complex lambda body
8let process = data -> {
9 let filtered = data | List.filter(x -> x > 0)
10 filtered | List.map(x -> x * 2)
11}Function Decorators#
Supports function decorator syntax:
1@timing
2@cache(300)
3fn expensive_calculation(input) {
4 # Complex calculation
5 heavy_computation(input)
6}Chained Calls#
Supports object-oriented style chained method calls:
1# String chained operations
2"hello world"
3 .split(' ')
4 .map(s -> s.to_upper())
5 .join('-')
6
7# Data processing chain
8data
9 .filter(x -> x.active)
10 .sort(x -> x.priority)
11 .take(10)Indexing and Slicing#
1# Array indexing
2let arr = [1, 2, 3, 4, 5]
3let first = arr[0] # 1
4
5# Array slicing
6let slice1 = arr[1:4] # [2, 3, 4]
7let slice2 = arr[1:] # [2, 3, 4, 5]
8let slice3 = arr[:3] # [1, 2, 3]
9let slice4 = arr[-3:] # [3, 4, 5]
10let slice5 = arr[::2] # [1, 3, 5] (step of 2)
11
12# Map indexing
13let obj = {name: "Alice", age: 25}
14let name = obj[name] # "Alice"
15let ages = obj.age # 25 (dot access)
16let age = obj@age # 25 (@ access)Range Operations#
1# Basic range
21..10 # 1 to 10
31..<10 # 1 to 9 (explicitly excluding)
4
5# Ranges with step
61..10:2 # 1, 3, 5, 7, 9
70..100:10 # 0, 10, 20, ..., 90String Handling#
1# String interpolation
2let name = "World"
3let age = 18
4let greeting = `Hello, ${age > 18 ? "Mr." : "Dear"} $name!`
5
6# Multi-line string
7let multiline = "
8This is a
9multi-line string
10"
11
12# Raw string (no escape)
13let raw = 'C:\path\to\file'Collection Operations#
1# List operations
2let numbers = [1, 2, 3, 4, 5]
3numbers.append(6) # Add element
4numbers + 6 # Add element
5# numbers.pop() # Remove last element
6numbers - 4 # Remove specified element
7numbers.len() # Get length
8
9# Map operations
10let person = {name: "Bob", age: 30}
11# person.city = "NYC" # Add property
12person + {city: "NYC"} # Add property
13# del person.age # Delete property
14person.keys() # Get all keysModule System#
1# Import modules
2use my_mod
3use my_mod as a
4
5# Use module functions
6Fs.ls("/tmp")
7String.split("hello world", " ")
8Math.sin(Math.PI / 2)Comments#
1# Single-line comment
2let x = 10 # End-of-line commentAdvanced Features#
Custom Operators#
1# Define custom operators
2let _+ = (a, b) -> a.concat(b) # Custom addition
3let __! = x -> Math.sum(x) # Custom prefix operator
4
5# Use custom operators
6[1, 2] _+ [3, 4] # [1, 2, 3, 4]
7[5, 6, 7] __! # 18Notes#
The design philosophy of Lumesh is to combine the elegant syntax of modern programming languages with the practicality of shell scripting, providing powerful error handling, pipeline operations, and a module system. The priority system implemented through the Pratt parser ensures the correct parsing of complex expressions, while the rich built-in modules and configuration options make it suitable for various scenarios, from simple command-line operations to complex system management scripts.
Learn more: