Click here to Skip to main content
Licence CPOL
First Posted 27 Oct 2011
Views 4,814
Downloads 312
Bookmarked 21 times

XScriptLib - Scripting language

By | 27 Oct 2011 | Article
XScript is a scripting language I made in C#

Introduction

XScript is a simple scripting language that I made in C#. Even though XScript is not executable, it can do complex math operations.
XScript can be used as a scripting language for engines or math libraries.

Using the Code

To use the XScript compiler, you will have to import the library and declare the XSParser variable.

XSParser xsParser = new XSParser(); 

Use the "ParseMathExpr" function to parse math expression, for example:

double result = xsParser.ParseMathExpr("2+2*(1+3)", new List<XArray>()); // Returns 10 

To return a variable(or array of variables) from XScript code, you have to use the functions:

  • ParseReturnVariable
  • ParseReturnArray
double var = xsParser.ParseReturnVariable(code);
double[] arr = xsParser.ParseReturnArray(code);

XScript Basic Syntax

Every command/function in xscript need to be declared in a new line. To declare a variable/array, use the "dec" word:

dec var
dec arr[5] 

In the first line, we declared a variable (named "var") and in the second line, we declared an array (length: 5, named "arr").

Use the "set" word to set a value to the variable you declared:

set var = 2-3*4
set arr[1+1] = 10+2 

If you want to declare and set a value to an array in the same line you can use the "dset" word:

dset var = 2-3*4
dset arr = 2,101,10+2,-2,-5+1 

To output/return your variable/array, use the "return" word:

return 2+2*3
return 10,5-3,7
return 15,var,arr[2],3
return var 

return arr[3]  

All those words are part of XScript's basic syntax. But XScript allows you to expand its syntax.

XArray

The XArray class is the form of variable/array in XScript. The XArray class has 3 fields:

  • name (string)
  • vars (array of doubles)
  • level (int)

name is simply the Name of the array, vars are the values of the array and I'll explain later about level.
XArray's constructor takes 2 parameters: The name of the array and the length of the array.
You can use the XArray class to parse math expressions with variables/arrays:

List<XArray> arrays = new List<XArray>();
arrays.Add(new XArray("arr", 3));
arrays.Add(new XArray("var", 1));
arrays[0].vars[0] = 10; // arr[0] = 10
arrays[0].vars[1] = 912; // arr[1] = 912
arrays[0].vars[2] = -12; // arr[2] = -12
arrays[1].vars[0] = 8; // var = 8
double result = xsParser.ParseMathExpr("2-var*(3+arr[0]*arr[2])/7+8*arr[1]", arrays);

Commands

Every command in XScript starts with the word "do":

do <command_name> <parameters> 

XScriptLib comes with one built-in command: SumArray. With SumArray, you can sum an array into a variable:

do tsum arr,var 

Where tsum is the SumArray command, arr is an array, var is a variable.

To add your own command to XScript, you need to create a class that inherits from the class: "Command" and override the function: "OverrideCommand":

class MyCommand : Command
{
    public MyCommand() : base("myCmd"){}

    protected override List<XArray> OverrideCommand(string[] parameters, 
        XSParser parser, List<XArray> arrs)
    {
        int v1Index = Helper.ArrayIndex(arrs, parameters[0].Replace(" ",""));
        int v2Index = Helper.ArrayIndex(arrs, parametrs[1].Replace(" ",""));
        if(arrs[v1Index].vars.Length > 1 || arrs[v2Index].vars.Length > 1)
            throw new Exception("Doesn't deal with arrays.");
        int temp = arrs[v1Index].vars[0];
        arrs[v1Index].vars[0] = arrs[v2Index].vars[0];
        arrs[v2Index].vars[0] = temp;
        return arrs;
    }
}

The command I just created is called: "myCmd" and it takes 2 variables and transposes their values.
Before you can use "myCmd" in your code, you need to add it to your compiler's commands list:

xsParser.commands.Add(new MyCommand());  

Processes

In XScript, process is a block with code in it. This block will run if/while... something happened.
*Every time you enter into a new process, your code level increases, at the end of every process, all the variables/arrays above or in the process level with be removed!*
XScriptLib comes with 4 built-in processes: If, While, For, Function.
Every process starts with the word "begin" and ends with the word: "end" for instance:

begin if(var=2)
    set var = var + 1
end

You can run processes inside other processes:

begin if(var=2)
    dset index = 0
    begin while(index < max)
        set arr[index] = index*max
        set index = index + 1
    end
end

To add your own process to XScript, you need to create a class that inherits from the class: "Process" and override the function: "OverrideProcess":

class IfNotProcess : Process
{
    public IfNotProcess() : base("IfNot"){}
    
    protected override List<XArray> OverrideProcess
    (string code, List<XArray> arrs, XSParser parser, string[] cons, int codeLevel)
    {
        if(cons.Length != 1)
            throw new Exception("IfNot takes one parameter.");
        if(!Helper.BooleanExpression(cons[0], arrs, parser))
            arrs = Helper.OnlyBeneathLevel
        (parser.ParseReturn(code, codeLevel+1, arrs), codeLevel);
        return arrs;
    }
} 

The process I just created is called: "IfNot" and it takes one condition/parameter and runs the code only if the condition of false.
Before you can use "IfNot" in your code, you need to add it to your compiler's processes list:

xsParser.processes.Add(new IfNot());  

Boolean Expressions

In XScript, Boolean expression are used in Conditions(If), Loops(while, for). These are the logic operators in XScript:

  • Equal (=)
  • Larger (>)
  • Smaller (<)
  • Not Equal (!)
  • Or (|)
  • And (&)

Functions

In XScript, you use functions to declare repetitive operators such as: sqrt, min, max, sin, cos and so on.
Function is a process that connects with the XScript Math Parser (XMParser).
To declare a function in XScript, you use the "func" word:

begin func(MyFunc,arr1,arr2)

In this line, I declare a function that's called: "MyFunc" and takes two arrays as parameters.
Inside the function, you can write code and use the arrays arr1 and arr2:

begin func(MyFunc,arr1,arr2)
    dset x = 0
    begin if(arr1.len = arr2.len)
        set x = 1
    end
    return x
end 

The function MyFunc gets two arrays as parameters and checks if the length of the first array is equal to the length of the second array.
If yes, the function will be one, otherwise it will return zero.

"params" Parameter

You can use the word params before the name of your function's parameter to make it an array of bunch of parameters the user may enter.

begin func(MyFunc,params arr)
    dset x = 0
    begin if(arr[0] = arr[1])
        set x = 1
    end
    return x
end  

The function MyFunc gets two variables and checks if the first variable is equal to the second.

History

  • 27th October, 2011: Initial post

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

ArikLekar



Israel Israel

Member



Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
GeneralMy vote of 5 Pinmemberdebiprasadghosh1:03 1 Nov '11  
GeneralMy vote of 5 Pinmemberlinuxjr15:30 27 Oct '11  
Questionsuper! PinmemberSouthmountain6:07 27 Oct '11  
AnswerRe: super! PinmemberArikLekar8:40 27 Oct '11  
GeneralInteresting stuff! PinmvpNishant Sivakumar4:07 27 Oct '11  

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Mobile
Web03 | 2.5.120517.1 | Last Updated 27 Oct 2011
Article Copyright 2011 by ArikLekar
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid