mirror of
https://github.com/janet-lang/janet
synced 2024-11-06 00:36:17 +00:00
74 lines
2.7 KiB
Plaintext
74 lines
2.7 KiB
Plaintext
|
# Copyright (c) 2020 Calvin Rose & contributors
|
||
|
#
|
||
|
# Permission is hereby granted, free of charge, to any person obtaining a copy
|
||
|
# of this software and associated documentation files (the "Software"), to
|
||
|
# deal in the Software without restriction, including without limitation the
|
||
|
# rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
|
||
|
# sell copies of the Software, and to permit persons to whom the Software is
|
||
|
# furnished to do so, subject to the following conditions:
|
||
|
#
|
||
|
# The above copyright notice and this permission notice shall be included in
|
||
|
# all copies or substantial portions of the Software.
|
||
|
#
|
||
|
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||
|
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||
|
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||
|
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||
|
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
||
|
# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
|
||
|
# IN THE SOFTWARE.
|
||
|
|
||
|
(import ./helper :prefix "" :exit true)
|
||
|
(start-suite 8)
|
||
|
|
||
|
###
|
||
|
### Compiling brainfuck to Janet.
|
||
|
###
|
||
|
|
||
|
(def- bf-peg
|
||
|
"Peg for compiling brainfuck into a Janet source ast."
|
||
|
(peg/compile
|
||
|
~{:+ (/ '(some "+") ,(fn [x] ~(+= (DATA POS) ,(length x))))
|
||
|
:- (/ '(some "-") ,(fn [x] ~(-= (DATA POS) ,(length x))))
|
||
|
:> (/ '(some ">") ,(fn [x] ~(+= POS ,(length x))))
|
||
|
:< (/ '(some "<") ,(fn [x] ~(-= POS ,(length x))))
|
||
|
:. (* "." (constant (prinf "%c" (get DATA POS))))
|
||
|
:loop (/ (* "[" :main "]") ,(fn [& captures]
|
||
|
~(while (not= (get DATA POS) 0)
|
||
|
,;captures)))
|
||
|
:main (any (+ :s :loop :+ :- :> :< :.)) }))
|
||
|
|
||
|
(defn bf
|
||
|
"Run brainfuck."
|
||
|
[text]
|
||
|
(eval
|
||
|
~(let [DATA (array/new-filled 100 0)]
|
||
|
(var POS 50)
|
||
|
,;(peg/match bf-peg text))))
|
||
|
|
||
|
(defn test-bf
|
||
|
"Test some bf for expected output."
|
||
|
[input output]
|
||
|
(def b @"")
|
||
|
(with-dyns [:out b]
|
||
|
(bf input))
|
||
|
(assert (= (string output) (string b))
|
||
|
(string "bf input '"
|
||
|
input
|
||
|
"' failed, expected "
|
||
|
(describe output)
|
||
|
", got "
|
||
|
(describe (string b))
|
||
|
".")))
|
||
|
|
||
|
(test-bf "++++++++[>++++[>++>+++>+++>+<<<<-]>+>+>->>+[<]<-]>>.>---.+++++++..+++.>>.<-.<.+++.------.--------.>>+.>++." "Hello World!\n")
|
||
|
|
||
|
(test-bf ">++++++++[-<+++++++++>]<.>>+>-[+]++>++>+++[>[->+++<<+++>]<<]>-----.>->
|
||
|
+++..+++.>-.<<+[>[+>+]>>]<--------------.>>.+++.------.--------.>+.>+."
|
||
|
"Hello World!\n")
|
||
|
|
||
|
(test-bf "+[+[<<<+>>>>]+<-<-<<<+<++]<<.<++.<++..+++.<<++.<---.>>.>.+++.------.>-.>>--."
|
||
|
"Hello, World!")
|
||
|
|
||
|
(end-suite)
|