Domanda

Please let me know if this isn't the right SE site (or otherwise) for this kind of question, it's the best match I could find.

I'm working on a humble interpreter written in C for a simple language I'm designing. I ran into a design problem in the interpreter which I'm sure people have already solved, and I would like to learn about the existing solutions to this.

Consider there's a builtin String type in the language. And in the interpreter each object has a attributes hash table associated with it. It maps a String* key to a Object* value.

Where do I have problems? Each String instance should have methods which operate on it: E.g. .length(), .at_index() etc.

The way I thought to implement this, is have the C function which is responsible to create String objects, also put the methods (Function objects) in the new object's attributes table, under the matching String keys.

This crashes the interpreter (the C program itself) with a Stack Overflow, because it means creating a new String has to create a new String, and so on...

I can probably work around this issue, maybe by specifically caching Strings (which is a good idea anyway), but I get the feeling that maybe this design is problematic in the first place.

Is it? How do existing language implementations, mainly simple ones, accomplish this?

È stato utile?

Soluzione

Your interpreter sits outside the language. It doesn't have to abide by the rules of the language. If it cannot initialize a property of an object because that property requires something else that doesn't exist yet, just don't initialize it, and wait until whatever you need to initialize it exists.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
scroll top