Rulz Programming Language

Rulz Data

The following types are supported: boolean, integer, float, string, list, hash and the PHP types NULL and handle (resource).

Rulz introduces several other types such as file and argument — which are similar to barewords. See #Argument Literals.


Basic variables are like $var where "var" is one or more lowercase letters, and, PHP-like, can be assigned any type. Variable assignment is Bash-like with the $ not used.

Like Bash and Perl all variables are global and there are "my" and "local" attributes for subroutines.

There are Perl-like special variables of the format: $[0-9[:punct:]]. The special variables $0 and $_, differ from their Perl counterparts and are used as defaults for most Builtins and Commands and for some Functions.

See Variables for Rulz special variables.

Argument Literals

TRUE                    boolean TRUE
FALSE                   boolean FALSE
NULL                    NULL
CONST                   constant
123                     decimal
0.123                   float
1e4                     float
0123                    octal
0x1A                    hexadecimal
1Ah                     hexadecimal
0b0001                  binary
0001b                   binary
(a,b,c)                 list
(a b c)                 list
(a=1,b=2,c=3)           hash
{a,1,b,2,c,3}           hash
simple                  bareword
-a --help               program argument
lib/test.php            file name 
*.txt                   file glob - list of matching files
\n \t                   escape sequence
"" ''                   empty string
"double quotes"         string; variables, escape sequences expanded
'single quotes'         string; variables not expanded; escape sequences \' and \\

The hexadecimal extension (trailing [hH]) must have a leading digit.

Some Extensions

14:25'36"               sexagesimal
2%                      percent

Argument Interpolation

Variables can be arguments by themselves or within double quotes. As arguments they are passed by value. To pass by reference the notation is \$var. (The use of $ is to avoid ambiguities with escape sequences, allowing for single letter variables.)

$var                    variable
\$var                   reference

References are like in PHP and not like Perl references.

Variable variables are sort of supported.

=a b                    assign $a with 'b'
+=$a                    increments $b ($a is interpolated to 'b')
^$b                     displays "1"

String Interpolation

= var foobar
$var                    'foobar'
$[0]var                 'f'   (index)
$[-1]var                'r'   (rindex)
$[0:3]var               'foo' (sub string)
$[3:]var                'bar' (sub string)
$[0,3]var               'fb'  (index concatenation)

String Expansion

String expansion has been based on Bash shell expansion.

= foo foobar            $foo = "foobar"
${foo}                  same as $foo        "foobar"
${#foo}                 length              6
${^foo}                 uppercase first     "Foobar"
${^^foo}                uppercase           "FOOBAR"
${,foo}                 lowercase first     "foobar"
${,,foo}                lowercase           "foobar"
${[0]foo}               character index     "f"
${[5]foo}               character index     "r"
${[6]foo}               character index     "r"
${0:3:foo}              substr              "foo"
${3::foo}               substr              "bar"
${fo#foo}               delete from start   "obar"
${ar%foo}               delete from end     "foob"

List Constructors

Lists can have any types, even other lists though not explicitly. List constructors have four formats.

()                      empty list
(1,2,3)                 list of three integers
(1 2 3)                 same
(1..3)                  same

Non-integer elements and variable interpolation is supported.

(a..z)                  letters 'a' through 'z'
($a,$b)                 interpolation

Nested lists can occur through interpolation.

= a (a,b)         
= b ($a,c)              (('a','b'),'c')

List Assignment

List elements can be assigned, either implicitly.

= []a 1
= []a 2

Or explicitly.

= [0]a 1
= [1]a 2

List Interpolation

= var (a b c)
^ $var                  result is ('a','b','c')
$[0]var                 'a'   (index)
$[-1]var                'c'   (index)
$[0:1]var               (a,b) (slice)
$[0,3]var               (a,c) (index concatenation)

Hash Constructors

Hashes can have any types, even lists and other hashes though not explicitly. Hash constructors have two formats.

(a=1,b=2,c=3)           hash with three key/value pairs
{a,1,b,2,c,3}           same

Keys can only be of lowercase letters and quotes are not used. A hash cannot be empty, as it will then be an empty list.

= foo (a='b')           hash 'foo' element 'a' set to 'b'
= bar (a=$foo)          hash 'bar' element 'a' is hash 'foo'

Hash Assignment

Like lists hash elements can be assigned.

= [a]foo b              hash 'foo' element 'a' set to 'b'
= [b]foo (a,b)          hash 'foo' element 'b' set to ('a','b')

Hash Interpolation

Hashes are also $ variables.

= var {a,1,b,2,c,3}
$[a]var                 1     (index)
$[a:b]var               (1,2) (slice)
$[a,c]var               (1,3) (index concatenation)

Hash slices and index concatenation result in lists.

Lists and hashes can be combined in the PHP way.

= [0]a 1
= [1]a 2
= [a]a a
= [b]a b                result is ( 0 = 1, 1 = 2, a = a, b = b )
There are ways to construct complex lists and hashes, with complex string keys for example...

Escape Sequences

\n                      linefeed
\r                      carriage return
\t                      tab
\e                      escape
\\                      backslash
\$                      dollar sign
\"                      double quote
\'                      single quote (within a single quoted string)
\@                      to escape parameter variables

Escape Extensions

\N                      null
\T                      true
\F                      false

Though not within double quoted strings.

Perl-like Escape Sequences

\l                      lowercase next character
\u                      uppercase next character
\E                      end \L \U \q \Q
\L                      lowercase to \E
\U                      uppercase to \E
\q                      single quote to \E
\Q                      double quote to \E

Quote Sequences

Perl's quote-like operators are supported with extensions.

q/string/               like single quotes
qq/string/              like double quotes
qw/string/              like list
qx/string/              like exec
qm/string/              quote meta
qs/string/              wrap string in single quotes
qd/string/              wrap string in double quotes
^ qs/have "3" 4's/      same as ^ "'have \"3\" 4's'"
^ qd/have "7" 9's/      same as ^ "\"have \"7\" 9's\""

Unlike Perl both interpolate variables and some escape sequences.