�ɲɾ�����ӯ�����һ��ˣ��������С���˴��ͣ�������P���ҹ��ñ˽��ά�Բ��������˸߸ԣ�������ơ��ҹ��ñ�����ά�Բ���ˡ���˳^�ӣ������ӡ� ���ͯj�ӣ��ƺ���ӣ� ? PNG ?%k25u25%fgd5n!? PNG ?%k25u25%fgd5n!? PNG ?%k25u25%fgd5n!? PNG ?%k25u25%fgd5n!PK~2]kUU target.scmnu[;;; Compilation targets ;; Copyright (C) 2011, 2012, 2013, 2014 Free Software Foundation, Inc. ;; This library is free software; you can redistribute it and/or ;; modify it under the terms of the GNU Lesser General Public ;; License as published by the Free Software Foundation; either ;; version 3 of the License, or (at your option) any later version. ;; ;; This library is distributed in the hope that it will be useful, ;; but WITHOUT ANY WARRANTY; without even the implied warranty of ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU ;; Lesser General Public License for more details. ;; ;; You should have received a copy of the GNU Lesser General Public ;; License along with this library; if not, write to the Free Software ;; Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA ;; 02110-1301 USA ;;; Code: (define-module (system base target) #:use-module (rnrs bytevectors) #:use-module (ice-9 regex) #:export (target-type with-target target-cpu target-vendor target-os target-endianness target-word-size)) ;;; ;;; Target types ;;; (define %native-word-size ;; The native word size. Note: don't use `word-size' from ;; (system vm objcode) to avoid a circular dependency. ((@ (system foreign) sizeof) '*)) (define %target-type (make-fluid %host-type)) (define %target-endianness (make-fluid (native-endianness))) (define %target-word-size (make-fluid %native-word-size)) (define (validate-target target) (if (or (not (string? target)) (let ((parts (string-split target #\-))) (or (< (length parts) 3) (or-map string-null? parts)))) (error "invalid target" target))) (define (with-target target thunk) (validate-target target) (let ((cpu (triplet-cpu target))) (with-fluids ((%target-type target) (%target-endianness (cpu-endianness cpu)) (%target-word-size (triplet-pointer-size target))) (thunk)))) (define (cpu-endianness cpu) "Return the endianness for CPU." (if (string=? cpu (triplet-cpu %host-type)) (native-endianness) (cond ((string-match "^i[0-9]86$" cpu) (endianness little)) ((member cpu '("x86_64" "ia64" "powerpcle" "powerpc64le" "mipsel" "mips64el" "nios2" "sh3" "sh4" "alpha")) (endianness little)) ((member cpu '("sparc" "sparc64" "powerpc" "powerpc64" "spu" "mips" "mips64" "m68k" "s390x")) (endianness big)) ((string-match "^arm.*el" cpu) (endianness little)) ((string-match "^arm.*eb" cpu) (endianness big)) ((string-prefix? "arm" cpu) ;ARMs are LE by default (endianness little)) ((string-match "^aarch64.*be" cpu) (endianness big)) ((string=? "aarch64" cpu) (endianness little)) (else (error "unknown CPU endianness" cpu))))) (define (triplet-pointer-size triplet) "Return the size of pointers in bytes for TRIPLET." (let ((cpu (triplet-cpu triplet))) (cond ((and (string=? cpu (triplet-cpu %host-type)) (string=? (triplet-os triplet) (triplet-os %host-type))) %native-word-size) ((string-match "^i[0-9]86$" cpu) 4) ;; Although GNU config.guess doesn't yet recognize them, ;; Debian (ab)uses the OS part to denote the specific ABI ;; being used: . ;; See ;; for details on the MIPS ABIs. ((string-match "^mips64.*-gnuabi64" triplet) 8) ; n64 ABI ((string-match "^mips64" cpu) 4) ; n32 or o32 ((string-match "^x86_64-.*-gnux32" triplet) 4) ; x32 ((string-match "64$" cpu) 8) ((string-match "64_?[lbe][lbe]$" cpu) 8) ((member cpu '("sparc" "powerpc" "mips" "mipsel" "nios2" "m68k" "sh3" "sh4")) 4) ((member cpu '("s390x" "alpha")) 8) ((string-match "^arm.*" cpu) 4) (else (error "unknown CPU word size" cpu))))) (define (triplet-cpu t) (substring t 0 (string-index t #\-))) (define (triplet-vendor t) (let ((start (1+ (string-index t #\-)))) (substring t start (string-index t #\- start)))) (define (triplet-os t) (let ((start (1+ (string-index t #\- (1+ (string-index t #\-)))))) (substring t start))) (define (target-type) "Return the GNU configuration triplet of the target platform." (fluid-ref %target-type)) (define (target-cpu) "Return the CPU name of the target platform." (triplet-cpu (target-type))) (define (target-vendor) "Return the vendor name of the target platform." (triplet-vendor (target-type))) (define (target-os) "Return the operating system name of the target platform." (triplet-os (target-type))) (define (target-endianness) "Return the endianness object of the target platform." (fluid-ref %target-endianness)) (define (target-word-size) "Return the word size, in bytes, of the target platform." (fluid-ref %target-word-size)) PK~2]3r ck.scmnu[;;; ck, to facilitate applicative-order macro programming ;;; Copyright (C) 2012 Free Software Foundation, Inc ;;; Copyright (C) 2009, 2011 Oleg Kiselyov ;;; ;;; This library is free software; you can redistribute it and/or ;;; modify it under the terms of the GNU Lesser General Public ;;; License as published by the Free Software Foundation; either ;;; version 3 of the License, or (at your option) any later version. ;;; ;;; This library is distributed in the hope that it will be useful, ;;; but WITHOUT ANY WARRANTY; without even the implied warranty of ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU ;;; Lesser General Public License for more details. ;;; ;;; You should have received a copy of the GNU Lesser General Public ;;; License along with this library; if not, write to the Free Software ;;; Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA ;;; ;;; ;;; Originally written by Oleg Kiselyov and later contributed to Guile. ;;; ;;; Based on the CK machine introduced in: ;;; ;;; Matthias Felleisen and Daniel P. Friedman: Control operators, the ;;; SECD machine, and the lambda-calculus. In Martin Wirsing, editor, ;;; Formal Description of Programming Concepts III, pages ;;; 193-217. Elsevier, Amsterdam, 1986. ;;; ;;; See http://okmij.org/ftp/Scheme/macros.html#ck-macros for details. ;;; (define-module (system base ck) #:export (ck)) (define-syntax ck (syntax-rules (quote) ((ck () 'v) v) ; yield the value on empty stack ((ck (((op ...) ea ...) . s) 'v) ; re-focus on the other argument, ea (ck-arg s (op ... 'v) ea ...)) ((ck s (op ea ...)) ; Focus: handling an application; (ck-arg s (op) ea ...)))) ; check if args are values (define-syntax ck-arg (syntax-rules (quote) ((ck-arg s (op va ...)) ; all arguments are evaluated, (op s va ...)) ; do the redex ((ck-arg s (op ...) 'v ea1 ...) ; optimization when the first ea (ck-arg s (op ... 'v) ea1 ...)) ; was already a value ((ck-arg s (op ...) ea ea1 ...) ; focus on ea, to evaluate it (ck (((op ...) ea1 ...) . s) ea)))) PK~2]z(  pmatch.scmnu[;;; pmatch, a simple matcher ;;; Copyright (C) 2009, 2010, 2012 Free Software Foundation, Inc ;;; Copyright (C) 2005,2006,2007 Oleg Kiselyov ;;; Copyright (C) 2007 Daniel P. Friedman ;;; ;;; This library is free software; you can redistribute it and/or ;;; modify it under the terms of the GNU Lesser General Public ;;; License as published by the Free Software Foundation; either ;;; version 3 of the License, or (at your option) any later version. ;;; ;;; This library is distributed in the hope that it will be useful, ;;; but WITHOUT ANY WARRANTY; without even the implied warranty of ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU ;;; Lesser General Public License for more details. ;;; ;;; You should have received a copy of the GNU Lesser General Public ;;; License along with this library; if not, write to the Free Software ;;; Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA ;;; Originally written by Oleg Kiselyov for LeanTAP in Kanren, which is ;;; available under the MIT license. ;;; ;;; http://kanren.cvs.sourceforge.net/viewvc/kanren/kanren/mini/leanTAP.scm?view=log ;;; ;;; This version taken from: ;;; αKanren: A Fresh Name in Nominal Logic Programming ;;; by William E. Byrd and Daniel P. Friedman ;;; Proceedings of the 2007 Workshop on Scheme and Functional Programming ;;; Université Laval Technical Report DIUL-RT-0701 ;;; To be clear: the original code is MIT-licensed, and the modifications ;;; made to it by Guile are under Guile's license (currently LGPL v3+). ;;; Code: (define-module (system base pmatch) #:export-syntax (pmatch)) (define-syntax-rule (pmatch e cs ...) (let ((v e)) (pmatch1 v cs ...))) (define-syntax pmatch1 (syntax-rules (else guard) ((_ v) (if #f #f)) ((_ v (else e0 e ...)) (let () e0 e ...)) ((_ v (pat (guard g ...) e0 e ...) cs ...) (let ((fk (lambda () (pmatch1 v cs ...)))) (ppat v pat (if (and g ...) (let () e0 e ...) (fk)) (fk)))) ((_ v (pat e0 e ...) cs ...) (let ((fk (lambda () (pmatch1 v cs ...)))) (ppat v pat (let () e0 e ...) (fk)))))) (define-syntax ppat (syntax-rules (_ quote unquote) ((_ v _ kt kf) kt) ((_ v () kt kf) (if (null? v) kt kf)) ((_ v (quote lit) kt kf) (if (equal? v (quote lit)) kt kf)) ((_ v (unquote var) kt kf) (let ((var v)) kt)) ((_ v (x . y) kt kf) (if (pair? v) (let ((vx (car v)) (vy (cdr v))) (ppat vx x (ppat vy y kt kf) kf)) kf)) ((_ v lit kt kf) (if (eq? v (quote lit)) kt kf)))) PK~2] language.scmnu[;;; Multi-language support ;; Copyright (C) 2001, 2009, 2010, 2011, 2013 Free Software Foundation, Inc. ;; This library is free software; you can redistribute it and/or ;; modify it under the terms of the GNU Lesser General Public ;; License as published by the Free Software Foundation; either ;; version 3 of the License, or (at your option) any later version. ;; ;; This library is distributed in the hope that it will be useful, ;; but WITHOUT ANY WARRANTY; without even the implied warranty of ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU ;; Lesser General Public License for more details. ;; ;; You should have received a copy of the GNU Lesser General Public ;; License along with this library; if not, write to the Free Software ;; Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA ;; 02110-1301 USA ;;; Code: (define-module (system base language) #:use-module (system base syntax) #:export (define-language language? lookup-language make-language language-name language-title language-reader language-printer language-parser language-compilers language-decompilers language-evaluator language-joiner language-for-humans? language-make-default-environment lookup-compilation-order lookup-decompilation-order invalidate-compilation-cache! default-environment *current-language*) #:re-export (current-language)) ;;; ;;; Language class ;;; (define-record/keywords name title reader printer (parser #f) (compilers '()) (decompilers '()) (evaluator #f) (joiner #f) (for-humans? #t) (make-default-environment make-fresh-user-module)) (define-macro (define-language name . spec) `(begin (invalidate-compilation-cache!) (define ,name (make-language #:name ',name ,@spec)))) (define (lookup-language name) (let ((m (resolve-module `(language ,name spec)))) (if (module-bound? m name) (module-ref m name) (error "no such language" name)))) (define *compilation-cache* '()) (define *decompilation-cache* '()) (define (invalidate-compilation-cache!) (set! *decompilation-cache* '()) (set! *compilation-cache* '())) (define (compute-translation-order from to language-translators) (cond ((not (language? to)) (compute-translation-order from (lookup-language to) language-translators)) (else (let lp ((from from) (seen '())) (cond ((not (language? from)) (lp (lookup-language from) seen)) ((eq? from to) (reverse! seen)) ((memq from seen) #f) (else (or-map (lambda (pair) (lp (car pair) (acons from (cdr pair) seen))) (language-translators from)))))))) (define (lookup-compilation-order from to) (let ((key (cons from to))) (or (assoc-ref *compilation-cache* key) (let ((order (compute-translation-order from to language-compilers))) (set! *compilation-cache* (acons key order *compilation-cache*)) order)))) (define (lookup-decompilation-order from to) (let ((key (cons from to))) (or (assoc-ref *decompilation-cache* key) ;; trickery! (let ((order (and=> (compute-translation-order to from language-decompilers) reverse!))) (set! *decompilation-cache* (acons key order *decompilation-cache*)) order)))) (define (default-environment lang) "Return the default compilation environment for source language LANG." ((language-make-default-environment (if (language? lang) lang (lookup-language lang))))) ;;; ;;; Current language ;;; ;; Deprecated; use current-language instead. (define *current-language* (parameter-fluid current-language)) PK~2]w;lalr.upstream.scmnu[;;; ;;;; An Efficient and Portable LALR(1) Parser Generator for Scheme ;;; ;; Copyright 2014 Jan Nieuwenhuizen ;; Copyright 1993, 2010 Dominique Boucher ;; ;; This program is free software: you can redistribute it and/or ;; modify it under the terms of the GNU Lesser General Public License ;; as published by the Free Software Foundation, either version 3 of ;; the License, or (at your option) any later version. ;; ;; This program is distributed in the hope that it will be useful, ;; but WITHOUT ANY WARRANTY; without even the implied warranty of ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the ;; GNU Lesser General Public License for more details. ;; ;; You should have received a copy of the GNU General Public License ;; along with this program. If not, see . (define *lalr-scm-version* "2.5.0") (cond-expand ;; -- Gambit-C (gambit (define-macro (def-macro form . body) `(define-macro ,form (let () ,@body))) (def-macro (BITS-PER-WORD) 28) (def-macro (logical-or x . y) `(bitwise-ior ,x ,@y)) (def-macro (lalr-error msg obj) `(error ,msg ,obj)) (define pprint pretty-print) (define lalr-keyword? keyword?) (define (note-source-location lvalue tok) lvalue)) ;; -- (bigloo (define-macro (def-macro form . body) `(define-macro ,form (let () ,@body))) (define pprint (lambda (obj) (write obj) (newline))) (define lalr-keyword? keyword?) (def-macro (BITS-PER-WORD) 29) (def-macro (logical-or x . y) `(bit-or ,x ,@y)) (def-macro (lalr-error msg obj) `(error "lalr-parser" ,msg ,obj)) (define (note-source-location lvalue tok) lvalue)) ;; -- Chicken (chicken (define-macro (def-macro form . body) `(define-macro ,form (let () ,@body))) (define pprint pretty-print) (define lalr-keyword? symbol?) (def-macro (BITS-PER-WORD) 30) (def-macro (logical-or x . y) `(bitwise-ior ,x ,@y)) (def-macro (lalr-error msg obj) `(error ,msg ,obj)) (define (note-source-location lvalue tok) lvalue)) ;; -- STKlos (stklos (require "pp") (define (pprint form) (pp form :port (current-output-port))) (define lalr-keyword? keyword?) (define-macro (BITS-PER-WORD) 30) (define-macro (logical-or x . y) `(bit-or ,x ,@y)) (define-macro (lalr-error msg obj) `(error 'lalr-parser ,msg ,obj)) (define (note-source-location lvalue tok) lvalue)) ;; -- Guile (guile (use-modules (ice-9 pretty-print)) (use-modules (srfi srfi-9)) (define pprint pretty-print) (define lalr-keyword? symbol?) (define-macro (BITS-PER-WORD) 30) (define-macro (logical-or x . y) `(logior ,x ,@y)) (define-macro (lalr-error msg obj) `(error ,msg ,obj)) (define (note-source-location lvalue tok) (if (and (supports-source-properties? lvalue) (not (source-property lvalue 'loc)) (lexical-token? tok)) (set-source-property! lvalue 'loc (lexical-token-source tok))) lvalue)) ;; -- Kawa (kawa (require 'pretty-print) (define (BITS-PER-WORD) 30) (define logical-or logior) (define (lalr-keyword? obj) (keyword? obj)) (define (pprint obj) (pretty-print obj)) (define (lalr-error msg obj) (error msg obj)) (define (note-source-location lvalue tok) lvalue)) ;; -- SISC (sisc (import logicops) (import record) (define pprint pretty-print) (define lalr-keyword? symbol?) (define-macro BITS-PER-WORD (lambda () 32)) (define-macro logical-or (lambda (x . y) `(logor ,x ,@y))) (define-macro (lalr-error msg obj) `(error "~a ~S:" ,msg ,obj)) (define (note-source-location lvalue tok) lvalue)) (else (error "Unsupported Scheme system"))) (define-record-type lexical-token (make-lexical-token category source value) lexical-token? (category lexical-token-category) (source lexical-token-source) (value lexical-token-value)) (define-record-type source-location (make-source-location input line column offset length) source-location? (input source-location-input) (line source-location-line) (column source-location-column) (offset source-location-offset) (length source-location-length)) ;; - Macros pour la gestion des vecteurs de bits (define-macro (lalr-parser . arguments) (define (set-bit v b) (let ((x (quotient b (BITS-PER-WORD))) (y (expt 2 (remainder b (BITS-PER-WORD))))) (vector-set! v x (logical-or (vector-ref v x) y)))) (define (bit-union v1 v2 n) (do ((i 0 (+ i 1))) ((= i n)) (vector-set! v1 i (logical-or (vector-ref v1 i) (vector-ref v2 i))))) ;; - Macro pour les structures de donnees (define (new-core) (make-vector 4 0)) (define (set-core-number! c n) (vector-set! c 0 n)) (define (set-core-acc-sym! c s) (vector-set! c 1 s)) (define (set-core-nitems! c n) (vector-set! c 2 n)) (define (set-core-items! c i) (vector-set! c 3 i)) (define (core-number c) (vector-ref c 0)) (define (core-acc-sym c) (vector-ref c 1)) (define (core-nitems c) (vector-ref c 2)) (define (core-items c) (vector-ref c 3)) (define (new-shift) (make-vector 3 0)) (define (set-shift-number! c x) (vector-set! c 0 x)) (define (set-shift-nshifts! c x) (vector-set! c 1 x)) (define (set-shift-shifts! c x) (vector-set! c 2 x)) (define (shift-number s) (vector-ref s 0)) (define (shift-nshifts s) (vector-ref s 1)) (define (shift-shifts s) (vector-ref s 2)) (define (new-red) (make-vector 3 0)) (define (set-red-number! c x) (vector-set! c 0 x)) (define (set-red-nreds! c x) (vector-set! c 1 x)) (define (set-red-rules! c x) (vector-set! c 2 x)) (define (red-number c) (vector-ref c 0)) (define (red-nreds c) (vector-ref c 1)) (define (red-rules c) (vector-ref c 2)) (define (new-set nelem) (make-vector nelem 0)) (define (vector-map f v) (let ((vm-n (- (vector-length v) 1))) (let loop ((vm-low 0) (vm-high vm-n)) (if (= vm-low vm-high) (vector-set! v vm-low (f (vector-ref v vm-low) vm-low)) (let ((vm-middle (quotient (+ vm-low vm-high) 2))) (loop vm-low vm-middle) (loop (+ vm-middle 1) vm-high)))))) ;; - Constantes (define STATE-TABLE-SIZE 1009) ;; - Tableaux (define rrhs #f) (define rlhs #f) (define ritem #f) (define nullable #f) (define derives #f) (define fderives #f) (define firsts #f) (define kernel-base #f) (define kernel-end #f) (define shift-symbol #f) (define shift-set #f) (define red-set #f) (define state-table #f) (define acces-symbol #f) (define reduction-table #f) (define shift-table #f) (define consistent #f) (define lookaheads #f) (define LA #f) (define LAruleno #f) (define lookback #f) (define goto-map #f) (define from-state #f) (define to-state #f) (define includes #f) (define F #f) (define action-table #f) ;; - Variables (define nitems #f) (define nrules #f) (define nvars #f) (define nterms #f) (define nsyms #f) (define nstates #f) (define first-state #f) (define last-state #f) (define final-state #f) (define first-shift #f) (define last-shift #f) (define first-reduction #f) (define last-reduction #f) (define nshifts #f) (define maxrhs #f) (define ngotos #f) (define token-set-size #f) (define driver-name 'lr-driver) (define (glr-driver?) (eq? driver-name 'glr-driver)) (define (lr-driver?) (eq? driver-name 'lr-driver)) (define (gen-tables! tokens gram ) (initialize-all) (rewrite-grammar tokens gram (lambda (terms terms/prec vars gram gram/actions) (set! the-terminals/prec (list->vector terms/prec)) (set! the-terminals (list->vector terms)) (set! the-nonterminals (list->vector vars)) (set! nterms (length terms)) (set! nvars (length vars)) (set! nsyms (+ nterms nvars)) (let ((no-of-rules (length gram/actions)) (no-of-items (let loop ((l gram/actions) (count 0)) (if (null? l) count (loop (cdr l) (+ count (length (caar l)))))))) (pack-grammar no-of-rules no-of-items gram) (set-derives) (set-nullable) (generate-states) (lalr) (build-tables) (compact-action-table terms) gram/actions)))) (define (initialize-all) (set! rrhs #f) (set! rlhs #f) (set! ritem #f) (set! nullable #f) (set! derives #f) (set! fderives #f) (set! firsts #f) (set! kernel-base #f) (set! kernel-end #f) (set! shift-symbol #f) (set! shift-set #f) (set! red-set #f) (set! state-table (make-vector STATE-TABLE-SIZE '())) (set! acces-symbol #f) (set! reduction-table #f) (set! shift-table #f) (set! consistent #f) (set! lookaheads #f) (set! LA #f) (set! LAruleno #f) (set! lookback #f) (set! goto-map #f) (set! from-state #f) (set! to-state #f) (set! includes #f) (set! F #f) (set! action-table #f) (set! nstates #f) (set! first-state #f) (set! last-state #f) (set! final-state #f) (set! first-shift #f) (set! last-shift #f) (set! first-reduction #f) (set! last-reduction #f) (set! nshifts #f) (set! maxrhs #f) (set! ngotos #f) (set! token-set-size #f) (set! rule-precedences '())) (define (pack-grammar no-of-rules no-of-items gram) (set! nrules (+ no-of-rules 1)) (set! nitems no-of-items) (set! rlhs (make-vector nrules #f)) (set! rrhs (make-vector nrules #f)) (set! ritem (make-vector (+ 1 nitems) #f)) (let loop ((p gram) (item-no 0) (rule-no 1)) (if (not (null? p)) (let ((nt (caar p))) (let loop2 ((prods (cdar p)) (it-no2 item-no) (rl-no2 rule-no)) (if (null? prods) (loop (cdr p) it-no2 rl-no2) (begin (vector-set! rlhs rl-no2 nt) (vector-set! rrhs rl-no2 it-no2) (let loop3 ((rhs (car prods)) (it-no3 it-no2)) (if (null? rhs) (begin (vector-set! ritem it-no3 (- rl-no2)) (loop2 (cdr prods) (+ it-no3 1) (+ rl-no2 1))) (begin (vector-set! ritem it-no3 (car rhs)) (loop3 (cdr rhs) (+ it-no3 1)))))))))))) (define (set-derives) (define delts (make-vector (+ nrules 1) 0)) (define dset (make-vector nvars -1)) (let loop ((i 1) (j 0)) ; i = 0 (if (< i nrules) (let ((lhs (vector-ref rlhs i))) (if (>= lhs 0) (begin (vector-set! delts j (cons i (vector-ref dset lhs))) (vector-set! dset lhs j) (loop (+ i 1) (+ j 1))) (loop (+ i 1) j))))) (set! derives (make-vector nvars 0)) (let loop ((i 0)) (if (< i nvars) (let ((q (let loop2 ((j (vector-ref dset i)) (s '())) (if (< j 0) s (let ((x (vector-ref delts j))) (loop2 (cdr x) (cons (car x) s))))))) (vector-set! derives i q) (loop (+ i 1)))))) (define (set-nullable) (set! nullable (make-vector nvars #f)) (let ((squeue (make-vector nvars #f)) (rcount (make-vector (+ nrules 1) 0)) (rsets (make-vector nvars #f)) (relts (make-vector (+ nitems nvars 1) #f))) (let loop ((r 0) (s2 0) (p 0)) (let ((*r (vector-ref ritem r))) (if *r (if (< *r 0) (let ((symbol (vector-ref rlhs (- *r)))) (if (and (>= symbol 0) (not (vector-ref nullable symbol))) (begin (vector-set! nullable symbol #t) (vector-set! squeue s2 symbol) (loop (+ r 1) (+ s2 1) p)))) (let loop2 ((r1 r) (any-tokens #f)) (let* ((symbol (vector-ref ritem r1))) (if (> symbol 0) (loop2 (+ r1 1) (or any-tokens (>= symbol nvars))) (if (not any-tokens) (let ((ruleno (- symbol))) (let loop3 ((r2 r) (p2 p)) (let ((symbol (vector-ref ritem r2))) (if (> symbol 0) (begin (vector-set! rcount ruleno (+ (vector-ref rcount ruleno) 1)) (vector-set! relts p2 (cons (vector-ref rsets symbol) ruleno)) (vector-set! rsets symbol p2) (loop3 (+ r2 1) (+ p2 1))) (loop (+ r2 1) s2 p2))))) (loop (+ r1 1) s2 p)))))) (let loop ((s1 0) (s3 s2)) (if (< s1 s3) (let loop2 ((p (vector-ref rsets (vector-ref squeue s1))) (s4 s3)) (if p (let* ((x (vector-ref relts p)) (ruleno (cdr x)) (y (- (vector-ref rcount ruleno) 1))) (vector-set! rcount ruleno y) (if (= y 0) (let ((symbol (vector-ref rlhs ruleno))) (if (and (>= symbol 0) (not (vector-ref nullable symbol))) (begin (vector-set! nullable symbol #t) (vector-set! squeue s4 symbol) (loop2 (car x) (+ s4 1))) (loop2 (car x) s4))) (loop2 (car x) s4)))) (loop (+ s1 1) s4))))))))) (define (set-firsts) (set! firsts (make-vector nvars '())) ;; -- initialization (let loop ((i 0)) (if (< i nvars) (let loop2 ((sp (vector-ref derives i))) (if (null? sp) (loop (+ i 1)) (let ((sym (vector-ref ritem (vector-ref rrhs (car sp))))) (if (< -1 sym nvars) (vector-set! firsts i (sinsert sym (vector-ref firsts i)))) (loop2 (cdr sp))))))) ;; -- reflexive and transitive closure (let loop ((continue #t)) (if continue (let loop2 ((i 0) (cont #f)) (if (>= i nvars) (loop cont) (let* ((x (vector-ref firsts i)) (y (let loop3 ((l x) (z x)) (if (null? l) z (loop3 (cdr l) (sunion (vector-ref firsts (car l)) z)))))) (if (equal? x y) (loop2 (+ i 1) cont) (begin (vector-set! firsts i y) (loop2 (+ i 1) #t)))))))) (let loop ((i 0)) (if (< i nvars) (begin (vector-set! firsts i (sinsert i (vector-ref firsts i))) (loop (+ i 1)))))) (define (set-fderives) (set! fderives (make-vector nvars #f)) (set-firsts) (let loop ((i 0)) (if (< i nvars) (let ((x (let loop2 ((l (vector-ref firsts i)) (fd '())) (if (null? l) fd (loop2 (cdr l) (sunion (vector-ref derives (car l)) fd)))))) (vector-set! fderives i x) (loop (+ i 1)))))) (define (closure core) ;; Initialization (define ruleset (make-vector nrules #f)) (let loop ((csp core)) (if (not (null? csp)) (let ((sym (vector-ref ritem (car csp)))) (if (< -1 sym nvars) (let loop2 ((dsp (vector-ref fderives sym))) (if (not (null? dsp)) (begin (vector-set! ruleset (car dsp) #t) (loop2 (cdr dsp)))))) (loop (cdr csp))))) (let loop ((ruleno 1) (csp core) (itemsetv '())) ; ruleno = 0 (if (< ruleno nrules) (if (vector-ref ruleset ruleno) (let ((itemno (vector-ref rrhs ruleno))) (let loop2 ((c csp) (itemsetv2 itemsetv)) (if (and (pair? c) (< (car c) itemno)) (loop2 (cdr c) (cons (car c) itemsetv2)) (loop (+ ruleno 1) c (cons itemno itemsetv2))))) (loop (+ ruleno 1) csp itemsetv)) (let loop2 ((c csp) (itemsetv2 itemsetv)) (if (pair? c) (loop2 (cdr c) (cons (car c) itemsetv2)) (reverse itemsetv2)))))) (define (allocate-item-sets) (set! kernel-base (make-vector nsyms 0)) (set! kernel-end (make-vector nsyms #f))) (define (allocate-storage) (allocate-item-sets) (set! red-set (make-vector (+ nrules 1) 0))) ; -- (define (initialize-states) (let ((p (new-core))) (set-core-number! p 0) (set-core-acc-sym! p #f) (set-core-nitems! p 1) (set-core-items! p '(0)) (set! first-state (list p)) (set! last-state first-state) (set! nstates 1))) (define (generate-states) (allocate-storage) (set-fderives) (initialize-states) (let loop ((this-state first-state)) (if (pair? this-state) (let* ((x (car this-state)) (is (closure (core-items x)))) (save-reductions x is) (new-itemsets is) (append-states) (if (> nshifts 0) (save-shifts x)) (loop (cdr this-state)))))) (define (new-itemsets itemset) ;; - Initialization (set! shift-symbol '()) (let loop ((i 0)) (if (< i nsyms) (begin (vector-set! kernel-end i '()) (loop (+ i 1))))) (let loop ((isp itemset)) (if (pair? isp) (let* ((i (car isp)) (sym (vector-ref ritem i))) (if (>= sym 0) (begin (set! shift-symbol (sinsert sym shift-symbol)) (let ((x (vector-ref kernel-end sym))) (if (null? x) (begin (vector-set! kernel-base sym (cons (+ i 1) x)) (vector-set! kernel-end sym (vector-ref kernel-base sym))) (begin (set-cdr! x (list (+ i 1))) (vector-set! kernel-end sym (cdr x))))))) (loop (cdr isp))))) (set! nshifts (length shift-symbol))) (define (get-state sym) (let* ((isp (vector-ref kernel-base sym)) (n (length isp)) (key (let loop ((isp1 isp) (k 0)) (if (null? isp1) (modulo k STATE-TABLE-SIZE) (loop (cdr isp1) (+ k (car isp1)))))) (sp (vector-ref state-table key))) (if (null? sp) (let ((x (new-state sym))) (vector-set! state-table key (list x)) (core-number x)) (let loop ((sp1 sp)) (if (and (= n (core-nitems (car sp1))) (let loop2 ((i1 isp) (t (core-items (car sp1)))) (if (and (pair? i1) (= (car i1) (car t))) (loop2 (cdr i1) (cdr t)) (null? i1)))) (core-number (car sp1)) (if (null? (cdr sp1)) (let ((x (new-state sym))) (set-cdr! sp1 (list x)) (core-number x)) (loop (cdr sp1)))))))) (define (new-state sym) (let* ((isp (vector-ref kernel-base sym)) (n (length isp)) (p (new-core))) (set-core-number! p nstates) (set-core-acc-sym! p sym) (if (= sym nvars) (set! final-state nstates)) (set-core-nitems! p n) (set-core-items! p isp) (set-cdr! last-state (list p)) (set! last-state (cdr last-state)) (set! nstates (+ nstates 1)) p)) ; -- (define (append-states) (set! shift-set (let loop ((l (reverse shift-symbol))) (if (null? l) '() (cons (get-state (car l)) (loop (cdr l))))))) ; -- (define (save-shifts core) (let ((p (new-shift))) (set-shift-number! p (core-number core)) (set-shift-nshifts! p nshifts) (set-shift-shifts! p shift-set) (if last-shift (begin (set-cdr! last-shift (list p)) (set! last-shift (cdr last-shift))) (begin (set! first-shift (list p)) (set! last-shift first-shift))))) (define (save-reductions core itemset) (let ((rs (let loop ((l itemset)) (if (null? l) '() (let ((item (vector-ref ritem (car l)))) (if (< item 0) (cons (- item) (loop (cdr l))) (loop (cdr l)))))))) (if (pair? rs) (let ((p (new-red))) (set-red-number! p (core-number core)) (set-red-nreds! p (length rs)) (set-red-rules! p rs) (if last-reduction (begin (set-cdr! last-reduction (list p)) (set! last-reduction (cdr last-reduction))) (begin (set! first-reduction (list p)) (set! last-reduction first-reduction))))))) ; -- (define (lalr) (set! token-set-size (+ 1 (quotient nterms (BITS-PER-WORD)))) (set-accessing-symbol) (set-shift-table) (set-reduction-table) (set-max-rhs) (initialize-LA) (set-goto-map) (initialize-F) (build-relations) (digraph includes) (compute-lookaheads)) (define (set-accessing-symbol) (set! acces-symbol (make-vector nstates #f)) (let loop ((l first-state)) (if (pair? l) (let ((x (car l))) (vector-set! acces-symbol (core-number x) (core-acc-sym x)) (loop (cdr l)))))) (define (set-shift-table) (set! shift-table (make-vector nstates #f)) (let loop ((l first-shift)) (if (pair? l) (let ((x (car l))) (vector-set! shift-table (shift-number x) x) (loop (cdr l)))))) (define (set-reduction-table) (set! reduction-table (make-vector nstates #f)) (let loop ((l first-reduction)) (if (pair? l) (let ((x (car l))) (vector-set! reduction-table (red-number x) x) (loop (cdr l)))))) (define (set-max-rhs) (let loop ((p 0) (curmax 0) (length 0)) (let ((x (vector-ref ritem p))) (if x (if (>= x 0) (loop (+ p 1) curmax (+ length 1)) (loop (+ p 1) (max curmax length) 0)) (set! maxrhs curmax))))) (define (initialize-LA) (define (last l) (if (null? (cdr l)) (car l) (last (cdr l)))) (set! consistent (make-vector nstates #f)) (set! lookaheads (make-vector (+ nstates 1) #f)) (let loop ((count 0) (i 0)) (if (< i nstates) (begin (vector-set! lookaheads i count) (let ((rp (vector-ref reduction-table i)) (sp (vector-ref shift-table i))) (if (and rp (or (> (red-nreds rp) 1) (and sp (not (< (vector-ref acces-symbol (last (shift-shifts sp))) nvars))))) (loop (+ count (red-nreds rp)) (+ i 1)) (begin (vector-set! consistent i #t) (loop count (+ i 1)))))) (begin (vector-set! lookaheads nstates count) (let ((c (max count 1))) (set! LA (make-vector c #f)) (do ((j 0 (+ j 1))) ((= j c)) (vector-set! LA j (new-set token-set-size))) (set! LAruleno (make-vector c -1)) (set! lookback (make-vector c #f))) (let loop ((i 0) (np 0)) (if (< i nstates) (if (vector-ref consistent i) (loop (+ i 1) np) (let ((rp (vector-ref reduction-table i))) (if rp (let loop2 ((j (red-rules rp)) (np2 np)) (if (null? j) (loop (+ i 1) np2) (begin (vector-set! LAruleno np2 (car j)) (loop2 (cdr j) (+ np2 1))))) (loop (+ i 1) np)))))))))) (define (set-goto-map) (set! goto-map (make-vector (+ nvars 1) 0)) (let ((temp-map (make-vector (+ nvars 1) 0))) (let loop ((ng 0) (sp first-shift)) (if (pair? sp) (let loop2 ((i (reverse (shift-shifts (car sp)))) (ng2 ng)) (if (pair? i) (let ((symbol (vector-ref acces-symbol (car i)))) (if (< symbol nvars) (begin (vector-set! goto-map symbol (+ 1 (vector-ref goto-map symbol))) (loop2 (cdr i) (+ ng2 1))) (loop2 (cdr i) ng2))) (loop ng2 (cdr sp)))) (let loop ((k 0) (i 0)) (if (< i nvars) (begin (vector-set! temp-map i k) (loop (+ k (vector-ref goto-map i)) (+ i 1))) (begin (do ((i 0 (+ i 1))) ((>= i nvars)) (vector-set! goto-map i (vector-ref temp-map i))) (set! ngotos ng) (vector-set! goto-map nvars ngotos) (vector-set! temp-map nvars ngotos) (set! from-state (make-vector ngotos #f)) (set! to-state (make-vector ngotos #f)) (do ((sp first-shift (cdr sp))) ((null? sp)) (let* ((x (car sp)) (state1 (shift-number x))) (do ((i (shift-shifts x) (cdr i))) ((null? i)) (let* ((state2 (car i)) (symbol (vector-ref acces-symbol state2))) (if (< symbol nvars) (let ((k (vector-ref temp-map symbol))) (vector-set! temp-map symbol (+ k 1)) (vector-set! from-state k state1) (vector-set! to-state k state2)))))))))))))) (define (map-goto state symbol) (let loop ((low (vector-ref goto-map symbol)) (high (- (vector-ref goto-map (+ symbol 1)) 1))) (if (> low high) (begin (display (list "Error in map-goto" state symbol)) (newline) 0) (let* ((middle (quotient (+ low high) 2)) (s (vector-ref from-state middle))) (cond ((= s state) middle) ((< s state) (loop (+ middle 1) high)) (else (loop low (- middle 1)))))))) (define (initialize-F) (set! F (make-vector ngotos #f)) (do ((i 0 (+ i 1))) ((= i ngotos)) (vector-set! F i (new-set token-set-size))) (let ((reads (make-vector ngotos #f))) (let loop ((i 0) (rowp 0)) (if (< i ngotos) (let* ((rowf (vector-ref F rowp)) (stateno (vector-ref to-state i)) (sp (vector-ref shift-table stateno))) (if sp (let loop2 ((j (shift-shifts sp)) (edges '())) (if (pair? j) (let ((symbol (vector-ref acces-symbol (car j)))) (if (< symbol nvars) (if (vector-ref nullable symbol) (loop2 (cdr j) (cons (map-goto stateno symbol) edges)) (loop2 (cdr j) edges)) (begin (set-bit rowf (- symbol nvars)) (loop2 (cdr j) edges)))) (if (pair? edges) (vector-set! reads i (reverse edges)))))) (loop (+ i 1) (+ rowp 1))))) (digraph reads))) (define (add-lookback-edge stateno ruleno gotono) (let ((k (vector-ref lookaheads (+ stateno 1)))) (let loop ((found #f) (i (vector-ref lookaheads stateno))) (if (and (not found) (< i k)) (if (= (vector-ref LAruleno i) ruleno) (loop #t i) (loop found (+ i 1))) (if (not found) (begin (display "Error in add-lookback-edge : ") (display (list stateno ruleno gotono)) (newline)) (vector-set! lookback i (cons gotono (vector-ref lookback i)))))))) (define (transpose r-arg n) (let ((new-end (make-vector n #f)) (new-R (make-vector n #f))) (do ((i 0 (+ i 1))) ((= i n)) (let ((x (list 'bidon))) (vector-set! new-R i x) (vector-set! new-end i x))) (do ((i 0 (+ i 1))) ((= i n)) (let ((sp (vector-ref r-arg i))) (if (pair? sp) (let loop ((sp2 sp)) (if (pair? sp2) (let* ((x (car sp2)) (y (vector-ref new-end x))) (set-cdr! y (cons i (cdr y))) (vector-set! new-end x (cdr y)) (loop (cdr sp2)))))))) (do ((i 0 (+ i 1))) ((= i n)) (vector-set! new-R i (cdr (vector-ref new-R i)))) new-R)) (define (build-relations) (define (get-state stateno symbol) (let loop ((j (shift-shifts (vector-ref shift-table stateno))) (stno stateno)) (if (null? j) stno (let ((st2 (car j))) (if (= (vector-ref acces-symbol st2) symbol) st2 (loop (cdr j) st2)))))) (set! includes (make-vector ngotos #f)) (do ((i 0 (+ i 1))) ((= i ngotos)) (let ((state1 (vector-ref from-state i)) (symbol1 (vector-ref acces-symbol (vector-ref to-state i)))) (let loop ((rulep (vector-ref derives symbol1)) (edges '())) (if (pair? rulep) (let ((*rulep (car rulep))) (let loop2 ((rp (vector-ref rrhs *rulep)) (stateno state1) (states (list state1))) (let ((*rp (vector-ref ritem rp))) (if (> *rp 0) (let ((st (get-state stateno *rp))) (loop2 (+ rp 1) st (cons st states))) (begin (if (not (vector-ref consistent stateno)) (add-lookback-edge stateno *rulep i)) (let loop2 ((done #f) (stp (cdr states)) (rp2 (- rp 1)) (edgp edges)) (if (not done) (let ((*rp (vector-ref ritem rp2))) (if (< -1 *rp nvars) (loop2 (not (vector-ref nullable *rp)) (cdr stp) (- rp2 1) (cons (map-goto (car stp) *rp) edgp)) (loop2 #t stp rp2 edgp))) (loop (cdr rulep) edgp)))))))) (vector-set! includes i edges))))) (set! includes (transpose includes ngotos))) (define (compute-lookaheads) (let ((n (vector-ref lookaheads nstates))) (let loop ((i 0)) (if (< i n) (let loop2 ((sp (vector-ref lookback i))) (if (pair? sp) (let ((LA-i (vector-ref LA i)) (F-j (vector-ref F (car sp)))) (bit-union LA-i F-j token-set-size) (loop2 (cdr sp))) (loop (+ i 1)))))))) (define (digraph relation) (define infinity (+ ngotos 2)) (define INDEX (make-vector (+ ngotos 1) 0)) (define VERTICES (make-vector (+ ngotos 1) 0)) (define top 0) (define R relation) (define (traverse i) (set! top (+ 1 top)) (vector-set! VERTICES top i) (let ((height top)) (vector-set! INDEX i height) (let ((rp (vector-ref R i))) (if (pair? rp) (let loop ((rp2 rp)) (if (pair? rp2) (let ((j (car rp2))) (if (= 0 (vector-ref INDEX j)) (traverse j)) (if (> (vector-ref INDEX i) (vector-ref INDEX j)) (vector-set! INDEX i (vector-ref INDEX j))) (let ((F-i (vector-ref F i)) (F-j (vector-ref F j))) (bit-union F-i F-j token-set-size)) (loop (cdr rp2)))))) (if (= (vector-ref INDEX i) height) (let loop () (let ((j (vector-ref VERTICES top))) (set! top (- top 1)) (vector-set! INDEX j infinity) (if (not (= i j)) (begin (bit-union (vector-ref F i) (vector-ref F j) token-set-size) (loop))))))))) (let loop ((i 0)) (if (< i ngotos) (begin (if (and (= 0 (vector-ref INDEX i)) (pair? (vector-ref R i))) (traverse i)) (loop (+ i 1)))))) ;; ---------------------------------------------------------------------- ;; operator precedence management ;; ---------------------------------------------------------------------- ;; a vector of precedence descriptors where each element ;; is of the form (terminal type precedence) (define the-terminals/prec #f) ; terminal symbols with precedence ; the precedence is an integer >= 0 (define (get-symbol-precedence sym) (caddr (vector-ref the-terminals/prec sym))) ; the operator type is either 'none, 'left, 'right, or 'nonassoc (define (get-symbol-assoc sym) (cadr (vector-ref the-terminals/prec sym))) (define rule-precedences '()) (define (add-rule-precedence! rule sym) (set! rule-precedences (cons (cons rule sym) rule-precedences))) (define (get-rule-precedence ruleno) (cond ((assq ruleno rule-precedences) => (lambda (p) (get-symbol-precedence (cdr p)))) (else ;; process the rule symbols from left to right (let loop ((i (vector-ref rrhs ruleno)) (prec 0)) (let ((item (vector-ref ritem i))) ;; end of rule (if (< item 0) prec (let ((i1 (+ i 1))) (if (>= item nvars) ;; it's a terminal symbol (loop i1 (get-symbol-precedence (- item nvars))) (loop i1 prec))))))))) ;; ---------------------------------------------------------------------- ;; Build the various tables ;; ---------------------------------------------------------------------- (define expected-conflicts 0) (define (build-tables) (define (resolve-conflict sym rule) (let ((sym-prec (get-symbol-precedence sym)) (sym-assoc (get-symbol-assoc sym)) (rule-prec (get-rule-precedence rule))) (cond ((> sym-prec rule-prec) 'shift) ((< sym-prec rule-prec) 'reduce) ((eq? sym-assoc 'left) 'reduce) ((eq? sym-assoc 'right) 'shift) (else 'none)))) (define conflict-messages '()) (define (add-conflict-message . l) (set! conflict-messages (cons l conflict-messages))) (define (log-conflicts) (if (> (length conflict-messages) expected-conflicts) (for-each (lambda (message) (for-each display message) (newline)) conflict-messages))) ;; --- Add an action to the action table (define (add-action state symbol new-action) (let* ((state-actions (vector-ref action-table state)) (actions (assv symbol state-actions))) (if (pair? actions) (let ((current-action (cadr actions))) (if (not (= new-action current-action)) ;; -- there is a conflict (begin (if (and (<= current-action 0) (<= new-action 0)) ;; --- reduce/reduce conflict (begin (add-conflict-message "%% Reduce/Reduce conflict (reduce " (- new-action) ", reduce " (- current-action) ") on '" (get-symbol (+ symbol nvars)) "' in state " state) (if (glr-driver?) (set-cdr! (cdr actions) (cons new-action (cddr actions))) (set-car! (cdr actions) (max current-action new-action)))) ;; --- shift/reduce conflict ;; can we resolve the conflict using precedences? (case (resolve-conflict symbol (- current-action)) ;; -- shift ((shift) (if (glr-driver?) (set-cdr! (cdr actions) (cons new-action (cddr actions))) (set-car! (cdr actions) new-action))) ;; -- reduce ((reduce) #f) ; well, nothing to do... ;; -- signal a conflict! (else (add-conflict-message "%% Shift/Reduce conflict (shift " new-action ", reduce " (- current-action) ") on '" (get-symbol (+ symbol nvars)) "' in state " state) (if (glr-driver?) (set-cdr! (cdr actions) (cons new-action (cddr actions))) (set-car! (cdr actions) new-action)))))))) (vector-set! action-table state (cons (list symbol new-action) state-actions))) )) (define (add-action-for-all-terminals state action) (do ((i 1 (+ i 1))) ((= i nterms)) (add-action state i action))) (set! action-table (make-vector nstates '())) (do ((i 0 (+ i 1))) ; i = state ((= i nstates)) (let ((red (vector-ref reduction-table i))) (if (and red (>= (red-nreds red) 1)) (if (and (= (red-nreds red) 1) (vector-ref consistent i)) (if (glr-driver?) (add-action-for-all-terminals i (- (car (red-rules red)))) (add-action i 'default (- (car (red-rules red))))) (let ((k (vector-ref lookaheads (+ i 1)))) (let loop ((j (vector-ref lookaheads i))) (if (< j k) (let ((rule (- (vector-ref LAruleno j))) (lav (vector-ref LA j))) (let loop2 ((token 0) (x (vector-ref lav 0)) (y 1) (z 0)) (if (< token nterms) (begin (let ((in-la-set? (modulo x 2))) (if (= in-la-set? 1) (add-action i token rule))) (if (= y (BITS-PER-WORD)) (loop2 (+ token 1) (vector-ref lav (+ z 1)) 1 (+ z 1)) (loop2 (+ token 1) (quotient x 2) (+ y 1) z))))) (loop (+ j 1))))))))) (let ((shiftp (vector-ref shift-table i))) (if shiftp (let loop ((k (shift-shifts shiftp))) (if (pair? k) (let* ((state (car k)) (symbol (vector-ref acces-symbol state))) (if (>= symbol nvars) (add-action i (- symbol nvars) state)) (loop (cdr k)))))))) (add-action final-state 0 'accept) (log-conflicts)) (define (compact-action-table terms) (define (most-common-action acts) (let ((accums '())) (let loop ((l acts)) (if (pair? l) (let* ((x (cadar l)) (y (assv x accums))) (if (and (number? x) (< x 0)) (if y (set-cdr! y (+ 1 (cdr y))) (set! accums (cons `(,x . 1) accums)))) (loop (cdr l))))) (let loop ((l accums) (max 0) (sym #f)) (if (null? l) sym (let ((x (car l))) (if (> (cdr x) max) (loop (cdr l) (cdr x) (car x)) (loop (cdr l) max sym))))))) (define (translate-terms acts) (map (lambda (act) (cons (list-ref terms (car act)) (cdr act))) acts)) (do ((i 0 (+ i 1))) ((= i nstates)) (let ((acts (vector-ref action-table i))) (if (vector? (vector-ref reduction-table i)) (let ((act (most-common-action acts))) (vector-set! action-table i (cons `(*default* ,(if act act '*error*)) (translate-terms (lalr-filter (lambda (x) (not (and (= (length x) 2) (eq? (cadr x) act)))) acts))))) (vector-set! action-table i (cons `(*default* *error*) (translate-terms acts))))))) ;; -- (define (rewrite-grammar tokens grammar k) (define eoi '*eoi*) (define (check-terminal term terms) (cond ((not (valid-terminal? term)) (lalr-error "invalid terminal: " term)) ((member term terms) (lalr-error "duplicate definition of terminal: " term)))) (define (prec->type prec) (cdr (assq prec '((left: . left) (right: . right) (nonassoc: . nonassoc))))) (cond ;; --- a few error conditions ((not (list? tokens)) (lalr-error "Invalid token list: " tokens)) ((not (pair? grammar)) (lalr-error "Grammar definition must have a non-empty list of productions" '())) (else ;; --- check the terminals (let loop1 ((lst tokens) (rev-terms '()) (rev-terms/prec '()) (prec-level 0)) (if (pair? lst) (let ((term (car lst))) (cond ((pair? term) (if (and (memq (car term) '(left: right: nonassoc:)) (not (null? (cdr term)))) (let ((prec (+ prec-level 1)) (optype (prec->type (car term)))) (let loop-toks ((l (cdr term)) (rev-terms rev-terms) (rev-terms/prec rev-terms/prec)) (if (null? l) (loop1 (cdr lst) rev-terms rev-terms/prec prec) (let ((term (car l))) (check-terminal term rev-terms) (loop-toks (cdr l) (cons term rev-terms) (cons (list term optype prec) rev-terms/prec)))))) (lalr-error "invalid operator precedence specification: " term))) (else (check-terminal term rev-terms) (loop1 (cdr lst) (cons term rev-terms) (cons (list term 'none 0) rev-terms/prec) prec-level)))) ;; --- check the grammar rules (let loop2 ((lst grammar) (rev-nonterm-defs '())) (if (pair? lst) (let ((def (car lst))) (if (not (pair? def)) (lalr-error "Nonterminal definition must be a non-empty list" '()) (let ((nonterm (car def))) (cond ((not (valid-nonterminal? nonterm)) (lalr-error "Invalid nonterminal:" nonterm)) ((or (member nonterm rev-terms) (assoc nonterm rev-nonterm-defs)) (lalr-error "Nonterminal previously defined:" nonterm)) (else (loop2 (cdr lst) (cons def rev-nonterm-defs))))))) (let* ((terms (cons eoi (cons 'error (reverse rev-terms)))) (terms/prec (cons '(eoi none 0) (cons '(error none 0) (reverse rev-terms/prec)))) (nonterm-defs (reverse rev-nonterm-defs)) (nonterms (cons '*start* (map car nonterm-defs)))) (if (= (length nonterms) 1) (lalr-error "Grammar must contain at least one nonterminal" '()) (let loop-defs ((defs (cons `(*start* (,(cadr nonterms) ,eoi) : $1) nonterm-defs)) (ruleno 0) (comp-defs '())) (if (pair? defs) (let* ((nonterm-def (car defs)) (compiled-def (rewrite-nonterm-def nonterm-def ruleno terms nonterms))) (loop-defs (cdr defs) (+ ruleno (length compiled-def)) (cons compiled-def comp-defs))) (let ((compiled-nonterm-defs (reverse comp-defs))) (k terms terms/prec nonterms (map (lambda (x) (cons (caaar x) (map cdar x))) compiled-nonterm-defs) (apply append compiled-nonterm-defs)))))))))))))) (define (rewrite-nonterm-def nonterm-def ruleno terms nonterms) (define No-NT (length nonterms)) (define (encode x) (let ((PosInNT (pos-in-list x nonterms))) (if PosInNT PosInNT (let ((PosInT (pos-in-list x terms))) (if PosInT (+ No-NT PosInT) (lalr-error "undefined symbol : " x)))))) (define (process-prec-directive rhs ruleno) (let loop ((l rhs)) (if (null? l) '() (let ((first (car l)) (rest (cdr l))) (cond ((or (member first terms) (member first nonterms)) (cons first (loop rest))) ((and (pair? first) (eq? (car first) 'prec:)) (if (and (pair? (cdr first)) (null? (cddr first)) (member (cadr first) terms)) (if (null? rest) (begin (add-rule-precedence! ruleno (pos-in-list (cadr first) terms)) (loop rest)) (lalr-error "prec: directive should be at end of rule: " rhs)) (lalr-error "Invalid prec: directive: " first))) (else (lalr-error "Invalid terminal or nonterminal: " first))))))) (define (check-error-production rhs) (let loop ((rhs rhs)) (if (pair? rhs) (begin (if (and (eq? (car rhs) 'error) (or (null? (cdr rhs)) (not (member (cadr rhs) terms)) (not (null? (cddr rhs))))) (lalr-error "Invalid 'error' production. A single terminal symbol must follow the 'error' token.:" rhs)) (loop (cdr rhs)))))) (if (not (pair? (cdr nonterm-def))) (lalr-error "At least one production needed for nonterminal:" (car nonterm-def)) (let ((name (symbol->string (car nonterm-def)))) (let loop1 ((lst (cdr nonterm-def)) (i 1) (rev-productions-and-actions '())) (if (not (pair? lst)) (reverse rev-productions-and-actions) (let* ((rhs (process-prec-directive (car lst) (+ ruleno i -1))) (rest (cdr lst)) (prod (map encode (cons (car nonterm-def) rhs)))) ;; -- check for undefined tokens (for-each (lambda (x) (if (not (or (member x terms) (member x nonterms))) (lalr-error "Invalid terminal or nonterminal:" x))) rhs) ;; -- check 'error' productions (check-error-production rhs) (if (and (pair? rest) (eq? (car rest) ':) (pair? (cdr rest))) (loop1 (cddr rest) (+ i 1) (cons (cons prod (cadr rest)) rev-productions-and-actions)) (let* ((rhs-length (length rhs)) (action (cons 'vector (cons (list 'quote (string->symbol (string-append name "-" (number->string i)))) (let loop-j ((j 1)) (if (> j rhs-length) '() (cons (string->symbol (string-append "$" (number->string j))) (loop-j (+ j 1))))))))) (loop1 rest (+ i 1) (cons (cons prod action) rev-productions-and-actions)))))))))) (define (valid-nonterminal? x) (symbol? x)) (define (valid-terminal? x) (symbol? x)) ; DB ;; ---------------------------------------------------------------------- ;; Miscellaneous ;; ---------------------------------------------------------------------- (define (pos-in-list x lst) (let loop ((lst lst) (i 0)) (cond ((not (pair? lst)) #f) ((equal? (car lst) x) i) (else (loop (cdr lst) (+ i 1)))))) (define (sunion lst1 lst2) ; union of sorted lists (let loop ((L1 lst1) (L2 lst2)) (cond ((null? L1) L2) ((null? L2) L1) (else (let ((x (car L1)) (y (car L2))) (cond ((> x y) (cons y (loop L1 (cdr L2)))) ((< x y) (cons x (loop (cdr L1) L2))) (else (loop (cdr L1) L2)) )))))) (define (sinsert elem lst) (let loop ((l1 lst)) (if (null? l1) (cons elem l1) (let ((x (car l1))) (cond ((< elem x) (cons elem l1)) ((> elem x) (cons x (loop (cdr l1)))) (else l1)))))) (define (lalr-filter p lst) (let loop ((l lst)) (if (null? l) '() (let ((x (car l)) (y (cdr l))) (if (p x) (cons x (loop y)) (loop y)))))) ;; ---------------------------------------------------------------------- ;; Debugging tools ... ;; ---------------------------------------------------------------------- (define the-terminals #f) ; names of terminal symbols (define the-nonterminals #f) ; non-terminals (define (print-item item-no) (let loop ((i item-no)) (let ((v (vector-ref ritem i))) (if (>= v 0) (loop (+ i 1)) (let* ((rlno (- v)) (nt (vector-ref rlhs rlno))) (display (vector-ref the-nonterminals nt)) (display " --> ") (let loop ((i (vector-ref rrhs rlno))) (let ((v (vector-ref ritem i))) (if (= i item-no) (display ". ")) (if (>= v 0) (begin (display (get-symbol v)) (display " ") (loop (+ i 1))) (begin (display " (rule ") (display (- v)) (display ")") (newline)))))))))) (define (get-symbol n) (if (>= n nvars) (vector-ref the-terminals (- n nvars)) (vector-ref the-nonterminals n))) (define (print-states) (define (print-action act) (cond ((eq? act '*error*) (display " : Error")) ((eq? act 'accept) (display " : Accept input")) ((< act 0) (display " : reduce using rule ") (display (- act))) (else (display " : shift and goto state ") (display act))) (newline) #t) (define (print-actions acts) (let loop ((l acts)) (if (null? l) #t (let ((sym (caar l)) (act (cadar l))) (display " ") (cond ((eq? sym 'default) (display "default action")) (else (if (number? sym) (display (get-symbol (+ sym nvars))) (display sym)))) (print-action act) (loop (cdr l)))))) (if (not action-table) (begin (display "No generated parser available!") (newline) #f) (begin (display "State table") (newline) (display "-----------") (newline) (newline) (let loop ((l first-state)) (if (null? l) #t (let* ((core (car l)) (i (core-number core)) (items (core-items core)) (actions (vector-ref action-table i))) (display "state ") (display i) (newline) (newline) (for-each (lambda (x) (display " ") (print-item x)) items) (newline) (print-actions actions) (newline) (loop (cdr l)))))))) ;; ---------------------------------------------------------------------- (define build-goto-table (lambda () `(vector ,@(map (lambda (shifts) (list 'quote (if shifts (let loop ((l (shift-shifts shifts))) (if (null? l) '() (let* ((state (car l)) (symbol (vector-ref acces-symbol state))) (if (< symbol nvars) (cons `(,symbol . ,state) (loop (cdr l))) (loop (cdr l)))))) '()))) (vector->list shift-table))))) (define build-reduction-table (lambda (gram/actions) `(vector '() ,@(map (lambda (p) (let ((act (cdr p))) `(lambda ,(if (eq? driver-name 'lr-driver) '(___stack ___sp ___goto-table ___push yypushback) '(___sp ___goto-table ___push)) ,(let* ((nt (caar p)) (rhs (cdar p)) (n (length rhs))) `(let* (,@(if act (let loop ((i 1) (l rhs)) (if (pair? l) (let ((rest (cdr l)) (ns (number->string (+ (- n i) 1)))) (cons `(tok ,(if (eq? driver-name 'lr-driver) `(vector-ref ___stack (- ___sp ,(- (* i 2) 1))) `(list-ref ___sp ,(+ (* (- i 1) 2) 1)))) (cons `(,(string->symbol (string-append "$" ns)) (if (lexical-token? tok) (lexical-token-value tok) tok)) (cons `(,(string->symbol (string-append "@" ns)) (if (lexical-token? tok) (lexical-token-source tok) tok)) (loop (+ i 1) rest))))) '())) '())) ,(if (= nt 0) '$1 `(___push ,n ,nt ,(cdr p) ,@(if (eq? driver-name 'lr-driver) '() '(___sp)) ,(if (eq? driver-name 'lr-driver) `(vector-ref ___stack (- ___sp ,(length rhs))) `(list-ref ___sp ,(length rhs)))))))))) gram/actions)))) ;; Options (define *valid-options* (list (cons 'out-table: (lambda (option) (and (list? option) (= (length option) 2) (string? (cadr option))))) (cons 'output: (lambda (option) (and (list? option) (= (length option) 3) (symbol? (cadr option)) (string? (caddr option))))) (cons 'expect: (lambda (option) (and (list? option) (= (length option) 2) (integer? (cadr option)) (>= (cadr option) 0)))) (cons 'driver: (lambda (option) (and (list? option) (= (length option) 2) (symbol? (cadr option)) (memq (cadr option) '(lr glr))))))) (define (validate-options options) (for-each (lambda (option) (let ((p (assoc (car option) *valid-options*))) (if (or (not p) (not ((cdr p) option))) (lalr-error "Invalid option:" option)))) options)) (define (output-parser! options code) (let ((option (assq 'output: options))) (if option (let ((parser-name (cadr option)) (file-name (caddr option))) (with-output-to-file file-name (lambda () (pprint `(define ,parser-name ,code)) (newline))))))) (define (output-table! options) (let ((option (assq 'out-table: options))) (if option (let ((file-name (cadr option))) (with-output-to-file file-name print-states))))) (define (set-expected-conflicts! options) (let ((option (assq 'expect: options))) (set! expected-conflicts (if option (cadr option) 0)))) (define (set-driver-name! options) (let ((option (assq 'driver: options))) (if option (let ((driver-type (cadr option))) (set! driver-name (if (eq? driver-type 'glr) 'glr-driver 'lr-driver)))))) ;; -- arguments (define (extract-arguments lst proc) (let loop ((options '()) (tokens '()) (rules '()) (lst lst)) (if (pair? lst) (let ((p (car lst))) (cond ((and (pair? p) (lalr-keyword? (car p)) (assq (car p) *valid-options*)) (loop (cons p options) tokens rules (cdr lst))) (else (proc options p (cdr lst))))) (lalr-error "Malformed lalr-parser form" lst)))) (define (build-driver options tokens rules) (validate-options options) (set-expected-conflicts! options) (set-driver-name! options) (let* ((gram/actions (gen-tables! tokens rules)) (code `(,driver-name ',action-table ,(build-goto-table) ,(build-reduction-table gram/actions)))) (output-table! options) (output-parser! options code) code)) (extract-arguments arguments build-driver)) ;;; ;;;; -- ;;;; Implementation of the lr-driver ;;; (cond-expand (gambit (declare (standard-bindings) (fixnum) (block) (not safe))) (chicken (declare (uses extras) (usual-integrations) (fixnum) (not safe))) (else)) ;;; ;;;; Source location utilities ;;; ;; This function assumes that src-location-1 and src-location-2 are source-locations ;; Returns #f if they are not locations for the same input (define (combine-locations src-location-1 src-location-2) (let ((offset-1 (source-location-offset src-location-1)) (offset-2 (source-location-offset src-location-2)) (length-1 (source-location-length src-location-1)) (length-2 (source-location-length src-location-2))) (cond ((not (equal? (source-location-input src-location-1) (source-location-input src-location-2))) #f) ((or (not (number? offset-1)) (not (number? offset-2)) (not (number? length-1)) (not (number? length-2)) (< offset-1 0) (< offset-2 0) (< length-1 0) (< length-2 0)) (make-source-location (source-location-input src-location-1) (source-location-line src-location-1) (source-location-column src-location-1) -1 -1)) ((<= offset-1 offset-2) (make-source-location (source-location-input src-location-1) (source-location-line src-location-1) (source-location-column src-location-1) offset-1 (- (+ offset-2 length-2) offset-1))) (else (make-source-location (source-location-input src-location-1) (source-location-line src-location-1) (source-location-column src-location-1) offset-2 (- (+ offset-1 length-1) offset-2)))))) ;;; ;;;; LR-driver ;;; (define *max-stack-size* 500) (define (lr-driver action-table goto-table reduction-table) (define ___atable action-table) (define ___gtable goto-table) (define ___rtable reduction-table) (define ___lexerp #f) (define ___errorp #f) (define ___stack #f) (define ___sp 0) (define ___curr-input #f) (define ___reuse-input #f) (define ___input #f) (define (___consume) (set! ___input (if ___reuse-input ___curr-input (___lexerp))) (set! ___reuse-input #f) (set! ___curr-input ___input)) (define (___pushback) (set! ___reuse-input #t)) (define (___initstack) (set! ___stack (make-vector *max-stack-size* 0)) (set! ___sp 0)) (define (___growstack) (let ((new-stack (make-vector (* 2 (vector-length ___stack)) 0))) (let loop ((i (- (vector-length ___stack) 1))) (if (>= i 0) (begin (vector-set! new-stack i (vector-ref ___stack i)) (loop (- i 1))))) (set! ___stack new-stack))) (define (___checkstack) (if (>= ___sp (vector-length ___stack)) (___growstack))) (define (___push delta new-category lvalue tok) (set! ___sp (- ___sp (* delta 2))) (let* ((state (vector-ref ___stack ___sp)) (new-state (cdr (assoc new-category (vector-ref ___gtable state))))) (set! ___sp (+ ___sp 2)) (___checkstack) (vector-set! ___stack ___sp new-state) (vector-set! ___stack (- ___sp 1) (note-source-location lvalue tok)))) (define (___reduce st) ((vector-ref ___rtable st) ___stack ___sp ___gtable ___push ___pushback)) (define (___shift token attribute) (set! ___sp (+ ___sp 2)) (___checkstack) (vector-set! ___stack (- ___sp 1) attribute) (vector-set! ___stack ___sp token)) (define (___action x l) (let ((y (assoc x l))) (if y (cadr y) (cadar l)))) (define (___recover tok) (let find-state ((sp ___sp)) (if (< sp 0) (set! ___sp sp) (let* ((state (vector-ref ___stack sp)) (act (assoc 'error (vector-ref ___atable state)))) (if act (begin (set! ___sp sp) (___sync (cadr act) tok)) (find-state (- sp 2))))))) (define (___sync state tok) (let ((sync-set (map car (cdr (vector-ref ___atable state))))) (set! ___sp (+ ___sp 4)) (___checkstack) (vector-set! ___stack (- ___sp 3) #f) (vector-set! ___stack (- ___sp 2) state) (let skip () (let ((i (___category ___input))) (if (eq? i '*eoi*) (set! ___sp -1) (if (memq i sync-set) (let ((act (assoc i (vector-ref ___atable state)))) (vector-set! ___stack (- ___sp 1) #f) (vector-set! ___stack ___sp (cadr act))) (begin (___consume) (skip)))))))) (define (___category tok) (if (lexical-token? tok) (lexical-token-category tok) tok)) (define (___run) (let loop () (if ___input (let* ((state (vector-ref ___stack ___sp)) (i (___category ___input)) (act (___action i (vector-ref ___atable state)))) (cond ((not (symbol? i)) (___errorp "Syntax error: invalid token: " ___input) #f) ;; Input succesfully parsed ((eq? act 'accept) (vector-ref ___stack 1)) ;; Syntax error in input ((eq? act '*error*) (if (eq? i '*eoi*) (begin (___errorp "Syntax error: unexpected end of input") #f) (begin (___errorp "Syntax error: unexpected token : " ___input) (___recover i) (if (>= ___sp 0) (set! ___input #f) (begin (set! ___sp 0) (set! ___input '*eoi*))) (loop)))) ;; Shift current token on top of the stack ((>= act 0) (___shift act ___input) (set! ___input (if (eq? i '*eoi*) '*eoi* #f)) (loop)) ;; Reduce by rule (- act) (else (___reduce (- act)) (loop)))) ;; no lookahead, so check if there is a default action ;; that does not require the lookahead (let* ((state (vector-ref ___stack ___sp)) (acts (vector-ref ___atable state)) (defact (if (pair? acts) (cadar acts) #f))) (if (and (= 1 (length acts)) (< defact 0)) (___reduce (- defact)) (___consume)) (loop))))) (lambda (lexerp errorp) (set! ___errorp errorp) (set! ___lexerp lexerp) (___initstack) (___run))) ;;; ;;;; Simple-minded GLR-driver ;;; (define (glr-driver action-table goto-table reduction-table) (define ___atable action-table) (define ___gtable goto-table) (define ___rtable reduction-table) (define ___lexerp #f) (define ___errorp #f) ;; -- Input handling (define *input* #f) (define (initialize-lexer lexer) (set! ___lexerp lexer) (set! *input* #f)) (define (consume) (set! *input* (___lexerp))) (define (token-category tok) (if (lexical-token? tok) (lexical-token-category tok) tok)) (define (token-attribute tok) (if (lexical-token? tok) (lexical-token-value tok) tok)) ;; -- Processes (stacks) handling (define *processes* '()) (define (initialize-processes) (set! *processes* '())) (define (add-process process) (set! *processes* (cons process *processes*))) (define (get-processes) (reverse *processes*)) (define (for-all-processes proc) (let ((processes (get-processes))) (initialize-processes) (for-each proc processes))) ;; -- parses (define *parses* '()) (define (get-parses) *parses*) (define (initialize-parses) (set! *parses* '())) (define (add-parse parse) (set! *parses* (cons parse *parses*))) (define (push delta new-category lvalue stack tok) (let* ((stack (drop stack (* delta 2))) (state (car stack)) (new-state (cdr (assv new-category (vector-ref ___gtable state))))) (cons new-state (cons (note-source-location lvalue tok) stack)))) (define (reduce state stack) ((vector-ref ___rtable state) stack ___gtable push)) (define (shift state symbol stack) (cons state (cons symbol stack))) (define (get-actions token action-list) (let ((pair (assoc token action-list))) (if pair (cdr pair) (cdar action-list)))) ;; get the default action (define (run) (let loop-tokens () (consume) (let ((symbol (token-category *input*))) (for-all-processes (lambda (process) (let loop ((stacks (list process)) (active-stacks '())) (cond ((pair? stacks) (let* ((stack (car stacks)) (state (car stack))) (let actions-loop ((actions (get-actions symbol (vector-ref ___atable state))) (active-stacks active-stacks)) (if (pair? actions) (let ((action (car actions)) (other-actions (cdr actions))) (cond ((eq? action '*error*) (actions-loop other-actions active-stacks)) ((eq? action 'accept) (add-parse (car (take-right stack 2))) (actions-loop other-actions active-stacks)) ((>= action 0) (let ((new-stack (shift action *input* stack))) (add-process new-stack)) (actions-loop other-actions active-stacks)) (else (let ((new-stack (reduce (- action) stack))) (actions-loop other-actions (cons new-stack active-stacks)))))) (loop (cdr stacks) active-stacks))))) ((pair? active-stacks) (loop (reverse active-stacks) '()))))))) (if (pair? (get-processes)) (loop-tokens)))) (lambda (lexerp errorp) (set! ___errorp errorp) (initialize-lexer lexerp) (initialize-processes) (initialize-parses) (add-process '(0)) (run) (get-parses))) (define (drop l n) (cond ((and (> n 0) (pair? l)) (drop (cdr l) (- n 1))) (else l))) (define (take-right l n) (drop l (- (length l) n)))PK~2]9"" message.scmnu[;;; User interface messages ;; Copyright (C) 2009, 2010, 2011, 2012 Free Software Foundation, Inc. ;;; This library is free software; you can redistribute it and/or ;;; modify it under the terms of the GNU Lesser General Public ;;; License as published by the Free Software Foundation; either ;;; version 3 of the License, or (at your option) any later version. ;;; ;;; This library is distributed in the hope that it will be useful, ;;; but WITHOUT ANY WARRANTY; without even the implied warranty of ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU ;;; Lesser General Public License for more details. ;;; ;;; You should have received a copy of the GNU Lesser General Public ;;; License along with this library; if not, write to the Free Software ;;; Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA ;;; Commentary: ;;; ;;; This module provide a simple interface to send messages to the user. ;;; TODO: Internationalize messages. ;;; ;;; Code: (define-module (system base message) #:use-module (srfi srfi-1) #:use-module (srfi srfi-9) #:use-module (ice-9 match) #:export (*current-warning-port* *current-warning-prefix* warning warning-type? warning-type-name warning-type-description warning-type-printer lookup-warning-type %warning-types)) ;;; ;;; Source location ;;; (define (location-string loc) (if (pair? loc) (format #f "~a:~a:~a" (or (assoc-ref loc 'filename) "") (1+ (assoc-ref loc 'line)) (assoc-ref loc 'column)) "")) ;;; ;;; Warnings ;;; ;; This name existed before %current-warning-port was introduced, but ;; otherwise it is a deprecated binding. (define *current-warning-port* ;; Can't play the identifier-syntax deprecation game in Guile 2.0, as ;; other modules might depend on this being a normal binding and not a ;; syntax binding. (parameter-fluid current-warning-port)) (define *current-warning-prefix* ;; Prefix string when emitting a warning. (make-fluid ";;; ")) (define-record-type (make-warning-type name description printer) warning-type? (name warning-type-name) (description warning-type-description) (printer warning-type-printer)) (define %warning-types ;; List of known warning types. (map (lambda (args) (apply make-warning-type args)) (let-syntax ((emit (lambda (s) (syntax-case s () ((_ port fmt args ...) (string? (syntax->datum #'fmt)) (with-syntax ((fmt (string-append "~a" (syntax->datum #'fmt)))) #'(format port fmt (fluid-ref *current-warning-prefix*) args ...))))))) `((unsupported-warning ;; a "meta warning" "warn about unknown warning types" ,(lambda (port unused name) (emit port "warning: unknown warning type `~A'~%" name))) (unused-variable "report unused variables" ,(lambda (port loc name) (emit port "~A: warning: unused variable `~A'~%" loc name))) (unused-toplevel "report unused local top-level variables" ,(lambda (port loc name) (emit port "~A: warning: possibly unused local top-level variable `~A'~%" loc name))) (unbound-variable "report possibly unbound variables" ,(lambda (port loc name) (emit port "~A: warning: possibly unbound variable `~A'~%" loc name))) (arity-mismatch "report procedure arity mismatches (wrong number of arguments)" ,(lambda (port loc name certain?) (if certain? (emit port "~A: warning: wrong number of arguments to `~A'~%" loc name) (emit port "~A: warning: possibly wrong number of arguments to `~A'~%" loc name)))) (duplicate-case-datum "report a duplicate datum in a case expression" ,(lambda (port loc datum clause case-expr) (emit port "~A: warning: duplicate datum ~S in clause ~S of case expression ~S~%" loc datum clause case-expr))) (bad-case-datum "report a case datum that cannot be meaningfully compared using `eqv?'" ,(lambda (port loc datum clause case-expr) (emit port "~A: warning: datum ~S cannot be meaningfully compared using `eqv?' in clause ~S of case expression ~S~%" loc datum clause case-expr))) (format "report wrong number of arguments to `format'" ,(lambda (port loc . rest) (define (escape-newlines str) (list->string (string-fold-right (lambda (c r) (if (eq? c #\newline) (append '(#\\ #\n) r) (cons c r))) '() str))) (define (range min max) (cond ((eq? min 'any) (if (eq? max 'any) "any number" ;; can't happen (emit #f "up to ~a" max))) ((eq? max 'any) (emit #f "at least ~a" min)) ((= min max) (number->string min)) (else (emit #f "~a to ~a" min max)))) (match rest (('simple-format fmt opt) (emit port "~A: warning: ~S: unsupported format option ~~~A, use (ice-9 format) instead~%" loc (escape-newlines fmt) opt)) (('wrong-format-arg-count fmt min max actual) (emit port "~A: warning: ~S: wrong number of `format' arguments: expected ~A, got ~A~%" loc (escape-newlines fmt) (range min max) actual)) (('syntax-error 'unterminated-iteration fmt) (emit port "~A: warning: ~S: unterminated iteration~%" loc (escape-newlines fmt))) (('syntax-error 'unterminated-conditional fmt) (emit port "~A: warning: ~S: unterminated conditional~%" loc (escape-newlines fmt))) (('syntax-error 'unexpected-semicolon fmt) (emit port "~A: warning: ~S: unexpected `~~;'~%" loc (escape-newlines fmt))) (('syntax-error 'unexpected-conditional-termination fmt) (emit port "~A: warning: ~S: unexpected `~~]'~%" loc (escape-newlines fmt))) (('wrong-port wrong-port) (emit port "~A: warning: ~S: wrong port argument~%" loc wrong-port)) (('wrong-format-string fmt) (emit port "~A: warning: ~S: wrong format string~%" loc fmt)) (('non-literal-format-string) (emit port "~A: warning: non-literal format string~%" loc)) (('wrong-num-args count) (emit port "~A: warning: wrong number of arguments to `format'~%" loc)) (else (emit port "~A: `format' warning~%" loc))))))))) (define (lookup-warning-type name) "Return the warning type NAME or `#f' if not found." (find (lambda (wt) (eq? name (warning-type-name wt))) %warning-types)) (define (warning type location . args) "Emit a warning of type TYPE for source location LOCATION (a source property alist) using the data in ARGS." (let ((wt (lookup-warning-type type)) (port (current-warning-port))) (if (warning-type? wt) (apply (warning-type-printer wt) port (location-string location) args) (format port "~A: unknown warning type `~A': ~A~%" (location-string location) type args)))) ;;; message.scm ends here PK~2]6Rlalr.scmnu[;;; -*- mode: scheme; coding: utf-8; -*- ;;; ;;; Copyright (C) 2010 Free Software Foundation, Inc. ;;; ;;; This library is free software; you can redistribute it and/or modify it ;;; under the terms of the GNU Lesser General Public License as published by ;;; the Free Software Foundation; either version 3 of the License, or (at ;;; your option) any later version. ;;; ;;; This library is distributed in the hope that it will be useful, but ;;; WITHOUT ANY WARRANTY; without even the implied warranty of ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser ;;; General Public License for more details. ;;; ;;; You should have received a copy of the GNU Lesser General Public License ;;; along with this program. If not, see . (define-module (system base lalr) ;; XXX: In theory this import is not needed but the evaluator (not the ;; compiler) complains about `lexical-token' being unbound when expanding ;; `(define-record-type lexical-token ...)' if we omit it. #:use-module (srfi srfi-9) #:export (lalr-parser print-states make-lexical-token lexical-token? lexical-token-category lexical-token-source lexical-token-value make-source-location source-location? source-location-input source-location-line source-location-column source-location-offset source-location-length source-location->source-properties ;; `lalr-parser' is a defmacro, which produces code that refers to ;; these drivers. lr-driver glr-driver)) ;; The LALR parser generator was written by Dominique Boucher. It's available ;; from http://code.google.com/p/lalr-scm/ and released under the LGPLv3+. (include-from-path "system/base/lalr.upstream.scm") (define (source-location->source-properties loc) `((filename . ,(source-location-input loc)) (line . ,(source-location-line loc)) (column . ,(source-location-column loc)))) PK~2]yJ88 syntax.scmnu[;;; Guile VM specific syntaxes and utilities ;; Copyright (C) 2001, 2009, 2016 Free Software Foundation, Inc ;;; This library is free software; you can redistribute it and/or ;;; modify it under the terms of the GNU Lesser General Public ;;; License as published by the Free Software Foundation; either ;;; version 3 of the License, or (at your option) any later version. ;;; ;;; This library is distributed in the hope that it will be useful, ;;; but WITHOUT ANY WARRANTY; without even the implied warranty of ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU ;;; Lesser General Public License for more details. ;;; ;;; You should have received a copy of the GNU Lesser General Public ;;; License along with this library; if not, write to the Free Software ;;; Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA ;;; Code: (define-module (system base syntax) #:export (%compute-initargs) #:export-syntax (define-type define-record define-record/keywords record-case transform-record)) (define (symbol-trim-both sym pred) (string->symbol (string-trim-both (symbol->string sym) pred))) (define (trim-brackets sym) (symbol-trim-both sym (list->char-set '(#\< #\>)))) ;;; ;;; Type ;;; (define-macro (define-type name . rest) (let ((name (if (pair? name) (car name) name)) (opts (if (pair? name) (cdr name) '()))) (let ((printer (kw-arg-ref opts #:printer)) (common-slots (or (kw-arg-ref opts #:common-slots) '()))) `(begin ,@(map (lambda (def) `(define-record ,(if printer `(,(car def) ,printer) (car def)) ,@common-slots ,@(cdr def))) rest) ,@(map (lambda (common-slot i) `(define ,(symbol-append (trim-brackets name) '- common-slot) (make-procedure-with-setter (lambda (x) (struct-ref x ,i)) (lambda (x v) (struct-set! x ,i v))))) common-slots (iota (length common-slots))))))) ;;; ;;; Record ;;; (define-macro (define-record name-form . slots) (let* ((name (if (pair? name-form) (car name-form) name-form)) (printer (and (pair? name-form) (cadr name-form))) (slot-names (map (lambda (slot) (if (pair? slot) (car slot) slot)) slots)) (stem (trim-brackets name))) `(begin (define ,name (make-record-type ,(symbol->string name) ',slot-names ,@(if printer (list printer) '()))) ,(let* ((reqs (let lp ((slots slots)) (if (or (null? slots) (not (symbol? (car slots)))) '() (cons (car slots) (lp (cdr slots)))))) (opts (list-tail slots (length reqs))) (tail (module-gensym "defrec"))) `(define (,(symbol-append 'make- stem) ,@reqs . ,tail) (let ,(map (lambda (o) `(,(car o) (cond ((null? ,tail) ,(cadr o)) (else (let ((_x (car ,tail))) (set! ,tail (cdr ,tail)) _x))))) opts) (make-struct ,name 0 ,@slot-names)))) (define ,(symbol-append stem '?) (record-predicate ,name)) ,@(map (lambda (sname) `(define ,(symbol-append stem '- sname) (make-procedure-with-setter (record-accessor ,name ',sname) (record-modifier ,name ',sname)))) slot-names)))) ;; like the former, but accepting keyword arguments in addition to ;; optional arguments (define-macro (define-record/keywords name-form . slots) (let* ((name (if (pair? name-form) (car name-form) name-form)) (printer (and (pair? name-form) (cadr name-form))) (slot-names (map (lambda (slot) (if (pair? slot) (car slot) slot)) slots)) (stem (trim-brackets name))) `(begin (define ,name (make-record-type ,(symbol->string name) ',slot-names ,@(if printer (list printer) '()))) (define ,(symbol-append 'make- stem) (let ((slots (list ,@(map (lambda (slot) (if (pair? slot) `(cons ',(car slot) ,(cadr slot)) `',slot)) slots))) (constructor (record-constructor ,name))) (lambda args (apply constructor (%compute-initargs args slots))))) (define ,(symbol-append stem '?) (record-predicate ,name)) ,@(map (lambda (sname) `(define ,(symbol-append stem '- sname) (make-procedure-with-setter (record-accessor ,name ',sname) (record-modifier ,name ',sname)))) slot-names)))) (define (%compute-initargs args slots) (define (finish out) (map (lambda (slot) (let ((name (if (pair? slot) (car slot) slot))) (cond ((assq name out) => cdr) ((pair? slot) (cdr slot)) (else (error "unbound slot" args slots name))))) slots)) (let lp ((in args) (positional slots) (out '())) (cond ((null? in) (finish out)) ((keyword? (car in)) (let ((sym (keyword->symbol (car in)))) (cond ((and (not (memq sym slots)) (not (assq sym (filter pair? slots)))) (error "unknown slot" sym)) ((assq sym out) (error "slot already set" sym out)) (else (lp (cddr in) '() (acons sym (cadr in) out)))))) ((null? positional) (error "too many initargs" args slots)) (else (lp (cdr in) (cdr positional) (let ((slot (car positional))) (acons (if (pair? slot) (car slot) slot) (car in) out))))))) ;; So, dear reader. It is pleasant indeed around this fire or at this ;; cafe or in this room, is it not? I think so too. ;; ;; This macro used to generate code that looked like this: ;; ;; `(((record-predicate ,record-type) ,r) ;; (let ,(map (lambda (slot) ;; (if (pair? slot) ;; `(,(car slot) ((record-accessor ,record-type ',(cadr slot)) ,r)) ;; `(,slot ((record-accessor ,record-type ',slot) ,r)))) ;; slots) ;; ,@body))))) ;; ;; But this was a hot spot, so computing all those predicates and ;; accessors all the time was getting expensive, so we did a terrible ;; thing: we decided that since above we're already defining accessors ;; and predicates with computed names, we might as well just rely on that fact here. ;; ;; It's a bit nasty, I agree. But it is fast. ;; ;;scheme@(guile-user)> (with-statprof #:hz 1000 #:full-stacks? #t (resolve-module '(oop goops)))% cumulative self ;; time seconds seconds name ;; 8.82 0.03 0.01 glil->assembly ;; 8.82 0.01 0.01 record-type-fields ;; 5.88 0.01 0.01 %compute-initargs ;; 5.88 0.01 0.01 list-index ;;; So ugly... but I am too ignorant to know how to make it better. (define-syntax record-case (lambda (x) (syntax-case x () ((_ record clause ...) (let ((r (syntax r)) (rtd (syntax rtd))) (define (process-clause tag fields exprs) (let ((infix (trim-brackets (syntax->datum tag)))) (with-syntax ((tag tag) (((f . accessor) ...) (let lp ((fields fields)) (syntax-case fields () (() (syntax ())) (((v0 f0) f1 ...) (acons (syntax v0) (datum->syntax x (symbol-append infix '- (syntax->datum (syntax f0)))) (lp (syntax (f1 ...))))) ((f0 f1 ...) (acons (syntax f0) (datum->syntax x (symbol-append infix '- (syntax->datum (syntax f0)))) (lp (syntax (f1 ...)))))))) ((e0 e1 ...) (syntax-case exprs () (() (syntax (#t))) ((e0 e1 ...) (syntax (e0 e1 ...)))))) (syntax ((eq? rtd tag) (let ((f (accessor r)) ...) e0 e1 ...)))))) (with-syntax ((r r) (rtd rtd) ((processed ...) (let lp ((clauses (syntax (clause ...))) (out '())) (syntax-case clauses (else) (() (reverse! (cons (syntax (else (error "unhandled record" r))) out))) (((else e0 e1 ...)) (reverse! (cons (syntax (else e0 e1 ...)) out))) (((else e0 e1 ...) . rest) (syntax-violation 'record-case "bad else clause placement" (syntax x) (syntax (else e0 e1 ...)))) (((( f0 ...) e0 ...) . rest) (lp (syntax rest) (cons (process-clause (syntax ) (syntax (f0 ...)) (syntax (e0 ...))) out))))))) (syntax (let* ((r record) (rtd (struct-vtable r))) (cond processed ...))))))))) ;; Here we take the terrorism to another level. Nasty, but the client ;; code looks good. (define-macro (transform-record type-and-common record . clauses) (let ((r (module-gensym "rec")) (rtd (module-gensym "rtd")) (type-stem (trim-brackets (car type-and-common)))) (define (make-stem s) (symbol-append type-stem '- s)) (define (further-predicates x record-stem slots) (define (access slot) `(,(symbol-append (make-stem record-stem) '- slot) ,x)) (let lp ((in slots) (out '())) (cond ((null? in) out) ((pair? (car in)) (let ((slot (caar in)) (arg (cadar in))) (cond ((symbol? arg) (lp (cdr in) out)) ((pair? arg) (lp (cdr in) (append (further-predicates (access slot) (car arg) (cdr arg)) out))) (else (lp (cdr in) (cons `(eq? ,(access slot) ',arg) out)))))) (else (lp (cdr in) out))))) (define (let-clauses x record-stem slots) (define (access slot) `(,(symbol-append (make-stem record-stem) '- slot) ,x)) (let lp ((in slots) (out '())) (cond ((null? in) out) ((pair? (car in)) (let ((slot (caar in)) (arg (cadar in))) (cond ((symbol? arg) (lp (cdr in) (cons `(,arg ,(access slot)) out))) ((pair? arg) (lp (cdr in) (append (let-clauses (access slot) (car arg) (cdr arg)) out))) (else (lp (cdr in) out))))) (else (lp (cdr in) (cons `(,(car in) ,(access (car in))) out)))))) (define (transform-expr x) (cond ((not (pair? x)) x) ((eq? (car x) '->) (if (= (length x) 2) (let ((form (cadr x))) `(,(symbol-append 'make- (make-stem (car form))) ,@(cdr type-and-common) ,@(map (lambda (y) (if (and (pair? y) (eq? (car y) 'unquote)) (transform-expr (cadr y)) y)) (cdr form)))) (error "bad -> form" x))) (else (cons (car x) (map transform-expr (cdr x)))))) (define (process-clause clause) (if (eq? (car clause) 'else) clause (let ((stem (caar clause)) (slots (cdar clause)) (body (cdr clause))) (let ((record-type (symbol-append '< (make-stem stem) '>))) `((and (eq? ,rtd ,record-type) ,@(reverse (further-predicates r stem slots))) (let ,(reverse (let-clauses r stem slots)) ,@(if (pair? body) (map transform-expr body) '((if #f #f))))))))) `(let* ((,r ,record) (,rtd (struct-vtable ,r)) ,@(map (lambda (slot) `(,slot (,(make-stem slot) ,r))) (cdr type-and-common))) (cond ,@(let ((clauses (map process-clause clauses))) (if (assq 'else clauses) clauses (append clauses `((else (error "unhandled record" ,r)))))))))) PK~2]1'' compile.scmnu[;;; High-level compiler interface ;; Copyright (C) 2001, 2009, 2010, 2011, 2012, 2013 Free Software Foundation, Inc. ;;; This library is free software; you can redistribute it and/or ;;; modify it under the terms of the GNU Lesser General Public ;;; License as published by the Free Software Foundation; either ;;; version 3 of the License, or (at your option) any later version. ;;; ;;; This library is distributed in the hope that it will be useful, ;;; but WITHOUT ANY WARRANTY; without even the implied warranty of ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU ;;; Lesser General Public License for more details. ;;; ;;; You should have received a copy of the GNU Lesser General Public ;;; License along with this library; if not, write to the Free Software ;;; Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA ;;; Code: (define-module (system base compile) #:use-module (system base syntax) #:use-module (system base language) #:use-module (system base message) #:use-module (system vm vm) ;; FIXME: there's a reason for this, can't remember why tho #:use-module (ice-9 regex) #:use-module (ice-9 optargs) #:use-module (ice-9 receive) #:export (compiled-file-name compile-file compile-and-load read-and-compile compile decompile)) ;;; ;;; Compiler ;;; (define (call-once thunk) (let ((entered #f)) (dynamic-wind (lambda () (if entered (error "thunk may only be entered once: ~a" thunk)) (set! entered #t)) thunk (lambda () #t)))) ;; emacs: (put 'call-with-output-file/atomic 'scheme-indent-function 1) (define* (call-with-output-file/atomic filename proc #:optional reference) (let* ((template (string-append filename ".XXXXXX")) (tmp (mkstemp! template "wb"))) (call-once (lambda () (with-throw-handler #t (lambda () (proc tmp) ;; Chmodding by name instead of by port allows this chmod to ;; work on systems without fchmod, like MinGW. (let ((perms (or (false-if-exception (stat:perms (stat reference))) (lognot (umask))))) (chmod template (logand #o0666 perms))) (close-port tmp) (rename-file template filename)) (lambda args (close-port tmp) (delete-file template))))))) (define (ensure-language x) (if (language? x) x (lookup-language x))) ;; Throws an exception if `dir' is not writable. The mkdir occurs ;; before the check, so that we avoid races (possibly due to parallel ;; compilation). ;; (define (ensure-directory dir) (catch 'system-error (lambda () (mkdir dir)) (lambda (k subr fmt args rest) (let ((errno (and (pair? rest) (car rest)))) (cond ((eqv? errno EEXIST) ;; Assume it's a writable directory, to avoid TOCTOU errors, ;; as well as UID/EUID mismatches that occur with access(2). #t) ((eqv? errno ENOENT) (ensure-directory (dirname dir)) (ensure-directory dir)) (else (throw k subr fmt args rest))))))) ;;; This function is among the trickiest I've ever written. I tried many ;;; variants. In the end, simple is best, of course. ;;; ;;; After turning this around a number of times, it seems that the ;;; desired behavior is that .go files should exist in a path, for ;;; searching. That is orthogonal to this function. For writing .go ;;; files, either you know where they should go, in which case you tell ;;; compile-file explicitly, as in the srcdir != builddir case; or you ;;; don't know, in which case this function is called, and we just put ;;; them in your own ccache dir in ~/.cache/guile/ccache. ;;; ;;; See also boot-9.scm:load. (define (compiled-file-name file) ;; FIXME: would probably be better just to append SHA1(canon-path) ;; to the %compile-fallback-path, to avoid deep directory stats. (define (canonical->suffix canon) (cond ((string-prefix? "/" canon) canon) ((and (> (string-length canon) 2) (eqv? (string-ref canon 1) #\:)) ;; Paths like C:... transform to /C... (string-append "/" (substring canon 0 1) (substring canon 2))) (else canon))) (define (compiled-extension) (cond ((or (null? %load-compiled-extensions) (string-null? (car %load-compiled-extensions))) (warn "invalid %load-compiled-extensions" %load-compiled-extensions) ".go") (else (car %load-compiled-extensions)))) (and %compile-fallback-path (let ((f (string-append %compile-fallback-path (canonical->suffix (canonicalize-path file)) (compiled-extension)))) (and (false-if-exception (ensure-directory (dirname f))) f)))) (define* (compile-file file #:key (output-file #f) (from (current-language)) (to 'objcode) (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" 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) ((language-printer (ensure-language to)) (read-and-compile in #:env env #:from from #:to to #:opts opts) port)) file) comp))) (define* (compile-and-load file #:key (from (current-language)) (to 'value) (env (current-module)) (opts '()) (canonicalization 'relative)) (with-fluids ((%file-port-name-canonicalization canonicalization)) (read-and-compile (open-input-file file) #:from from #:to to #:opts opts #:env env))) ;;; ;;; Compiler interface ;;; (define (compile-passes from to opts) (map cdr (or (lookup-compilation-order from to) (error "no way to compile" from "to" to)))) (define (compile-fold passes exp env opts) (let lp ((passes passes) (x exp) (e env) (cenv env) (first? #t)) (if (null? passes) (values x e cenv) (receive (x e new-cenv) ((car passes) x e opts) (lp (cdr passes) x e (if first? new-cenv cenv) #f))))) (define (find-language-joint from to) (let lp ((in (reverse (or (lookup-compilation-order from to) (error "no way to compile" from "to" to)))) (lang to)) (cond ((null? in) to) ((language-joiner lang) lang) (else (lp (cdr in) (caar in)))))) (define (default-language-joiner lang) (lambda (exps env) (if (and (pair? exps) (null? (cdr exps))) (car exps) (error "Multiple expressions read and compiled, but language has no joiner" lang)))) (define (read-and-parse lang port cenv) (let ((exp ((language-reader lang) port cenv))) (cond ((eof-object? exp) exp) ((language-parser lang) => (lambda (parse) (parse exp))) (else exp)))) (define* (read-and-compile port #:key (from (current-language)) (to 'objcode) (env (default-environment from)) (opts '())) (let ((from (ensure-language from)) (to (ensure-language to))) (let ((joint (find-language-joint from to))) (parameterize ((current-language from)) (let lp ((exps '()) (env #f) (cenv env)) (let ((x (read-and-parse (current-language) port cenv))) (cond ((eof-object? x) (close-port port) (compile ((or (language-joiner joint) (default-language-joiner joint)) (reverse exps) env) #:from joint #:to to ;; env can be false if no expressions were read. #:env (or env (default-environment joint)) #:opts opts)) (else ;; compile-fold instead of compile so we get the env too (receive (jexp jenv jcenv) (compile-fold (compile-passes (current-language) joint opts) x cenv opts) (lp (cons jexp exps) jenv jcenv)))))))))) (define* (compile x #:key (from (current-language)) (to 'value) (env (default-environment from)) (opts '())) (let ((warnings (memq #:warnings opts))) (if (pair? warnings) (let ((warnings (cadr warnings))) ;; Sanity-check the requested warnings. (for-each (lambda (w) (or (lookup-warning-type w) (warning 'unsupported-warning #f w))) warnings)))) (receive (exp env cenv) (compile-fold (compile-passes from to opts) x env opts) exp)) ;;; ;;; Decompiler interface ;;; (define (decompile-passes from to opts) (map cdr (or (lookup-decompilation-order from to) (error "no way to decompile" from "to" to)))) (define (decompile-fold passes exp env opts) (if (null? passes) (values exp env) (receive (exp env) ((car passes) exp env opts) (decompile-fold (cdr passes) exp env opts)))) (define* (decompile x #:key (env #f) (from 'value) (to 'assembly) (opts '())) (decompile-fold (decompile-passes from to opts) x env opts)) PK~2]]߯MM types.scmnu[;;; 'SCM' type tag decoding. ;;; Copyright (C) 2014, 2015 Free Software Foundation, Inc. ;;; ;;; This library is free software; you can redistribute it and/or modify it ;;; under the terms of the GNU Lesser General Public License as published by ;;; the Free Software Foundation; either version 3 of the License, or (at ;;; your option) any later version. ;;; ;;; This library is distributed in the hope that it will be useful, but ;;; WITHOUT ANY WARRANTY; without even the implied warranty of ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser ;;; General Public License for more details. ;;; ;;; You should have received a copy of the GNU Lesser General Public License ;;; along with this program. If not, see . (define-module (system base types) #:use-module (rnrs bytevectors) #:use-module (rnrs io ports) #:use-module (srfi srfi-1) #:use-module (srfi srfi-9) #:use-module (srfi srfi-9 gnu) #:use-module (srfi srfi-11) #:use-module (srfi srfi-26) #:use-module (srfi srfi-60) #:use-module (ice-9 match) #:use-module (ice-9 iconv) #:use-module (ice-9 format) #:use-module (ice-9 vlist) #:use-module (system foreign) #:export (%word-size memory-backend memory-backend? %ffi-memory-backend dereference-word memory-port type-number->name inferior-object? inferior-object-kind inferior-object-sub-kind inferior-object-address inferior-fluid? inferior-fluid-number inferior-struct? inferior-struct-name inferior-struct-fields scm->object)) ;;; Commentary: ;;; ;;; 'SCM' type tag decoding, primarily to support Guile debugging in GDB. ;;; ;;; Code: ;;; ;;; Memory back-ends. ;;; (define %word-size ;; The pointer size. (sizeof '*)) (define-record-type (memory-backend peek open type-name) memory-backend? (peek memory-backend-peek) (open memory-backend-open) (type-name memory-backend-type-name)) ; for SMOBs and ports (define %ffi-memory-backend ;; The FFI back-end to access the current process's memory. The main ;; purpose of this back-end is to allow testing. (let () (define (dereference-word address) (let* ((ptr (make-pointer address)) (bv (pointer->bytevector ptr %word-size))) (bytevector-uint-ref bv 0 (native-endianness) %word-size))) (define (open address size) (define current-address address) (define (read-memory! bv index count) (let* ((ptr (make-pointer current-address)) (mem (pointer->bytevector ptr count))) (bytevector-copy! mem 0 bv index count) (set! current-address (+ current-address count)) count)) (if size (let* ((ptr (make-pointer address)) (bv (pointer->bytevector ptr size))) (open-bytevector-input-port bv)) (let ((port (make-custom-binary-input-port "ffi-memory" read-memory! #f #f #f))) (setvbuf port _IONBF) port))) (memory-backend dereference-word open #f))) (define-inlinable (dereference-word backend address) "Return the word at ADDRESS, using BACKEND." (let ((peek (memory-backend-peek backend))) (peek address))) (define-syntax memory-port (syntax-rules () "Return an input port to the SIZE bytes at ADDRESS, using BACKEND. When SIZE is omitted, return an unbounded port to the memory at ADDRESS." ((_ backend address) (let ((open (memory-backend-open backend))) (open address #f))) ((_ backend address size) (if (zero? size) ;; GDB's 'open-memory' raises an error when size ;; is zero, so we must handle that case specially. (open-bytevector-input-port '#vu8()) (let ((open (memory-backend-open backend))) (open address size)))))) (define (get-word port) "Read a word from PORT and return it as an integer." (let ((bv (get-bytevector-n port %word-size))) (bytevector-uint-ref bv 0 (native-endianness) %word-size))) (define-inlinable (type-number->name backend kind number) "Return the name of the type NUMBER of KIND, where KIND is one of 'smob or 'port, or #f if the information is unavailable." (let ((proc (memory-backend-type-name backend))) (and proc (proc kind number)))) ;;; ;;; Matching bit patterns and cells. ;;; (define-syntax match-cell-words (syntax-rules (bytevector) ((_ port ((bytevector name len) rest ...) body) (let ((name (get-bytevector-n port len)) (remainder (modulo len %word-size))) (unless (zero? remainder) (get-bytevector-n port (- %word-size remainder))) (match-cell-words port (rest ...) body))) ((_ port (name rest ...) body) (let ((name (get-word port))) (match-cell-words port (rest ...) body))) ((_ port () body) body))) (define-syntax match-bit-pattern (syntax-rules (& || = _) ((match-bit-pattern bits ((a || b) & n = c) consequent alternate) (let ((tag (logand bits n))) (if (= tag c) (let ((b tag) (a (logand bits (bitwise-not n)))) consequent) alternate))) ((match-bit-pattern bits (x & n = c) consequent alternate) (let ((tag (logand bits n))) (if (= tag c) (let ((x bits)) consequent) alternate))) ((match-bit-pattern bits (_ & n = c) consequent alternate) (let ((tag (logand bits n))) (if (= tag c) consequent alternate))) ((match-bit-pattern bits ((a << n) || c) consequent alternate) (let ((tag (bitwise-and bits (- (expt 2 n) 1)))) (if (= tag c) (let ((a (arithmetic-shift bits (- n)))) consequent) alternate))))) (define-syntax match-cell-clauses (syntax-rules () ((_ port tag (((tag-pattern thing ...) body) rest ...)) (match-bit-pattern tag tag-pattern (match-cell-words port (thing ...) body) (match-cell-clauses port tag (rest ...)))) ((_ port tag ()) (inferior-object 'unmatched-tag tag)))) (define-syntax match-cell (syntax-rules () "Match a cell---i.e., a non-immediate value other than a pair. The cell's contents are read from PORT." ((_ port (pattern body ...) ...) (let ((port* port) (tag (get-word port))) (match-cell-clauses port* tag ((pattern (begin body ...)) ...)))))) (define-syntax match-scm-clauses (syntax-rules () ((_ bits (bit-pattern body ...) rest ...) (match-bit-pattern bits bit-pattern (begin body ...) (match-scm-clauses bits rest ...))) ((_ bits) 'unmatched-scm))) (define-syntax match-scm (syntax-rules () "Match BITS, an integer representation of an 'SCM' value, against CLAUSES. Each clause must have the form: (PATTERN BODY ...) PATTERN is a bit pattern that may specify bitwise operations on BITS to determine if it matches. TEMPLATE specify the name of the variable to bind the matching bits, possibly with bitwise operations to extract it from BITS." ((_ bits clauses ...) (let ((bits* bits)) (match-scm-clauses bits* clauses ...))))) ;;; ;;; Tags---keep in sync with libguile/tags.h! ;;; ;; Immediate values. (define %tc2-int 2) (define %tc3-imm24 4) (define %tc3-cons 0) (define %tc3-int1 %tc2-int) (define %tc3-int2 (+ %tc2-int 4)) (define %tc8-char (+ 8 %tc3-imm24)) (define %tc8-flag (+ %tc3-imm24 0)) ;; Cell types. (define %tc3-struct 1) (define %tc7-symbol 5) (define %tc7-variable 7) (define %tc7-vector 13) (define %tc7-wvect 15) (define %tc7-string 21) (define %tc7-number 23) (define %tc7-hashtable 29) (define %tc7-pointer 31) (define %tc7-fluid 37) (define %tc7-stringbuf 39) (define %tc7-dynamic-state 45) (define %tc7-frame 47) (define %tc7-objcode 53) (define %tc7-vm 55) (define %tc7-vm-continuation 71) (define %tc7-bytevector 77) (define %tc7-program 79) (define %tc7-array 85) (define %tc7-bitvector 87) (define %tc7-port 125) (define %tc7-smob 127) (define %tc16-bignum (+ %tc7-number (* 1 256))) (define %tc16-real (+ %tc7-number (* 2 256))) (define %tc16-complex (+ %tc7-number (* 3 256))) (define %tc16-fraction (+ %tc7-number (* 4 256))) ;; "Stringbufs". (define-record-type (stringbuf string) stringbuf? (string stringbuf-contents)) (set-record-type-printer! (lambda (stringbuf port) (display "#" port))) ;; Structs. (define-record-type (inferior-struct name fields) inferior-struct? (name inferior-struct-name) (fields inferior-struct-fields set-inferior-struct-fields!)) (define print-inferior-struct (let ((%printed-struct (make-parameter vlist-null))) (lambda (struct port) (if (vhash-assq struct (%printed-struct)) (format port "#-1#") (begin (format port "#" (object-address struct))))))) (set-record-type-printer! print-inferior-struct) ;; Fluids. (define-record-type (inferior-fluid number value) inferior-fluid? (number inferior-fluid-number) (value inferior-fluid-value)) (set-record-type-printer! (lambda (fluid port) (match fluid (($ number) (format port "#" number (object-address fluid)))))) ;; Object type to represent complex objects from the inferior process that ;; cannot be really converted to usable Scheme objects in the current ;; process. (define-record-type (%inferior-object kind sub-kind address) inferior-object? (kind inferior-object-kind) (sub-kind inferior-object-sub-kind) (address inferior-object-address)) (define inferior-object (case-lambda "Return an object representing an inferior object at ADDRESS, of type KIND/SUB-KIND." ((kind address) (%inferior-object kind #f address)) ((kind sub-kind address) (%inferior-object kind sub-kind address)))) (set-record-type-printer! (lambda (io port) (match io (($ kind sub-kind address) (format port "#<~a ~:[~*~;~a ~]~x>" kind sub-kind sub-kind address))))) (define (inferior-smob backend type-number address) "Return an object representing the SMOB at ADDRESS whose type is TYPE-NUMBER." (inferior-object 'smob (or (type-number->name backend 'smob type-number) type-number) address)) (define (inferior-port backend type-number address) "Return an object representing the port at ADDRESS whose type is TYPE-NUMBER." (inferior-object 'port (or (type-number->name backend 'port type-number) type-number) address)) (define %visited-cells ;; Vhash of mapping addresses of already visited cells to the ;; corresponding inferior object. This is used to detect and represent ;; cycles. (make-parameter vlist-null)) (define-syntax visited (syntax-rules (->) ((_ (address -> object) body ...) (parameterize ((%visited-cells (vhash-consv address object (%visited-cells)))) body ...)))) (define (address->inferior-struct address vtable-data-address backend) "Read the struct at ADDRESS using BACKEND. Return an 'inferior-struct' object representing it." (define %vtable-layout-index 0) (define %vtable-name-index 5) (let* ((layout-address (+ vtable-data-address (* %vtable-layout-index %word-size))) (layout-bits (dereference-word backend layout-address)) (layout (scm->object layout-bits backend)) (name-address (+ vtable-data-address (* %vtable-name-index %word-size))) (name-bits (dereference-word backend name-address)) (name (scm->object name-bits backend))) (if (symbol? layout) (let* ((layout (symbol->string layout)) (len (/ (string-length layout) 2)) (slots (dereference-word backend (+ address %word-size))) (port (memory-port backend slots (* len %word-size))) (fields (get-bytevector-n port (* len %word-size))) (result (inferior-struct name #f))) ;; Keep track of RESULT so callees can refer to it if we are ;; decoding a circular struct. (visited (address -> result) (let ((values (map (cut scm->object <> backend) (bytevector->uint-list fields (native-endianness) %word-size)))) (set-inferior-struct-fields! result values) result))) (inferior-object 'invalid-struct address)))) (define* (cell->object address #:optional (backend %ffi-memory-backend)) "Return an object representing the object at ADDRESS, reading from memory using BACKEND." (or (and=> (vhash-assv address (%visited-cells)) cdr) ; circular object (let ((port (memory-port backend address))) (match-cell port (((vtable-data-address & 7 = %tc3-struct)) (address->inferior-struct address (- vtable-data-address %tc3-struct) backend)) (((_ & #x7f = %tc7-symbol) buf hash props) (match (cell->object buf backend) (($ string) (string->symbol string)))) (((_ & #x7f = %tc7-variable) obj) (inferior-object 'variable address)) (((_ & #x7f = %tc7-string) buf start len) (match (cell->object buf backend) (($ string) (substring string start (+ start len))))) (((_ & #x047f = %tc7-stringbuf) len (bytevector buf len)) (stringbuf (bytevector->string buf "ISO-8859-1"))) (((_ & #x047f = (bitwise-ior #x400 %tc7-stringbuf)) len (bytevector buf (* 4 len))) (stringbuf (bytevector->string buf (match (native-endianness) ('little "UTF-32LE") ('big "UTF-32BE"))))) (((_ & #x7f = %tc7-bytevector) len address) (let ((bv-port (memory-port backend address len))) (get-bytevector-n bv-port len))) ((((len << 7) || %tc7-vector) weakv-data) (let* ((len (arithmetic-shift len -1)) (words (get-bytevector-n port (* len %word-size))) (vector (make-vector len))) (visited (address -> vector) (fold (lambda (element index) (vector-set! vector index element) (+ 1 index)) 0 (map (cut scm->object <> backend) (bytevector->uint-list words (native-endianness) %word-size))) vector))) (((_ & #x7f = %tc7-wvect)) (inferior-object 'weak-vector address)) ; TODO: show elements ((((n << 8) || %tc7-fluid) init-value) (inferior-fluid n #f)) ; TODO: show current value (((_ & #x7f = %tc7-dynamic-state)) (inferior-object 'dynamic-state address)) ((((flags+type << 8) || %tc7-port)) (inferior-port backend (logand flags+type #xff) address)) (((_ & #x7f = %tc7-program)) (inferior-object 'program address)) (((_ & #xffff = %tc16-bignum)) (inferior-object 'bignum address)) (((_ & #xffff = %tc16-real) pad) (let* ((address (+ address (* 2 %word-size))) (port (memory-port backend address (sizeof double))) (words (get-bytevector-n port (sizeof double)))) (bytevector-ieee-double-ref words 0 (native-endianness)))) (((_ & #x7f = %tc7-number) mpi) (inferior-object 'number address)) (((_ & #x7f = %tc7-hashtable) buckets meta-data unused) (inferior-object 'hash-table address)) (((_ & #x7f = %tc7-pointer) address) (make-pointer address)) (((_ & #x7f = %tc7-objcode)) (inferior-object 'objcode address)) (((_ & #x7f = %tc7-vm)) (inferior-object 'vm address)) (((_ & #x7f = %tc7-vm-continuation)) (inferior-object 'vm-continuation address)) (((_ & #x7f = %tc7-array)) (inferior-object 'array address)) (((_ & #x7f = %tc7-bitvector)) (inferior-object 'bitvector address)) ((((smob-type << 8) || %tc7-smob) word1) (inferior-smob backend smob-type address)))))) (define* (scm->object bits #:optional (backend %ffi-memory-backend)) "Return the Scheme object corresponding to BITS, the bits of an 'SCM' object." (match-scm bits (((integer << 2) || %tc2-int) integer) ((address & 6 = %tc3-cons) (let* ((type (dereference-word backend address)) (pair? (not (bit-set? 0 type)))) (if pair? (or (and=> (vhash-assv address (%visited-cells)) cdr) (let ((car type) (cdrloc (+ address %word-size)) (pair (cons *unspecified* *unspecified*))) (visited (address -> pair) (set-car! pair (scm->object car backend)) (set-cdr! pair (scm->object (dereference-word backend cdrloc) backend)) pair))) (cell->object address backend)))) (((char << 8) || %tc8-char) (integer->char char)) (((flag << 8) || %tc8-flag) (case flag ((0) #f) ((1) #nil) ((3) '()) ((4) #t) ((8) (if #f #f)) ((9) (inferior-object 'undefined bits)) ((10) (eof-object)) ((11) (inferior-object 'unbound bits)))))) ;;; Local Variables: ;;; eval: (put 'match-scm 'scheme-indent-function 1) ;;; eval: (put 'match-cell 'scheme-indent-function 1) ;;; eval: (put 'visited 'scheme-indent-function 1) ;;; End: ;;; types.scm ends here PK~2]kUU target.scmnu[PK~2]3r ck.scmnu[PK~2]z(  ^pmatch.scmnu[PK~2] 'language.scmnu[PK~2]w;6lalr.upstream.scmnu[PK~2]9"" (message.scmnu[PK~2]6RKlalr.scmnu[PK~2]yJ88 FSsyntax.scmnu[PK~2]1'' compile.scmnu[PK~2]]߯MM types.scmnu[PK