Rename directory "engines" to "languages"

This commit is contained in:
Nilay Majorwar
2022-01-30 20:47:33 +05:30
parent 0bf7c0de3a
commit e3be5a8a83
82 changed files with 27 additions and 21 deletions

View File

@ -0,0 +1 @@
,[.,]

View File

@ -0,0 +1,19 @@
Calculate the value 256 and test if it's zero
If the interpreter errors on overflow this is where it'll happen
++++++++[>++++++++<-]>[<++++>-]
+<[>-<
Not zero so multiply by 256 again to get 65536
[>++++<-]>[<++++++++>-]<[>++++++++<-]
+>[>
# Print "32"
++++++++++[>+++++<-]>+.-.[-]<
<[-]<->] <[>>
# Print "16"
+++++++[>+++++++<-]>.+++++.[-]<
<<-]] >[>
# Print "8"
++++++++[>+++++++<-]>.[-]<
<-]<
# Print " bit cells"
+++++++++++[>+++>+++++++++>+++++++++>+<<<<-]>-.>-.+++++++.+++++++++++.<.
>>.++.+++++++..<-.>>-

View File

@ -0,0 +1,2 @@
>++++++++[-<+++++++++>]<.>>+>-[+]++>++>+++[>[->+++<<+++>]<<]>-----.>->
+++..+++.>-.<<+[>[+>+]>>]<--------------.>>.+++.------.--------.>+.>+.

View File

@ -0,0 +1,33 @@
+++++ +++ Set Cell #0 to 8
[
>++++ Add 4 to Cell #1; this will always set Cell #1 to 4
[ as the cell will be cleared by the loop
>++ Add 4*2 to Cell #2
>+++ Add 4*3 to Cell #3
>+++ Add 4*3 to Cell #4
>+ Add 4 to Cell #5
<<<<- Decrement the loop counter in Cell #1
] Loop till Cell #1 is zero
>+ Add 1 to Cell #2
>+ Add 1 to Cell #3
>- Subtract 1 from Cell #4
>>+ Add 1 to Cell #6
[<] Move back to the first zero cell you find; this will
be Cell #1 which was cleared by the previous loop
<- Decrement the loop Counter in Cell #0
] Loop till Cell #0 is zero
The result of this is:
Cell No : 0 1 2 3 4 5 6
Contents: 0 0 72 104 88 32 8
Pointer : ^
>>. Cell #2 has value 72 which is 'H'
>---. Subtract 3 from Cell #3 to get 101 which is 'e'
+++++ ++..+++. Likewise for 'llo' from Cell #3
>>. Cell #5 is 32 for the space
<-. Subtract 1 from Cell #4 for 87 to give a 'W'
<. Cell #3 was set to 'o' from the end of 'Hello'
+++.----- -.----- ---. Cell #3 for 'rl' and 'd'
>>+. Add 1 to Cell #5 gives us an exclamation point
>++. And finally a newline from Cell #6

View File

@ -0,0 +1,63 @@
import { readTestProgram, executeProgram } from "../../test-utils";
import { BFRS, serializeTapeMap } from "../common";
import Engine from "../runtime";
/**
* All test programs are picked up from https://esolangs.org/wiki/Brainfuck.
* - Cell cleanup code at end of cell size program is not included.
*/
/**
* Check if actual cell array matches expected cell array.
* Expected cell array must exclude trailing zeros.
* @param cellsMap Map of cell index to value, as provided in execution result.
* @param expected Array of expected cell values, without trailing zeros.
*/
const expectCellsToBe = (cellsMap: BFRS["tape"], expected: number[]) => {
const cells = serializeTapeMap(cellsMap);
expect(cells.length).toBeGreaterThanOrEqual(expected.length);
cells.forEach((value, idx) => {
if (idx < expected.length) expect(value).toBe(expected[idx]);
else expect(value).toBe(0);
});
};
describe("Test programs", () => {
// Standard hello-world program
test("hello world", async () => {
const code = readTestProgram(__dirname, "helloworld");
const result = await executeProgram(new Engine(), code);
expect(result.output).toBe("Hello World!\n");
expect(result.rendererState.pointer).toBe(6);
const expectedCells = [0, 0, 72, 100, 87, 33, 10];
expectCellsToBe(result.rendererState.tape, expectedCells);
});
// Hello-world program using subzero cell values
test("hello world with subzero cell values", async () => {
const code = readTestProgram(__dirname, "helloworld-subzero");
const result = await executeProgram(new Engine(), code);
expect(result.output).toBe("Hello World!\n");
expect(result.rendererState.pointer).toBe(6);
const expectedCells = [72, 0, 87, 0, 100, 33, 10];
expectCellsToBe(result.rendererState.tape, expectedCells);
});
// cat program
test("cat program", async () => {
const code = readTestProgram(__dirname, "cat");
const result = await executeProgram(new Engine(), code, "foo \n bar");
expect(result.output).toBe("foo \n bar");
expect(result.rendererState.pointer).toBe(0);
expectCellsToBe(result.rendererState.tape, []);
});
// Program to calculate cell size
test("cell size calculator", async () => {
const code = readTestProgram(__dirname, "cellsize");
const result = await executeProgram(new Engine(), code);
expect(result.output).toBe("8 bit cells");
expect(result.rendererState.pointer).toBe(4);
expectCellsToBe(result.rendererState.tape, [0, 32, 115, 108, 10]);
});
});