Skip to content

Commit 2711f9f

Browse files
committed
Add (limited) multiplication to stdlib.
1 parent 7696c1a commit 2711f9f

3 files changed

Lines changed: 27 additions & 1 deletion

File tree

langs/outlaw/compile.rkt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@
7070
system-type ;; hard-coded
7171
not findf
7272
read-line
73+
* ; limited
7374
exact->inexact / expt string->keyword ; unimplemented
7475
;; Op0
7576
read-byte peek-byte void

langs/outlaw/stdlib.rkt

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
not
1414
findf
1515
read-line
16+
* ; limited
1617
char-alphabetic? char-whitespace?
1718
displayln ; only works for strings
1819
write-string
@@ -92,6 +93,21 @@
9293
(define (procedure? x) (%procedure? x))
9394
(define (eq-hash-code x) (%eq-hash-code x))
9495

96+
(define (* x y)
97+
(match x
98+
[0 0]
99+
[1 y]
100+
[2 (arithmetic-shift y 1)]
101+
[4 (arithmetic-shift y 2)]
102+
[8 (arithmetic-shift y 3)]
103+
[10 ; 10a=2^3a+2a
104+
(+ (arithmetic-shift y 1)
105+
(arithmetic-shift y 3))]
106+
[16 (arithmetic-shift y 4)]
107+
[64 (arithmetic-shift y 6)]
108+
[_ (error "unimplemented multiplication")]))
109+
110+
95111
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
96112
;; Op2
97113
(define (+ . xs)

langs/outlaw/test/test-runner.rkt

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -668,7 +668,16 @@
668668
#f)
669669
(check-equal? (run '(apply string-append (list "x")))
670670
"x")
671-
)
671+
672+
(check-equal? (run '(* 0 8)) 0)
673+
(check-equal? (run '(* 1 8)) 8)
674+
(check-equal? (run '(* 2 9)) 18)
675+
(check-equal? (run '(* 2 -3)) -6)
676+
(check-equal? (run '(* 4 3)) 12)
677+
(check-equal? (run '(* 8 3)) 24)
678+
(check-equal? (run '(* 16 2)) 32)
679+
(check-equal? (run '(* 10 5)) 50)
680+
(check-equal? (run '(* 64 2)) 128))
672681

673682

674683
(define (test-runner-io run)

0 commit comments

Comments
 (0)