Author Topic: Creating a scripting engine.  (Read 3295 times)

PumpkinPieman

  • *
  • Posts: 256
    • View Profile
    • http://PumpkinPieman.blogspot.com
Creating a scripting engine.
« on: 2004-02-26 23:43:45 »
Has anyone here ever tried to make a scripting engine? Over the past two weeks I have thought about it and wondered how I can design functions within scripts. Along with that I have also been wondering how scripted functions can get parameters.

Cyberman

  • *
  • Posts: 1572
    • View Profile
Re: Creating a scripting engine.
« Reply #1 on: 2004-02-27 05:04:01 »
Quote from: PumpkinPieman
Has anyone here ever tried to make a scripting engine? Over the past two weeks I have thought about it and wondered how I can design functions within scripts. Along with that I have also been wondering how scripted functions can get parameters.

Well funny you should ask about that..
I know a bit about this very subject..
For scripting you have to consider WHAT YOU ARE SCRIPTING first.
For example game? What type of parameters and data are you going to need etc.
Passing Parameters is not hard either.. however the subject is not really about scripts..

You create a script engine to run the script.. right? This is called an
'Anstract machine' much like.. say JAVA.  Like Java  the script can be interpreted or compiled.  Binary form that is.  Compiled script is what you see in FF7 (in all those field files).  Variables are likely globalized to the script.  Parameters can be passed by utilizing a stack in your VM <Virtual Machine>. Function calls? well just implement them like you normally would in assembly by hand and work backwards to the scripting language.

Well anyhow, it's not an easy task to say the least but it's quite doable. I suggest you start with what you scripting language needs to do. No really I mean very basic functions. Like IO variables and data types. Find which ones are best implemented as a single code or implemented as part of your scripting library.  KEEP IT SIMPLE. Complex Instructions are complicated to use. Remember that.  Now you need to think of a code structure for your complied script.  IE opcode set what data types and instructions are neede. Conditional and relative branching subroutine calls absolute jumps data pushed on stack etc.

After that you need to implement your script compilor.  I suggest using Lex and Yacc to constract it, or Flex and Bison if you use GNU tools.  These will give you your lexical analyzer and token interpretation layers. After that you need to create your code generator and possbly your optmization phase if you think you need it.  Last but not least you need to link this with whatever pregenerated code you want if that's part of your language. IE Libraries.

Then you need something to execute your byte code generated by all this.  Time to make your Script engine to execute the script.  

That's kind of what's involved, believe it or not if you went really simple doing all that wouldn't take more than a week and a half.  More complicated languages, well it might take a while longer I believe.  If you don't know Lex or Yacc then add a week getting use to the tools :)

Cyb

sfx1999

  • *
  • Posts: 1142
    • View Profile
Creating a scripting engine.
« Reply #2 on: 2004-02-27 16:43:24 »
Why not take a peak at Mozilla's source code and see how they do it? Don't copy it, though, because there are probably restrictions. Just look at how they implement JavaScript.

DeadLajik

  • *
  • Posts: 53
    • View Profile
Creating a scripting engine.
« Reply #3 on: 2004-02-27 22:07:14 »
Well if you don't want to create your own language you might have a look at 2 languages that are commonly used in scripting. One is Tcl, and the other is Lua. Both have libraries that can be easily linked to C/C++ code and I believe they are pretty small scripting libraries..

One of the problems with Tcl is that it is not compiled so your scripting source is in plain ASCII text. This may be a problem with Lua too.. but i don't know since I havn't tried it.

Anyways full source code to the lua engine is at http://www.lua.org and also lots of documentation.

For examples of using Lua, you could check out some of the rogue based games (Angband, Nethack, maybe others).

Cyberman

  • *
  • Posts: 1572
    • View Profile
Creating a scripting engine.
« Reply #4 on: 2004-02-28 18:19:08 »
Now that I think about it. GCC can compile Java source.  :)

(as well as C and C++)

I strongly suggest thinking about what you want to do before just implementing something. It's a good way to prevent frustation ;)

Cyb

PumpkinPieman

  • *
  • Posts: 256
    • View Profile
    • http://PumpkinPieman.blogspot.com
Creating a scripting engine.
« Reply #5 on: 2004-02-28 18:30:15 »
No kidding, I'm still quite confused what I'm doing now. :isee: