Click here to Skip to main content
Click here to Skip to main content

Twiggery Scripting Language

, 12 Aug 2010 LGPL3
Rate this:
Please Sign up or sign in to vote.
Twiggery Scripting Language

Introduction

Twiggery is a tiny but useful scripting language. A full Twiggery system consists of a compiler and a virtual machine. And it’s easier than any other scripting languages to combine the script with the host program (C++, Java, C#, etc.), minimum TVM implementation is no more than 25KB of source code.

Background

Programming games are so funny! The simple reason is that you get to code so many different types of subsystems in a game, regardless of whether a renderer, sound system, AI, or game logical code; it’s full of challenges in all of these types of programming that you get to solve. In a lot of cases, using scripting is an ideal way of isolating game code from the main engine. On PC platform, MAC and consoles there are many powerful and mature scripting languages for developers to chose, such as Lua, Python, Ruby etc. But on mobile platforms like J2ME cell phones, there is no choice for a developer, that’s why this fine scripting language was created. You’ll learn what a Twiggery scripting language is and how it works; the best usage of Twiggery is using it as a cell phone scripting language; and, of course, you’ll learn a little about compiler theory. You’ll even get to examine how a full Twiggery Virtual Machine is developed! And if you love coding especially coding games, I'm confident that you'll enjoy finding out more about this tiny scripting language. Have fun!

How to Code with Twiggery

Basic Syntax

Keywords

There are only 17 keywords in Twiggery, see below:

  • function
  • if
  • elseif
  • else
  • for
  • to
  • step
  • while
  • do
  • break
  • continue
  • return
  • and
  • or
  • not
  • true
  • false

The meaning of each word will be mentioned in the following parts.

Twiggery is a case sensitive language, it is to say that “function” is a keyword and means a head of a script function, but Function or FUNCTION is different from “function” in syntax.

Operators

There are 15 operators in Twiggery, see below:

Type

Lexicon

Description

Example

Assignment

=

Assign a value to a variable

pi = 3.14: pi is 3.14

Numeric

calculation

+

Add a pair of value up

r = 2 + 3: r is 5

-

Minus one value with another

r = 5 – 2: r is 3

*

Multiply two value together

r = 3 * 7: r is 21

/

Divide one value with another

r = 18 / 3: r is 6

%

Mod operation

r = 10 % 3: r is 1

Numeric

comparison

<

Be less than

c = 1 < 2: c is true

>

Be greater than

c = 1 > 2: c is false

<=

Be less than or equals with

c = 1 <= 2: c is true

>=

Be greater than or equals with

c = 1 >= 1: c is true

==

Equals with

c = 4 == 5: c is false

~=

Not equals with

c = 4 ~= 5: c is true

Boolean

operation

and

“And” logic

See “operators”

or

“Or” logic

not

“Not” logic

The keyword “false” means result of an expression which is false, it is equal to numeric zero like in C language; and “true” means true or not zero. “false” or “true” is a result value of a numeric comparison or a Boolean operation.

Keywords “and”, “or” and “not” are also operators. See the Twiggery operation priority level table.

Level Operation
1 * / %
2 + -
3 < > <= >= ~= ==
4 not
5 and or
6 =

The priority of level 1 is the highest and level 6 is the lowest. Higher level operations are done before the lower ones. A formula is processed from left to right; operations in a same level are dealt in the same way. Brackets ‘(‘ and ‘)’ are used in pair to process formula between them; like 5 * (1 + 2) % 4 returns value 3.

Comment

A compiler would do nothing but skipping with comments. That does not mean comments are not important, comment plays an important role as code notes and can give much information to the code reader.

Twiggery comments begin with a character ‘ and end at the end of line. Anything after ‘ would be skipped. See below:

' Welcome to Twiggery Scripting Language. This line is a comment.
function main() {
  output("Hello, Twiggery!"); ' Output a string. This is also a comment.
} 

Variable and Operation

Data Type

There are three data types in Twiggery, Numeric, Boolean and String.

Numeric is a basic data type. Twiggery only contains one numerical type; it is stored in 32bit, and always defined as an IEEE754 standard float type. Its range is 3.402823e+38 ~ 1.401298e-45.

Boolean can be assigned with one of two values, “true” or “false”. In Twiggery, a boolean is stored and treated similar as Numeric in 32bit; numerical value zero means “false”, and all non zero means “true”.

String is implemented by native string type in different TVM coding languages. String can only be used as the argument of a function. Like: output("Twiggery");.

You can do Numeric and Boolean calculation, comparison and assignment in Twiggery, as shown below:

' assignment 
 pi = 3.14; 
' remote function call, input a value into 'r' 
 input ( r );  
 ' calculation  
 s  = pi * r * r; 
' compare two value and assignment the result to a boolean 
 t = 1 < 2; ' t is true 
 f = 1 == 2; ' f is false 
' two simple variables 
 a = 1; 
 b = 0; 
' boolean operation between a numeric and a boolean const 
 b1 = a or false; ' b1 is true 
 b2 = not b and true; ' b2 is true 

Every variable in a script is global, that is to say, you can access any variable wherever and whenever. Declaration of a variable type is not required because Twiggery is a loosely-typed language, if you want to use one, just code it down.

Numeric Calculation

The main purpose of a programming language is data processing, and the main purpose of data processing is numerical calculation.

There are five Numeric calculation operations. Nothing is easier than this, so more explanation will make you bored. You can use +, -, *, /, % as add, minus, multiply, divide and mod like below:

 s = v0 * t + (1 / 2) * 9.8 * t * t;
 m = 10 % 3; ' m is 1 

Numeric Comparison

These operations compare two numbers and return Boolean values, they give out “true” or “false” depending on whether or not the comparison formulas are equal. Any of the comparisons could only give one of “true” or “false”. See below for Twiggery numeric comparisons.

Comparison Sample Description (result as true)
< ex1 < ex2 ex1 is less than ex2
> ex1 > ex2 ex1 is greater than ex2
<= ex1 <= ex2 ex1 is less than or equal to ex2
>= ex1 >= ex2 ex1 is greater than or equal to ex2
== ex1 == ex2 ex1 is equal to ex2
~= ex1 ~= ex2 ex1 is not equal to ex2

Boolean Operation

Boolean is a special type expressing way of numeric, so Boolean and numeric comparisons are supported. “true” is dealt as nonzero and “false” as zero.

An execution controlling Twig uses a boolean value as its condition. More details will be shown later.

String

Twiggery is designed as a JavaME embedded scripting language at first, just for easy logical controlling and numeric processing. So String operation is not a strong point of it.

String type values are only allowed to pass as a remote function argument in some usage. For example, pass a String “Hello” to the standard output function “output”, and the String will be shown on standard console.

Function

As you see, there is only one function in a script source file in current version of Twiggery. Unlike other complex programming languages, Twiggery is a tiny and easy embedded script, so the designing philosophy is “Do more in less”.

Each Twiggery script source contains only one function without arguments. The execution flow starts from the first line of the function. Multi function calling and function arguments supporting at enter point may be optional in later versions.

Execution Structures

We call Twiggery a scripting language but not a formula processor, because there are three types of execution structures like other programming languages, that is serial, conditional and loop flows. In this part, you will learn how to use them.

Similar to C/C++/Java/C#, etc. Twiggery scope is determined by the placement of curly braces {}, and a statement is ended by a semicolon;. Braces must be used in pair. It is allowed but not suggested to write several statements in a single line.

Serial Flow

Serial flow statements are written one by one, and they are executed according to the sequence you write. For example, test some code like below.

function main() {
 ' Now let's do some calculating.
  pi = 3.1415926; ' Step 1: Assign p to variable pi
  r = input(); ' Step 2: Input a value to variable r
  s = pi * r * r; ' Step 3: Calculate area of a circle
  output( s); ' Step 4: Output the result
} 

As you see, each Twiggery script source contains only one “function”. In Step 1, a float type value 3.1415926 is assigned to a variable named pi; in Step 2, we call a standard remote function “input” to get a value from user input(console or input box…) and set it to variable r; in Step 3, we do a calculation as you know: S =pR2; Finally, the result area “s” is shown in the last Step 4.

It is so easy to get start with Twiggery, isn’t it?

if-elseif-else

Standard “if” twig follows the syntax as below:

if( expression) {
   twig_body;
}

If the result of the formula expression is “true” or nonzero, the sub twig “twig_body” will be executed; otherwise it will turn to the next twig followed after this “if” block.

A twig body contains one or several sub twigs. For example:

n =  input();
if(<a name="OLE_LINK14"> n </a>% 400 == 0 or ( n % 4 == 0 and  n % 100 ~= 0)) {
   output("leap year");
}

There is no “optimal path” in Twiggery. That means the formula after “or” will be calculated every time. So, even “ n % 400 == 0” is true in this sample; and “n % 100 ~= 0” will be processed in any case.

It is allowed to append at most one “else” twig after “if” twig directly. Furthermore “if-elseifelse” structure is very useful in complex conditional twig. “elseif” and “else” are both optional in an “if-elseifelse” structure. See below:

c = input(); ' Input a variable.
if( c == 1) { ' If the variable "c" equals to "1"...
  output("Uno"); ' Call a system function to output something.
} elseif(c == 2) {
  output("Dos");
} elseif(c == 3) {
  output("Tres");
} else {
  output(c);
} 

In this sample, the second condition ”c == 2” is determinate only if “c” is not 1, the third is similar to that. The last “output(c)” is executed only if “c” is not 1, 2 and 3.

for, while and do-while loop

if-elseifelse” twig could only provide skip ability to Twiggery. In this section, you will learn some useful twig with repeating ability.

There are three types of repeating twig: “for-to-step”, “while” and “do-while”.

The “for” twig is deemed as fixed step loop. See below:

 n = 1; ' Initialize "n" with value "1".
 e = 10;
for( i = 1 to e step 1) { ' A loop.
   n  = n * i;
 }
 output ( n );  ' Output n!

You see in this sample, variable “i" is counted from 1 to e(as 10) and the loop step is 1. That means the twig body “n = n * I” is dealt with 10 times. The default value of loop step is 1, so this twig head can be written like “for(i = 1 to e)” as well.

We do not know how many steps the loop should be, sometimes. For this purpose, dynamic variable loops are necessary. There are two kinds of unfixed loops in Twiggery, “while” and “do-while” loop.

while” and “do-while” twigs are conditional loops in fact. It will head to a twig body execution once the condition is true in an “if” twig; and the twig body will be executed again and again until the loop condition is not true anymore in “while” and “do-while”; the difference between these two loops is, if the condition is false then the “while” twig body would not be treated, but whatever the condition value is, the twig body would run at least once in “do-while” twig, that is because of a loop condition is judged before twig body in a “while” but after twig body in a “do-while”. For example:

 a = 0;
while( a <= 5) {
  ' Execute inside if loop condition is true
   output ( a );
   a = a + 1;
 }
 b  = 0;
do  {
  ' Execute inside at least once whether loop condition is true or false
  output(b);
  b = b + 1;
} while(b <= 5)

break, continue and return

break” in Twiggery is used to interrupt current loop and continue to execute the program after it. It is the same as C programming language. It always breaks off current loop by external condition trigger (usually tested by an “if” twig). “break” can be used in “for”, “while” and “do-while” twigs.

 num =  input();
 count =  num / 2;
while( count > 0) {
  if( num %  count == 0) {
     output( count, " is the largest factor of ",  num);
    break;
  }
   count =  count - 1;
}

This script will find out the largest factor of the given number “num”. We iterate all possible numbers, and use “count” as a loop variable, reject all numbers that cannot be divided exactly by “num” in descending order. Then the first number which can be divided exactly by “num” is the one we are looking for, and there is no need to do more loop execution if we find it, we just use “break” to exit the loop.

There is no difference between “continue” in Twiggery and other programming languages. It can be used in “for”, “while” and “do-while”, like “break”. “while” and “do-while” are conditional loops and “for” is a iterative loop. Whether or not the next circle of a loop will be executed when it meets a “continue” also depends on the demand of a loop type; a loop will be terminated normally when it is not satisfied. See below:

 valid = false;
 count = 3;
while( count > 0) {
  it = input();
  if(it == 123) {
    valid = true;
    break;
  }
  if(not valid) {
    output("Invalid");
    count = count - 1;
    continue;
  } else {
    break;
  }
}

Sometimes we want to break deeply out of a whole function and exit an execution of a script, “return” is for this; “return” is only used to do such thing but cannot return a value to outer caller. See below:

for( i = 0 to 10) {
   n =  input();
  if( i %  n ~= 0) {
    return;
  }
}

The program will be terminated directly if “i % n ~= 0” is true with no doubt.

Input and Output

We can do nothing using a programming language without input and output but only internal processing. In Twiggery, there is one standard input function and output function. The input” function gets no arguments and returns a numeric value and the “output” function gets at least one argument in numeric or string format and outputs it to standard stream (a message box on Windows, a line printing on console, etc.).

History

  • Aug. 12, 2010 - Ver: 0.5.2.44
    • Add try-catch for C# TVM to load and run bytecode directly
    • Add drag drop support for script text box
    • Bytecode loading bugfix in Java TVM
  • July. 28, 2010 - Ver: 0.5.1.43
    • Audio plugin bugfix
    • Add array support to the Core plugin
    • Add dynamic multi arguments support to all TVMs
    • Accept function call in a common formula
    • Negative symbol bugfix
  • July. 3, 2010 - Ver: 0.4.2.38
    • Script internal exception bugfix
  • June. 24, 2010 - Ver: 0.4.1.36
    • Add a plugin loading process window
    • Set '.twg' and '.tad' file related
    • Add a new Core plugin
    • Add auto-compleate tip to CodeLeaf
    • Condition expression parsing bugfix
    • Add some new interface in the Graphics plugin
  • June. 18, 2010 - Ver: 0.4.0.30
    • Modify the Graphics plugin: put canvas image in a PictureBox instead of right in the form
    • Add keyword 'do' and 'do-while' twig support
    • Add negative symbol surpport
    • Reconfigurate the Graphics plugin
  • June. 16, 2010 - Ver: 0.3.5.28
    • Show heap unit usage in RAM box
    • Accept multi arguments in standard function 'input' and 'output'
    • Swap two operands if their addressing mode are all AM_STK
    • TASM show box items count bugfix
    • Add string type plugin function argument support
    • Modify the Graphics plugin: create Canvas, draw Sprites, get KeyDown, etc.
    • Modify the Audio plugin: C# source code format plugin, play *.wav file
    • Modify the IO plugin: open, close, write, read a file
    • Push arguments back into stack if get operand failed
    • 'if-elseif-else' block compiling bugfix
    • Set default 'for' loop step argument value as 1
    • Get returned value as format 'a = input();'
    • Reconfigurate exception messages
    • Add new example scripts using plugins
    • Escape 'true', 'false' into '1', '0' in function arguments.
  • June. 12, 2010 - Ver: 0.2.3.25
    • Add compiled assembly DLL and C# source code plugin surpport
    • Use 'v is t' instead of 'v.GetType() == typeof(t)' in TVM execution.
  • June. 9, 2010 - Ver: 0.1.2.24
    • 'if-elseif-else' jump instruction compiling bugfix
  • Feb. 24, 2010 - Ver: 0.1.0.20
    • First released edition. Full compiler and VM

License

This article, along with any associated source code and files, is licensed under The GNU Lesser General Public License (LGPLv3)

Share

About the Author

paladin_t
Architect
China China
Video game player & creator.

Comments and Discussions

 
QuestionThe Best! New Version? PinmemberMember 45586618-Dec-12 3:42 
GeneralMy vote of 5 PinmvpCPallini26-Apr-12 9:56 
GeneralRe: My vote of 5 Pinmemberpaladin_t26-Apr-12 13:59 
GeneralThis is good PinmemberRick York12-Aug-10 10:51 
GeneralRe: This is good PinmemberTony's Toy12-Aug-10 14:48 
GeneralMy vote of 5 Pinmemberenan12-Aug-10 3:29 
GeneralRe: My vote of 5 PinmemberTony's Toy12-Aug-10 14:28 
GeneralJSR 223 compliance PinmemberSumit Pandya2-Aug-10 21:02 
GeneralRe: JSR 223 compliance PinmemberTony's Toy12-Aug-10 3:03 
GeneralSo amazing!!! So good.... PinmemberAntonom2-Aug-10 18:21 
GeneralRe: So amazing!!! So good.... PinmemberTony's Toy12-Aug-10 2:59 
Thanks a lot!
GeneralGreat Work PinmemberTL Wallace1-Aug-10 11:11 
GeneralRe: Great Work PinmemberTony's Toy12-Aug-10 2:58 
GeneralMy vote of 5 PinmemberTL Wallace1-Aug-10 11:11 
GeneralRe: My vote of 5 PinmemberTony's Toy12-Aug-10 2:57 
GeneralQuestions [modified] PinmvpPIEBALDconsult31-Jul-10 6:36 
GeneralRe: Questions PinmemberTony's Toy31-Jul-10 18:24 
GeneralRe: Questions PinmvpPIEBALDconsult1-Aug-10 4:09 
GeneralRe: Questions PinmemberTony's Toy1-Aug-10 4:29 
GeneralRe: Questions [modified] PinmvpCPallini26-Apr-12 9:55 
GeneralRe: Questions [modified] PinmemberPIEBALDconsult26-Apr-12 12:52 
GeneralRe: Questions [modified] PinmvpCPallini26-Apr-12 21:25 
GeneralRe: Questions [modified] PinmemberPIEBALDconsult27-Apr-12 3:08 
GeneralRe: Questions [modified] PinmvpCPallini27-Apr-12 3:17 
GeneralLooks nice... PinmentorSandeep Mewara31-Jul-10 3:38 

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.

| Advertise | Privacy | Mobile
Web02 | 2.8.141022.1 | Last Updated 12 Aug 2010
Article Copyright 2010 by paladin_t
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid