- Mark Farner
#===================================================================================================
# Example Script: If Statement
# Demonstrates the use of the 'if' statement in normal methods, compiler methods, classes and at
# the module level.
#===================================================================================================
data<string> const MyClassChoice = "wxyz"
# 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.
data<Weather> compiler CompilerWeather = Weather.sunny
# Compiler method compares incoming string and returns an int. Called from within class definition.
method<int> compiler CompilerClassOption(string ClassOptionStr)
{
data<int> ClassOptionId
# At compile-time, we can compare input string to enumeration "names".
if ( ClassOptionStr == "abc" )
{
ClassOptionId = 1
}
else if ( ClassOptionStr == "wxyz" )
{
ClassOptionId = 2
}
else
{
ClassOptionId = 3
}
return(ClassOptionId)
}
#---------------------------------------------------------------------------------------
# Compile-time logic at module level, where 'if' is allowed. This logic gets the string
# representation of a Weather enum, and checks to see if it matches a flag from the
# command line. It keeps checking each one until it finds a match, or 'else'.
#---------------------------------------------------------------------------------------
if ( CompilerIsFlag(CompilerEnumStr(Weather.sunny)) )
CompilerWeather = Weather.sunny
else if ( CompilerIsFlag(CompilerEnumStr(Weather.cloudy)) )
CompilerWeather = Weather.cloudy
else if ( CompilerIsFlag(CompilerEnumStr(Weather.rainy)) )
CompilerWeather = Weather.rainy
else
CompilerWeather = Weather.stormy
# 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.
if ( OptionStr == "go" )
OptionId = ProceedOption.go
else if ( OptionStr == "stay" )
OptionId = ProceedOption.stay
else if ( OptionStr == "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 along with a confidence factor from the shared class method.
StdIO.Write("Weather is " + CompilerWeather.Str() + " and final option is " + OptionId.Str())
Test.PrintConfidence()
}
# Class definition which uses compile-time logic to define a method.
class Test()
{
#-----------------------------------------------------------------------------------------
# Logic occurs at compile-time within class to create different shared method logic based
# on option. Obviously we don't need a separate method definition to handle the simple
# differences below, but hopefully it gets the point across.
#-----------------------------------------------------------------------------------------
if ( CompilerClassOption(MyClassChoice) != 3 )
{
method shared PrintConfidence()
{
StdIO.Write("Value from Test class suggest results are valid.")
}
}
else
{
method shared PrintConfidence()
{
StdIO.Write("Value from Test class suggest results might be invalid.")
}
}
}