Skip to main content
gamedev.net
🔒 Locked 🤖 Godot

Interpreter help (c++ scripting)

Started by jeremic 2 replies 900+ views
Original Post
jeremic
jeremic
Hello, I'm trying to create a simple interpreter for my VERY simple scripting language. Heres a script that I want to run display 5 + $myvar This should just simply print 14 on my console. $myvar is an internal static variable which holds the value 9. How would I parse this line so that my output is predictable. When I grab this line from the user its stored in a char array. How do I convert an instruction (such as addition) from a char array to an actual instruction? And how would I parse $myvar to be equal to 9? I was thinking of using strtok but maybe theres an easier way. I'm open to suggestions, guidance, comments, and pointers. Thank you!!
Telastyn
Telastyn
1. If you need to ask how to parse something, you might need to take a few steps back; it's important to know that before designing your syntax.

2. In general, you want to develop a parser for your syntax. strtok is sucky even for its original design. There are parser builders like yacc or antlr that are designed to develop parsers. boost::spirit is another alternative.

3. To get that to parse... hmm 'as you expect' is probably the best description of it, you'll need to determine an order of operations that places + ahead of display (or simply method invocation). Something to consider before making the parser.

4. To make an instruction into a executable token, you'll end up with some mechanism to convert the syntax tree you get from the parser into something usable. It might be replacement, it might be doing a lookup in a map and running a stored functor, it might be a giant if/else tree. Again, if you don't have a naive guess about how to do it, this is perhaps beyond you at the moment. Most of the parser builders include an example calculator which should serve as a good starting point.

5. This sort of thing is a well studied topic, and exists online in a lot of places in varying degrees of usefulness. General keywords: lexer/lexing, parser/parsing, abstract syntax tree, how to build a compiler, how to build an interpreter ...
jeremic
jeremic
Very useful info, thank you!
AshleysBrain
AshleysBrain
Tokenising the input before parsing is usually a good idea; you don't want the parser to fiddle with individual chars. Tokenising is just removing whitespace and converting to a sequence of basic symbols like 'display', '5', '+', '$myvar'.

Also, your code will end up a lot more complex if you want to support complex expressions like 100 / (5 + $myvar * 2). You'll need something to sort out the order-of-operations and a tree of classes to evaluate each operator. If you want to keep it simple, just stick to 'x add y' and 'x multiply by y' basic commands.
Construct (Free open-source game creator)

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.