Phantom Documentation

[Help Home] [Phantom Home]
General Language Syntax

The syntax for the Phantom language is not unlike the 'C' syntax. Generally, most statements must be followed by a semi-colon (;), except for flow control statements. Comments can be made as either entire lines or at the end of a line by placing a '#' in front of the comment. Strings are placed in double quotes (") and integers and real numbers are typed as-is (see variables).

As in C or Java, variables must be declared before use. This is done by specifying the data type followed by a data name. Assignments to many (but not all) data types are made using the ‘=’ symbol, and many data types accept basic math operations (+,-,*,/, etc…). Multiple statements can be made on a single line, separated via semi-colons:


int i; i = 10; i = i + 2;



The scope of variables not declared inside a function is global. All variables declared in a function are available only in the scope of that function. Functions can also access globally declared variables. Variables passed to a function are made into local copies, so changes made to variables passed to functions do not affect the variable outside the function.

Both user defined functions and built in functions are called by simply typing the function name, along with any parameters. The parameters must be placed in parentheses ( '(' and ')' ) and separated by a comma (,). Flow control statements (for, while, etc...) are also similar to 'C', with the flow control statement first, followed by an opening brace ({), followed by some statements, and finally followed by a closing brace (}).

for(int i = 0; i < 10; i++){
  if(i > 5){
    disp("i is less than 5");
  }else{
    disp("i is more than or equal to 5");
  }
}


Operators
Phantom supports many types of operators used to work with variables. For a list of operators and how to use them see the Operators section.

Members

Member functions and variables for variable types that support them (for example 'window', 'bitmap', 'exception') are called in a similar fashion to member functions and variables called in C++ or Java: the variable name, followed by a period (.) followed by the function or member variable name. For window types each child window is considered a member variable to the parent window, and as such is accessed as a member variable.

Example Code

# Declare an exception type
exception e;

# Display the exception's members
members(e);

# Call exception member functions
e.SetWarning("A warning");
e.throw();

# Declare some windows
window w;
window child;

# Access a window's variables
w.Tag = "Notepad*";
w.Class = "Notepad";
disp("Tag is: " + w.Tag);

# Create the Child
child = MainWin(1, "Edit");

# Add the child to w by calling a member function
w.AddChild(child, "Edit");

# Show that the child was added
ListWindow(w);

# Open Notepad by calling 'System' function
System("c:\\windows\\system32\\notepad.exe");

# Call a child window function
w.Edit.TypeKeys("Hello");



The output from the above example is as follows:

Output
<Member Variables>
Name Type
------------------------------------------------------------

<Member Functions>
Name Return Parameters
------------------------------------------------------------
GetMessage string
GetState int
Set void int,string
SetError void string
SetFailure void string
SetOK void string
SetWarning void string
throw void
** WARNING: (1) from 'C:\Documents and Settings\HP_Administrator\My Documents\test.psc' on line 9 - A warning
Tag is: Notepad*
 {Class:Notepad; Tag:Notepad*}
  Edit {Class:Edit; Tag:[1]}



Some Differences from C
The primary difference between C and the Phantom interpreted language is how strings are declared and how functions are declared. In C, a string is a character array (char*), and functions need no special declaration other than the return type. There are no pointers in the Phantom language, so strings are declared using the string keyword. No memory needs to be allocated or freed as with C strings.


disp("A string");
string s = "global domination"; # char* s = "this is C";


User defined functions in Phantom must be declared using the function keyword followed by the return type, function name, and parameters:

# C does not require 'function'
function int Add(int i1, int i2){
  return i1 + i2;
}


A Note About Variable Scope
The variable scope is where the variable is accessible from. The scope changes whenever a user-defined function is called or a flow control statement is executed. Each function and flow control statement has its own 'scope' (called the 'local' scope) and any variables declared inside that scope are not available outside the scope. Any variables declared in one scope are destroyed when that scope exits. For example if a variable, 'x', is declared inside an 'if' statement, it is not accessible outside the 'if' statement and will be destroyed when execution exits the 'if' statement. The same applies to functions.

For flow control statements, the any variables declared outside the flow control are available to the flow control. However, for functions, any variables declared outside the function are not available. The exception to this are global variables, which are accessible everywhere. Global variables are variables declared directly in the script and are not in a function or flow statement.

If a flow statement declares a variable with the same name as a variable outside its scope or a function declares a variable with the same name as a global variable, the variable in the local scope is used when that variable name is used. Likewise, if there are multiple scope levels above the current scope for a flow statement, and multiple levels have a variable with the same name, the variable in the level closest to the local level will be used.

The following sample code illustrates how the scope is used:

Example Code

Line

Code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
32

int i = 10; # i is global

if(i > 0){ # Enter 'if' scope
  # i is accessible because in a
  # 'parent' scope... in this case global
  disp(i);
  int k = 3;

} # Exit 'if' scope

# k is no longer available
# k = 3; would produce an error


function void f(){ # Enter 'f' scope
  # i is global, so is accessible
  disp(i);
  # q is local, so is accessible. When the
  # function exits, q no longer exists.
  int q = 0;
} # Exit 'f' scope

# Call 'f'
f();

# This would cause an error because 'q' is gone:
# q = 3;

if(i > 0){
  # Can declare this because in a new cope

  int i = 3;
  disp(i); # Will show 3 because closest 'scope'
  if(i < 7){ # Compares 3 because closest 'scope'
    disp("i < 7");
  }
}




An Example

The following is a sample that illustrates the syntax of Phantom:

First, a sample window declarations file is used. This declaration is a simplified declaration of the 'Notepad' application and is saved as 'notepad.dec' for the purposes of this demonstration. This file would normally be recorded using The Window Declarations Recorder. The file is included as 'sample.dec' with the Phantom distribution.

Next, a sample script is created that will perform actions on Notepad. This assumes that notepad is already open. Note the comments that describe each action.

Example Code

# Include the window declarations
use "sample.dec";

#Set some variables
int ctr = 0;
string sctr;

#Start Notepad
System("c:\\windows\\system32\\notepad.exe");

# Perform an action on Notepad
Notepad.Edit1.TypeKeys("Hello, Notepad\n");

# A for loop
for(ctr = 0; ctr < 10; ctr++) {
  # Convert an integer to a string
  sctr = IntToStr(ctr);
  # Perform more actions on notepad
  Notepad.Edit1.TypeKeys(sctr);
  Notepad.Edit1.TypeKeys("\n");
}



This file (call it 'sample1.psc') would then be run using the Phantom interpreter as illustrated in The Phantom Interpreter. Note that the window 'Notepad' and all child windows were created automatically when the window declarations file was loaded using the 'use' statement.


See Also: Variables, Using Window Declarations, Flow Control, Using Window Functions


Copyright 2000-2011 Phantom Automated Solutions