Skip to content

Learn Dao in Y minutes

Limin Fu edited this page Sep 8, 2017 · 10 revisions

Learn X in Y minutes, Where X=Dao

# Learn X in Y minutes, Where X=Dao

# A number sign starts a one-line comment;

#{
    Adding a pair of { and a } makes it a multi-line comment;
#}

###################################################################
## Constant, Variable, Invariable and Type Alias Declaration;
###################################################################
const maxCount = 512        # Constant with initialization value;
var count = 128             # Variable with implicit integer type;
var text: string = "Hello"  # Variable with explicit string type;
invar message = text        # Invariable (immutable within the scope);
type MyStringType = string  # Type aliasing;

io.writeln( maxCount, count, text, message, MyStringType )

###################################################################
## Numbers:
###################################################################
var bnum1: bool    = true    # Boolean number with true value;
var bnum2: bool    = false   # Boolean number with false value;
var inum1: int     = 123     # Integer number;
var inum2: int     = 0x12d   # Integer in hexadecimal format;
var fnum1: float   = 123.45  # Floating pointer number;
var fnum2: float   = 12E-3   # Float in scientific format;
var cnum1: complex = 123C    # Complex number;
var cnum2: complex = 12.3C   # Complex number;

bnum1 = bnum1 and bnum2;
inum1 += inum2 % 128;  # Modulo;
fnum1 -= fnum2 * inum1;
cnum1 += cnum2 / fnum2;

io.writeln( bnum1, inum1, fnum1, cnum1 )

###################################################################
## Strings:
###################################################################
var astring  = "Hello"
var verbatim = @[]Verbatim text@[]
var html_source =
@[html:123456]
<body>
<span name="test"></span>
</body>
@[html:123456]
# Verbatim text can be included in Dao source code with
# a pair of special quotation strings "@[delimiter]".
# As long as "@[delimiter]" does not appear in the text body,
# any character can be used in the text.

io.writeln( astring + verbatim )

###################################################################
## Enum Symbols
###################################################################
# "enum" is a type that defines a set of symbols with associated
# integer values. It can be used to represent states and flags.

# State enum uses comma to separate entries:
enum Direction
{
    East,
    West,
    South,
    North,
}
var dir1: Direction = Direction::East
var dir2: Direction = $East
# The above two are equivalent. But the second one uses enum symbol
# which is more convenient in many cases. Enum symbol is defined by
# prefixing an identifier with a dollar sign. Its interpretation is
# determined by the target enum types. In this case, the target type
# is "Direction", so "$East" will be interpreted as "Direction::East".

# Flag enum uses semi-colon to separate entries:
enum RectSide
{
    East;
    West;
    South;
    North;
}
var freeSides = $East + $West        # Flag enums are combinable;
var freeSouth = $South in freeSides  # Flag checking;

# Direction and RectSide can also be defined as:
type Direction2 = enum<East,West,South,North>
type RectSide2  = enum<East;West;South;North>

io.writeln( dir1, freeSides, freeSouth, Direction2.East, RectSide2.East )

###################################################################
## Numeric Arrays
###################################################################
var vec1 = [1, 2, 3]    # array<int> vector (1x3 matrix);
var vec2 = [1.0; 2; 3]  # array<float> transposed vector (3x1 matrix);

var mat1 = [1.0, 2; 3, 4]         # array<float> 2x2 matrix;
var mat2 = [ [1, 2], [3, 4] ]     # 2x2 matrix
var mat3 = array{ 1.0, 2; 3, 4 }  # 2x2 matrix

mat2[0,:] += vec1[:2]  # Add vector to the first row (element-wise);
mat3 += mat1 * mat2    # Element-wise operations;
mat3 /= 5.0

io.writeln( vec1 )
io.writeln( vec2 )
io.writeln( mat1 )
io.writeln( mat2 )
io.writeln( mat3 )

###################################################################
## Lists
###################################################################
var list1 = { 1, 2, 3 }        # list<int>
var list2 = { 1.0, 2, 3 }      # list<float>
var list3 = { 1 : 1 : 5 }      # list<int>: { 1, 2, 3, 4, 5 }
var list4 = list{ 1 : 2 : 3 }  # list<int>: { 1, 3, 5 }

list4.append( 100 )
list4.push( 101, $front )
list4.push( 102, $back )

io.writeln( list1, list2 )
io.writeln( list3, list4 )
io.writeln( "Number of items:", %list4 )

###################################################################
## Maps
###################################################################
# A map with ordered keys can be created using "=>":
var map1 = { => }
var map2 = { "EE" => 5, "BB" => 2, "CC" => 3, "AA" => 1 } 

# A hash map with unordered keys can be created using "->":
var hash1 = { -> }
var hash2 = { "EE" -> 5, "BB" -> 2, "CC" -> 3, "AA" -> 1 }

# Alternative ways to enumerate maps and hash maps:
var map3  = map{ "ABC" => 123, "DEF" => 456 }
var hash3 = map{ "ABC" -> 123, "DEF" -> 456 }

# With explicit type, the initializing float keys are
# converted to integers automatically:
var map4: map<int,string> = { 12.3 => "abc", 45.6 => "def" }

io.writeln( %map1, %hash2, hash3, map4 )

# Get value by key:
var mapValue1 = map2["AA"]
var mapValue2 = hash2["BB"]

# Set value by key:
hash2["CC"] = 33
io.writeln( mapValue1, mapValue2, hash2 )

###################################################################
## Tuples
###################################################################
var tup01 = ( 123, "abc" )          # Tuple with unnamed items;
var tup02 = ( index = 123, "abc" )  # First item is named as "index";
var tup03 = tuple{ 123, name = "abc" }

io.writeln( tup01, tup02, tup03 )

var tup11 : tuple<int,int> = (1, 2)
var tup12 : tuple<index:int,name:string> = (123, "abc")
var tup13 : tuple<int,int,...> = (1, 2, 3, "abc")

io.writeln( tup11, tup12, tup13 )
io.writeln( "number of items:", %tup13 )

###################################################################
## Operations
###################################################################
var fvalue1 = 234.5
var fvalue2 = fvalue1 + 67       # Addition;
var fvalue3 = fvalue2 - 89       # Subtraction;
var fvalue4 = fvalue1 * fvalue2  # Multiplication;
var fvalue5 = fvalue1 / fvalue2  # Division;
var fvalue6 = fvalue1 % fvalue2  # Modulo;
var fvalue7 = fvalue1 ** 1.5     # Power;
# Same operations are supported for integers and arrays with integer
# or float elements. Such operations on arrays are element-wise.

var svalue1 = "abc"
var svalue2 = svalue1 + "123"         # String Concatenation;
var svalue3 = "/home/test" / svalue1  # Path Concatenation;

var bvalue1 = fvalue1 == fvalue2  # Equal;
var bvalue2 = fvalue2 != fvalue3  # Not Equal;
var bvalue3 = fvalue3 <  fvalue4  # Less Than;
var bvalue4 = fvalue3 >  fvalue4  # Greater Than;
var bvalue5 = fvalue4 <= fvalue5  # No Greater Than;
var bvalue6 = fvalue4 >= fvalue5  # No Less Than;

var bvalue7  = bvalue1 && bvalue2   # And;
var bvalue8  = bvalue3 || bvalue4   # Or;
var bvalue9  = ! bvalue5            # Not;
var bvalue10 = bvalue1 and bvalue2  # And;
var bvalue11 = bvalue3 or bvalue4   # Or;
var bvalue12 = not bvalue5          # Not;

var ivalue1 = 12
var ivalue2 = ivalue1 & 34       # Bitwise And;
var ivalue3 = ivalue2 | 56       # Bitwise Or;
var ivalue4 = ivalue2 ^ ivalue3  # Bitwise Xor;
var ivalue5 = ivalue3 << 3       # Bitwise left shift;
var ivalue6 = ivalue3 >> 3       # Bitwise right shift;

svalue2 += "xyz"   # String Concatenation Assignment;
svalue3 /= "def"   # Path Concatenation Assignment;

fvalue2 += fvalue1   # Addition Assignment;
fvalue3 -= fvalue1   # Subtraction Assignment;
fvalue4 *= fvalue2   # Multiplication Assignment;
fvalue5 /= fvalue3   # Division Assignment;
fvalue6 %= fvalue4   # Modulo Assignment

ivalue2 &= ivalue1   # Bitwise And Assignment;
ivalue3 |= ivalue1   # Bitwise Or Assignment;
ivalue4 ^= ivalue2   # Bitwise Xor Assignment;

ivalue3 = (int) fvalue2   # Conversion to int;
fvalue4 = (float) "78.9"  # Conversion to float;

ivalue4 = % svalue3  # Length of string;
# The length/size operator can also be applied to
# arrays, lists, maps and tuples;

###################################################################
## Control Constructs
###################################################################
# If conditional control:
if( fvalue7 < 10 ){
    io.writeln( "Value less than 10" );
}else if( fvalue7 < 100 ){
    io.writeln( "Value less than 100" );
}else{
    io.writeln( "Value no less than 100" );
}

# Range for loop control (by step of 1):
for(var i = 0 : 5 ){
    io.writeln( "Range for looping:", i )
}
# Range for loop control (by step of 2):
for(var i = 0 : 2 : 5 ){
    io.writeln( "Range for looping:", i )
}
# List iteration for loop:
for(var i in { 1, 2, 3 } ){
    io.writeln( "For-in looping:", i )
}
# C-style for loop;
for(var i=0; i<5; ++i){
    io.writeln( "C-style for looping:", i )
}

# While loop:
var step = 0
while( step < 5 ){
    io.writeln( "While looping:", step );
    step += 2;
}

# Do-While loop:
do {
    step += 1;
    io.writeln( "Do-while looping:", step )
} while( step < 9 )

# Switch-case:
switch( bvalue1 ){
case true : io.writeln( "Value is true" );
case false: io.writeln( "Value is false" );
}

switch( dir1 ){
case $East : io.writeln( "Direction is east" );
case $West : io.writeln( "Direction is west" );
case $South: io.writeln( "Direction is south" );
case $North: io.writeln( "Direction is north" );
}

Clone this wiki locally