Matt Wette
2018-08-12 19:48:49 UTC
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
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