Advertisement

Writing a scripting language

Started by September 14, 2004 03:45 PM
43 comments, last by GameDev.net 20 years, 4 months ago
@redmilamber

Using the java virtual machine ties you into doing things their way, I had a look and it would be very good to make your language run on the vm (and I may still do that) but there is not a lot of room to play with new ideas. Your language has to look like java in the first place or you have to do a lot of fancy stuff which takes away the ease factor.

I actually have no idea if what I just said it true, but it is my best guess.


@frozenheart

Once you have a parser and lexer you need to do the following:

Build a tree from the parser
Make sure the syntax is correct
Compile the bugger.
- Basically for each part of the tree you have to generate code, for example:

code

a = (b / c) + d * (e - f)

tree

     =     |+----+----+|         |a         +          |       +--+--+       |     |       /     *       |     |      +++  +-+-+      | |  |   |      b c  d   -               |              +++              | |               e f


bytecode


load e 1
load f 2
sub 1 1 2
load d 2
mult 1 1 2
load b 2
load c 3
div 2 2 3
add 1 1 2
save a 1

You need to work backwards through your tree (from the right) and take each leaf as it goes.

a
|
| |
b c

Bascially calculate c (if it is a variable then load it) and put it in the first available register
Then calculate b not using the register / stack / whatever that c is in (anything past it is okay)
then do the operation, saving the result where c was

The trick is to have a counter that has the currently available register.

It gets compilcated if you run out of registers.

You have to do the following for each node on the tree.


current free variable = n
calculate c using n
....
current free variable = n+1
calculate b using n+1
...
do operation c storing result in n


It gets different when doing function calls (stack etc.) if you haven't already it is a good idea to look at some assembly.
Just my completely biased and opinionated 2 cents. It was based a lot on guesswork and I have had no experience working with java bytecode.

I was guessing that a language like prolog would be hard to compile into java bytecode mostly due to the weird constructs that it uses. It would be hard to capture the runnings of prolog due to the fact that it is mostly structure based. It would be possible but not easy - which was what I was arguing for, ease of use. It would be far easier to write a prolog vm in C++ than build it into the java vm.

Quote:

Um...in that case, why did you bother saying it? ???


I said that it is my best guess, and I still feel like I have a point, just that I don't know a lot about java bytecode. I know that it is similar to assembly - which I do know about.

[Edited by - umbrae on October 15, 2004 10:56:40 PM]
Advertisement
The first public version of NerveBreak was made in a weekend (plus a Friday). I made the compiler, the VM and a very small function library (print, get, and text file I/O).

The whole "thing" was made to be embeddable in any purpose. Because of that i decided that the VM should perform only stuff similar that a CPU could perform. The initial version included only instructions that perform basic integer math (add, sub, mul, div, mod), stack (push, pop, pushArg, popArg), jumps (jmp) and function calls (call, natcall and ret).

The reason that i made it so quickly is that i had written lots of parsers for almost any reason: games, apps, etc. Everytime i needed scripting in a program, i was writing a parser. Not as powerfull as a complete scripting system, but most of them was more powerfull than configuration files or state scripts.

Since my first programming days (we go back to ~1992) i was fascinated with the idea of creating my own programming language. While now i can't create a programming language that can be used to write complete games (only because of performance reasons), i can say that i can write a good scripting system (if i sit down and work on it of course - today and yesterday i did a lot more work than the last two months :-P).

So one day i decided that i written a lot of code and that i should try to write a full system that i can use later wherever i needed it (well now that i have it, i use it almost in whatever i write - i can say that my apps are "overscripted" :-P).

Of course that initial release had lots of bugs (f.e. functions didn't worked always due stack errors). But it was powerfull enough to be used in order to write a script that generated a web page: it's web page.

So, yes, it is possible to do it in a short of time.
--Slashstone - www.slashstone.comNerveBreak free scripting system.
Check this out for a working scripting language using Boost.Spirit

http://www.gamedev.net/community/forums/topic.asp?topic_id=278376
daerid@gmail.com

This topic is closed to new replies.

Advertisement