/* Skrypt ARexx'a dla Leto. Skrypt napisaî Henryk Cygert. Gdaïsk 1995
** Kalkulator ($10-Hex, 16-Dec, %10000-Bin, +,-,*,/,.)
** np.	$10-10+%110 da wynik 12
**	$10-10+%110*3 da wynik 36 (sorry za takie mnoûenie i dzielenie)
*/



Options results

CR='0A'X

InfoText 'Podaj wyraûenie do obliczenia'
if rc then Exit
String=LT_Info
Znak='+'	/* dodawaj */
Wynik=0		/* wynik */
i=1
Max=Length(String)

do Until Error=1 | i>Max
	Error=0
	Skladnik=0
	A=SUBSTR(String, i, 1)
	if A='-' | A='+' | A='*' | A='/' then Znak=A
	else
		do
		Call ObliczLiczbe
		Call SprawdzZnak
		i=i-1
		end
	i=i+1
end

if Error then Info1 30 'Bîâd wyraûenia|Wystâpiî bîâd w wyraûeniu'
else Text Wynik

Exit


SprawdzZnak:
	if Znak='+' then
		do
		call Dodaj
		Return
		end
	if Znak='-' then
		do
		call Odejmij
		Return
		end
	if Znak='*' then
		do
		call Mnoz
		Return
		end
	if Znak='/' then 
		do
		call Dziel
		Return
		end
	Return



Dodaj:
	Wynik=Wynik+Skladnik
	Return
Odejmij:
	Wynik=Wynik-Skladnik
	Return
Mnoz:
	Wynik=Wynik*Skladnik
	Return
Dziel:
	Wynik=Wynik/Skladnik
	Return



ObliczLiczbe:
	if A='$' then
		do
		call DoHex
		Return
		end
	if A='%' then
		do
		call DoBin
		Return
		end
	if A>='0' & A<='9' then
		do
		call DoInt
		Return
		end
	Error=1
	Return


DoInt:
	Ptr=i
	do until ((B<'0') | (B>'9')) ^ B='.'
		i=i+1
		B=Substr( String, i, 1)
	end
	Skladnik=Substr( String, Ptr, i-Ptr)
	Return


DoBin:
	Ptr=i
	Skladnik=0
	B='0'
	do until B~='0' & B~='1'
		Skladnik=Skladnik+Skladnik+B
		i=i+1
		B=Substr( String, i, 1)
	end
	Return


DoHex:
	Ptr=i
	B='0'
	Skladnik=0
	do until ((B<'0') | (B>'9')) & (B<'A' | B>'F') & (B<'a' | B>'f')
		Skladnik=16*Skladnik+GetHex(B)
		i=i+1
		B=Substr( String, i, 1)
	end
	Return

GetHex:
	arg Kod
	j=0
	do Until Substr('0123456789ABCDEFabcdef',j,1)=Kod
	j=j+1
	end
	if j>16 then j=j-7
	Return j-1



