On Friday, 10 June 2016 at 14:25:37 UTC, Adam D. Ruppe wrote:
On Friday, 10 June 2016 at 13:55:28 UTC, Chris wrote:
I have neither time nor the required expertise to write a
scripting language from scratch ;) You on the other hand ...
:-)
Oh, it isn't that hard, at least to do a quick basic thing. You
might want to start with the various math parsers. A postfix
one is relatively easy:
2 3 +
break it up into tokens, read them in, build a syntax tree
(well, for the postfix thing, it is probably a stack!).
That approach will even work for a Lisp-like language!
Then try an infix one. You'd use the same tokenizer, but the
parser is different... and this kind of parser gets you started
for a typical script language.
2 + 3
The way this works is you read the token, peek ahead, create an
object and build a tree. You'd use different functions for
different contexts. So it might start with readExpression which
readFactor. Then readFactor might call readAddend...
If you look at the D grammar:
http://dlang.org/spec/grammar.html you'll find the various
terms are defined as WhateverExpressions and often
recursively...
you can write the parser to follow that basically the same way!
You end up with one of these:
https://en.wikipedia.org/wiki/Recursive_descent_parser
Once you get addition and multiplication working with correct
order of operations, you just kinda start adding stuff! Make a
function call and an if/loop statement and boom, you have a
simple programming language.
After that, it is basically just adding more token recognition
and AST classes.
To make an interpreter, you can just add a method to the AST
objects that interprets and gives a result.... boom, it works!
Compiling is basically the same idea, just spitting out
something other than the result of the expression - spitting
out code that gives you the result. That gets harder to get
into all the fancy techniques, but it builds on the same
foundation.
It is a good thing to know how to do, at least the basic parts!
I agree. It's good to know how to do it. But don't get me
started, else I'll have a new obsession ... ;)
But seriously, would you like to work on something like DScript.
Your scripting language already fulfills things that were on my
wishlist (easy D interop).