-
Notifications
You must be signed in to change notification settings - Fork 1
/
murmel-langref.lisp
1848 lines (1491 loc) · 56.7 KB
/
murmel-langref.lisp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
;;;
;;; === Murmel Language Reference =====
;;;
;;; The file `murmel-langref.lisp`
;;; is an executable language reference manual
;;; for Murmel, a single-namespace Lisp dialect
;;; inspired by Common Lisp.
;;;
;;; See also Murmel's default library [Mlib](mlib.md)
;;; which contains additional functions and macros.
;;;
;;; The file `murmel-langref.lisp` can be read as-is or run with:
;;;
;;; $ java -jar jmurmel.jar --repl --echo < murmel-langref.lisp
;;;
;;; or transformed to Markdown:
;;;
;;; $ sed -nf scripts/langref-to-md.sed murmel-langref.lisp \
;;; > murmel-langref.md
;;;
;;; Murmel is WIP, please note the section
;;; [Known issues](#known-issues) at the end of this file.
;;; == Murmel Reference ===============
;;; - [S-expressions](#s-expressions)
;;; - [Comments](#comments)
;;; - [Predefined Symbols](#predefined-symbols)
;;; - [Basic Special Forms](#basic-special-forms)
;;; - [Function application](#function-application)
;;; - [Data types](#data-types)
;;; - [Reserved words](#reserved-words)
;;; - [Variables and scope](#variables-and-scope)
;;; - [Additional Special Forms](#additional-special-forms)
;;; - [Backquote - fill-in templates](#backquote)
;;; - [Basic Primitives](#basic-primitives)
;;; - [Logic, Predicates](#logic-predicates)
;;; - [Conses and lists](#conses-and-lists)
;;; - [Vectors, Sequences](#vectors-sequences)
;;; - [Hashtables](#hash-tables)
;;; - [I/O](#io)
;;; - [Misc](#misc)
;;; - [Time](#time)
;;; - [Predefined Numeric Primitives](#predefined-numeric-primitives)
;;; - [Predefined Graphics Primitives](#predefined-graphics-primitives)
;;; - [Java FFI](#java-ffi)
;;;
;;; Additional functions that can be loaded with `(require "mlib")`
;;; - [Mlib - Default library for Murmel](mlib.md)
;;; == Introduction ===================
;;;
;;; "Hello, World!" program written in Murmel:
(jformat t "Hello, World!")
;;; The program text above when run in the REPL should print the famous
;;;
;;; Hello, World!
;;;
;;; followed by the result of `jformat`
;;;
;;; ==> nil
;;;
;;; and the prompt `JMurmel>`.
;;; == S-expressions ==================
;;;
;;; Murmel is a Lisp dialect. As such the language isn't
;;; really defined in terms of program text but in terms
;;; of in-memory objects (lists, symbols and other atoms)
;;; that are acceptable to `eval`.
;;;
;;; That said, JMurmel's default reader turns S-expressions
;;; from the input stream into equivalent in-memory objects. So
;;; for this reference we'll use S-expressions to describe
;;; programs that are valid Murmel, i.e. are acceptable to `eval`.
;;;
;;; In this reference in-memory objects that are valid Murmel
;;; (as well as their textual representation as S-expressions)
;;; are referred to as "forms".
;;;
;;; The following are S-expressions that JMurmel's reader
;;; will accept and transform into in-memory objects.
;;; Valid S-expressions may or may not be "forms"
;;; (i.e. may or may not eval w/o error).
; = Atoms that are not symbols
1
1.0
"a string"
#\a ; the character 'a'
#xff ; the integer number 255 in hex
; = Single quote
; A single quote `'` is a shorthand for `(quote form)`,
; see [Basic special forms](#basic-special-forms).
'form
; = Atoms that are symbols
; Symbols usually start with a letter and are composed of
; letters, digits and dashes ('-'),
; but pretty much any token that is not another atom will be
; parsed as a symbol.
; Murmel handles symbols similar to Common Lisp,
; see "CLHS 2.3.4 Symbols as Tokens" http://clhs.lisp.se/Body/02_cd.htm
; for details.
;
; Examples:
'a-symbol
'|a symbol|
'a\ symbol
'123"345
'123.456.789
; = Conses (pairs) and lists
; A dotted pair
'(a . b) ; ==> (a . b)
; A dotted list
'(a . (b . (c . d)))
; Shorthand for dotted list
'(a b c . d)
; A proper list
'(a . (b . (c . ()))) ; ==> (a b c)
; Shorthand for a proper list
'(a b c) ; ==> (a b c)
; = Labeled subobjects
; Since: 1.4.6
'(a #1=b #1# c) ; ==> (a b b c)
; Note that a label can only be used after it's value was completely read,
; i.e. `'#1=(a b . #1#) is an error in Murmel (while valid in CL).
; = Hashtables
; Since: 1.3.1
#H(eql k1 v1 k2 v2 k3 v3)
;;; == Comments =======================
; One line comments are started with `;`, i.e. everything between a semicolon
; and the end of the line is ignored:
; ; this is a comment
; Multiline comments are started with `#|` and ended with `|#`:
#|
This is a
multiline comment.
|#
; The pair `#!` and `!#` can also be used for multiline comments.
;;; == Predefined Symbols =============
; = `nil` and `t`
; `nil` and `t` are pre-defined self-evaluating symbols.
nil ; ==> nil
t ; ==> t
; Murmel treats the symbols `nil` and `t` the same way
; as Mr. Moon specified them in a Memo (see "The Evolution
; of Lisp pp 62"):
;
; > NIL is a symbol, the empty list, and the distinguished
; > "false" value. SYMBOLP, ATOM, and LISTP are true of it;
; > CONSP is not. CAR, CDR, and EVAL of NIL are NIL.
; > NIL may not be used as a function, nor as a variable.
; >
; > T is a symbol and the default "true" value used by predicates that
; > are not semi-predicates (i.e., that don’t return "meaningful" values
; > when they are true.) EVAL of T is T. T may not be used as a variable.
; > T is a keyword recognized by certain functions, such as JFORMAT.
; = internal-time-units-per-second
; `internal-time-units-per-second` contains the resolution
; of the time related functions, see below.
internal-time-units-per-second ; ==> 1.0E9
; = pi
; `pi` contains the value of the mathematical constant pi
; in double precision.
pi ; ==> 3.141592653589793
; = \*command-line-argument-list\*
; `*command-line-argument-list*` contains all command line arguments
; to the Murmel program. Below example illustrates this:
;
; C:\> java -jar jmurmel.jar -- a b c
; ...
; JMurmel> *command-line-argument-list*
;
; ==> ("a" "b" "c")
; JMurmel>
; = array-dimension-limit
; Largest acceptable vector index.
; = most-positive-fixnum, most-negative-fixnum
; These global variables contain the smallest and largest fixnum value.
; = \*condition-handler\*
;
; Since: 1.4
;
; This variable can be set to a function of one parameter.
; If `*condition-handler*` is non-nil then it will be invoked in case of an error,
; the argument will be the condition describing the error.
; During invocation of the condition handler it will be disabled:
; if the current handler dynamically replaced a previous handler
; then the previous handler will be temporarily restored.
; = \*random-state\*
;
; Since: 1.4.4
;
; Will initially be `nil`. Will be lazily created by one-arg `random`
; or zero-arg `make-random-state`.
; = REPL variables
; @-, @+, @++, @+++, @\*, @\*\*, @\*\*\*, @/, @//, @///
;
; These variables are only defined when using the REPL.
; They work similar to CL's REPL variables without the leading `@`.
;
; The global variables @\*, @\*\*, @\*\*\* are maintained by the Lisp read-eval-print loop
; to save the values of results that are printed each time through the loop.
;
; The value of @\* is the most recent primary value that was printed,
; the value of @\*\* is the previous value of @\*,
; and the value of @\*\*\* is the previous value of @\*\*.
;
; If several values are produced, @\* contains the first value only;
; @\* contains nil if zero values are produced.
;
; The values of @\*, @\*\*, and @\*\*\* are updated immediately prior to printing the return value
; of a top-level form by the Lisp read-eval-print loop.
; If the evaluation of such a form is aborted prior to its normal return,
; the values of @\*, @\*\*, and @\*\*\* are not updated.
;;; == Basic Special Forms ============
; = quote
; (quote form) -> form
;
; `quote` returns a form without evaluating it.
(quote a-symbol) ; ==> a-symbol
; = lambda
; (lambda (params*) forms*) -> closure
;
; When a lambda is created by the special form `lambda`
; then the lexical environment is captured at the time of lambda creation.
; Arguments to the special form `lambda` are not evaluated.
(lambda (p1 p2) p1)
; `lambda` with varargs:
; If paramlist is a symbol then all arguments will be
; packed into a list and bound to the symbol.
; If paramlist is a dotted list then remaining arguments
; will be bound to the last parameter.
(lambda popt (write popt)) ; no mandatory arguments
(lambda (p1 p2 . prest) (write prest)) ; two mandatory arguments
;;; == Data types =====================
;;;
;;; Murmel supports symbols and cons cells (and lists built from cons cells)
;;; as well as other atoms that are not symbols.
;;; These other atoms are double precision floating point numbers,
;;; integer numbers, vectors, strings, characters, bits and more.
;;; Custom primitives may support additional atoms.
;
; Murmel's type system (and JMurmel's corresponding host types) look like so:
;
; ;; Murmel type ; description or "Murmel form -> Java class used in JMurmel"
;
; t
;
; cons ; (cons 1 2) -> ConsCell
;
; atom ; all Murmel objects except cons cells
; ; and all Java Objects are atoms
;
; symbol ; 'sym -> LambdaJSymbol
; null ; nil -> null
; ; nil is the only object of type null
;
; number ; java.lang.Number is accepted for reading
; float ; 2.3 -> java.lang.Double
; integer ; 42 -> java.lang.Long
; ; only 54 bits are used
;
; character ; #\A -> java.lang.Character
;
; random-state ; (make-random-state) -> java.util.Random
;
; vector ; (make-array NN t t) -> java.util.ArrayList
; ; (make-array NN t CC) -> java.util.ArrayList
; ; makes an ArrayList w/ size NN and initial capacity CC
; ; java.util.List is acceptable for seqref and seqset
; simple-vector ; (make-array NN t nil) -> Object[]
; ; #(1 2 3) -> Object[]
; string ; (make-array NN 'character t) -> java.lang.StringBuilder
; ; java.lang.StringBuffer is acceptable for sref and sset
; ; java.lang.CharSequence is accepted for sref
; simple-string ; (make-array NN 'character nil) -> char[]
; ; "abc" -> java.lang.String
; bit-vector ; (make-array NN 'bit t) -> BitVector
; simple-bit-vector ; (make-array NN 'bit nil) -> boolean[]
; : #*0101 -> boolean[]
;
; hash-table ; (make-hash-table [test [size]])
; ; #H(eql one 1 two 2 three 3) -> EqlMap
; ; (make-hash-table ['eql [size]]) -> EqlMap
; ; (make-hash-table ['equal [size]]) -> EqualMap
; ; (make-hash-table 'eq [size]) -> java.util.IdentityHashMap
; ; (make-hash-table 't [size]) -> java.util.HashMap
; ; java.util.Map is acceptable for hashref, hashset, clrhash...
;
; function ; (lambda (param) param) -> Closure or MurmelFunction
;
; (list ::= cons | null)
; (sequence ::= list | vector)
;
;
; ;; Murmel's condition type hierarchy is a subset of CL's condition type hierarchy,
; ;; `error` and subtypes are pretty much the same. Murmel doesn't have multiple
; ;; inheritance, though, and `reader-error` only extends `stream-error`.
; ;; JMurmel maps conditions to Java exceptions as best as possible.
;
; condition java.lang.Throwable
; error java.lang.Exception
;
; (murmel-error LambdaJError extends RuntimeException extends Exception)
;
; ;;; extends LambdaJError
; simple-error SimpleError extends LambdaJError
;
; cell-error CellError extends LambdaJError
; unbound-variable UnboundVariable extends CellError extends LambdaJError
; undefined-function UndefinedFunction extends CellError extends LambdaJError
;
; control-error ControlError extends LambdaJError
;
; program-error ProgramError extends LambdaJError
;
; parse-error SExpressionReader.ParseError extends LambdaJError
;
;
; arithmetic-error java.lang.ArithmeticException extends RuntimeException
; (overflow, underflow)
;
; type-error java.lang.ClassCastException extends RuntimeException
; simple-type-error SimpleTypeError extends ClassCastException
; invalid-index-error InvalidIndexError extends IndexOutOfBoundsException
;
; file-error java.nio.file.InvalidPathException
; extends IllegalArgumentException extends RuntimeException
;
;
; stream-error java.io.IOException
; end-of-file java.io.EOFException extends IOException
; reader-error ReaderError extends IOException
; NOTE:
; CL's reader-error has two superclasses: stream-error and parse-error
; if c was a reader-error then both
; (typep c 'stream-error) and (typep c 'parse-error) are true
; The above is a subset of CLtL2's predefined data types,
; see "CLtL2 2. Data Types" https://www.cs.cmu.edu/Groups/AI/html/cltl/clm/node15.html
; Murmel's condition hierarchy is a subset of CLtL2 predefined condition types,
; see "CLtL2 29.5. Predefined Condition Types" https://www.cs.cmu.edu/Groups/AI/html/cltl/clm/node346.html
; and "CLHS 9.1.1 Condition Types" http://clhs.lisp.se/Body/09_aa.htm
; Murmel treats symbols case-insensitive.
; Symbol names are of arbitrary length, however only the
; first 30 chars are significant.
;
; Implementation note: JMurmel preserves the
; capitalization of the first encounter of a symbol
; for printing, e.g.:
;
; '(AbC . dEf) -> (AbC . dEf)
;
; but
;
; '(AbC . abc) -> (AbC . AbC)
'*a-sample-symbol*
'a\ symbol\ with\ spaces!
; Empty list, printed as `nil`
; ()
; Shorthand for empty list
; nil
; Tokens that consist of a sign or digit followed by digits
; are interpreted as integer numbers (java.lang.Long)
1
; Datatype float: a number in double precision
;
; numbers that contain the characters '.eE' are interpreted
; as floating point numbers (java.lang.Double)
1.0
; a float in scientific notation
1e3
; Stringliterals may have a maximum length of 2000 chars.
; Stringliterals of the same value are coalesced (interned).
"a string literal"
"another 'literal' \"string literal\""
;;; == Reserved words =================
; In addition to `nil` and `t` the symbols of the special forms are reserved, i.e.
; may not be used as a function nor as a variable:
;
; nil, t,
; lambda, quote, cond, labels, if, define, defun, let, let*, letrec,
; setq, progn, catch, thwrow, unwind-protect, try,
; multiple-value-bind, multiple-value-call,
; macrolet, defmacro, declaim, load, require, provide
;;; == Variables and Scope ============
; Symbols are bound to global variables using `define` or `defun`
; (see below), and to local variables using `let, let*, letrec` or
; lambda parameter lists.
;
; Murmel's global bindings are lexical and must be defined before use,
; i.e. a symbol is bound to
; a newly created variable when define or defun are actually executed.
; The symbol's binding as well as the variable's extent (lifetime)
; last to the end of the program (the symbol's binding may be
; temporarily replaced by a local or dynamic binding, though).
;
; Murmel's local bindings are lexical, i.e. a symbol is bound to
; a newly created variable when a `let/let*/letrec/lambda` form
; is evaluated. The symbol's binding as well as the associated variable
; are removed when leaving the lexical scope of the `let/let*/letrec/lambda`
; form, restoring any previously existing binding (which may have
; been local or global).
; Except: `let* dynamic` will treat global symbols as "special", see below.
;;; == Additional Special Forms =======
;;; = (define symbol [optional-object]) -> symbol
;
; `define` binds symbols in the global environment with
; memory locations that hold values.
; Murmel's `define` is somewhat similar to Common Lisp's `defparameter`
; except:
;
; CL's `defparameter` creates special global variables while
; Murmel's `define` creates global variables that can be
; lexically hidden by e.g. a let-binding.
;
; The first argument is not evaluated, the second one - if given - is.
; The second argument to `define` is optional and defaults to `nil`.
(define *global-var* 42) ; ==> *gloval-var*
(define f1 (lambda (p1 p2) (+ p1 p2))) ; ==> f1
; `define` forms can appear inside toplevel `let, let*, letrec, multiple-value-bind, labels` forms.
;;; = (defun symbol (params\*) [docstring] forms\*) -> symbol
;
; `defun` is a shorthand for defining functions:
;
; (defun symbol (params*) forms*)
; <=>
; (define symbol (lambda (params*) forms*))
;
; An optional docstring is ignored.
; Arguments to `defun` are not evaluated.
(defun f2 (p1 p2) (+ p1 p2)) ; ==> f2
; `defun` forms can appear inside toplevel `let, let*, letrec, multiple-value-bind, labels` forms
; and will bind a globally visible symbol to a closure (aka let-over-lambda):
(let ((counter 0))
(defun pre-increment-counter ()
(setq counter (1+ counter)))
(defun pre-decrement-counter ()
(setq counter (1- counter)))
(defun peek-counter ()
counter))
(pre-increment-counter) ; ==> 1
(pre-increment-counter) ; ==> 2
(pre-decrement-counter) ; ==> 1
(peek-counter) ; ==> 1
;;; = (defmacro name (params\*) [docstring] forms\*) -> symbol<br/>(defmacro name) -> prev-name
;
; `defmacro` defines a macro, similar to CL's `defmacro`.
; Macros are somewhat similar to functions:
; On a function application the functions's arguments
; are eval'd and the result of the function will be used as is.
; On a macro application the macro's arguments are
; NOT eval'd, but the result of the macro is.
;
; IOW a function produces a value, a macro application
; produces code that will be eval'd.
; Macros are defined in a macro namespace.
; All macros are in the global scope, i.e. macros
; are not scoped but once defined they are visible everywhere.
;
; `(defmacro name)` can be used to unbind previously
; defined macros.
;
; An optional docstring is ignored.
(defmacro twice (arg) (list '* arg 2)) ; ==> twice
(twice 3) ; ==> 6.0
(defmacro twice) ; ==> twice; macro is unbound
(defmacro twice) ; ==> twice; no-op
;;; = (setq symbol value [more-symbols more-values]\*) -> last-value
;
; `setq` updates the value(s) of the given global or local symbol(s).
; In interpreted Murmel undefined variables will be created on the fly,
; in compiled mode variables must have been defined previously.
(define a nil)
(let ((b nil) (c nil))
(setq a 1 b 2 c (+ a b))) ; ==> 3.0
;;; = (if condform form [optionalform]) -> result
(if nil 'YASSS! 'OHNOOO!!!) ; ==> OHNOOO!!!
;;; = (progn forms\*) -> result
(if t (progn (write 'abc) (write 'def)))
;;; = (cond (condform forms\*)\* [(t forms\*)]) -> result
;;; = (labels ((symbol (params\*) forms\*)\*) forms\*) -> result
;
;;; = (let [optsymbol] ((symbol bindingform)\*) bodyforms\*) -> result
;
; Works similar to CL's `let` with the addition
; of Scheme's "named let".
; The let-bound variables `symbol` as well as `optsymbol` - if given -
; are bound inside bodyforms.
; `optsymbol` will be bound inside `bodyforms` to a lambda
; whose parameters are the let-variables and whose code is
; `bodyforms`. Therefore `optsymbol` can be used for
; recursive calls within `bodyforms`.
(let loop ((x 3)
(msg "hi"))
(if (= x 0)
(write msg)
(progn (write (floor x)) (loop (- x 1) msg))))
;;; = (let dynamic ((symbol bindingform)\*) bodyforms\*) -> result
;
; Similar to `let` except: globals are not shadowed but temporarily
; bound to the given value, and the previous value is restored when
; leaving the scope of the `let` form.
; I.e. `let dynamic` treats globals as "special".
;
; Example:
(setq a 1)
(define b 2)
(defun f () (write (cons a b)))
(f)
(let dynamic ((a 11) (b a))
(f))
(f)
; will print `(1 . 2)(11 . 1)(1 . 2)`.
;;; = (let* [optsymbol] ((symbol bindingform)\*) bodyforms\*) -> result
;
; Works like `let` (see above) with the addition:
; each `bindingform` "sees" the previous symbols. If multiple
; let-bindings use the same symbol the last one hides
; preceeding ones.
(let* loop ((y 10)
(x (+ y 20))
(x (floor (/ x 10)))
(msg 'hi))
(if (= x 0)
(write msg)
(progn (write (floor x)) (loop 0 0 (- x 1) msg))))
;;; = (let* dynamic ((symbol bindingform)\*) bodyforms\*) -> result
;
; Similar to `let*` except: globals are not shadowed but temporarily
; bound to the given value, and the previous value is restored when
; leaving the scope of the `let*` form.
; I.e. `let* dynamic` treats globals as "special".
(define *g* 'global)
(defun fun () (write *g*))
(let* dynamic ((*g* 'temp)) (fun)) ; fun will write temp
*g* ; ==> 'global
;;; = (letrec [optsybol] ((symbol bindingform)\*) bodyforms\*) -> result
;
; `letrec` works like `let` and `let*` except each bindingform "sees"
; all other let symbols as well as it's own symbol.
; All symbols are bound, but only the preceeding bindings
; are defined (have a value) while eval'ing the `bindingform`.
; That way a let-bound variable could be a recursive lambda.
(letrec ((x 1) (y (+ x 1))) (write y))
;;; = (macrolet ((symbol params [docstring] forms)\*) forms\*) -> result
;
; Since: 1.4.7
;
; `macrolet` defines local macros and executes `forms` using the local definitions.
; It is an error to shadow local macros with a local function (see `labels`)
; or with a named-let loop label.
; A docstring if given will be ignored.
;;; = (catch tagform forms\*) -> result
;
; Since: 1.3
;
; `catch` is used as the destination of a non-local control transfer by `throw`.
;;; = (throw tagform resultform) -> |
;
; Since: 1.3
;
; `throw` causes a non-local control transfer to a `catch` whose tag is `eq` to tag.
; TODO: If there is no outstanding catch tag that matches the throw tag,
; no unwinding of the stack is performed, and an error of type control-error is signaled.
;;; = (unwind-protect protected-form cleanupforms\*) -> result
;
; Since: 1.3
;
; `unwind-protect` evaluates `protected-form` and guarantees that `cleanup-forms`
; are executed before unwind-protect exits, whether it terminates normally or is aborted
; by a control transfer of some kind.
;;; = (try protected-form . error-obj) -> result
;
; Since 1.4
;
; `try` evaluates `protected-form`. If no error occurred during evaluation then
; the values from `protected-form` are the final result.
; If an error occurs then `try` returns the `error-obj` if given or null as the primary result,
; the secondary result is the condition.
;
; Example:
;
; (multiple-value-bind (result condition)
; (try (1+ most-positive-fixnum) 'error)
; (if (eq result 'error)
; (progn (write "an error occurred: " nil)
; (write condition)
; 'bummer)
; result))
; ; ==> bummer
;;; = (multiple-value-call function-form values-forms\*) -> result
;
; Since: 1.2
;
; `multiple-value-call` first evaluates the `function-form` to obtain function,
; and then evaluates each `values-form`. All the values of each form
; are gathered together (not just one value from each) and given as arguments
; to the function.
(multiple-value-call + 0.0 (values 1 2) 3) ; ==> 6.0
;;; = (multiple-value-bind (symbols\*) values-form bodyforms\*) -> result
;
; Since: 1.2
;
; `values-form` is evaluated, and each of the `symbols` is bound
; to the respective value returned by that form.
; If there are more `symbols` than values returned,
; `nil` is assigned to the remaining vars.
; If there are more values than symbols, the excess values are discarded.
; The symbols are bound to the values over the execution of the `bodyforms`,
; which make up an implicit progn.
(multiple-value-bind (a b) (values 'Hello\, '\ World!)
(jformat nil "%s%s" a b))
; ==> "Hello, World!"
(multiple-value-bind (a b . c) (values 1 2 3 4 5)
(writeln a) (writeln b) (writeln c))
; ; 1
; ; 2
; ; (3 4 5)
; ; ==> (3 4 5)
(multiple-value-bind a (values 1 2 3 4 5)
(write a))
; ; (1 2 3 4 5)
; ; ==> (1 2 3 4 5)
;;; = (load filespec) -> result
;
; Since: 1.1
;
; Eval the contents of the given file, return value
; is the value returned by the last form or nil
; in case of an empty file.
;
; When compiling Murmel `load` is performed at
; compile time and must appear as a toplevel form.
;
; `filespec` is not eval'd and must be a string.
; Unless filespec ends with ".lisp" the file extension
; ".lisp" will be appended.
; If filespec is an absolute path then it will be used as is.
; Otherwise the file will be searched in the same directory
; as the file that contains the `load` and after that
; in "libdir" (set with `--libdir`, "libdir" defaults to the
; directory containing jmurmel.jar).
; If `load` is entered into the REPL then the file
; will be searched in the current directory and then
; in the "libdir".
; (load "nul") ; ==> nil, NUL is Windows specific
; (load "lib") ; will search for lib.lisp and eval it's contents
;;; = (require module-name optional-file-path)
;
; Since: 1.1
;
; Load the given file once. Murmel maintains an internal
; set of loaded modules, and `require` will ignore
; loading files that were already loaded by comparing
; `module-name` to the set of already loaded modules.
;
; When compiling Murmel `require` is performed at
; compile time and must appear as a toplevel form.
;
; If `optional-file-path` is omitted or nil then
; `module-name` will be used as the file path.
;
; `module-name` and `optional-file-path` are not eval'd
; and must be strings.
; (require "mlib") ; will search for the file mlib.lisp
; unless the module "mlib" was already loaded
;;; = (provide module-name)
;
; Since: 1.1
;
; Set a file's modulename so that `require` won't
; load it twice.
;;; = (declaim (optimize ...
;
; `declaim` currently only supports `optimize`, others will be ignored.
; `optimize` only supports speed, others will be ignored.
;;; == Function application ===========
;;; = function call
;
; Applies the operator returned by operatorform to
; the eval'd operands
;
; (operatorform operands*)
;;; == Backquote ======================
; Backquote "\`" starts "fill-in templates":
; backquote, comma and comma-at work similar to CL,
; except: comma-dot is not supported.
(setq a 'a-val) (setq b 'b-val) (define c 'c-val)
(define d '(d-val1 d-val2))
`((,a b) ,c ,@d) ; ==> ((a-val b) c-val d-val1 d-val2)
(define y 'b) (define l '(a b))
(eval ``(,a ,,@l ,,y) '((a . a) (b . b) (y . y)))
; ; ==> (a-val a-val b-val b-val)
(define x '(1 2 3))
`(normal= ,x splicing= ,@x see?)
; ; ==> (normal= (1 2 3) splicing= 1 2 3 see?)
`(normal= ,x fakesplicing= . ,x)
; ; ==> (normal= (1 2 3) fakesplicing= 1 2 3)
;;; == Basic Primitives ==========
;;; = (apply form argform) -> result
;
; `form` must eval to a symbol, primitive or lambda.
; `argform` must eval to a proper list.
;(apply '+ '(1 2)) ; ==> 3.0
(apply + '(1 2)) ; ==> 3.0
;;; = (eval form) -> result<br/> (eval form env) -> result
;
; `form` will be eval'd, it must return a form.
; The optional argument `env` will be eval'd, it must return a list of `(symbol . value)`.
; If the optional argument `env` is omitted or `nil`
; then the environment for the recursive eval is "all predefined global symbols"
; else it is the concatenation of `env` and all predefined globals
(eval '(+ 1 2)) ; ==> 3.0
(eval '(+ x y) (list '(x . 2) '(y . 3))) ; ==> 5.0
(eval '(+ x y) (list (cons 'x 2) (cons 'y 3))) ; ==> 5.0
;;; == Logic, predicates
; = (eq x y) -> boolean
;
; Returns `t` if `x` and `y` are the same object, `nil` otherwise.
; = (eql x y) -> boolean
;
; Return `t` if any of the following is true
;
; - `a` and `b` are `eq`
; - `a` and `b` are numbers of the same type and have the same value
; - `a` and `b` are the same characters
;
; Examples:
(eql 2 2) ; ==> t
(eql #\a (sref "aaa" 0)) ; ==> t
(eql -0.0 0.0) ; ==> nil
; = (equal a b) -> boolean
;
; Since: 1.4
;
; Return `t` if any of the following is true:
;
; - `a` and `b` are `eql`
; - `a` and `b` are strings that have the same text value
; - `a` and `b` are bitvectors whose elements are eql
; - `a` and `b` are conses whose car and cdr are `equal` respectively
; = consp, atom, symbolp, null, listp
; = numberp, integerp, floatp
;
; - `numberp` returns `t` for all Murmel and Java number types.
; - `integerp` returns `t` for all Murmel and Java integral number types.
; - `floatp` returns `t` for Murmel and Java decimal number types.
; = characterp
; = random-state-p
;
; Since: 1.4.3
; = hash-table-p
;
; Since: 1.4
; = functionp
;
; Since: 1.3
; = vectorp, simple-vector-p
; Since: 1.3
; = stringp
;
; Return `t` for Murmel strings and all Java Objects implementing java.lang.CharSequence
; = simple-string-p, bit-vector-p, simple-bit-vector-p,
; Since: 1.3
; = (typep obj typespec) -> boolean
;
; Since: 1.4
;
; `typep` returns `t` if `obj` is of type `typespec` or of a subtype.
; = (adjustable-array-p obj) -> boolean
;
; Since: 1.3
;
; `adjustable-array-p` returns `t` if `obj` is an adjustable vector.
;
; Note that in Murmel `(adjustable-array-p 1)` returns nil
; while in Common Lisp that would signal a `type-error`.
;;; == Conses and lists
; = (car list) -> 1st element of list
(car '(a b c)) ; ==> a
; = (cdr list) -> rest of list
(cdr '(a b c)) ; ==> (b c)
; = (cons e1 e2) -> conscell
(cons 'a 'b) ; ==> (a . b)
;;; = rplaca, rplacd
;
; Replace the value of the CAR or CDR slot of a cons cell.
(setq l '(1 2))
(rplaca l 11) ; ==> (11 2)
(rplacd l 22) ; ==> (11 . 22)
;;; = (list elems\*) -> list<br/>(list\* elems+) -> atom-or-dotted-list
;
; `list` will create a list consisting of it's arguments,
; `list*` will create a dotted list.
(list) ; ==> nil
(list 1) ; ==> (1)
(list 1 2 3) ; ==> (1 2 3)
;(list*) ; ==> error
(list* 1) ; ==> 1
(list* 1 2 3) ; ==> (1 2 . 3)