Dictionary as a builtin variable type.
Displaying 1-8 of 8 total.
1
Please enter a numerical value for the importance of this sticky.
Enter 0 to unsticky.
Overkill

So, I guess Jesse came up with this: http://ceramicpig.no-ip.org/dictproposal.pdf That's a good start for dictionary functionality, but I think it could be improved a bit down the line to be less ugly.

Dunno if this is too complicated, or OOP-like, but the following could work for a later stage of dictionary control. It could give lots of functionality and give a really good list structure. I'm sure it would probably give headaches trying to get the compiler and interpretter to manage with the following, but it would pay off.


dictionary myDict; // Create new dictionary type.
myDict['dood'] = 'Bill'; // Create new key in dict by setting it.
myDict['ohshi'] = 'Bubbles'; // Create *another* new key in dict by setting it.
s = myDict['dood']; // Return the value of a key if it exists.
myDict.remove('dood'); // Remove a key in the dict.
myDict.clear(); // Clear out all keys from the dict.
exists = myDict.contains('dood'); // True/false return.
myDict['subDict'] = dict(); // Create a dictionary in a dictionary.
myDict['subDict']['bob'] = 'cool'; // Assign the subdictionary a new key.
myDict['anotherDict'] = myDict['subDict']; // Copy the dictionary.
myDict['subDict'] = ''; // Wipe the dictionary from existance and replace key with a new value.
s = myDict['anotherDict']['bob']; // Read from a subdictionary.


dict(); in the above case, would be a function that would return a dictionary type. And assigning it to a dictionary would let you have subdictionaries. The dict() function's return would only assignable to dictionary types, not ints or strings. It would surely be pain, having to add new rules to the compiler and a whole new variable type to screw around with. But I dunno, weigh the risks to better-verge-c-code-as-a-result benefits? Comments?

Posted on 2005-10-22 23:49:19 (last edited on 2005-10-22 23:55:46)

Jesse

First, that document was written by Kildorf. I'm just implementing it.

I agree that better syntax is needed; it was our intention to start with library functions, then add syntactic sugar later. I don't think that going full-blown object-oriented is a good idea when no other part of verge is in that style.

I'm also not sure if it's realistic to implement the flexibility you describe. Once there's some consensus on what everyone wants it to look like, I'll think about it some more. At the moment, I think that code would compile down to something like the following, which should be possible to figure out at compile time. (Some functions used below don't exist, but their meaning should be pretty clear.)

dict myDict = DictNew();
DictSetString(myDict, 'dood', 'Bill');
DictSetString(myDict, 'ohshi', 'Bubbles');
s = DictGetString(myDict, 'dood');
DictRemove(myDict, 'dood');
DictClear(myDict);
exists = DictContains(myDict, 'dood');

DictMakeSub(myDict, 'subDict');
DictSetString(myDict, 'subDict!bob', 'cool');
DictCopy(myDict, 'anotherDict', myDict, 'subDict');
DictSetString(myDict, 'subDict', '');
s = DictGetString(myDict, 'anotherDict!bob');

// at end of block
DictFree(myDict);


Where the '!' is some character like 0xFF that no one will ever use in a string.

Posted on 2005-10-23 10:28:08

Overkill

Ah, I figured it *could* have been Kildorf, after all, the pdf is on his server, but eh. Hmm, yeah, OOP is probably a bad idea. Hmm. Would something like the following work instead? It's a little more syntactically pleasing, and refrains from OOP.


dict myDict; // Dictionary exists until it escapes its scope. Initialized to a value of DictNew() by default.
myDict['dood'] = 'Bill';
myDict['ohshi'] = 'Bubbles';
s = myDict['dood'];
DictRemove('dood');
DictClear(myDict);
exists = DictContains(myDict, 'dood');
myDict['subDict'] = DictNew();
myDict['subDict']['bob'] = 'cool';
myDict['subDict']['subSubDict'] = DictNew();
myDict['subDict']['yetAnotherDict'] = DictCopy(myDict['subDict']['subSubDict']);
myDict['anotherDict'] = DictCopy(myDict['subDict']);
myDict['subDict'] = '';
s = myDict['anotherDict']['bob'];
Log(DictToString(myDict)); // A string-able representation of a dictionary.
// Scope is escaped (unless myDict is global), free the chunks of memory used by the dict and its subdictionaries.


DictNew() would probably return an int, but VergeC would be able to detect subdictionaries, by the way the player tries to access them. myDict['subDict']['bob'] would first check that myDict exists and is a dictionary, then myDict['subDict'] is a valid associative array key, and that the entry was a dictionary, and then it would check if 'bob' was a valid key with in the subdictionary, and if any of the conditions fail, it raises either a wrong type error, array-out-of-bounds-error. Maybe to distinguish dicts from ints could instead be a string of chr(255) + str(index), because ints can't start with a non-numeric, and following it would be a useful index with a character nobody will probably ever use. So, all the dictionary functions, you'd end up passing integer string indices to, but with a special character at the beginning.

Posted on 2005-10-23 11:24:20 (last edited on 2005-10-23 11:35:13)

Jesse

I think the example code is better, but I would get rid of the calls to DictCopy. Assigning a dict should always copy, or else we need to get into garbage collection (or the user has to explicitly free everything.)

I'd also like to disagree with my previous post in terms of the sub-dicts. They should be full-fledged dictionaries, accessible with DictGetDict or something, rather than messing around with all kinds of weird strings.

In that same vein, I'd like to propose that if you DictSetInt() a value, you can't then DictGetString() it - that is, there is actually typing in the dict. Otherwise, we get into (basically unsolvable) garbage collection issues or crashes (because you can DictGetInt() a dict handle and then try to use it after it's been freed automatically.)

So, with what I've just described, dicts don't act like references (like in Java), but like objects (like in C++ when not using pointers.) So, they have the same semantics as strings and ints, basically - pass by value, copy on assignment. We could use copy-on-write for efficiency. How do people feel about this?

Posted on 2005-10-23 12:08:28 (last edited on 2005-10-23 12:22:45)

Jesse

I should mention that the garbage collection route isn't impossible - we could have reference semantics for the dicts if people want. I'd just like some consensus on the issue.

Posted on 2005-10-23 12:21:03

Overkill

Hey Jesse, how's the dictionary stuff going? Last I heard you were adding proper variable type handling, but that was quite a while ago.

Posted on 2005-11-10 14:27:35

Jesse

There's a bug or two still lurking in the typing code, and I've been too busy to look at it recently. I expect that to continue to be the case for at least a couple weeks.

Posted on 2005-11-11 09:26:02

Jesse

I've been thinking about this some more, and I don't think I will be finishing this work. The changes made to the compiler and core make them both more difficult to read and change, and probably slower, too. I don't think this is a good tradeoff.

If we want this kind of ability, I think a more serious reorganization (even re-writing) of the compiler/interpreter is needed. I am not in a position to start such a project right now.

Posted on 2005-11-26 08:30:02


Displaying 1-8 of 8 total.
1
 
Newest messages

Ben McGraw's lovingly crafted this website from scratch for years.
It's a lot prettier this go around because of Jon Wofford.
Verge-rpg.com is a member of the lunarnet irc network, and would like to take this opportunity to remind you that regardless how babies taste, it is wrong to eat them.