DL/SQL

Syntax

How does it look like?

The syntax was inspired by LISP and bash. Sounds strange? For example,

(* (+ 2 4 (#FACT 4)) 3)

means (2 + 4 + fact(4)) * 3 where fact is user-defined function for factorial.

Basics

As you've probably noticed, DL/SQL has got prefix (polish) notation with compulsory bracketing. That means, in front any name of function there must be opening bracket and after last parameter of a function there must be closing bracket. With this rule it's really easy to implement variable-length argument lists.

Datatypes

As for now, DL/SQL supports only two types: float and list of floats (no lists of lists, etc.).

Lists syntax

There's special syntax of lists in DL/SQL. The regular (yes, we need just regular expression with simple lists) expression describing the list is:

{\(-?[0-9]*\(\.[0-9]+\)\([+-]?\([eE][0-9]+\)?:\)*}

Well, that doesn't help much, so:
  • empty list is {}
  • list with one member (10) is {10:} (the colon is compulsory)
  • list with three members (10, 20 and 30) is {10:20:30:}
  • as far we've simple lists, the list with one member is equal to the value of the member. Ie. the value of (+ {10:} {20:} 30) is 60.

Boolean values

When evaluating logical functions, the input value of TRUE is every non-zero value. Input value of FALSE is 0, 0.0, an empty list and list with one zero member ({0:}). Output value of TRUE is 1 and output value of FALSE is 0, meaning that result of (* 3 (+ (> 3 2) 1)) is 6.

Hardcoded functions

Here follows list of functions implemented in DL/SQL.

Basic nameAliasesParametersDescription
Arithmetic functions
+ 2+Add
- 2+Substract
* 2+Multiply
/ 2+Divide
% 2+Modulo
INT 1Integer part of number
Comparation functions
> 2+Greater than
>==>2+Greater than or equal
===2+Equal
!=<>2+Not equal
<==<2+Less than or equal
< 2+Less than
Logical functions
! 1Not
&& 2+Logical AND
|| 2+Logical OR
Bit functions
&AND2+Binary AND
|OR2+Binary OR
^XOR2+Binary XOR
List functions
~HEAD1List's Head
_TAIL1List's Tail
LEN 1List's Length
EMPTY 1List's Emptiness
@INDEX2Return n-th member of a list
:CAT2+Concatenate lists
Misc functions
?IF3Condition
ECHO 3Output
$ 2Parameter selection

Condition

The only function with strict evaluation strategy is IF (form of ? is also acceptable). If first parameter equals true the second is returned, otherwise the third one is returned.

Calling user functions

I wanted the parser to be the simplest one, so there's little hack about calling custom functions. Every name of called user function must have prepended the hash sign (#). So calling custom function called QUICKSORT is done like this:
(#QUICKSORT {10:20:30:})
Please notice the fact that function names in database are without hash signs.

Defining user functions

Because the idea of putting everything in the database, we don't need to have semantic construction for defining custom function - we simply put another row into database with correct definition of function body and that's it. :-)
For referencing parameters in function's definition, we can use several ways to do so:

Bash-like parameters

First parameter is referenced as $1, second as $2 and so on. Parameter $0 contains number of parameters passed to function.

Passing all parameters

There's special construction for passing all parameters passed to function to another function. This construction is made through $*.

Selective parameter passing

This one is little hack: sign $ can be taken as a special function. Its parameter is list of parameter indexes, which should be pushed into variable stack. Following example clears the situation up:

Lets say we have function called #SUMPARS246 defined as (+ ($ {2:4:6:})). If we call the function with paremeters (#RETPARS246 10 20 30 40 50 60 70), its result will be 20+40+60 = 120. (sum parameters 2, 4 and 6)

(the fact that $3 = ($ 3) = ($ {3:}) is not coincidence at all)