Fix some normalization issues, add codegen tests
authorLuke Lau <luke_lau@icloud.com>
Mon, 22 Jul 2019 01:37:02 +0000 (02:37 +0100)
committerLuke Lau <luke_lau@icloud.com>
Mon, 22 Jul 2019 01:37:02 +0000 (02:37 +0100)
codegen.scm
tests.scm
typecheck.scm

index 04816ab75d533b6b950ff7fe17813cfce1880383..d30adc54e61a144097c3b0bb541a5835af8c028a 100644 (file)
@@ -59,6 +59,8 @@
   (for-each (lambda (form) (codegen-expr form inner-si inner-env)) body)))
 
 (define (codegen-var name si env)
+  (when (not (assoc name env))
+    (error #f (format "Variable ~a is not bound" name)))
   (let ((offset (cdr (assoc name env))))
     (emit "movq ~a(%rsp), %rax" offset)))
 
     (amd64-abi
      (lambda () (codegen-expr xform-prog 0 '())))))
 
-(define (compile-to-binary program)
+(define (compile-to-binary program output)
   (when (not (eq? (typecheck program) 'int)) (error #f "not an int"))
   (let ([tmp-path "/tmp/a.s"])
     (when (file-exists? tmp-path) (delete-file tmp-path))
     (with-output-to-file tmp-path
       (lambda () (codegen program)))
-    (system "clang -fomit-frame-pointer /tmp/a.s rts.c")))
+    (system (format "clang -fomit-frame-pointer /tmp/a.s rts.c -o ~a" output))))
index 39ca24011c249c1ff0c239bd293b20a1b761888a..964b6443fec10001cb73798836c33a2b6a919e9c 100644 (file)
--- a/tests.scm
+++ b/tests.scm
@@ -1,3 +1,4 @@
+(load "codegen.scm")
 (load "typecheck.scm")
 
 (define (test actual expected)
@@ -6,5 +7,30 @@
           (format "test failed:\nexpected: ~a\nactual:   ~a"
                   expected actual))))
 
+(define (read-file file)
+  (call-with-input-file file
+    (lambda (p)
+      (let loop ((next (read-char p))
+                 (result ""))
+        (if (eof-object? next)
+            result
+            (loop (read-char p) (string-append result (string next))))))))
+
+(define (test-prog prog output)
+  (compile-to-binary prog "/tmp/test-prog")
+  (system "/tmp/test-prog > /tmp/test-output.txt")
+  (let ((str (read-file "/tmp/test-output.txt")))
+    (test (substring str 0 (- (string-length str) 1))
+         output)))
+
 (test (typecheck '(lambda (x) (+ ((lambda (y) (x y 3)) 5) 2)))
       '(abs (abs int (abs int int)) int))
+
+(test-prog '(+ 1 2) "3")
+(test-prog '((lambda (x) ((lambda (y) (+ x y)) 42)) 100) "142")
+; todo: support recursive let
+(test-prog '(let ((x (+ 1 32))
+                 (y x))
+             ((lambda (z) (+ 1 z)) (* y x)))
+          "1090")
+
index 21b874c2fdc043b41297575d84bdd12132d559a5..55c2fd8201f2467ff3e136189412b6dc351780fd 100644 (file)
        (list 'lambda (lambda-args prog) (normalize (caddr prog)))))
    ((app? prog)
     (if (null? (cddr prog))
-       (cons (normalize (car prog)) (normalize (cdr prog))) ; (f a)
-       (list (list (normalize (car prog)) (normalize (cadr prog))) (normalize (caddr prog))))) ; (f a b)
+       `(,(normalize (car prog)) ,(normalize (cadr prog))) ; (f a)
+       `(,(list (normalize (car prog)) (normalize (cadr prog)))
+         ,(normalize (caddr prog))))) ; (f a b)
+       ;; (list (list (normalize (car prog))
+       ;;          (normalize (cadr prog))) (normalize (caddr prog))))) ; (f a b)
    ((let? prog)
     (append (list 'let
-                 (map (lambda (x) (cons (car x) (normalize (cdr x))))
+                 (map (lambda (x) `(,(car x) ,(normalize (cadr x))))
                       (let-bindings prog)))
            (map normalize (let-body prog))))
    (else prog)))