Manual.txt 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449
  1. TinySCHEME Version 1.38
  2. "Safe if used as prescribed"
  3. -- Philip K. Dick, "Ubik"
  4. This software is open source, covered by a BSD-style license.
  5. Please read accompanying file COPYING.
  6. -------------------------------------------------------------------------------
  7. This Scheme interpreter is based on MiniSCHEME version 0.85k4
  8. (see miniscm.tar.gz in the Scheme Repository)
  9. Original credits in file MiniSCHEMETribute.txt.
  10. D. Souflis (dsouflis@acm.org)
  11. -------------------------------------------------------------------------------
  12. What is TinyScheme?
  13. -------------------
  14. TinyScheme is a lightweight Scheme interpreter that implements as large
  15. a subset of R5RS as was possible without getting very large and
  16. complicated. It is meant to be used as an embedded scripting interpreter
  17. for other programs. As such, it does not offer IDEs or extensive toolkits
  18. although it does sport a small top-level loop, included conditionally.
  19. A lot of functionality in TinyScheme is included conditionally, to allow
  20. developers freedom in balancing features and footprint.
  21. As an embedded interpreter, it allows multiple interpreter states to
  22. coexist in the same program, without any interference between them.
  23. Programmatically, foreign functions in C can be added and values
  24. can be defined in the Scheme environment. Being a quite small program,
  25. it is easy to comprehend, get to grips with, and use.
  26. Known bugs
  27. ----------
  28. TinyScheme is known to misbehave when memory is exhausted.
  29. Things that keep missing, or that need fixing
  30. ---------------------------------------------
  31. There are no hygienic macros. No rational or
  32. complex numbers. No unwind-protect and call-with-values.
  33. Maybe (a subset of) SLIB will work with TinySCHEME...
  34. Decent debugging facilities are missing. Only tracing is supported
  35. natively.
  36. Scheme Reference
  37. ----------------
  38. If something seems to be missing, please refer to the code and
  39. "init.scm", since some are library functions. Refer to the MiniSCHEME
  40. readme as a last resort.
  41. Environments
  42. (interaction-environment)
  43. See R5RS. In TinySCHEME, immutable list of association lists.
  44. (current-environment)
  45. The environment in effect at the time of the call. An example of its
  46. use and its utility can be found in the sample code that implements
  47. packages in "init.scm":
  48. (macro (package form)
  49. `(apply (lambda ()
  50. ,@(cdr form)
  51. (current-environment))))
  52. The environment containing the (local) definitions inside the closure
  53. is returned as an immutable value.
  54. (defined? <symbol>) (defined? <symbol> <environment>)
  55. Checks whether the given symbol is defined in the current (or given)
  56. environment.
  57. Symbols
  58. (gensym)
  59. Returns a new interned symbol each time. Will probably move to the
  60. library when string->symbol is implemented.
  61. Directives
  62. (gc)
  63. Performs garbage collection immediatelly.
  64. (gcverbose) (gcverbose <bool>)
  65. The argument (defaulting to #t) controls whether GC produces
  66. visible outcome.
  67. (quit) (quit <num>)
  68. Stops the interpreter and sets the 'retcode' internal field (defaults
  69. to 0). When standalone, 'retcode' is returned as exit code to the OS.
  70. (tracing <num>)
  71. 1, turns on tracing. 0 turns it off. (Only when USE_TRACING is 1).
  72. Mathematical functions
  73. Since rationals and complexes are absent, the respective functions
  74. are also missing.
  75. Supported: exp, log, sin, cos, tan, asin, acos, atan, floor, ceiling,
  76. trunc, round and also sqrt and expt when USE_MATH=1.
  77. Number-theoretical quotient, remainder and modulo, gcd, lcm.
  78. Library: exact?, inexact?, odd?, even?, zero?, positive?, negative?,
  79. exact->inexact. inexact->exact is a core function.
  80. Type predicates
  81. boolean?,eof-object?,symbol?,number?,string?,integer?,real?,list?,null?,
  82. char?,port?,input-port?,output-port?,procedure?,pair?,environment?',
  83. vector?. Also closure?, macro?.
  84. Types
  85. Types supported:
  86. Numbers (integers and reals)
  87. Symbols
  88. Pairs
  89. Strings
  90. Characters
  91. Ports
  92. Eof object
  93. Environments
  94. Vectors
  95. Literals
  96. String literals can contain escaped quotes \" as usual, but also
  97. \n, \r, \t, \xDD (hex representations) and \DDD (octal representations).
  98. Note also that it is possible to include literal newlines in string
  99. literals, e.g.
  100. (define s "String with newline here
  101. and here
  102. that can function like a HERE-string")
  103. Character literals contain #\space and #\newline and are supplemented
  104. with #\return and #\tab, with obvious meanings. Hex character
  105. representations are allowed (e.g. #\x20 is #\space).
  106. When USE_ASCII_NAMES is defined, various control characters can be
  107. refered to by their ASCII name.
  108. 0 #\nul 17 #\dc1
  109. 1 #\soh 18 #\dc2
  110. 2 #\stx 19 #\dc3
  111. 3 #\etx 20 #\dc4
  112. 4 #\eot 21 #\nak
  113. 5 #\enq 22 #\syn
  114. 6 #\ack 23 #\etv
  115. 7 #\bel 24 #\can
  116. 8 #\bs 25 #\em
  117. 9 #\ht 26 #\sub
  118. 10 #\lf 27 #\esc
  119. 11 #\vt 28 #\fs
  120. 12 #\ff 29 #\gs
  121. 13 #\cr 30 #\rs
  122. 14 #\so 31 #\us
  123. 15 #\si
  124. 16 #\dle 127 #\del
  125. Numeric literals support #x #o #b and #d. Flonums are currently read only
  126. in decimal notation. Full grammar will be supported soon.
  127. Quote, quasiquote etc.
  128. As usual.
  129. Immutable values
  130. Immutable pairs cannot be modified by set-car! and set-cdr!.
  131. Immutable strings cannot be modified via string-set!
  132. I/O
  133. As per R5RS, plus String Ports (see below).
  134. current-input-port, current-output-port,
  135. close-input-port, close-output-port, input-port?, output-port?,
  136. open-input-file, open-output-file.
  137. read, write, display, newline, write-char, read-char, peek-char.
  138. char-ready? returns #t only for string ports, because there is no
  139. portable way in stdio to determine if a character is available.
  140. Also open-input-output-file, set-input-port, set-output-port (not R5RS)
  141. Library: call-with-input-file, call-with-output-file,
  142. with-input-from-file, with-output-from-file and
  143. with-input-output-from-to-files, close-port and input-output-port?
  144. (not R5RS).
  145. String Ports: open-input-string, open-output-string,
  146. open-input-output-string. Strings can be used with I/O routines.
  147. Vectors
  148. make-vector, vector, vector-length, vector-ref, vector-set!, list->vector,
  149. vector-fill!, vector->list, vector-equal? (auxiliary function, not R5RS)
  150. Strings
  151. string, make-string, list->string, string-length, string-ref, string-set!,
  152. substring, string->list, string-fill!, string-append, string-copy.
  153. string=?, string<?, string>?, string>?, string<=?, string>=?.
  154. (No string-ci*? yet). string->number, number->string. Also atom->string,
  155. string->atom (not R5RS).
  156. Symbols
  157. symbol->string, string->symbol
  158. Characters
  159. integer->char, char->integer.
  160. char=?, char<?, char>?, char<=?, char>=?.
  161. (No char-ci*?)
  162. Pairs & Lists
  163. cons, car, cdr, list, length, map, for-each, foldr, list-tail,
  164. list-ref, last-pair, reverse, append.
  165. Also member, memq, memv, based on generic-member, assoc, assq, assv
  166. based on generic-assoc.
  167. Streams
  168. head, tail, cons-stream
  169. Control features
  170. Apart from procedure?, also macro? and closure?
  171. map, for-each, force, delay, call-with-current-continuation (or call/cc),
  172. eval, apply. 'Forcing' a value that is not a promise produces the value.
  173. There is no call-with-values, values, nor dynamic-wind. Dynamic-wind in
  174. the presence of continuations would require support from the abstract
  175. machine itself.
  176. Property lists
  177. TinyScheme inherited from MiniScheme property lists for symbols.
  178. put, get.
  179. Dynamically-loaded extensions
  180. (load-extension <filename without extension>)
  181. Loads a DLL declaring foreign procedures.
  182. Esoteric procedures
  183. (oblist)
  184. Returns the oblist, an immutable list of all the symbols.
  185. (macro-expand <form>)
  186. Returns the expanded form of the macro call denoted by the argument
  187. (define-with-return (<procname> <args>...) <body>)
  188. Like plain 'define', but makes the continuation available as 'return'
  189. inside the procedure. Handy for imperative programs.
  190. (new-segment <num>)
  191. Allocates more memory segments.
  192. defined?
  193. See "Environments"
  194. (get-closure-code <closure>)
  195. Gets the code as scheme data.
  196. (make-closure <code> <environment>)
  197. Makes a new closure in the given environment.
  198. Obsolete procedures
  199. (print-width <object>)
  200. Programmer's Reference
  201. ----------------------
  202. The interpreter state is initialized with "scheme_init".
  203. Custom memory allocation routines can be installed with an alternate
  204. initialization function: "scheme_init_custom_alloc".
  205. Files can be loaded with "scheme_load_file". Strings containing Scheme
  206. code can be loaded with "scheme_load_string". It is a good idea to
  207. "scheme_load" init.scm before anything else.
  208. External data for keeping external state (of use to foreign functions)
  209. can be installed with "scheme_set_external_data".
  210. Foreign functions are installed with "assign_foreign". Additional
  211. definitions can be added to the interpreter state, with "scheme_define"
  212. (this is the way HTTP header data and HTML form data are passed to the
  213. Scheme script in the Altera SQL Server). If you wish to define the
  214. foreign function in a specific environment (to enhance modularity),
  215. use "assign_foreign_env".
  216. The procedure "scheme_apply0" has been added with persistent scripts in
  217. mind. Persistent scripts are loaded once, and every time they are needed
  218. to produce HTTP output, appropriate data are passed through global
  219. definitions and function "main" is called to do the job. One could
  220. add easily "scheme_apply1" etc.
  221. The interpreter state should be deinitialized with "scheme_deinit".
  222. DLLs containing foreign functions should define a function named
  223. init_<base-name>. E.g. foo.dll should define init_foo, and bar.so
  224. should define init_bar. This function should assign_foreign any foreign
  225. function contained in the DLL.
  226. The first dynamically loaded extension available for TinyScheme is
  227. a regular expression library. Although it's by no means an
  228. established standard, this library is supposed to be installed in
  229. a directory mirroring its name under the TinyScheme location.
  230. Foreign Functions
  231. -----------------
  232. The user can add foreign functions in C. For example, a function
  233. that squares its argument:
  234. pointer square(scheme *sc, pointer args) {
  235. if(args!=sc->NIL) {
  236. if(sc->isnumber(sc->pair_car(args))) {
  237. double v=sc->rvalue(sc->pair_car(args));
  238. return sc->mk_real(sc,v*v);
  239. }
  240. }
  241. return sc->NIL;
  242. }
  243. Foreign functions are now defined as closures:
  244. sc->interface->scheme_define(
  245. sc,
  246. sc->global_env,
  247. sc->interface->mk_symbol(sc,"square"),
  248. sc->interface->mk_foreign_func(sc, square));
  249. Foreign functions can use the external data in the "scheme" struct
  250. to implement any kind of external state.
  251. External data are set with the following function:
  252. void scheme_set_external_data(scheme *sc, void *p);
  253. As of v.1.17, the canonical way for a foreign function in a DLL to
  254. manipulate Scheme data is using the function pointers in sc->interface.
  255. Standalone
  256. ----------
  257. Usage: tinyscheme -?
  258. or: tinyscheme [<file1> <file2> ...]
  259. followed by
  260. -1 <file> [<arg1> <arg2> ...]
  261. -c <Scheme commands> [<arg1> <arg2> ...]
  262. assuming that the executable is named tinyscheme.
  263. Use - in the place of a filename to denote stdin.
  264. The -1 flag is meant for #! usage in shell scripts. If you specify
  265. #! /somewhere/tinyscheme -1
  266. then tinyscheme will be called to process the file. For example, the
  267. following script echoes the Scheme list of its arguments.
  268. #! /somewhere/tinyscheme -1
  269. (display *args*)
  270. The -c flag permits execution of arbitrary Scheme code.
  271. Error Handling
  272. --------------
  273. Errors are recovered from without damage. The user can install his
  274. own handler for system errors, by defining *error-hook*. Defining
  275. to '() gives the default behavior, which is equivalent to "error".
  276. USE_ERROR_HOOK must be defined.
  277. A simple exception handling mechanism can be found in "init.scm".
  278. A new syntactic form is introduced:
  279. (catch <expr returned exceptionally>
  280. <expr1> <expr2> ... <exprN>)
  281. "Catch" establishes a scope spanning multiple call-frames
  282. until another "catch" is encountered.
  283. Exceptions are thrown with:
  284. (throw "message")
  285. If used outside a (catch ...), reverts to (error "message").
  286. Example of use:
  287. (define (foo x) (write x) (newline) (/ x 0))
  288. (catch (begin (display "Error!\n") 0)
  289. (write "Before foo ... ")
  290. (foo 5)
  291. (write "After foo"))
  292. The exception mechanism can be used even by system errors, by
  293. (define *error-hook* throw)
  294. which makes use of the error hook described above.
  295. If necessary, the user can devise his own exception mechanism with
  296. tagged exceptions etc.
  297. Reader extensions
  298. -----------------
  299. When encountering an unknown character after '#', the user-specified
  300. procedure *sharp-hook* (if any), is called to read the expression.
  301. This can be used to extend the reader to handle user-defined constants
  302. or whatever. It should be a procedure without arguments, reading from
  303. the current input port (which will be the load-port).
  304. Colon Qualifiers - Packages
  305. ---------------------------
  306. When USE_COLON_HOOK=1:
  307. The lexer now recognizes the construction <qualifier>::<symbol> and
  308. transforms it in the following manner (T is the transformation function):
  309. T(<qualifier>::<symbol>) = (*colon-hook* 'T(<symbol>) <qualifier>)
  310. where <qualifier> is a symbol not containing any double-colons.
  311. As the definition is recursive, qualifiers can be nested.
  312. The user can define his own *colon-hook*, to handle qualified names.
  313. By default, "init.scm" defines *colon-hook* as EVAL. Consequently,
  314. the qualifier must denote a Scheme environment, such as one returned
  315. by (interaction-environment). "Init.scm" defines a new syntantic form,
  316. PACKAGE, as a simple example. It is used like this:
  317. (define toto
  318. (package
  319. (define foo 1)
  320. (define bar +)))
  321. foo ==> Error, "foo" undefined
  322. (eval 'foo) ==> Error, "foo" undefined
  323. (eval 'foo toto) ==> 1
  324. toto::foo ==> 1
  325. ((eval 'bar toto) 2 (eval 'foo toto)) ==> 3
  326. (toto::bar 2 toto::foo) ==> 3
  327. (eval (bar 2 foo) toto) ==> 3
  328. If the user installs another package infrastructure, he must define
  329. a new 'package' procedure or macro to retain compatibility with supplied
  330. code.
  331. Note: Older versions used ':' as a qualifier. Unfortunately, the use
  332. of ':' as a pseudo-qualifier in existing code (i.e. SLIB) essentially
  333. precludes its use as a real qualifier.