Difference between revisions of "LavishScript:Script Syntax"
Line 3: | Line 3: | ||
All scripts must have a function called '''main'''. The '''main''' function will be executed when the script is run, and any extra parameters given to the RunScript command will be passed to the function. | All scripts must have a function called '''main'''. The '''main''' function will be executed when the script is run, and any extra parameters given to the RunScript command will be passed to the function. | ||
+ | |||
+ | Scripts ''may'' have a function called '''atexit'''. The '''atexit''' function will be executed when the script ends for any reason (be it ending naturally, a script error, endscript command, etc) and is perfect for clearing up any UI elements, binds, aliases, global variables, etc. | ||
=== Preprocessing === | === Preprocessing === |
Revision as of 01:36, 21 June 2005
Contents
Overview
Scripts consist of comments, functions, code blocks, commands and control structures.
All scripts must have a function called main. The main function will be executed when the script is run, and any extra parameters given to the RunScript command will be passed to the function.
Scripts may have a function called atexit. The atexit function will be executed when the script ends for any reason (be it ending naturally, a script error, endscript command, etc) and is perfect for clearing up any UI elements, binds, aliases, global variables, etc.
Preprocessing
Before a script is used, it is sent through a preprocessor. The preprocessor's job is to take text (a single script file), and replace, remove, or insert parts, including other files. For example, the preprocessor allows the creation of entire sections of code that are conditionally used in the final version used by the system. Preprocessing directives and macros are recognized only by the preprocessor, not by the actual script or command interpreters.
Comments
Comments must be on their own line, never at the end of another line. Any line that starts with a semi-colon (;) is considered a comment, and will not be handled by the script.
Functions
Functions are defined like so:
function MyFunc1() { commands } function MyFunc2(parameter1,parameter2,parameter3) { commands } function MyFunc3(string parameter1,int parameter2) { commands }
Functions may have any number of parameters, and each will default to the string type if a type is not given. Each function must have its own code block. The function ends either at the final }, or from a return command. Either way, execution proceeds after the previous call statement, or ends the script if the function is main.
Code Blocks
Code blocks are defined by having a { to begin, and } to end, on their own lines. These blocks are used to define the scope of a function, and are also used by certain commands such as If and While.
Commands
Commands are simply LavishScript commands, as may be entered in a console or command file.
Control Structures
Some commands available only to scripts change the flow of execution of the script. For example, a command may cause a block of code to repeat, or only be executed in a certain condition. These commands along with the code blocks they affect make up control structures.
Conditional
Repetitive
Selective
Examples
Example 1
Obligatory "Hello World" script
function main() { echo Hello World }
Example 2
A fairly pointless script that demonstrates looping, functions, variables, data methods
function MyFunction(int Count) { return -${Count} } function main() { Declare Count int 1 do { call MyFunction ${Count} echo Count: ${Count} ${Return} } while "${Count:Inc}<=10" }