Skip to content

Features

Greg Tourville edited this page Jul 21, 2015 · 1 revision

Features

Duck supports integers, floats, strings, and references. It also supports first-class functions and automatic memory management. Any implementation of the run time must also implement the functions duck.print and duck.println to be valid.

Pretty simple, huh?

Syntax

Duck version 0.1

A program is made up of a list of statements. Statements may be one of the following:

An import statement.
import <namespace>

Imports all elements of the given reference into the global namespace. For example:

import duck

After the example statement is executed, it is valid to use the functions print and println without a prefix.

An if or if-else or if-else-if statement.

An if statement without an else block looks like this.

if <value> then
    <statements>
end

An if statement with an else block looks like this.

if <value> then
    <statements>
else
    <statements>
end

An if statement with an else-if looks like the following.

if <value> then
    <statements>
else if <value> then
    <statements>
end

And an if statement with any number of else-if branches and an else statement looks like this.

if <value> then
    <statements>
else if <value> then
    <statements>
else if <value> then
...
else
    <statements>
end
A function definition.

A function declared without any parameters looks like either of the following.

function <name>
    <statements>
end

or

function <name> ()
    <statements>
end

A function that is declared with parameters must include the list of arguments, separated by commas.

function <name> (<parameter1>, <parameter2>, ...)
    <statements>
end
A return statement.
return <value>

or simply

return

Exits a function and optionally returns a value. Used without a value, the function returns 'nil'.

A while loop.

A while loop continues executing the body of the loop while the condition is true.

while <condition> do
    <statements>
loop
A for loop.

A for loop assigns a local variable each of the values from start to end, including end, and executes the body statements for each of these values.

for <variable> = <start> to <end> do
    <statements>
loop
A break statement.

A break statement exits a loop.

break
A continue statement.

A continue statement begins the next iteration of a loop.

continue
An expression.

An expression consists of one or more variables, constants, references, or function calls combined with use of the operators

+, -, *, /, and, or, not, !, <, >, <=, >=, !=, ==,

or parentheses, where

+, -, *, /, and, or, not, <, >, <=, >=, !=, ==,

are binary operations combined as <term> + <term> and

-, !, (, )

are unary operators invoked as !<term>, -<term>, or (<term>).

Expressions can be used as their own statements or must be used as a value in any other statement that expects a value.

An assignment
<assignee> = <value>

Assigns the value of an expression to a named variable or reference.

An empty line.

Self-explanatory really. In general, there must be a newline at the end of each statement.

Dictionaries and References

An array is assigned as a list of expressions in-between square brackets. As in

arr = [1, 2, 3, 4]

Here the values of arr[0] would be 1, arr[1] would be 2, and arr[3] would 3. Additionally, an empty array can be created using the [] expression, as so

arr = []

Elements can be assigned to an empty array without specifying the size. In addition to integers, any other non-nil type can be used to index an array.

Dictionaries are specified in much the same way. Depending on the platform, dictionaries are declared with either square or angled braces and with a list of key-value pairs.

e.g.

dict = {a: 1, b: 2, c: 3}

or

dict = [a: 1, b: 2, c: 3]

Dictionaries and arrays are both implemented as the reference type, and as such, once initialized either type can be addressed with the same syntax. Dictionary elements are addressed as follows:

dict.a
dict.b
dict.c

In an example of an assignment,

dict.d = arr

Duck version 0.2

In version 0.2, the keyword mod has been added as a binary operator.

The import statement has been enhanced to allow lists of identifiers to be imported into the global namespace. Additionally an include statement has been added which allows for including source code from separate modules.

Proposed improvements include adding the self-assignment operators +=, -=, /=, and *=. The post-increment and post-decrement operator, x++ and x--, have also been proposed. These will most likely be added as statements, as in instructions that must appear on their own line.

The PC implementation of DPL or the Duck Programming Language includes an additional built in function, string.split, which returns an array of strings containing each character of the parameter string.

Duck Programming Language

Clone this wiki locally