Difference between revisions of "LavishScript:Script Syntax"

From Lavish Software Wiki
Jump to navigation Jump to search
 
(24 intermediate revisions by 2 users not shown)
Line 1: Line 1:
== Overview ==
+
#REDIRECT [[LavishScript:Language and Engine Overview]]
Scripts may consist of <tt>comments</tt>, <tt>functions</tt>, <tt>code blocks</tt>, <tt>commands</tt>, <tt>control structures</tt> and <tt>object definitions</tt>.
 
 
 
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 [[LavishScript:Preprocessor|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 (<tt>;</tt>) is considered a comment, and will not be handled by the script (comments are actually removed by the preprocessor).
 
 
 
== Functions ==
 
Functions are defined like so:
 
function MyFunc1()
 
{
 
  ''commands''
 
}
 
function MyFunc2(parameter1,parameter2,parameter3)
 
{
 
  ''commands''
 
}
 
function MyFunc3(string parameter1,int parameter2)
 
{
 
  ''commands''
 
}
 
function MyFunc4(string parameter1,int parameter2=10)
 
{
 
  ''commands''
 
}
 
 
 
Functions may have any number of parameters, and each will default to the string type if a type is not given.  Each parameter can also have a "default" value, denoted by an = followed by the value.  If no default is given, the default "blank" value for its type is used (for example, 0 for numbers or empty string for strings).  If a parameter is not passed to the function in the call statement, its default value will be used. 
 
 
 
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 [[Command:If|If]] and [[Command:While|While]].  Code blocks can be used freely within functions, and ''do not'' modify variable scope within functions (i.e. variables declared within a local code block still exist after the code block, and must not be re-declared in a loop!).
 
 
 
== Commands ==
 
Commands are simply LavishScript commands, as may be entered in a console or command file.  See [[LavishScript:Syntax|LavishScript Command Syntax]] for LavishScript command syntax rules.
 
 
 
== 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 <tt>control structures</tt>.  See [[LavishScript:Mathematical Formulae|Mathematical Formulae]] for insights on how "conditions" are processed (they are simply math!).
 
 
 
=== Conditional ===
 
* [[Command:If|If]]..[[Command:Else|Else]]
 
=== Repetitive ===
 
* [[Command:Do|Do]]..[[Command:While|While]] ([[Command:Break|Break]], [[Command:Continue|Continue]])
 
=== Selective ===
 
* [[Command:Switch|Switch]]..[[Command:EndScript|EndScript]] ([[Command:Break|Break]], [[Command:Case|Case]], [[Command:Default|Default]])
 
 
 
== Object Definitions ==
 
Object definitions allow the creation of custom datatypes within a script.  Object definitions consist of an "objectdef" block which may contain "member" lines as well as functions.  Object definitions ''cannot'' provide methods.  Objects are defined like so:
 
objectdef mytype
 
{
 
  ''members and functions''
 
}
 
=== Object Members ===
 
Member lines contain a datatype (can be an object type, as long as that type is not the current object type!), a name for the member, and optionally a default value.  If no default is given, the type's "blank" equivalent (such as 0 for integers or an empty string for strings) is automatically used.  When a variable is created of this object type, all of its members are initialized to their default value.  Members are defined like so:
 
member string Name
 
member int Size
 
member int Height=52
 
 
 
=== Object Functions ===
 
Functions in objects look and behave exactly like normal functions, but are later called by prepending the function name with the name of a variable of this type and a period, as in accessing a member.  For example:
 
objectdef myobject
 
{
 
    ''members''
 
    int Data=10
 
 
    function MyFunction2()
 
    {
 
      echo ${Data}
 
    }
 
 
    function MyFunction(string p1,int p2)
 
    {
 
      call MyFunction2
 
    }
 
}
 
 
function main()
 
{
 
  declare MyVariable myobject
 
  call MyVariable.MyFunction "parameter 1" 2
 
}
 
 
 
Within an object function, a special "object" variable scope exists, which allows you to access members of the current object.  Functions within the object may also be called without the object name.  A special Top-Level Object called [[TLO:This|This]] also exists to retrieve the current object if necessary.
 
 
 
== 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"
 
}
 
=== Example 3 ===
 
The same pointless script as above, but with a parameter passed to main to determine the size of the loop.
 
function MyFunction(int Count)
 
{
 
  return -${Count}
 
}
 
 
function main(int Max)
 
{
 
  if ${Max<1}
 
    return
 
  Declare Count int 1
 
  do
 
  {
 
      call MyFunction ${Count}
 
      echo Count: ${Count} ${Return}
 
  }
 
  while "${Count:Inc}<=${Max}"
 
}
 
The script may be run like so:
 
runscript myscript 10
 
 
 
== See Also ==
 
* [[LavishScript]]
 
* [[LavishScript:Syntax|LavishScript Command Syntax]]
 
* [[LavishScript:Mathematical Formulae|Mathematical Formulae]]
 
* [[LavishScript:Preprocessor|Preprocessor]]
 
* [[LavishScript:Designing Scripts|Designing Scripts in LavishScript]]
 
 
 
[[Category:LavishScript]]
 

Latest revision as of 04:36, 16 May 2006