Structure of how this language should work

Started by
15 comments, last by Zotoaster 16 years, 10 months ago
I've tried endlessly for the past few days to figure out how all this would work, but it's just not knocking in. The problem is basically working with classes and functions etc. If I have a very simple program:

class myclass
{

   void mymethod()
   {
      char boo[4] = "hello";
      print boo;
   }

}

void main()
{
   myclass myobj;
   myobj.mymethod();
}
The problem is, I have got to close to getting this to work, except for a few things: - Getting everything only in functions to work. I was thinking of perhaps making "frames" or something for this. - Maing an object ¬.¬ I figure it has to be in memory before you can make an object, let alone call any vars of functions. Problem is, the entry point is "main", you have just skipped out all the class code, so how do you make an object then. If the entry point isn't "main", how do you stop everything in the class (and it's functions) being executed? Thanks for your help :)
Advertisement
C++ implementations face a similar problem. The OS doesn't call main() directly, rather, it (usually) calls start() which does environment setup code (such as constructors), finishing with a call to main().

If I'm understanding you right, you want to do the same thing. At compile-time, generate a list of classes and store them with the script somehow. Then, just before starting your script with a call to main(), the execution engine can initialize all that class data.
That's exactly what I want. Can you give any more detail? :)
The start functon (which is the actual entry point your program) sets up your environment for you. So it will do things like initialise various things the standard library will need and call the constructors for any global classes (at least I think it's done there, it's certainly done before main).

However in your sample code the only instance you have of myclass is on the stack and is local to main. Hence the constructor wouldn't be called before main. What would happen is at the beginning of main space would be allocated on the stack for myclass, and then the myclass constructor would be called for that newly allocated instance.
I'm finding it pretty hard to understand.. Why does it call constructors? And how exactly does it set up the environment?

Sorry for being such a bugger :/
Global classes are initialized via constructors, too. The start function does all of that before actually calling main. The environment is set up by allocating stack space, collecting command-line arguments/environment variables, etc. Because you're designing a scripting language, you won't need to actually have a start function, just have the engine set it up before executing the script.
Hmm, but how do you allocate stack space when ugh, the stack is totally dynamic?I think this might be getting too confusing :]
The stack is a fundamental concept and understanding it will help you - it will also help you see why you probably don't need one for as many things as C++ does. Stacks are dynamic in nature, and they manage their own 'space'; that's what they're for.

If you haven't got functions working yet, or variables, forget classes. Work on one problem at a time.

What do you mean by "Getting everything only in functions to work"? Functions compile down to code, like your main function does. Calling a function is typically a case of:
- remembering the current position in the program by pushing it onto a call stack
- moving the current position to the start of the function you're calling
- creating storage for any local variables within this function
- initialising any function arguments as local variables

Then you execute the function, and at the end you pop the return value off the call stack to see where to send execution to next.

As for creating objects - whether classes, or any other type - you just allocate space locally of the appropriate size, initialise that space accordingly, and then have your instructions refer to the relevant parts of that space. In machine compiled code this is typically done by allocating space on the stack, and having all variable accesses resolve to accessing stack memory. In bytecode compilation or any other high level language though, you could have some sort of name -> object lookup, where an 'object' is a polymorphic/generic variable type.

The entry point isn't all that relevant. Either you compile all code before execution, so that it's always there when you need it, or you execute the code as you go along, so that you'd just have to search for the definition before creating the object. Or you have some hybrid scheme in the middle. Having an arbitrary entry point doesn't mean ignoring everything before it in the file.

Creating a class is much like creating any other object, except initialising the space is typically a case of calling some sort of constructor.
Well what I meant about the functions, was that I only want code within functions to work, and not anything outside functions unless it's creating classes or global objects/variables etc.

I was also sorta worrying about making classes mainly because I thought if I didn't get them done now, I would encounter problems in the future with how the rest of my code works.

I'll keep reading :)

[edit]

Oh also, as for the local variables bit, I have never understood them (obviously I know what they are etc), I understand turning parameters into variables, but having an array for local variables seems kinda of pointless, seeing as the braces {} will clear all the variables by the time the function has finished anyway.
Quote:Original post by Zotoaster
Hmm, but how do you allocate stack space when ugh, the stack is totally dynamic?I think this might be getting too confusing :]


That's what needs to be done for compiled programs, since you have total control of the runtime environment of your scripts, you can do whatever setup is necessary.

Quote:Original post by Zotoaster
Well what I meant about the functions, was that I only want code within functions to work, and not anything outside functions unless it's creating classes or global objects/variables etc.

That's an easy one. When parsing outside of a function, refuse to parse any executable statements, just declarations and whatnot.

This topic is closed to new replies.

Advertisement