-
+ C48269A2C2F362F53C3E68E188B54351006342812EEAEA1CC41418C4CF1BFD0E780CF215AAD11E50FB0C53E1EEF05874121958BEE709E35E131D32B85831BC46
bitcoin/src/shiva/Manual.txt
(0 . 0)(1 . 452)
1487
1488
1489 TinySCHEME Version 1.41
1490
1491 "Safe if used as prescribed"
1492 -- Philip K. Dick, "Ubik"
1493
1494 This software is open source, covered by a BSD-style license.
1495 Please read accompanying file COPYING.
1496 -------------------------------------------------------------------------------
1497
1498 This Scheme interpreter is based on MiniSCHEME version 0.85k4
1499 (see miniscm.tar.gz in the Scheme Repository)
1500 Original credits in file MiniSCHEMETribute.txt.
1501
1502 D. Souflis (dsouflis@acm.org)
1503
1504 -------------------------------------------------------------------------------
1505 What is TinyScheme?
1506 -------------------
1507
1508 TinyScheme is a lightweight Scheme interpreter that implements as large
1509 a subset of R5RS as was possible without getting very large and
1510 complicated. It is meant to be used as an embedded scripting interpreter
1511 for other programs. As such, it does not offer IDEs or extensive toolkits
1512 although it does sport a small top-level loop, included conditionally.
1513 A lot of functionality in TinyScheme is included conditionally, to allow
1514 developers freedom in balancing features and footprint.
1515
1516 As an embedded interpreter, it allows multiple interpreter states to
1517 coexist in the same program, without any interference between them.
1518 Programmatically, foreign functions in C can be added and values
1519 can be defined in the Scheme environment. Being a quite small program,
1520 it is easy to comprehend, get to grips with, and use.
1521
1522 Known bugs
1523 ----------
1524
1525 TinyScheme is known to misbehave when memory is exhausted.
1526
1527
1528 Things that keep missing, or that need fixing
1529 ---------------------------------------------
1530
1531 There are no hygienic macros. No rational or
1532 complex numbers. No unwind-protect and call-with-values.
1533
1534 Maybe (a subset of) SLIB will work with TinySCHEME...
1535
1536 Decent debugging facilities are missing. Only tracing is supported
1537 natively.
1538
1539
1540 Scheme Reference
1541 ----------------
1542
1543 If something seems to be missing, please refer to the code and
1544 "init.scm", since some are library functions. Refer to the MiniSCHEME
1545 readme as a last resort.
1546
1547 Environments
1548 (interaction-environment)
1549 See R5RS. In TinySCHEME, immutable list of association lists.
1550
1551 (current-environment)
1552 The environment in effect at the time of the call. An example of its
1553 use and its utility can be found in the sample code that implements
1554 packages in "init.scm":
1555
1556 (macro (package form)
1557 `(apply (lambda ()
1558 ,@(cdr form)
1559 (current-environment))))
1560
1561 The environment containing the (local) definitions inside the closure
1562 is returned as an immutable value.
1563
1564 (defined? <symbol>) (defined? <symbol> <environment>)
1565 Checks whether the given symbol is defined in the current (or given)
1566 environment.
1567
1568 Symbols
1569 (gensym)
1570 Returns a new interned symbol each time. Will probably move to the
1571 library when string->symbol is implemented.
1572
1573 Directives
1574 (gc)
1575 Performs garbage collection immediatelly.
1576
1577 (gcverbose) (gcverbose <bool>)
1578 The argument (defaulting to #t) controls whether GC produces
1579 visible outcome.
1580
1581 (quit) (quit <num>)
1582 Stops the interpreter and sets the 'retcode' internal field (defaults
1583 to 0). When standalone, 'retcode' is returned as exit code to the OS.
1584
1585 (tracing <num>)
1586 1, turns on tracing. 0 turns it off. (Only when USE_TRACING is 1).
1587
1588 Mathematical functions
1589 Since rationals and complexes are absent, the respective functions
1590 are also missing.
1591 Supported: exp, log, sin, cos, tan, asin, acos, atan, floor, ceiling,
1592 trunc, round and also sqrt and expt when USE_MATH=1.
1593 Number-theoretical quotient, remainder and modulo, gcd, lcm.
1594 Library: exact?, inexact?, odd?, even?, zero?, positive?, negative?,
1595 exact->inexact. inexact->exact is a core function.
1596
1597 Type predicates
1598 boolean?,eof-object?,symbol?,number?,string?,integer?,real?,list?,null?,
1599 char?,port?,input-port?,output-port?,procedure?,pair?,environment?',
1600 vector?. Also closure?, macro?.
1601
1602 Types
1603 Types supported:
1604
1605 Numbers (integers and reals)
1606 Symbols
1607 Pairs
1608 Strings
1609 Characters
1610 Ports
1611 Eof object
1612 Environments
1613 Vectors
1614
1615 Literals
1616 String literals can contain escaped quotes \" as usual, but also
1617 \n, \r, \t, \xDD (hex representations) and \DDD (octal representations).
1618 Note also that it is possible to include literal newlines in string
1619 literals, e.g.
1620
1621 (define s "String with newline here
1622 and here
1623 that can function like a HERE-string")
1624
1625 Character literals contain #\space and #\newline and are supplemented
1626 with #\return and #\tab, with obvious meanings. Hex character
1627 representations are allowed (e.g. #\x20 is #\space).
1628 When USE_ASCII_NAMES is defined, various control characters can be
1629 referred to by their ASCII name.
1630 0 #\nul 17 #\dc1
1631 1 #\soh 18 #\dc2
1632 2 #\stx 19 #\dc3
1633 3 #\etx 20 #\dc4
1634 4 #\eot 21 #\nak
1635 5 #\enq 22 #\syn
1636 6 #\ack 23 #\etv
1637 7 #\bel 24 #\can
1638 8 #\bs 25 #\em
1639 9 #\ht 26 #\sub
1640 10 #\lf 27 #\esc
1641 11 #\vt 28 #\fs
1642 12 #\ff 29 #\gs
1643 13 #\cr 30 #\rs
1644 14 #\so 31 #\us
1645 15 #\si
1646 16 #\dle 127 #\del
1647
1648 Numeric literals support #x #o #b and #d. Flonums are currently read only
1649 in decimal notation. Full grammar will be supported soon.
1650
1651 Quote, quasiquote etc.
1652 As usual.
1653
1654 Immutable values
1655 Immutable pairs cannot be modified by set-car! and set-cdr!.
1656 Immutable strings cannot be modified via string-set!
1657
1658 I/O
1659 As per R5RS, plus String Ports (see below).
1660 current-input-port, current-output-port,
1661 close-input-port, close-output-port, input-port?, output-port?,
1662 open-input-file, open-output-file.
1663 read, write, display, newline, write-char, read-char, peek-char.
1664 char-ready? returns #t only for string ports, because there is no
1665 portable way in stdio to determine if a character is available.
1666 Also open-input-output-file, set-input-port, set-output-port (not R5RS)
1667 Library: call-with-input-file, call-with-output-file,
1668 with-input-from-file, with-output-from-file and
1669 with-input-output-from-to-files, close-port and input-output-port?
1670 (not R5RS).
1671 String Ports: open-input-string, open-output-string, get-output-string,
1672 open-input-output-string. Strings can be used with I/O routines.
1673
1674 Vectors
1675 make-vector, vector, vector-length, vector-ref, vector-set!, list->vector,
1676 vector-fill!, vector->list, vector-equal? (auxiliary function, not R5RS)
1677
1678 Strings
1679 string, make-string, list->string, string-length, string-ref, string-set!,
1680 substring, string->list, string-fill!, string-append, string-copy.
1681 string=?, string<?, string>?, string>?, string<=?, string>=?.
1682 (No string-ci*? yet). string->number, number->string. Also atom->string,
1683 string->atom (not R5RS).
1684
1685 Symbols
1686 symbol->string, string->symbol
1687
1688 Characters
1689 integer->char, char->integer.
1690 char=?, char<?, char>?, char<=?, char>=?.
1691 (No char-ci*?)
1692
1693 Pairs & Lists
1694 cons, car, cdr, list, length, map, for-each, foldr, list-tail,
1695 list-ref, last-pair, reverse, append.
1696 Also member, memq, memv, based on generic-member, assoc, assq, assv
1697 based on generic-assoc.
1698
1699 Streams
1700 head, tail, cons-stream
1701
1702 Control features
1703 Apart from procedure?, also macro? and closure?
1704 map, for-each, force, delay, call-with-current-continuation (or call/cc),
1705 eval, apply. 'Forcing' a value that is not a promise produces the value.
1706 There is no call-with-values, values, nor dynamic-wind. Dynamic-wind in
1707 the presence of continuations would require support from the abstract
1708 machine itself.
1709
1710 Property lists
1711 TinyScheme inherited from MiniScheme property lists for symbols.
1712 put, get.
1713
1714 Dynamically-loaded extensions
1715 (load-extension <filename without extension>)
1716 Loads a DLL declaring foreign procedures. On Unix/Linux, one can make use
1717 of the ld.so.conf file or the LD_RUN_PATH system variable in order to place
1718 the library in a directory other than the current one. Please refer to the
1719 appropriate 'man' page.
1720
1721 Esoteric procedures
1722 (oblist)
1723 Returns the oblist, an immutable list of all the symbols.
1724
1725 (macro-expand <form>)
1726 Returns the expanded form of the macro call denoted by the argument
1727
1728 (define-with-return (<procname> <args>...) <body>)
1729 Like plain 'define', but makes the continuation available as 'return'
1730 inside the procedure. Handy for imperative programs.
1731
1732 (new-segment <num>)
1733 Allocates more memory segments.
1734
1735 defined?
1736 See "Environments"
1737
1738 (get-closure-code <closure>)
1739 Gets the code as scheme data.
1740
1741 (make-closure <code> <environment>)
1742 Makes a new closure in the given environment.
1743
1744 Obsolete procedures
1745 (print-width <object>)
1746
1747 Programmer's Reference
1748 ----------------------
1749
1750 The interpreter state is initialized with "scheme_init".
1751 Custom memory allocation routines can be installed with an alternate
1752 initialization function: "scheme_init_custom_alloc".
1753 Files can be loaded with "scheme_load_file". Strings containing Scheme
1754 code can be loaded with "scheme_load_string". It is a good idea to
1755 "scheme_load" init.scm before anything else.
1756
1757 External data for keeping external state (of use to foreign functions)
1758 can be installed with "scheme_set_external_data".
1759 Foreign functions are installed with "assign_foreign". Additional
1760 definitions can be added to the interpreter state, with "scheme_define"
1761 (this is the way HTTP header data and HTML form data are passed to the
1762 Scheme script in the Altera SQL Server). If you wish to define the
1763 foreign function in a specific environment (to enhance modularity),
1764 use "assign_foreign_env".
1765
1766 The procedure "scheme_apply0" has been added with persistent scripts in
1767 mind. Persistent scripts are loaded once, and every time they are needed
1768 to produce HTTP output, appropriate data are passed through global
1769 definitions and function "main" is called to do the job. One could
1770 add easily "scheme_apply1" etc.
1771
1772 The interpreter state should be deinitialized with "scheme_deinit".
1773
1774 DLLs containing foreign functions should define a function named
1775 init_<base-name>. E.g. foo.dll should define init_foo, and bar.so
1776 should define init_bar. This function should assign_foreign any foreign
1777 function contained in the DLL.
1778
1779 The first dynamically loaded extension available for TinyScheme is
1780 a regular expression library. Although it's by no means an
1781 established standard, this library is supposed to be installed in
1782 a directory mirroring its name under the TinyScheme location.
1783
1784
1785 Foreign Functions
1786 -----------------
1787
1788 The user can add foreign functions in C. For example, a function
1789 that squares its argument:
1790
1791 pointer square(scheme *sc, pointer args) {
1792 if(args!=sc->NIL) {
1793 if(sc->isnumber(sc->pair_car(args))) {
1794 double v=sc->rvalue(sc->pair_car(args));
1795 return sc->mk_real(sc,v*v);
1796 }
1797 }
1798 return sc->NIL;
1799 }
1800
1801 Foreign functions are now defined as closures:
1802
1803 sc->interface->scheme_define(
1804 sc,
1805 sc->global_env,
1806 sc->interface->mk_symbol(sc,"square"),
1807 sc->interface->mk_foreign_func(sc, square));
1808
1809
1810 Foreign functions can use the external data in the "scheme" struct
1811 to implement any kind of external state.
1812
1813 External data are set with the following function:
1814 void scheme_set_external_data(scheme *sc, void *p);
1815
1816 As of v.1.17, the canonical way for a foreign function in a DLL to
1817 manipulate Scheme data is using the function pointers in sc->interface.
1818
1819 Standalone
1820 ----------
1821
1822 Usage: tinyscheme -?
1823 or: tinyscheme [<file1> <file2> ...]
1824 followed by
1825 -1 <file> [<arg1> <arg2> ...]
1826 -c <Scheme commands> [<arg1> <arg2> ...]
1827 assuming that the executable is named tinyscheme.
1828
1829 Use - in the place of a filename to denote stdin.
1830 The -1 flag is meant for #! usage in shell scripts. If you specify
1831 #! /somewhere/tinyscheme -1
1832 then tinyscheme will be called to process the file. For example, the
1833 following script echoes the Scheme list of its arguments.
1834
1835 #! /somewhere/tinyscheme -1
1836 (display *args*)
1837
1838 The -c flag permits execution of arbitrary Scheme code.
1839
1840
1841 Error Handling
1842 --------------
1843
1844 Errors are recovered from without damage. The user can install his
1845 own handler for system errors, by defining *error-hook*. Defining
1846 to '() gives the default behavior, which is equivalent to "error".
1847 USE_ERROR_HOOK must be defined.
1848
1849 A simple exception handling mechanism can be found in "init.scm".
1850 A new syntactic form is introduced:
1851
1852 (catch <expr returned exceptionally>
1853 <expr1> <expr2> ... <exprN>)
1854
1855 "Catch" establishes a scope spanning multiple call-frames
1856 until another "catch" is encountered.
1857
1858 Exceptions are thrown with:
1859
1860 (throw "message")
1861
1862 If used outside a (catch ...), reverts to (error "message").
1863
1864 Example of use:
1865
1866 (define (foo x) (write x) (newline) (/ x 0))
1867
1868 (catch (begin (display "Error!\n") 0)
1869 (write "Before foo ... ")
1870 (foo 5)
1871 (write "After foo"))
1872
1873 The exception mechanism can be used even by system errors, by
1874
1875 (define *error-hook* throw)
1876
1877 which makes use of the error hook described above.
1878
1879 If necessary, the user can devise his own exception mechanism with
1880 tagged exceptions etc.
1881
1882
1883 Reader extensions
1884 -----------------
1885
1886 When encountering an unknown character after '#', the user-specified
1887 procedure *sharp-hook* (if any), is called to read the expression.
1888 This can be used to extend the reader to handle user-defined constants
1889 or whatever. It should be a procedure without arguments, reading from
1890 the current input port (which will be the load-port).
1891
1892
1893 Colon Qualifiers - Packages
1894 ---------------------------
1895
1896 When USE_COLON_HOOK=1:
1897 The lexer now recognizes the construction <qualifier>::<symbol> and
1898 transforms it in the following manner (T is the transformation function):
1899
1900 T(<qualifier>::<symbol>) = (*colon-hook* 'T(<symbol>) <qualifier>)
1901
1902 where <qualifier> is a symbol not containing any double-colons.
1903
1904 As the definition is recursive, qualifiers can be nested.
1905 The user can define his own *colon-hook*, to handle qualified names.
1906 By default, "init.scm" defines *colon-hook* as EVAL. Consequently,
1907 the qualifier must denote a Scheme environment, such as one returned
1908 by (interaction-environment). "Init.scm" defines a new syntantic form,
1909 PACKAGE, as a simple example. It is used like this:
1910
1911 (define toto
1912 (package
1913 (define foo 1)
1914 (define bar +)))
1915
1916 foo ==> Error, "foo" undefined
1917 (eval 'foo) ==> Error, "foo" undefined
1918 (eval 'foo toto) ==> 1
1919 toto::foo ==> 1
1920 ((eval 'bar toto) 2 (eval 'foo toto)) ==> 3
1921 (toto::bar 2 toto::foo) ==> 3
1922 (eval (bar 2 foo) toto) ==> 3
1923
1924 If the user installs another package infrastructure, he must define
1925 a new 'package' procedure or macro to retain compatibility with supplied
1926 code.
1927
1928 Note: Older versions used ':' as a qualifier. Unfortunately, the use
1929 of ':' as a pseudo-qualifier in existing code (i.e. SLIB) essentially
1930 precludes its use as a real qualifier.
1931
1932
1933
1934
1935
1936
1937
1938