Skip to content

Commit 1b75e04

Browse files
committed
Add write-char and displayln (for strings).
1 parent 28e6505 commit 1b75e04

File tree

5 files changed

+31
-1
lines changed

5 files changed

+31
-1
lines changed

langs/outlaw/compile-ops.rkt

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,12 @@
153153
(pad-stack)
154154
(Call 'is_char_whitespace)
155155
(unpad-stack))]
156+
['write-char
157+
(seq (assert-char rax)
158+
(Mov rdi rax)
159+
(pad-stack)
160+
(Call 'print_codepoint_out)
161+
(unpad-stack))]
156162

157163
;; Op2
158164
['+

langs/outlaw/compile.rkt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@
8080
string->uninterned-symbol
8181
open-input-file
8282
write-char error integer?
83-
eq-hash-code char-alphabetic? char-whitespace?
83+
eq-hash-code char-alphabetic? char-whitespace? displayln
8484
;; Op2
8585
+ - < = cons eq? make-vector vector-ref
8686
make-string string-ref string-append
@@ -104,6 +104,7 @@
104104
peek_byte_port
105105
is_char_alphabetic
106106
is_char_whitespace
107+
print_codepoint_out
107108
system_type)))
108109

109110
(define cons-function

langs/outlaw/io.c

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
#define port_buffer_bytes 8
1010

1111
void utf8_encode_string(val_str_t *, char *);
12+
int utf8_encode_char(val_char_t, char *);
1213

1314
val_t read_byte(void)
1415
{
@@ -30,6 +31,14 @@ val_t write_byte(val_t c)
3031
return val_wrap_void();
3132
}
3233

34+
val_t print_codepoint_out(val_t c)
35+
{
36+
char buffer[5] = {0};
37+
utf8_encode_char(val_unwrap_char(c), buffer);
38+
fprintf(out, "%s", buffer);
39+
return val_wrap_void();
40+
}
41+
3342
val_t open_input_file(val_t in) {
3443
FILE *f;
3544
char *buf;

langs/outlaw/stdlib.rkt

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
findf
1515
read-line
1616
char-alphabetic? char-whitespace?
17+
displayln ; only works for strings
1718
; unimplemented
1819
exact->inexact / expt string->keyword
1920
;; Op0
@@ -65,6 +66,7 @@
6566
(define (zero? n) (%zero? n))
6667
(define (char? n) (%char? n))
6768
(define (write-byte b) (%write-byte b)) ; IMPROVE: add port
69+
(define (write-char c) (%write-char c))
6870
(define (eof-object? x) (%eof-object? x))
6971
(define (integer->char i) (%integer->char i))
7072
(define (char->integer c) (%char->integer c))
@@ -555,6 +557,12 @@
555557
(define (char-alphabetic? x) (%char-alphabetic? x))
556558
(define (char-whitespace? x) (%char-whitespace? x))
557559

560+
(define (displayln s)
561+
(if (string? s)
562+
(begin (map write-char (string->list s))
563+
(write-char #\newline))
564+
(error "unimplemented displayln for non-strings")))
565+
558566
(define (exact->inexact x)
559567
(error "exact->inexact not implemented"))
560568

langs/outlaw/test/test-runner.rkt

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -674,4 +674,10 @@
674674
(cons #\a ""))
675675
(check-equal? (run "ab" '(cons (read-char) (read-char)))
676676
(cons '(#\a . #\b) ""))
677+
(check-equal? (run "" '(write-char #\a))
678+
(cons (void) "a"))
679+
(check-equal? (run "" '(write-char #\newline))
680+
(cons (void) "\n"))
681+
(check-equal? (run "" '(displayln "hello world"))
682+
(cons (void) "hello world\n"))
677683
)

0 commit comments

Comments
 (0)