
1: @node Programming in Gauche, Core syntax, Concepts, Top 2: @chapter Programming in Gauche 3: @c NODE Gauche?Ç¤Î¥×¥í¥°???? 4: @menu 5: * Invoking Gosh:: 6: * Interactive development:: 7: * Writing Scheme scripts:: 8: * Debugging:: 9: * Platform-dependent features:: 10: * Profiling and tuning:: 11: * Writing Gauche modules:: 12: * Using extension packages:: 13: @end menu 14: 15: @node Invoking Gosh, Interactive development, Programming in Gauche, Programming in Gauche 16: @section Invoking Gosh 17: @c NODE Gauche???????@c EN 18: Gauche can be used either as an independent Scheme interpreter 19: or as an embedded Scheme library. The interpreter 20: which comes with Gauche distribution is a program named @code{gosh}. 21: @c JP 22: Gauche????????Scheme???󥿥ץ꥿?Ȥ??Ƥ⡢??ß¹??ߤ?cheme?饤?Ö¥??Ȥ??Ƥ?Ȥ????Ȥ??Ǥ??Þ¤???Gauche?Υǥ????È¥??塼?????ˤϡ?@code{gosh}?Ȥ??? 23: ???󥿥ץ꥿???°???Ƥ??Þ¤??? 24: @c COMMON 25: 26: @deftp {Program} gosh [options] [scheme-file arg @dots{}] 27: @c EN 28: Gauche's interpreter. Without @var{scheme-file}, @code{gosh} works 29: interactively, i.e. it reads a Scheme expression from the standard input, 30: evaluates it, and prints the result, and repeat that until it reads EOF or 31: is terminated. 32: @c JP 33: Gauche?Î¥??󥿥ץ꥿?Ǥ??? @var{scheme-file}??Í¿?????ʤ????? 34: @code{gosh}?Ï¥??󥿥饯?Æ¥??Ö¤????Þ¤??????ʤ???ɸ???????heme????ß¹??ß¡? 35: ????ɾ????Æ·?????Ȥ???????ư????EOF??फ???Ū?˽?λ??????????³???Þ¤??? 36: @c COMMON 37: 38: @c EN 39: If @code{gosh} is invoked without @var{scheme-file}, but the input 40: is not a terminal, it enters read-eval-print loop but not writes 41: out a prompt while waiting input form. This is useful when you 42: pipe Scheme program into @code{gosh}. You can force this behavior 43: or suppress this behavior by @code{-b} and @code{-i} options. 44: @c JP 45: ?⤷@var{scheme-file}??Í¿?????Ƥ??餺????????????ü??Ǥʤ????? 46: @code{gosh}??ead-eval-print?롼?פ???????????????κݤ˥ץ??ץȤ??????Þ¤??󡣤???Scheme??????פ?var{gosh}?˽????????????Ǥ??? 47: @code{-b}?Þ¤???code{-i}???×¥????Ǥ?????????Ū??n?Þ¤???ff?Ǥ??Þ¤??? 48: @c COMMON 49: 50: @c EN 51: If @var{scheme-file} is specified, @code{gosh} runs it as a 52: Scheme program and exit. 53: @xref{Writing Scheme scripts}, for details. 54: @c JP 55: @var{scheme-file}??Í¿??????????@code{gosh}?Ϥ???Scheme?×¥í¥°???Ȥ????í¡¼?ɤ?????λ???Þ¤???????????ؤ??Ƥ?ref{Writing Scheme scripts}?ò»²¾È¤??????????? 56: @c COMMON 57: @end deftp 58: 59: @c EN 60: @subheading Command-line options 61: @c JP 62: @subheading ???Þ¥?饤?󥪥ץ??? 63: @c COMMON 64: 65: @c EN 66: The following command line options are recognized by @code{gosh}. 67: The first command line argument which doesn't begin with `@minus{}' 68: is recognized as the script file. If you want to specify a file 69: that begins with a minus sign, use a dummy option `@code{--}'. 70: @c JP 71: @code{gosh}?ˤϰʲ??Τ褦?Ê¥??Þ¥?饤?󥪥ץ????????????? 72: `@minus{}' ?ǻϤޤ????ǽ????Þ¥?饤??ô¤¬¥??????È¥Õ¥?????ǧ?????????? 73: ???????È¥Õ¥??????? `@minus{}' ?ǻϤޤäƤ???ǽ?????????????ß¡????×¥??? 74: `@code{--}' ?ò¥¹¥????È¥Õ¥??????????Ö¤??Ʋ??????? 75: @c COMMON 76: 77: @deftp {Command Option} -I path 78: @c EN 79: Prepends @var{path} to the load path list. 80: You can specify this option more than once to add multiple paths. 81: @c JP 82: @var{path}????ɥѥ??Υꥹ?Ȥκǽ??ä??Þ¤??????Î¥??×¥????????????Þ¤??? 83: @c COMMON 84: @end deftp 85: 86: @deftp {Command Option} -A path 87: @c EN 88: Appends @var{path} to the tail of the load path list. 89: You can specify this option more than once to add multiple paths. 90: @c JP 91: @var{path}????ɥѥ??Υꥹ?Ȥ??˲ä??Þ¤??????Î¥??×¥????????????Þ¤??? 92: @c COMMON 93: @end deftp 94: 95: @deftp {Command Option} -q 96: @c EN 97: Makes @code{gosh} not to load the default initialization file. 98: @c JP 99: @code{gosh}????ư???Ë¥????Æ¥??????Õ¥??????í¡¼?ɤ??ʤ??褦?ˤ??Þ¤??? 100: @c COMMON 101: @end deftp 102: 103: @deftp {Command Option} -V 104: @c EN 105: Prints the @code{gosh} version and exits. 106: @c JP 107: @code{gosh}?ΥС???????????ƽ?λ???Þ¤??? 108: @c COMMON 109: @end deftp 110: 111: @deftp {Command Option} -u module 112: @c EN 113: Use @var{module}. Before starting execution of @var{scheme-file} or entering 114: the read-eval-print loop, the specified module is @var{use}d, i.e. 115: it is loaded and imported (@xref{Defining and selecting modules}, for details of @code{use}). 116: You can specify this option more than once to use multiple modules. 117: @c JP 118: ??ư?å¡¢???󥿥饯?Æ¥??Ö¤?ead-eval-print?롼?פ???????⤷????var{scheme-file} 119: ????ɤ?????var{module}??se''???Þ¤????Ĥޤꤽ?Υ⥸?塼?뤬?í¡¼?ɤ????????Ȥ???????(@code{use}?ξܺ٤ˤĤ??Ƥ?ref{Defining and selecting modules}?ò»²¾È¤??Ʋ???????) 120: ???Î¥??×¥????????????Þ¤??? 121: @c COMMON 122: @end deftp 123: 124: @deftp {Command Option} -l file 125: @c EN 126: Load @var{file} before starting execution of @var{scheme-file} or entering 127: the read-eval-print loop. The file is loaded in the same way as 128: @code{load} (@xref{Loading Scheme file}). 129: You can specify this option more than once to load multiple files. 130: @c JP 131: ??ư?å¡¢???󥿥饯?Æ¥??Ö¤?ead-eval-print?롼?פ???????⤷????var{scheme-file} 132: ????ɤ?????var{file}????ɤ??Þ¤????í¡¼?ɤξܺ٤ˤĤ??Ƥ?ref{Loading Scheme file} 133: ?ò»²¾È¤??Ʋ??????????Î¥??×¥????????????Þ¤??? 134: @c COMMON 135: @end deftp 136: 137: @deftp {Command Option} -e scheme-expression 138: @c EN 139: Evaluate @var{scheme-expression} 140: before starting execution of @var{scheme-file} or entering 141: the read-eval-print loop. Evaluation is done in the 142: @var{interaction-environment} (@xref{Eval and repl}). 143: You can specify this option more than once to evaluate multiple expressions. 144: @c JP 145: ??ư?å¡¢???󥿥饯?Æ¥??Ö¤?ead-eval-print?롼?פ???????⤷????var{scheme-file} 146: ????ɤ?????var{scheme-expression}?????Þ¤???ɾ???@var{interaction-environment}??ǹԤ??Þ¤?(@ref{Eval and repl}?????? 147: ???Î¥??×¥????????????Þ¤??? 148: @c COMMON 149: @end deftp 150: 151: @deftp {Command Option} -E scheme-expression 152: @c EN 153: Same as -e, except the @var{scheme-expression} is read 154: as if it is surrounded by parenthesis. For example: 155: @c JP 156: ???×¥???-e?Ȥۤ?????Ǥ?????@var{scheme-expression}?????줬???ǰϤޤ????뤫?Τ褦??ɤޤ????? 157: ???Î¥??×¥????????????Þ¤?????? 158: @c COMMON 159: @example 160: % gosh -umath.const -E"print (sin (* pi/180 15))" -Eexit 161: 0.25881904510252074 162: @end example 163: @end deftp 164: 165: @deftp {Command Option} -b 166: @c EN 167: Batch. Does not print prompts even if the input is a terminal. 168: @c JP 169: ?Хå??????ü??Ǥ??äƤ????ץȤ???ʤ??褦?ˤ??Þ¤??? 170: @c COMMON 171: @end deftp 172: 173: @deftp {Command Option} -i 174: @c EN 175: Interactive. Print prompts even if the input is not a terminal. 176: @c JP 177: ???󥿥饯?Æ¥??Ö¡?????ü???????Ƥ????ץȤ???褦?ˤ??Þ¤??? 178: @c COMMON 179: @end deftp 180: 181: @deftp {Command Option} -f compiler-option 182: @c EN 183: This option controls compiler and runtime behavior. For now we have 184: following options available: 185: @table @asis 186: @item no-inline 187: Prohibits the compiler from inlining procedures and constants. Equivalent to 188: no-inline-globals, no-inline-locals and no-inline constants combined. 189: @item no-inline-globals 190: Prohibits the compiler from inlining global procedures. 191: @item no-inline-locals 192: Prohibits the compiler from inlining local procedures. 193: @item no-inline-constants 194: Prohibits the compiler from inlining constants. 195: @item load-verbose 196: Reports whenever a file is loaded. 197: @item no-source-info 198: Don't keep source information for debugging. Consumes less memory. 199: @item case-fold 200: Ignore case for symbols as specified in R5RS. 201: (It is distinguished by default). @xref{Case-sensitivity}. 202: @item test 203: Adds "@code{../src}" and "@code{../lib}" to the load path before loading 204: initialization file. This is useful when you want to test the 205: compiled @code{gosh} interpreter inside source tree, without installing it. 206: @end table 207: @c JP 208: ???Î¥??×¥????Ï¥??????????????ư???ƶ????Þ¤??? 209: ???ΤȤ??í¡¢???Î¥??×¥????Τߤ?@var{compiler-option}?Ȥ???????? 210: @table @asis 211: @item no-inline 212: ???Î¥???????????Þ¤??󡣤??Î¥??×¥????ϰʲ???no-inline-globals 213: no-inline-locals ???? no-inline-constants ????Ë»?ꤷ???Τ?????? 214: @item no-inline-globals 215: ???lobal)?Ø¿?????????????ʤ??Þ¤???@item no-inline-locals 216: ?ɽ?ocal)?Ø¿?????????????ʤ??Þ¤???@item no-inline-constants 217: ????????????ʤ??Þ¤???@item no-source-info 218: ?ǥХå??Τ??????????Õ¥???????????ó¡£¥??????̤Ͼ??????ʤ????? 219: @item load-verbose 220: ?Õ¥????뤬?í¡¼?ɤ??????ˤ?????𤷤ޤ??? 221: @item case-fold 222: R5RS?˼???????ê¡¢??????ʸ????????Þ¤???(?Ç¥Õ¥????ǤϤ????϶???????)?? @ref{Case-sensitivity} ?ò»²¾È¤??Ʋ??????? 223: @item test 224: "@code{../src}" ??"@code{../lib}" ?ò¡¢½????Õ¥???????????í¡¼?ɥѥ??˲ä??Þ¤??????????????줿@code{gosh}?ò¥¤¥ó¥¹¥È¡??뤻???????????ĥ꡼????¹Ԥ??Ƥߤ??????? 225: @end table 226: @c COMMON 227: @end deftp 228: 229: @deftp {Command Option} -p profiler-option 230: @c EN 231: Turn on the profiler. The following @var{profiler-option} is recognized: 232: @c JP 233: ?×¥???????Í???Þ¤????ʲ??Τ褦??var{profiler-option}?? 234: ???ΤȤ??í¥µ?Ý¡??Ȥ??????Þ¤??? 235: @c COMMON 236: 237: @table @code 238: @item time 239: @c EN 240: Records and reports time spent on function calls and number of times 241: each function is called. 242: @c JP 243: ?Ø¿??ñ¤µ¤ì¤¿???֤ȡ??Æ´Ø¿ô¤¬¸Æ¤Ð¤ì¤¿???????????Þ¤??? 244: @c COMMON 245: @item load 246: @c EN 247: Records and reports time spent on loading each modules. 248: Useful to tune start-up time of the scripts. 249: (Results are in elapsed time). 250: @c JP 251: ?ƥ⥸?塼???í¡¼?ɤ????ˤ????ä????Ö¤?????????Þ¤??? 252: ???????Ȥε?ư???Ö¤?塼?󤹤????? 253: (?·в??Ö¤???𤵤???)?? 254: @c COMMON 255: @end table 256: 257: @c EN 258: See @ref{Using profiler} for the details of the profiler. 259: @c JP 260: ?ܤ?????ref{Using profiler}?ò»²¾È¤??Ʋ??????? 261: @c COMMON 262: @end deftp 263: 264: 265: @deftp {Command Option} @code{--} 266: @c EN 267: When @code{gosh} sees this option, it stops processing the options 268: and takes next command line argument as a script file. It is useful 269: in case if you have a script file that begins with a minus sign, 270: although it is not generally recommended. 271: @c JP 272: ???Î¥??×¥????˽вñ¤¦¤È¡?@code{gosh}?Ï¥??×¥????β????????μ??ΰ? 273: ̵???var{scheme-file}?Ǥ?????????Þ¤???@var{scheme-file}???Þ¥??Ê¥????? 274: ?ϤޤäƤ?????ɬ??Ǥ??? 275: @c COMMON 276: @end deftp 277: 278: @c EN 279: The options -I, -A, -l, -u, -e and -E are processes in the order 280: of appearance. For example, adding a load path by -I affects the 281: -l and -u option after it but not before it. 282: @c JP 283: ???×¥???-I, -A, -l, -u, -e ?Ú¤?-E ?Ï¡??????????Þ¥?饤???????и?????????????????????С?-I?ˤ???ä????í¡¼?ɥѥ????????ߤ?l?????×¥????˱ƶ????Þ¤???????????Τ??ˤϱƶ?????Þ¤???@c COMMON 284: 285: @c EN 286: @subheading Environment variables 287: @c JP 288: @subheading ?Ķ???? COMMON 289: 290: @c EN 291: The following environment variables are recognized: 292: @c JP 293: ?ʲ??δĶ??????????Þ¤??? 294: @c COMMON 295: 296: @deftp {Environment variable} GAUCHE_LOAD_PATH 297: @c EN 298: You can specify additional load paths by this environment 299: variable, delimiting the paths by '@code{:}'. 300: The paths are appended before the system default load paths. 301: @c JP 302: ???δĶ??????Æ¡???ä??????ɥѥ???????Þ¤??? 303: ?Ñ¥???'@code{:}' ?Ƕ??????? 304: ????Ñ¿???Æ»?ꤵ?줿?Ñ¥??Ï¥????Æ¥??Ç¥Õ¥????Î¥í¡¼?ɥѥ??????Ï¢?뤵?????? 305: @c COMMON 306: @end deftp 307: 308: @deftp {Environment variable} GAUCHE_DYNLOAD_PATH 309: @c EN 310: You can specify additional load paths for dynamically loaded 311: objects by this environment variable, delimiting the paths by '@code{:}'. 312: The paths are appended before the system default load paths. 313: @c JP 314: ????Ñ¿???Æ¡?ưŪ?Ë¥í¡¼?ɤ??륪?Ö¥???????Ѥ?É²Ã¥í¡¼?ɥѥ?????Ǥ??Þ¤????Ñ¥???'@code{:}' ?Ƕ??????? 315: ????Ñ¿???Æ»?ꤵ?줿?Ñ¥??Ï¥????Æ¥??Ç¥Õ¥????Î¥í¡¼?ɥѥ??????Ï¢?뤵?????? 316: @c COMMON 317: @end deftp 318: 319: @c ---------------------------------------------------------------------- 320: @node Interactive development, Writing Scheme scripts, Invoking Gosh, Programming in Gauche 321: @section Interactive development 322: @c NODE ???󥿥饯?Æ¥??֤ʳ?ȯ 323: 324: @c EN 325: When @code{gosh} is invoked without any script files, 326: it goes into interactive read-eval-print loop. 327: @c JP 328: ???????È¥Õ¥????뤬Ϳ?????ʤ??ä????? 329: @code{gosh}?Ï¥??󥿥饯?Æ¥??Ö¤?ead-eval-print?롼?פ???????? 330: @c COMMON 331: 332: @c EN 333: To exit the interpreter, type EOF (usually Control-D in Unix terminals) 334: or evaluate @code{(exit)}. 335: @c JP 336: ???󥿥ץ꥿????????Ï¡?EOFʸ??xü??Ǥ?̾?ntrol-D)?ò¥¿¥??פ??뤫?? 337: @code{(exit)}?????Þ¤??? 338: @c COMMON 339: 340: @c EN 341: In interactive session, @code{gosh} loads @code{gauche.interactive} 342: module (@xref{Interactive session}) for the 343: convenience. The module also loads a file @file{.gaucherc} under 344: the user's home directory if it exists. 345: You may put settings there that would help interactive debugging. 346: (As of Gauche release 0.7.3, 347: @file{.gaucherc} is no longer loaded when @code{gosh} is run 348: in script mode.) 349: @c JP 350: ???󥿥饯?Æ¥??Ö¥??Ã¥????Ǥϡ?@code{gosh}??@code{gauche.interactive}?⥸?塼???í¡¼?ɤ??Þ¤? 351: (@ref{Interactive session}?????? 352: ???Υ⥸?塼???Þ¤????桼?????Î¥Û¡??????쥯?È¥?@file{.gaucherc}?Ȥ??? 353: ?Õ¥????뤬?????????ɤ??Þ¤??? 354: ???󥿥饯?Æ¥??֥ǥХå???????ò¤½¤??˽ñ¤¤¤Æ¤??????Ȥ??Ǥ??Þ¤??? 355: (Gauche release 0.7.3???é¡¢@file{.gaucherc}??osh?????????ȥ⡼?ɤ???ư???줿????ɤޤ????ʤ???????) 356: @c COMMON 357: 358: @c EN 359: I recommend you to run @code{gosh} inside Emacs, for it has 360: rich features useful to interact with internal Scheme process. 361: Put the following line to your @file{.emacs} file: 362: @example 363: (setq scheme-program-name "gosh -i") 364: @end example 365: And you can run @code{gosh} by @key{M-x run-scheme}. 366: @c JP 367: @code{gosh}??cs???????뤳?Ȥò¤ª´??ᤷ?Þ¤??? 368: Emacs??cheme???Ö¥×¥í¥»?????뤿??Ë??ʵ?ǽ??äƤ??Þ¤??? 369: ???ιԤ?le{.emacs}?˲ä??Ƥ????È¡?@key{M-x run-scheme} ??Emacs?ΥХåե????code{gosh}????????? 370: @example 371: (setq scheme-program-name "gosh -i") 372: @end example 373: @c COMMON 374: 375: @c EN 376: If you want to use multibyte characters in the interaction, 377: make sure your terminal's settings is in sync with @code{gosh}'s 378: internal character encodings. 379: @c JP 380: ???Ķ??Ç¥Þ¥??Ð¥???????????Ï¡?ü???????????Ç¥??ó¥°¤?de{gosh}???????󥳡??Ç¥??󥰤ȹ????????ˤ??Ʋ??????? 381: @c COMMON 382: 383: @c EN 384: Currently @code{gosh} has no fancy features such as 385: line-editing or command history. They'll be added in future. 386: @c JP 387: ???ΤȤ??í¡¢@code{gosh}?Ϲ?Ô½??䥳?Þ¥?Ò¥??È¥??ɤ???ǽ?????Ƥ??Þ¤??ó¡£¾???Ϥ??Τؤ???¤??Ƥ椯?Ǥ??礦?? 388: @c COMMON 389: 390: @c ---------------------------------------------------------------------- 391: @node Writing Scheme scripts, Debugging, Interactive development, Programming in Gauche 392: @section Writing Scheme scripts 393: @c NODE Scheme???????Ȥ?? 394: 395: @c EN 396: When a Scheme program file is given to @code{gosh}, it 397: bounds a global variable @code{*argv*} to the list of the remaining 398: command-line arguments, then loads the Scheme program. 399: If the first line of @var{scheme-file} begins with two character 400: sequence ``@code{#!}'', the entire line is ignored by @code{gosh}. 401: This is useful to write a Scheme program that works as an executable 402: script in unix-like systems. 403: @c JP 404: @code{gosh}?Î¥??Þ¥?饤??cheme?×¥í¥°???Î¥Õ¥???????????줿???? 405: @code{gosh}?Ϥ????ߤΥ??Þ¥?饤???ꥹ?Ȥò¥°¥í¡¼?Ð¥???de{*argv*}???????? 406: Scheme?×¥í¥°??????ɤ??Þ¤????⤷@var{scheme-file}?κǽ??Ô¤?``@code{#!}''?ǻϤޤä??????é¡¢???ιԤ???뤵??????????????Unix?ϤΥ????Æ¥??¹Բ????cheme??????????????Ȥ???????? 407: @c COMMON 408: 409: @c EN 410: Typical Gauche script has the first 411: line like these 412: @example 413: #!/usr/local/bin/gosh 414: @r{or,} 415: #!/usr/bin/env gosh 416: @r{or,} 417: #!/bin/sh 418: :; exec gosh -- $0 "$@@" 419: @end example 420: The second and third form uses a ``shell trampoline'' technique 421: so that the script works as far as @code{gosh} is in the PATH. 422: The third form is useful when you want to pass extra arguments 423: to @code{gosh}, for typically @code{#!}-magic of executable scripts 424: has limitations for the number of arguments to pass the interpreter. 425: @c JP 426: ŵ??Ū??auche???????Ȥκǽ??Ԥϼ??Τ褦?ʤ??Ǥ??? 427: @example 428: #!/usr/local/bin/gosh 429: @r{?Þ¤???} 430: #!/usr/bin/env gosh 431: @r{?Þ¤???} 432: #!/bin/sh 433: :; exec gosh -- $0 "$@@" 434: @end example 435: ??2?Ĥϡ֥????????Ý¥??ץƥ??˥å?????Æ¡?@code{gosh}??PATH?ˤ??????쥯?È¥? 436: ?ɤ????ˤ?????ư?Ǥ??????ˤ??Ƥ??Þ¤???3?????? 437: @code{gosh}?ˤ????Ĥ????Þ¥?饤?󥪥ץ?????????????????? 438: @c COMMON 439: 440: @c EN 441: After the file is successfully loaded, @code{gosh} calls a 442: procedure named `@code{main}' if it is defined in the user module. 443: @code{Main} receives a single argument, a list of command line 444: arguments. Its first element is the script name itself. 445: 446: When @code{main} returns, and its value is an integer, @code{gosh} 447: uses it for exit code of the program. 448: Otherwise, @code{gosh} exits with exit code 70 (@code{EX_SOFTWARE}). 449: This behavior is compatible with the SRFI-22. 450: 451: If the @code{main} procedure is not defined, @code{gosh} exits 452: after loading the script file. 453: @c JP 454: ?Õ¥????뤬????í¡¼?ɤ??줿?é¡¢@code{gosh}??user?⥸?塼?? `@code{main}' ?Ȥ??????????????????뤫?ɤ???Ä´?Ù¡? 455: ???????????????ƤӤޤ???@code{main}?ˤϡ????????Ȥؤΰ??ꥹ?Ȥ? 456: Í£????????Ϥ????????ꥹ?Ȥκǽ????Ï¥??????È¥Õ¥??????Ǥ??? 457: 458: @code{main}?????ͤ?????é¡¢@code{gosh}?Ϥ???ͤ?????Æ¡??????Ȥ??ƽ?λ???Þ¤??? 459: @code{main}????????ͤ????????code{gosh}?Ͻ?λ???Æ¡?????70 460: (@code{EX_SOFTWARE})?ǽ?λ???Þ¤??????Τդ?????RFI-22?ȸߴ??Ǥ??? 461: 462: @code{main}???????????ʤ???@code{gosh}?Ï¥í¡¼?ɸ????Τޤޥ??Æ¡?????0????λ???Þ¤??? 463: @c COMMON 464: 465: @c EN 466: Although you can still write the program 467: main body as toplevel expressions, like shell scripts or Perl scripts, 468: it is much convenient to use this `@code{main}' convention, for 469: you can load the script file interactively to debug. 470: @c JP 471: ?????륹?????Ȥ?rl???????Ȥ?????褦?Ë¡????????ȤΥܥǥ????? 472: ?¹Ԥ???????????Ȥ????Þ¤??????ʤ??? `@code{main}' ??ä??Ë¡???????Ȥ???ᤷ?Þ¤????????????????????Ȥò¥¤¥ó¥¿¥×¥ê¥¿?Ë¥??󥿥饯?Æ¥??Ö¤??í¡¼?ɤ??ƥǥХå????뤳?Ȥ????Þ¤??? 473: @c COMMON 474: 475: 476: @defvar *argv* 477: @c EN 478: The program arguments passed to the Scheme script is bound to this 479: variable. If @code{gosh} is invoked in an interactive mode, this 480: variable always contains @code{()}. 481: 482: STk uses this variable to pass the command line argument as well, 483: but other Scheme implementations use different ways. It is better 484: to define @code{main} procedure and uses its argument to receive 485: command line arguments, for it is expected to be more portable. 486: @c JP 487: Scheme???????ȰʹߤΥ??Þ¥?饤???ꥹ?Ȥϡ????????È¤Î¥í¡¼?ɤ????????Ñ¿???????????@code{gosh}?????󥿥饯?Æ¥??֥⡼?ɤǵ?ư???줿????????Ñ¿?code{()}?Ȥʤ????? 488: 489: ????Ñ¿?Tk?ȸߴ??????????????¾??cheme???????Ë¡??äƥ??Þ¥?饤?????Þ¤????Ý¡????Ö¥????????Ȥ???ˤϡ?@code{main}????????Ë¡????ᤷ?Þ¤??? 490: @c COMMON 491: @end defvar 492: 493: @defvar *program-name* 494: @c EN 495: This variable is bound to the script file name passed to @code{gosh}. 496: If @code{gosh} is invoked in the interactive mode, this variable 497: contains the pathname of @code{gosh} itself. 498: 499: STk uses the same variable to access the program/script name. 500: Other Scheme implementations use different ways. 501: @c JP 502: ????Ñ¿???????È¥Õ¥????????????????????󥿥饯?Æ¥??֥⡼?ɤǤ?????Ñ¿?code{gosh}???Ȥ?????äƤ??Þ¤??? 503: 504: ????Ñ¿?Tk?ȸߴ??????????????¾?μ??????Ë¡????Þ¤??? 505: @c COMMON 506: @end defvar 507: 508: @c EN 509: Now I show several simple examples below. 510: First, this script works like @code{cat(1)}, without any command-line 511: option processing and error handling. 512: @c JP 513: ?????Ĥ????????????Þ¤????ǽ????nix??code{cat(1)}???Þ¥??????ΤǤ??? 514: ???顼???䥳?Þ¥?饤?󥪥ץ????ν??ϹԤäƤ??Þ¤???@c COMMON 515: 516: @example 517: #!/usr/bin/env gosh 518: 519: (define (main args) ;@r{entry point} 520: (if (null? (cdr args)) 521: (copy-port (current-input-port) (current-output-port)) 522: (for-each (lambda (file) 523: (call-with-input-file file 524: (lambda (in) 525: (copy-port in (current-output-port))))) 526: (cdr args))) 527: 0) 528: @end example 529: 530: @c EN 531: The following script is a simple grep command. 532: @c JP 533: ???Î¥??????Ȥϴ????rep???Þ¥?Ǥ??? 534: @c COMMON 535: 536: @example 537: #!/usr/bin/env gosh 538: 539: (define (usage) 540: (format (current-error-port) 541: "Usage: ~a regexp file ...\n" *program-name*) 542: (exit 2)) 543: 544: (define (grep rx port) 545: (with-input-from-port port 546: (lambda () 547: (port-for-each 548: (lambda (line) 549: (when (rxmatch rx line) 550: (format #t "~a:~a: ~a\n" 551: (port-name port) 552: (- (port-current-line port) 1) 553: line))) 554: read-line)))) 555: 556: (define (main args) 557: (if (null? (cdr args)) 558: (usage) 559: (let ((rx (string->regexp (cadr args)))) 560: (if (null? (cddr args)) 561: (grep rx (current-input-port)) 562: (for-each (lambda (f) 563: (call-with-input-file f 564: (lambda (p) (grep rx p)))) 565: (cddr args))))) 566: 0) 567: @end example 568: 569: @c EN 570: See also @ref{Parsing command-line options}, for a convenient way to 571: parse command-line options. 572: @c JP 573: ?Þ¤???@ref{Parsing command-line options}????ȼ??Ë¥??Þ¥?饤?󥪥ץ??? 574: ?????Ȥ??Ǥ??Þ¤??? 575: @c COMMON 576: 577: @c ---------------------------------------------------------------------- 578: @node Debugging, Platform-dependent features, Writing Scheme scripts, Programming in Gauche 579: @section Debugging 580: @c NODE ?ǥХå? 581: 582: @c EN 583: Gauche doesn't have much support for debugging yet. 584: The idea of good debugging interfaces are welcome. 585: @c JP 586: Gauche?ˤϤޤ?ǥХå??ò¥µ¥Ý¡??Ȥ??뵡ǽ?????Þ¤?????????Þ¤????ǥХå??󥰤Υ??󥿥ե??????˴ؤ???ɤ??????Ç¥????????????󤻤???????? 587: @c COMMON 588: 589: @c EN 590: For now, the author uses the classic 'debug print stub' technique 591: when necessary. A special reader syntax @code{#?=@var{expr}} is 592: defined, which is read as @code{(debug-print @var{expr})}. 593: The macro @code{debug-print} evaluates and returns the result(s), 594: printing messages before and after evaluation of @var{expr}. 595: @c JP 596: ???ΤȤ??í¡¢???????Ê»??ϸ??Ū?ʡ֥ץ??È¥????Ö¡??ȤäƤ??Þ¤??? 597: ???Τ????꡼????Þ¥???code{#?=@var{expr}} ???????????Þ¤??? 598: ???? @code{(debug-print @var{expr})} ?Τ褦??ɤޤ졢 599: ?Þ¥???code{debug-print} ??var{expr}?????Ƥ???ͤ???Þ¤????? 600: ɾ???????????????????Ϥ??Þ¤??? 601: @c COMMON 602: 603: @example 604: gosh> #?=(+ 2 3) 605: #?="(stdin)":1:(+ 2 3) 606: #?- 5 607: 5 608: gosh> #?=(begin (print "foo") (values 'a 'b 'c)) 609: #?="(stdin)":2:(begin (print "foo") (values 'a 'b 'c)) 610: foo 611: #?- a 612: #?+ b 613: #?+ c 614: a 615: b 616: c 617: gosh> (define (fact n) 618: (if (zero? n) 619: 1 620: (* n #?=(fact (- n 1))))) 621: fact 622: gosh> (fact 5) 623: #?="(stdin)":6:(fact (- n 1)) 624: #?="(stdin)":6:(fact (- n 1)) 625: #?="(stdin)":6:(fact (- n 1)) 626: #?="(stdin)":6:(fact (- n 1)) 627: #?="(stdin)":6:(fact (- n 1)) 628: #?- 1 629: #?- 1 630: #?- 2 631: #?- 6 632: #?- 24 633: 120 634: @end example 635: 636: @c EN 637: You can also use trace/untrace macros in Gauche, written by Shigenobu Kimura: 638: @url{http://homepage.mac.com/skimu/ggc/}. 639: @c JP 640: ?Þ¤????¼?É¿?????auche???race/untrace?Þ¥????ñ¤¤¤Æ¤????????? 641: @url{http://homepage.mac.com/skimu/ggc/}. 642: @c COMMON 643: 644: @c ---------------------------------------------------------------------- 645: @node Platform-dependent features, Profiling and tuning, Debugging, Programming in Gauche 646: @section Using platform-dependent features 647: @c NODE ?×¥??È¥Õ¥?????¸?ε?ǽ 648: 649: @c EN 650: Gauche tries to provide low-level APIs close to what the underlying 651: system provides, but sometimes they vary among systems. For example, 652: POSIX does not require @code{symlink}, so some systems may lack 653: @code{sys-symlink} (@xref{Directory manipulation}). Quite a few 654: unix-specific system functions are not available on Windows platform. 655: @c JP 656: Gauche?Ǥ?S?????????I?˶ᤤ?????I??????????ˤ??Ƥ??Þ¤??? 657: ?????????????ƥऴ?Ȥ˰????ΰ㤦???????????????Ȥ??С?POSIX?Ǥ?@code{symlink}????ܤǤϤ???????Ç¡??????Æ¥????Ƥ?@code{sys-symlink} (@ref{Directory manipulation}??????????????UNIX?ϤΥ????Æ¥?????ʤ??餺Windows?ǤϻȤ??Þ¤??? 658: @c EN 659: To allow writing a portable program across those platforms, Gauche 660: uses @code{cond-expand} (@xref{Feature conditional}) extensively. 661: A set of extended @emph{feature-identifier}s is provided to check 662: availability of specific features. For example, on systems that 663: has @code{symlink}, a feature identifier @code{gauche.sys.symlink} 664: is defined. So you can write a code that can switch based on 665: the availability of @code{sys-symlink} as follows: 666: @c JP 667: ?×¥??È¥Õ¥??????ǥݡ????Ö¥??×¥í¥°???????????Gauche?Ǥ?????code{cond-expand} ????Þ¤?(@ref{Feature conditional}?????? 668: ??????줿@emph{??ǽ?????feature-identifier)}????????????ê¡¢?????ä?????ǽ??????????ɤ???????Ã¥??Ǥ??Þ¤??????Ȥ??С? 669: @code{symlink}?????륷???Æ¥??ϵ?ǽ?????code{gauche.sys.symlink}?? 670: ?????????????????äơ??ʲ??Τ褦??code{sys-symlink}?????Ǥ??뤫 671: ?ɤ????ˤ??Æ¡??????ɤò¥¹¥??Ã¥???????Ê¥×¥í¥°??????Þ¤??? 672: @c COMMON 673: 674: @example 675: (cond-expand 676: (gauche.sys.symlink 677: ... code that uses sys-symlink ...) 678: (else 679: ... alternative code ...) 680: ) 681: @end example 682: 683: @c EN 684: If you're familiar with system programming in C, you can think 685: it equivalent to the following C idiom: 686: @c JP 687: C?Î¥????Æ¥??í¥°???󥰤˾ܤ????ʤ顢???????ɤϰʲ????Î¥??Ç¥???????????Ȥߤʤ??Þ¤??? 688: @c COMMON 689: 690: @example 691: #if defined(HAVE_SYMLINK) 692: ... code that uses symlink ... 693: #else 694: ... alternative code ... 695: #endif 696: @end example 697: 698: @c EN 699: There are quite a few such feature identifiers; each identifier is 700: explained in the maunal entry of the procedures that depend on the feature. 701: Here we list a few important ones: 702: @c JP 703: ???Τ褦?ʵ?ǽ????ҤϤ??????󤢤ꡢ???줾??????ҤˤĤ??ƤϤ??Υޥ˥?????Τ??ε?ǽ?˰???????????ι???????Ƥ??Þ¤?????˽?פʤ? 704: ????˥ꥹ?È¥??åפ??Ƥ????Þ¤??? 705: @c COMMON 706: 707: @table @code 708: @item gauche 709: @c EN 710: This feature identifier is always defined. It is useful when you 711: write Scheme code portable across multiple implementations. 712: @c JP 713: ???ε?ǽ????ҤϾ??????????Þ¤???Gauche?ʳ???cheme?μ????? 714: ??Τ??륳???ɤ???Ȥ??˻Ȥ??Þ¤??? 715: @c COMMON 716: @item gauche.os.windows 717: @c EN 718: Defined on Windows native platform (currently using MinGW, but we'll 719: have VC++ version in future). Note that cygwin is considered unix. 720: @c JP 721: Windows?Í¥??Æ¥??Ö¥×¥??È¥Õ¥???????????????(??????Ǥϡ?MinGW???äƤ??Þ¤????????VC++??ò¥µ¥Ý¡??Ȥ?????Ǥ???) cygwin??NIX?Ȥߤ??????Ȥ?????Ƥ???????? 722: @c COMMON 723: @item gauche.sys.pthreads 724: @c EN 725: Defined if Gauche is compiled with pthread support. 726: @c JP 727: Gauche??pthread?ò¥µ¥Ý¡??Ȥ??????Ë¥?????뤵?????????????????? 728: @c COMMON 729: @item gauche.net.ipv6 730: @c EN 731: Defined if Gauche is compiled with IPv6 support. 732: @c JP 733: Gauche??IPv6?ò¥µ¥Ý¡??Ȥ??????Ë¥?????뤵?????????????????? 734: @c COMMON 735: @end table 736: 737: @c EN 738: Because @code{cond-expand} is a macro, the body of clauses 739: are expanded into toplevel if @code{cond-expand} itself is in 740: toplevel. That means you can switch toplevel definitions: 741: @c JP 742: @code{cond-expand}?Ï¥Þ¥????Τǡ?@code{cond-expand}???Ȥ??ȥåץ???ˤ?????????Τϥȥåץ???Ÿ???????????????ȥåץ???? 743: ??ò¥¹¥??Ã¥?Ǥ??????????ȤǤ??? 744: @c COMMON 745: 746: @example 747: (cond-expand 748: (gauche.os.windows 749: (define (get-current-user) 750: ... get current username ...)) 751: (else 752: (define (get-current-user) 753: (sys-uid->user-name (sys-getuid))))) 754: @end example 755: 756: @c EN 757: Or even conditionally "use" the modules: 758: @c JP 759: ???뤤?Ͼ?ˤ??ƥ⥸?塼???Ȥ??櫓???Þ¤??? 760: @c COMMON 761: 762: @example 763: (cond-expand 764: (gauche.os.windows 765: (use "my-windows-compatibility-module")) 766: (else)) 767: @end example 768: 769: