Writing a Javascript parser

Hi all,

Yesterday I started work on a Javascript parser…source can be found -here-. And -here- is what it looks like in the browser.

First off, if there are any display or browser issues, please let me know. I’ve tested it in Safari and Chrome and it works fine on an iMac and a MacBook, as well as Safari on the iPhone. Windows/Android users, please check to see if it works on your system.

Secondly, I’m looking for advice in which direction to take this, before the program gets much larger. As it is, it has very little functionality and you can’t really do anything, the only command I programmed in is “look”. It will recognize the “go north”, “n”, “go n” commands, although it doesn’t do anything with them yet, since I am still trying to figure out how to build the world. Preferably, I am not going to hard-code in the actual worlds to explore, but have it read from another file. I don’t care about reading .z5 files or other types of story files. My goal with this is just to create my own standalone engine with good language processing.

So I probably want to build a “dictionary” file that handles all the language parsing, a different file that interprets world/story data, and a third file (maybe XML?) that contains the world/story data. Let me know any suggestions or ideas you guys might have. And please, don’t direct me to an already completed file to see how it works. I’m trying to build this from scratch.

Thanks all!

In moments like this is when I think it’s a pitty projects like fi.js (baltasarq.github.io/fi-js/) don’t have english documentation. I’m pretty sure you could use the IF engine to handle all parsing and database work, and just concentrate in replicating the game itself.

Good luck with your work anyway, I hope you manage to do it and in the meantime create a code that can be used for other games :slight_smile:

Edit: feel free to get any code you may need from my javascript based engine ngPAWS. Though the games are not programmed in javascript, the runtime is javascript so there are functions for parsing, standarize, handle objects, puzzles, etc. Probably you can find some useful stuff from line 975 and further in this file:

github.com/Utodev/ngPAWS/blob/m … runtime.js

Thank you very much for your reply! It looks like you are much farther along in a similar project. I am mainly trying to accomplish this as a learning exercise for myself in creating web-based applications. I want to try to avoid re-using other people’s code as much as possible, but your project will be a great resource if I get stuck.

That’s good, and don’t forget the fun you get in the way :slight_smile:

And yes, if you get stuck you can allways have a look and how some circumstance was solved before, and adopt it, or not :slight_smile:

So I just recently learned that switch statements are not gonna be the way to go for parsing. I probably want to use a different type of data structure altogether.

It might be interesting for you to check out the course code of the Inform parser?

It was after learning how the JSON.parse function worked that I realized that “parsing” in general is going to be too complex for the style I was going for.

On your suggestion Victor, I cracked open the Inform source code. :open_mouth:

There’s an insane amount of stuff going on…and that’s for a game engine that has absolutely no actual game data built in to it. Theres thousands and thousands of lines of code, spread across 20+ files. I’m going to need to build my engine slowly, case by case (not going to allow myself to copy any of their code). I may need to start over completely. It’s definitely a good idea to separate out the parts of the program. The user interface is entirely HTML/CSS/JQuery, and that functionality needs to be built separately from the core parsing engine, which needs to be separate from the game data (rooms, objects, actors, etc.).

It’s good that I’m figuring this all out now, and not later on. I’m in an intensive 12-week JavaScript course right now (coding bootcamp, heh). I’m learning a lot, but don’t have a whole lot of time to work on this Zork project. I will come back to it in a few months hopefully. I guess I don’t really need to start over, but I will want to break the game.js file down into many more files.

Thanks for your replies.

Quite a lot of game data, if you count the default world model and verbs.

The Inform 6 parser makes for a simpler read – only eight files, for a start. (parserm.h and verblibm.h contain the bulk of what you care about.)

However, you might do even better reading inform7.com/extensions/Ron%20New … ource.html .

That’s super interesting. Thanks for the link. Can you tell me what kind of code that is? Totally unlike anything I’ve ever done.

It’s Inform 7, the successor to I6 which tries to look as much like natural English as possible.

There’s also the ZILF parser (link). Inform’s parser is more of a general pattern matching engine, where each verb and object can define its own patterns. ZILF uses heuristics to match commands to a standard ideal sentence, relying on the compiler’s knowledge of which parts of speech each word can be used as: find a verb, find up to two noun phrases consisting of adjectives and nouns connected with and/but, look for a preposition before each one.