Difference between pages "ASH Errors" and "Compiling from Source"

From Kolmafia
(Difference between pages)
Jump to navigation Jump to search
imported>PhilmASTErpLus
(Added "cannot initialize parameter..." (finally!))
 
 
Line 1: Line 1:
{{TOCright}}
+
=== News (once great and big, now secondhand news) ===
Errors happen. When they do, it helps to have some idea of what what wrong.
 
  
===Abort===
+
==KoLmafia has migrated to the GitHub, GIT and gradle.==
 +
The Windows, Linux and macOS instructions have been updated, and might have been lightly tested
  
Whenever a script runs the [[abort|abort()]] function, this error is generated.
+
The good news is that the [[Development Environment Setup|IDE setup]] is a lot easier.  The bad news is you'll need to take some steps to build anything.
  
{{CodeSample|
+
To contribute to the Mafia project, you'll need to be able to install the Java Development Kit, connect to the git repository, and be comfortable writing Java Code. Instructions for the first two are included below:
description=The zero-parameter form:|
 
code=
 
<syntaxhighlight>
 
abort();
 
</syntaxhighlight>}}
 
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|
+
==Windows==
description=The one-parameter form:|
+
# Download Tools
code=
+
## Download and install Java Development Kit 17 or newer, available from [https://adoptium.net/temurin/releases/?version=17 Adoptium].  The minimum recommended version is Java 17, but check the *Recommended Java* version on the [https://kolmafia.us KoLmafia Forums] to be up to date].
<syntaxhighlight>
+
## Download and install GIT following the instructions from [https://www.atlassian.com/git/tutorials/install-git#windows Atlassian's GIT install page.]
abort("Aborting script...");
+
## Download and install Tortoise GIT (if needed) from https://tortoisegit.org
</syntaxhighlight>}}
+
# Configure Windows Environment Variables[[File:Environment vars.jpg|thumb|Win 10 Environment Variable Editor]]
will stop the execution and print: <span style="color:red">Aborting script...</span>
+
## Windows 10
 +
### Go to settings
 +
### type 'enviro' in the search
 +
### click on the 'edit environment variables' suggestion
 +
### add or edit JAVA_HOME, and PATH variables<br>''PATH should include JAVA_HOME''</br>If the JAVA installer has already set these variables, they can be updated or left alone.
 +
# Open a new Command Prompt (or Powershell) window and navigate to the directory to which you wish to create kolmafia (e.g. <code>cd %USERPROFILE%\projects</code>, or some similar directory)
 +
# Clone the source code for the KoLmafia Project.<br>
 +
##''If you have a github account:''<syntaxhighlight lang="bash">
 +
git clone git@github.com:kolmafia/kolmafia.git</syntaxhighlight>
 +
##''If not:''<syntaxhighlight lang="bash">git clone https://github.com/kolmafia/kolmafia.git</syntaxhighlight>
 +
# Compile KoLmafia with the Gradle wrapper ''<syntaxhighlight lang="bash">cd kolmafia && gradlew.bat runShadow</syntaxhighlight>''
  
===Cannot initialize parameter [parameter]====
+
=== Optional ===
 +
If you intend to commit code to GitHub, you will need to follow GitHub's instructions for [https://docs.github.com/en/authentication/keeping-your-account-and-data-secure/creating-a-personal-access-token adding a Personal Access Token (PAT)]
 +
==Linux==
  
This error is created when an assignment is attempted inside a function declaration.
+
Do once:
The following code results in an error.
+
# Install git - you probably already have this. If not, follow [https://www.atlassian.com/git/tutorials/install-git#linux this guide.]
 +
# Get an appropriate JDK. If your package manager does not contain a sufficiently up-to-date version of Java, the safest and easiest way to do this is with [https://adoptium.net/installation.html#x64_linux-jdk Adoptium].
 +
# Clone the repo:
 +
## If you have a github account:<syntaxhighlight lang="bash">
 +
git clone git@github.com:kolmafia/kolmafia.git
 +
</syntaxhighlight>If not:<pre>git clone https://github.com/kolmafia/kolmafia.git</pre>
  
{{CodeSample|
+
Do every session:
code=
+
# Set up your environment to use that JDK you just downloaded<pre> export PATH="/where/you/put/that/jdk/bin:${PATH}"</pre> (Optionally, put this in your ''.profile'' or ''.bashrc'' if you want to set and forget.)
<syntaxhighlight>
+
# Navigate to your cloned repository: <pre>cd /where/you/cloned/kolmafia</pre>
/* test.ash */
+
# Get new updates with <pre>git pull</pre>  If you have local uncommitted changes, you may want to instead use ''git stash && git pull && git stash pop''.
 +
# Build the new jar: <pre>./gradlew clean shadowJar</pre>
  
void my_function( string myvar = "value" )
+
You have a KoLmafia jar file that can be invoked with <pre>java -jar dist/KoLmafia-*.jar</pre>
{
 
  /* Do something */
 
}
 
</syntaxhighlight>}}
 
  
will halt the script and print: <span style="color: red;">Cannot initialize parameter myvar (test.ash, line X)</span>
+
Do other installation steps as you wish.
  
===Cannot return [datatype] from [datatype] function===
+
==macOS==
 +
# Verify required packages
 +
## <code>''java --version''</code>
 +
##* if not found or less than Java 17...  Install Java Development Kit version 17 from [https://adoptium.net Adoptium Temurin]
 +
## <code>''git --version''</code>
 +
##* If not found ...  Install git:  Atlassian has a great document on [https://www.atlassian.com/git/tutorials/install-git the various ways to install Git]
 +
# Configure development environment<br>Most of these steps can be included in ''<code>.profile</code>'' or the equivalent for your shell of choice.
 +
## In the terminal set JAVA_HOME to the java home directory. <br>''<code>/usr/libexec/java_home -V</code>'' will list all known values for java_home.<br>If you have multiple copies of java, choose the version you want to use''.'' <code>''export JAVA_HOME=`/usr/libexec/java_home -v17''</code>
 +
# Open a new terminal window and navigate to the directory to which you wish to create kolmafia (e.g. <code>cd ~/projects</code>, or some similar directory)
 +
# Clone the source code for the KoLmafia Project.<br>
 +
##''If you have a github account:''<syntaxhighlight lang="bash">
 +
git clone git@github.com:kolmafia/kolmafia.git</syntaxhighlight>
 +
##''If not:''<syntaxhighlight lang="bash">git clone https://github.com/kolmafia/kolmafia.git</syntaxhighlight>
  
This indicates that the return value does not match the function's type.
+
Do every session:
  
{{CodeSample|
+
# Navigate to your cloned repository: <pre>cd ~/projects/kolmafia</pre>
description=Example:|
+
# Get new updates with <pre>git pull</pre> If you have local uncommitted changes, you may want to instead use ''git stash && git pull && git stash pop''.
code=
+
# Compile KoLmafia with the Gradle wrapper: ''<syntaxhighlight lang="bash">./gradlew runShadow</syntaxhighlight>''
<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|
+
=== Optional ===
description=Similarly, for void functions:|
+
If you intend to commit code to GitHub, you will need to follow GitHub's instructions for [https://docs.github.com/en/authentication/keeping-your-account-and-data-secure/creating-a-personal-access-token adding a Personal Access Token (PAT)]
code=
+
''[[Category:Tech Support]]''
<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===
 
 
 
This generally indicates a syntax problem (missing ending semi-colon, unmatched braces, unmatched parenthesis etc...).
 
 
 
{{CodeSample|
 
description=Example:|
 
code=
 
<syntaxhighlight>
 
int a = 1
 
print(a);
 
</syntaxhighlight>}}
 
will cause this error: <span style="color:red">Expected ;, found print (test.ash, line 2)</span>
 
 
 
===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>
 
 
 
==="if" requires a boolean conditional expression ===
 
 
 
This indicates that the condition inside an if statement is not a boolean.
 
 
 
{{CodeSample|
 
description=Example:|
 
code=
 
<syntaxhighlight>
 
if(42) {
 
  print("Don't panic.");
 
}
 
</syntaxhighlight>}}
 
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>
 
 
 
===Invalid field name===
 
 
 
This indicates that the record does not contain the field in question.
 
 
 
{{CodeSample|
 
description=Example:|
 
code=
 
<syntaxhighlight>
 
record my_record {
 
  string a;
 
  string b;
 
};
 
 
 
my_record [int] my_map;
 
my_map[1].c = "hello";
 
</syntaxhighlight>}}
 
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>
 
 
 
===Invalid type name===
 
 
 
This indicates that the specified type is not recognized by Mafia.
 
 
 
{{CodeSample|
 
description=Example:|
 
code=
 
<syntaxhighlight>
 
int[fruit] map;
 
</syntaxhighlight>}}
 
will cause this error: <span style="color:red">Invalid type name 'fruit' (test.ash, line 1)</span>
 
 
 
===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.
 
 
 
{{CodeSample|
 
description=Example:|
 
code=
 
<syntaxhighlight>
 
void stuff() {
 
  // stuff happens here
 
 
 
void main() {
 
  stuff();
 
}
 
</syntaxhighlight>}}
 
will cause this error: <span style="color:red">main method must appear at top level (test.ash, line 6)</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===
 
 
 
This indicates that an unrecognized record was found. This often appears when you forget to append "()" to a function name.
 
 
 
{{CodeSample|
 
description=Example:|
 
code=
 
<syntaxhighlight>
 
print(4.to_string);
 
</syntaxhighlight>}}
 
will cause this error: <span style="color:red">Record expected (test.ash, line 2)</span>
 
 
 
===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.
 
 
 
{{CodeSample|
 
description=Example:|
 
code=
 
<syntaxhighlight>
 
record str {
 
  string a;
 
  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>
 
 
 
===Return needs [datatype] value===
 
 
 
This indicates that the return value is void when the function is not.
 
 
 
{{CodeSample|
 
description=Example:|
 
code=
 
<syntaxhighlight>
 
int stuff() {
 
  return;
 
}
 
</syntaxhighlight>}}
 
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.
 
 
 
{{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>
 
 
 
An interesting point to note is that ASH functions take precedent over identically named custom functions rather than throw an exception.
 
 
 
{{CodeSample|
 
description=An invalid record name:|
 
code=
 
<syntaxhighlight>
 
record string {
 
  string a;
 
  string b;
 
};
 
</syntaxhighlight>}}
 
will cause this error: <span style="color:red">Reserved word 'string' cannot be a record name (test.ash, line 1)</span>
 
 
 
{{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 same variable has been declared twice.
 
 
 
{{CodeSample|
 
description=Example:|
 
code=
 
<syntaxhighlight>
 
int Drops ;
 
int Meat ;
 
int Drops = numeric_modifier("Item Drop");
 
</syntaxhighlight>}}
 
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.}}
 
 
 
[[Category:Scripting]]
 

Revision as of 00:56, 5 December 2023

News (once great and big, now secondhand news)

KoLmafia has migrated to the GitHub, GIT and gradle.

The Windows, Linux and macOS instructions have been updated, and might have been lightly tested

The good news is that the IDE setup is a lot easier. The bad news is you'll need to take some steps to build anything.

To contribute to the Mafia project, you'll need to be able to install the Java Development Kit, connect to the git repository, and be comfortable writing Java Code. Instructions for the first two are included below:

Windows

  1. Download Tools
    1. Download and install Java Development Kit 17 or newer, available from Adoptium. The minimum recommended version is Java 17, but check the *Recommended Java* version on the KoLmafia Forums to be up to date].
    2. Download and install GIT following the instructions from Atlassian's GIT install page.
    3. Download and install Tortoise GIT (if needed) from https://tortoisegit.org
  2. Configure Windows Environment Variables
    Win 10 Environment Variable Editor
    1. Windows 10
      1. Go to settings
      2. type 'enviro' in the search
      3. click on the 'edit environment variables' suggestion
      4. add or edit JAVA_HOME, and PATH variables
        PATH should include JAVA_HOME
        If the JAVA installer has already set these variables, they can be updated or left alone.
  3. Open a new Command Prompt (or Powershell) window and navigate to the directory to which you wish to create kolmafia (e.g. cd %USERPROFILE%\projects, or some similar directory)
  4. Clone the source code for the KoLmafia Project.
    1. If you have a github account:
      git clone git@github.com:kolmafia/kolmafia.git
      
    2. If not:
      git clone https://github.com/kolmafia/kolmafia.git
      
  5. Compile KoLmafia with the Gradle wrapper
    cd kolmafia && gradlew.bat runShadow
    

Optional

If you intend to commit code to GitHub, you will need to follow GitHub's instructions for adding a Personal Access Token (PAT)

Linux

Do once:

  1. Install git - you probably already have this. If not, follow this guide.
  2. Get an appropriate JDK. If your package manager does not contain a sufficiently up-to-date version of Java, the safest and easiest way to do this is with Adoptium.
  3. Clone the repo:
    1. If you have a github account:
      git clone git@github.com:kolmafia/kolmafia.git
      
      If not:
      git clone https://github.com/kolmafia/kolmafia.git

Do every session:

  1. Set up your environment to use that JDK you just downloaded
     export PATH="/where/you/put/that/jdk/bin:${PATH}"
    (Optionally, put this in your .profile or .bashrc if you want to set and forget.)
  2. Navigate to your cloned repository:
    cd /where/you/cloned/kolmafia
  3. Get new updates with
    git pull
    If you have local uncommitted changes, you may want to instead use git stash && git pull && git stash pop.
  4. Build the new jar:
    ./gradlew clean shadowJar

You have a KoLmafia jar file that can be invoked with

java -jar dist/KoLmafia-*.jar

Do other installation steps as you wish.

macOS

  1. Verify required packages
    1. java --version
      • if not found or less than Java 17... Install Java Development Kit version 17 from Adoptium Temurin
    2. git --version
  2. Configure development environment
    Most of these steps can be included in .profile or the equivalent for your shell of choice.
    1. In the terminal set JAVA_HOME to the java home directory.
      /usr/libexec/java_home -V will list all known values for java_home.
      If you have multiple copies of java, choose the version you want to use. export JAVA_HOME=`/usr/libexec/java_home -v17
  3. Open a new terminal window and navigate to the directory to which you wish to create kolmafia (e.g. cd ~/projects, or some similar directory)
  4. Clone the source code for the KoLmafia Project.
    1. If you have a github account:
      git clone git@github.com:kolmafia/kolmafia.git
      
    2. If not:
      git clone https://github.com/kolmafia/kolmafia.git
      

Do every session:

  1. Navigate to your cloned repository:
    cd ~/projects/kolmafia
  2. Get new updates with
    git pull
    If you have local uncommitted changes, you may want to instead use git stash && git pull && git stash pop.
  3. Compile KoLmafia with the Gradle wrapper:
    ./gradlew runShadow
    

Optional

If you intend to commit code to GitHub, you will need to follow GitHub's instructions for adding a Personal Access Token (PAT) '