void CalcView::Evaluate() { BString expression = fExpressionTextView->Text(); if (expression.Length() == 0) { beep(); return; } _AudioFeedback(false); // evaluate expression BString value; try { ExpressionParser parser; parser.SetDegreeMode(fOptions->degree_mode); value = parser.Evaluate(expression.String()); } catch (ParseException e) { BString error(e.message.String()); error << " at " << (e.position + 1); fExpressionTextView->SetText(error.String()); return; } // render new result to display fExpressionTextView->SetValue(value.String()); }
/*static*/ status_t CalcView::_EvaluateThread(void* data) { CalcView* calcView = reinterpret_cast<CalcView*>(data); if (calcView == NULL) return B_BAD_TYPE; BMessenger messenger(calcView); if (!messenger.IsValid()) return B_BAD_VALUE; BString result; status_t status = acquire_sem(calcView->fEvaluateSemaphore); if (status == B_OK) { ExpressionParser parser; parser.SetDegreeMode(calcView->fOptions->degree_mode); BString expression(calcView->fExpressionTextView->Text()); try { result = parser.Evaluate(expression.String()); } catch (ParseException e) { result << e.message.String() << " at " << (e.position + 1); status = B_ERROR; } release_sem(calcView->fEvaluateSemaphore); } else result = strerror(status); BMessage message(kMsgDoneEvaluating); message.AddString(status == B_OK ? "value" : "error", result.String()); messenger.SendMessage(&message); return status; }