Pietro Menna Home page

Writing my first Interpreter

Talking with Brad on Wednesday, he showed to me an Interpreter for BrainFuck he written in Go. I really liked his idea, so I also implemented mine but in JS (I have never written or seen before an interpreter).

The interpreter

I am impresed which such a cool name there is for a switch statement with some bunch of extra variables.

Since Brain F* only contains few keywords: ‘>’, ‘<’, ‘]’, ‘[’, ‘+’, ‘-‘, ‘,’ and ‘.’. It was very quick to implement it.

Now I am wondering if all the interpreted languages just have a huge switch for each keyword? I will have to figure this out!

My interpreter code

	this.run = function(){
		while(instruction_pointer<instructions.length){
			switch(instructions[instruction_pointer]) {
				case '>':
					data_pointer = data_pointer + 1;
					break;
				case '<':
					data_pointer = data_pointer - 1;
					break;
				case '+':
					memory[data_pointer] = memory[data_pointer] + 1;
					break;
				case '-':
					memory[data_pointer] = memory[data_pointer] - 1;
					break;
				case '.':
					output = String.fromCharCode(memory[data_pointer]);
					var current_output = document.getElementById("output").innerHTML;
					current_output = current_output + output;
					document.getElementById("output").innerHTML = current_output;
					console.log(String.fromCharCode(memory[data_pointer]));
					break;
				case ',':
					var character = prompt("Input a character to be stored");
					memory[data_pointer] = character.charCodeAt(0);
					break;
				case '[':
					if (memory[data_pointer] == 0) {
						instruction_pointer = this.findMatching(instruction_pointer);
						continue;
					}
					break;
				case ']':
					if (memory[data_pointer] != 0){
						instruction_pointer = this.findMatching(instruction_pointer);
						continue;
					}
					break;
			}
			instruction_pointer = instruction_pointer + 1;
		}
	}

#Live run your Brain F*ck code below #

Brain F*ck code goes here!

</br>

Output of the program

Thanks a lot Brad for the idea and help during the implementation!

How I began with Clojure - My first steps

During my lunch today at Recurse Center, I don’t know how, but luckily in the table I was sitting, there were only people interested in learning Clojure. I was really glad since this did not happen in my Batch. (most Spring 2 2015 colleagues were interested in learning Haskell).

In this post, I plan to write about what I think would have been useful to me in those first days.

  1. Start by installing correctly leiningen. Do not use brew install leiningen. Install manually!
  2. Try the Clojure Workshop training.
  3. Do lots of 4clojure exercises!
  4. Get a Clojure Book. I think some Clojure Books are available at RC Libary. I got the first edition of Joy of Clojure which David gave me as present.
  5. Try an easy project on your own. Maybe do one you already did in the past.

At this point I can share that I have not done the 5th. I tried to do the BitTorrent Client, but this project has problems on its own. Also java has it’s quirks which makes it difficult to do it. At lost focus and energy trying to do this project. So the advice is again do something you did already in a language you master, and try to port it.

The last but not least I would have told me work in groups. Unfortunately I did not have that resource. But I received some great help from the people whom I got to pair at least for a small time (Charlie, Mary Rose and Zach).

#Some resources which I found be useful#

  • Grimoire: A Cheatsheet of many Clojure functions presented in a nice way!
  • ClojureDocs: Is the community-powered documentation and examples repository.
  • Clojure Werkz A growing collection of open source Clojure libraries

#What I am currently doing with Clojure?#

Still doing, 4clojure exercises, and I am also trying to port an App I did in group which is Follow Recursers to Clojure. This App is currently written in JS.

If you would like to join me, you are more than welcome!

Game of Life!

Today I paired with Erisa in order to implement the Game of Life. The code can be found here.

Initially I thought that this was a very complex problem, but if you read from wikipedia article, it can be coded with only few rules. This was one of the problems which I never attacked because I thought it would be really hard.