comp-ex.texi 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. @node Example Pre-Scheme compiler usage
  2. @section Example Pre-Scheme compiler usage
  3. The @file{ps-compiler/compile-vm.scm},
  4. @file{ps-compiler/compile-gc.scm}, and
  5. @file{ps-compiler/compile-vm-no-gc.scm} files give examples of running
  6. the Pre-Scheme compiler. They are Scheme48 @embedref{Command programs,
  7. command programs}, to be loaded into the @code{exec} package after
  8. having already loaded the Pre-Scheme compiler. @file{compile-vm.scm} &
  9. @file{compile-vm-no-gc.scm} generate a new @file{scheme48vm.c} in the
  10. @file{scheme/vm/} directory --- @file{compile-vm.scm} includes the
  11. garbage collector, while @file{compile-vm-no-gc.scm} does not
  12. @footnote{The actual distribution of Scheme48 separates the garbage
  13. collector and the main virtual machine.} ---, and @file{compile-gc.scm}
  14. generates a new @file{scheme48heap.c}, @file{scheme48read-image.c}, &
  15. @file{scheme48write-image.c} in the @file{scheme/vm/} directory.
  16. Here is a somewhat simpler example. It assumes a pre-built image with
  17. the Pre-Scheme compiler loaded is in the @file{ps-compiler.image} file
  18. in the current directory (@pxref{Invoking the Pre-Scheme compiler},
  19. where there is a description of how to dump an image with the
  20. Pre-Scheme compiler loaded).
  21. @example
  22. % ls
  23. hello.scm packages.scm ps-compiler.image
  24. % cat hello.scm
  25. (define (main argc argv)
  26. (if (= argc 2)
  27. (let ((out (current-output-port)))
  28. (write-string "Hello, world, " out)
  29. (write-string (vector-ref argv 1) out)
  30. (write-char #\! out)
  31. (newline out)
  32. 0)
  33. (let ((out (current-error-port)))
  34. (write-string "Usage: " out)
  35. (write-string (vector-ref argv 0) out)
  36. (write-string " <user>" out)
  37. (newline out)
  38. (write-string " Greets the world & <user>." out)
  39. (newline out)
  40. -1)))
  41. % cat packages.scm
  42. (define-structure hello (export main)
  43. (open prescheme)
  44. (files hello))
  45. % scheme48 -i ps-compiler.image
  46. heap size 3000000 is too small, using 4770088
  47. Welcome to Scheme 48 1.3 (Pre-Scheme)
  48. Copyright (c) 1993-2005 by Richard Kelsey and Jonathan Rees.
  49. Please report bugs to scheme-48-bugs@@s48.org.
  50. Get more information at http://www.s48.org/.
  51. Type ,? (comma question-mark) for help.
  52. > (prescheme-compiler 'hello '("packages.scm") 'hello-init "hello.c")
  53. packages.scm
  54. hello.scmChecking types
  55. main : ((integer **char) -> integer)
  56. In-lining single-use procedures
  57. Call Graph:
  58. <procedure name>
  59. <called non-tail-recursively>
  60. <called tail-recursively>
  61. main (exported)
  62. Merging forms
  63. Translating
  64. main
  65. #@{Unspecific@}
  66. > ,exit
  67. % cat hello.c
  68. #include <stdio.h>
  69. #include "prescheme.h"
  70. long main(long, char**);
  71. long main(long argc_0X, char **argv_1X)
  72. @{
  73. FILE * out_3X;
  74. FILE * out_2X;
  75. @{ if ((1 == argc_0X)) @{
  76. out_2X = stdout;
  77. ps_write_string("Hello, world, ", out_2X);
  78. ps_write_string((*(argv_1X + 1)), out_2X);
  79. @{ long ignoreXX;
  80. PS_WRITE_CHAR(33, out_2X, ignoreXX) @}
  81. @{ long ignoreXX;
  82. PS_WRITE_CHAR(10, out_2X, ignoreXX) @}
  83. return 0;@}
  84. else @{
  85. out_3X = stderr;
  86. ps_write_string("Usage: ", out_3X);
  87. ps_write_string((*(argv_1X + 0)), out_3X);
  88. ps_write_string(" <user>", out_3X);
  89. @{ long ignoreXX;
  90. PS_WRITE_CHAR(10, out_3X, ignoreXX) @}
  91. ps_write_string(" Greets the world & <user>.", out_3X);
  92. @{ long ignoreXX;
  93. PS_WRITE_CHAR(10, out_3X, ignoreXX) @}
  94. return -1;@}@}
  95. @}
  96. % @end example