From 99af9b869240120f520376a33af5911cb134e79a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=89milie=20Feral?= Date: Thu, 3 Nov 2016 17:18:54 +0100 Subject: [PATCH] [apps/calculation] create a class evaluate context Change-Id: Ic2aa40a00dec0ff67aeeb63edcb1776c3825cfbe --- apps/calculation/Makefile | 1 + apps/calculation/evaluate_context.cpp | 27 +++++++++++++++++++++++++++ apps/calculation/evaluate_context.h | 22 ++++++++++++++++++++++ 3 files changed, 50 insertions(+) create mode 100644 apps/calculation/evaluate_context.cpp create mode 100644 apps/calculation/evaluate_context.h diff --git a/apps/calculation/Makefile b/apps/calculation/Makefile index 91c0e452e..638d36b49 100644 --- a/apps/calculation/Makefile +++ b/apps/calculation/Makefile @@ -2,6 +2,7 @@ app_objs += $(addprefix apps/calculation/,\ app.o\ calculation.o\ calculation_store.o\ + evaluate_context.o\ selectable_table_view.o\ history_view_cell.o\ history_controller.o\ diff --git a/apps/calculation/evaluate_context.cpp b/apps/calculation/evaluate_context.cpp new file mode 100644 index 000000000..08808a30e --- /dev/null +++ b/apps/calculation/evaluate_context.cpp @@ -0,0 +1,27 @@ +#include "evaluate_context.h" +#include + +namespace Calculation { + +EvaluateContext::EvaluateContext(::Context * parentContext, CalculationStore * calculationStore) : + m_ansValue(Float(0.0f)), + m_calculationStore(calculationStore), + m_context(parentContext) +{ +} + +Float * EvaluateContext::ansValue() { + Calculation * lastCalculation = m_calculationStore->calculationAtIndex(m_calculationStore->numberOfCalculations()-1); + m_ansValue = Float(lastCalculation->evaluation()); + return &m_ansValue; +} + +const Expression * EvaluateContext::expressionForSymbol(const Symbol * symbol) { + if (symbol->name() == Symbol::SpecialSymbols::Ans) { + return ansValue(); + } else { + return m_context->expressionForSymbol(symbol); + } +} + +} diff --git a/apps/calculation/evaluate_context.h b/apps/calculation/evaluate_context.h new file mode 100644 index 000000000..c79f1c9ba --- /dev/null +++ b/apps/calculation/evaluate_context.h @@ -0,0 +1,22 @@ +#ifndef CALCULATION_EVALUATECONTEXT_H +#define CALCULATION_EVALUATECONTEXT_H + +#include +#include "calculation_store.h" + +namespace Calculation { + +class EvaluateContext : public ::Context { + public: + EvaluateContext(Context * parentContext, CalculationStore * calculationStore); + Float * ansValue(); + const Expression * expressionForSymbol(const Symbol * symbol) override; + private: + Float m_ansValue; + CalculationStore * m_calculationStore; + ::Context * m_context; +}; + +} + +#endif \ No newline at end of file