#lang racket
(require (only-in srfi/13 string-tokenize))
(define (translator some-file)
(with-input-from-file some-file
(lambda ()
(let loop ((line (read-line)) (acc '()))
(if (eof-object? line)
(with-output-to-file
(string-replace some-file "." "-rkt.")
(lambda ()
(let loop ((ls (reverse acc)))
(unless (null? ls)
(display (car ls))
(newline)
(loop (cdr ls))))))
(loop (read-line)
(cons
(if (char=? (string-ref line 0) #\#)
(if (char=? (string-ref line 1) #\#)
(format "(~a)" (string-trim
(string-replace line "##" "label")))
(let ((ls (string-tokenize
(string-trim (substring line 1)))))
(case (string->symbol (car ls))
((back) (format "(~a ~s)" (car ls) (cadr ls)))
((putChar)
(format "(~a ~s ~a)" (car ls)
(cadr ls) (caddr ls)))
((branch)
(format "(~a ~a ~s ~a ~s)"
(first ls) (second ls)
(third ls) (fourth ls) (fifth ls)))
((jump)
(format "(~a ~a)" (car ls) (cadr ls)))
(else
(format "(~a)" (car ls))))))
(format "(msg ~s)"
(string-trim
(string-replace line "\\n" "~%"))))
acc)))))))