-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathEsolang-Interpreters-#1---Introduction-to-Esolangs-and-My-First-Interpreter.ts
35 lines (28 loc) · 2.04 KB
/
Esolang-Interpreters-#1---Introduction-to-Esolangs-and-My-First-Interpreter.ts
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
/*
Time to write your first Esolang interpreter :D
Your task is to implement a MiniStringFuck interpreter myFirstInterpreter()/my_first_interpreter()/Interpreter()/interpret() MyFirstInterpreter() (depending on your language) which accepts exactly 1 required argument code/$code/strng which is the MiniStringFuck program to be executed. The output of the program should then be returned by your interpreter as a string.
Since this is the first time you are about to write an interpreter for an Esolang, here are a few quick tips:
If you are afraid that your interpreter will be confused by non-command characters appearing in the MiniStringFuck program, you can try to remove all non-command characters from the code input before letting your interpreter interpret it
The memory cell in MiniStringFuck only ever contains a single integer value - think of how it can be modelled in your interpreter
If you are stuck as to how to interpret the string as a program, try thinking of strings as an array of characters. Try looping through the "program" like you would an array
Writing an interpreter for an Esolang can sometimes be quite confusing! It never hurts to add a few comments in your interpreter as you implement it to remind yourself of what is happening within the interpreter at every stage
NOTE: If you would not like to name your interpreter as myFirstInterpreter()/my_first_interpreter(), you can optionally rename it to either miniStringFuck()/mini_string_fuck() or interpreter() instead - the test cases will handle the rest for you. Not available in Java, Go, Swift, TypeScript, Haskell, Elixir, C++, C#, Rust, R, Erlang, F#, COBOL and NASM; irrelevant to Brainfuck solvers ;)
Good luck :D
*/
// Answer:
export function myFirstInterpreter(code: string):string {
let current = 0;
let result = '';
for (let char of code) {
if (char === '+') {
current += 1;
if (current === 256) {
current = 0;
}
} else if (char === '.') {
result += String.fromCharCode(current);
}
}
return result;
}
// BigO: O(n)