adding extended documentation to README, updating version and

copyright information.
This commit is contained in:
Alex Shinn 2009-12-28 22:53:20 +09:00
parent ad068bc1f8
commit f141b22cb3
34 changed files with 453 additions and 50 deletions

24
COPYING Normal file
View file

@ -0,0 +1,24 @@
Copyright (c) 2009 Alex Shinn
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions
are met:
1. Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.
3. The name of the author may not be used to endorse or promote products
derived from this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

380
README
View file

@ -15,6 +15,9 @@ macros based on syntactic-closures, string ports and exceptions.
Chibi-Scheme is written in highly portable C and supports multiple
simultaneous VM instances to run.
------------------------------------------------------------------------
INSTALLING
To build, just run "make". This will provide a shared library
"libchibi-scheme", as well as a sample "chibi-scheme" command-line
repl. The "chibi-scheme-static" make target builds an equivalent
@ -24,7 +27,7 @@ You can edit the file config.h for a number of settings, mostly
disabling features to make the executable smaller. You can specify
standard options directly as arguments to make, for example
make CFLAGS=-Os
make CFLAGS=-Os CPPFLAGS=-DSEXP_USE_NO_FEATURES=1
to optimize for size, or
@ -36,46 +39,25 @@ By default Chibi uses a custom, precise, non-moving GC. You can link
against the Boehm conservative GC by editing the config file, or
directly from make with:
make USE_BOEHM=1
make SEXP_USE_BOEHM=1
See the file main.c for an example of using chibi-scheme as a library.
The essential functions to remember are:
------------------------------------------------------------------------
CHIBI-SCHEME LANGUAGE
#include <chibi/eval.h>
The default language is mostly compatible with the R5RS, with all
differences made by design, not through difficulty of implementation.
The following procedures are omitted:
sexp_make_eval_context(NULL, NULL, NULL)
returns a new context with a fresh stack and primitive environment
transcript-on and transcript-off (because they're silly)
rationalize (pending the addition of rational numbers)
sexp_load_standard_env(context, env, version)
loads the init.scm file in primitive environment env
(version should be SEXP_FIVE)
Apart from this, chibi-scheme is case-sensitive, unlike the R5RS.
The default configuration includes fixnums, flonums and bignums
but no exact rationals or complex numbers.
sexp_destroy_context(context)
free a context and all associated memory
sexp_eval(context, expr, env)
evaluates an s-expression in an environment
env can be NULL to use the context's default env
sexp_eval_string(context, str)
reads an s-expression from str and evaluates it
sexp_load(context, file, env)
read and eval all top-level forms from file
sexp_context_env(context)
a macro returning the environment associated with a context
sexp_env_define(context, env, symbol, value)
define a variable in an environment
A minimal module system is provided by default. Currently you can
load the following SRFIs with (import (srfi N)):
0, 1, 2, 6, 8, 9, 11, 16, 22, 23, 26, 27, 33, 46, 62, 69, 98
although 0, 22, 23, 46 and 62 are built into the default environment
so there's no need to import them.
Full continuations are supported, but currently continuations don't
take C code into account. The only higher-order C functions in the
standard environment are LOAD and EVAL.
LOAD is extended to accept an optional environment argument, like
EVAL. You can also LOAD shared libraries in addition to Scheme source
@ -84,23 +66,23 @@ called with the following signature:
sexp_init_library(sexp context, sexp environment)
To define new primitive functions from C, use sexp_define_foreign,
which takes a Scheme environment, a name, a number of arguments the C
function takes (not counting the context argument), and a C function.
SYNTAX-RULES macros are provided by default, with the extensions from
SRFI-46. In addition, low-level hygienic macros are provided with
a syntactic-closures interface, including SC-MACRO-TRANSFORMER,
RSC-MACRO-TRANSFORMER, and ER-MACRO-TRANSFORMER. A good introduction
to syntactic-closures can be found at:
/* sexp_define_foreign(context, env, name, num_args, f) */
http://community.schemewiki.org/?syntactic-closures
sexp add (sexp context, sexp x, sexp y) {
return sexp_fx_add(x, y);
}
IDENTIFIER?, IDENTIFIER->SYMBOL, IDENTIFIER=?, and
MAKE-SYNTACTIC-CLOSURE and STRIP-SYNTACTIC-CLOSURES are provided.
sexp_define_foreign(context, env, "add", 2, add);
SRFI-0's COND-EXPAND is provided, with the feature `chibi'.
You can also define functions with a single optional argument:
STRING-CONCATENATE concatenates a list of strings.
sexp_define_foreign_opt(context, env, "add", 2, add, sexp_make_fixnum(1));
See the SRFI-69 implementation for more detailed examples of this.
------------------------------------------------------------------------
TYPES
You can define new data types with SRFI-9. This is just syntactic
sugar for the following more primitive type constructors:
@ -120,3 +102,305 @@ sugar for the following more primitive type constructors:
(make-setter <setter-name-string> <type-id> <field-index>)
=> <opcode> ; takes 2 args, sets the field located at the index
------------------------------------------------------------------------
MODULE SYSTEM
A configurable module system, in the style of the Scheme48 module
system, is provided by default.
Modules names are hierarchical lists of symbols or numbers. The
definition of the module (foo bar baz) is searched for in the file
foo/bar/baz.module. This file should contain an expression of the
form:
(define-module (foo bar baz)
<module-declarations> ...)
where <module-declarations> can be any of
(export <id> ...) - specify an export list
(import <import-spec> ...) - specify one or more imports
(import-immutable <import-spec> ...) - specify an immutable import
(body <expr> ...) - inline Scheme code
(include <file> ...) - load one or more files
(include-shared <file> ...) - dynamic load a library
<import-spec> can either be a module name or any of
(only <import-spec> <id> ...)
(except <import-spec> <id> ...)
(rename <import-spec> (<from-id> <to-id>) ...)
(prefix <prefix-id> <import-spec>)
The can be composed and perform basic selection and renaming of
individual identifiers from the given module.
Files are loaded relative to the .module file, and are written with
their extension (so you can use whatever suffix you prefer - .scm,
.ss, .sls, etc.).
Shared modules, on the other hand, should be specified _without_ the
extension - the correct suffix will be added portably (e.g. .so for
Unix and .dylib for OS X).
You may also use COND-EXPAND and arbitrary macro expansions in a
module definition to generate <module-declarations>.
------------------------------------------------------------------------
MODULES
The default environment is (scheme) - you almost always want to import
this.
Currently you can load the following SRFIs with (import (srfi N)):
0, 1, 2, 6, 8, 9, 11, 16, 22, 23, 26, 27, 33, 39, 46, 62, 69, 95, 98
although 0, 22, 23, 46 and 62 are built into the default environment
so there's no need to import them.
Included non-standard modules are put in the (chibi) module namespace.
The following additional modules are available:
(chibi net) - networking interface
(chibi filesystem) - local filesystem and file descriptor interface
(chibi process) - processes and signals
(chibi system) - host system and user information
(chibi time) - time and date library
(chibi match) - pattern-matching library
(chibi loop) - extensible loop syntax
(chibi pathname) - pathname manipulation utilities
(chibi uri) - URI parsing and construction utilities
(chibi macroexpand) - macro expansion utility
(chibi ast) - interface to the internal Abstract Syntax Tree
(chibi disasm) - disassembly utility for the chibi VM
(chibi heap-stats) - debugging tool to analyze or dump the heap
------------------------------------------------------------------------
C INTERFACE
See the file main.c for an example of using chibi-scheme as a library.
The basic usage involves creating a context for evaluation and loading
or evaluating Scheme source with it. Begin by including the eval.h
header file:
#include <chibi/eval.h>
then call
sexp_scheme_init();
with no parameters to initialize any globals (this actually does
nothing in the standard configuration but is a good idea to call
anyway).
Then you can use the following to create and manipulate contexts:
sexp_make_eval_context(context, stack, environment, heap_size)
Creates a new context with the given stack and environment.
If context is non-NULL, this will be the "parent" context and
the two contexts will share a heap. Otherwise, a new heap
will be allocated with heap_size, or a default size if heap_size
is zero. stack and environment may both also be NULL (and _must_
be NULL if context is NULL) and will be given standard defaults.
Thus the to create your first context you generally call:
sexp_make_eval_context(NULL, NULL, NULL, 0)
You can create as many contexts as you want, and other than those
sharing a heap they are all independent and thread-safe.
sexp_load_standard_env(context, env, version)
Loads the init.scm file in the environment env. Version refers
to the RnRS version number and should always be SEXP_FIVE. The
environment created with sexp_make_eval_context only contains
core syntactic forms and C primitives (thus for example it has
CAR but not CADR or LIST), so to get a full featured
environment, plus a module system with which to load additional
modules, you want to use this.
sexp_destroy_context(context)
Signals that you no longer need context, or any other context
sharing the heap. It will thus free() the context and heap and
all associated memory. Does nothing if using the Boehm GC.
Environments can be handled with the following:
sexp_context_env(context)
A macro returning the default environment associated with context.
sexp_env_define(context, env, symbol, value)
Define a variable in an environment.
sexp_env_ref(env, symbol, dflt)
Fetch the binding for symbol from the environment env,
returning the default dflt if the symbol is unbound.
You can evaluate code with the following utility:
sexp_eval(context, expr, env)
Evaluates an s-expression in an environment.
env can be NULL to use the context's default env.
sexp_eval_string(context, str, env)
Reads an s-expression from str and evaluates it in env.
sexp_load(context, file, env)
Read and eval all top-level forms from file in environment env.
As described in LOAD above, file may be a shared library.
To define new primitive functions from C, use sexp_define_foreign,
which takes a Scheme environment, a name, a number of arguments the C
function takes (not counting the context argument), and a C function.
/* sexp_define_foreign(context, env, name, num_args, f) */
sexp add (sexp context, sexp x, sexp y) {
return sexp_fx_add(x, y);
}
sexp_define_foreign(context, env, "add", 2, add);
You can also define functions with a single optional argument:
sexp_define_foreign_opt(context, env, "add", 2, add, sexp_make_fixnum(1));
See the SRFI-69 implementation for more detailed examples of this.
------------------------------------------------------------------------
FFI
Simple C FFI. "genstubs.scm file.stub" will read in the C function
FFI definitions from file.stub and output the appropriate C
wrappers into file.c. You can then compile that file with:
cc -fPIC -shared file.c -lchibi-scheme
(or using whatever flags are appropriate to generate shared libs on
your platform) and then the generated .so file can be loaded
directly with LOAD, or portably using (include-shared "file") in a
module definition (note that include-shared uses no suffix).
The goal of this interface is to make access to C types and
functions easy, without requiring the user to write any C code.
That means the stubber needs to be intelligent about various C
calling conventions and idioms, such as return values passed in
actual parameters. Writing C by hand is still possible, and
several of the core modules provide C interfaces directly without
using the stubber.
================================
Struct Interface
(define-c-struct struct-name
[predicate: predicate-name]
[constructor: constructor-name]
[finalizer: c_finalizer_name]
(type c_field_name getter-name setter-name) ...)
================================
Function Interface
(define-c return-type name-spec (arg-type ...))
where name-space is either a symbol name, or a list of
(scheme-name c_name). If just a symbol, the C name is taken
to be the same with -'s replaced by _'s.
arg-type is a type suitable for input validation and conversion.
================================
Types
Types
Basic Types
void
boolean
char
sexp (no conversions)
Integer Types:
signed-char short int long
unsigned-char unsigned-short unsigned-int unsigned-long size_t pid_t
time_t (in seconds, but using the chibi epoch of 2010/01/01)
errno (as a return type returns #f on error)
Float Types:
float double long-double
String Types:
string - a null-terminated char*
env-string - a VAR=VALUE string represented as a (VAR . VALUE) pair inScheme
in addition you can use (array char) as a string
Port Types:
input-port output-port
Struct Types:
Struct types are by default just referred to by the bare
struct-name from define-c-struct, and it is assumed you want a
pointer to that type. To refer to the full struct, use the struct
modifier, as in (struct struct-name).
Type modifiers
Any type may also be written as a list of modifiers followed by the
type itself. The supported modifiers are:
const: prepends the "const" C type modifier
* as a return or result parameter, makes non-immediates immutable
free: it's Scheme's responsibility to "free" this resource
* as a return or result parameter, registers the freep flag
this causes the type finalizer to be run when GCed
maybe-null: this pointer type may be NULL
* as a result parameter, NULL is translated to #f
normally this would just return a wrapped NULL pointer
* as an input parameter, #f is translated to NULL
normally this would be a type error
pointer: create a pointer to this type
* as a return parameter, wraps the result in a vanilla cpointer
* as a result parameter, boxes then unboxes the value
struct: treat this struct type as a struct, not a pointer
* as an input parameter, dereferences the pointer
* as a type field, indicates a nested struct
link: add a gc link
* as a field getter, link to the parent object, so the
parent won't be GCed so long as we have a reference
to the child. this behavior is automatic for nested
structs.
result: return a result in this parameter
* if there are multiple results (including the return type),
they are all returned in a list
* if there are any result parameters, a return type
of errno returns #f on failure, and as eliminated
from the list of results otherwise
(value <expr>): specify a fixed value
* as an input parameter, this parameter is not provided
in the Scheme API but always passed as <expr>
(default <expr>): specify a default value
* as the final input parameter, makes the Scheme parameter
optional, defaulting to <expr>
(array <type> [<length>]) an array type
* length must be specified for return and result parameters
* if specified, length can be any of
** an integer, for a fixed size
** the symbol null, indicating a NULL-terminated array

View file

@ -1 +1 @@
0.2
0.3

View file

@ -1,3 +1,6 @@
/* ast.c -- interface to the Abstract Syntax Tree */
/* Copyright (c) 2009 Alex Shinn. All rights reserved. */
/* BSD-style license: http://synthcode.com/license.txt */
#include <chibi/eval.h>

View file

@ -1,4 +1,4 @@
/* debug.c -- optional debugging utilities */
/* disasm.c -- optional debugging utilities */
/* Copyright (c) 2009 Alex Shinn. All rights reserved. */
/* BSD-style license: http://synthcode.com/license.txt */

View file

@ -1,3 +1,6 @@
;; filesystem.scm -- additional filesystem utilities
;; Copyright (c) 2009 Alex Shinn. All rights reserved.
;; BSD-style license: http://synthcode.com/license.txt
(define (directory-fold dir kons knil)
(let ((dir (opendir dir)))

View file

@ -1,3 +1,6 @@
/* heap-stats.c -- count or dump heap objects */
/* Copyright (c) 2009 Alex Shinn. All rights reserved. */
/* BSD-style license: http://synthcode.com/license.txt */
#include <chibi/eval.h>

View file

@ -1,3 +1,9 @@
;; macroexpand.scm -- macro expansion utility
;; Copyright (c) 2009 Alex Shinn. All rights reserved.
;; BSD-style license: http://synthcode.com/license.txt
;; This actually analyzes the expression then reverse-engineers an
;; sexp from the result, generating a minimal amount of renames.
(define (macroexpand x)
(ast->sexp (analyze x)))

View file

@ -1,3 +1,6 @@
;; net.scm -- the high-level network interface
;; Copyright (c) 2009 Alex Shinn. All rights reserved.
;; BSD-style license: http://synthcode.com/license.txt
(define (with-net-io host service proc)
(let lp ((addr (get-address-info host service #f)))

View file

@ -1,3 +1,6 @@
;; pathname.scm -- a general, non-host-specific path lib
;; Copyright (c) 2009 Alex Shinn. All rights reserved.
;; BSD-style license: http://synthcode.com/license.txt
(define (string-scan c str . o)
(let ((limit (string-length str)))

View file

@ -1,3 +1,6 @@
/* signal.c -- process signals interface */
/* Copyright (c) 2009 Alex Shinn. All rights reserved. */
/* BSD-style license: http://synthcode.com/license.txt */
#define SEXP_MAX_SIGNUM 32

View file

@ -1,3 +1,6 @@
;; uri.scm -- URI parsing library
;; Copyright (c) 2009 Alex Shinn. All rights reserved.
;; BSD-style license: http://synthcode.com/license.txt
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; URI representation

View file

@ -1,3 +1,6 @@
;; config.scm -- configuration module
;; Copyright (c) 2009 Alex Shinn. All rights reserved.
;; BSD-style license: http://synthcode.com/license.txt
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; modules

View file

@ -1,3 +1,6 @@
;; init.scm -- R5RS library procedures
;; Copyright (c) 2009 Alex Shinn. All rights reserved.
;; BSD-style license: http://synthcode.com/license.txt
;; provide c[ad]{2,4}r

View file

@ -1,3 +1,6 @@
;; alist.scm -- association list utilities
;; Copyright (c) 2009 Alex Shinn. All rights reserved.
;; BSD-style license: http://synthcode.com/license.txt
(define (alist-cons key value ls) (cons (cons key value) ls))

View file

@ -1,3 +1,6 @@
;; constructors.scm -- list construction utilities
;; Copyright (c) 2009 Alex Shinn. All rights reserved.
;; BSD-style license: http://synthcode.com/license.txt
(define (xcons a b) (cons b a))

View file

@ -1,3 +1,6 @@
;; deletion.scm -- list deletion utilities
;; Copyright (c) 2009 Alex Shinn. All rights reserved.
;; BSD-style license: http://synthcode.com/license.txt
(define (delete x ls . o)
(let ((eq (if (pair? o) (car o) equal?)))

View file

@ -1,3 +1,6 @@
;; fold.scm -- list fold/reduce utilities
;; Copyright (c) 2009 Alex Shinn. All rights reserved.
;; BSD-style license: http://synthcode.com/license.txt
(define (fold kons knil ls . lists)
(if (null? lists)

View file

@ -1,3 +1,6 @@
;; lset.scm -- list set library
;; Copyright (c) 2009 Alex Shinn. All rights reserved.
;; BSD-style license: http://synthcode.com/license.txt
(define (lset<= eq . sets)
(if (null? sets)

View file

@ -1,3 +1,6 @@
;; misc.scm -- miscellaneous list utilities
;; Copyright (c) 2009 Alex Shinn. All rights reserved.
;; BSD-style license: http://synthcode.com/license.txt
(define (map-onto proc ls init)
(let lp ((ls ls) (res init))

View file

@ -1,3 +1,6 @@
;; predicates.scm -- list prediates
;; Copyright (c) 2009 Alex Shinn. All rights reserved.
;; BSD-style license: http://synthcode.com/license.txt
(define (proper-list? x)
(cond ((null? x) #t)

View file

@ -1,3 +1,6 @@
;; search.scm -- list searching and splitting
;; Copyright (c) 2009 Alex Shinn. All rights reserved.
;; BSD-style license: http://synthcode.com/license.txt
(define (find pred ls)
(cond ((find-tail pred ls) => car) (else #f)))

View file

@ -1,3 +1,6 @@
;; selectors.scm -- extended list selectors
;; Copyright (c) 2009 Alex Shinn. All rights reserved.
;; BSD-style license: http://synthcode.com/license.txt
(define first car)
(define second cadr)

View file

@ -1,3 +1,6 @@
;; constructors.scm -- random function constructors
;; Copyright (c) 2009 Alex Shinn. All rights reserved.
;; BSD-style license: http://synthcode.com/license.txt
(define (random-source-make-integers rs)
(lambda (n) (%random-integer rs n)))

View file

@ -1,3 +1,6 @@
/* rand.c -- rand_r/random_r interface */
/* Copyright (c) 2009 Alex Shinn. All rights reserved. */
/* BSD-style license: http://synthcode.com/license.txt */
#include <time.h>
#include <chibi/eval.h>

View file

@ -1,3 +1,6 @@
/* bit.c -- bitwise operators */
/* Copyright (c) 2009 Alex Shinn. All rights reserved. */
/* BSD-style license: http://synthcode.com/license.txt */
#include <chibi/eval.h>
#include <limits.h>

View file

@ -1,3 +1,6 @@
;; bitwise.scm -- high-level bitwise functions
;; Copyright (c) 2009 Alex Shinn. All rights reserved.
;; BSD-style license: http://synthcode.com/license.txt
(define (bitwise-not i) (- (+ i 1)))

View file

@ -1,3 +1,6 @@
/* hash.c -- type-general hashing */
/* Copyright (c) 2009 Alex Shinn. All rights reserved. */
/* BSD-style license: http://synthcode.com/license.txt */
#include <chibi/eval.h>

View file

@ -1,3 +1,8 @@
;; interface.scm -- hash-table interface
;; Copyright (c) 2009 Alex Shinn. All rights reserved.
;; BSD-style license: http://synthcode.com/license.txt
;; the non-exported hash-table-cell is the heart of the implemenation
(define (make-hash-table . o)
(let ((eq-fn (if (pair? o) (car o) equal?))

View file

@ -1,3 +1,6 @@
;; types.scm -- the hash-table record type
;; Copyright (c) 2009 Alex Shinn. All rights reserved.
;; BSD-style license: http://synthcode.com/license.txt
(define-record-type hash-table
(%make-hash-table buckets size hash-fn eq-fn)

View file

@ -1,3 +1,6 @@
/* qsort.c -- quicksort implementation */
/* Copyright (c) 2009 Alex Shinn. All rights reserved. */
/* BSD-style license: http://synthcode.com/license.txt */
#include "chibi/eval.h"

View file

@ -1,3 +1,6 @@
;; sort.scm -- SRFI-95 sorting utilities
;; Copyright (c) 2009 Alex Shinn. All rights reserved.
;; BSD-style license: http://synthcode.com/license.txt
(define (copy seq)
(if (vector? seq)

View file

@ -1,3 +1,6 @@
/* env.c -- SRFI-98 environment interface */
/* Copyright (c) 2009 Alex Shinn. All rights reserved. */
/* BSD-style license: http://synthcode.com/license.txt */
#ifdef __APPLE__
#include <crt_externs.h>

View file

@ -1,3 +1,6 @@
/* simplify.c -- basic simplification pass */
/* Copyright (c) 2009 Alex Shinn. All rights reserved. */
/* BSD-style license: http://synthcode.com/license.txt */
#define simplify_it(it) ((it) = simplify(ctx, it, substs, lambda))