1. Challenge: parse.js -> parse.exw
- Posted by petelomax Oct 24, 2020
- 1325 views
I've been studying Douglas Crockford's Top Down Operator Precedence page: http://crockford.com/javascript/tdop/index.html
Personally, I have always favoured precedence climbing (eg http://www.engr.mun.ca/theo/Misc/exp_parsing.htm ) however the one thing (that I covet) in DC's solution is the direct embodiment of scope, for example if you paste his parse.js into his page but change the last call to statements() into staRtments(), you get a pretty decent/accurate syntax error.
Challenge: download parse.js (and tokens.js and index.html which you'll then be able to run locally), and translate to parse.exw, which loads parse.js and produces some semblance of the same output, with similar error handling.
(You get that output immediately on clicking the first link above, via the semi-dirty trick of go("let make_parse = "+ make_parse.toString() + ";");)
(Yes, in time, I'll probably want it to cope with [the new] parse.exw, but let's not go there just yet.)
It's only 534 lines, and tokens.js being another 138 lines with a rather intense but well commented regex.
However, I think it's far from easy.
Some tips (feel free to ignore any that don't help):
You can change the opening
let make_parse = function () {
to
function make_parse() {
and it still works, thanks mainly to the "dirty trick" mentioned above beginning with "let", however the parser as is won't cope with any such equivalent changes any further down (because it only does "let" forms of function defs).
I've replaced quite a few "this" with "scope" and it still works, but not all of them and certainly not the one in "itself".
(And here was me thinking I had a fairly reasonable grasp on "this", until I ran into "this" particular bit of source code...)
I've replaced several eg
infix("?", 20, function (left) {...
with
function qu(left) {... infix("?", 20, qu
which is obviously much closer to the routine_id("qu") that you'll [probably] need in parse.exw.
I am generally expecting a "token.id" ==> "enum ID, token[ID]" approach, but whatever works for you...
Pretty sure we can replace the
"Object.prototype.error = function (message, t) {" in index.html with just
"global procedure error(string msg)"
and remove all "xxx." in any "xxx.error(", but the parser itself apparently won't cope if we try to do that in the js source itself.
Be warned that lots of things like "symbol_table[id] = s;" are associative arrays; id is not an integer idx.
(I can certainly live with potentially far slower find()s on new complementary arrays for now, no problem.)
2. Re: Challenge: parse.js -> parse.exw
- Posted by petelomax Nov 07, 2020
- 1061 views
Just to let y'all know: the alternative precedence climbing algorithm is working fine anyway.
I finally realised, just yesterday, that the right way to "get rid of this" is simply to pass a "this" as the first argument...