Skip to content

Commit ea9d5da

Browse files
committed
compiler: add fixnums Word{64,32,16,8}, Int{64,32,16,8}
and example/fixnums.kl and associated operations such as: - {word|int}{8|16|32|64}{<|>|<=|>=|=} - {word|int}-compliment,-popcount,->string - {word|int}{8|16|32|64}->string and redefine #%integer-literal to not assume an Integer.
1 parent 5e8ff57 commit ea9d5da

45 files changed

Lines changed: 1795 additions & 156 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

README.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,7 @@ want to help make Klister a more practical language, please `reach out`_!
145145
Here are the most prominent Racket features which are missing from Klister:
146146

147147
* Klister does not yet support custom readers, and thus every ``#lang`` looks like a
148-
Lisp. This also limits languages to Integer literals and String literals.
148+
Lisp. This also limits languages to Integer literals, builtin fixnums and String literals.
149149
* `local-expand`_ is planned, but not yet implemented.
150150
* `Syntax parameters`_ are planned, but not yet implemented.
151151

examples/anaphoric-if.kl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,6 @@
3333
,else])))))
3434

3535
(example
36-
(if-non-empty (snoc (list 1 2 3) 4)
36+
(if-non-empty (snoc (list (the Integer 1) 2 3) 4)
3737
(:: 0 it)
3838
(list 0)))

examples/datatype-macro.kl

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,9 @@
3232
[(cons x xs)
3333
(pure (quasiquote/loc more (:: ,x ,(cons-list-syntax 'list xs xs))))])]))]))
3434

35-
(example (reverse (:: 1 (:: 2 (:: 3 (nil))))))
35+
(example (reverse (:: (the Integer 1) (:: 2 (:: 3 (nil))))))
3636

37-
(example (reverse (list 1 2 3)))
37+
(example (reverse (list (the Integer 1) 2 3)))
3838

3939
(define-macros
4040
([null (lambda (stx) (pure '(nil)))]))
@@ -48,5 +48,4 @@
4848

4949
(example (case (reverse null) [null 'a]))
5050

51-
(example (case (reverse (list 1 2 3)) [(head x) x]))
52-
51+
(example (case (reverse (list (the Integer 1) 2 3)) [(head x) x]))

examples/define-syntax-rule-test.kl

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,16 +9,16 @@
99
(define-macro (lambda2a x y body)
1010
(pure `(lambda (,x ,y) ,body)))
1111
(define mk-pair-a (lambda2a x y (pair x y)))
12-
(example (mk-pair-a 1 2))
12+
(example (mk-pair-a (the Integer 1) (the Integer 2)))
1313

1414
(define-variadic-macro (lambda2b stx)
1515
(case (open-syntax stx)
1616
[(list-contents (list _ x y body))
1717
(pure `(lambda (,x ,y) ,body))]))
1818
(define mk-pair-b (lambda2b x y (pair x y)))
19-
(example (mk-pair-b 1 2))
19+
(example (mk-pair-b (the Integer 1) (the Integer 2)))
2020

2121
(define-syntax-rule (lambda2c x y body)
22-
(lambda (x y) body))
22+
(lambda (x y) body))
2323
(define mk-pair-c (lambda2c x y (pair x y)))
24-
(example (mk-pair-c 1 2))
24+
(example (mk-pair-c (the Integer 1) (the Integer 2)))

examples/either-datatype.kl

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@
4949
(example (run-either integer->string id))
5050

5151
-- "42"
52-
(example (run-either integer->string id (left 42)))
52+
(example (run-either integer->string id (left (the Integer 42))))
5353

5454

5555
-- Next, let's define a variant of Either with three cases: leftmost, center,
@@ -115,10 +115,10 @@
115115
(pure `(:: ,x (nil)))]))))
116116

117117
-- (:: 42 (nil))
118-
(example (head 42))
118+
(example (head (the Integer 42)))
119119

120120
-- 1
121-
(example (case (list 1 2 3)
121+
(example (case (list (the Integer 1) 2 3)
122122
[(head x)
123123
x]))
124124

examples/eta-case.kl

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,13 @@
1919
[(:: x xs)
2020
(pair x xs)]
2121
[(nil)
22-
(pair 0 (nil))]))
22+
(pair (the Integer 0) (nil))]))
2323

2424
(example (list-to-pair (list)))
2525
-- =>
2626
-- (pair 0 (nil))
2727

28-
(example (list-to-pair (list 1 2 3)))
28+
(example (list-to-pair (list (the Integer 1) 2 3)))
2929
-- =>
3030
-- (pair 1 (list 2 3))
3131

@@ -332,11 +332,11 @@
332332
-- =>
333333
-- (lambda (x xs) (:: x xs))
334334

335-
(example (cons-lambda 1))
335+
(example (cons-lambda (the Integer 1)))
336336
-- =>
337337
-- (lambda (xs) (:: 1 xs))
338338

339-
(example (cons-lambda 1 (list 2 3)))
339+
(example (cons-lambda (the Integer 1) (list 2 3)))
340340
-- =>
341341
-- (list 1 2 3)
342342

@@ -381,40 +381,40 @@
381381
-- =>
382382
-- (lambda (x xs) (:: x xs))
383383

384-
(example (cons 1))
384+
(example (cons (the Integer 1)))
385385
-- =>
386386
-- (lambda (xs) (:: 1 xs))
387387

388-
(example (cons 1 (list 2 3)))
388+
(example (cons (the Integer 1) (list 2 3)))
389389
-- =>
390390
-- (list 1 2 3)
391391

392392
(example
393-
(case (list 1 2 3)
393+
(case (list (the Integer 1) 2 3)
394394
[(cons x xs)
395395
(pair x xs)]))
396396
-- =>
397397
-- (pair 1 (list 2 3))
398398

399399
-- All right, we can finally test eta-case and eta-case-aux!
400400
(example
401-
((eta-case-aux (list 1 2 3)
401+
((eta-case-aux (list (the Integer 1) 2 3)
402402
(cons)
403403
(pair)
404-
[(nil) (pair 0 (nil))])
404+
[(nil) (pair (the Integer 0) (nil))])
405405
(cons)))
406406
-- =>
407407
-- (pair 1 (list 2 3))
408408

409409
(defun eta-list-to-pair (xs0)
410410
(eta-case xs0
411411
[(cons) (pair)]
412-
[(nil) (pair 0 (nil))]))
412+
[(nil) (pair (the Integer 0) (nil))]))
413413

414414
(example (eta-list-to-pair (list)))
415415
-- =>
416416
-- (pair 0 (nil))
417417

418-
(example (eta-list-to-pair (list 1 2 3)))
418+
(example (eta-list-to-pair (list (the Integer 1) 2 3)))
419419
-- =>
420420
-- (pair 1 (list 2 3))

examples/fixnum.golden

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
12 : Integer
2+
255 : Word64
3+
26 : Word32
4+
22 : Word16
5+
12 : Word8
6+
12 : Int64
7+
12 : Int32
8+
12 : Int16
9+
0 : Int8
10+
8 : Int32
11+
92 : Int8
12+
-8 : Int8
13+
25 : Integer
14+
129 : Integer

examples/fixnum.kl

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
#lang "prelude.kl"
2+
3+
4+
(example (the Integer 0b1100)) -- 12
5+
(example (the Word64 0xFF)) -- 255
6+
(example (the Word32 0o32)) -- 26
7+
(example (the Word16 22))
8+
(example (the Word8 12))
9+
(example (the Int64 12))
10+
(example (the Int32 12))
11+
(example (the Int16 12))
12+
(example (the Int8 256)) -- note no bounds checking with the primitives this is 0
13+
(example (int32-popcount 0xFF)) -- 8
14+
(example (int8-+ 42 50))
15+
(example (int8-compliment 7)) -- will be -8 because 2's complement
16+
(example (+ 0b0111 0o22))
17+
(example (+ 0b0111 0x7A))

examples/free-identifier-case-test.kl

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,13 @@
99
(syntax-case stx
1010
[(list (_ x))
1111
(free-identifier-case x
12-
[if (pure '1)]
13-
[else (pure '2)]
14-
[define (pure '3)]
15-
[#%app (pure '4)]
12+
[if (pure '(the Integer 1))]
13+
[else (pure '(the Integer 2))]
14+
[define (pure '(the Integer 3))]
15+
[#%app (pure '(the Integer 4))]
1616
[(else y) (pure y)])]))]))
1717

18-
(define hello 32)
18+
(define hello (the Integer 32))
1919
(example (test if))
2020
(example (test else))
2121
(example (test define))

examples/hygiene.kl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,14 +39,14 @@
3939
(let [,misc-id 'other]
4040
x)))]))]))
4141
(example
42-
((should-be-id x) 5))
42+
((should-be-id x) (the Integer 5)))
4343

4444
-- The following example demonstrates that we can have two (define x ...)
4545
-- declarations with overlapping scopes without any accidental captures.
4646
(define-syntax-rule (define-six misc-id)
4747
(group
4848
(define x "defined six")
49-
(define misc-id 6)
49+
(define misc-id (the Integer 6))
5050
(example x)))
5151
(define-six x)
5252
(example x)

0 commit comments

Comments
 (0)