esolangs/melanjan.factor

48 lines
1.4 KiB
Factor
Executable File

! Copyright (C) 2022 Raghu Ranganathan.
! See http://factorcode.org/license.txt for BSD license.
USING: kernel math sequences locals arrays formatting strings combinators
io io.files io.encodings.ascii system command-line ;
IN: melanjan
: end ( str -- ) [ print flush ] with-output>error 0 exit ;
: dump ( a b c i seq -- ) "a=%d | b=%d | c=%d | i=%d\nProgram:\n%[%d, %]\n" printf flush ;
:: run-m ( dbg prg -- )
0 0 0 0 :> ( a! b! c! i! )
prg length :> l
[ l i = not ] [
i l >= i 0 < and [ "Instruction pointer 'i' out of program bounds, exiting" sprintf end ] when
i 1 + prg nth {
{ CHAR: a [ a ] }
{ CHAR: b [ b ] }
{ CHAR: c [ c ] }
{ CHAR: i [ i ] }
{ CHAR: A [ prg a nth ] }
{ CHAR: B [ prg b nth ] }
{ CHAR: C [ prg c nth ] }
{ CHAR: 1 [ 1 ] }
{ CHAR: o [ read1 ] }
[ drop f ]
} case
i prg nth {
{ CHAR: a [ [ a - a! ] ] }
{ CHAR: b [ [ b - b! ] ] }
{ CHAR: c [ [ c - c! ] ] }
{ CHAR: i [ [ i - i! ] ] }
{ CHAR: A [ [ a prg nth - a prg set-nth ] ] }
{ CHAR: B [ [ b prg nth - b prg set-nth ] ] }
{ CHAR: C [ [ c prg nth - c prg set-nth ] ] }
{ CHAR: o [ [ write1 ] ] }
[ drop f ]
} case
call( x -- )
i 2 + i!
dbg [ a b c i prg dump ] when
] while
;
(command-line) dup length 3 <
[ "USAGE: melanjan <file> [-debug]" end ] [ [ 3 ?nth ] [ 2 ?nth ] bi ] if
ascii file-contents >array run