I am expanding my knowledge in C++ and would like to write my own scripting language as a means to challenge myself and also as a project to use for casual scripts etc. I saw some old posts on stackoverflow on this but they're pretty dated and was wondering if anybody could give any newer sources examples etc...?
-
1Really, write your own scripting language? This is not for the faint of heart. Search the internet for "Compiler design theory" and also "computer language theory". You need to write a Lexer, Parser and interpreter. In schools, this can be a year long course. Good luck!Thomas Matthews– Thomas Matthews2016-10-12 23:19:27 +00:00Commented Oct 12, 2016 at 23:19
-
2Voting to close - too broad. Instructions for writing languages cannot fit in the answer section for one question. There are 1000 page books already written on this topic, search for "Compiler Design book Aho", often nicknamed "the dragon book".Thomas Matthews– Thomas Matthews2016-10-12 23:21:03 +00:00Commented Oct 12, 2016 at 23:21
1 Answer
In a nutshell:
Define Your Language
You will need to define all the commands and their syntaxes.
After defining them, you can group them by syntax patterns.
Design the Lexer
The Lexer is the part that scans the input for language elements and converts them to tokens. The tokens are then fed to the Parser.
Design the Parser
The Parser is the part that evaluates the tokens, such as their order and number of parameters.
Design the Interpreter
The Interpreter is the part that executes the output from the Parser.
You should think of it as a very high level model of a computer. For example, will you be needing variables, registers, input, output, math instructions, etc.
A complex interpreter is a BASIC language interpreter (or Visual BASIC). Java has a JVM which interprets byte-codes. There are also LISP interpreters.
You may want to review the Python interpreter source code if you can get it.
Also, review the source code for the GNU and CLanguage compilers.
Compilers and interpreters are usually written by teams of people (to reduce the development time). Be prepared to spend a long time with this project.