JSMeth
Jump to navigation
Jump to search
JSMeth is a JavaScript implementation of brainfuck. The commands are the same, but they're expressed as JS methods.
Syntax
The syntax is simple. The language only includes a Tape object, with the following methods, all of which return the Tape itself upon calls.
Method | Result |
---|---|
.moveLeft()
|
Moves the tape pointer to the next cell. |
.moveRight()
|
Moves the tape pointer to the previous cell. |
.inc()
|
Increments the value of the cell by 1. |
.dec()
|
Decrements the value of the cell. |
.loop(f)
|
Executes the function f while the value of the current cell isn't 0. |
.out()
|
Outputs the value of the current cell. |
.in()
|
Accepts one byte of input and stores it to the current cell. |
Example
Adder
The adder in BF is ,>,<[->+<]>.
. The equivalent JSMeth code is Tape.in().moveRight().in().moveLeft().loop(function(){this.dec().moveRight().inc().moveLeft();}).moveRight().out();
Hello World
The HW program in BF is ++++++++[>++++[>++>+++>+++>+<<<<-]>+>+>->>+[<]<-]>>.>---.+++++++..+++.>>.<-.<.+++.------.--------.>>+.>++.
.
In JSMeth, it's
Tape.inc().inc().inc().inc().inc().inc().inc().inc().loop(function(){this.moveRight().inc().inc().inc().inc().loop(function(){this.moveRight().inc().inc().moveRight().inc().inc().inc().moveRight().inc().inc().inc().moveRight().inc().moveLeft().moveLeft().moveLeft().moveLeft().dec();}).moveRight().inc().moveRight().inc().moveRight().dec().moveRight().moveRight().inc().loop(function(){this.moveLeft();}).moveLeft().dec();}).moveRight().moveRight().out().moveRight().dec().dec().dec().out().inc().inc().inc().inc().inc().inc().inc().out().out().inc().inc().inc().out().moveRight().moveRight().out().moveLeft().dec().out().moveLeft().out().inc().inc().inc().out().dec().dec().dec().dec().dec().dec().out().dec().dec().dec().dec().dec().dec().dec().dec().out().moveRight().moveRight().inc().out().moveRight().inc().inc().out();
Real JavaScript implementation
This is a real implementation of a Tape object constructor in JavaScript for browsers:
var Tape = function() { this.tape = [0]; this.index = 0; }; Tape.prototype.inc = function() { this.tape[this.index]++; return this; }; Tape.prototype.dec = function() { this.tape[this.index]--; return this; }; Tape.prototype.moveLeft = function() { this.index++; if (!this.tape[this.index]) this.tape[this.index] = 0; return this; }; Tape.prototype.moveRight = function() { this.index--; if (!this.tape[this.index]) this.tape[this.index] = 0; return this; }; Tape.prototype.out = function() { console.log(this.tape[this.index]); return this; }; Tape.prototype.in = function() { this.tape[this.index] = prompt(); return this; }; Tape.prototype.loop = function(f) { while (this.tape[this.index] != 0) f.call(this); return this; };