-
+ C48269A2C2F362F53C3E68E188B54351006342812EEAEA1CC41418C4CF1BFD0E780CF215AAD11E50FB0C53E1EEF05874121958BEE709E35E131D32B85831BC46tinyscheme/Manual.txt(0 . 0)(1 . 452)
513
514
515 TinySCHEME Version 1.41
516
517 "Safe if used as prescribed"
518 -- Philip K. Dick, "Ubik"
519
520 This software is open source, covered by a BSD-style license.
521 Please read accompanying file COPYING.
522 -------------------------------------------------------------------------------
523
524 This Scheme interpreter is based on MiniSCHEME version 0.85k4
525 (see miniscm.tar.gz in the Scheme Repository)
526 Original credits in file MiniSCHEMETribute.txt.
527
528 D. Souflis (dsouflis@acm.org)
529
530 -------------------------------------------------------------------------------
531 What is TinyScheme?
532 -------------------
533
534 TinyScheme is a lightweight Scheme interpreter that implements as large
535 a subset of R5RS as was possible without getting very large and
536 complicated. It is meant to be used as an embedded scripting interpreter
537 for other programs. As such, it does not offer IDEs or extensive toolkits
538 although it does sport a small top-level loop, included conditionally.
539 A lot of functionality in TinyScheme is included conditionally, to allow
540 developers freedom in balancing features and footprint.
541
542 As an embedded interpreter, it allows multiple interpreter states to
543 coexist in the same program, without any interference between them.
544 Programmatically, foreign functions in C can be added and values
545 can be defined in the Scheme environment. Being a quite small program,
546 it is easy to comprehend, get to grips with, and use.
547
548 Known bugs
549 ----------
550
551 TinyScheme is known to misbehave when memory is exhausted.
552
553
554 Things that keep missing, or that need fixing
555 ---------------------------------------------
556
557 There are no hygienic macros. No rational or
558 complex numbers. No unwind-protect and call-with-values.
559
560 Maybe (a subset of) SLIB will work with TinySCHEME...
561
562 Decent debugging facilities are missing. Only tracing is supported
563 natively.
564
565
566 Scheme Reference
567 ----------------
568
569 If something seems to be missing, please refer to the code and
570 "init.scm", since some are library functions. Refer to the MiniSCHEME
571 readme as a last resort.
572
573 Environments
574 (interaction-environment)
575 See R5RS. In TinySCHEME, immutable list of association lists.
576
577 (current-environment)
578 The environment in effect at the time of the call. An example of its
579 use and its utility can be found in the sample code that implements
580 packages in "init.scm":
581
582 (macro (package form)
583 `(apply (lambda ()
584 ,@(cdr form)
585 (current-environment))))
586
587 The environment containing the (local) definitions inside the closure
588 is returned as an immutable value.
589
590 (defined? <symbol>) (defined? <symbol> <environment>)
591 Checks whether the given symbol is defined in the current (or given)
592 environment.
593
594 Symbols
595 (gensym)
596 Returns a new interned symbol each time. Will probably move to the
597 library when string->symbol is implemented.
598
599 Directives
600 (gc)
601 Performs garbage collection immediatelly.
602
603 (gcverbose) (gcverbose <bool>)
604 The argument (defaulting to #t) controls whether GC produces
605 visible outcome.
606
607 (quit) (quit <num>)
608 Stops the interpreter and sets the 'retcode' internal field (defaults
609 to 0). When standalone, 'retcode' is returned as exit code to the OS.
610
611 (tracing <num>)
612 1, turns on tracing. 0 turns it off. (Only when USE_TRACING is 1).
613
614 Mathematical functions
615 Since rationals and complexes are absent, the respective functions
616 are also missing.
617 Supported: exp, log, sin, cos, tan, asin, acos, atan, floor, ceiling,
618 trunc, round and also sqrt and expt when USE_MATH=1.
619 Number-theoretical quotient, remainder and modulo, gcd, lcm.
620 Library: exact?, inexact?, odd?, even?, zero?, positive?, negative?,
621 exact->inexact. inexact->exact is a core function.
622
623 Type predicates
624 boolean?,eof-object?,symbol?,number?,string?,integer?,real?,list?,null?,
625 char?,port?,input-port?,output-port?,procedure?,pair?,environment?',
626 vector?. Also closure?, macro?.
627
628 Types
629 Types supported:
630
631 Numbers (integers and reals)
632 Symbols
633 Pairs
634 Strings
635 Characters
636 Ports
637 Eof object
638 Environments
639 Vectors
640
641 Literals
642 String literals can contain escaped quotes \" as usual, but also
643 \n, \r, \t, \xDD (hex representations) and \DDD (octal representations).
644 Note also that it is possible to include literal newlines in string
645 literals, e.g.
646
647 (define s "String with newline here
648 and here
649 that can function like a HERE-string")
650
651 Character literals contain #\space and #\newline and are supplemented
652 with #\return and #\tab, with obvious meanings. Hex character
653 representations are allowed (e.g. #\x20 is #\space).
654 When USE_ASCII_NAMES is defined, various control characters can be
655 referred to by their ASCII name.
656 0 #\nul 17 #\dc1
657 1 #\soh 18 #\dc2
658 2 #\stx 19 #\dc3
659 3 #\etx 20 #\dc4
660 4 #\eot 21 #\nak
661 5 #\enq 22 #\syn
662 6 #\ack 23 #\etv
663 7 #\bel 24 #\can
664 8 #\bs 25 #\em
665 9 #\ht 26 #\sub
666 10 #\lf 27 #\esc
667 11 #\vt 28 #\fs
668 12 #\ff 29 #\gs
669 13 #\cr 30 #\rs
670 14 #\so 31 #\us
671 15 #\si
672 16 #\dle 127 #\del
673
674 Numeric literals support #x #o #b and #d. Flonums are currently read only
675 in decimal notation. Full grammar will be supported soon.
676
677 Quote, quasiquote etc.
678 As usual.
679
680 Immutable values
681 Immutable pairs cannot be modified by set-car! and set-cdr!.
682 Immutable strings cannot be modified via string-set!
683
684 I/O
685 As per R5RS, plus String Ports (see below).
686 current-input-port, current-output-port,
687 close-input-port, close-output-port, input-port?, output-port?,
688 open-input-file, open-output-file.
689 read, write, display, newline, write-char, read-char, peek-char.
690 char-ready? returns #t only for string ports, because there is no
691 portable way in stdio to determine if a character is available.
692 Also open-input-output-file, set-input-port, set-output-port (not R5RS)
693 Library: call-with-input-file, call-with-output-file,
694 with-input-from-file, with-output-from-file and
695 with-input-output-from-to-files, close-port and input-output-port?
696 (not R5RS).
697 String Ports: open-input-string, open-output-string, get-output-string,
698 open-input-output-string. Strings can be used with I/O routines.
699
700 Vectors
701 make-vector, vector, vector-length, vector-ref, vector-set!, list->vector,
702 vector-fill!, vector->list, vector-equal? (auxiliary function, not R5RS)
703
704 Strings
705 string, make-string, list->string, string-length, string-ref, string-set!,
706 substring, string->list, string-fill!, string-append, string-copy.
707 string=?, string<?, string>?, string>?, string<=?, string>=?.
708 (No string-ci*? yet). string->number, number->string. Also atom->string,
709 string->atom (not R5RS).
710
711 Symbols
712 symbol->string, string->symbol
713
714 Characters
715 integer->char, char->integer.
716 char=?, char<?, char>?, char<=?, char>=?.
717 (No char-ci*?)
718
719 Pairs & Lists
720 cons, car, cdr, list, length, map, for-each, foldr, list-tail,
721 list-ref, last-pair, reverse, append.
722 Also member, memq, memv, based on generic-member, assoc, assq, assv
723 based on generic-assoc.
724
725 Streams
726 head, tail, cons-stream
727
728 Control features
729 Apart from procedure?, also macro? and closure?
730 map, for-each, force, delay, call-with-current-continuation (or call/cc),
731 eval, apply. 'Forcing' a value that is not a promise produces the value.
732 There is no call-with-values, values, nor dynamic-wind. Dynamic-wind in
733 the presence of continuations would require support from the abstract
734 machine itself.
735
736 Property lists
737 TinyScheme inherited from MiniScheme property lists for symbols.
738 put, get.
739
740 Dynamically-loaded extensions
741 (load-extension <filename without extension>)
742 Loads a DLL declaring foreign procedures. On Unix/Linux, one can make use
743 of the ld.so.conf file or the LD_RUN_PATH system variable in order to place
744 the library in a directory other than the current one. Please refer to the
745 appropriate 'man' page.
746
747 Esoteric procedures
748 (oblist)
749 Returns the oblist, an immutable list of all the symbols.
750
751 (macro-expand <form>)
752 Returns the expanded form of the macro call denoted by the argument
753
754 (define-with-return (<procname> <args>...) <body>)
755 Like plain 'define', but makes the continuation available as 'return'
756 inside the procedure. Handy for imperative programs.
757
758 (new-segment <num>)
759 Allocates more memory segments.
760
761 defined?
762 See "Environments"
763
764 (get-closure-code <closure>)
765 Gets the code as scheme data.
766
767 (make-closure <code> <environment>)
768 Makes a new closure in the given environment.
769
770 Obsolete procedures
771 (print-width <object>)
772
773 Programmer's Reference
774 ----------------------
775
776 The interpreter state is initialized with "scheme_init".
777 Custom memory allocation routines can be installed with an alternate
778 initialization function: "scheme_init_custom_alloc".
779 Files can be loaded with "scheme_load_file". Strings containing Scheme
780 code can be loaded with "scheme_load_string". It is a good idea to
781 "scheme_load" init.scm before anything else.
782
783 External data for keeping external state (of use to foreign functions)
784 can be installed with "scheme_set_external_data".
785 Foreign functions are installed with "assign_foreign". Additional
786 definitions can be added to the interpreter state, with "scheme_define"
787 (this is the way HTTP header data and HTML form data are passed to the
788 Scheme script in the Altera SQL Server). If you wish to define the
789 foreign function in a specific environment (to enhance modularity),
790 use "assign_foreign_env".
791
792 The procedure "scheme_apply0" has been added with persistent scripts in
793 mind. Persistent scripts are loaded once, and every time they are needed
794 to produce HTTP output, appropriate data are passed through global
795 definitions and function "main" is called to do the job. One could
796 add easily "scheme_apply1" etc.
797
798 The interpreter state should be deinitialized with "scheme_deinit".
799
800 DLLs containing foreign functions should define a function named
801 init_<base-name>. E.g. foo.dll should define init_foo, and bar.so
802 should define init_bar. This function should assign_foreign any foreign
803 function contained in the DLL.
804
805 The first dynamically loaded extension available for TinyScheme is
806 a regular expression library. Although it's by no means an
807 established standard, this library is supposed to be installed in
808 a directory mirroring its name under the TinyScheme location.
809
810
811 Foreign Functions
812 -----------------
813
814 The user can add foreign functions in C. For example, a function
815 that squares its argument:
816
817 pointer square(scheme *sc, pointer args) {
818 if(args!=sc->NIL) {
819 if(sc->isnumber(sc->pair_car(args))) {
820 double v=sc->rvalue(sc->pair_car(args));
821 return sc->mk_real(sc,v*v);
822 }
823 }
824 return sc->NIL;
825 }
826
827 Foreign functions are now defined as closures:
828
829 sc->interface->scheme_define(
830 sc,
831 sc->global_env,
832 sc->interface->mk_symbol(sc,"square"),
833 sc->interface->mk_foreign_func(sc, square));
834
835
836 Foreign functions can use the external data in the "scheme" struct
837 to implement any kind of external state.
838
839 External data are set with the following function:
840 void scheme_set_external_data(scheme *sc, void *p);
841
842 As of v.1.17, the canonical way for a foreign function in a DLL to
843 manipulate Scheme data is using the function pointers in sc->interface.
844
845 Standalone
846 ----------
847
848 Usage: tinyscheme -?
849 or: tinyscheme [<file1> <file2> ...]
850 followed by
851 -1 <file> [<arg1> <arg2> ...]
852 -c <Scheme commands> [<arg1> <arg2> ...]
853 assuming that the executable is named tinyscheme.
854
855 Use - in the place of a filename to denote stdin.
856 The -1 flag is meant for #! usage in shell scripts. If you specify
857 #! /somewhere/tinyscheme -1
858 then tinyscheme will be called to process the file. For example, the
859 following script echoes the Scheme list of its arguments.
860
861 #! /somewhere/tinyscheme -1
862 (display *args*)
863
864 The -c flag permits execution of arbitrary Scheme code.
865
866
867 Error Handling
868 --------------
869
870 Errors are recovered from without damage. The user can install his
871 own handler for system errors, by defining *error-hook*. Defining
872 to '() gives the default behavior, which is equivalent to "error".
873 USE_ERROR_HOOK must be defined.
874
875 A simple exception handling mechanism can be found in "init.scm".
876 A new syntactic form is introduced:
877
878 (catch <expr returned exceptionally>
879 <expr1> <expr2> ... <exprN>)
880
881 "Catch" establishes a scope spanning multiple call-frames
882 until another "catch" is encountered.
883
884 Exceptions are thrown with:
885
886 (throw "message")
887
888 If used outside a (catch ...), reverts to (error "message").
889
890 Example of use:
891
892 (define (foo x) (write x) (newline) (/ x 0))
893
894 (catch (begin (display "Error!\n") 0)
895 (write "Before foo ... ")
896 (foo 5)
897 (write "After foo"))
898
899 The exception mechanism can be used even by system errors, by
900
901 (define *error-hook* throw)
902
903 which makes use of the error hook described above.
904
905 If necessary, the user can devise his own exception mechanism with
906 tagged exceptions etc.
907
908
909 Reader extensions
910 -----------------
911
912 When encountering an unknown character after '#', the user-specified
913 procedure *sharp-hook* (if any), is called to read the expression.
914 This can be used to extend the reader to handle user-defined constants
915 or whatever. It should be a procedure without arguments, reading from
916 the current input port (which will be the load-port).
917
918
919 Colon Qualifiers - Packages
920 ---------------------------
921
922 When USE_COLON_HOOK=1:
923 The lexer now recognizes the construction <qualifier>::<symbol> and
924 transforms it in the following manner (T is the transformation function):
925
926 T(<qualifier>::<symbol>) = (*colon-hook* 'T(<symbol>) <qualifier>)
927
928 where <qualifier> is a symbol not containing any double-colons.
929
930 As the definition is recursive, qualifiers can be nested.
931 The user can define his own *colon-hook*, to handle qualified names.
932 By default, "init.scm" defines *colon-hook* as EVAL. Consequently,
933 the qualifier must denote a Scheme environment, such as one returned
934 by (interaction-environment). "Init.scm" defines a new syntantic form,
935 PACKAGE, as a simple example. It is used like this:
936
937 (define toto
938 (package
939 (define foo 1)
940 (define bar +)))
941
942 foo ==> Error, "foo" undefined
943 (eval 'foo) ==> Error, "foo" undefined
944 (eval 'foo toto) ==> 1
945 toto::foo ==> 1
946 ((eval 'bar toto) 2 (eval 'foo toto)) ==> 3
947 (toto::bar 2 toto::foo) ==> 3
948 (eval (bar 2 foo) toto) ==> 3
949
950 If the user installs another package infrastructure, he must define
951 a new 'package' procedure or macro to retain compatibility with supplied
952 code.
953
954 Note: Older versions used ':' as a qualifier. Unfortunately, the use
955 of ':' as a pseudo-qualifier in existing code (i.e. SLIB) essentially
956 precludes its use as a real qualifier.
957
958
959
960
961
962
963
964