From e51b9e423665428c41cddac0642d1e34b18ca1da Mon Sep 17 00:00:00 2001 From: Luke Lau Date: Mon, 22 Jul 2019 02:37:02 +0100 Subject: [PATCH] Fix some normalization issues, add codegen tests --- codegen.scm | 6 ++++-- tests.scm | 26 ++++++++++++++++++++++++++ typecheck.scm | 9 ++++++--- 3 files changed, 36 insertions(+), 5 deletions(-) diff --git a/codegen.scm b/codegen.scm index 04816ab..d30adc5 100644 --- a/codegen.scm +++ b/codegen.scm @@ -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))) @@ -259,10 +261,10 @@ (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)))) diff --git a/tests.scm b/tests.scm index 39ca240..964b644 100644 --- 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") + diff --git a/typecheck.scm b/typecheck.scm index 21b874c..55c2fd8 100644 --- a/typecheck.scm +++ b/typecheck.scm @@ -57,11 +57,14 @@ (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))) -- 2.30.2