Blog posts

Intro to GrapeScript

Posted: 2009-07-20 05:18:08 (Modified: 2009-07-20 12:58:37) by cyanpjh

GrapeScript is a simple, small language built into the Grape runtime. Homegrown, yet not particularly bad, when code is loaded it is compiled into the bytecode for the homegrown virtual machine (VM).

Editing Code

To edit code, you have to go in the View menu and select Code View. After doing your edits, remember to click "Save and Apply", any errors from compiling will pop up at this point.

Running Code

To run what you've written, select "Run Function..." from Code menu

Functions

To begin, here is an empty function:

sub Main

endsub

There is no actual need for a main function as of yet, and it isn't called on load. However, its a good starting point to see what is possible. GrapeScript arguments are given in two different formats:

Format 1, just simply load the arguments if they exist:

sub Main args (myarg)

endsub

Format 2, executes code before loading the arguments that will overwrite them (essentially default variable values, but a bit more flexible):

sub Main
    args myarg
    defaults
        0 set myarg
    endargs
endsub

The results of function calls are returned with return:

return 0

Finally, functions are called in the following format:

Main()

Printing to the Debug Box

Grape has a small box underneath the code editor, this is where text goes that comes in from the built-in functions print, println and trace. trace and println are essentially the same, and print does the same things as the former but doesn't add a newline at the end.

sub Main
    println ("Hello world")
endsub

Show a MessageBox

Another built in function is the alert function. Which takes in a single argument like print, println, trace, but shows a message box instead. This is most useful for error boxes, as unlike the others, these functions aren't silent in exported Grape modules.

Variable Definition

Variables can be either global or local, the global variables are defined outside the function, and the locals are defined within. Variables can be defined like this:

var a

Which defines a variable a

Expressions, Assignments and Conditionals

The only implemented expressions at the moment are numeric operations, such as +, -, etc. String and boolean operations will be done soon. Every expression in GrapeScript are enclosed in at least one level of parenthesis (this actually changes the parse mode of the GrapeScript parser).

Examples of expressions:

(-10)
(1 + 2)
(a * 3 + 2)

The results of expressions can then be set to a variable like this:

(1 + 2) set a

Conditionals can harness the expressions to do useful things:

if (expr) then
    dosomething()
else 
    if (notherexpr) then
        anotherthing()
    endif
endif

Technically parseable expressions:

((32 != a)                   # 32 != a can also be written as
  equal (32 !equal a)        # this as well as
  equal (32 not equal a)     # this as well as
  equal (32 not == a))       # this
if (!1+!(2*!3)==!(1||!add()) # arbitrary whitespace and newlines
   not == 4-(2/5)!=2&&1+2*3  # don't affect the parsing
   >=!(23 not equal !42) ||  # so any type of formatting 
   0 * !(4 or 8)) then       # is allowed, also there is no semicolons
    1 set a                   # to worry about.
    else if (!1+!2*!3==!1
        ||!
        add() not == 4-2/5!=2&&1+2*3>=23 not equal 42 || 0 * 4 or 8) then
      (1*2+3*4==5*6==7*8&&9*0) set a
    else if (!1+2*3==1||add() not == 4-2/5!=2&&1+2*3>=23 not equal 42 || 0 * 4 or 8)
        then
             (1*2+3*4+5*6+(7*8)+9*0) set a
        else (1*2-3+4/5*6+(7*8)==9*0) set a
        endif
    endif
endif

Loops

Looping is done with the do whiletrue/whilefalse loop:

do (expr) whiletrue
   dosomething()
loop

if you want to loop while something is false, replace whiletrue with whilefalse.

Factorials and Counters in GrapeScript

The following code demonstrates a recursive factorial function, and a counter loop working together.

sub factorial args (n)
    if (n <= 1) then
        return 1
    else
        return (n * factorial(n-1))
    endif
endsub

sub count
    var a
    0 set a
    do (a < 10) whiletrue
        println(factorial(a))
        (a + 1) set a
    loop
endsub
Comments
inductive (posted 2009-07-21) Reply
nice, my grapes are swollen with scripting ideas!
cyanpjh (posted 2009-07-21) Reply
Nice to see you again inductive! How's your keyboard, did you find a replacement/fix?
Back to Home Page