Aztec® Programming Language
Version 1.1 Alpha 2

Copyright © 2010-2017, Aztec Development Group, All Rights Reserved

Download Aztec

Search        Contact Us

Time to open fire...

- Joe Walsh

 

The Branch statement provides a mechanism to conditionally execute a single block of statements based on the value of an expression. The following table shows the syntax for the Branch statement.

Aztec Branch Statement Syntax

 

♦ The 'branch' statement compares the expression against the list of values in the 'when' clauses. It goes top to bottom through the statement looking for a match in each 'when' clause, and executes the first block of statements where a match is found. It then exits the branch statement after the block is executed. Only one block is executed.

♦ The expression can be any primitive value (int, float, string or bool) and any type of user defined enumeration. A primitive reference ('@') is not allowed.

♦ Any number of separate 'when' blocks can be used, but at least one is required.

♦ The ‘else’ block is optional, and it is executed only if no match is found in any of the 'when' clauses.

♦ The curly braces for the branch statement ( '{' and '}' ) must be used, even if only a single 'when' clause is used.

♦ Each 'when' clause can contain any number of separate expressions inside the parentheses, each delimited by a comma (Value1, Value2, etc.). The data type for each expression must be compatible with the main expression specified on the 'branch' statement. An expression in the 'when' clause does not need to be an expression. Any valid expression can be used, including the use of method calls.

♦ The ‘when’ clause also supports a range of values (Value1..Value2) which returns a match if the value falls within the specified range.

♦ A range goes from a low limit to a high limit, inclusive, and can be any primitive data type or user defined enumeration that is compatible with the main branch expression.

♦ The 'branch' statement can only be used inside a "compiler" method or a "normal" method.

♦ An example script showing Aztec 'branch' statements inside a 'compiler' method and inside a normal (run-time) method.

#===================================================================================================
# Example Script: Branch Statement
# Demonstrates the use of the 'branch' statement in normal methods and compiler methods.
#===================================================================================================

data<string> const MyWeatherChoice = "sunny"

# Enumerations to match OS command line 'flags' at compile-time and 'arguments' at run-time.
enum Weather { sunny, rainy, cloudy, stormy } # Used at compile-time for OS 'flag' option
enum ProceedOption { go, stay, retreat, surrender } # Used at run-time for OS '-arg' option

#-------------------------------------------------------------------------------------------
# Compiler variable used during compile logic but also carries "weather" over to run-time.
# The value is set by calling a separate compiler method using input string.
#-------------------------------------------------------------------------------------------
data<Weather> compiler CompilerWeather = CompilerSetWeather(MyWeatherChoice)

# Compiler method to return "weather" enumeration based on incoming string.
method<Weather> compiler CompilerSetWeather(string WeatherStr)
{
data<Weather> MyWeather
data<int> shared MethodCallCount = 0

CompilerInc(@MethodCallCount)

#----------------------------------------------------------------------------
# Select the return weather based on incoming string. All of the 'when'
# constructs uses compiler methods to determine the value(s) to be compared.
#----------------------------------------------------------------------------
branch ( WeatherStr )
{
when ( CompilerEnumStr(Weather.sunny) ) # "sunny"
MyWeather = Weather.sunny
when ( CompilerEnumStr(Weather.cloudy) ) # "cloudy"
MyWeather = Weather.cloudy
when ( CompilerEnumStr(Weather.rainy), CompilerGetOptionString(MethodCallCount) )
MyWeather = Weather.rainy
else
MyWeather = Weather.stormy
}

return(MyWeather)
}

# Compiler method returns a string given an incoming integer using 'branch'.
method<string> compiler CompilerGetOptionString(int MethodCallCount)
{
data<string> ClassOptionStr

# Use 'branch' statement to determine the appropriate return value.
branch ( MethodCallCount )
{
when ( 1, 2, 3, 6..10 )
{
ClassOptionStr = "100"
}
when ( 21, 25..100 )
{
ClassOptionStr = "200"
}
else
{
ClassOptionStr = "300"
}
}

return(ClassOptionStr)
}

# Run-time entry point for the script.
method Main()
{
data<string> OptionStr = GetScript().GetArg(1).Lwr()
data<ProceedOption> OptionId

# At run-time, choose option id based on lower case of 1st command line arg.
branch ( OptionStr )
{
when ( "go" )
OptionId = ProceedOption.go
when ( "stay" )
OptionId = ProceedOption.stay
when ( "retreat" )
OptionId = ProceedOption.retreat
else
OptionId = ProceedOption.surrender
}

# Possibly modify option based on weather from compile-time logic and class method.
if ( CompilerWeather == Weather.stormy )
{
OptionId = ProceedOption.surrender
}

# Print the result based on above logic.
StdIO.Write("Weather is " + CompilerWeather.Str() + " and final option is " + OptionId.Str())
}

Page UpPage DownCopyright © 2010-2017
Aztec Development Group
All Rights Reserved

Download Aztec