History

if: make the else a list of forms with implicit begin

Sigrid Solveig Haflínudóttir committed 1 year ago

Commit 235b9081 parent 69965463 files · +128 −129Browse files

Showing the changes to src (2 of 3 files). Show all

Unified · Split

2 files
  1. src/compiler.sl +88 −91
  2. src/system.sl +28 −27
src/compiler.sl+88−91
@@ -23,9 +23,9 @@ · 22 unmodified lines
2323 (nconst (bcode:nconst b)))
2424 (if (has? const-to-idx v)
2525 (get const-to-idx v)
26− (begin (put! const-to-idx v nconst)
27− (prog1 nconst
28− (aset! b 2 (+ nconst 1)))))))
26+ (put! const-to-idx v nconst)
27+ (prog1 nconst
28+ (aset! b 2 (+ nconst 1))))))
2929
3030 (def (emit e inst . args)
3131 (def (load? i)
@@ -39,37 +39,36 @@ · 7 unmodified lines
3939 (aset! e 0 (cdr bc)))
4040 (else
4141 (aset! e 0 (cons inst bc)))))
42− (begin
43− (when (memq inst '(loadv loadg setg))
44− (set! args (list (bcode:indexfor e (car args)))))
45− (let ((longform
46− (assq inst '((loadv loadv.l) (loadg loadg.l) (setg setg.l)
47− (loada loada.l) (seta seta.l) (box box.l)))))
48− (when (and longform (> (car args) 255))
49− (set! inst (cadr longform))))
50− (let ((longform
51− (assq inst '((loadc loadc.l)))))
52− (when (and longform (> (car args) 255))
53− (set! inst (cadr longform))))
54− (if (eq? inst 'loada)
42+ (when (memq inst '(loadv loadg setg))
43+ (set! args (list (bcode:indexfor e (car args)))))
44+ (let ((longform
45+ (assq inst '((loadv loadv.l) (loadg loadg.l) (setg setg.l)
46+ (loada loada.l) (seta seta.l) (box box.l)))))
47+ (when (and longform (> (car args) 255))
48+ (set! inst (cadr longform))))
49+ (let ((longform
50+ (assq inst '((loadc loadc.l)))))
51+ (when (and longform (> (car args) 255))
52+ (set! inst (cadr longform))))
53+ (if (eq? inst 'loada)
54+ (cond ((equal? args '(0))
55+ (set! inst 'loada0 args NIL))
56+ ((equal? args '(1))
57+ (set! inst 'loada1 args NIL)))
58+ (when (eq? inst 'loadc)
5559 (cond ((equal? args '(0))
56− (set! inst 'loada0 args NIL))
60+ (set! inst 'loadc0 args NIL))
5761 ((equal? args '(1))
58− (set! inst 'loada1 args NIL)))
59− (when (eq? inst 'loadc)
60− (cond ((equal? args '(0))
61− (set! inst 'loadc0 args NIL))
62− ((equal? args '(1))
63− (set! inst 'loadc1 args NIL)))))
64− (let ((lasti (car bc)))
65− (cond ((and (eq? inst 'brn)
66− (cond ((eq? lasti 'not)
67− (aset! e 0 (cons (car args) (cons 'brnn (cdr bc)))))
68− ((eq? lasti 'eq?)
69− (aset! e 0 (cons (car args) (cons 'brne (cdr bc)))))
70− (else NIL))))
71− (else
72− (aset! e 0 (nreconc (cons inst args) bc)))))))
62+ (set! inst 'loadc1 args NIL)))))
63+ (let ((lasti (car bc)))
64+ (cond ((and (eq? inst 'brn)
65+ (cond ((eq? lasti 'not)
66+ (aset! e 0 (cons (car args) (cons 'brnn (cdr bc)))))
67+ ((eq? lasti 'eq?)
68+ (aset! e 0 (cons (car args) (cons 'brne (cdr bc)))))
69+ (else NIL))))
70+ (else
71+ (aset! e 0 (nreconc (cons inst args) bc))))))
7372 e))
7473
7574 (defmacro (make-label e) `(gensym))
@@ -97,47 +96,46 @@ · 21 unmodified lines
9796 (if (eq? vi 'label)
9897 (begin (put! label-to-loc (aref v (+ i 1)) (size-of bcode))
9998 (set! i (+ i 2)))
100− (begin
101− (io-write bcode
102− (get Instructions
103− (if long?
104− (case vi
105− (jmp 'jmp.l)
106− (brne 'brne.l)
107− (brnn 'brnn.l)
108− (brn 'brn.l)
109− (else vi))
110− vi)))
111− (set! i (+ i 1)
112− nxt (and (< i n) (aref v i)))
113− (cond ((memq vi '(jmp brne brnn brn))
114− (put! fixup-to-label (size-of bcode) nxt)
115− (io-write bcode ((if long? s32 s16) 0))
116− (set! i (+ i 1)))
117− ((eq? vi 'bounda)
118− (io-write bcode (s32 nxt) :little-endian)
119− (set! i (+ i 1)))
120− ((num? nxt)
121− (case vi
122− ((loadv.l loadg.l setg.l loada.l seta.l
123− argc.l vargc.l call.l tcall.l loadc.l box.l)
124− (io-write bcode (s32 nxt) :little-endian)
125− (set! i (+ i 1)))
126−
127− ((optargs keyargs) ; 2 s32 args
128− (io-write bcode (s32 nxt) :little-endian)
129− (set! i (+ i 1))
99+ (io-write bcode
100+ (get Instructions
101+ (if long?
102+ (case vi
103+ (jmp 'jmp.l)
104+ (brne 'brne.l)
105+ (brnn 'brnn.l)
106+ (brn 'brn.l)
107+ (else vi))
108+ vi)))
109+ (set! i (+ i 1)
110+ nxt (and (< i n) (aref v i)))
111+ (cond ((memq vi '(jmp brne brnn brn))
112+ (put! fixup-to-label (size-of bcode) nxt)
113+ (io-write bcode ((if long? s32 s16) 0))
114+ (set! i (+ i 1)))
115+ ((eq? vi 'bounda)
116+ (io-write bcode (s32 nxt) :little-endian)
117+ (set! i (+ i 1)))
118+ ((num? nxt)
119+ (case vi
120+ ((loadv.l loadg.l setg.l loada.l seta.l
121+ argc.l vargc.l call.l tcall.l loadc.l box.l)
122+ (io-write bcode (s32 nxt) :little-endian)
123+ (set! i (+ i 1)))
124+
125+ ((optargs keyargs) ; 2 s32 args
126+ (io-write bcode (s32 nxt) :little-endian)
127+ (set! i (+ i 1))
128+ (io-write bcode (s32 (aref v i)) :little-endian)
129+ (set! i (+ i 1))
130+ (when (eq? vi 'keyargs)
130131 (io-write bcode (s32 (aref v i)) :little-endian)
131− (set! i (+ i 1))
132− (when (eq? vi 'keyargs)
133− (io-write bcode (s32 (aref v i)) :little-endian)
134− (set! i (+ i 1))))
132+ (set! i (+ i 1))))
135133
136− (else
137− ; other number arguments are always u8
138− (io-write bcode (u8 nxt))
139− (set! i (+ i 1)))))
140− (else NIL)))))
134+ (else
135+ ; other number arguments are always u8
136+ (io-write bcode (u8 nxt))
137+ (set! i (+ i 1)))))
138+ (else NIL))))
141139
142140 (for-each
143141 (λ (addr labl)
@@ -242,9 +240,9 @@ · 98 unmodified lines
242240 (compile-in g env NIL rhs)
243241 (bcode:stack g -1)
244242 (emit g 'set-car!))
245− (begin (compile-in g env NIL rhs)
246− (unless arg? (error (str "internal error: misallocated var " s)))
247− (emit g 'seta idx)))))))
243+ (compile-in g env NIL rhs)
244+ (unless arg? (error (str "internal error: misallocated var " s)))
245+ (emit g 'seta idx))))))
248246
249247 (def (box-vars g env)
250248 (let loop ((e env))
@@ -457,19 +455,19 @@ · 206 unmodified lines
457455 (begin (let {[nargs (compile-arglist g env (cdr x))]}
458456 (bcode:stack g (- nargs))
459457 (compile-builtin-call g env tail? x head b nargs)))
460− (begin (compile-in g env NIL head)
461− (bcode:stack g 1)
462− (let* {[nargs (compile-arglist g env (cdr x))]
463− [loc (and (sym? head) (lookup-sym head env 0))]
464− [fn (and (eq? loc 'global) (bound? head) (top-level-value head))]
465− [code (and (closure? fn) (fn-code fn))]
466− [argc? (and code (= (aref code 0) (get Instructions 'argc)))]
467− [argc (and argc? (aref code 1))]}
468− (unless (or (not argc) (= argc nargs))
469− (compile-error "arity error: expected " argc ", got " nargs))
470− (bcode:stack g (- nargs))
471− (bcode:stack g -1)
472− (emit g (if tail? 'tcall 'call) nargs))))))))
458+ (compile-in g env NIL head)
459+ (bcode:stack g 1)
460+ (let* {[nargs (compile-arglist g env (cdr x))]
461+ [loc (and (sym? head) (lookup-sym head env 0))]
462+ [fn (and (eq? loc 'global) (bound? head) (top-level-value head))]
463+ [code (and (closure? fn) (fn-code fn))]
464+ [argc? (and code (= (aref code 0) (get Instructions 'argc)))]
465+ [argc (and argc? (aref code 1))]}
466+ (unless (or (not argc) (= argc nargs))
467+ (compile-error "arity error: expected " argc ", got " nargs))
468+ (bcode:stack g (- nargs))
469+ (bcode:stack g -1)
470+ (emit g (if tail? 'tcall 'call) nargs)))))))
473471
474472 ;; lambda, main compilation loop
475473
@@ -738,10 +736,9 @@ · 262 unmodified lines
738736 (if (not kwa)
739737 (emit g 'optargs nreq
740738 (if atail (- nargs) nargs))
741− (begin
742− (bcode:indexfor g (map 'vec car kwa))
743− (emit g 'keyargs nreq nkwa
744− (if atail (- nargs) nargs)))))
739+ (bcode:indexfor g (map 'vec car kwa))
740+ (emit g 'keyargs nreq nkwa
741+ (if atail (- nargs) nargs))))
745742
746743 (cond ((> nargs 255) (emit g (if atail 'vargc.l 'argc.l) nargs))
747744 (atail (emit g 'vargc nargs))
src/system.sl+28−27
@@ -170,14 +170,13 @@ · 169 unmodified lines
170170 (print-type term)
171171 (newline))
172172 sigs)
173− (begin
174− (princ lpad)
175− (if kind
176− (begin (princ (caddr term))
177− (print-type "doc group"))
178− (begin (print term)
179− (print-type term)))
180− (newline)))
173+ (princ lpad)
174+ (if kind
175+ (begin (princ (caddr term))
176+ (print-type "doc group"))
177+ (print term)
178+ (print-type term))
179+ (newline))
181180 (newline))
182181
183182 (defmacro (help topic (:print-header help-print-header) . rest)
@@ -234,9 +233,9 @@ · 50 unmodified lines
234233 (princ lpad)
235234 (princ (cons term sig)))
236235 sigs)
237− (begin (newline)
238− (princ lpad)
239− (princ term))))
236+ (newline)
237+ (princ lpad)
238+ (princ term)))
240239 (def (first-line s)
241240 (str-sub s 0 (str-find s "\n")))
242241 (def (doc-group? k)
@@ -1325,9 +1324,8 @@ · 1082 unmodified lines
13251324 (reverse! acc)
13261325 (if (has? ta (car l))
13271326 (loop (cdr l) acc)
1328− (begin
1329− (put! ta (car l) T)
1330− (loop (cdr l) (cons (car l) acc))))))))
1327+ (put! ta (car l) T)
1328+ (loop (cdr l) (cons (car l) acc)))))))
13311329
13321330 (def (find-if pred lst (:key identity)
13331331 (:start 0)
@@ -1793,16 +1791,16 @@ · 459 unmodified lines
17931791 (newline)
17941792 'ignored
17951793 'done)
1796− (begin (print 0)
1797− (newline)
1798− 'else))
1794+ (print 0)
1795+ (newline)
1796+ 'else)
17991797 ; ‼ 1
18001798 ; ↳ done»
18011799 :doc-group syntax
18021800 :doc-see prog1
18031801 (cons '$begin body))
18041802
1805−(defmacro (if cond then else)
1803+(defmacro (if cond then . else)
18061804 «Return the result of `then` if `cond` evaluated as "true", `else`
18071805 otherwise.
18081806
@@ -1813,14 +1811,18 @@ · 4 unmodified lines
18131811 Examples:
18141812
18151813 (def x '(1))
1816− (if x 'yes 0)
1814+ (if x (begin (print "yep!") (newline) 'yes) 0)
1815+ ; ‼ yep!
18171816 ; ↳ yes
1818− (if (str? x) 1 'no)
1817+ (if (str? x) 1 (print "nope!") (newline) 'no)
1818+ ; ‼ nope!
18191819 ; ↳ no»
18201820 :doc-group syntax
18211821 :doc-see when
18221822 :doc-see unless
1823− (list '$if cond then else))
1823+ (unless else
1824+ (arg-error "if without an else branch"))
1825+ (list '$if cond then (cons 'begin else)))
18241826
18251827 (defmacro (when cond . body)
18261828 «Shorthand for `(if cond body NIL)`.
@@ -2010,9 +2012,8 @@ · 183 unmodified lines
20102012 (if ,test-expr
20112013 (begin
20122014 ,@(cdr test-spec))
2013− (begin
2014− ,@commands
2015− (loop# ,.steps))))))
2015+ ,@commands
2016+ (loop# ,.steps)))))
20162017 (loop# ,.inits))))
20172018
20182019 ; SRFI 8
@@ -3073,9 +3074,9 @@ · 1054 unmodified lines
30733074 (when (void? v)
30743075 (*prompt*)
30753076 (io-flush *io-out*))
3076− (begin (print V)
3077− (set! that V)
3078− (newline)))
3077+ (print V)
3078+ (set! that V)
3079+ (newline))
30793080 #.(void)))))
30803081 (def (reploop)
30813082 (when (trycatch (prompt)

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t