PageRenderTime 45ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/doc/intro.tex

http://github.com/barak/scheme2c
LaTeX | 431 lines | 376 code | 55 blank | 0 comment | 0 complexity | 5059307acc798903add03adcda09f525 MD5 | raw file
Possible License(s): Unlicense
  1. \documentclass[11pt]{article}
  2. \usepackage{fullpage}
  3. \usepackage{parskip}
  4. \usepackage{newcent}
  5. \usepackage{s2c}
  6. \title{An Introduction to \StoC\ in 19 Prompts}
  7. \author{Joel F. Bartlett}
  8. \date{12 August 1988}
  9. \begin{document}
  10. \maketitle
  11. \StoC\ is an implementation of the language Scheme. Besides
  12. the usual interpreter, the implementation includes an unusual
  13. compiler which compiles Scheme to C. This allows stand-alone
  14. programs and programs combining Scheme and other programming
  15. languages. The implementation is also highly portable and when
  16. combined with a good C compiler, fairly efficient.
  17. Please consider this annotated typescript an invitation to try it.
  18. Questions and comments are encouraged.
  19. \begin{small}
  20. \begin{quote}
  21. \begin{verbatim}
  22. alerion 1 >man s2ci
  23. S2CI(1) UNIX Programmer's Manual S2CI(1)
  24. NAME
  25. s2ci - Scheme interpreter
  26. SYNTAX
  27. s2ci [ option ]
  28. DESCRIPTION
  29. The s2ci command invokes a Scheme interpreter. The language
  30. accepted by ...
  31. \end{verbatim}
  32. \end{quote}
  33. \end{small}
  34. Using your favorite editor, create a file containing the Fibonacci
  35. function. Scheme code is generally expected to be in files ending
  36. with ``.sc''.
  37. \begin{small}
  38. \begin{quote}
  39. \begin{verbatim}
  40. alerion 2>more fib.sc
  41. ;;; (FIB n) returns the Fibonacci number for n.
  42. (module fib)
  43. (define (FIB n)
  44. (cond ((> n 1) (+ (fib (- n 1)) (fib (- n 2))))
  45. ((= n 1) 1)
  46. ((= n 0) 0)
  47. (else (error 'FIB "Argument is out of range: ~s" n))))
  48. \end{verbatim}
  49. \end{quote}
  50. \end{small}
  51. A ``;'' indicates that the rest of the line is a comment. The form
  52. \texttt{(module fib)}, which must be the first form in the file, indicates
  53. that the functions in the file should be part of the module ``fib''.
  54. Typically the module name is the file name (less the ``.sc'' extension)
  55. of the source file.
  56. \begin{small}
  57. \begin{quote}
  58. \begin{verbatim}
  59. alerion 3 >s2ci
  60. SCHEME->C -- 08aug88jfb -- Copyright 1988 Digital Equipment Corporation
  61. > (load "fib.sc")
  62. MODULE form ignored
  63. FIB
  64. "fib.sc"
  65. \end{verbatim}
  66. \end{quote}
  67. \end{small}
  68. \texttt{(load "\textnormal{\emph{file-name}}")} loads a file into the
  69. interpreter. Each
  70. form in the file is evaluated and the result is printed on the
  71. standard output device. Note that the module form is currently
  72. ignored by the interpreter. Another way to load the file is to use
  73. \texttt{(loade "\textnormal{\emph{file-name}}")} which will also echo the text read from
  74. the file onto the standard output file. Since \texttt{FIB} was defined
  75. when this function was evaluated, a warning message is printed:
  76. \begin{small}
  77. \begin{quote}
  78. \begin{verbatim}
  79. > (loade "fib.sc")
  80. ;;; (FIB n) returns the Fibonacci number for n.
  81. (module fib)
  82. MODULE form ignored
  83. (define (FIB n)
  84. (cond ((> n 1) (+ (fib (- n 1)) (fib (- n 2))))
  85. ((= n 1) 1)
  86. ((= n 0) 0)
  87. (else (error 'FIB "Argument is out of range: ~s" n))))
  88. ***** FIB is redefined
  89. FIB
  90. "fib.sc"
  91. > (fib 1)
  92. 1
  93. > (fib 2)
  94. 1
  95. > (fib 0)
  96. 0
  97. > (fib -1)
  98. ***** FIB Argument is out of range: -1
  99. \end{verbatim}
  100. \end{quote}
  101. \end{small}
  102. (\texttt{trace} \emph{function} ...) allows one or more functions to be
  103. traced. (\texttt{untrace} \emph{function} ...) removes tracing from selected
  104. functions, and (\texttt{untrace}) removes tracing from all functions.
  105. \begin{small}
  106. \begin{quote}
  107. \begin{verbatim}
  108. > (trace fib)
  109. (FIB)
  110. > (fib 5)
  111. (FIB 5)
  112. (FIB 4)
  113. (FIB 3)
  114. (FIB 2)
  115. (FIB 1)
  116. ==> 1
  117. (FIB 0)
  118. ==> 0
  119. ==> 1
  120. (FIB 1)
  121. ==> 1
  122. ==> 2
  123. (FIB 2)
  124. (FIB 1)
  125. ==> 1
  126. (FIB 0)
  127. ==> 0
  128. ==> 1
  129. ==> 3
  130. (FIB 3)
  131. (FIB 2)
  132. (FIB 1)
  133. ==> 1
  134. (FIB 0)
  135. ==> 0
  136. ==> 1
  137. (FIB 1)
  138. ==> 1
  139. ==> 2
  140. ==> 5
  141. 5
  142. > (untrace)
  143. (FIB)
  144. \end{verbatim}
  145. \end{quote}
  146. \end{small}
  147. \texttt{(bpt \textnormal{\emph{function}})} sets a breakpoint on function entry and
  148. exit. At the function call, the arguments are in \texttt{*args*}
  149. which may be changed. After completing inspection, type \^D to
  150. evaluate the function. On function exit, the result is in
  151. \texttt{*result*} and the program stops for inspection. To
  152. continue with that result, one types \^D or \texttt{(proceed)}. A
  153. different result may be returned by entering \texttt{(proceed
  154. \textnormal{\emph{expression}})}.
  155. \texttt{(unbpt \textnormal{\emph{function ...}})} removes
  156. breakpoints from selected functions, and \texttt{(unbpt)} removes
  157. all breakpoints. While at a breakpoint, one may return to the
  158. ``top-level'' interpreter by executing the function
  159. \texttt{(top-level)}.
  160. \begin{small}
  161. \begin{quote}
  162. \begin{verbatim}
  163. > (bpt fib)
  164. FIB
  165. > (fib 1)
  166. 0 -calls - (FIB 1)
  167. 0- *args*
  168. (1)
  169. 0- ^D
  170. 0 -returns- 1
  171. 0- *result*
  172. 1
  173. 0- (proceed 23.7)
  174. 23.7
  175. 0- ^D
  176. 23.7
  177. > (unbpt fib)
  178. (FIB)
  179. \end{verbatim}
  180. \end{quote}
  181. \end{small}
  182. Breakpoints may also have a boolean function supplied which decides
  183. whether to take the breakpoint. Needless to say, such a function can
  184. also do things like count the number of times the function is called.
  185. \begin{small}
  186. \begin{quote}
  187. \begin{verbatim}
  188. > (bpt fib (lambda (n) (set! fibcnt (+ 1 fibcnt)) #f))
  189. FIB
  190. > (set! fibcnt 0)
  191. 0
  192. > (fib 5)
  193. 5
  194. > fibcnt
  195. 15
  196. > (unbpt fib)
  197. (FIB)
  198. > (fib 20)
  199. 6765
  200. >^D
  201. alerion 4 >
  202. \end{verbatim}
  203. \end{quote}
  204. \end{small}
  205. Since \texttt{(fib 20)} took a while to compute, it might be a good idea
  206. to compile it, so the interpreter is exited, and an augmented version
  207. of the interpreter is created which has a compiled version of FIB.
  208. \begin{small}
  209. \begin{quote}
  210. \begin{verbatim}
  211. alerion 4 >man s2cc
  212. S2CC(1) UNIX Programmer's Manual S2CC(1)
  213. NAME
  214. s2cc - Scheme to C compiler
  215. SYNTAX
  216. s2cc [ option ] ... file ...
  217. DESCRIPTION
  218. The s2cc command invokes a Scheme compiler which accepts the
  219. language ...
  220. alerion 5 >s2cc -i -o sc+fib fib.sc
  221. fib.sc:
  222. fib.c:
  223. SC-TO-C.c:
  224. alerion 6 >sc+fib
  225. SCHEME->C -- 08aug88jfb -- Copyright 1988 Digital Equipment Corporation
  226. > fib
  227. #*PROCEDURE*
  228. > (fib 1)
  229. 1
  230. > (fib 0)
  231. 0
  232. > (fib 20)
  233. 6765
  234. >^D
  235. \end{verbatim}
  236. \end{quote}
  237. \end{small}
  238. Now for a little different example, where a Scheme version of the
  239. shell command ``echo'' is created as a stand alone program. The module
  240. form now has an additional component, \texttt{(main do-echo)}, which
  241. indicates that the \texttt{do-echo} function is the program main. As with
  242. any other UNIX program, the main is called with the arguments from
  243. the shell. This is done in Scheme by providing the main with a list
  244. of strings which are the arguments.
  245. \begin{small}
  246. \begin{quote}
  247. \begin{verbatim}
  248. alerion 7 >more echo.sc
  249. ;;; ECHO - Echo Arguments
  250. ;;;
  251. ;;; % echo [options] [args]
  252. ;;;
  253. ;;; Option:
  254. ;;; -n newlines are not added to output
  255. (module echo (main do-echo))
  256. (define (DO-ECHO clargs)
  257. (let ((nonewline (and (cdr clargs) (equal? (cadr clargs) "-n"))))
  258. (do ((args (if nonewline (cddr clargs) (cdr clargs)) (cdr args)))
  259. ((null? args)
  260. (unless nonewline (newline)))
  261. (display (car args))
  262. (if (cdr args) (display " ")))))
  263. alerion 8>
  264. \end{verbatim}
  265. \end{quote}
  266. \end{small}
  267. The program is loaded into the interpreter and tested with a few
  268. possible values. Note that the first argument is always the program
  269. name.
  270. \begin{small}
  271. \begin{quote}
  272. \begin{verbatim}
  273. alerion 8>s2ci
  274. SCHEME->C -- 08aug88jfb -- Copyright 1988 Digital Equipment Corporation
  275. > (load "echo.sc")
  276. MODULE form ignored
  277. DO-ECHO
  278. "echo.sc"
  279. > (do-echo '("echo" "-n" "a"))
  280. a#F
  281. > (do-echo '("echo" "a" "b" "c"))
  282. a b c
  283. #F
  284. > (do-echo '("echo" "-n" "a" "b" "c"))
  285. a b c#F
  286. > (do-echo '("echo"))
  287. #F
  288. > ^D
  289. \end{verbatim}
  290. \end{quote}
  291. \end{small}
  292. Now, compile it and build a stand-alone program:
  293. \begin{small}
  294. \begin{quote}
  295. \begin{verbatim}
  296. alerion 9 >s2cc -o scheme-echo echo.sc
  297. echo.sc:
  298. alerion 10 >scheme-echo *.sc
  299. counter.sc echo.sc fib.sc fsm2.sc fsmexample.sc hello.sc
  300. alerion 11 >scheme-echo -n *.sc
  301. counter.sc echo.sc fib.sc fsm2.sc fsmexample.sc hello.scalerion 12 >
  302. \end{verbatim}
  303. \end{quote}
  304. \end{small}
  305. The next example shows the interface to routines written in other
  306. languages by building a program which uses the routines in the C library
  307. (described in the \emph{ULTRX-32 Programmer's Manual}) to print out the
  308. current Greenwich mean time.
  309. \begin{small}
  310. \begin{quote}
  311. \begin{verbatim}
  312. alerion 12 >more gmt.sc
  313. ;;; Print current GMT on standard output.
  314. (module gmt (main gmt))
  315. (define-c-external (time pointer) int "time")
  316. (define-c-external (gmtime pointer) pointer "gmtime")
  317. (define-c-external (asctime pointer) pointer "asctime")
  318. (define (GMT clargs)
  319. (let ((current-time (make-string 4)))
  320. (time current-time)
  321. (display (c-string->string (asctime (gmtime current-time))))))
  322. \end{verbatim}
  323. \end{quote}
  324. \end{small}
  325. The procedure \emph{time} stores the number of seconds since GMT. Jan.
  326. 1. 1970 in the location referenced by \emph{pointer}. \emph{gmtime}
  327. converts that value to a \emph{tm} structure and returns a pointer to
  328. it. \emph{asctime} then converts the referenced \emph{tm} structure to a
  329. string and returns a pointer to it. In order to display it,
  330. \emph{c-string->string} is used to make a Scheme copy of the string.
  331. \begin{small}
  332. \begin{quote}
  333. \begin{verbatim}
  334. alerion 13 >s2cc -o gmt gmt.sc
  335. gmt.sc:
  336. alerion 14 >gmt
  337. Thu Aug 11 22:19:08 1988
  338. \end{verbatim}
  339. \end{quote}
  340. \end{small}
  341. To allay any doubts that this implementation might not be
  342. Scheme, we conclude with the following ``proof by example'',
  343. produced by Eugene Kohlbecker:
  344. \begin{small}
  345. \begin{quote}
  346. \begin{verbatim}
  347. alerion 15 >more mondo.sc
  348. (module mondo (main call-mondo))
  349. (define (call-mondo clargs) (mondo-bizarro) (newline))
  350. (define (mondo-bizarro)
  351. (let ((k (call-with-current-continuation (lambda (c) c))))
  352. (display 1)
  353. (call-with-current-continuation (lambda (c) (k c)))
  354. (display 2)
  355. (call-with-current-continuation (lambda (c) (k c)))
  356. (display 3)))
  357. alerion 16 >s2ci
  358. SCHEME-C -- 08aug88jfb -- Copyright 1988 Digital Equipment Corporation
  359. > (load "mondo.sc")
  360. MODULE form ignored
  361. CALL-MONDO
  362. MONDO-BIZARRO
  363. "mondo.sc"
  364. > (call-mondo '())
  365. 11213
  366. #F
  367. > ^D
  368. alerion 17 >s2cc -o mondo mondo.sc
  369. mondo.sc:
  370. alerion 18 >mondo
  371. 11213
  372. alerion 19 >logout
  373. \end{verbatim}
  374. \end{quote}
  375. \end{small}
  376. \end{document}