Skip to content

Commit ecf8723

Browse files
committed
Add write-string to stdlib.
1 parent eb4e9f5 commit ecf8723

File tree

6 files changed

+36
-5
lines changed

6 files changed

+36
-5
lines changed

langs/outlaw/a86/printer.rkt

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
#lang racket
2-
(provide asm-string current-shared?)
2+
(provide asm-string current-shared? asm-display)
33
(require "ast.rkt")
44

55
(define current-shared?
@@ -216,3 +216,27 @@
216216
(instrs->string a)
217217
#;
218218
(error "program does not have an initial label")])))
219+
220+
(define (asm-display a)
221+
(begin
222+
(set-box! external-labels '())
223+
;; entry point will be first label
224+
(match (findf Label? a)
225+
[(Label g)
226+
(begin
227+
(write-string
228+
(string-append
229+
tab "global " (label-symbol->string g) "\n"
230+
tab "default rel\n"
231+
tab "section .text\n"))
232+
(asm-display-instrs a))]
233+
[_
234+
(asm-display-instrs a)])))
235+
236+
(define (asm-display-instrs a)
237+
(match a
238+
['() (void)]
239+
[(cons i a)
240+
(begin (write-string (instr->string i))
241+
(write-string "\n")
242+
(asm-display-instrs a))]))

langs/outlaw/compile-library.rkt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,4 @@
88
(begin
99
(read-line) ; ignore #lang racket line
1010
(current-shared? #t)
11-
(displayln (asm-string (compile-library (parse-library (read-all)))))))
11+
(asm-display (compile-library (parse-library (read-all))))))

langs/outlaw/compile-stdin.rkt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,4 @@
99
(begin
1010
(read-line) ; ignore #lang racket line
1111
(current-shared? #t)
12-
(displayln (asm-string (compile (parse (read-all)))))))
12+
(asm-display (compile (parse (read-all))))))

langs/outlaw/compile.rkt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@
8181
string->uninterned-symbol
8282
open-input-file
8383
write-char error integer?
84-
eq-hash-code char-alphabetic? char-whitespace? displayln
84+
eq-hash-code char-alphabetic? char-whitespace? displayln write-string
8585
;; Op2
8686
+ - < = cons eq? make-vector vector-ref
8787
make-string string-ref string-append

langs/outlaw/stdlib.rkt

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
read-line
1616
char-alphabetic? char-whitespace?
1717
displayln ; only works for strings
18+
write-string
1819
; unimplemented
1920
exact->inexact / expt string->keyword
2021
;; Op0
@@ -559,10 +560,14 @@
559560

560561
(define (displayln s)
561562
(if (string? s)
562-
(begin (map write-char (string->list s))
563+
(begin (write-string s)
563564
(write-char #\newline))
564565
(error "unimplemented displayln for non-strings")))
565566

567+
(define (write-string s)
568+
(begin (map write-char (string->list s))
569+
(string-length s)))
570+
566571
(define (exact->inexact x)
567572
(error "exact->inexact not implemented"))
568573

langs/outlaw/test/test-runner.rkt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -725,6 +725,8 @@
725725
(cons (void) "a"))
726726
(check-equal? (run "" '(write-char #\newline))
727727
(cons (void) "\n"))
728+
(check-equal? (run "" '(write-string "hello world"))
729+
(cons (void) "hello world"))
728730
(check-equal? (run "" '(displayln "hello world"))
729731
(cons (void) "hello world\n"))
730732
)

0 commit comments

Comments
 (0)