1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35
|
// Main brainfuck syntax. Based on the BS language.
// Files start with a number, the space needed by the
// brainfuck program. Each file will be declared as its
// own function. A function takes a string for input, and
// returns a string, which is the output from the program.
delimiter = BFDelimiter;
void BFDelimiter();
BFDelimiter : "[ \n\r\t]*";
Str SNumber();
SNumber => v : "[0-9]+" v #constant;
BfExpr SFile();
SFile => BfExpr(pos, space, code) : SNumber space - SComment - SRoot code;
// Note: the {} chars are used to end blocks in other languages.
void SComment();
SComment : "[^<>,\.\+\-\[\]{}]*" #comment;
Array<BfToken> SRoot();
SRoot => Array<BfToken>() : ( SToken -> push - SComment )*;
BfToken SToken();
SToken => BfBack(pos) : "<";
SToken => BfFwd(pos) : ">";
SToken => BfInput(pos) : ",";
SToken => BfOutput(pos) : "\.";
SToken => BfInc(pos) : "\+";
SToken => BfDec(pos) : "\-";
SToken => BfLoop(pos, c) : "\[" #keyword, SRoot c, "\]" #keyword;
// Integration with BS.
lang.bs.SAtom => BfExpr(pos, space, code, input) :
"bf" #typeName, "(", lang.bs.SExpr(block) input, ",", SNumber space, ")", "{", SRoot code, "}";
|