[NAME] ALL.dao.tutorial.basics [TITLE] Basics [DESCRIPTION] This tutorial will cover some basics of Dao programming. We will start with the traditional hello world example. 0.1 Hello World! To simply write "Hello world!" to the screen, one can use, 1 io.write( 'Hello world!' ) In this simple example, the built-in io module is used to access the IO functionalities of the module. write() is a method provided by the io module to write outputs on to the standard output (the screen in this case). In Dao, string can be quoted with a pair of single quotation marks or with a pair of double quotation marks. Semicolons are optional at the end of statements. 0.2 Running Scripts There are three ways to run codes in Dao. The most typical way is to create a script file (e.g. hello_world.dao), and put the codes into this file, and then run dao using this file as argument, 1 $$ dao hello_world.dao But if you just want to run the codes once, you can simple run them from command line using, 1 $$ dao -e "io.write( 'Hello world!' )" For learning Dao, it may be the best to run codes in interactive mode. Invoking dao without any argument will start an interactive shell, 1 $$ dao 2 3 Dao Virtual Machine 2.0 4 Built date: Jun 16 2013 5 Changeset ID: FOS.a019d384dd7c 6 7 Copyright(C) 2006-2013, Fu Limin 8 Dao is released under the terms of the Simplified BSD License 9 Dao Language website: http://www.daovm.net 10 11 (dao) 0.3 Accessing Online Help If you have the Dao help properly installed, starting dao in interactive mode will automatically load the help files and print out the following information, 1 Module Help is loaded. 2 Now you may run "help()" to list all the available help entries; 3 or run "help('help')" for detailed information about the help system. If you are reading this tutorial from a webpage, and want to access it from the interactive command line, you can simple run, 1 (dao) help( 'dao.tutorial.basics' ) Such online helps come in two languages: English and Chinese. Currently the English version is more complete. To choose the language, one can define an environment variable: 1 DAO_HELP_LANG = EN # for English; 2 DAO_HELP_LANG = ZH # for Chinese; Or simply run Dao with, 1 DAO_HELP_LANG=EN/ZH dao ... 0.4 Commenting Codes It is always a good idea to comment your codes. Dao supports both single line comments and multiple line comments. A single comment starts at a # (not followed by a left curly bracket {), and ends at the end of the current line. And a multiple line comment (or just part of a single line) opens by #{ and closes by #}. For example, 1 # This is a simple demo: 2 io.write( "Hello World!" #{ comment inside codes #} ); 3 #{ 4 Here are multi-lines comments. 5 Here are multi-lines comments. 6 #} 0.5 Declaring Constant and Variables Dao supports the explicit declaration of constants, local variables and global variables. Constants are declared with keyword const, 1 const DEFAULT_INDEX = 123 2 const DEFAULT_NAME = 'abc' Constants can only be initialized with constant expression. Constants that are declared at the top lexical scope are global constants. Global constants can also be declared inside nested lexical scopes by preceeding the const keyword with global. Keyword global will declare global variables, 1 global current_index = 456 2 global current_name = 'def' By default, in Dao all variables are implicitly declared as local variables. When a variable need to be explicitly declared as local, to avoid messing with global variables or local variables in the upper lexical scopes, one can use keyword var, 1 var temp_index = 789 2 var temp_name = 'ghi' Note: in interactve mode, all variables are automatically declared as global variables.