- Joe Walsh
#===================================================================================================
# 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())
}