2016-10-21 20:44:50 +00:00
|
|
|
#! /bin/sh
|
|
|
|
# -*- scheme -*-
|
|
|
|
exec ${GUILE-guile} --no-auto-compile -L $HOME/src/mes/build-aux -L build-aux -e '(@@ (mes-snarf) main)' -s "$0" ${1+"$@"}
|
|
|
|
!#
|
|
|
|
|
|
|
|
;;; Mes --- Maxwell Equations of Software
|
2017-11-21 18:22:26 +00:00
|
|
|
;;; Copyright © 2016,2017 Jan (janneke) Nieuwenhuizen <janneke@gnu.org>
|
2016-10-21 20:44:50 +00:00
|
|
|
;;;
|
|
|
|
;;; mes-snarf.scm: This file is part of Mes.
|
|
|
|
;;;
|
|
|
|
;;; Mes is free software; you can redistribute it and/or modify it
|
|
|
|
;;; under the terms of the GNU General Public License as published by
|
|
|
|
;;; the Free Software Foundation; either version 3 of the License, or (at
|
|
|
|
;;; your option) any later version.
|
|
|
|
;;;
|
|
|
|
;;; Mes 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 General Public License for more details.
|
|
|
|
;;;
|
|
|
|
;;; You should have received a copy of the GNU General Public License
|
|
|
|
;;; along with Mes. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
|
|
|
(define-module (mes-snarf)
|
|
|
|
#:use-module (srfi srfi-1)
|
|
|
|
#:use-module (srfi srfi-26)
|
|
|
|
#:use-module (ice-9 curried-definitions)
|
|
|
|
#:use-module (ice-9 rdelim)
|
|
|
|
#:use-module (ice-9 regex)
|
|
|
|
#:use-module (oop goops))
|
|
|
|
|
|
|
|
(define ((regexp-replace regexp replace) string)
|
|
|
|
(or (and=> (string-match regexp string)
|
|
|
|
(cut regexp-substitute #f <> 'pre replace 'post))
|
|
|
|
string))
|
|
|
|
|
2017-04-02 15:01:22 +00:00
|
|
|
(define %gcc? #t)
|
2016-10-21 20:44:50 +00:00
|
|
|
;; (define-record-type function (make-function name formals annotation)
|
|
|
|
;; function?
|
|
|
|
;; (name .name)
|
|
|
|
;; (formals .formals)
|
|
|
|
;; (annotation .annotation))
|
|
|
|
|
|
|
|
(define-class <file> ()
|
|
|
|
(name #:accessor .name #:init-keyword #:name)
|
|
|
|
(content #:accessor .content #:init-keyword #:content))
|
|
|
|
|
|
|
|
(define-class <function> ()
|
|
|
|
(name #:accessor .name #:init-keyword #:name)
|
|
|
|
(formals #:accessor .formals #:init-keyword #:formals)
|
|
|
|
(annotation #:accessor .annotation #:init-keyword #:annotation))
|
|
|
|
|
|
|
|
(define (function-scm-name f)
|
|
|
|
(or (assoc-ref (.annotation f) 'name)
|
2016-12-23 10:31:34 +00:00
|
|
|
(let ((name ((compose
|
|
|
|
(regexp-replace "_" "-")
|
|
|
|
(regexp-replace "_" "-")
|
|
|
|
(regexp-replace "_" "-")
|
|
|
|
(regexp-replace "_" "-")
|
|
|
|
(regexp-replace "_to_" "->")
|
|
|
|
(regexp-replace "_x$" "!")
|
2018-04-20 12:38:24 +00:00
|
|
|
(regexp-replace "_x_$" "!-")
|
2017-03-25 14:58:44 +00:00
|
|
|
(regexp-replace "_p$" "?")
|
|
|
|
(regexp-replace "___" "***")
|
|
|
|
(regexp-replace "___" "***"))
|
2016-12-23 10:31:34 +00:00
|
|
|
(.name f))))
|
|
|
|
(if (not (string-suffix? "-" name)) name
|
|
|
|
(string-append "core:" (string-drop-right name 1))))))
|
2016-10-21 20:44:50 +00:00
|
|
|
|
2016-11-21 08:28:34 +00:00
|
|
|
(define %builtin-prefix% "scm_")
|
2016-10-21 20:44:50 +00:00
|
|
|
(define (function-builtin-name f)
|
|
|
|
(string-append %builtin-prefix% (.name f)))
|
|
|
|
|
2016-11-21 08:28:34 +00:00
|
|
|
(define %cell-prefix% "cell_")
|
|
|
|
(define (function-cell-name f)
|
|
|
|
(string-append %cell-prefix% (.name f)))
|
2016-10-21 20:44:50 +00:00
|
|
|
|
2016-11-21 08:28:34 +00:00
|
|
|
(define %start 1)
|
|
|
|
(define (symbol->header s i)
|
2016-12-14 18:02:19 +00:00
|
|
|
(format #f "#define cell_~a ~a\n" s i))
|
2016-11-21 08:28:34 +00:00
|
|
|
|
|
|
|
(define (symbol->source s i)
|
|
|
|
(string-append
|
2017-01-04 07:16:14 +00:00
|
|
|
(format #f "g_free++;\n")
|
2017-03-17 21:45:48 +00:00
|
|
|
(format #f "g_cells[cell_~a] = scm_~a;\n\n" s s)))
|
2016-11-21 08:28:34 +00:00
|
|
|
|
2016-12-23 09:38:41 +00:00
|
|
|
(define (symbol->names s i)
|
2017-04-02 15:01:22 +00:00
|
|
|
(if %gcc?
|
2017-03-17 21:45:48 +00:00
|
|
|
(format #f "g_cells[cell_~a].car = cstring_to_list (scm_~a.name);\n" s s)
|
|
|
|
(format #f "g_cells[cell_~a].car = cstring_to_list (scm_~a.car);\n" s s)))
|
2016-12-23 09:38:41 +00:00
|
|
|
|
2016-11-21 08:28:34 +00:00
|
|
|
(define (function->header f i)
|
2016-11-03 20:28:05 +00:00
|
|
|
(let* ((arity (or (assoc-ref (.annotation f) 'arity)
|
|
|
|
(if (string-null? (.formals f)) 0
|
|
|
|
(length (string-split (.formals f) #\,)))))
|
|
|
|
(n (if (eq? arity 'n) -1 arity)))
|
2016-11-19 21:31:30 +00:00
|
|
|
(string-append
|
|
|
|
(format #f "SCM ~a (~a);\n" (.name f) (.formals f))
|
2017-04-02 15:01:22 +00:00
|
|
|
(if %gcc?
|
core+mini-mes: Replace manual snippets by snarfed includes.
* build-aux/mes-snarf.scm (symbol->source, function->header,
function->source, function->environment): Add workarounds to
avoid struct-copy initializers.
* GNUmakefile (mini-mes): Snarf symbols and functions.
* scaffold/mini-mes.c: Include mini-mes.h, mini-mes.symbols.h,
mini-mes.symbols.i, mini-mes.i, mini-mes.environment.i.
Add snarfable symbol/special definitions.
(type_t): Prefix all types with `T', update users.
(assert_defined, gc_push_frame, gc_peek_frame, gc_init_cells): Mark
as internal.
* mes.c (type_t): Prefix all types with `T', update users.
* scaffold/mini-mes.c (eq_p, type_, car_, cdr_,
list_of_char_equal_p, lookup_macro, write_byte): New functions (from
mes.c).
(assq): Add debugging, workaround.
2017-03-10 19:56:18 +00:00
|
|
|
(format #f "struct function fun_~a = {.function~a=&~a, .arity=~a, .name=~s};\n" (.name f) arity (.name f) n (function-scm-name f))
|
|
|
|
(format #f "struct function fun_~a = {&~a, ~a, ~s};\n" (.name f) (.name f) n (function-scm-name f)))
|
2017-04-02 15:01:22 +00:00
|
|
|
(if %gcc?
|
core+mini-mes: Replace manual snippets by snarfed includes.
* build-aux/mes-snarf.scm (symbol->source, function->header,
function->source, function->environment): Add workarounds to
avoid struct-copy initializers.
* GNUmakefile (mini-mes): Snarf symbols and functions.
* scaffold/mini-mes.c: Include mini-mes.h, mini-mes.symbols.h,
mini-mes.symbols.i, mini-mes.i, mini-mes.environment.i.
Add snarfable symbol/special definitions.
(type_t): Prefix all types with `T', update users.
(assert_defined, gc_push_frame, gc_peek_frame, gc_init_cells): Mark
as internal.
* mes.c (type_t): Prefix all types with `T', update users.
* scaffold/mini-mes.c (eq_p, type_, car_, cdr_,
list_of_char_equal_p, lookup_macro, write_byte): New functions (from
mes.c).
(assq): Add debugging, workaround.
2017-03-10 19:56:18 +00:00
|
|
|
(format #f "struct scm ~a = {TFUNCTION, .name=0, .function=0};\n" (function-builtin-name f))
|
|
|
|
(format #f "struct scm ~a = {TFUNCTION, 0, 0};\n" (function-builtin-name f)))
|
2016-11-19 21:31:30 +00:00
|
|
|
(format #f "SCM cell_~a;\n\n" (.name f)))))
|
|
|
|
|
|
|
|
(define (function->source f i)
|
|
|
|
(string-append
|
2017-04-02 15:01:22 +00:00
|
|
|
(if %gcc?
|
core+mini-mes: Replace manual snippets by snarfed includes.
* build-aux/mes-snarf.scm (symbol->source, function->header,
function->source, function->environment): Add workarounds to
avoid struct-copy initializers.
* GNUmakefile (mini-mes): Snarf symbols and functions.
* scaffold/mini-mes.c: Include mini-mes.h, mini-mes.symbols.h,
mini-mes.symbols.i, mini-mes.i, mini-mes.environment.i.
Add snarfable symbol/special definitions.
(type_t): Prefix all types with `T', update users.
(assert_defined, gc_push_frame, gc_peek_frame, gc_init_cells): Mark
as internal.
* mes.c (type_t): Prefix all types with `T', update users.
* scaffold/mini-mes.c (eq_p, type_, car_, cdr_,
list_of_char_equal_p, lookup_macro, write_byte): New functions (from
mes.c).
(assq): Add debugging, workaround.
2017-03-10 19:56:18 +00:00
|
|
|
(format #f "~a.function = g_function;\n" (function-builtin-name f))
|
|
|
|
(format #f "~a.cdr = g_function;\n" (function-builtin-name f)))
|
2017-03-02 19:26:13 +00:00
|
|
|
(format #f "g_functions[g_function++] = fun_~a;\n" (.name f))
|
2017-01-04 07:16:14 +00:00
|
|
|
(format #f "cell_~a = g_free++;\n" (.name f))
|
2017-03-17 21:45:48 +00:00
|
|
|
(format #f "g_cells[cell_~a] = ~a;\n\n" (.name f) (function-builtin-name f))))
|
2016-11-19 21:31:30 +00:00
|
|
|
|
|
|
|
(define (function->environment f i)
|
|
|
|
(string-append
|
2017-04-02 15:01:22 +00:00
|
|
|
(if %gcc?
|
core+mini-mes: Replace manual snippets by snarfed includes.
* build-aux/mes-snarf.scm (symbol->source, function->header,
function->source, function->environment): Add workarounds to
avoid struct-copy initializers.
* GNUmakefile (mini-mes): Snarf symbols and functions.
* scaffold/mini-mes.c: Include mini-mes.h, mini-mes.symbols.h,
mini-mes.symbols.i, mini-mes.i, mini-mes.environment.i.
Add snarfable symbol/special definitions.
(type_t): Prefix all types with `T', update users.
(assert_defined, gc_push_frame, gc_peek_frame, gc_init_cells): Mark
as internal.
* mes.c (type_t): Prefix all types with `T', update users.
* scaffold/mini-mes.c (eq_p, type_, car_, cdr_,
list_of_char_equal_p, lookup_macro, write_byte): New functions (from
mes.c).
(assq): Add debugging, workaround.
2017-03-10 19:56:18 +00:00
|
|
|
(format #f "scm_~a.string = cstring_to_list (fun_~a.name);\n" (.name f) (.name f))
|
|
|
|
(format #f "scm_~a.car = cstring_to_list (fun_~a.name);\n" (.name f) (.name f)))
|
2017-04-02 15:01:22 +00:00
|
|
|
(if %gcc?
|
core+mini-mes: Replace manual snippets by snarfed includes.
* build-aux/mes-snarf.scm (symbol->source, function->header,
function->source, function->environment): Add workarounds to
avoid struct-copy initializers.
* GNUmakefile (mini-mes): Snarf symbols and functions.
* scaffold/mini-mes.c: Include mini-mes.h, mini-mes.symbols.h,
mini-mes.symbols.i, mini-mes.i, mini-mes.environment.i.
Add snarfable symbol/special definitions.
(type_t): Prefix all types with `T', update users.
(assert_defined, gc_push_frame, gc_peek_frame, gc_init_cells): Mark
as internal.
* mes.c (type_t): Prefix all types with `T', update users.
* scaffold/mini-mes.c (eq_p, type_, car_, cdr_,
list_of_char_equal_p, lookup_macro, write_byte): New functions (from
mes.c).
(assq): Add debugging, workaround.
2017-03-10 19:56:18 +00:00
|
|
|
(format #f "g_cells[cell_~a].string = MAKE_STRING (scm_~a.string);\n" (.name f) (.name f))
|
|
|
|
(format #f "g_cells[cell_~a].car = MAKE_STRING (scm_~a.car);\n" (.name f) (.name f)))
|
2017-04-02 15:01:22 +00:00
|
|
|
(if %gcc?
|
2017-03-25 14:58:44 +00:00
|
|
|
(format #f "a = acons (lookup_symbol_ (scm_~a.string), ~a, a);\n\n" (.name f) (function-cell-name f))
|
|
|
|
(format #f "a = acons (lookup_symbol_ (scm_~a.car), ~a, a);\n\n" (.name f) (function-cell-name f)))))
|
2016-10-21 20:44:50 +00:00
|
|
|
|
|
|
|
(define (snarf-symbols string)
|
core+mini-mes: Replace manual snippets by snarfed includes.
* build-aux/mes-snarf.scm (symbol->source, function->header,
function->source, function->environment): Add workarounds to
avoid struct-copy initializers.
* GNUmakefile (mini-mes): Snarf symbols and functions.
* scaffold/mini-mes.c: Include mini-mes.h, mini-mes.symbols.h,
mini-mes.symbols.i, mini-mes.i, mini-mes.environment.i.
Add snarfable symbol/special definitions.
(type_t): Prefix all types with `T', update users.
(assert_defined, gc_push_frame, gc_peek_frame, gc_init_cells): Mark
as internal.
* mes.c (type_t): Prefix all types with `T', update users.
* scaffold/mini-mes.c (eq_p, type_, car_, cdr_,
list_of_char_equal_p, lookup_macro, write_byte): New functions (from
mes.c).
(assq): Add debugging, workaround.
2017-03-10 19:56:18 +00:00
|
|
|
(let* ((matches (list-matches "\nstruct scm scm_([a-z_0-9]+) = [{](TSPECIAL|TSYMBOL)," string)))
|
2016-10-21 20:44:50 +00:00
|
|
|
(map (cut match:substring <> 1) matches)))
|
|
|
|
|
|
|
|
(define (snarf-functions string)
|
|
|
|
(let* ((matches (list-matches
|
2016-11-21 08:28:34 +00:00
|
|
|
"\nSCM[ \n]?([a-z0-9_]+) [(]((SCM ?[^,)]+|, )*)[)][^\n(]*([^\n]*)"
|
2016-10-21 20:44:50 +00:00
|
|
|
string)))
|
|
|
|
(map (lambda (m)
|
|
|
|
(make <function>
|
|
|
|
#:name (match:substring m 1)
|
|
|
|
#:formals (match:substring m 2)
|
|
|
|
#:annotation (with-input-from-string (match:substring m 4) read)))
|
|
|
|
matches)))
|
|
|
|
|
Move optional type predicates to type.c.
* mes.c (char_p, macro_p, number_p, pair_p, string_p, symbol_p,
vector_p, builtin_p, boolean_p): Move to type.c
* type.c: New file.
* GNUmakefile (mes.o): Depend on type snarf output.
* module/mes/loop-0.mes (cond, map, let, or, and not, evlis-env,
apply-env, eval-expand, uquote, add-unquoters, eval,
expand-macro-env, eval-begin-env, eval-if-env, sexp:define,
env:define, env:macro): Move to mes-0.mes.
* module/mes/mes-0.mes: New file.
* module/mes/type-0.mes: New file.
* scripts/include.mes: If BOOT, also include mes-0.mes. If TYPE0,
also include type-0.mes.
2016-10-22 10:16:19 +00:00
|
|
|
(define (content? f)
|
|
|
|
((compose not string-null? .content) f))
|
|
|
|
|
2016-10-21 20:44:50 +00:00
|
|
|
(define (internal? f)
|
|
|
|
((compose (cut assoc-ref <> 'internal) .annotation) f))
|
|
|
|
|
|
|
|
(define (no-environment? f)
|
|
|
|
((compose (cut assoc-ref <> 'no-environment) .annotation) f))
|
|
|
|
|
|
|
|
(define (generate-includes file-name)
|
|
|
|
(let* ((string (with-input-from-file file-name read-string))
|
|
|
|
(functions (snarf-functions string))
|
|
|
|
(functions (delete-duplicates functions (lambda (a b) (equal? (.name a) (.name b)))))
|
|
|
|
(functions (filter (negate internal?) functions))
|
|
|
|
(symbols (snarf-symbols string))
|
|
|
|
(base-name (basename file-name ".c"))
|
2017-07-02 14:25:14 +00:00
|
|
|
(dir (or (getenv "OUT") (dirname file-name)))
|
2017-04-12 19:27:59 +00:00
|
|
|
(base-name (string-append dir "/" base-name))
|
|
|
|
(base-name (if %gcc? base-name
|
|
|
|
(string-append base-name ".mes")))
|
2016-10-21 20:44:50 +00:00
|
|
|
(header (make <file>
|
2016-11-21 08:28:34 +00:00
|
|
|
#:name (string-append base-name ".h")
|
|
|
|
#:content (string-join (map function->header functions (iota (length functions) (+ %start (length symbols)))) "")))
|
|
|
|
(source (make <file>
|
|
|
|
#:name (string-append base-name ".i")
|
2016-11-19 21:31:30 +00:00
|
|
|
#:content (string-join (map function->source (filter (negate no-environment?) functions) (iota (length functions) (+ (length symbols) %start))) "")))
|
2016-10-21 20:44:50 +00:00
|
|
|
(environment (make <file>
|
|
|
|
#:name (string-append base-name ".environment.i")
|
2016-11-21 08:28:34 +00:00
|
|
|
#:content (string-join (map function->environment (filter (negate no-environment?) functions) (iota (length functions) (+ (length symbols) %start))) "")))
|
|
|
|
(symbols.h (make <file>
|
|
|
|
#:name (string-append base-name ".symbols.h")
|
|
|
|
#:content (string-join (map symbol->header symbols (iota (length symbols) %start)) "")))
|
|
|
|
(symbols.i (make <file>
|
|
|
|
#:name (string-append base-name ".symbols.i")
|
2016-12-23 09:38:41 +00:00
|
|
|
#:content (string-join (map symbol->source symbols (iota (length symbols))) "")))
|
|
|
|
(symbol-names.i (make <file>
|
|
|
|
#:name (string-append base-name ".symbol-names.i")
|
|
|
|
#:content (string-join (map symbol->names symbols (iota (length symbols))) ""))))
|
|
|
|
(list header source environment symbols.h symbols.i symbol-names.i)))
|
2016-10-21 20:44:50 +00:00
|
|
|
|
|
|
|
(define (file-write file)
|
|
|
|
(with-output-to-file (.name file) (lambda () (display (.content file)))))
|
|
|
|
|
|
|
|
(define (main args)
|
2017-04-12 19:27:59 +00:00
|
|
|
(let* ((files (if (not (and (pair? (cdr args)) (equal? (cadr args) "--mes"))) (cdr args)
|
2017-04-02 15:01:22 +00:00
|
|
|
(begin (set! %gcc? #f)
|
|
|
|
(cddr args)))))
|
Move optional type predicates to type.c.
* mes.c (char_p, macro_p, number_p, pair_p, string_p, symbol_p,
vector_p, builtin_p, boolean_p): Move to type.c
* type.c: New file.
* GNUmakefile (mes.o): Depend on type snarf output.
* module/mes/loop-0.mes (cond, map, let, or, and not, evlis-env,
apply-env, eval-expand, uquote, add-unquoters, eval,
expand-macro-env, eval-begin-env, eval-if-env, sexp:define,
env:define, env:macro): Move to mes-0.mes.
* module/mes/mes-0.mes: New file.
* module/mes/type-0.mes: New file.
* scripts/include.mes: If BOOT, also include mes-0.mes. If TYPE0,
also include type-0.mes.
2016-10-22 10:16:19 +00:00
|
|
|
(map file-write (filter content? (append-map generate-includes files)))))
|
2016-10-21 20:44:50 +00:00
|
|
|
|
|
|
|
;;(define string (with-input-from-file "../mes.c" read-string))
|