kashell.texi 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482
  1. \input texinfo
  2. @settitle The KaShell Programming Language
  3. @macro stxlit{TEXT}
  4. @code{\TEXT\}
  5. @end macro
  6. @macro stxdef{NAME}
  7. @anchor{meta-\NAME\}@var{\NAME\} @t{::=}
  8. @end macro
  9. @macro arbno{THING}
  10. \THING\@sup{*}
  11. @end macro
  12. @ifnotinfo
  13. @ifnottex
  14. @macro stxref{NAME}
  15. @var{\NAME\}
  16. @end macro
  17. @end ifnottex
  18. @end ifnotinfo
  19. @ifinfo
  20. @macro stxref{NAME}
  21. @var{\NAME\}
  22. @end macro
  23. @end ifinfo
  24. @iftex
  25. @macro stxref{NAME}
  26. @var{\NAME\}
  27. @end macro
  28. @end iftex
  29. KaShell is an evolving design for a programming language
  30. with a compact syntax similar to shell and friendly for interactive use,
  31. and with semantics similar to optionally-typed languages like Scheme.
  32. The prototype is a variant of @uref{https://www.gnu.org/software/kawa,Kawa}.
  33. To try it, @uref{https://www.gnu.org/software/kawa/Installation.html,install Kawa},
  34. and then start up kawa with the @code{--kashell} option.
  35. KaShell was previously known as Q2.
  36. There is some old documentation/ideas
  37. @uref{https://www.gnu.org/software/kawa/q2,here}.
  38. @subheading Basic syntax
  39. Whitespace and indentation are signficant.
  40. Commands are similar to shell syntax:
  41. A simple command has the form of a command followed
  42. by the argument expressions, separated by spaces:
  43. @example
  44. expt 2 3
  45. @end example
  46. This calls the @code{expt} function with the given arguments.
  47. Parentheses are not needed, except for grouping:
  48. @example
  49. expt 2 (sqrt 9)
  50. @end example
  51. Such a command is an example of a @dfn{phrase}.
  52. The function name and each argument is a @dfn{word}.
  53. @display
  54. @stxdef{word} @var{identifier} | @var{literal} | ....
  55. | @stxlit{(} @var{phrase} @stxlit{)}
  56. @stxdef{phrase} @arbno{@stxref{word}}
  57. @end display
  58. Phrases can be separated by newline or semicolons.
  59. A procedure call is a @dfn{phrase} whose first word
  60. evaluates to a procedure value.
  61. (It can be a single-word phrase, if there are no arguments.)
  62. A syntactic form is @dfn{phrase} whose first word is
  63. a predefined syntactic keyword or an in-scope macro.
  64. @subheading Identifiers
  65. An identifier is used to name things in a program.
  66. The allowed characters in an identifier is bigger that
  67. in most programming languages and roughly follows Scheme.
  68. There are no reserved identifiers, though there are
  69. syntactic keywords predefined in the default scope.
  70. The recommend style for multi-part names is to use hyphens between the parts:
  71. @code{array-rank}.
  72. There will be some syntax to include otherwise-disallowed characters
  73. in an identifier. This has not been decided or implemented
  74. but I'm leaning towards backslash followed by a string template.
  75. For example @code{\@{1.5@}} would be an identifier
  76. (with the 3 characters @code{"1"}, @code{"."}, and @code{"5"}) rather than a number.
  77. Compound identifiers have two parts, separated by a colon (and no whitespace).
  78. The first part is a namespace (an identifier), and the second part is a name
  79. within that namespace.
  80. @subheading Indentation
  81. Indentation is significant:
  82. @example
  83. foo 1 2 3
  84. bar 4 5
  85. 3 + 3
  86. baz 10 11
  87. @end example
  88. is equivalent to:
  89. @example
  90. foo 1 2 3 (bar 4 5 (3 + 3)) (baz 10 11)
  91. @end example
  92. @subheading Comments
  93. A hash-sign @code{# } followed by at least one space
  94. comments out the rest of the line.
  95. A hash-sign followed by an exclamation point @code{#!} is also a comment.
  96. Syntax for nestable comments hasn't been decided yet.
  97. Candidates include @code{#[comment#]} or @code{#[comment]#} or plain @code{#[comment}`.
  98. @subheading Numbers
  99. KaShell implements the Kawa Scheme ``numeric tower'',
  100. including exact integers and rationals, floating-point reals,
  101. and complex numbers. (Syntax of literals may change slightly from Kawa Scheme.)
  102. Quaternions are also supported.
  103. A general radix can be specified:
  104. @example
  105. @var{radix}@stxlit{r}@var{radix-digits}
  106. @end example
  107. For example:
  108. @example
  109. 16rFFFF
  110. 2r110011
  111. @end example
  112. We may add exact decimal numbers, possibly with repeating fractional part.
  113. These are mathematically equivalent to exact rationals, but are
  114. typically easier to read and write.
  115. Quantities are a product of a real number and a unit.
  116. For example: @code{3cm + 2in} evaluates to @code{8.08cm} (the second
  117. quantity is converted to the unit of the first).
  118. A designed extension will be able to do unit-checking at compile-time
  119. based on @uref{https://www.gnu.org/software/kawa/Ideas-and-tasks.html#Types-for-units,this design}.
  120. @subheading Arithmetic
  121. The usual infix and operator precedence rules apply.
  122. For example, the following evaluates as expected to 22:
  123. @example
  124. 10 + 2 * 6
  125. @end example
  126. Note that spaces are (generally) required.
  127. However, note that infix operators like @code{+} are @emph{not} reserved
  128. syntax. They are predefined syntatic keywords (with associated
  129. precedence information), and there will be a way to
  130. add or replace operators.
  131. @subheading Variables and definitions
  132. All variables must be defined before using them,
  133. to catch typos. However, the syntax to define a variable
  134. is quite compact - you just need to add @code{^} after the variable:
  135. @example
  136. twenty^ = 10 + 5 + 5
  137. @end example
  138. Initially, we will restrict the left side to be a pattern,
  139. and the right side to be an expression:
  140. @example
  141. @stxref{pattern} @stxlit{=} @stxref{expression}
  142. @end example
  143. You can do simple pattern matching:
  144. @example
  145. [x^ y^] = [3 4]
  146. @end example
  147. (In the future, the @code{=} operator may be extended to bi-directonal
  148. @uref{https://en.wikipedia.org/wiki/Unification_(computer_science),unification}.)
  149. Variables defined using @code{=} are write-one ``logic'' variables,
  150. and so they may not be re-assigned
  151. (though this is not currently enforced).
  152. Their scope is the entire current block (or function).
  153. Lexical override is not allowed - the can be only a single definition in any scope.
  154. You can declare regular mutable variables with the @stxlit{:=} operator,
  155. but with pattern restricted to a single identifier with an optional type:
  156. @example
  157. @stxref{identifier}@stxlit{^} @stxlit{:=} @stxref{expression}
  158. @stxref{identifier}@stxlit{^}@stxref{type} @stxlit{:=} @stxref{expression}
  159. @end example
  160. For example:
  161. @example
  162. counter^ := 0
  163. counter := counter + 1
  164. @end example
  165. @subheading Logic programming [possible future]
  166. Check out @uref{http://picat-lang.org/,Picat}
  167. and @uref{http://www.ps.uni-saarland.de/alice/,Alice}.
  168. Also check out Kanren/MiniKanren/cKanren.
  169. @subheading Optional type specifiers [not working yet]
  170. You can add an optional type specifier after the `^` in a definition:
  171. @example
  172. pi^float = 3.14
  173. @end example
  174. @subheading Patterns
  175. @emph{(Not yet implemented.)}
  176. Patterns are conceptually similar to Kawa, but with a different syntax.
  177. The most noticable differences is that @samp{^} is used to separate
  178. a variable name from it type-specifiers, and that a plain
  179. identifier is not a valid pattern - it must be followed by a @samp{^}.
  180. A @var{pattern} is one of:
  181. @table @asis
  182. @item @var{identifier}@stxlit{^}
  183. This is the simplest and most common form of pattern.
  184. The @var{identifier} is bound to a new variable
  185. that is initialized to the incoming value.
  186. The @stxlit{^} must be followed by a space or
  187. a closing delimiter (such as a right bracket).
  188. @item @stxlit{_}
  189. This pattern just discards the incoming value.
  190. It is equivalent to a unique otherwise-unused @var{identifier}.
  191. @item @stxref{identifier} @stxlit{^} @stxref{type}
  192. @itemx @stxref{pattern} @stxlit{^} @stxref{type}
  193. The incoming value is coerced to a value of the specified @var{type},
  194. and then the coerced value is matched against the sub-@var{pattern},
  195. or bound to the @var{identifier}.
  196. No spaces are allowed on either side of the @stxlit{^}.
  197. @item @stxref{pattern-literal}
  198. Matches if the value is @code{equal?} to the @var{pattern-literal}.
  199. @end table
  200. @subheading Functions
  201. @display
  202. @stxdef{lambda-form} @stxlit{(|} @var{parameter-list} @stxlit{|)} @var{phrase}
  203. @end display
  204. @display
  205. @stxlit{fn} @var{name} @arbno{@stxref{lambda-form}}
  206. @end display
  207. @subheading Conditional operator
  208. The @code{?>} is syntatically an infix operator but it integrates
  209. with the phrase-parsing to provide a ternary if-the-else operator:
  210. @example
  211. (3 > 4 ?> "it is true"; "if is false")
  212. @end example
  213. or:
  214. @example
  215. x > 0 ?>
  216. display x
  217. display " is positive"
  218. newline
  219. x < 0 ?>
  220. display x
  221. display " is negative"
  222. newline
  223. display x
  224. display " is zero"
  225. newline
  226. @end example
  227. [This is a hack that needs further thought and specification.]
  228. @subheading Vectors and arrays
  229. Use square brackets to construct (immutable) vectors:
  230. @example
  231. [3 (2 + 2) 5]
  232. @end example
  233. A vector is a function from an integer to an element.
  234. @example
  235. [3 4 5] 2
  236. @end example
  237. evalutes to 5.
  238. You can use a vector index to select elements:
  239. @example
  240. [10 11 12] [2 1]
  241. @end example
  242. evaluates to @code{[12 11]}.
  243. There is support for @uref{https://www.gnu.org/software/kawa/Arrays.html,multi-dimensional arrays} but specifics (such as syntax and operator names) have not been decided.
  244. @subheading Strings
  245. A string is an immutable sequence (vector) of characters (Unicode code points).
  246. You can index it (like a vector) to get a character.
  247. (Not yet implemented: A character is also a string of length 1,
  248. so @code{"X" 0} yields the same @code{"X"}.
  249. This removes the need for distinct character literal syntax.)
  250. There are two kinds of string literals - using
  251. delimited by traditional double-quotes, or by braces:
  252. @display
  253. @stxdef{qstring} @stxlit{"}@arbno{@var{qstring-element}}@stxlit{"}
  254. @stxdef{bstring} @stxlit{&@lbracechar{}} @arbno{@var{bstring-element}}@stxlit{@rbracechar{}}
  255. @end display
  256. @subsubheading Double quoted string literals
  257. A @var{qstring} is the traditional syntax with double quotes: @code{"Hello"}.
  258. It supports all the standard C-style or JSON escapes.
  259. Most C-style escapes are supported: @code{"Hello!\n"}.
  260. ECMAScript 2015 ``Unicode code point escapes'' seems a
  261. reasonable extension: @stxlit{\u@{hex-digits@}}.
  262. (They may be a way to continue line using some escape sequence,
  263. details not yet decided.)
  264. @subsubheading Brace string literals
  265. A @var{bstring} is written using curly braces: @code{@{Hello@}}.
  266. Braces nest: @code{@{string with @{braces@}.@}}.
  267. These maybe multi-line and there are various escape sequences,
  268. @uref{https://www.gnu.org/software/kawa/String-literals.html#String-templates,like Kawa template string}, though backslash is used as the
  269. escape character rather than @code{&}.
  270. @code{@{L\aelig;rdals\oslash;yri@}} evaluates to @code{"Lærdalsøyri"}.
  271. @code{@{Adding 3 and 4 yields \(3 + 4).@}} evaluates to @code{"Adding 3 and 4 yields 7."}.
  272. You can also add formatting specifiers.
  273. You can nest bstrings and qstrings by prefixing them with a backslash
  274. (not implemented).
  275. @subheading Object constructor syntax
  276. An identifier allowed by a brace-literal is a conveniece syntax
  277. for constructing complex objects:
  278. @example
  279. URI@{http://example.com/@}
  280. @end example
  281. The constructor can also contain expressions in parentheses
  282. (which is evaluated), or bracket literals that contain multiple expressions.
  283. There may be no (unescaped) spaces between the parts of an object literal.
  284. The concept and implementation are similar to Kawa's and SRFI-108's
  285. @uref{https://srfi.schemers.org/srfi-108/srfi-108.html,Named quasi-literal constructors}. However, the syntax is different in using backslash
  286. as the escape character, and not requiring an initial backslash.
  287. @subheading Rich text objects [partially implemented]
  288. A rich text is an enhanced string,
  289. with embedded objects and formatting.
  290. It is syntatic sugar for a kind of object constructor.
  291. It has the form of a single-quote followed by a @var{bstring}.
  292. @example
  293. '@{Some text *strong* and \em@{emphasized@}.@}
  294. @end example
  295. A subset of Markdown syntax is recognized, including
  296. @code{*}, @code{_} and blank lines for paragrph separator.
  297. Beyond that, general object literal syntax is used.
  298. The above is equivalent to:
  299. @example
  300. text@{Some text \text:b@{strong@} and \text:em@{emphasized@}.@}
  301. @end example
  302. Evaluating either expression yields a text object,
  303. which is a tree-structure that generalies strings.
  304. The text object can then be converted to various formats
  305. depending on context. For example:
  306. @example
  307. write-pdf -filename=hello.pdf '@{Hello!@}
  308. @end example
  309. or
  310. @example
  311. as-html '@{Some text *strong* and \em@{emphasized@}.@}
  312. @end example
  313. which yields @code{"<p>Some text <b>strong</b> and <em>emphasized</em>.</p>"}
  314. It is intended that text literals be used to document programs.
  315. Tools that pretty-print programs or extract API information
  316. should format these documentation strings.
  317. The DomTerm terminal emulator allows ``printing'' HTML as rich text.
  318. When printing a text value in a DomTerm REPL it should
  319. implicitly call @code{as-html} and show that.
  320. @subheading Keywords
  321. @emph{(Not yet implemented.)}
  322. One problem with the existing (Kawa) keyword syntax is that
  323. it does not support (tab-)completion, because it does not start with
  324. a special character.
  325. To fix that we can ``merge'' keyword syntax with command option syntax,
  326. by prefixing with @samp{-}. For example:
  327. @example
  328. --name=John
  329. @end example
  330. corresponds to Kawa-Scheme's
  331. @example
  332. name: "John" ;; or maybe: name: 'John
  333. @end example
  334. Either a single @samp{-} or double @samp{--} are allowed.
  335. They have the same effect in evaluation node, but are
  336. different in non-eval (quoted) mode.
  337. An @var{operand} (compare Kawa syntax) can be one of:
  338. @table @asis
  339. @item @stxlit{-}@var{identifier}@stxlit{=}@var{word}
  340. @itemx @stxlit{--}@var{identifier}@stxlit{=}@var{word}
  341. The @var{word} is implicitly quoted.
  342. No space is allowed before or after the @samp{=}.
  343. @item @stxlit{-}@var{identifier}@stxlit{:} @var{expression}
  344. @itemx @stxlit{--}@var{identifier}@stxlit{:} @var{expression}
  345. A number keyword-argument pair, with @var{expression} evaluated a call time.
  346. Space is required after the @samp{:}.
  347. @item @stxlit{-}@var{identifier}
  348. @itemx @stxlit{--}@var{identifier}
  349. Equivalent to: @stxlit{--}@var{identifier}@stxlit{: #t}.
  350. (Assuming @var{identifier} does not start with @stxlit{no-}.)
  351. @item @stxlit{-no-}@var{identifier}
  352. @itemx @stxlit{--no-}@var{identifier}
  353. Equivalent to: @stxlit{--}@var{identifier}@stxlit{: #f}.
  354. @end table
  355. In non-eval (quoted) mode, the reverse mapping is performed
  356. (more-or-less - details/restrictions to come).
  357. @subheading Running programs [not implemented yet]
  358. The @code{run} macro quasi-quotes its arguments, and then executes
  359. the resulting string list as a process invocation,
  360. as if using the Kawa @code{run-process} function.
  361. @example
  362. run date --utc
  363. @end example
  364. The result is a @dfn{process} object. A process can be coerced to
  365. a string (or more generally a @dfn{blob}), which is the result of
  366. standard output from the process.
  367. A @var{process} ``written'' to the REPL coerces it to a string.
  368. The @code{run} macro can be left out if the following word
  369. has the form of a fully-qualified filename (i.e. starting with @code{/}).
  370. Also, if the following word is not in the lexical scope,
  371. but if there is (at compile-time) an executable file by that name in
  372. the @code{PATH} then @code{run} is also implied.
  373. Filename globbing is performed.
  374. Enclosed expressions are evaluated (at run-time). If they evaluate to
  375. a string without newlines, the result is interpolated in the command argument.
  376. If the result is a multi-line string or a sequence,
  377. more complex rules (TBD) are in play.
  378. @bye