Skip to content

Commit 384eaf4

Browse files
committed
adding boolean and and or macros
1 parent 2e7ffd8 commit 384eaf4

File tree

1 file changed

+64
-29
lines changed

1 file changed

+64
-29
lines changed

schemer.sch

Lines changed: 64 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -16,24 +16,25 @@
1616
;; along with toy-bytecode. If not, see <http://www.gnu.org/licenses/>.
1717

1818
; utility functions
19-
(define last (lambda (l) (if (null? l) l
20-
(if (not (pair? l)) l
21-
(if (not (pair? (cdr l))) l
22-
(if (null? (cdr l)) l
23-
(last (cdr l))))))))
19+
(define last (lambda (l) (if (and (not (null? l))
20+
(and (pair? l)
21+
(and (pair? (cdr l))
22+
(not (null? (cdr l))))))
23+
(last (cdr l))
24+
l)))
2425

2526
(define string-is-numeric? (lambda (str)
2627
(let ((first-char (string-ref str 0))
2728
(strlen (string-length str))
28-
(test (lambda (c v) (if (not v) v (char-is-digit? c)))))
29-
(if (= 0 strlen) #f
30-
(if (< 1 strlen)
31-
(if (if (char=? first-char #\-) #t
32-
(if (char=? first-char #\+) #t
33-
(char-is-digit? first-char)))
34-
(string-fold test #t str 1 (string-length str))
35-
#f)
36-
(char-is-digit? first-char))))))
29+
(test (lambda (c v) (if v (char-is-digit? c) v))))
30+
(and (< 0 strlen)
31+
(or (and (< 1 strlen)
32+
(and
33+
(or (char=? first-char #\-)
34+
(or (char=? first-char #\+)
35+
(char-is-digit? first-char)))
36+
(string-fold test #t str 1 (string-length str))))
37+
(char-is-digit? first-char))))))
3738

3839

3940
; The underlying VM uses a tagged memory model that differentiates between
@@ -139,6 +140,8 @@
139140
(cons "define" compile-define)
140141
(cons "begin" compile-begin)
141142
(cons "quote" compile-quote)
143+
(cons "and" compile-and)
144+
(cons "or" compile-or)
142145
)
143146
))
144147

@@ -148,9 +151,9 @@
148151
(lambda (f)
149152
(letrec ((helper (lambda (ss)
150153
(if (null? ss) #f
151-
(if (not (string? f)) #f
152-
(if (string=? f (car (car ss))) (cdr (car ss))
153-
(helper (cdr ss))))))))
154+
(if (string=? f (car (car ss)))
155+
(cdr (car ss))
156+
(helper (cdr ss)))))))
154157
(helper (special-forms)))))
155158

156159
; The top-level-env is a list containing the list of symbols
@@ -188,7 +191,8 @@
188191
; u-call-* below.
189192

190193
(define top-level-env
191-
(quote ((("=" "equal")
194+
(quote ((("equal?" "equal")
195+
("=" "equal")
192196
("<" "less_than")
193197
("null?" "null_q")
194198
("cons" "cons")
@@ -690,14 +694,12 @@
690694
(u-call-cons)
691695
(lambda ()
692696
(append-instruction (asm-label-definition label))
693-
(let ((param-list (car (cdr l)))
694-
(body (cdr (cdr l))))
695-
(let ((r (compile-sequence body
696-
(cons
697-
(process-params param-list)
698-
env) #f)))
699-
(assembly-funret)
700-
r))))))
697+
(let ((r (compile-sequence (cddr l)
698+
(cons
699+
(cadr l)
700+
env) #f)))
701+
(assembly-funret)
702+
r)))))
701703

702704
(define compile-let-bindings
703705
(lambda (bs env)
@@ -796,6 +798,42 @@
796798
(append-instruction ins-pop)
797799
r)))
798800

801+
(define compile-and
802+
(lambda (l env rest)
803+
(let ((out-label (fresh-label)))
804+
(let ((r1 (compile-sexp (cadr l) env #t))
805+
(xx (append-instructions ins-dup
806+
ins-push false-value
807+
ins-eq
808+
ins-push (asm-label-reference out-label)
809+
ins-jtrue
810+
ins-pop))
811+
(r2 (compile-sexp (caddr l) env #t)))
812+
(append-instruction (asm-label-definition out-label))
813+
(lambda ()
814+
(do-compile-task r1)
815+
(do-compile-task r2))))))
816+
817+
(define compile-or
818+
(lambda (l env rest)
819+
(let ((out-label (fresh-label))
820+
(t2-label (fresh-label)))
821+
(let ((r1 (compile-sexp (cadr l) env #t))
822+
(xx (append-instructions ins-dup
823+
ins-push false-value
824+
ins-eq
825+
ins-push (asm-label-reference t2-label)
826+
ins-jtrue
827+
ins-push (asm-label-reference out-label)
828+
ins-jmp
829+
(asm-label-definition t2-label)
830+
ins-pop))
831+
(r2 (compile-sexp (caddr l) env #t)))
832+
(append-instructions (asm-label-definition out-label))
833+
(lambda ()
834+
(do-compile-task r1)
835+
(do-compile-task r2))))))
836+
799837
; when we can detect application of a builtin
800838
; we can avoid function call overhead and just inline the assembly
801839
(define compile-if
@@ -1492,9 +1530,6 @@
14921530
(lambda () (parse-token (next-token))))
14931531

14941532
; then run the compiler.
1495-
; (display (read-datum (next-non-ws-skip-comments)))
1496-
; (newline)
1497-
14981533
(append-instructions
14991534
ins-push (asm-label-reference initial-env-label) ins-wtrr)
15001535
(compiler-run)

0 commit comments

Comments
 (0)