From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.org!.POSTED!not-for-mail From: Matt Wette Newsgroups: gmane.lisp.guile.bugs Subject: bug#30094: proposed code for alt languages Date: Sun, 19 Aug 2018 14:24:40 -0700 Message-ID: References: <876086wvan.fsf@elephly.net> NNTP-Posting-Host: blaine.gmane.org Mime-Version: 1.0 Content-Type: multipart/mixed; boundary="------------9BEE22966A3B393C65FD3FB6" X-Trace: blaine.gmane.org 1534713789 21945 195.159.176.226 (19 Aug 2018 21:23:09 GMT) X-Complaints-To: usenet@blaine.gmane.org NNTP-Posting-Date: Sun, 19 Aug 2018 21:23:09 +0000 (UTC) User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:52.0) Gecko/20100101 Thunderbird/52.9.1 Cc: matt.wette@gmail.com To: 30094@debbugs.gnu.org Original-X-From: bug-guile-bounces+guile-bugs=m.gmane.org@gnu.org Sun Aug 19 23:23:05 2018 Return-path: Envelope-to: guile-bugs@m.gmane.org Original-Received: from lists.gnu.org ([208.118.235.17]) by blaine.gmane.org with esmtp (Exim 4.84_2) (envelope-from ) id 1frVA8-0005YU-He for guile-bugs@m.gmane.org; Sun, 19 Aug 2018 23:23:04 +0200 Original-Received: from localhost ([::1]:44152 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1frVCD-0004D2-DB for guile-bugs@m.gmane.org; Sun, 19 Aug 2018 17:25:13 -0400 Original-Received: from eggs.gnu.org ([2001:4830:134:3::10]:57683) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1frVC7-00049V-88 for bug-guile@gnu.org; Sun, 19 Aug 2018 17:25:08 -0400 Original-Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1frVC2-0000zX-BA for bug-guile@gnu.org; Sun, 19 Aug 2018 17:25:07 -0400 Original-Received: from debbugs.gnu.org ([208.118.235.43]:49847) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_128_CBC_SHA1:16) (Exim 4.71) (envelope-from ) id 1frVC2-0000zS-6b for bug-guile@gnu.org; Sun, 19 Aug 2018 17:25:02 -0400 Original-Received: from Debian-debbugs by debbugs.gnu.org with local (Exim 4.84_2) (envelope-from ) id 1frVC1-0003Om-Vs for bug-guile@gnu.org; Sun, 19 Aug 2018 17:25:02 -0400 X-Loop: help-debbugs@gnu.org In-Reply-To: <876086wvan.fsf@elephly.net> Resent-From: Matt Wette Original-Sender: "Debbugs-submit" Resent-CC: bug-guile@gnu.org Resent-Date: Sun, 19 Aug 2018 21:25:01 +0000 Resent-Message-ID: Resent-Sender: help-debbugs@gnu.org X-GNU-PR-Message: followup 30094 X-GNU-PR-Package: guile X-GNU-PR-Keywords: Original-Received: via spool by 30094-submit@debbugs.gnu.org id=B30094.153471388913041 (code B ref 30094); Sun, 19 Aug 2018 21:25:01 +0000 Original-Received: (at 30094) by debbugs.gnu.org; 19 Aug 2018 21:24:49 +0000 Original-Received: from localhost ([127.0.0.1]:54865 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1frVBo-0003OH-Vj for submit@debbugs.gnu.org; Sun, 19 Aug 2018 17:24:49 -0400 Original-Received: from mail-pl0-f51.google.com ([209.85.160.51]:46624) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1frVBn-0003O5-FB for 30094@debbugs.gnu.org; Sun, 19 Aug 2018 17:24:47 -0400 Original-Received: by mail-pl0-f51.google.com with SMTP id a4-v6so2578859plm.13 for <30094@debbugs.gnu.org>; Sun, 19 Aug 2018 14:24:47 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=to:from:subject:cc:message-id:date:user-agent:mime-version :content-language; bh=tXii2OIyDZIOG0ZZyI+sO25g8+83wZXa9qb+JBKgOUA=; b=Y0thArIHvLaPhWNR7jaLUeOTa4s8jX32MfsXyF5cmOd4tGBqNNDUmEgCVKSqMtWi0J Nrw/S6Kp+6aBCVTTkB7mjeth0G994wDNHLnHwcet/xpXJpb2OG9RyuCKl7/F9h180zXT jTPHhPkvE1Sj13VpBBRz9Nd0+Uf9Ew4VBHIuxiLLRk8FycElz4CuaomliC1mw98ORUCf /dZiVW0XL3EbOqO9fgpRKY5ISkJUbKAZoqtX3+zJ9p9kiYA3izOOciQOPWIflK2nu6EZ I1pbf8du3QkMkhDgF7Fte3APmYkXLCMy1aVZ06tlFifQjzCsudofav+XBX0UICaRNaao k0dg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:to:from:subject:cc:message-id:date:user-agent :mime-version:content-language; bh=tXii2OIyDZIOG0ZZyI+sO25g8+83wZXa9qb+JBKgOUA=; b=HB+/d1+RPAcwoNGdNT5VSyOedzPXeqdpkdF2btor35lqx+tZFnV+qSmPlOnNL6s4+V B23Xu1amzA7I/3/l8o2bmuoUkvzuoo08xaHNgtuk23zhArWxQY5AZleCmqfuwFqIXbW7 z4KQ06WizdxgBP8Pf4ImxG0+PDPYmKqL6kQHcVFeVbPT2dbg6c/wZnOMXt9DWIfp0rPm xYnCyRmyVmpD15MOLQKa0u+CK02aPXK69/MbnOhccCPS6H+umvCX1bMIeE+8U+IcAAZq IJYyBzg+wAUh9fofLqB+YXlRUZw9/4bk24tBZF2gYje1Y/0D6pYRYSUOUa4v9E56Dfxw iLxQ== X-Gm-Message-State: AOUpUlFoV7jVQ582LAdRLQLHDkdfYSK+uz/3oApzbtCa/ZYmAF5yKNCi xqxGOGkRq5xci0UymbEyX4M= X-Google-Smtp-Source: AA+uWPxIGD9yDTuc1AYI8ROWjyhc6nHWZX1y6HcdcKuTZdN8ciP/zFNfsPZ6aBMklxGRBqdSOrRmbA== X-Received: by 2002:a17:902:8b86:: with SMTP id ay6-v6mr42285653plb.25.1534713881409; Sun, 19 Aug 2018 14:24:41 -0700 (PDT) Original-Received: from [192.168.2.183] (216-165-229-229.championbroadband.com. [216.165.229.229]) by smtp.gmail.com with ESMTPSA id a17-v6sm12821549pfg.106.2018.08.19.14.24.40 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Sun, 19 Aug 2018 14:24:40 -0700 (PDT) Content-Language: en-US X-BeenThere: debbugs-submit@debbugs.gnu.org X-Mailman-Version: 2.1.18 Precedence: list X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] X-Received-From: 208.118.235.43 X-BeenThere: bug-guile@gnu.org List-Id: "Bug reports for GUILE, GNU's Ubiquitous Extension Language" List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: bug-guile-bounces+guile-bugs=m.gmane.org@gnu.org Original-Sender: "bug-guile" Xref: news.gmane.org gmane.lisp.guile.bugs:9119 Archived-At: This is a multi-part message in MIME format. --------------9BEE22966A3B393C65FD3FB6 Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: 7bit Hey all, The attached code implements alternative languages. It is roughly a patch to (system base compile). It provides 1) extra procedures lang-from-port and lang-from-file 2) the global %file-extension-map 3) an altered version of compile-file Behavior: 1) if the first line of the file is `#lang ' then that is used as from 2) if the file-ending matches an entry in the a-list %file-extension-map the ref is used Matt In the following, compile.scm and ncompile.scm are trimmed to only contain the procedure compile-file mwette$ diff -c compile.scm ncompile.scm *** compile.scm Sun Aug 19 14:19:44 2018 --- ncompile.scm Sun Aug 19 14:20:07 2018 *************** *** 1,10 **** ! (define* (compile-file file #:key ! (output-file #f) ! (from (current-language)) ! (to 'bytecode) ! (env (default-environment from)) ! (opts '()) ! (canonicalization 'relative)) (with-fluids ((%file-port-name-canonicalization canonicalization)) (let* ((comp (or output-file (compiled-file-name file) (error "failed to create path for auto-compiled file" --- 1,10 ---- ! (define* (ncompile-file file #:key ! (output-file #f) ! (from #f) ! (to 'bytecode) ! (env #f) ! (opts '()) ! (canonicalization 'relative)) (with-fluids ((%file-port-name-canonicalization canonicalization)) (let* ((comp (or output-file (compiled-file-name file) (error "failed to create path for auto-compiled file" *************** *** 16,25 **** (ensure-directory (dirname comp)) (call-with-output-file/atomic comp ! (lambda (port) ! ((language-printer (ensure-language to)) ! (read-and-compile in #:env env #:from from #:to to #:opts ! (cons* #:to-file? #t opts)) ! port)) ! file) ! comp))) --- 16,31 ---- (ensure-directory (dirname comp)) (call-with-output-file/atomic comp ! (lambda (port) ! (let* ((from (or from ! (lang-from-port in) ! (lang-from-file file) ! (current-language))) ! (env (or env (default-environment from)))) ! (simple-format (current-error-port) "compiling from lang ~A\n" from) ! ((language-printer (ensure-language to)) ! (read-and-compile in #:env env #:from from #:to to #:opts ! (cons* #:to-file? #t opts)) ! port))) ! file) ! comp))) Here is ncompile:;; ncompile v180819b ;; usage: ;; (ncompile-file "foo.m") ;; first checks for first line of the form ;; #lang ;; then uses file ending ".m" => nx-matlab (define-module (ncompile) #:export (ncompile-file) ) (define (lang-from-port port) (define (release chl) (let loop ((chl chl)) (unless (null? chl) (unread-char (car chl) port) (loop (cdr chl)))) #f) (define (return chl) (string->symbol (reverse-list->string chl))) (let loop ((cl '()) (st 0) (kl '(#\# #\l #\a #\n #\g)) (ch (read-char port))) (case st ((0) (cond ; read `#lang' ((eof-object? ch) (release cl)) ((null? kl) (loop cl 1 kl ch)) ((char=? ch (car kl)) (loop (cons ch cl) st (cdr kl) (read-char port))) (else (release (cons ch cl))))) ((1) (cond ; skip spaces ((eof-object? ch) (release cl)) ((char=? ch #\space) (loop (cons ch cl) st kl (read-char port))) (else (loop cl 2 '() ch)))) ((2) (cond ; collect lang name ((eof-object? ch) (return kl)) ((char=? ch #\newline) (return kl)) ((char-whitespace? ch) (loop cl 3 kl ch)) (else (loop cl st (cons ch kl) (read-char port))))) ((3) (cond ((eof-object? ch) (return kl)) ((char=? ch #\newline) (return kl)) (else (loop cl st kl (read-char port)))))))) (define %file-extension-map '(("scm" . scheme) ("el" . elisp) ("m" . nx-matlab) ("js" . ecmascript))) (define* (lang-from-file file) (let* ((ix (string-rindex file #\.)) (ext (and ix (substring file (1+ ix))))) (and ext (assoc-ref %file-extension-map ext)))) (define call-with-output-file/atomic (@@ (system base compile) call-with-output-file/atomic)) (define language-printer (@ (system base language) language-printer)) (define ensure-language (@@ (system base compile) ensure-language)) (define ensure-directory (@@ (system base compile) ensure-directory)) (define read-and-compile (@@ (system base compile) read-and-compile)) (define compiled-file-name (@@ (system base compile) compiled-file-name)) (define default-environment (@@ (system base compile) default-environment)) (define* (ncompile-file file #:key (output-file #f) (from #f) (to 'bytecode) (env #f) (opts '()) (canonicalization 'relative)) (with-fluids ((%file-port-name-canonicalization canonicalization)) (let* ((comp (or output-file (compiled-file-name file) (error "failed to create path for auto-compiled file" file))) (in (open-input-file file)) (enc (file-encoding in))) ;; Choose the input encoding deterministically. (set-port-encoding! in (or enc "UTF-8")) (ensure-directory (dirname comp)) (call-with-output-file/atomic comp (lambda (port) (let* ((from (or from (lang-from-port in) (lang-from-file file) (current-language))) (env (or env (default-environment from)))) (simple-format (current-error-port) "compiling from lang ~A\n" from) ((language-printer (ensure-language to)) (read-and-compile in #:env env #:from from #:to to #:opts (cons* #:to-file? #t opts)) port))) file) comp))) ;; Local Variables: ;; eval: (put 'call-with-output-file/atomic 'scheme-indent-function 1) ;; End: ;; --- last line --- --------------9BEE22966A3B393C65FD3FB6 Content-Type: text/x-scheme; name="ncompile.scm" Content-Transfer-Encoding: 7bit Content-Disposition: attachment; filename="ncompile.scm" ;; ncompile v180819b ;; usage: ;; (ncompile-file "foo.m") ;; first checks for first line of the form ;; #lang ;; then uses file ending ".m" => nx-matlab (define-module (ncompile) #:export (ncompile-file) ) (define (lang-from-port port) (define (release chl) (let loop ((chl chl)) (unless (null? chl) (unread-char (car chl) port) (loop (cdr chl)))) #f) (define (return chl) (string->symbol (reverse-list->string chl))) (let loop ((cl '()) (st 0) (kl '(#\# #\l #\a #\n #\g)) (ch (read-char port))) (case st ((0) (cond ; read `#lang' ((eof-object? ch) (release cl)) ((null? kl) (loop cl 1 kl ch)) ((char=? ch (car kl)) (loop (cons ch cl) st (cdr kl) (read-char port))) (else (release (cons ch cl))))) ((1) (cond ; skip spaces ((eof-object? ch) (release cl)) ((char=? ch #\space) (loop (cons ch cl) st kl (read-char port))) (else (loop cl 2 '() ch)))) ((2) (cond ; collect lang name ((eof-object? ch) (return kl)) ((char=? ch #\newline) (return kl)) ((char-whitespace? ch) (loop cl 3 kl ch)) (else (loop cl st (cons ch kl) (read-char port))))) ((3) (cond ((eof-object? ch) (return kl)) ((char=? ch #\newline) (return kl)) (else (loop cl st kl (read-char port)))))))) (define %file-extension-map '(("scm" . scheme) ("el" . elisp) ("m" . nx-matlab) ("js" . ecmascript))) (define* (lang-from-file file) (let* ((ix (string-rindex file #\.)) (ext (and ix (substring file (1+ ix))))) (and ext (assoc-ref %file-extension-map ext)))) (define call-with-output-file/atomic (@@ (system base compile) call-with-output-file/atomic)) (define language-printer (@ (system base language) language-printer)) (define ensure-language (@@ (system base compile) ensure-language)) (define ensure-directory (@@ (system base compile) ensure-directory)) (define read-and-compile (@@ (system base compile) read-and-compile)) (define compiled-file-name (@@ (system base compile) compiled-file-name)) (define default-environment (@@ (system base compile) default-environment)) (define* (ncompile-file file #:key (output-file #f) (from #f) (to 'bytecode) (env #f) (opts '()) (canonicalization 'relative)) (with-fluids ((%file-port-name-canonicalization canonicalization)) (let* ((comp (or output-file (compiled-file-name file) (error "failed to create path for auto-compiled file" file))) (in (open-input-file file)) (enc (file-encoding in))) ;; Choose the input encoding deterministically. (set-port-encoding! in (or enc "UTF-8")) (ensure-directory (dirname comp)) (call-with-output-file/atomic comp (lambda (port) (let* ((from (or from (lang-from-port in) (lang-from-file file) (current-language))) (env (or env (default-environment from)))) (simple-format (current-error-port) "compiling from lang ~A\n" from) ((language-printer (ensure-language to)) (read-and-compile in #:env env #:from from #:to to #:opts (cons* #:to-file? #t opts)) port))) file) comp))) ;; Local Variables: ;; eval: (put 'call-with-output-file/atomic 'scheme-indent-function 1) ;; End: ;; --- last line --- --------------9BEE22966A3B393C65FD3FB6--