To attack problems like this, first describe a data type that captures the operations you want in your DSL, rather than concentrating on surface syntax. Once you've got the data type in hand, you should have a much easier time with the problem.
From a first glance, it looks like we can design 3 fundamental forms in your language:
- Strings
- Repetition
- Sequencing
We can represent this disjoint class with primitive strings and structures. Let's call this class as a whole a 'pexpr', for "printable expr". In code:
;; An pexpr is one of the following:
;; * a primitive string,
;; * a seq, or
;; * a repeat
(struct seq (bodies) #:transparent) ;; bodies is a list of pexpr
(struct repeat (n body) #:transparent) ;; n is a number, body is a pexpr
It might help to make some helper functions as abbreviations since "seq" and "repeat" are themselves a bit long-winded.
;; For convenience, we define some abbreviations s and r for seq and repeat,
;; respectively.
(define (s . bodies)
(seq bodies))
(define (r n . bodies)
(repeat n (seq bodies)))
Your example "I" string can be written as this:
(define an-example
(s
(r 3 (r 9 "X") "\n")
(r 6 (r 3 " ") (r 3 "X") "\n")
(r 3 (r 9 "X") "\n")))
Note that this encoding has an explicit representation for newlines which, from the surface syntax alone, is implicit. It then becomes the job of a parser to take lines in your surface syntax and turning them into pexprs, but that shouldn't be too difficult. Hopefully. :)
Anyway, the interpret function, then, becomes a matter of filling in the details for a template like this:
(define (interpret pexpr)
(match pexpr
[(? string?)
...]
[(struct seq (bodies))
...]
[(struct repeat (n body))
...]))
where the '...'s should be easy to fill in.
This approach to these kinds of problems is one described by How to Design Programs and Programming Languages: Application and Interpretation. I'd recommend looking at them: they're good stuff.
'X
and'b
would be a lot more natural to process than a string.