🎉 Celebrating 25 Years of GameDev.net! 🎉

Not many can claim 25 years on the Internet! Join us in celebrating this milestone. Learn more about our history, and thank you for being a part of our community!

Derived Classes and Compilation (Own Scripting Language)

Started by
1 comment, last by Phillip Schuster 24 years, 7 months ago
Sorry, I can't answer your question, but I'm planning on writing a scriptlanguage of my,
and I wonder if you know any good books or webpages with some nice info?
Advertisement
Hi all !!

The last days I created my own scripting language from scratch (I did not use Lex or Yacc, I created the parser from scratch myself, that was easier than playing around with these two very complicated tools). Everything was going very fast. I started last Saturday and today I can say I have a fully functional language complete with Bytecode-Compiler, Stack and Virtual Machine, the language is very similar to C++ and features nearly all C++ Datatypes. It is missing references (or pointers) and user defined types such as structs, but this will also be included the next days.

However I am not quite sure how to implement class derivation. My scripts look like that:

code:
class Character;import class Vector3;import class Weapon;//Here are global variablesvar Vector3 position;var Vector3 rotation;var Weapon  pistol;function bool SeesPlayer () {   if (Engine::CheckPlayerInViewSpace(this) == true) {     return true;   }   return false;    }function FireWeapon() {   pistol.Fire();}//This function is called when the script is loadedfunction Init() {   position.Set(0,0,0);   rotation.Set(0,0,0);   pistol.InitAmmo();}//This is the main function which is called every Framefunction DoTick() {   if (SeesPlayer() == true) {      FireWeapon();   }}

Ok, so, this is what my scripts look like. My question is:

If I create another class derived from Character this class inherits all variables and functions form it's parent. So that's recursive, cause this parent also inherits all variables and functions of its parent and so on....
Now, should I combine all Parent-Classes together during compile time or should I first do that at load time. So, if my new class would like like that:

code:
class Monter1 extends Charakter;function DoTick() {   //Do something other than class character does that.}

In this case my function DoTick overrides the function of character. Well, function overriding is easy. Either during compile-time or loadtime I just have to check if the parent has a function with the same name as it's child, if so then do not load parent's function, that's easy both during compile and loadtime.

What do you think, which one would be better ??


Phillip

Phillip Schuster
My suggestion:

do it like they do in c++
make the functions that you want to override in derrived classes virtual and store the pointer to them in a vtable. If a child class then overrides such a virtual function, it just writes the pointer to the new function into the vtable.
anyway, I'm not very experienced yet with scripting languages and stuff, so there might be better solutions.

code:
parent class{  //virtual function virtFn(params)  vtable [    ptr to virtFn(params)    //contains pointer to virtFn  ]}child class{  //virtual function virtFn(params) //overriden fn  vtable [    ptr to virtFn(params)    //contains pointer to child virtFn instead of parent class virtFn  ]}

HTH

[This message has been edited by Chappa (edited November 22, 1999).]

This topic is closed to new replies.

Advertisement