ASH Errors: Difference between revisions

From Kolmafia
Jump to navigation Jump to search
imported>Heeheehee
m Extended info regarding abort.
imported>Slyz
added "Function undefined" error
 
(17 intermediate revisions by 6 users not shown)
Line 12: Line 12:
abort();
abort();
</syntaxhighlight>}}
</syntaxhighlight>}}
will stop the execution and print: <span style="color:red">KoLmafia declares world peace.</span> Note that this may also be a result of pressing Esc in the Main Interface or hitting "stop now" in the Adventure tab.
will stop the execution and print: <span style="color:red">KoLmafia declares world peace.</span> Note that this message may also appear as a result of pressing Esc in the Main Interface or hitting "stop now" in the Adventure tab.


{{CodeSample|
{{CodeSample|
Line 21: Line 21:
</syntaxhighlight>}}
</syntaxhighlight>}}
will stop the execution and print: <span style="color:red">Aborting script...</span>
will stop the execution and print: <span style="color:red">Aborting script...</span>
===Cannot initialize parameter [parameter]===
This error is created when an assignment is attempted inside a function declaration.
The following code results in an error.
{{CodeSample|
code=
<syntaxhighlight>
/* test.ash */
void my_function( string myvar = "value" )
{
  /* Do something */
}
</syntaxhighlight>}}
will halt the script and print: <span style="color: red;">Cannot initialize parameter myvar (test.ash, line X)</span>
===Cannot return [datatype] from [datatype] function===
This indicates that the return value does not match the function's type.
{{CodeSample|
description=Example:|
code=
<syntaxhighlight>
int stuff(){
  return "4";
}
</syntaxhighlight>}}
will cause this error: <span style="color:red">Cannot return string value from int function (test.ash, line 2)</span>
{{CodeSample|
description=Similarly, for void functions:|
code=
<syntaxhighlight>
void stuff(){
  return 4;
}
</syntaxhighlight>}}
will cause this error: <span style="color:red">Cannot return a value from a void function (test.ash, line 2)</span>
===Encountered '[break|continue]' outside loop===
This indicates that the control structure in question was not in a loop.
{{CodeSample|
description=Example:|
code=
<syntaxhighlight>
continue;
</syntaxhighlight>}}
will cause this error: <span style="color:red">Encountered 'continue' outside of loop (test.ash, line 1)</span>


===Expected===
===Expected===
Line 35: Line 88:
will cause this error: <span style="color:red">Expected ;, found print (test.ash, line 2)</span>
will cause this error: <span style="color:red">Expected ;, found print (test.ash, line 2)</span>


===Missing return value===
===Function defined multiple times===
 
This indicates that the function in question has already been defined.
 
{{CodeSample|
description=Example:|
code=
<syntaxhighlight>
int abs(int a){
    return a*((a>0).to_int()*2 - 1);
}
int abs(int a){
    if(a<0) a = -a;
    return a;
}
</syntaxhighlight>}}
will cause this error: <span style="color:red">Function <nowiki>'abs( int )'</nowiki> defined multiple times (test.ash, line 4)</span>
 
===Function undefined===
 
This happens when calling a function that KoLMafia doesn't know, either because the script uses a built-in ASH function that doesn't exist yet in the user's build, or because the function hasn't been defined yet, neither in the imported scripts nor in the script itself.
 
{{CodeSample|
description=Example:|
code=
<syntaxhighlight>
my_function();
 
void my_function(){
    print( "hello world" );
}
</syntaxhighlight>}}
will cause this error: <span style="color:red">Function <nowiki>'my_function()'</nowiki> undefined. This script may require a more recent version of KoLmafia and/or its supporting scripts. (test.ash, line 1)</span>
 
==="if" requires a boolean conditional expression ===


The last line of a user-defined function has to be "return <value>;" (although the [[return]] command can be used before too).
This indicates that the condition inside an if statement is not a boolean.


{{CodeSample|
{{CodeSample|
Line 43: Line 130:
code=
code=
<syntaxhighlight>
<syntaxhighlight>
int my_function( int a, int b ) {
if(42) {
    if ( a > b ) return a ;
  print("Don't panic.");
    else return b ;
}
}
</syntaxhighlight>}}
</syntaxhighlight>}}
will cause this error: <span style="color:red">Missing return value (test.ash, line 4)</span>
will cause this error: <span style="color:red">"if" requires a boolean conditional expression (test.ash, line 1)</span>
 
===Index type is not a primitive type===
 
This indicates that the map's index is not a standard datatype (float, int, string, etc.).
 
{{CodeSample|
description=Example:|
code=
<syntaxhighlight>
record alpha{
  int a;
  int b;
};
int[alpha] map;
</syntaxhighlight>}}
will cause this error: <span style="color:red">Index type 'alpha' is not a primitive type (test.ash, line 5)</span>


===Script parsing error===
===Invalid field name===


This generally indicates a syntax problem, as in having an excess amount of braces.
This indicates that the record does not contain the field in question.


{{CodeSample|
{{CodeSample|
Line 58: Line 160:
code=
code=
<syntaxhighlight>
<syntaxhighlight>
put_shop(0 ,0 ,$item[wolf mask]);
record my_record {
put_shop(0 ,0 ,$item[rave whistle]);
  string a;
put_shop(0 ,0 ,$item[giant needle]);
  string b;
cli_execute ("undercut");
};
}
 
my_record [int] my_map;
my_map[1].c = "hello";
</syntaxhighlight>}}
</syntaxhighlight>}}
will cause this error: <span style="color:red">Script parsing error (test.ash, line 5)</span>
will cause this error: <span style="color:red">Invalid field name 'c' (test.ash, line 7)</span>
 
Note that this error may also be encountered when neglecting to name a field.
{{CodeSample|
description= For instance:|
code=
<syntaxhighlight>
record my_record {
  string;
};
</syntaxhighlight>}}
will cause this error: <span style="color:red">Invalid field name ';' (test.ash, line 2)</span>


===Variable is already defined===
===Invalid type name===


This generally indicates that you declare the same variable twice.
This indicates that the specified type is not recognized by Mafia.


{{CodeSample|
{{CodeSample|
Line 74: Line 189:
code=
code=
<syntaxhighlight>
<syntaxhighlight>
int Drops ;
int[fruit] map;
int Meat ;
int Drops = numeric_modifier("Item Drop");
</syntaxhighlight>}}
</syntaxhighlight>}}
will cause this error: <span style="color:red">Variable Drops is already defined (test.ash, line 3)</span>
will cause this error: <span style="color:red">Invalid type name 'fruit' (test.ash, line 1)</span>


===Main method must appear at top level===
===Main method must appear at top level===
Line 96: Line 209:
</syntaxhighlight>}}
</syntaxhighlight>}}
will cause this error: <span style="color:red">main method must appear at top level (test.ash, line 6)</span>
will cause this error: <span style="color:red">main method must appear at top level (test.ash, line 6)</span>
===Map modified within foreach===
You cannot add or remove new entries into a map within a <code>[[foreach]]</code> loop, with the exception of removing the current key. This can happen if you accidentally wrap the name of a string variable with double-quotes for referencing a map entry.
{{CodeSample|
description=For example, the following code:|
code=
<syntaxhighlight>
/* test.ash */
item [string] my_map;
/* Populate my_map with several entries */
foreach str in my_map
  print( str + " => " + my_map[ "str" ] );
</syntaxhighlight>}}
Yields: <span style="color: red;">Map modified within foreach (test.ash, line X)</span>
===Missing return value===
The last line of a user-defined function has to be "return <value>;" (although the [[Control_Structures#return|return]] command can be used before too).
{{CodeSample|
description=Example:|
code=
<syntaxhighlight>
int my_function( int a, int b ) {
    if ( a > b ) return a ;
    else return b ;
}
</syntaxhighlight>}}
will cause this error: <span style="color:red">Missing return value (test.ash, line 4)</span>
===No closing found===
This can be caused by forgetting to put a '''"''' or a ''']''' at the end of an argument
{{CodeSample|
description=Example:|
code=
<syntaxhighlight>
buy(1000 , $item[disco ball, 225);
</syntaxhighlight>}}
will cause this error: <span style="color:red">No closing ] found (testing.ash, line 1)</span>


===Record expected===
===Record expected===
Line 109: Line 266:
will cause this error: <span style="color:red">Record expected (test.ash, line 2)</span>
will cause this error: <span style="color:red">Record expected (test.ash, line 2)</span>


===Invalid field name===
===Record name is already defined===


This indicates that the record does not contain the field in question.
This indicates that the record name has already been defined, and so cannot be reused. The solution is to change the record name.


{{CodeSample|
{{CodeSample|
Line 117: Line 274:
code=
code=
<syntaxhighlight>
<syntaxhighlight>
record my_record {
record str {
   string a;
   string a;
   string b;
   string b;
};
};
record str {
  int a;
  int b;
};
</syntaxhighlight>}}
will cause this error: <span style="color:red">Record name 'str' is already defined (test.ash, line 5)</span>


my_record [int] my_map;
===Return needs [datatype] value===
my_map[1].c = "hello";
 
This indicates that the return value is void when the function is not.
 
{{CodeSample|
description=Example:|
code=
<syntaxhighlight>
int stuff() {
  return;
}
</syntaxhighlight>}}
</syntaxhighlight>}}
will cause this error: <span style="color:red">Invalid field name 'c' (test.ash, line 7)</span>
will cause this error: <span style="color:red">Return needs int value (test.ash, line 2)</span>
 
===Reserved word cannot be a [function|record|variable] name===
 
This indicates that the desired name has been [[Reserved Words|reserved]], and so cannot be used. The solution is to use a different name.


===Reserved word cannot be a record name===
{{CodeSample|
description=An invalid name:|
code=
<syntaxhighlight>
int float(float a) {
  return round(a);
}
</syntaxhighlight>}}
will cause this error: <span style="color:red">Reserved word 'float' cannot be used as a function name (test.ash, line 1)</span>


This indicates that the record name has already been reserved, and so cannot be used. The solution is to change the record name.
An interesting point to note is that ASH functions take precedent over identically named custom functions rather than throw an exception.


{{CodeSample|
{{CodeSample|
description=Example:|
description=An invalid record name:|
code=
code=
<syntaxhighlight>
<syntaxhighlight>
Line 142: Line 326:
will cause this error: <span style="color:red">Reserved word 'string' cannot be a record name (test.ash, line 1)</span>
will cause this error: <span style="color:red">Reserved word 'string' cannot be a record name (test.ash, line 1)</span>


===Record name is already defined===
{{CodeSample|
description=An invalid variable name:|
code=
<syntaxhighlight>
string item = "bottle of gin";
</syntaxhighlight>}}
will cause this error: <span style="color:red">Reserved word 'item' cannot be a variable name (test.ash, line 1)</span>
 
===Script parsing error===
 
This generally indicates a syntax problem, as in having an excess amount of braces or a variable/function/record name that starts with an invalid character.
 
{{CodeSample|
description=Example:|
code=
<syntaxhighlight>
put_shop(0 ,0 ,$item[wolf mask]);
put_shop(0 ,0 ,$item[rave whistle]);
put_shop(0 ,0 ,$item[giant needle]);
cli_execute ("undercut");
}
</syntaxhighlight>}}
will cause this error: <span style="color:red">Script parsing error (test.ash, line 5)</span>
 
===Variable is already defined===


This indicates that the record name has already been defined, and so cannot be reused. The solution is to change the record name.
This indicates that the same variable has been declared twice.


{{CodeSample|
{{CodeSample|
Line 150: Line 358:
code=
code=
<syntaxhighlight>
<syntaxhighlight>
record str {
int Drops ;
  string a;
int Meat ;
  string b;
int Drops = numeric_modifier("Item Drop");
};
record str {
  int a;
  int b;
};
</syntaxhighlight>}}
</syntaxhighlight>}}
will cause this error: <span style="color:red">Record name 'str' is already defined (test.ash, line 5)</span>
will cause this error: <span style="color:red">Variable Drops is already defined (test.ash, line 3)</span>




{{RFI|Obviously, a lot more info is needed here.}}
{{RFI|Obviously, a lot more info is needed here.}}
[[Category:Scripting]]

Latest revision as of 11:25, 24 June 2011

Errors happen. When they do, it helps to have some idea of what what wrong.

Abort

Whenever a script runs the abort() function, this error is generated.

The zero-parameter form:

abort();

will stop the execution and print: KoLmafia declares world peace. Note that this message may also appear as a result of pressing Esc in the Main Interface or hitting "stop now" in the Adventure tab.

The one-parameter form:

abort("Aborting script...");

will stop the execution and print: Aborting script...

Cannot initialize parameter [parameter]

This error is created when an assignment is attempted inside a function declaration. The following code results in an error.


/* test.ash */

void my_function( string myvar = "value" )
{
   /* Do something */
}

will halt the script and print: Cannot initialize parameter myvar (test.ash, line X)

Cannot return [datatype] from [datatype] function

This indicates that the return value does not match the function's type.

Example:

int stuff(){
   return "4";
}

will cause this error: Cannot return string value from int function (test.ash, line 2)

Similarly, for void functions:

void stuff(){
   return 4;
}

will cause this error: Cannot return a value from a void function (test.ash, line 2)

Encountered '[break|continue]' outside loop

This indicates that the control structure in question was not in a loop.

Example:

continue;

will cause this error: Encountered 'continue' outside of loop (test.ash, line 1)

Expected

This generally indicates a syntax problem (missing ending semi-colon, unmatched braces, unmatched parenthesis etc...).

Example:

int a = 1
print(a);

will cause this error: Expected ;, found print (test.ash, line 2)

Function defined multiple times

This indicates that the function in question has already been defined.

Example:

int abs(int a){
    return a*((a>0).to_int()*2 - 1);
}
int abs(int a){
    if(a<0) a = -a;
    return a;
}

will cause this error: Function 'abs( int )' defined multiple times (test.ash, line 4)

Function undefined

This happens when calling a function that KoLMafia doesn't know, either because the script uses a built-in ASH function that doesn't exist yet in the user's build, or because the function hasn't been defined yet, neither in the imported scripts nor in the script itself.

Example:

my_function();

void my_function(){
    print( "hello world" );
}

will cause this error: Function 'my_function()' undefined. This script may require a more recent version of KoLmafia and/or its supporting scripts. (test.ash, line 1)

"if" requires a boolean conditional expression

This indicates that the condition inside an if statement is not a boolean.

Example:

if(42) {
   print("Don't panic.");
}

will cause this error: "if" requires a boolean conditional expression (test.ash, line 1)

Index type is not a primitive type

This indicates that the map's index is not a standard datatype (float, int, string, etc.).

Example:

record alpha{
   int a;
   int b;
};
int[alpha] map;

will cause this error: Index type 'alpha' is not a primitive type (test.ash, line 5)

Invalid field name

This indicates that the record does not contain the field in question.

Example:

record my_record {
   string a;
   string b;
};

my_record [int] my_map;
my_map[1].c = "hello";

will cause this error: Invalid field name 'c' (test.ash, line 7)

Note that this error may also be encountered when neglecting to name a field.

For instance:

record my_record {
   string;
};

will cause this error: Invalid field name ';' (test.ash, line 2)

Invalid type name

This indicates that the specified type is not recognized by Mafia.

Example:

int[fruit] map;

will cause this error: Invalid type name 'fruit' (test.ash, line 1)

Main method must appear at top level

This indicates that the script's main method is inside another method, for some reason. This is usually caused by a missing brace.

Example:

void stuff() {
   // stuff happens here

void main() {
   stuff();
}

will cause this error: main method must appear at top level (test.ash, line 6)

Map modified within foreach

You cannot add or remove new entries into a map within a foreach loop, with the exception of removing the current key. This can happen if you accidentally wrap the name of a string variable with double-quotes for referencing a map entry.

For example, the following code:

/* test.ash */
item [string] my_map;
/* Populate my_map with several entries */
foreach str in my_map
   print( str + " => " + my_map[ "str" ] );

Yields: Map modified within foreach (test.ash, line X)

Missing return value

The last line of a user-defined function has to be "return <value>;" (although the return command can be used before too).

Example:

int my_function( int a, int b ) {	
    if ( a > b ) return a ;
    else return b ;
}

will cause this error: Missing return value (test.ash, line 4)


No closing found

This can be caused by forgetting to put a " or a ] at the end of an argument

Example:

buy(1000 , $item[disco ball, 225);

will cause this error: No closing ] found (testing.ash, line 1)

Record expected

This indicates that an unrecognized record was found. This often appears when you forget to append "()" to a function name.

Example:

print(4.to_string);

will cause this error: Record expected (test.ash, line 2)

Record name is already defined

This indicates that the record name has already been defined, and so cannot be reused. The solution is to change the record name.

Example:

record str {
   string a;
   string b;
};
record str {
   int a;
   int b;
};

will cause this error: Record name 'str' is already defined (test.ash, line 5)

Return needs [datatype] value

This indicates that the return value is void when the function is not.

Example:

int stuff() {
   return;
}

will cause this error: Return needs int value (test.ash, line 2)

Reserved word cannot be a [function|record|variable] name

This indicates that the desired name has been reserved, and so cannot be used. The solution is to use a different name.

An invalid name:

int float(float a) {
   return round(a);
}

will cause this error: Reserved word 'float' cannot be used as a function name (test.ash, line 1)

An interesting point to note is that ASH functions take precedent over identically named custom functions rather than throw an exception.

An invalid record name:

record string {
   string a;
   string b;
};

will cause this error: Reserved word 'string' cannot be a record name (test.ash, line 1)

An invalid variable name:

string item = "bottle of gin";

will cause this error: Reserved word 'item' cannot be a variable name (test.ash, line 1)

Script parsing error

This generally indicates a syntax problem, as in having an excess amount of braces or a variable/function/record name that starts with an invalid character.

Example:

put_shop(0 ,0 ,$item[wolf mask]);
put_shop(0 ,0 ,$item[rave whistle]);
put_shop(0 ,0 ,$item[giant needle]);
cli_execute ("undercut");
}

will cause this error: Script parsing error (test.ash, line 5)

Variable is already defined

This indicates that the same variable has been declared twice.

Example:

int Drops ;
int Meat ;
int Drops = numeric_modifier("Item Drop");

will cause this error: Variable Drops is already defined (test.ash, line 3)


Attention KoLmafia Experts!

We need your help; some details of this function's operation are unknown or unclear.

The following specific question has been raised:

  • Obviously, a lot more info is needed here.