Neatware Header

Ladybug
StudioXP
Video Control

PlayerXP
MPEG,AVI,VCD

Snaml
Tcl
XHTML,WML

C#
.NET,XML,ASP

Java
J2EE,XML,JSP

MyrmecoX
Professional
Tcl/Tk IDE

Enterprise
Server,Terminal


Control

Sequence, Condition, Loop, Return, Exception

  1. Sequence

    Sequence control organizes the command flow in sequence. Each commands is executed one after another. In Snaml all the block commands for HTML and XML are sequence. Following paragraphs introduce primitive sequence commands. They are also the Tcl commands.

    • set

      set command assigns value to a variable. Its first argument is a variable and its second argument is an expression. You can use a variable anywhere without declare its type. A variable with dollar sign $ returns its value

      set v "a string value"
      set u $v
      
    • incr

      The first argument of incr command is a variable name. The second optional argument is increment integer. incr command increases (decreases) variable value by increment. The default value of increment is 1.

      incr x               # x increase 1
      incr x -1            # x decrease 1
      incr x [expr 2*3]    # x increment
      
    • expr

      expr command evaluates a math expression with C expression syntax. It concatenates all the arguments to form a input string. The expression may be integer, floating point, and boolean. The return value is a numeric string. Many basic math functions in the standard C math library have been built in.

      expr 2/3                      # return 0 (why?)
      expr 2.0/3.0                  # 0.666666666667
      expr asin(1.0)*round(sqrt(2)) # 1.579079632679
      
    • # Comment

      comment in the Snaml starts with # in the start of a line. You'd better use comment # as a command. There are some quirks prohibited you from writing comment anywhere. (i.e. no inside the switch command.

      set x 6; # comment here. ; new command
      # comment start
      
    • quote

      quote command outputs its argument string to a IO channel. The IO channel is defined by __output command. It maybe a file or stdout. It is a special start command with only one argument and no tags.

      quote "output a $string and a [compute $value]"
      
  2. Condition

    Condition control may select a command to execute according to the variable value. It branches the command flow.

    • if

      if command will execute truebody when the expression is true, otherwise it will execute elsebody. Its body is a group of commands. The else and elseif keyword are optional for if command. Following codes are several forms of if command

      if {expression} {truebody}
      if {expression} {truebody} else {elsebody}
      if {expression1} {
        truebody1
      } elseif {expression2} {
        truebody2
      } else {
        elsebody
      }
      
    • switch

      The syntax of switch command is:

      switch option value pattern body ... 
      switch option value {pattern body ...}
      

      switch command compares a value with patterns. If one of them is matched then program executes the related body. The first argument of the switch command is an option. The '-exact' attribute will match the value to the pattern exactly; '-glob' attribute will use glob pattern matching; and '-regexp' will match with regular expression pattern. '--' represents the end of the option. The last pattern 'default' will execute its body if no patterns are matched before.

      In the following example, since pattern and body pairs are grouped into an argument, there is no substitution inside the pattern/body pairs.

      switch -exact -- $val {
        first {doFirst}
        second {doSecond}
        third {doThird}
        default {doDefault}
      }
      

      following example can substitute its patterns

      switch -glob -- $val  $v1 do_v1  $v2 do_v2  $v3 do_v3
      
  3. Loop

    Loop commands execute a group of commands in iteration. The iteration may terminate after all the elements are traversed or a condition expression becomes true.

    • foreach

      foreach command repeatly executes its body until all the elements in a list have been traversed. Its form is,

      foreach var alist ... body
      

      The var is the current loop variable that is assigned an element from the alist one after another. foreach will traverse all the elements in the alist. This command is a compact expression of iteration.

      foreach v {a b c d e} {
        quote $v
      }
      

      You can declare two or more loop variables. The variables will orderly sample the elements in the list until all of them are traversed. Following example shows that varaiable (v1 v2) pair is assigned the value (a b) respectively, and then the value (c d), and so on.

      foreach {v1 v2} {a b c d e f} {
        quote "($v1 $v2)"
      }
      

      To loop over multiple lists, you may organize arguments in var/list pair order. The variable var may also be multiple variables. A loop variable will be set to empty {} when its list has finished traverse but the entire loop did not terminated.

      foreach v {a b c d} {v1 v2} {1 2 3 4 5 6} {
        quote "($v) ($v1 $v2)"
      }
      
    • while

      while command evaluates the expression, if it is true then executes the body, and then evaluates the expression again until the expression is not true. Its syntax is like while statement of C language.

      while expression body
      

      An example of while command is,

      set count 7 
      while {$count > 0} { 
        quote "2*$count" 
        incr count -1 
      }
      
    • for

      The for command syntax is,

      for initial expression increment body
      

      At first it evaluates the initail argument and then evaluates the expression. If the expression is true it executes the body and increment. Repeatlly, it evaluates the expression again and continues the loop until expression returns a false value.

      set len 7
      for {set count 0} {$count < $len} {incr count 1} {
        quote "2*$count"
      }
      
  4. Return

    return command comes back from a procedure with a value; break command exits from a loop; and continue command will goto the start of a loop to execute the next iteration.

    set b 6
    set c 5
    set len 7
    for {set count 0} {$count < $len} {incr count 1} {
      if {$count == $b} {
        break
      } elseif {$count == $c} {
        continue
      } 
    }
    return $c
    

  5. Exception

    Exceptions raise abnormal conditions during the execution of commands.

    • Catch

      catch command caught the exceptions of a command during its execution. Its syntax is,

      catch {command args ... } result
      

      catch command sets trap to the command in the curly braces. When there is an exception during the command execution, the exception message is assigned to the variable 'result', otherwise the 'result' gets the return value of the command. catch command returns zero when no exception is raised, otherwise returns non-zero.

      if [catch {test $exception} result] {
        quote "Exception: $result"
      } else {
        quote "OK: $result"
      }
      
    • Error

      error command generates an error code. Its first argument is a string that indicates the reason of an error

      catch {...} errmsg
      error $errmsg