Discussion:
nyacc support for extension languages
Matt Wette
2018-08-12 19:48:49 UTC
Permalink
I am working on providing a module to add to the nyacc distribution for
the purpose of generating extension languages, and putting together a few
extensions for demo. So far I'm playing with javascript and matlab.
These languages use a "nx-" prefix to designate them as "nyacc extension".
One can interpret "nx" to also mean "not exactly", so "nx-matlab" is not
exactly MATLAB. It has taken some work to get my yacc like parser to
parse expressions instead of files, so some work has gone into updating
the nyacc parser. nyacc/parse.scm now has parser generators that provide
an #:interactive option and more robust default-reductions for generating
parsers which work well in interactive mode.

nyacc/parse.scm fragment:
(define* (make-lalr-parser/num mach #:key (skip-if-unexp '()) interactive)
(let* ((len-v (assq-ref mach 'len-v))
(rto-v (assq-ref mach 'rto-v))
(pat-v (assq-ref mach 'pat-v))
(xct-v (make-xct (assq-ref mach 'act-v)))
(start (assq-ref (assq-ref mach 'mtab) '$start)))
(lambda* (lexr #:key debug)
(let iter ((state (list 0)) ; state stack
(stack (list '$@)) ; sval stack
(nval #f) ; prev reduce to non-term val
(lval #f)) ; lexical value (from lex'er)
(cond
((and interactive nval (eqv? (car nval) start)) ; done
(cdr nval))
((not (or nval lval))
(if (eqv? $default (caar (vector-ref pat-v (car state))))
(iter state stack (cons $default #f) lval) ; default reduction
(iter state stack nval (lexr)))) ; reload
(else
...


Here is a demo for "nx-matlab":

$ cat simp0.m
function [c,d,e] = simp0(a, b)
x = a + 1;
y = b + 1;
c = x * y;
d = 1;
e = 2;
end

$ guild compile --from=nx-matlab simp0.m
wrote `/home/mwette/.cache/......./simp0.m.go'

$ guile
scheme@(guile-user)> (load "simp0.m")

scheme@(guile-user)> (call-with-values (lambda () (simp0 1 2))
... (lambda (c d e) (simple-format #t "~S\n~S\n~S\n" c d e)))
6
1
2

scheme@(guile-user)> ,L nx-matlab
Happy hacking with nx-matlab! To switch back, type `,L scheme'.

nx-matlab@(guile-user)> [c,d,e] = simp0(1, 2);
nx-matlab@(guile-user)> c
$1 = 6
nx-matlab@(guile-user)> d
$2 = 1
nx-matlab@(guile-user)> e
$3 = 2


I am working in the nxdev branch of git://git.savannah.nongnu.org/nyacc.git.

Matt
Matt Wette
2018-08-19 21:30:49 UTC
Permalink
Post by Matt Wette
I am working on providing a module to add to the nyacc distribution for
the purpose of generating extension languages, and putting together a few
extensions for demo. So far I'm playing with javascript and matlab.
These languages use a "nx-" prefix to designate them as "nyacc extension".
One can interpret "nx" to also mean "not exactly", so "nx-matlab" is not
exactly MATLAB. It has taken some work to get my yacc like parser to
parse expressions instead of files, so some work has gone into updating
the nyacc parser. nyacc/parse.scm now has parser generators that provide
an #:interactive option and more robust default-reductions for generating
parsers which work well in interactive mode.
This is super cool! Is the goal to eventually make it exactly
matlab/exactly javascript? Or will there always be some portability
issues?
I thought about this a bit. I think hope for 100% compatibility is not there.
For example, javascript strings are always 16bit code points, whereas Guile strings
are either 8-bit characters or 32-bit code points.

Also, I'd like to "fix" some problems in javascript. For example

var a = 1;
if (1) {
var a = 2;
}
a => 2
if (1) {
let a = 3;
}
a => 2

I have made "var" inside blocks illegal. And "let" is not standard, but I'm adding it.

I also want to make the underlying data model consistent among languages, so that may
take some "massaging" of the language def's.

Matt
Nala Ginrut
2018-08-20 03:56:58 UTC
Permalink
Super neat!
I think nycc can help to alleviate the pain of writing/tweaking parser, and
let people concentrate on implementing better middle-end then convert to
tree-il.
Post by Matt Wette
Post by Matt Wette
I am working on providing a module to add to the nyacc distribution for
the purpose of generating extension languages, and putting together a
few
Post by Matt Wette
extensions for demo. So far I'm playing with javascript and matlab.
These languages use a "nx-" prefix to designate them as "nyacc
extension".
Post by Matt Wette
One can interpret "nx" to also mean "not exactly", so "nx-matlab" is not
exactly MATLAB. It has taken some work to get my yacc like parser to
parse expressions instead of files, so some work has gone into updating
the nyacc parser. nyacc/parse.scm now has parser generators that
provide
Post by Matt Wette
an #:interactive option and more robust default-reductions for
generating
Post by Matt Wette
parsers which work well in interactive mode.
This is super cool! Is the goal to eventually make it exactly
matlab/exactly javascript? Or will there always be some portability
issues?
I thought about this a bit. I think hope for 100% compatibility is not there.
For example, javascript strings are always 16bit code points, whereas Guile strings
are either 8-bit characters or 32-bit code points.
Also, I'd like to "fix" some problems in javascript. For example
var a = 1;
if (1) {
var a = 2;
}
a => 2
if (1) {
let a = 3;
}
a => 2
I have made "var" inside blocks illegal. And "let" is not standard, but I'm adding it.
I also want to make the underlying data model consistent among languages, so that may
take some "massaging" of the language def's.
Matt
Joshua Branson
2018-08-20 16:00:51 UTC
Permalink
Post by Matt Wette
I thought about this a bit. I think hope for 100% compatibility is not there.
For example, javascript strings are always 16bit code points, whereas Guile strings
are either 8-bit characters or 32-bit code points.
Also, I'd like to "fix" some problems in javascript. For example
var a = 1;
if (1) {
var a = 2;
}
a => 2
if (1) {
let a = 3;
}
a => 2
I have made "var" inside blocks illegal. And "let" is not standard, but I'm adding it.
That's pretty cool to add let inside javascript!
Post by Matt Wette
I also want to make the underlying data model consistent among languages, so that may
take some "massaging" of the language def's.
Matt
Matt Wette
2018-08-20 23:40:52 UTC
Permalink
Also, I'd like to "fix" some problems in javascript. For example
Post by Matt Wette
var a = 1;
if (1) {
var a = 2;
}
a => 2
if (1) {
let a = 3;
}
a => 2
I have made "var" inside blocks illegal. And "let" is not standard, but I'm adding it.
That's pretty cool to add let inside javascript!
FYI, the code above was executed in nodejs.

Matt

Loading...