// Nine-digit GUI calculator.
// Works with LMNtal Java with '--wt' option.
// May not detect errneous input.
// Kill the process to finish.

{
	window.
	name("test").
	size(300, 300).
	killer.
	sys.perpetual(on).

	// オブジェクトの配置
	{textarea, id("answer"), position(0,0), text("0"), weight(1.0, 1.0), size(5,1)}.

	{button, id("0"), position(0,4), text("0"), weight(1.0, 1.0), size(2,1), num(0)}.
	{button, id("1"), position(0,3), text("1"), weight(1.0, 1.0), num(1)}.
	{button, id("2"), position(1,3), text("2"), weight(1.0, 1.0), num(2)}.
	{button, id("3"), position(2,3), text("3"), weight(1.0, 1.0), num(3)}.
	{button, id("4"), position(0,2), text("4"), weight(1.0, 1.0), num(4)}.
	{button, id("5"), position(1,2), text("5"), weight(1.0, 1.0), num(5)}.
	{button, id("6"), position(2,2), text("6"), weight(1.0, 1.0), num(6)}.
	{button, id("7"), position(0,1), text("7"), weight(1.0, 1.0), num(7)}.
	{button, id("8"), position(1,1), text("8"), weight(1.0, 1.0), num(8)}.
	{button, id("9"), position(2,1), text("9"), weight(1.0, 1.0), num(9)}.

	{button, id("pul"), position(3,1), text("+"), weight(1.0, 1.0), operator}.
	{button, id("min"), position(3,2), text("-"), weight(1.0, 1.0), operator}.
	{button, id("mul"), position(3,3), text("*"), weight(1.0, 1.0), operator}.
	{button, id("div"), position(3,4), text("/"), weight(1.0, 1.0), operator}.

	{button, id("clear"), position(4,1), text("c"), weight(1.0, 1.0), size(1,2)}.
	{button, id("equal"), position(4,3), text("="), weight(1.0, 1.0), size(1,2)}.


	//****以下ルール****// 
	
	numa(0).
	numb(0).
	ope("").

	pushint1@@ // 数字を押されたらaに数字を代入する。
	{clicked, num(_IX), $p}, numa(_IA)
	:-
	{num(_IX), $p}, numa(_IA * 10 + _IX), viewa.
	
	pushint2@@ // aの値をtextareaに表示する。
	numa(_IA), {textarea, text(_GT), $p}, viewa
	:-
	numa(_IA), {textarea, text(_IA), $p}.
	
	pushope@@ // 演算子が押されたら、演算子を保存して演算子を表示する。
	{clicked, text(_SL), operator, $p}, {textarea, text(_GT), $q}, numa(_IA), numb(_IB), ope(_SO)
	:-
	{text(_SL), operator, $p}, {textarea, text(_SL), $q}, numa(0), numb(_IA), ope(_SL).
	
	pushequal@@ // イコールが押されたら、結果を表示する(不正な場合は0になる)
	{clicked, text("="), $p}, numa(_IA), numb(_IB), ope("+"), {textarea, text(_GT), $q}
	:-
	{text("="), $p}, numa(0), numb(0), ope("+"), {textarea, text(_IB + _IA), $q}.
	
	{clicked, text("="), $p}, numa(_IA), numb(_IB), ope("-"), {textarea, text(_GT), $q}
	:-
	{text("="), $p}, numa(0), numb(0), ope("-"), {textarea, text(_IB - _IA), $q}.
	
	{clicked, text("="), $p}, numa(_IA), numb(_IB), ope("*"), {textarea, text(_GT), $q}
	:-
	{text("="), $p}, numa(0), numb(0), ope("*"), {textarea, text(_IB * _IA), $q}.
	
	{clicked, text("="), $p}, numa(_IA), numb(_IB), ope("/"), {textarea, text(_GT), $q}
	:-
	{text("="), $p}, numa(0), numb(0), ope("/"), {textarea, text(_IB / _IA), $q}.
	
	pushclear@@ // クリアが押されたら、全部初期化する。
	{clicked, text("c"), $p}, numa(_IA), numb(_IB), ope(_SO), {textarea, text(_GT), $q}
	:-
	{text("c"), $p}, numa(0), numb(0), ope(""), {textarea, text(0), $q}.

}