From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.org!not-for-mail From: Matt Wette Newsgroups: gmane.lisp.guile.user Subject: Re: Q on (language spec) Date: Sun, 18 Oct 2015 13:00:45 -0700 Message-ID: <02BEFF46-4DA9-4222-989C-6AAA4FABDCE0@verizon.net> References: NNTP-Posting-Host: plane.gmane.org Mime-Version: 1.0 (Mac OS X Mail 8.2 \(2104\)) Content-Type: multipart/alternative; boundary="Apple-Mail=_9DD30009-A1A1-46CE-9AA8-04F7355AF09D" X-Trace: ger.gmane.org 1445198492 19729 80.91.229.3 (18 Oct 2015 20:01:32 GMT) X-Complaints-To: usenet@ger.gmane.org NNTP-Posting-Date: Sun, 18 Oct 2015 20:01:32 +0000 (UTC) To: guile-user@gnu.org Original-X-From: guile-user-bounces+guile-user=m.gmane.org@gnu.org Sun Oct 18 22:01:22 2015 Return-path: Envelope-to: guile-user@m.gmane.org Original-Received: from lists.gnu.org ([208.118.235.17]) by plane.gmane.org with esmtp (Exim 4.69) (envelope-from ) id 1Znu8v-0005up-Fo for guile-user@m.gmane.org; Sun, 18 Oct 2015 22:01:21 +0200 Original-Received: from localhost ([::1]:35405 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1Znu8u-0005fl-K1 for guile-user@m.gmane.org; Sun, 18 Oct 2015 16:01:20 -0400 Original-Received: from eggs.gnu.org ([2001:4830:134:3::10]:57098) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1Znu8k-0005fb-SK for guile-user@gnu.org; Sun, 18 Oct 2015 16:01:12 -0400 Original-Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1Znu8h-00051Q-G1 for guile-user@gnu.org; Sun, 18 Oct 2015 16:01:10 -0400 Original-Received: from vms173017pub.verizon.net ([206.46.173.17]:40373) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1Znu8h-00050M-7g for guile-user@gnu.org; Sun, 18 Oct 2015 16:01:07 -0400 Original-Received: from [192.168.2.127] ([72.87.204.128]) by vms173017.mailsrvcs.net (Oracle Communications Messaging Server 7.0.5.32.0 64bit (built Jul 16 2014)) with ESMTPA id <0NWF00CNFM99PB20@vms173017.mailsrvcs.net> for guile-user@gnu.org; Sun, 18 Oct 2015 15:00:47 -0500 (CDT) X-CMAE-Score: 0 X-CMAE-Analysis: v=2.1 cv=MtGvkDue c=1 sm=1 tr=0 a=Jf1g6iwM2K3MHzQE8uPn/Q==:117 a=o1OHuDzbAAAA:8 a=oR5dmqMzAAAA:8 a=5lJygRwiOn0A:10 a=69wJf7TsAAAA:8 a=3ikPBziGXGRR-exnRNcA:9 a=5xAh_jdlXBOIff69:21 a=iphtj4lG_eMonW3b:21 a=CjuIK1q_8ugA:10 a=m3aZ4SQqfikW753ZaRMA:9 a=gsSufubAVvCwDRjW:21 a=emyZ9gwQO2sz_rOm:21 a=DG3x3Y5GeSXijviW:21 a=_W_S_7VecoQA:10 In-reply-to: X-Mailer: Apple Mail (2.2104) X-detected-operating-system: by eggs.gnu.org: Genre and OS details not recognized. X-Received-From: 206.46.173.17 X-BeenThere: guile-user@gnu.org X-Mailman-Version: 2.1.14 Precedence: list List-Id: General Guile related discussions List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: guile-user-bounces+guile-user=m.gmane.org@gnu.org Original-Sender: guile-user-bounces+guile-user=m.gmane.org@gnu.org Xref: news.gmane.org gmane.lisp.guile.user:12096 Archived-At: --Apple-Mail=_9DD30009-A1A1-46CE-9AA8-04F7355AF09D Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset=us-ascii > On Oct 17, 2015, at 12:02 PM, Matt Wette = wrote: > I am playing with the compiler tower and have been digging through the = (system base language) module to try to get my hands around writing to = the compiler tower. . Here is a simple calculator example that I have working with my own = intermediate (SXML based) language.=20 scheme@(guile-user)> ,L calc Happy hacking with calc! To switch back, type `,L scheme'. calc@(guile-user)> a =3D (2.5 + 4.5)/(9.3 - 1) calc@(guile-user)> ,L scheme Happy hacking with Scheme! To switch back, type `,L calc'. scheme@(guile-user)> a $1 =3D 0.8433734939759036 The implementation consists of the files spec.scm, parser.scm and = compiler.scm which are listed below. All files are: ;;; Copyright (C) 2015 Matthew R. Wette ;;; ;;; This program is free software: you can redistribute it and/or modify ;;; it under the terms of the GNU General Public License as published by=20= ;;; the Free Software Foundation, either version 3 of the License, or=20 ;;; (at your option) any later version. and to appear at https://savannah.nongnu.org/projects/nyacc = . spec.scm: (define-module (language calc spec) #:export (calc) #:use-module (system base language) #:use-module (nyacc lang calc parser) #:use-module (nyacc lang calc compiler)) (define (calc-reader port env) (let ((iport (current-input-port))) (dynamic-wind (lambda () (set-current-input-port port)) (lambda () (calc-parse #:debug #f)) (lambda () (set-current-input-port iport))))) (define-language calc #:title "calc" #:reader calc-reader #:compilers `((tree-il . ,calc-sxml->tree-il)) #:printer write) parser.scm: (define-module (nyacc lang calc parser) #:export (calc-parse calc-spec calc-mach) #:use-module (nyacc lalr) #:use-module (nyacc lex) #:use-module (nyacc parse) ) (define calc-spec (lalr-spec (prec< (left "+" "-") (left "*" "/")) (start stmt-list-proxy) (grammar (stmt-list-proxy (stmt-list "\n" ($$ (cons 'stmt-list (reverse $1))))) (stmt-list (stmt ($$ (list $1))) (stmt-list ";" stmt ($$ (cons $3 $1)))) (stmt (ident "=3D" expr ($$ `(assn-stmt ,$1 ,$3))) (expr ($$ `(expr-stmt ,$1))) ( ($$ '(empty-stmt)))) (expr (expr "+" expr ($$ `(add ,$1 ,$3))) (expr "-" expr ($$ `(sub ,$1 ,$3))) (expr "*" expr ($$ `(mul ,$1 ,$3))) (expr "/" expr ($$ `(div ,$1 ,$3))) ('$fixed ($$ `(fixed ,$1))) ('$float ($$ `(float ,$1))) ("(" expr ")" ($$ $2))) (ident ('$ident ($$ `(ident ,$1)))) ))) (define calc-mach (compact-machine (hashify-machine (make-lalr-machine calc-spec)))) (define calc-parse (let ((gen-lexer (make-lexer-generator (assq-ref calc-mach 'mtab) #:space-chars " \t")) (parser (make-lalr-ia-parser calc-mach))) (lambda* (#:key (debug #f)) (parser (gen-lexer) #:debug debug)))) compiler.scm: (define-module (nyacc lang calc compiler) #:export (calc-sxml->tree-il) #:use-module (sxml match) #:use-module (sxml fold) ;;#:use-module (system base language) #:use-module (language tree-il)) (define (fup tree) (sxml-match tree ((fixed ,fx) `(const ,(string->number fx))) ((float ,fl) `(const ,(string->number fl))) ((ident ,id) `(toplevel ,(string->symbol id))) ((add ,lt ,rt) `(apply (toplevel +) ,lt ,rt)) ((sub ,lt ,rt) `(apply (toplevel -) ,lt ,rt)) ((mul ,lt ,rt) `(apply (toplevel *) ,lt ,rt)) ((div ,lt ,rt) `(apply (toplevel /) ,lt ,rt)) ((assn-stmt (toplevel ,lhs) ,rhs) `(define ,lhs ,rhs)) ((empty-stmt) '(begin)) ((stmt-list ,items ...) `(begin ,items ...)) (,otherwise tree))) (define (calc-sxml->tree-il exp env opts) (let* ((tree (foldt fup identity exp)) (code (parse-tree-il tree))) (values code env env))) --Apple-Mail=_9DD30009-A1A1-46CE-9AA8-04F7355AF09D Content-Transfer-Encoding: quoted-printable Content-Type: text/html; charset=us-ascii
On Oct 17, 2015, at 12:02 PM, Matt Wette <matthew.wette@verizon.net> wrote:
I am playing with the compiler tower and have been digging = through the (system base language) module to try to get my hands around = writing to the compiler tower.  .

Here is a simple calculator example that I have = working with my own intermediate (SXML based) = language. 

scheme@(guile-user)> ,L calc

Happy hacking with calc!  To switch back, type `,L = scheme'.

calc@(guile-user)> a =3D (2.5 + 4.5)/(9.3 - 1)

calc@(guile-user)> ,L scheme

Happy hacking with Scheme!  To switch back, type `,L = calc'.

scheme@(guile-user)> a

$1 =3D 0.8433734939759036


The implementation consists of the = files spec.scm, parser.scm and compiler.scm which are listed = below.

All = files are:

;;;= Copyright (C) 2015 Matthew R. Wette

;;;

;;; This program is free software: you can redistribute it = and/or modify

;;; it under the terms of the GNU General Public License as = published by 

;;; the Free Software Foundation, either version 3 of the = License, or 

;;; (at your option) any later version.



spec.scm:

(define-module (language calc spec)

  #:export (calc)

  #:use-module (system base language)

  #:use-module (nyacc lang calc parser)

  #:use-module (nyacc lang calc compiler))


(define (calc-reader port env)

  (let ((iport (current-input-port)))

    (dynamic-wind

        (lambda () = (set-current-input-port port))

        (lambda () (calc-parse #:debug = #f))

        (lambda () = (set-current-input-port iport)))))


(define-language calc

  #:title       "calc"

  #:reader      calc-reader

  #:compilers   `((tree-il . = ,calc-sxml->tree-il))

  #:printer     write)



parser.scm:
(define-module (nyacc lang calc = parser)

  #:export (calc-parse calc-spec calc-mach)

  #:use-module (nyacc lalr)

  #:use-module (nyacc lex)

  #:use-module (nyacc parse)

  )


(define calc-spec

  (lalr-spec

   (prec< (left "+" "-") (left "*" "/"))

   (start stmt-list-proxy)

   (grammar


    (stmt-list-proxy

     (stmt-list "\n" ($$ (cons 'stmt-list = (reverse $1)))))


    (stmt-list

     (stmt ($$ (list $1)))

     (stmt-list ";" stmt ($$ (cons $3 = $1))))


    (stmt

     (ident "=3D" expr ($$ `(assn-stmt ,$1 = ,$3)))

     (expr ($$ `(expr-stmt ,$1)))

     ( ($$ '(empty-stmt))))


    (expr

     (expr "+" expr ($$ `(add ,$1 ,$3)))

     (expr "-" expr ($$ `(sub ,$1 ,$3)))

     (expr "*" expr ($$ `(mul ,$1 ,$3)))

     (expr "/" expr ($$ `(div ,$1 ,$3)))

     ('$fixed ($$ `(fixed ,$1)))

     ('$float ($$ `(float ,$1)))

     ("(" expr ")" ($$ $2)))


    (ident ('$ident ($$ = `(ident ,$1))))

    )))


(define calc-mach

  = (compact-machine

   (hashify-machine

     (make-lalr-machine calc-spec))))


(define = calc-parse

  (let ((gen-lexer (make-lexer-generator = (assq-ref calc-mach 'mtab)

           =                     =           #:space-chars " \t"))

        (parser (make-lalr-ia-parser = calc-mach)))

    (lambda* (#:key (debug #f)) (parser (gen-lexer) = #:debug debug))))



compiler.scm:

(define-module (nyacc lang calc compiler)

  #:export (calc-sxml->tree-il)

  #:use-module (sxml match)

  #:use-module (sxml fold)

  ;;#:use-module (system base language)

  #:use-module (language tree-il))


(define (fup tree)

  (sxml-match tree

    ((fixed ,fx) `(const ,(string->number = fx)))

    ((float ,fl) `(const ,(string->number = fl)))

    ((ident ,id) `(toplevel ,(string->symbol = id)))

    ((add ,lt ,rt) `(apply (toplevel +) ,lt = ,rt))

    ((sub ,lt ,rt) `(apply (toplevel -) ,lt = ,rt))

    ((mul ,lt ,rt) `(apply (toplevel *) ,lt = ,rt))

    ((div ,lt ,rt) `(apply (toplevel /) ,lt = ,rt))

    ((assn-stmt (toplevel ,lhs) ,rhs) `(define ,lhs = ,rhs))

    ((empty-stmt) '(begin))

    ((stmt-list ,items ...) `(begin ,items = ...))

    (,otherwise tree)))


(define (calc-sxml->tree-il exp env opts)

  (let* ((tree (foldt fup identity exp))

         (code (parse-tree-il = tree)))

    (values code env env)))


= --Apple-Mail=_9DD30009-A1A1-46CE-9AA8-04F7355AF09D--