chabo-dsl Language Reference
Overview
chabo-dsl is a lightweight scripting language centered around expression evaluation.
Its main features are:
- Numeric operations
- String manipulation
- Comparison operations
- Logical operations
- Ternary operator
- Variables
- Global variables
- Arrays
- Hashes
- User-defined functions
- Recursive calls
- Built-in functions
- Unicode variable names
Data Types
Numbers
Both integers and floating-point numbers can be used.
123
3.14
-10
Example
1 + 2 * 3
Result:
7
Strings
Strings can be enclosed in either single quotes or double quotes.
"hello"
'world'
Example
"abc"
Variables
Variables are defined by assignment.
x = 10;
arr = [1, 2, 3];
has = {a: 1, b: 2};
Local Variables
Normal assignments are treated as local variables.
a = 10;
b = a + 5;
Global Variables
Using := stores a variable in the global scope.
a := 10;
Arrays
Arrays are defined using [].
[1, 2, 3]
Accessing Array Elements
a[0]
Internally, this is converted to the following form:
array(a, 0)
Hashes
Hashes are defined using {}.
{
name: "john",
age: 20
}
Accessing Hash Values
user{name}
Internally, this is converted to the following form:
hash(user, name)
Operators
Operators have precedence.
The smaller the precedence value, the weaker the operator.
Statement Separator
Statements are separated by ;.
a = 1;
b = 2;
a + b
Result:
3
Logical Operators
OR
||
Example:
a || b
AND
&&
Example:
a && b
Logical operators use short-circuit evaluation.
Assignment Operators
Assignment
=
Example:
a = 10;
Addition Assignment
+=
Example:
count += 1;
Global Assignment
:=
Example:
a := 10;
Comparison Operators
The following comparison operators are available.
| Operator | Meaning |
|---|---|
!= |
Not equal |
< |
Less than |
<= |
Less than or equal to |
> |
Greater than |
>= |
Greater than or equal to |
Automatic Type Detection for Comparisons
When performing a comparison, looks_like_number() is used to automatically determine whether the operands are numbers or strings.
For numbers:
10 < 20
For strings, the following comparisons corresponding to Perl's string comparison operators are used.
| Comparison | Operator |
|---|---|
| Less than | lt |
| Greater than | gt |
| Less than or equal to | le |
| Greater than or equal to | ge |
| Equal | eq |
| Not equal | ne |
Arithmetic Operators
Multiplication
*
Division
/
If division by zero occurs, a Zero divied!! exception is raised.
Modulo
%
Exponentiation
**
Example:
2 ** 8
Result:
256
Unary Operators
Sign Negation
-x
Internal representation:
NGE
Negation
!x
Ternary Operator
Returns one of two values depending on a condition.
Syntax
condition ? true_value : false_value
Example
age >= 20 ? "adult" : "child"
Increment and Decrement
Prefix
++x
--x
Postfix
x++
x--
User-Defined Functions
Defining a Function
User-defined functions can be defined using the following syntax.
add(a, b) = a + b
Calling a Function
add(1, 2)
Result:
3
Function Calls
Functions are called using the following syntax.
func(arg1, arg2)
Example:
sqrt(9)
Control Constructs
return
Exits a user-defined function.
return(expr)
Example:
fact(n) =
n <= 1
? 1
: n * fact(n - 1)
return(fact(5))
continue
Returns to the beginning of a user-defined function.
continue()
Recursion
User-defined functions can call themselves recursively.
Example:
fact(n) =
n <= 1
? 1
: n * fact(n - 1)
In this example, fact() calls itself.
Built-in Functions
Mathematical Functions
sqrt
sqrt(x)
Returns the square root.
sin
sin(x)
cos
cos(x)
int
int(x)
String Functions
uc
uc(text)
Converts a string to uppercase.
lc
lc(text)
Converts a string to lowercase.
length
length(x)
Returns the length of a string or array.
substr
substr(text, start, length)
Returns a portion of a string.
split
split(regex, text)
Splits a string using a regular expression.
Return value:
Array
join
join(sep, array)
Concatenates the elements of an array using the specified separator.
Example:
join(",", ["a", "b", "c"])
Result:
"a,b,c"
match
match(regex, text)
Returns a list of strings matching the regular expression.
Return value:
Array
replace
replace(regex, replacement, text)
Replaces portions matching the regular expression.
Example:
replace("a", "X", "abc")
Result:
Xbc
Array Manipulation Functions
push
push(arr, value)
Adds an element to the end of an array.
pop
pop(arr)
Removes and returns an element from the end of an array.
shift
shift(arr)
Removes and returns an element from the beginning of an array.
unshift
unshift(arr, value)
Adds an element to the beginning of an array.
Range Operator
The .. operator generates an array of consecutive numbers.
1 .. 10
Result:
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
Index Access
Array
arr[3]
Hash
user{name}
Hash Manipulation Functions
keys
keys(hash)
Returns a list of the hash keys.
Return value:
Array
map
map applies an expression to each element of an array and returns the results as an array.
Syntax
map(expr, array)
Special Variable
$_ contains the array element currently being processed.
Example
map($_ * 2, [1, 2, 3])
Result:
[2, 4, 6]
Execution Limits
chabo-dsl has execution limits to prevent infinite recursion and excessively long-running programs.
Recursion Limit
If recursive calls exceed 1000 levels, the following exception is raised:
stack over!
Execution Timeout
If execution takes longer than 5 seconds, an AST::Timeout exception is raised.
Variable Names
Variable names support Unicode.
Variable names can contain Unicode letters, numbers, and underscores.
Expected format:
[\p{L}_][\p{L}\p{N}_]*
Valid Variable Names
foo
_bar
数量
売上2025
Contribution
Bug reports and suggestions are welcome.
Licence
This software is released under the same terms as Perl itself. See the LICENSE file for details.
Author
john smith john.smith.7701@gmail.com