KonsolScript

From Wikipedia, the free encyclopedia

KonsolScript is an opensource scripting language primarily intended for, but not limited to, writing desktop 2D games.

This project was officially started in November 2005 by MJ Mendoza IV, an information technology student from Saint Mary's University, Philippines.

Its simplicity makes it a candidate for a teaching tool. Unlike BASIC and Pascal, this language would teach programming using the syntax of C programming language. This is because KonsolScript's syntax is mostly derived from Adobe's ActionScript, which is a language influenced by C. KonsolScript is only available for Windows 98, XP and NT.

KonsolScript's script engine / interpreter, called Free KonsolScript Engine or simply FreeKE, is written in Microsoft's Visual Basic 6. It is hosted at SourceForge.

It is possible for KonsolScript to handle graphic files (such as BMP, JPG and GIF), manipulate text files, and execute external files (such as BAT, COM and EXE files). It could also handle WAV and Midi audio formats.


Contents

[edit] KonsolScript Basics

Like its ancestors, C/C++, KonsolScript is case sensitive. The variable "myVar" in not the same as "myVAR".

[edit] Initial Requirements

Before getting started with learning the language, here are the the core necessities.

[edit] FreeKE.EXE file

The first thing you need is the latest Free KonsolScript Engine. FreeKE is written in Microsoft Visual Basic 6, so only Windows user can run FreeKE. A plan of porting FreeKE in C/C++ is taken into consideration.

[edit] MAIN.KS file

The second thing you need is a blank text file. Save it to MAIN dot KS. This is the default file that KonsolScript will be looking onced it is runned. Although, you can tweak FreeKE's configuration to look for another file instead of Main.ks. For simplicity's sake, simply make a Main.ks file.

[edit] Konfig. CFG file

The third and optional thing you need is another blank text file. Save it to Konfig dot CFG. This is FreeKE's configuration file. Although KonsolScript will not basically be looking for it, this is your only way of setting FreeKE's default script file. (written last February 3, 2006 is Config.EXE, a GUI for Konfig. CFG file, included in the official FreeKE distribution)

[edit] Comments

The commenting ability of any programming/scripting language is very much required. Through commenting, programmer gets to continue an abandoned work even for a long period of time. That's the main reason why KonsolScript's author provided the script a commenting ability. To comment a script, the author adapted how C/C++ comments, this is by using two consecutive forward slash '//' for a single line comment and forward slash with asterisk '/*' for multiline commenting to be ended by asterisk-forward slash '*/'.

  //this is single line commenting and will not be interpreted by KonsolScript
  function main() {
    /*
      Whatever is written between the multiline comments will
      not be interpreted by KonsolScript. This is the good way
      to comment things that are supposed to happen to the codes
      that you wrote, whether it'd be in C/C++, Java, etc...
    */
  }

[edit] Smallest runnable KonsolScript

Below is the smallest possible script for KonsolScript to run error-free. Without this KonsolScript will keep on prompting an error.

  function main() {
  }

The word function main() is the KonsolScript's entry point, synanymous to C/C++'s main(), to Java's public static void main(String[] args) or to Visual Basic's Sub Main(). function main is the only required function in a ".ks" file, which is also required to be a Void function and not to have any parameter.

[edit] Hello World (independent)

The code below is header-file independent since all commands that is needed to achieve a simple "Hello World" program is already present. This program will run error-free showing a window with message saying, "Hello World".

  function main() {
    Screen:Show()
    Screen:PrintString("Hello World")
    while(lastpress EQ nochar) {
      Screen:Render()
    }
  }

[edit] Hello World (using header)

KonsolScript distribution is packed with a standard header called "console". This second "Hello World" sample showcases its use.

  #include "console"
  function main() {
    write("Hello World")
    end()
  }

[edit] Void Functions

All functions that doesn't return any value are called Void Functions. Below is a sample of a KonsolScript that calls a user-defined void function.

  function main() {
    UserDefinedFunc()
  }

  function UserDefinedFunc() {
    //codes...
  }

[edit] Non-Void Functions

All functions that returns a Number, String, Boolean values are called Non-Void Functions. Below are two samples of Non-Void functions. Read the comments for information on what is actually happening in the script.

1. KonsolScript that calls a user-defined String function.
  //actual runnable 8-line script; copy and save to "main.ks" file
  function main() {                                                    //L01:KonsolScript's entry point
    Var:String myPath;                                           //L02:declares myPath as String
    GetPath(myPath)                                              //L03:calls GetPath user-function
    Konsol:Message ("This resides in " + myPath + ".", "Test KS") //L04:prompts a message
  }

  function GetPath():String {                                          //L06:user-defined function with no parameter
    return path;                                                 //L07:returns the value of path (KonsolScript constant) to callee
  }

Look in Line #6 and Line #3. Did you notice that there is no parameter specified in function GetPath but function main called the GetPath and provided 1 parameter? That is because the supplied parameter is where GetPath function will return the value of path.

2. KonsolScript that calls a user-defined Number function.
  //actual runnable 15-line script; copy and save to "main.ks" file
  function main() {                                               //L01:KonsolScript's entry point
    Var:Number myNum1 = 8;                                  //L02:declares myNum1 as Integer with initial value of 8
    Var:Number myNum2 = 24;                                 //L03:declares myNum2 as Integer with initial value of 24
    Var:Number iPerc;                                       //L04:declares iPerc as Integer with no initial value
    GetPercent (myNum1, myNum2, iPerc)                       //L05:calls GetPercent user-function
    Konsol:Message ("Value is " + iPerc + "%.", "Test KS")   //L06:prompts a message
  }

  function GetPercent (Number iNum1,Number iNum2):Number {         //L09:user-defined function with two parameters
    Var:Number iRet;                                        //L10:declares iRet as Integer with no initial value
    Math:Div (iNum1, iNum2, iRet)                            //L11:divides iNum1 with iNum2 and sets the value to iRet
    Math:Mul (iRet, 100, iRet)                               //L12:multiplies iRet with 100 and sets the value to iRet
    Math:Round (iRet, 2, iRet)                               //L13:rounds iRet with only two trailing decimal places
    return iRet;                                            //L14:returns the value of iRet to callee
  }

Look in Line #9 and Line #5 of sample 2. Did you notice that there are only 2 parameters specified in function GetPercent but function main called the GetPercent and provided 3 parameters? That is because the third parameter is where GetPercent function will return the value of iRet.

If a Non-Void Function is called, the callee should provide the numbers of Non-Void Function's parameters plus one for the return parameter as the last parameter.

[edit] KonsolScript (pseudo) Classes

KonsolScript Classes are actually neither a real Class nor a Namespace. But in some ways, it works the same.

Most of KonsolScript functions are written after a KonsolScript Class and a colon. So, if you want to add two numbers you need the Math Class to use the Add function.

Syntax:

                KonsolScript_Class:KonsolScript_Function([parameter/s])

Sample Usage:

                Math:Add (1, 1, sum)

Array - Class to be used if you want to declare Array and/or set its value.

Draw - For drawing Rectangle, Circle and Line.

File - Accessing files for Read, Write or both.

Image - For loading and blitting image files.

Konsol - Class to be used for general functions like showing message box.

Math - For manipulating numbers and numeric values.

Mouse - For showing and hiding mouse cursor.

Screen - For displaying messages to screen.

Sound - For loading and playing WAV audio files.

String - For manipulating String variables.

Time - To have access to the PC clock.

Var - Class to be used if you want to declare variable and/or set its value

[edit] External Links