From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.org!not-for-mail From: Nala Ginrut Newsgroups: gmane.lisp.guile.user Subject: Re: Q on (language spec) Date: Mon, 19 Oct 2015 11:53:02 +0800 Organization: HFG Message-ID: <1445226782.11524.38.camel@Renee-desktop.suse> References: <02BEFF46-4DA9-4222-989C-6AAA4FABDCE0@verizon.net> NNTP-Posting-Host: plane.gmane.org Mime-Version: 1.0 Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: 7bit X-Trace: ger.gmane.org 1445226815 21339 80.91.229.3 (19 Oct 2015 03:53:35 GMT) X-Complaints-To: usenet@ger.gmane.org NNTP-Posting-Date: Mon, 19 Oct 2015 03:53:35 +0000 (UTC) Cc: guile-user@gnu.org To: Matt Wette Original-X-From: guile-user-bounces+guile-user=m.gmane.org@gnu.org Mon Oct 19 05:53:27 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 1Zo1Vm-00069i-43 for guile-user@m.gmane.org; Mon, 19 Oct 2015 05:53:26 +0200 Original-Received: from localhost ([::1]:36345 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1Zo1Vl-0001VZ-9A for guile-user@m.gmane.org; Sun, 18 Oct 2015 23:53:25 -0400 Original-Received: from eggs.gnu.org ([2001:4830:134:3::10]:39838) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1Zo1Vc-0001V0-8O for guile-user@gnu.org; Sun, 18 Oct 2015 23:53:17 -0400 Original-Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1Zo1VY-0001f6-6S for guile-user@gnu.org; Sun, 18 Oct 2015 23:53:16 -0400 Original-Received: from mail-pa0-x230.google.com ([2607:f8b0:400e:c03::230]:33375) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1Zo1VX-0001f2-Lz for guile-user@gnu.org; Sun, 18 Oct 2015 23:53:11 -0400 Original-Received: by pabrc13 with SMTP id rc13so177377181pab.0 for ; Sun, 18 Oct 2015 20:53:10 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=message-id:subject:from:to:cc:date:in-reply-to:references :organization:content-type:mime-version:content-transfer-encoding; bh=Kes75ghlLtRCayiXjTiy/XIvaVGK1ph64D0H3Cr00z0=; b=gERYUx9rqWgfyubSK/q+FRTqO0iQhGnbPmvC9k4L2Oj0aYMe+Mutknbw2if0fCLkXM QsXVDeHdGuihMbGTOWCqtMYHNB9PLqoLPuJOvcikvOrwt/62TO+ynztvR1pwYw/TRbv8 sXw1c/N2r3I9TJ12xmlAffR/H3ICXdOMT6Qz0nW9my8jpnvobQRoVWVlU/w2fyvJPr0v zmBTb7M8A9h2mPJ4h15y+iqmHU4OBkPsZWgZ+vNWrI6OypR3bdYfZq/purcXjBPFg/zu P4FFFodMlJOPCF8QshpNu8KQWA2zACXtBnQ5Wf/P2+hlu6imxqO6Sxf4WMSoIKEok+dA khEA== X-Received: by 10.67.30.74 with SMTP id kc10mr31340833pad.147.1445226790706; Sun, 18 Oct 2015 20:53:10 -0700 (PDT) Original-Received: from [147.2.147.86] ([203.192.156.9]) by smtp.gmail.com with ESMTPSA id yg2sm33291659pbb.79.2015.10.18.20.53.09 (version=SSLv3 cipher=RC4-SHA bits=128/128); Sun, 18 Oct 2015 20:53:10 -0700 (PDT) In-Reply-To: <02BEFF46-4DA9-4222-989C-6AAA4FABDCE0@verizon.net> X-Mailer: Evolution 3.4.4 X-detected-operating-system: by eggs.gnu.org: Error: Malformed IPv6 address (bad octet value). X-Received-From: 2607:f8b0:400e:c03::230 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:12097 Archived-At: Nice work! For more generic discussion about multi-lang, I could share some opinions. Most of the time, we just need to convert our AST/IR to tree-il. But I saw old guile-lua frontend added something in the lower level. I haven't learned it deeper. There're two identical forms of tree-il, s-expr form and record form. Personally, I like s-expr one, it's simple to use. But record form could store the src meta info, which is considered better. And I'm grad that you write a new lexer generator (before it I only know silex), it's great! Would you like to make the generated tokens compatible with scm-lalr? If so, people may rewrite their lexer module with your lexer generator, and no need to rewrite the parser. I saw the token name is string rather than symbol, so I guess it's not compatible with scm-lalr. Happy hacking! On Sun, 2015-10-18 at 13:00 -0700, Matt Wette wrote: > > 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. > > scheme@(guile-user)> ,L calc > Happy hacking with calc! To switch back, type `,L scheme'. > calc@(guile-user)> a = (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 = 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. > > 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 "=" 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))) >