3
1
Introduction
Brainstract, a newly created dialect of Brainfuck adds a bit more elements to the syntax. In addition to the normal Brainfuck syntax, there are macros. To define a macro:
{macro_name ++++}
Then use it:
{macro_name} Adds 4 to the current cell
Challenge
Your challenge is to take a valid Brainstract program as input (command-line-arguments, function arguments, STDIN or file), and output a valid Brainfuck program that is equivalent to the Brainstract program (non-brainfuck characters are allowed in output). You may assume all input will be valid Brainstract.
Brainstract Spec
Whitespace counts as ANY whitespace (tabs, spaces, etc.)
A macro is defined by an opening brace character ({
) followed by a macro name which must be made up of any characters except whitespace, brainfuck, and Brainstract characters (anything except whitespace and {}[]<>+-.,
)
Then, it is followed by any amount of whitespace and a macro definition, made up of macro calls and non-brace characters and finally the macro is closed off with a closing brace (}
)
A macro call is of the form {macro_name}
where macro name is the same as above.
Examples
Format: Input -> Output
{cat ,.} {cat}{cat}{cat} -> ,.,.,.
{add [>+<-]} ++++>+<{add} -> ++++>+<[>+<-]
{recursive ,.{recursive}} -> (undefined behavior)
++++ {decrement -} ++++ {decrement} -> ++++++++-
{a >>>---<<<} {b {a}{a}} {b} -> >>>---<<<>>>---<<<
Could you perhaps add some test cases? Example Brainstract programs and their expected Brainfuck program outputs? – Kevin Cruijssen – 2018-09-19T13:42:47.253
@KevinCruijssen Will do – FireCubez – 2018-09-19T13:44:56.343
Could you also link to a Brainstract documentation? – Jonathan Frech – 2018-09-19T14:03:59.610
@JonathanFrech Brainstract is actually just a language I made for the purpose of this challenge – FireCubez – 2018-09-19T14:06:10.297
1@FireCubez Under those circumstances, I would suggest mentioning that the language is newly created and adding a more sophisticated language definition -- what counts as a macro_name, which characters can be used as macros, etc. – Jonathan Frech – 2018-09-19T14:19:06.633
Could a macro definition appear anywhere in the Brainstract source code? Or must it appear to the left of its first use? Or must it appear "before" its first use when taking into account how brainfuck moves around its source code? – ngm – 2018-09-19T14:23:14.240
2Is
{cat ,.} {cat3 {cat}{cat}{cat}} {cat3}
a valid input? – Arnauld – 2018-09-19T14:28:07.090@ngm It can appear anywhere – FireCubez – 2018-09-19T14:46:30.013
@Arnauld yes it is, added to question. – FireCubez – 2018-09-19T14:48:55.690
(repeating Jonathan Frech) What characters can appear in the macro name? Also, what characters can appear in the input? – user202729 – 2018-09-19T14:51:02.137
@user202729 Any non-whitespace and non-Brainstract/brainfuck characters can appear in the macro name. Input is valid Brainstract characters – FireCubez – 2018-09-19T14:55:51.353
@JonathanFrech Done. – FireCubez – 2018-09-19T14:57:58.390
Is it guaranteed that the macro name and definition must be a space, or can it be any whitespace character?
– user202729 – 2018-09-19T15:07:34.503May macro name be empty? – user202729 – 2018-09-19T15:08:22.863
@user202729 Any whitespace character – FireCubez – 2018-09-19T15:09:20.530
Let us continue this discussion in chat.
– FireCubez – 2018-09-19T15:09:32.547Using
->
to separate the input and output in the examples is kinda dumb... – Jo King – 2018-10-21T14:36:50.390If you have a better idea you can edit the post or tell me – FireCubez – 2018-10-22T14:00:29.910
@FireCubez Does the "Any non-whitespace and non-Brainstract/brainfuck characters can appear in the macro name. Input is valid Brainstract characters" still apply? If so, please edit it into the question – ASCII-only – 2018-12-22T10:21:24.003
@FireCubez it's dumb because both
-
and>
are valid brainfuck characters... – ASCII-only – 2018-12-22T10:21:56.323@ASCII-only Well, what I meant by that was "valid Brainstract" i.e. the macro names don't need to be valid brainstract characters – FireCubez – 2018-12-22T11:04:40.140
@FireCubez No I mean, most of the answers assume only alphanumeric names, which appears to not fit the spec. Plus, this specific part of the spec isn't in the question yet... – ASCII-only – 2018-12-22T11:22:03.710
@ASCII-only My original intention for names is the one I specified in the post now, but I wasn't so good at expressing my original intention when I posted this so now all the answers are different from my requirements – FireCubez – 2018-12-22T11:31:23.460