/*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; }
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()); }
int main(int argc, char* argv[]) { if (argc == 1) { // run GUI CalcApplication* app = new CalcApplication(); app->Run(); delete app; } else { // evaluate expression from command line BString expression; int32 i = 1; while (i < argc) { expression << argv[i]; i++; } try { ExpressionParser parser; BString result = parser.Evaluate(expression.String()); printf("%s\n", result.String()); } catch (ParseException e) { printf("%s at %" B_PRId32 "\n", e.message.String(), e.position + 1); return 1; } } return 0; }