Difference between revisions of "AGIWiki/Logic syntax"

From ScummVM :: Wiki
Jump to navigation Jump to search
m (syntax -> source)
m (Text replacement - "</source>" to "</syntaxhighlight>")
 
(2 intermediate revisions by 2 users not shown)
Line 1: Line 1:
{{AGIWiki}}
{{AGIWiki}}


This article describes the '''logic source''' for the C-like "official" AGI source described in the [[AGIWiki/AGI Specs|AGI Specs]] and supported by [[AGIWiki/AGI Studio|AGI Studio]] and [[AGIWiki/WinAGI|WinAGI]].
This article describes the '''logic syntax''' for the C-like "official" AGI syntax described in the [[AGIWiki/AGI Specs|AGI Specs]] and supported by [[AGIWiki/AGI Studio|AGI Studio]] and [[AGIWiki/WinAGI|WinAGI]].


'''Note:''' The [[AGIWiki/WinAGI|WinAGI]] development environment supports an [[AGIWiki/VB Logic source|alternate source]], that is based on Microsoft's Visual Basic language, rather than the C language. At this time, the C-based source described in this article is the most widely-supported AGI logic source.
'''Note:''' The [[AGIWiki/WinAGI|WinAGI]] development environment supports an [[AGIWiki/VB Logic Syntax|alternate syntax]], that is based on Microsoft's Visual Basic language, rather than the C language. At this time, the C-based syntax described in this article is the most widely-supported AGI logic syntax.


== Action Commands ==
== Action Commands ==
Line 9: Line 9:
Normal action commands are specified by the command name followed by parentheses which contain the arguments, separated by commas. A semicolon is placed after the parentheses. The parentheses are required even if there are no arguments. The arguments given must have the correct prefix for that type of argument as explained later in this document (this is to make sure the programmer does not use a [[AGIWiki/Variable|variable]], for example, when they think they are using a [[AGIWiki/Flag|flag]]).
Normal action commands are specified by the command name followed by parentheses which contain the arguments, separated by commas. A semicolon is placed after the parentheses. The parentheses are required even if there are no arguments. The arguments given must have the correct prefix for that type of argument as explained later in this document (this is to make sure the programmer does not use a [[AGIWiki/Variable|variable]], for example, when they think they are using a [[AGIWiki/Flag|flag]]).


<source lang="cpp">
<syntaxhighlight lang="cpp">
assign.v(v50,0);
assign.v(v50,0);


program.control();
program.control();
</source>
</syntaxhighlight>


Multiple commands may be placed on the one line:
Multiple commands may be placed on the one line:


<source lang="cpp">
<syntaxhighlight lang="cpp">
reset(f6); reset(f7);
reset(f6); reset(f7);
</source>
</syntaxhighlight>


Substitutions for the following action commands are available:
Substitutions for the following action commands are available:


<source lang="cpp">
<syntaxhighlight lang="cpp">
increment(v30);      v30++;
increment(v30);      v30++;
decrement(v30);      v30--;
decrement(v30);      v30--;
Line 40: Line 40:
lindirectv(v30,v32); *v30 = v32;
lindirectv(v30,v32); *v30 = v32;
rindirect(v30,v32);  v30 = *v32;
rindirect(v30,v32);  v30 = *v32;
</source>
</syntaxhighlight>


== If structures and test commands ==
== If structures and test commands ==
Line 46: Line 46:
An if structure looks like this:
An if structure looks like this:


<source lang="cpp">
<syntaxhighlight lang="cpp">
if (test commands) {
if (test commands) {
   action commands
   action commands
}
}
</source>
</syntaxhighlight>


or like this
or like this


<source lang="cpp">
<syntaxhighlight lang="cpp">
if (test commands) {
if (test commands) {
   action commands
   action commands
Line 61: Line 61:
   more action commands
   more action commands
}
}
</source>
</syntaxhighlight>


Carriage returns are not necessary:
Carriage returns are not necessary:


<source lang="cpp">
<syntaxhighlight lang="cpp">
if (test commands) { action Commands } else { more action commands }
if (test commands) { action Commands } else { more action commands }
</source>
</syntaxhighlight>




Test commands are coded like action commands except there is no semicolon. They are separated by <code>&&</code> or <code>||</code> for AND and OR, respectively:
Test commands are coded like action commands except there is no semicolon. They are separated by <code>&&</code> or <code>||</code> for AND and OR, respectively:


<source lang="cpp">
<syntaxhighlight lang="cpp">
if (isset(f5) &&
if (isset(f5) &&
     greatern(v5,6)) { ......
     greatern(v5,6)) { ......
</source>
</syntaxhighlight>


Again, carriage returns are not necessary within the if statement:
Again, carriage returns are not necessary within the if statement:


<source lang="cpp">
<syntaxhighlight lang="cpp">
if (lessn(v5,6) && (greatern(v5,2)) { .......
if (lessn(v5,6) && (greatern(v5,2)) { .......


if (isset(f90) && equalv(v32,v34) &&
if (isset(f90) && equalv(v32,v34) &&
     greatern(v34,20)) { .......
     greatern(v34,20)) { .......
</source>
</syntaxhighlight>


A&nbsp;! placed in front of a command signifies a NOT.
A&nbsp;! placed in front of a command signifies a NOT.


<source lang="cpp">
<syntaxhighlight lang="cpp">
if (!isset(f7)) {
if (!isset(f7)) {
   ......
   ......
</source>
</syntaxhighlight>


Boolean expressions are not necessarily simplified so they must follow the rules set down by the file format. If test commands are to be ORred together, they must be placed in brackets.
Boolean expressions are not necessarily simplified so they must follow the rules set down by the file format. If test commands are to be ORred together, they must be placed in brackets.


<source lang="cpp">
<syntaxhighlight lang="cpp">
if ((isset(f1) || isset(f2)) {
if ((isset(f1) || isset(f2)) {
   ......
   ......
Line 103: Line 103:


if (isset(1) || (isset(2) && isset(3))) {    is NOT legal
if (isset(1) || (isset(2) && isset(3))) {    is NOT legal
</source>
</syntaxhighlight>


Depending on the compiler, simplification of boolean expressions may be supported, so the above may not apply in all cases (although if these are rules are followed then the logic will work with all compilers).
Depending on the compiler, simplification of boolean expressions may be supported, so the above may not apply in all cases (although if these are rules are followed then the logic will work with all compilers).
Line 109: Line 109:
Substitutions for the following test commands are available:
Substitutions for the following test commands are available:


<source lang="cpp">
<syntaxhighlight lang="cpp">
equaln(v30,4)      v30 == 4
equaln(v30,4)      v30 == 4
equalv(v30,v32)    v30 == v32
equalv(v30,v32)    v30 == v32
Line 122: Line 122:
!lessn(v30,4)      v30 = 4
!lessn(v30,4)      v30 = 4
!lessv(v30,v32)    v30 = v32
!lessv(v30,v32)    v30 = v32
</source>
</syntaxhighlight>


Also, flags can be tested for by just using the name of the flag:
Also, flags can be tested for by just using the name of the flag:


<source lang="cpp">
<syntaxhighlight lang="cpp">
if (f6) { .....
if (f6) { .....


if (v7 > 0 && !f6) { .....
if (v7 > 0 && !f6) { .....
</source>
</syntaxhighlight>


which is equivalent to:
which is equivalent to:


<source lang="cpp">
<syntaxhighlight lang="cpp">
if (isset(f6)) { .....
if (isset(f6)) { .....


if (v7 > 0 && !isset(f6)) { .....
if (v7 > 0 && !isset(f6)) { .....
</source>
</syntaxhighlight>


== Argument types ==
== Argument types ==
Line 192: Line 192:
Compilers can enforce type checking, so that the programmer must use the correct prefix for an argument so that they know they are using the right type. Decoders should display arguments with the right type.
Compilers can enforce type checking, so that the programmer must use the correct prefix for an argument so that they know they are using the right type. Decoders should display arguments with the right type.


<source lang="cpp">
<syntaxhighlight lang="cpp">
move.obj(o4, 80, 120, 2, f66);
move.obj(o4, 80, 120, 2, f66);


if (obj.in.box(o2, 30, 60, 120, 40)) { .....
if (obj.in.box(o2, 30, 60, 120, 40)) { .....
</source>
</syntaxhighlight>


For a complete list of the commands and their argument types, see [[AGIWiki/Logic commands by name|Logic commands by name]].
For a complete list of the commands and their argument types, see [[AGIWiki/Logic commands by name|Logic commands by name]].
Line 202: Line 202:
Messages and inventory items may be given in either numerical or text format:
Messages and inventory items may be given in either numerical or text format:


<source lang="cpp">
<syntaxhighlight lang="cpp">
print("He's not here.");
print("He's not here.");


Line 211: Line 211:
if (has(i9)) { .....
if (has(i9)) { .....


</source>
</syntaxhighlight>
Messages can also be split over multiple lines:
Messages can also be split over multiple lines:


<source lang="cpp">
<syntaxhighlight lang="cpp">
print("This message is split "
print("This message is split "
       "over multiple lines.");
       "over multiple lines.");
</source>
</syntaxhighlight>


Quote marks must be used around messages and inventory item names. This is important because some messages or inventory item names may contain parentheses or commas, which could confuse the compiler. This is also the case for the said command which will be described shortly.
Quote marks must be used around messages and inventory item names. This is important because some messages or inventory item names may contain parentheses or commas, which could confuse the compiler. This is also the case for the said command which will be described shortly.


<source lang="cpp">
<syntaxhighlight lang="cpp">
if (has("Buckazoid(s)")) { .....  // no ambiguity here about where
if (has("Buckazoid(s)")) { .....  // no ambiguity here about where
                                   // the argument ends
                                   // the argument ends
</source>
</syntaxhighlight>


If quote marks are part of the message or inventory object, a \ should be placed in front of these. To use a \, \\ should be used. \n can also be used for a new line.
If quote marks are part of the message or inventory object, a \ should be placed in front of these. To use a \, \\ should be used. \n can also be used for a new line.
Line 230: Line 230:
The said test command uses different parameters to all the other commands. Where as the others use 8 bit arguments (0-255), said takes 16 bit arguments (0-65535). Also, the number of arguments in a said command can vary. The numbers given in the arguments are the word group numbers from the WORDS.TOK file.
The said test command uses different parameters to all the other commands. Where as the others use 8 bit arguments (0-255), said takes 16 bit arguments (0-65535). Also, the number of arguments in a said command can vary. The numbers given in the arguments are the word group numbers from the WORDS.TOK file.


<source lang="cpp">
<syntaxhighlight lang="cpp">
if (said(4, 80)) { .....
if (said(4, 80)) { .....
</source>
</syntaxhighlight>


Words can also be given in place of the numbers:
Words can also be given in place of the numbers:


<source lang="cpp">
<syntaxhighlight lang="cpp">
if (said("look")) { .....
if (said("look")) { .....


if (said("open","door")) { .....
if (said("open","door")) { .....
</source>
</syntaxhighlight>


Quote marks must also be used around the words.
Quote marks must also be used around the words.
Line 248: Line 248:
Labels are given like this:
Labels are given like this:


<source lang="cpp">
<syntaxhighlight lang="cpp">
Label1:
Label1:
</source>
</syntaxhighlight>


The label name can contain letters, numbers, and the characters '_' and '.'. No spaces are allowed.
The label name can contain letters, numbers, and the characters '_' and '.'. No spaces are allowed.
Line 256: Line 256:
The [[AGIWiki/Goto|Goto]] command takes on parameter, the name of a label:
The [[AGIWiki/Goto|Goto]] command takes on parameter, the name of a label:


<source lang="cpp">
<syntaxhighlight lang="cpp">
goto(Label1);
goto(Label1);
</source>
</syntaxhighlight>


== Comments ==
== Comments ==
Line 264: Line 264:
There are three ways that comments can be used.
There are three ways that comments can be used.


<source lang="cpp">
<syntaxhighlight lang="cpp">
// - rest of line is ignored
// - rest of line is ignored


Line 270: Line 270:


/* Text between these are ignored */
/* Text between these are ignored */
</source>
</syntaxhighlight>


The /*...*/ can be nested:
The /*...*/ can be nested:


<source lang="cpp">
<syntaxhighlight lang="cpp">
/* comment start
/* comment start


Line 288: Line 288:


*/                  // uncomments
*/                  // uncomments
</source>
</syntaxhighlight>


'''Note:''' the fact that these comments can be nested is very different from almost every other language that uses these types of comments, including C, C , and Java.
'''Note:''' the fact that these comments can be nested is very different from almost every other language that uses these types of comments, including C, C , and Java.
Line 304: Line 304:
In some cases you may want to assign a specific number to a message so you can refer to it in other places. This is done by using the #message command, followed by the number of the message then the message itself:
In some cases you may want to assign a specific number to a message so you can refer to it in other places. This is done by using the #message command, followed by the number of the message then the message itself:


<source lang="cpp">
<syntaxhighlight lang="cpp">
#message 4 "You can't do that now."
#message 4 "You can't do that now."
</source>
</syntaxhighlight>


Then you can give the message number as the parameter in commands:
Then you can give the message number as the parameter in commands:


<source lang="cpp">
<syntaxhighlight lang="cpp">
print(m4);
print(m4);
</source>
</syntaxhighlight>


Or embed the message in commands as normal and the number you assigned to it before will be used:
Or embed the message in commands as normal and the number you assigned to it before will be used:


<source lang="cpp">
<syntaxhighlight lang="cpp">
print("You can't do that now.");
print("You can't do that now.");
</source>
</syntaxhighlight>


<nowiki>#message can be used anywhere in the file, so you do not have to set the message before you use it. </nowiki>
<nowiki>#message can be used anywhere in the file, so you do not have to set the message before you use it. </nowiki>

Latest revision as of 15:17, 25 October 2018

AGIWiki


This article describes the logic syntax for the C-like "official" AGI syntax described in the AGI Specs and supported by AGI Studio and WinAGI.

Note: The WinAGI development environment supports an alternate syntax, that is based on Microsoft's Visual Basic language, rather than the C language. At this time, the C-based syntax described in this article is the most widely-supported AGI logic syntax.

Action Commands

Normal action commands are specified by the command name followed by parentheses which contain the arguments, separated by commas. A semicolon is placed after the parentheses. The parentheses are required even if there are no arguments. The arguments given must have the correct prefix for that type of argument as explained later in this document (this is to make sure the programmer does not use a variable, for example, when they think they are using a flag).

assign.v(v50,0);

program.control();

Multiple commands may be placed on the one line:

reset(f6); reset(f7);

Substitutions for the following action commands are available:

increment(v30);       v30++;
decrement(v30);       v30--;
assignn(v30,4);       v30 = 4;
assignv(v30,v32);     v30 = v32;
addn(v30,4);          v30 = v30 + 4;   or  v30 += 4;
addv(v30,v32);        v30 = v30 + v32; or  v30 += v32;
subn(v30,4);          v30 = v30 - 4;   or  v30 -= 4;
subv(v30,v32);        v30 = v30 - v32; or  v30 -= v32;
mul.n(v30,4);         v30 = v30 * 4;   or  v30 *= 4;
mul.v(v30,v32);       v30 = v30 * v32; or  v30 *= v32;
div.n(v30,4);         v30 = v30 / 4;   or  v30 /= 4;
div.v(v30,v32);       v30 = v30 / v32; or  v30 /= v32;

lindirectn(v30,4);   *v30 = 4;
lindirectv(v30,v32); *v30 = v32;
rindirect(v30,v32);   v30 = *v32;

If structures and test commands

An if structure looks like this:

if (test commands) {
  action commands
}

or like this

if (test commands) {
  action commands
}
else {
  more action commands
}

Carriage returns are not necessary:

if (test commands) { action Commands } else { more action commands }


Test commands are coded like action commands except there is no semicolon. They are separated by && or || for AND and OR, respectively:

if (isset(f5) &&
    greatern(v5,6)) { ......

Again, carriage returns are not necessary within the if statement:

if (lessn(v5,6) && (greatern(v5,2)) { .......

if (isset(f90) && equalv(v32,v34) &&
    greatern(v34,20)) { .......

A ! placed in front of a command signifies a NOT.

if (!isset(f7)) {
  ......

Boolean expressions are not necessarily simplified so they must follow the rules set down by the file format. If test commands are to be ORred together, they must be placed in brackets.

if ((isset(f1) || isset(f2)) {
  ......

if (isset(f1) && (isset(f2) || isset(f3))) {
  ......

if (isset(1) || (isset(2) && isset(3))) {    is NOT legal

Depending on the compiler, simplification of boolean expressions may be supported, so the above may not apply in all cases (although if these are rules are followed then the logic will work with all compilers).

Substitutions for the following test commands are available:

equaln(v30,4)      v30 == 4
equalv(v30,v32)    v30 == v32
greatern(v30,4)    v30   4
greaterv(v30,v32)  v30   v32
lessn(v30,4)       v30   4
lessv(v30,v32)     v30   v32
!equaln(v30,4)     v30&nbsp;!= 4
!equalv(v30,v32)   v30&nbsp;!= v32
!greatern(v30,4)   v30 = 4
!greaterv(v30,v32) v30 = v32
!lessn(v30,4)      v30 = 4
!lessv(v30,v32)    v30 = v32

Also, flags can be tested for by just using the name of the flag:

if (f6) { .....

if (v7 > 0 && !f6) { .....

which is equivalent to:

if (isset(f6)) { .....

if (v7 > 0 && !isset(f6)) { .....

Argument types

There are 9 different types of arguments that commands use:

Type Prefix
Number (no prefix)

Variable

v

Flag

f

Message

m

Object

o

Inventory item

i

String

s

Word

w

Controller

c

The said test command uses its own special arguments which will be described later.

Each of these types of arguments is given by the prefix and then a number from 0-255, e.g. v5, f6, m27, o2.

The word type represents words that the player has typed in (as opposed to words that are stored in the WORDS.TOK file). Strings are the temporary string variables stored in memory, not to be confused with messages (that are stored in the logic resources). Controllers are menu items and keys.

Compilers can enforce type checking, so that the programmer must use the correct prefix for an argument so that they know they are using the right type. Decoders should display arguments with the right type.

move.obj(o4, 80, 120, 2, f66);

if (obj.in.box(o2, 30, 60, 120, 40)) { .....

For a complete list of the commands and their argument types, see Logic commands by name.

Messages and inventory items may be given in either numerical or text format:

print("He's not here.");

print(m12);

if (has("Jetpack")) { .....

if (has(i9)) { .....

Messages can also be split over multiple lines:

print("This message is split "
      "over multiple lines.");

Quote marks must be used around messages and inventory item names. This is important because some messages or inventory item names may contain parentheses or commas, which could confuse the compiler. This is also the case for the said command which will be described shortly.

if (has("Buckazoid(s)")) { .....   // no ambiguity here about where
                                   // the argument ends

If quote marks are part of the message or inventory object, a \ should be placed in front of these. To use a \, \\ should be used. \n can also be used for a new line.

The said test command uses different parameters to all the other commands. Where as the others use 8 bit arguments (0-255), said takes 16 bit arguments (0-65535). Also, the number of arguments in a said command can vary. The numbers given in the arguments are the word group numbers from the WORDS.TOK file.

if (said(4, 80)) { .....

Words can also be given in place of the numbers:

if (said("look")) { .....

if (said("open","door")) { .....

Quote marks must also be used around the words.

Labels and the goto command

Labels are given like this:

Label1:

The label name can contain letters, numbers, and the characters '_' and '.'. No spaces are allowed.

The Goto command takes on parameter, the name of a label:

goto(Label1);

Comments

There are three ways that comments can be used.

// - rest of line is ignored

[ - rest of line is ignored

/* Text between these are ignored */

The /*...*/ can be nested:

/* comment start

  print("Hello");    // won't be run

  /*                 // a new comment start (will be ignored!)

    v32 = 15;        // won't be run

  */                 // uncomments the most inner comment

  print("Hey!");     // won't be run, still inside comments

*/                   // uncomments

Note: the fact that these comments can be nested is very different from almost every other language that uses these types of comments, including C, C , and Java.

Defines

See Defines.

Including files

See Includes.

More on messages

In some cases you may want to assign a specific number to a message so you can refer to it in other places. This is done by using the #message command, followed by the number of the message then the message itself:

#message 4 "You can't do that now."

Then you can give the message number as the parameter in commands:

print(m4);

Or embed the message in commands as normal and the number you assigned to it before will be used:

print("You can't do that now.");

#message can be used anywhere in the file, so you do not have to set the message before you use it.

For more details, see Message.

The return command

The return command is just a normal action command (command number 0), with no arguments. This must be the last command in every logic resource.

See also

Sources