Live data from Hacker News

How to implement a programming language in JavaScript

lisperator.net

1–10 of 39 posts

Re: How to implement a programming language in JavaScript

#4
post #3

Just wondering if it is true that most of the modern language parsers are written in C?

Probably true, but only because the implementation language for them is also C. Some of them also frequently use a mix of the implementation language and grammar specification DSL like yacc.

Re: How to implement a programming language in JavaScript

#5
Is there such a thing as a language that compiles to a value rather than an executable program?

I've done a really hacky version of something this where a user would input a succinct, high-level description of a sequence of colored lighting, and my program would parse* that and build the tedious, low-level JSON representation of that sequence for yet another program to perform. Is there a fancy or unfancy name for that?

*Ok really it was a little bit of munging and then eval.

Re: How to implement a programming language in JavaScript

#7
post #5

Is there such a thing as a language that compiles to a value rather than an executable program? I've done a really hacky version of something this where a user would input a succinct, high-level description of a sequence of colored lighting, and my program would parse* that and build the tedious, low-level JSON representation of that sequence for yet another program to perform. Is there a fancy or unfancy name for th…

Well, at some level every language compiles to a value rather than an executable program - the parse tree of a source module is a data structure describing the program, and the executable is a sequence of bytes that is interpreted by the processor.

There's a continuum between this and what we think of as a "value" in programming terms (eg. an int or a string), but the boundary is fuzzy. It was pretty common at Google to write DSLs that compiled to a protobuf that'd be loaded into the server as configuration, for example. CUDA compiles programs into a buffer that is then sent to the GPU. HTML compiles into the DOM, which is a value that can be manipulated with Javascript.

Re: How to implement a programming language in JavaScript

#8
post #3

Just wondering if it is true that most of the modern language parsers are written in C?

Probably true, but only because the implementation language for them is also C . Some of them also frequently use a mix of the implementation language and grammar specification DSL like yacc.

Use of DSLs and tools like flex and bison makes the implementation a whole lot more reliable (but only if they're used, and some commonly run languages have a very implementation-specific grammar).

consider:

    input:    /* empty string */
            | input line
    ;
    
    line:     '\n'
            | exp '\n'  { printf ("\t%.10g\n", $1); }
    ;
    
    exp:      NUM                { $$ = $1;         }
            | exp '+' exp        { $$ = $1 + $3;    }
            | exp '-' exp        { $$ = $1 - $3;    }
            | exp '*' exp        { $$ = $1 * $3;    }
            | exp '/' exp        { $$ = $1 / $3;    }
            | '-' exp  %prec NEG { $$ = -$2;        }
            | exp '^' exp        { $$ = pow ($1, $3); }
            | '(' exp ')'        { $$ = $2;         }
    ;
http://dinosaur.compilertools.net/bison/bison_5.html#SEC27
Post reply on HN