Variables in a scripting language?

BlitzPlus Forums/BlitzPlus Programming/Variables in a scripting language?

neos300(Posted 2009) [#1]
How would i go about making variables for a scripting language, ive already got the idea that i need to use banks, and have working variable writer, but i need to read it too!


Yasha(Posted 2009) [#2]
Why would you use banks?

The way John J does it in GameScript (and how I do it in my shameless ripoff of GameScript) is to have each variable be a type object, with fields for each type of data or pointers to objects. eg.:

Type Var
    Field int_value,float_value#,string_value$,object_pointer
    ;other useful fields (type, name, scope etc.)
End Type


That way acessing the object both ways is equally easy; simply get the variable's object (either from an array or using Object/Handle) and either set or get the field corresponding to its type.


neos300(Posted 2009) [#3]
I kinda figured out that i was supposed to use types by looking at other scripting languages made, but now i am having a problem. Can you please help me with this code?

Var command:
Case "var"
i.var = New var
space02 = Instr(params$, " ", 1)
name$ = Left(params$, space02-1)
value = Mid(params$, space02+1)
i\name = name$
i\value = value
i\create = 1
Print i\value ;debugging

for viewing and adding to the vars:
Default
var.var = FindVar(func$)
space02 = Instr(params$, " ", 1)
op$ = Left(params$, space02-1)
value = Mid(params$, space02+1)
If var\create = 1
Select op$
Case "+"
var\value = var\value + value
Case "*"
var\value = var\value * value
Case "/"
var\value = var\value / value
End Select
Print var\value
Else
echo("Function '" + func$ + "' undefined")
EndIf

My var type
Type var
Field name
Field value
Field create
End Type

Function FindVar.var(name$)
For n.var = Each var
If n\name = name$
Return n
EndIf
Next
End Function

I'm a bit confused on how to access the vars, writing to them is fine, but i need to be able to access them without it crashing.


Yasha(Posted 2009) [#4]
I don't fully understand what you're trying to do here... what's not working?

Also it looks like your script engine is interpreting source code directly. Generally it's easier to write separate compiler and interpreter libraries (not to mention a lot faster when it comes to execution).


neos300(Posted 2009) [#5]
Bit confused about your above statement, why would i need a compiler? It's a scripting language i'm developing.


Yasha(Posted 2009) [#6]
Reading strings is slow, and they're difficult to parse. Most scripting languages compile down to bytecode, which expresses the script in an instruction-set-like format. This is very compact and very easy to interpret, which together with the fact that no strings are involved (unless you're explicitly dealing with strings) makes it very fast indeed (can be up to 30% of the speed of native code). It's also far easier to write a parser that simply outputs instructions than it is to write one that then has to act on the instructions as well.


neos300(Posted 2009) [#7]
Ok.... could you give an me an example or link me a tutorial to compiling and reading bytecode?


Yasha(Posted 2009) [#8]
Well, there are a few...

- The simplest example and probably the best starting point is Mark Sibly's compiler example in the code archives (the only difference between assembly/machine code and bytecode is that bytecode is not inteded to be run by a physical processor).

- As linked above, John J's GameScript SDK seems to be everyone's favourite. It comes with a manual and the source is very well commented. This is probably your best bet.

- My scripting engine isn't as well commented, but the source is also there to look at if it helps (slightly more complex though).

- You can get the source to id's QuakeC compiler and interpreter here. I found this very useful, but be warned that it's messy, poorly commented and worst of all, GPL (so be careful which ideas you borrow). QuakeC is pretty much the original ingame scripting engine.

- Java is the most famous language to use bytecode. More on that here. If you have time, I recommend reading the entire specification (you can download the first and second editions).

There are a few more examples in the code archives, if you have the patience to look, and if you don't mind looking at BlitzMax code there's a lot of stuff to do with Lua in that part of the site, which may also be helpful. Otherwise, hopefully the above is at least a useful starting point...