- Lennon and McCartney
#===========================================================================================
# Example Script: WhyCompileTime
# Demonstrates the use of compile-time logic and creates array dynamically during compile.
#===========================================================================================
#----------------------------------------------------------------------------------------
# Compiler variables to hold the array initialization code and name of the actual array.
# The code will ultimately create a global array variable named 'SimpleClassArray'.
#----------------------------------------------------------------------------------------
data<bool> compiler FileSuccess = false
data<string> compiler DataFileName = "AztecTest.txt"
data<string> compiler ClassArrayName = "SimpleClassArray"
data<string> compiler FullArrayInitCode = "public data<SimpleClass[]> " + ClassArrayName + " = { "
# Process data file at compile time and build up the array initialization code.
FileSuccess = CompilerProcessDataFile(DataFileName)
if ( FileSuccess )
{
# Dynamically compile the code to create and initialize the 'SimpleClassArray' array.
CompilerLoadModule(FullArrayInitCode,false)
}
else
{
CompilerWriteLog("Error reading data file '" + DataFileName + "'. Compile process was aborted.")
CompilerAbort()
}
# Compile-time method to read file, parse each record into two strings, and create array initialization string.
method<bool> compiler CompilerProcessDataFile(string FileName)
{
data<int> FileId
data<int> NumRecords = 0
data<bool> Success = false
data<string> Record
data<string> RecArrayInitCode
# Open the text file for processing and check for success.
FileId = CompilerOpenFile(FileName)
if ( FileId > 0 )
{
# Loop through entire text file and process each record one at a time.
while ( !CompilerEndOfFile(FileId) )
{
CompilerInc(@NumRecords)
# Read the record, parse it into two items, and create the 'new<>' code for this record.
Record = CompilerReadFile(FileId)
RecArrayInitCode = "new<SimpleClass('" + CompilerStrToken(Record,1) + "','" + CompilerStrToken(Record,2) + "')>"
# If we're not first line, precede with comma to separate from previous record.
if ( NumRecords > 1 )
{
RecArrayInitCode = "," + RecArrayInitCode
}
# Add the record level code to the full string of code.
CompilerStrAdd(@FullArrayInitCode,RecArrayInitCode)
}
# If we have at least one record, set success flag and append final '}' to array init code.
if ( NumRecords > 0 )
{
Success = true
CompilerStrAdd(@FullArrayInitCode,"}")
}
}
return(Success)
}
# Main is the script's run-time entry point. Sit in a loop and dump out array contents.
method Main()
{
data<int> Count
data<SimpleClass> MyClass
# Loop through array that was created and initialized "dynamically" at compile-time.
iterate ( Count in 1..SimpleClassArray.Size() )
{
# Get this object from the array and write it to console.
MyClass = SimpleClassArray[Count]
StdIO.Write(MyClass.Name1() + "," + MyClass.Name2())
}
}
# Simple example class to store contents of a single text record.
class SimpleClass
{
method SimpleClass(string Str1, string Str2)
{
Name1 = Str1
Name2 = Str2
}
method<string> Name1()
{
return(Name1)
}
method<string> Name2()
{
return(Name2)
}
data<string> Name1
data<string> Name2
}