From 15ebcc764c30c18f41f179d589ad1ad5a45194f1 Mon Sep 17 00:00:00 2001 From: Tom Smeding Date: Thu, 21 Nov 2019 12:57:59 +0100 Subject: Better string and IO support --- tests/interactive.lisp | 6 ++++++ tests/letrec.lisp | 6 ------ tests/stdlib.lisp | 29 +++++++++++++++++++++-------- tests/stringtest.lisp | 9 +++++++++ tests/stringtest.out | 1 + 5 files changed, 37 insertions(+), 14 deletions(-) create mode 100644 tests/interactive.lisp delete mode 100644 tests/letrec.lisp create mode 100644 tests/stringtest.lisp create mode 100644 tests/stringtest.out (limited to 'tests') diff --git a/tests/interactive.lisp b/tests/interactive.lisp new file mode 100644 index 0000000..d40758b --- /dev/null +++ b/tests/interactive.lisp @@ -0,0 +1,6 @@ +#include "stdlib.lisp" + +(sys-put-string stdout "line: ") +(sys-flush stdout) +(define str (read-line stdin)) +(print str) diff --git a/tests/letrec.lisp b/tests/letrec.lisp deleted file mode 100644 index 52a7de9..0000000 --- a/tests/letrec.lisp +++ /dev/null @@ -1,6 +0,0 @@ -(define fibo (n) - (let ((helper (lambda (m a b) - (if (= m n) b (helper m b (+ a b)))))) - (if (<= n 0) 0 - (if (<= n 2) 1 - (helper 2 1 1))))) diff --git a/tests/stdlib.lisp b/tests/stdlib.lisp index 380fa2a..96a55bb 100644 --- a/tests/stdlib.lisp +++ b/tests/stdlib.lisp @@ -2,6 +2,10 @@ (define caddr (x) (car (cdr (cdr x)))) (define cadddr (x) (car (cdr (cdr (cdr x))))) +(define not (x) (if x 0 1)) +(define or (x y) (if x 1 (if y 1 0))) +(define and (x y) (if x (if y 1 0) 0)) + (define YY (recur) (lambda (f) (lambda (a) (f ((recur recur) f) a)))) (define Y (YY YY)) @@ -10,6 +14,10 @@ (do (f start) (for (+ start 1) end f)) '())) +(define stdin (sys-stdin)) +(define stdout (sys-stdout)) +(define stderr (sys-stderr)) + (define with-open-file (path mode f) (let ((fid (sys-open-file mode path))) (let ((value (f fid))) @@ -17,12 +25,17 @@ (sys-close-file fid) value)))) +(define read-until (fid predicate) + (let ((helper (lambdarec rec (s) + (let ((ch (sys-get-char fid))) + (if (predicate ch) s (rec (concat s ch))))))) + (helper ""))) + +(define read-until-eof (fid) + (read-until fid null?)) + (define read-file (path) - (with-open-file path 0 (lambda (fid) - ; (print (sys-get-char fid)) - ; (print (let ((x (lambda (arg) fid))) (x 42))) - (let ((helper (Y (lambda (recur s) - (let ((ch (sys-get-char fid))) - (if (null? ch) s (recur (+ s ch)))))))) - (helper "")) - ))) + (with-open-file path 0 read-until-eof)) + +(define read-line (fid) + (read-until fid (lambda (ch) (or (= ch "\n") (null? ch))))) diff --git a/tests/stringtest.lisp b/tests/stringtest.lisp new file mode 100644 index 0000000..aa4898c --- /dev/null +++ b/tests/stringtest.lisp @@ -0,0 +1,9 @@ +#include "stdlib.lisp" + +(define str (read-file "tests/stringtest.lisp")) + +(for 0 (length str) (lambda (i) + (if (= (mod i 60) 0) + (sys-put-string stdout (concat "<" (substr str i 10) ">")) + '()))) +(print "") diff --git a/tests/stringtest.out b/tests/stringtest.out new file mode 100644 index 0000000..a0c457b --- /dev/null +++ b/tests/stringtest.out @@ -0,0 +1 @@ +<#include "> -- cgit v1.2.3-54-g00ecf