Difference between revisions of "LavishScript:Script Syntax"

From Lavish Software Wiki
Jump to navigation Jump to search
Line 114: Line 114:
  
 
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.
 
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.
 +
 +
== Atoms ==
 +
[[LavishScript:Atoms|Atoms]] can be defined in the same way as functions, except using the word <tt>atom</tt>, like so:
 +
atom MyAtom1()
 +
{
 +
  ''commands''
 +
}
 +
atom MyAtom2(parameter1,parameter2,parameter3)
 +
{
 +
  ''commands''
 +
}
 +
atom MyAtom3(string parameter1,int parameter2)
 +
{
 +
  ''commands''
 +
}
 +
atmo MyAtom4(string parameter1,int parameter2=10)
 +
{
 +
  ''commands''
 +
}
 +
 +
Atoms may additionally use the keyword <tt>global</tt> to create a global atom, like so:
 +
global atom MyAtom1()
 +
{
 +
  ''commands''
 +
}
 +
global atom MyAtom2(parameter1,parameter2,parameter3)
 +
{
 +
  ''commands''
 +
}
 +
global atom MyAtom3(string parameter1,int parameter2)
 +
{
 +
  ''commands''
 +
}
 +
global atom MyAtom4(string parameter1,int parameter2=10)
 +
{
 +
  ''commands''
 +
}
  
 
== Examples ==
 
== Examples ==

Revision as of 16:56, 23 September 2005

Overview

Scripts may consist of comments, functions, code blocks, commands, control structures and object definitions.

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

Comment Type 1

Any line that starts with a semi-colon (;) is considered a comment, and will not be handled by the script (comments are actually removed by the preprocessor). These comments must be on their own line, never at the end of another line.

; good comment
echo hi ; this is not a comment and will be executed as a command!

Comment Type 2

The second type of comment, known to programmers as an ANSI-C comment, starts with /* and ends with */. These comments may be used anywhere at all, and may contain multiple lines.

/*
 * This is a sample ANSI-C comment
 * It has multiple lines
 * A style like this particular comment is often used for authorship and license notices in source code
 */
echo hi/* this is a valid comment and will not affect the echo output, which also contains: */ my name is bob
/* The above is effectively "echo hi my name is bob" */

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 If and 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 Command Syntax for LavishScript command syntax rules.

Control Structures

Some commands available only to scripts can change the flow of execution of the script. These are not actually commands and follow only special rules for control structures. A control structure is generally made up of one or more keywords, such as "If", followed by a condition or other expression, along with one command or a code block. These structures may cause a section of code to repeat, or only be executed under a certain condition. These keywords along with the commands or code blocks they affect make up control structures. See Mathematical Formulae for insights on how "conditions" are processed (they are simply math!).

Conditional

Repetitive

Selective

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 definitions can also inherit other object definitions. This means that a second object definition defined like so...

objectdef othertype inherits mytype
{
  members and functions
}

... will automatically have all members and functions provided by mytype, as well as othertype.

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 This also exists to retrieve the current object if necessary.

Atoms

Atoms can be defined in the same way as functions, except using the word atom, like so:

atom MyAtom1()
{
  commands
}
atom MyAtom2(parameter1,parameter2,parameter3)
{
  commands
}
atom MyAtom3(string parameter1,int parameter2)
{
  commands
}
atmo MyAtom4(string parameter1,int parameter2=10)
{
  commands
}

Atoms may additionally use the keyword global to create a global atom, like so:

global atom MyAtom1()
{
  commands
}
global atom MyAtom2(parameter1,parameter2,parameter3)
{
  commands
}
global atom MyAtom3(string parameter1,int parameter2)
{
  commands
}
global atom MyAtom4(string parameter1,int parameter2=10)
{
  commands
}

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