#include <Array.au3>
#include <String.au3>

Opt("GUIOnEventMode", 1)

Global $hGui = Guicreate("BrainFuck - GUI", 500, 300)
GUISetOnEvent(-3, "_Exit", $hGui)
Global $Asc2BF_Input = GUICtrlCreateEdit("", 0, 25, 250, 100, 0x00200000)
Global $Asc2BF_Output = GUICtrlCreateEdit("", 0, 125, 250, 100, 0x00200000)

Global $BF2Asc_Input = GUICtrlCreateEdit("", 250, 25, 250, 100, 0x00200000)
Global $BF2Asc_Output = GUICtrlCreateEdit("", 250, 125, 250, 100, 0x00200000)

GUICtrlCreateLabel("AscII To BrainFuck", 0, 10,250, 15, 1)
GUICtrlCreateLabel("BrainFuck To AscII", 250, 10,250, 15, 1)

GUICtrlCreateButton("Asc2BF", 75, 230, 100, 23)
GUICtrlSetOnEvent(-1, "__Asc2BF")
GUICtrlCreateButton("BF2Asc", 250 + 75, 230, 100, 23)
GUICtrlSetOnEvent(-1, "__BF2Asc")

GUICtrlCreateLabel("Die Interpretierfunktionen für BrainFuck sind Alle von BugFix", 0, 270, 500, 16,1)

GUISetState(@SW_SHOW)

While 1
	Sleep(10)
WEnd

Func _Exit()
Exit
EndFunc

Func __Asc2BF()
	GUICtrlSetData($Asc2BF_Output, "Wird berechnet ...")
	Local $a = GUICtrlRead($Asc2BF_Input)
	GUICtrlSetData($Asc2BF_Output, _AsciiToBFcode($a))
EndFunc

Func __BF2Asc()
	GUICtrlSetData($BF2Asc_Output, "Wird berechnet ...")
	Local $a = GUICtrlRead($BF2Asc_Input)
	GUICtrlSetData($BF2Asc_Output, _BFtextInterpreter($a))
EndFunc

#cs
#####################################
Folgende Funktionen sind von BugFix
#####################################
#ce

Func _BFtextInterpreter($sBFcode)
	Local $aCode = StringSplit(StringStripWS($sBFcode, 8), '', 2)
	Local $sOut = '', $index = 0, $count, $iLoop = -1
	Local $a[30000], $pt = 0
	Do
		If $index < UBound($aCode) -1 Then
			$count = __CountEvenNeighbour($aCode, $index)
		Else
			$count = 0
		EndIf
		Switch $aCode[$index]
			Case '>'
				$pt += 1 + $count
				$index += 1 + $count
			Case '<'
				$pt -= (1 + $count)
				$index += 1 + $count
			Case '+'
				$a[$pt] += 1 + $count
				$index += 1 + $count
			Case '-'
				$a[$pt] -= 1 + $count
				$index += 1 + $count
			Case '['
				$iLoop = $index +1
				$index += 1 + $count
			Case ']'
				If $a[$pt] = 0 Then
					$iLoop = -1
					$index += 1 + $count
				Else
					$index = $iLoop
				EndIf
			Case '.'
				$sOut &= Chr($a[$pt])
				$index += 1
			Case ',' ; erwartet Input von Standardeingabe
;~ 	 			$a[$pt] = Asc('Input')
;~ 	 			$index += 1
		EndSwitch
	Until $index = UBound($aCode)
	Return $sOut
EndFunc  ;==>_BFtextInterpreter

Func __CountEvenNeighbour(ByRef $aCode, $iStart)
	Local $counter = 0
	While $aCode[$iStart +$counter +1] == $aCode[$iStart]
		$counter += 1
	WEnd
	Return $counter
EndFunc  ;==>__CountEvenNeighbour

Func _AsciiToBFcode($sString)
	Local $aString = StringSplit($sString, '', 2)
	Local $aWork[UBound($aString)][3], $sUniqueRounded
	; === ASCII-Werte ermitteln und die zugehörigen gerundeten Zehner-ASCII-Werte
	For $i = 0 To UBound($aString) -1
		$aWork[$i][0] = Asc($aString[$i])            ; ASCII-Wert des Zeichens
		$aWork[$i][1] = Round($aWork[$i][0] /10) *10 ; auf Zehnerwert auf/abgerundeter ASCII-Wert
		$aWork[$i][2] = $i                           ; Position im Ausgangsstring
	Next
	; === nach ASCII-Werten aufsteigend sortieren
	_ArraySort($aWork)
	; === gerundete Zehner-ASCII-Werte zusammenfassen
	$sUniqueRounded = $aWork[UBound($aWork)-1][1]
	For $i = UBound($aWork) -1 To 0 Step -1
		If $i > 0 Then
			If $aWork[$i][1] <> $aWork[$i-1][1] Then $sUniqueRounded = $aWork[$i-1][1] & ',' & $sUniqueRounded
		EndIf
	Next
	; === wieder nach Reihenfolge Zeichen sortieren
	_ArraySort($aWork, 0, 0,0,2)
	Local $aUnique = StringSplit($sUniqueRounded, ',', 2) ; Array mit gerundeten Zehner-ASCII-Werten
	; === je eine Zelle auf einen einmal vorkommenden gerundeten Zehner-ASCII-Wert setzen
	Local $sBF = '>++++++++++['                           ; Faktor 10 vor der Schleife erstellen
	For $i = 0 To UBound($aUnique) -1
		$sBF &= '>' & _StringRepeat('+', $aUnique[$i]/10) ; Faktor für Zehner-Multiplikation
	Next
	$sBF &= _StringRepeat('<', UBound($aUnique)) & '-]'   ; zurück auf Schleifenzähler und dekrementieren
	$sBF &= '>'                                           ; Pointer auf erste Wertezelle
	Local $index, $index_last = 0, $step = 0, $diff = 0, $nextStep, $backStep
	For $i = 0 To UBound($aString) -1
		$index = _ArraySearch($aUnique, $aWork[$i][1])
		$diff = $index - $index_last
		$index_last = $index
		Select
			Case $diff < 0
				$nextStep = _StringRepeat('<', Abs($diff))
			Case 0
				$nextStep = ''
			Case Else
				$nextStep = _StringRepeat('>', $diff)
		EndSelect
		$sBF &= $nextStep
		$diff = $aWork[$i][0] - $aWork[$i][1]            ; Differenz zw. tatsächlichem Wert und Round-Wert
		; === Zelle unter Pointer um $diff in/dekrementieren, Zeichen ausgeben, Wert zurücksetzen
		Select
			Case $diff < 0
				$nextStep = _StringRepeat('-', Abs($diff))
				$backStep = _StringRepeat('+', Abs($diff))
			Case 0
				$nextStep = ''
			Case Else
				$nextStep = _StringRepeat('+', $diff)
				$backStep = _StringRepeat('-', $diff)
		EndSelect
		If $i = UBound($aString) -1 Then $backStep = ''
		$sBF &= $nextStep & '.' & $backStep
	Next
	Return $sBF
EndFunc  ;==>_AsciiToBFcode

