User:999eagle/Brainfuck Interpreter

From Esolang
Jump to navigation Jump to search

This is an interpreter for brainfuck written in LOLcode. It was tested with the lci interpreter. Due to limitations of LOLcode this interpreter supports input and output only for chars with ASCII-Codes between 32 and 127 and only output for the new line character (ASCII 10). All other characters are output as " " and input as 32.

#!lci

HAI 1.3
CAN HAS STDIO?
CAN HAS STRING?

O HAI IM charlib
	BTW                  		0         10         20        30 33 35   40        50
	BTW                  		|         |          |         |  |  |    |         |
	I HAS A mapping ITZ 	   "          :)                      !:"#$%&'()*+,-./012"
	BTW                         |          |         |         |         |         |
	mapping R SMOOSH mapping AN "3456789::;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_`abcd" MKAY
	mapping R SMOOSH mapping AN "efghijklmnopqrstuvwxyz{|}~" MKAY
	
	HOW IZ I gettinCharFrom YR asciiCode
		DIFFRINT 126 AN BIGGR OF 126 AN asciiCode, O RLY?
			YA RLY, asciiCode R 32
			OIC
		DIFFRINT 0 AN SMALLR OF 0 AN asciiCode, O RLY?
			YA RLY, asciiCode R 32
			OIC
		FOUND YR I IZ STRING'Z AT YR ME'Z mapping AN YR asciiCode MKAY
	IF U SAY SO
	
	HOW IZ I gettingAsciiCodeFrom YR char
		BOTH SAEM char AN " ", O RLY?
			YA RLY, FOUND YR 32
			OIC
		IM IN YR loop UPPIN YR var TIL BOTH SAEM var AN 127
			I HAS A c ITZ STRING IZ AT YR ME'Z mapping AN YR var MKAY
			BOTH SAEM c AN char, O RLY?
				YA RLY, FOUND YR var
				OIC
		IM OUTTA YR loop
		FOUND YR 32
	IF U SAY SO
KTHX

O HAI IM pile
	I HAS A length ITZ 0
	I HAS A max ITZ -1
	
	HOW IZ I pushin YR item
		DIFFRINT ME'Z max AN BIGGR OF ME'Z max AN ME'Z length, O RLY?
			YA RLY, ME HAS A SRS ME'Z length ITZ item, ME'Z max R SUM OF ME'Z max AN 1
			NO WAI, ME'Z SRS ME'Z length R item
		OIC
		ME'Z length R SUM OF ME'Z length AN 1
	IF U SAY SO
	
	HOW IZ I popin
		DIFFRINT ME'Z length AN 0, O RLY?
		YA RLY
			ME'Z length R DIFF OF ME'Z length AN 1
			I HAS A item ITZ ME'Z SRS ME'Z length
			ME'Z SRS ME'Z length R NOOB
			FOUND YR item
		OIC
	IF U SAY SO
	
	HOW IZ I gettinLen
		FOUND YR ME'Z length
	IF U SAY SO
KTHX

I HAS A fielnaem ITZ A YARN
I HAS A fiel ITZ A NOOB
IM IN YR loop
	VISIBLE "Enter fielnaem: "!
	GIMMEH fielnaem
	fiel R I IZ STDIO'Z OPEN YR fielnaem AN YR "r" MKAY
	I IZ STDIO'Z DIAF YR fiel MKAY, O RLY?
	YA RLY
		VISIBLE "Oh noes! Fiel not fund!"
		I IZ STDIO'Z CLOSE YR fiel MKAY
	NO WAI
		GTFO
	OIC
IM OUTTA YR loop

I HAS A data ITZ I IZ STDIO'Z LUK YR fiel AN YR 1024 MKAY
I HAS A len ITZ I IZ STRING'Z LEN YR data MKAY
I IZ STDIO'Z CLOSE YR fiel MKAY

BTW VISIBLE "Len iz " AN len
VISIBLE "Data iz " AN data

I HAS A tape ITZ A BUKKIT
I HAS A pos ITZ 0
I HAS A min ITZ 0
I HAS A max ITZ 0
tape HAS A SRS pos ITZ 0
I HAS A looppile ITZ LIEK A pile
I HAS A jumpin ITZ -1

IM IN YR loop UPPIN YR var TIL BOTH SAEM var AN len
	I HAS A char ITZ I IZ STRING'Z AT YR data AN YR var MKAY
	BTW VISIBLE char
	BOTH SAEM jumpin AN -1, O RLY?											BTW if jumpin=-1 execute normally
		YA RLY
			char, WTF?
				OMG ">"
					pos R SUM OF pos AN 1
					DIFFRINT max AN BIGGR OF pos AN max, O RLY?
						YA RLY
							tape HAS A SRS pos ITZ 0
							max R pos
							GTFO
						OIC
					GTFO
				OMG "<"
					pos R DIFF OF pos AN 1
					DIFFRINT min AN SMALLR OF pos AN min, O RLY?
						YA RLY
							tape HAS A SRS pos ITZ 0
							min R pos
							GTFO
						OIC
					GTFO
				OMG "+"
					tape'Z SRS pos R SUM OF tape'Z SRS pos AN 1
					GTFO
				OMG "-"
					tape'Z SRS pos R DIFF OF tape'Z SRS pos AN 1
					GTFO
				OMG ","
					I HAS A temp ITZ A YARN
					GIMMEH temp
					temp R STRING IZ AT YR temp AN YR 0 MKAY
					tape'Z SRS pos R charlib IZ gettingAsciiCodeFrom YR temp MKAY
					GTFO
				OMG "."
					VISIBLE charlib IZ gettinCharFrom YR tape'Z SRS pos MKAY!
					GTFO
				OMG "["
					DIFFRINT tape'Z SRS pos AN 0, O RLY?
						YA RLY
							BTW VISIBLE "loop entered, cell is " AN tape'Z SRS pos
							looppile IZ pushin YR var MKAY
						NO WAI
							BTW VISIBLE "jump over loop"
							jumpin R 0
						OIC
					GTFO
				OMG "]"
					DIFFRINT tape'Z SRS pos AN 0, O RLY?
						YA RLY
							BTW VISIBLE "jump to loop start"
							var R DIFF OF looppile IZ popin MKAY AN 1
						NO WAI
							BTW VISIBLE "loop left"
							looppile IZ popin MKAY
						OIC
					GTFO
				OIC
		NO WAI																BTW if jumpin > -1 jump to ]
			char, WTF?
				OMG "["
					jumpin R SUM OF jumpin AN 1
					GTFO
				OMG "]"
					jumpin R DIFF OF jumpin AN 1
					GTFO
				OIC
		OIC
IM OUTTA YR loop
VISIBLE "done, printing tape"
IM IN YR loop UPPIN YR var TIL BOTH SAEM DIFF OF max AN min AN var
	VISIBLE tape'Z SRS SUM OF min AN var AN "|"!
IM OUTTA YR loop
VISIBLE tape'Z SRS max
KTHXBYE