mirror of
https://github.com/Xahau/xahaud.git
synced 2025-12-06 17:27:52 +00:00
0e7bac9Fix include pathe5bb90fFix constness of Proxyac0142aUse template castef6e381Add missing Url.h include206e65cFix constness of operator[]695cc38Use template instantiation for friend declaration7b1e03aAdd BaseFromMember49bc04fMake List<>::Node not uncopyabled5954ffAdd Journal to UnitTest58da106Temporarily disable ServiceQueue dtor precondition assertsfe58c1aAdd missing #include2c02580Add PropertyStream for server state introspection24c2315Add ScopedWrapperContexta3845f5Add RelativeTime::value_type typedef7442932Fix missing PropertyStream membersed5a98fMore PropertyStream output for PeerFinderfcfa10dAdd PropertyStream3cf0729Tidy up AbstractHandler usage in HTTPClient55171f4Remove obsolete source files1311ca3Increase arity of SharedFunction67d807dAdd IPEndpoint::key_equalebf395eAdd ErrorCode and boost library2c3ead3Add ServiceQueue::wrap6c7f5d0Move many Thread related classes93e9d86Measure CPU utilization in ServiceQueueca47d72Move ServiceQueue, ThreadLocalValue, SpinLockc864e4dMove WaitableEventff305e6Add CPUMeter and ScopedTimeInterval01fd05cAdd RecursiveMutex, UnlockGuard, TryLockGuard5831a53Remove Journal from most Stoppable overridesb60a7f3Add Request and Response HTTP parsers44445ffRefactor net buffers classesac37c38Beast class refactor8b7056bFix eof on HTTP client get228b664Remove obsolete beast container classes1dfd655Use RelativeTime from startup in DeadlineTimerae22d5dAdd more methods to RelativeTimec67929eRemove unhandled exception catcher2472a90Add 64 bit output for MurmurHashf3d97c7Add RelativeTime::fromStartupb0b8660IPEndpoint better parsingae551cdAdd alternate form string parsing to IPEndpointd0a0dbfDon't break on Throw0e46762Add hasher functors for IPEndpointa1ec423Add Thread::stopThreadAsync4f7dca3Add compiler, stdlib, and platform skeleton to beast/config4394594Tidy up some use of Error for throwe5e0f52Journal console output improvementsf07515eAdd Stoppable prepare and start interfacesd37dd46Move RelativeTime to chrono, add ostream support3f6e7aaAdd console feature to Journalad0064aJournal option to write to Output window (MSVC)0b7574bAdd compilation test scriptcc05ce1Add ServiceQueuee132aabUse boost for functional when the config is set026b926Fix is_continuation for boost versionc807a4eFix invoked_type type reference2ff781bRemove LockFreeStack::size3acb474Add SharedData::ConstAccess7e4c834Add LockFreeStack::empty9c61a6dAdded AbstractHandler, WrapHandler. HTTPClient Fixes.94e40dcFix unittest, by removing recursive call.38bf408Fix nonstandard C++ extension in getNullSink1ef044dBuild fixesd5d3746Fix missing <cmath> include for Gentoo5f231d3Update copyright notice and licenses7b89bf6Add FixedArray, IntrusiveArray, Crypto5c5de57Reorganize beast modules and files9e18bb3Merge commit '43deaaa5cf0d0178a4a6c3cb69c02a2a9a43ec7d' as 'src/beast/beast/http/impl/http-parser'57703acFix BeforeBoost.h includefbc247bAdd Stoppable to beast56496d8IPEndpoint comparisons9d9c822Migrate some headers and general tidying1a3cddcAdd SharedArg and AsyncObject373ca9cAdd HTTPRequest and improvements to HTTPMessage parsing9534516Add some thread classes and fix SharedData with a simple mutex adapter755ab36Make CallQueue unit test runManualc0ca037Remove Beast version printing on startup7efb6a3Reorganize some MPL and Utility classes and files69c26a1Fix missing BeastConfig.h include in Net.cpp40aa552Disable Beast version printing in Ripple BeastConfig.h7b1352dAdd InterruptibleThread unit test68cf759ThreadWithCallQueue unit test adjustment6501deaIPEndpoint parsing and tidying72fc42bMove and add some template metaprogramming classes2a164f0Change filname capitalization (end)6a14f25Change filename capitalization92fd417Move integer types to beast/CStdInt.hebbd9ffMove TargetPlatform.h to beast/Config.h874b524Add IPEndpoint14b34fcTidy up some zlib macro undefines34fffcaRename beast sources for consistency4e59ab2Add CallQueue unit test327d7a6Fixes for consolidated beast unity includesd5ece4eRemove unused and broken classes39f13beRemove unused ConcurrentObject37624a7Add ThreadWithCallQueue unit teste82ec68Remove obsolete beast_Function90551a6Temporarily leave sqlite3 in whatever threading mode it was already in.43ebbb1Fix SharedSingleton to use memoryBarrierf343941Tidy up SharedSingleton doc comments001997eFix leak on exit from Singleton dependency cycle83b9d22Rename to DeadlineTimer::cancel()77874eeUse new instead of ::new for placement2a04dccJournal improvements50965caSharedFunction improvements277e32bAdd LockFreeStack iteratorsd94e4c2Fix undefined behavior in UnsignedIntegerCalc (again)2dc25ceFix DeadlineTimer, callback while holding lock207ffdeFix undefined behavior in UnsignedIntegerCalc1ad8ff9Fix UnsignedInteger::isZero1dd2836Add support for multiprecision integer arithmetic and binary data encodinga45fc47Update .gitignore962a95dTidy up UnsignedIntegerca695faAdd Time::isNull()e96ce99Better random number facilities in UnitTest550b8e5Fine tune UnsignedInteger declaration8e7e3b7Allow negative relative expirations in DeadlineTimerf3dc7ceAdd generic Journal class for loggingbfdda32Make ChildProcess UnitTest manual since it malfunctions02acf7dGeneral refactoring of beast framework classes84ef06eFix ExitHook to derive from AtExitHookf0acc9cReduce the max threads in the Workers unit test55447b0New SharedSingleton, resolves destruction of objects with static storage duration.41eb8a1Remove deprecated SharedPtr::getObject9eda4bcMake SharedObject members const, the counter mutable6eda777Remove deprecated createOnDemandOnce SingletonLifetime option8c522aaFix off by one in pending i/o count on HTTPClient057344eAdd HTTPMessage::toString and familyee728e3Add UniformResourceLocator::emptyae324fbMove ./modules to ./src git-subtree-dir: src/beast git-subtree-split:0e7bac945f
1163 lines
38 KiB
C++
1163 lines
38 KiB
C++
//------------------------------------------------------------------------------
|
|
/*
|
|
This file is part of Beast: https://github.com/vinniefalco/Beast
|
|
Copyright 2013, Vinnie Falco <vinnie.falco@gmail.com>
|
|
|
|
Portions of this file are from JUCE.
|
|
Copyright (c) 2013 - Raw Material Software Ltd.
|
|
Please visit http://www.juce.com
|
|
|
|
Permission to use, copy, modify, and/or distribute this software for any
|
|
purpose with or without fee is hereby granted, provided that the above
|
|
copyright notice and this permission notice appear in all copies.
|
|
|
|
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
|
|
WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
|
|
MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
|
|
ANY SPECIAL , DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
|
|
WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
|
|
ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
|
|
OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
|
*/
|
|
//==============================================================================
|
|
|
|
class Expression::Term
|
|
: public SingleThreadedSharedObject
|
|
{
|
|
public:
|
|
Term() {}
|
|
virtual ~Term() {}
|
|
|
|
virtual Type getType() const noexcept = 0;
|
|
virtual Term* clone() const = 0;
|
|
virtual SharedPtr<Term> resolve (const Scope&, int recursionDepth) = 0;
|
|
virtual String toString() const = 0;
|
|
virtual double toDouble() const { return 0; }
|
|
virtual int getInputIndexFor (const Term*) const { return -1; }
|
|
virtual int getOperatorPrecedence() const { return 0; }
|
|
virtual int getNumInputs() const { return 0; }
|
|
virtual Term* getInput (int) const { return nullptr; }
|
|
virtual SharedPtr<Term> negated();
|
|
|
|
virtual SharedPtr<Term> createTermToEvaluateInput (const Scope&, const Term* /*inputTerm*/,
|
|
double /*overallTarget*/, Term* /*topLevelTerm*/) const
|
|
{
|
|
bassertfalse;
|
|
return SharedPtr<Term>();
|
|
}
|
|
|
|
virtual String getName() const
|
|
{
|
|
bassertfalse; // You shouldn't call this for an expression that's not actually a function!
|
|
return String::empty;
|
|
}
|
|
|
|
virtual void renameSymbol (const Symbol& oldSymbol, const String& newName, const Scope& scope, int recursionDepth)
|
|
{
|
|
for (int i = getNumInputs(); --i >= 0;)
|
|
getInput (i)->renameSymbol (oldSymbol, newName, scope, recursionDepth);
|
|
}
|
|
|
|
class SymbolVisitor
|
|
{
|
|
public:
|
|
virtual ~SymbolVisitor() {}
|
|
virtual void useSymbol (const Symbol&) = 0;
|
|
};
|
|
|
|
virtual void visitAllSymbols (SymbolVisitor& visitor, const Scope& scope, int recursionDepth)
|
|
{
|
|
for (int i = getNumInputs(); --i >= 0;)
|
|
getInput(i)->visitAllSymbols (visitor, scope, recursionDepth);
|
|
}
|
|
};
|
|
|
|
|
|
//==============================================================================
|
|
struct Expression::Helpers
|
|
{
|
|
typedef SharedPtr<Term> TermPtr;
|
|
|
|
static void checkRecursionDepth (const int depth)
|
|
{
|
|
if (depth > 256)
|
|
throw EvaluationError ("Recursive symbol references");
|
|
}
|
|
|
|
friend class Expression::Term;
|
|
|
|
//==============================================================================
|
|
/** An exception that can be thrown by Expression::evaluate(). */
|
|
class EvaluationError : public std::exception
|
|
{
|
|
public:
|
|
EvaluationError (const String& desc) : description (desc)
|
|
{
|
|
DBG ("Expression::EvaluationError: " + description);
|
|
}
|
|
|
|
String description;
|
|
};
|
|
|
|
//==============================================================================
|
|
class Constant : public Term
|
|
{
|
|
public:
|
|
Constant (const double val, const bool resolutionTarget)
|
|
: value (val), isResolutionTarget (resolutionTarget) {}
|
|
|
|
Type getType() const noexcept { return constantType; }
|
|
Term* clone() const { return new Constant (value, isResolutionTarget); }
|
|
TermPtr resolve (const Scope&, int) { return this; }
|
|
double toDouble() const { return value; }
|
|
TermPtr negated() { return new Constant (-value, isResolutionTarget); }
|
|
|
|
String toString() const
|
|
{
|
|
String s (value);
|
|
if (isResolutionTarget)
|
|
s = "@" + s;
|
|
|
|
return s;
|
|
}
|
|
|
|
double value;
|
|
bool isResolutionTarget;
|
|
};
|
|
|
|
//==============================================================================
|
|
class BinaryTerm : public Term
|
|
{
|
|
public:
|
|
BinaryTerm (Term* const l, Term* const r) : left (l), right (r)
|
|
{
|
|
bassert (l != nullptr && r != nullptr);
|
|
}
|
|
|
|
int getInputIndexFor (const Term* possibleInput) const
|
|
{
|
|
return possibleInput == left ? 0 : (possibleInput == right ? 1 : -1);
|
|
}
|
|
|
|
Type getType() const noexcept { return operatorType; }
|
|
int getNumInputs() const { return 2; }
|
|
Term* getInput (int index) const { return index == 0 ? left.get() : (index == 1 ? right.get() : 0); }
|
|
|
|
virtual double performFunction (double left, double right) const = 0;
|
|
virtual void writeOperator (String& dest) const = 0;
|
|
|
|
TermPtr resolve (const Scope& scope, int recursionDepth)
|
|
{
|
|
return new Constant (performFunction (left ->resolve (scope, recursionDepth)->toDouble(),
|
|
right->resolve (scope, recursionDepth)->toDouble()), false);
|
|
}
|
|
|
|
String toString() const
|
|
{
|
|
String s;
|
|
|
|
const int ourPrecendence = getOperatorPrecedence();
|
|
if (left->getOperatorPrecedence() > ourPrecendence)
|
|
s << '(' << left->toString() << ')';
|
|
else
|
|
s = left->toString();
|
|
|
|
writeOperator (s);
|
|
|
|
if (right->getOperatorPrecedence() >= ourPrecendence)
|
|
s << '(' << right->toString() << ')';
|
|
else
|
|
s << right->toString();
|
|
|
|
return s;
|
|
}
|
|
|
|
protected:
|
|
const TermPtr left, right;
|
|
|
|
TermPtr createDestinationTerm (const Scope& scope, const Term* input, double overallTarget, Term* topLevelTerm) const
|
|
{
|
|
bassert (input == left || input == right);
|
|
if (input != left && input != right)
|
|
return TermPtr();
|
|
|
|
const Term* const dest = findDestinationFor (topLevelTerm, this);
|
|
|
|
if (dest == nullptr)
|
|
return new Constant (overallTarget, false);
|
|
|
|
return dest->createTermToEvaluateInput (scope, this, overallTarget, topLevelTerm);
|
|
}
|
|
};
|
|
|
|
//==============================================================================
|
|
class SymbolTerm : public Term
|
|
{
|
|
public:
|
|
explicit SymbolTerm (const String& sym) : symbol (sym) {}
|
|
|
|
TermPtr resolve (const Scope& scope, int recursionDepth)
|
|
{
|
|
checkRecursionDepth (recursionDepth);
|
|
return scope.getSymbolValue (symbol).term->resolve (scope, recursionDepth + 1);
|
|
}
|
|
|
|
Type getType() const noexcept { return symbolType; }
|
|
Term* clone() const { return new SymbolTerm (symbol); }
|
|
String toString() const { return symbol; }
|
|
String getName() const { return symbol; }
|
|
|
|
void visitAllSymbols (SymbolVisitor& visitor, const Scope& scope, int recursionDepth)
|
|
{
|
|
checkRecursionDepth (recursionDepth);
|
|
visitor.useSymbol (Symbol (scope.getScopeUID(), symbol));
|
|
scope.getSymbolValue (symbol).term->visitAllSymbols (visitor, scope, recursionDepth + 1);
|
|
}
|
|
|
|
void renameSymbol (const Symbol& oldSymbol, const String& newName, const Scope& scope, int /*recursionDepth*/)
|
|
{
|
|
if (oldSymbol.symbolName == symbol && scope.getScopeUID() == oldSymbol.scopeUID)
|
|
symbol = newName;
|
|
}
|
|
|
|
String symbol;
|
|
};
|
|
|
|
//==============================================================================
|
|
class Function : public Term
|
|
{
|
|
public:
|
|
explicit Function (const String& name) : functionName (name) {}
|
|
|
|
Function (const String& name, const Array<Expression>& params)
|
|
: functionName (name), parameters (params)
|
|
{}
|
|
|
|
Type getType() const noexcept { return functionType; }
|
|
Term* clone() const { return new Function (functionName, parameters); }
|
|
int getNumInputs() const { return parameters.size(); }
|
|
Term* getInput (int i) const { return parameters.getReference(i).term; }
|
|
String getName() const { return functionName; }
|
|
|
|
TermPtr resolve (const Scope& scope, int recursionDepth)
|
|
{
|
|
checkRecursionDepth (recursionDepth);
|
|
double result = 0;
|
|
const int numParams = parameters.size();
|
|
if (numParams > 0)
|
|
{
|
|
HeapBlock<double> params ((size_t) numParams);
|
|
for (int i = 0; i < numParams; ++i)
|
|
params[i] = parameters.getReference(i).term->resolve (scope, recursionDepth + 1)->toDouble();
|
|
|
|
result = scope.evaluateFunction (functionName, params, numParams);
|
|
}
|
|
else
|
|
{
|
|
result = scope.evaluateFunction (functionName, nullptr, 0);
|
|
}
|
|
|
|
return new Constant (result, false);
|
|
}
|
|
|
|
int getInputIndexFor (const Term* possibleInput) const
|
|
{
|
|
for (int i = 0; i < parameters.size(); ++i)
|
|
if (parameters.getReference(i).term == possibleInput)
|
|
return i;
|
|
|
|
return -1;
|
|
}
|
|
|
|
String toString() const
|
|
{
|
|
if (parameters.size() == 0)
|
|
return functionName + "()";
|
|
|
|
String s (functionName + " (");
|
|
|
|
for (int i = 0; i < parameters.size(); ++i)
|
|
{
|
|
s << parameters.getReference(i).term->toString();
|
|
|
|
if (i < parameters.size() - 1)
|
|
s << ", ";
|
|
}
|
|
|
|
s << ')';
|
|
return s;
|
|
}
|
|
|
|
const String functionName;
|
|
Array<Expression> parameters;
|
|
};
|
|
|
|
//==============================================================================
|
|
class DotOperator
|
|
: public BinaryTerm
|
|
{
|
|
public:
|
|
DotOperator (SymbolTerm* const l, Term* const r) : BinaryTerm (l, r) {}
|
|
|
|
TermPtr resolve (const Scope& scope, int recursionDepth)
|
|
{
|
|
checkRecursionDepth (recursionDepth);
|
|
|
|
EvaluationVisitor visitor (right, recursionDepth + 1);
|
|
scope.visitRelativeScope (getSymbol()->symbol, visitor);
|
|
return visitor.output;
|
|
}
|
|
|
|
Term* clone() const { return new DotOperator (getSymbol(), right); }
|
|
String getName() const { return "."; }
|
|
int getOperatorPrecedence() const { return 1; }
|
|
void writeOperator (String& dest) const { dest << '.'; }
|
|
double performFunction (double, double) const { return 0.0; }
|
|
|
|
void visitAllSymbols (SymbolVisitor& visitor, const Scope& scope, int recursionDepth)
|
|
{
|
|
checkRecursionDepth (recursionDepth);
|
|
visitor.useSymbol (Symbol (scope.getScopeUID(), getSymbol()->symbol));
|
|
|
|
SymbolVisitingVisitor v (right, visitor, recursionDepth + 1);
|
|
|
|
try
|
|
{
|
|
scope.visitRelativeScope (getSymbol()->symbol, v);
|
|
}
|
|
catch (...) {}
|
|
}
|
|
|
|
void renameSymbol (const Symbol& oldSymbol, const String& newName, const Scope& scope, int recursionDepth)
|
|
{
|
|
checkRecursionDepth (recursionDepth);
|
|
getSymbol()->renameSymbol (oldSymbol, newName, scope, recursionDepth);
|
|
|
|
SymbolRenamingVisitor visitor (right, oldSymbol, newName, recursionDepth + 1);
|
|
|
|
try
|
|
{
|
|
scope.visitRelativeScope (getSymbol()->symbol, visitor);
|
|
}
|
|
catch (...) {}
|
|
}
|
|
|
|
private:
|
|
//==============================================================================
|
|
class EvaluationVisitor
|
|
: public Scope::Visitor
|
|
, public Uncopyable
|
|
{
|
|
public:
|
|
EvaluationVisitor (const TermPtr& t, const int recursion)
|
|
: input (t), output (t), recursionCount (recursion) {}
|
|
|
|
void visit (const Scope& scope) { output = input->resolve (scope, recursionCount); }
|
|
|
|
const TermPtr input;
|
|
TermPtr output;
|
|
const int recursionCount;
|
|
};
|
|
|
|
class SymbolVisitingVisitor
|
|
: public Scope::Visitor
|
|
, public Uncopyable
|
|
{
|
|
public:
|
|
SymbolVisitingVisitor (const TermPtr& t, SymbolVisitor& v, const int recursion)
|
|
: input (t), visitor (v), recursionCount (recursion) {}
|
|
|
|
void visit (const Scope& scope) { input->visitAllSymbols (visitor, scope, recursionCount); }
|
|
|
|
private:
|
|
const TermPtr input;
|
|
SymbolVisitor& visitor;
|
|
const int recursionCount;
|
|
};
|
|
|
|
class SymbolRenamingVisitor
|
|
: public Scope::Visitor
|
|
, public Uncopyable
|
|
{
|
|
public:
|
|
SymbolRenamingVisitor (const TermPtr& t, const Expression::Symbol& symbol_, const String& newName_, const int recursionCount_)
|
|
: input (t), symbol (symbol_), newName (newName_), recursionCount (recursionCount_) {}
|
|
|
|
void visit (const Scope& scope) { input->renameSymbol (symbol, newName, scope, recursionCount); }
|
|
|
|
private:
|
|
const TermPtr input;
|
|
const Symbol& symbol;
|
|
const String newName;
|
|
const int recursionCount;
|
|
};
|
|
|
|
SymbolTerm* getSymbol() const { return static_cast <SymbolTerm*> (left.get()); }
|
|
};
|
|
|
|
//==============================================================================
|
|
class Negate : public Term
|
|
{
|
|
public:
|
|
explicit Negate (const TermPtr& t) : input (t)
|
|
{
|
|
bassert (t != nullptr);
|
|
}
|
|
|
|
Type getType() const noexcept { return operatorType; }
|
|
int getInputIndexFor (const Term* possibleInput) const { return possibleInput == input ? 0 : -1; }
|
|
int getNumInputs() const { return 1; }
|
|
Term* getInput (int index) const { return index == 0 ? input.get() : nullptr; }
|
|
Term* clone() const { return new Negate (input->clone()); }
|
|
|
|
TermPtr resolve (const Scope& scope, int recursionDepth)
|
|
{
|
|
return new Constant (-input->resolve (scope, recursionDepth)->toDouble(), false);
|
|
}
|
|
|
|
String getName() const { return "-"; }
|
|
TermPtr negated() { return input; }
|
|
|
|
TermPtr createTermToEvaluateInput (const Scope& scope, const Term* t, double overallTarget, Term* topLevelTerm) const
|
|
{
|
|
(void) t;
|
|
bassert (t == input);
|
|
|
|
const Term* const dest = findDestinationFor (topLevelTerm, this);
|
|
|
|
return new Negate (dest == nullptr ? new Constant (overallTarget, false)
|
|
: dest->createTermToEvaluateInput (scope, this, overallTarget, topLevelTerm));
|
|
}
|
|
|
|
String toString() const
|
|
{
|
|
if (input->getOperatorPrecedence() > 0)
|
|
return "-(" + input->toString() + ")";
|
|
|
|
return "-" + input->toString();
|
|
}
|
|
|
|
private:
|
|
const TermPtr input;
|
|
};
|
|
|
|
//==============================================================================
|
|
class Add : public BinaryTerm
|
|
{
|
|
public:
|
|
Add (Term* const l, Term* const r) : BinaryTerm (l, r) {}
|
|
|
|
Term* clone() const { return new Add (left->clone(), right->clone()); }
|
|
double performFunction (double lhs, double rhs) const { return lhs + rhs; }
|
|
int getOperatorPrecedence() const { return 3; }
|
|
String getName() const { return "+"; }
|
|
void writeOperator (String& dest) const { dest << " + "; }
|
|
|
|
TermPtr createTermToEvaluateInput (const Scope& scope, const Term* input, double overallTarget, Term* topLevelTerm) const
|
|
{
|
|
const TermPtr newDest (createDestinationTerm (scope, input, overallTarget, topLevelTerm));
|
|
if (newDest == nullptr)
|
|
return TermPtr();
|
|
|
|
return new Subtract (newDest, (input == left ? right : left)->clone());
|
|
}
|
|
};
|
|
|
|
//==============================================================================
|
|
class Subtract : public BinaryTerm
|
|
{
|
|
public:
|
|
Subtract (Term* const l, Term* const r) : BinaryTerm (l, r) {}
|
|
|
|
Term* clone() const { return new Subtract (left->clone(), right->clone()); }
|
|
double performFunction (double lhs, double rhs) const { return lhs - rhs; }
|
|
int getOperatorPrecedence() const { return 3; }
|
|
String getName() const { return "-"; }
|
|
void writeOperator (String& dest) const { dest << " - "; }
|
|
|
|
TermPtr createTermToEvaluateInput (const Scope& scope, const Term* input, double overallTarget, Term* topLevelTerm) const
|
|
{
|
|
const TermPtr newDest (createDestinationTerm (scope, input, overallTarget, topLevelTerm));
|
|
if (newDest == nullptr)
|
|
return TermPtr();
|
|
|
|
if (input == left)
|
|
return new Add (newDest, right->clone());
|
|
|
|
return new Subtract (left->clone(), newDest);
|
|
}
|
|
};
|
|
|
|
//==============================================================================
|
|
class Multiply : public BinaryTerm
|
|
{
|
|
public:
|
|
Multiply (Term* const l, Term* const r) : BinaryTerm (l, r) {}
|
|
|
|
Term* clone() const { return new Multiply (left->clone(), right->clone()); }
|
|
double performFunction (double lhs, double rhs) const { return lhs * rhs; }
|
|
String getName() const { return "*"; }
|
|
void writeOperator (String& dest) const { dest << " * "; }
|
|
int getOperatorPrecedence() const { return 2; }
|
|
|
|
TermPtr createTermToEvaluateInput (const Scope& scope, const Term* input, double overallTarget, Term* topLevelTerm) const
|
|
{
|
|
const TermPtr newDest (createDestinationTerm (scope, input, overallTarget, topLevelTerm));
|
|
if (newDest == nullptr)
|
|
return TermPtr();
|
|
|
|
return new Divide (newDest, (input == left ? right : left)->clone());
|
|
}
|
|
};
|
|
|
|
//==============================================================================
|
|
class Divide : public BinaryTerm
|
|
{
|
|
public:
|
|
Divide (Term* const l, Term* const r) : BinaryTerm (l, r) {}
|
|
|
|
Term* clone() const { return new Divide (left->clone(), right->clone()); }
|
|
double performFunction (double lhs, double rhs) const { return lhs / rhs; }
|
|
String getName() const { return "/"; }
|
|
void writeOperator (String& dest) const { dest << " / "; }
|
|
int getOperatorPrecedence() const { return 2; }
|
|
|
|
TermPtr createTermToEvaluateInput (const Scope& scope, const Term* input, double overallTarget, Term* topLevelTerm) const
|
|
{
|
|
const TermPtr newDest (createDestinationTerm (scope, input, overallTarget, topLevelTerm));
|
|
if (newDest == nullptr)
|
|
return TermPtr();
|
|
|
|
if (input == left)
|
|
return new Multiply (newDest, right->clone());
|
|
|
|
return new Divide (left->clone(), newDest);
|
|
}
|
|
};
|
|
|
|
//==============================================================================
|
|
static Term* findDestinationFor (Term* const topLevel, const Term* const inputTerm)
|
|
{
|
|
const int inputIndex = topLevel->getInputIndexFor (inputTerm);
|
|
if (inputIndex >= 0)
|
|
return topLevel;
|
|
|
|
for (int i = topLevel->getNumInputs(); --i >= 0;)
|
|
{
|
|
Term* const t = findDestinationFor (topLevel->getInput (i), inputTerm);
|
|
|
|
if (t != nullptr)
|
|
return t;
|
|
}
|
|
|
|
return nullptr;
|
|
}
|
|
|
|
static Constant* findTermToAdjust (Term* const term, const bool mustBeFlagged)
|
|
{
|
|
bassert (term != nullptr);
|
|
|
|
if (term->getType() == constantType)
|
|
{
|
|
Constant* const c = static_cast<Constant*> (term);
|
|
if (c->isResolutionTarget || ! mustBeFlagged)
|
|
return c;
|
|
}
|
|
|
|
if (term->getType() == functionType)
|
|
return nullptr;
|
|
|
|
const int numIns = term->getNumInputs();
|
|
|
|
for (int i = 0; i < numIns; ++i)
|
|
{
|
|
Term* const input = term->getInput (i);
|
|
|
|
if (input->getType() == constantType)
|
|
{
|
|
Constant* const c = static_cast<Constant*> (input);
|
|
|
|
if (c->isResolutionTarget || ! mustBeFlagged)
|
|
return c;
|
|
}
|
|
}
|
|
|
|
for (int i = 0; i < numIns; ++i)
|
|
{
|
|
Constant* const c = findTermToAdjust (term->getInput (i), mustBeFlagged);
|
|
if (c != nullptr)
|
|
return c;
|
|
}
|
|
|
|
return nullptr;
|
|
}
|
|
|
|
static bool containsAnySymbols (const Term* const t)
|
|
{
|
|
if (t->getType() == Expression::symbolType)
|
|
return true;
|
|
|
|
for (int i = t->getNumInputs(); --i >= 0;)
|
|
if (containsAnySymbols (t->getInput (i)))
|
|
return true;
|
|
|
|
return false;
|
|
}
|
|
|
|
//==============================================================================
|
|
class SymbolCheckVisitor
|
|
: public Term::SymbolVisitor
|
|
, public Uncopyable
|
|
{
|
|
public:
|
|
SymbolCheckVisitor (const Symbol& symbol_) : wasFound (false), symbol (symbol_) {}
|
|
void useSymbol (const Symbol& s) { wasFound = wasFound || s == symbol; }
|
|
|
|
bool wasFound;
|
|
|
|
private:
|
|
const Symbol& symbol;
|
|
};
|
|
|
|
//==============================================================================
|
|
class SymbolListVisitor
|
|
: public Term::SymbolVisitor
|
|
, public Uncopyable
|
|
{
|
|
public:
|
|
SymbolListVisitor (Array<Symbol>& list_) : list (list_) {}
|
|
void useSymbol (const Symbol& s) { list.addIfNotAlreadyThere (s); }
|
|
|
|
private:
|
|
Array<Symbol>& list;
|
|
};
|
|
|
|
//==============================================================================
|
|
class Parser : public Uncopyable
|
|
{
|
|
public:
|
|
//==============================================================================
|
|
Parser (String::CharPointerType& stringToParse)
|
|
: text (stringToParse)
|
|
{
|
|
}
|
|
|
|
TermPtr readUpToComma()
|
|
{
|
|
if (text.isEmpty())
|
|
return new Constant (0.0, false);
|
|
|
|
const TermPtr e (readExpression());
|
|
|
|
if (e == nullptr || ((! readOperator (",")) && ! text.isEmpty()))
|
|
throw ParseError ("Syntax error: \"" + String (text) + "\"");
|
|
|
|
return e;
|
|
}
|
|
|
|
private:
|
|
String::CharPointerType& text;
|
|
|
|
//==============================================================================
|
|
static inline bool isDecimalDigit (const beast_wchar c) noexcept
|
|
{
|
|
return c >= '0' && c <= '9';
|
|
}
|
|
|
|
bool readChar (const beast_wchar required) noexcept
|
|
{
|
|
if (*text == required)
|
|
{
|
|
++text;
|
|
return true;
|
|
}
|
|
|
|
return false;
|
|
}
|
|
|
|
bool readOperator (const char* ops, char* const opType = nullptr) noexcept
|
|
{
|
|
text = text.findEndOfWhitespace();
|
|
|
|
while (*ops != 0)
|
|
{
|
|
if (readChar ((beast_wchar) (uint8) *ops))
|
|
{
|
|
if (opType != nullptr)
|
|
*opType = *ops;
|
|
|
|
return true;
|
|
}
|
|
|
|
++ops;
|
|
}
|
|
|
|
return false;
|
|
}
|
|
|
|
bool readIdentifier (String& identifier) noexcept
|
|
{
|
|
text = text.findEndOfWhitespace();
|
|
String::CharPointerType t (text);
|
|
int numChars = 0;
|
|
|
|
if (t.isLetter() || *t == '_')
|
|
{
|
|
++t;
|
|
++numChars;
|
|
|
|
while (t.isLetterOrDigit() || *t == '_')
|
|
{
|
|
++t;
|
|
++numChars;
|
|
}
|
|
}
|
|
|
|
if (numChars > 0)
|
|
{
|
|
identifier = String (text, (size_t) numChars);
|
|
text = t;
|
|
return true;
|
|
}
|
|
|
|
return false;
|
|
}
|
|
|
|
Term* readNumber() noexcept
|
|
{
|
|
text = text.findEndOfWhitespace();
|
|
String::CharPointerType t (text);
|
|
|
|
const bool isResolutionTarget = (*t == '@');
|
|
if (isResolutionTarget)
|
|
{
|
|
++t;
|
|
t = t.findEndOfWhitespace();
|
|
text = t;
|
|
}
|
|
|
|
if (*t == '-')
|
|
{
|
|
++t;
|
|
t = t.findEndOfWhitespace();
|
|
}
|
|
|
|
if (isDecimalDigit (*t) || (*t == '.' && isDecimalDigit (t[1])))
|
|
return new Constant (CharacterFunctions::readDoubleValue (text), isResolutionTarget);
|
|
|
|
return nullptr;
|
|
}
|
|
|
|
TermPtr readExpression()
|
|
{
|
|
TermPtr lhs (readMultiplyOrDivideExpression());
|
|
|
|
char opType;
|
|
while (lhs != nullptr && readOperator ("+-", &opType))
|
|
{
|
|
TermPtr rhs (readMultiplyOrDivideExpression());
|
|
|
|
if (rhs == nullptr)
|
|
throw ParseError ("Expected expression after \"" + String::charToString ((beast_wchar) (uint8) opType) + "\"");
|
|
|
|
if (opType == '+')
|
|
lhs = new Add (lhs, rhs);
|
|
else
|
|
lhs = new Subtract (lhs, rhs);
|
|
}
|
|
|
|
return lhs;
|
|
}
|
|
|
|
TermPtr readMultiplyOrDivideExpression()
|
|
{
|
|
TermPtr lhs (readUnaryExpression());
|
|
|
|
char opType;
|
|
while (lhs != nullptr && readOperator ("*/", &opType))
|
|
{
|
|
TermPtr rhs (readUnaryExpression());
|
|
|
|
if (rhs == nullptr)
|
|
throw ParseError ("Expected expression after \"" + String::charToString ((beast_wchar) (uint8) opType) + "\"");
|
|
|
|
if (opType == '*')
|
|
lhs = new Multiply (lhs, rhs);
|
|
else
|
|
lhs = new Divide (lhs, rhs);
|
|
}
|
|
|
|
return lhs;
|
|
}
|
|
|
|
TermPtr readUnaryExpression()
|
|
{
|
|
char opType;
|
|
if (readOperator ("+-", &opType))
|
|
{
|
|
TermPtr e (readUnaryExpression());
|
|
|
|
if (e == nullptr)
|
|
throw ParseError ("Expected expression after \"" + String::charToString ((beast_wchar) (uint8) opType) + "\"");
|
|
|
|
if (opType == '-')
|
|
e = e->negated();
|
|
|
|
return e;
|
|
}
|
|
|
|
return readPrimaryExpression();
|
|
}
|
|
|
|
TermPtr readPrimaryExpression()
|
|
{
|
|
TermPtr e (readParenthesisedExpression());
|
|
if (e != nullptr)
|
|
return e;
|
|
|
|
e = readNumber();
|
|
if (e != nullptr)
|
|
return e;
|
|
|
|
return readSymbolOrFunction();
|
|
}
|
|
|
|
TermPtr readSymbolOrFunction()
|
|
{
|
|
String identifier;
|
|
if (readIdentifier (identifier))
|
|
{
|
|
if (readOperator ("(")) // method call...
|
|
{
|
|
Function* const f = new Function (identifier);
|
|
ScopedPointer<Term> func (f); // (can't use ScopedPointer<Function> in MSVC)
|
|
|
|
TermPtr param (readExpression());
|
|
|
|
if (param == nullptr)
|
|
{
|
|
if (readOperator (")"))
|
|
return func.release();
|
|
|
|
throw ParseError ("Expected parameters after \"" + identifier + " (\"");
|
|
}
|
|
|
|
f->parameters.add (Expression (param));
|
|
|
|
while (readOperator (","))
|
|
{
|
|
param = readExpression();
|
|
|
|
if (param == nullptr)
|
|
throw ParseError ("Expected expression after \",\"");
|
|
|
|
f->parameters.add (Expression (param));
|
|
}
|
|
|
|
if (readOperator (")"))
|
|
return func.release();
|
|
|
|
throw ParseError ("Expected \")\"");
|
|
}
|
|
|
|
if (readOperator ("."))
|
|
{
|
|
TermPtr rhs (readSymbolOrFunction());
|
|
|
|
if (rhs == nullptr)
|
|
throw ParseError ("Expected symbol or function after \".\"");
|
|
|
|
if (identifier == "this")
|
|
return rhs;
|
|
|
|
return new DotOperator (new SymbolTerm (identifier), rhs);
|
|
}
|
|
|
|
// just a symbol..
|
|
bassert (identifier.trim() == identifier);
|
|
return new SymbolTerm (identifier);
|
|
}
|
|
|
|
return TermPtr();
|
|
}
|
|
|
|
TermPtr readParenthesisedExpression()
|
|
{
|
|
if (! readOperator ("("))
|
|
return TermPtr();
|
|
|
|
const TermPtr e (readExpression());
|
|
if (e == nullptr || ! readOperator (")"))
|
|
return TermPtr();
|
|
|
|
return e;
|
|
}
|
|
};
|
|
};
|
|
|
|
//==============================================================================
|
|
Expression::Expression()
|
|
: term (new Expression::Helpers::Constant (0, false))
|
|
{
|
|
}
|
|
|
|
Expression::~Expression()
|
|
{
|
|
}
|
|
|
|
Expression::Expression (Term* const term_)
|
|
: term (term_)
|
|
{
|
|
bassert (term != nullptr);
|
|
}
|
|
|
|
Expression::Expression (const double constant)
|
|
: term (new Expression::Helpers::Constant (constant, false))
|
|
{
|
|
}
|
|
|
|
Expression::Expression (const Expression& other)
|
|
: term (other.term)
|
|
{
|
|
}
|
|
|
|
Expression& Expression::operator= (const Expression& other)
|
|
{
|
|
term = other.term;
|
|
return *this;
|
|
}
|
|
|
|
#if BEAST_COMPILER_SUPPORTS_MOVE_SEMANTICS
|
|
Expression::Expression (Expression&& other) noexcept
|
|
: term (static_cast <SharedPtr<Term>&&> (other.term))
|
|
{
|
|
}
|
|
|
|
Expression& Expression::operator= (Expression&& other) noexcept
|
|
{
|
|
term = static_cast <SharedPtr<Term>&&> (other.term);
|
|
return *this;
|
|
}
|
|
#endif
|
|
|
|
Expression::Expression (const String& stringToParse)
|
|
{
|
|
String::CharPointerType text (stringToParse.getCharPointer());
|
|
Helpers::Parser parser (text);
|
|
term = parser.readUpToComma();
|
|
}
|
|
|
|
Expression Expression::parse (String::CharPointerType& stringToParse)
|
|
{
|
|
Helpers::Parser parser (stringToParse);
|
|
return Expression (parser.readUpToComma());
|
|
}
|
|
|
|
double Expression::evaluate() const
|
|
{
|
|
return evaluate (Expression::Scope());
|
|
}
|
|
|
|
double Expression::evaluate (const Expression::Scope& scope) const
|
|
{
|
|
try
|
|
{
|
|
return term->resolve (scope, 0)->toDouble();
|
|
}
|
|
catch (Helpers::EvaluationError&)
|
|
{}
|
|
|
|
return 0;
|
|
}
|
|
|
|
double Expression::evaluate (const Scope& scope, String& evaluationError) const
|
|
{
|
|
try
|
|
{
|
|
return term->resolve (scope, 0)->toDouble();
|
|
}
|
|
catch (Helpers::EvaluationError& e)
|
|
{
|
|
evaluationError = e.description;
|
|
}
|
|
|
|
return 0;
|
|
}
|
|
|
|
Expression Expression::operator+ (const Expression& other) const { return Expression (new Helpers::Add (term, other.term)); }
|
|
Expression Expression::operator- (const Expression& other) const { return Expression (new Helpers::Subtract (term, other.term)); }
|
|
Expression Expression::operator* (const Expression& other) const { return Expression (new Helpers::Multiply (term, other.term)); }
|
|
Expression Expression::operator/ (const Expression& other) const { return Expression (new Helpers::Divide (term, other.term)); }
|
|
Expression Expression::operator-() const { return Expression (term->negated()); }
|
|
Expression Expression::symbol (const String& symbol) { return Expression (new Helpers::SymbolTerm (symbol)); }
|
|
|
|
Expression Expression::function (const String& functionName, const Array<Expression>& parameters)
|
|
{
|
|
return Expression (new Helpers::Function (functionName, parameters));
|
|
}
|
|
|
|
Expression Expression::adjustedToGiveNewResult (const double targetValue, const Expression::Scope& scope) const
|
|
{
|
|
ScopedPointer<Term> newTerm (term->clone());
|
|
|
|
Helpers::Constant* termToAdjust = Helpers::findTermToAdjust (newTerm, true);
|
|
|
|
if (termToAdjust == nullptr)
|
|
termToAdjust = Helpers::findTermToAdjust (newTerm, false);
|
|
|
|
if (termToAdjust == nullptr)
|
|
{
|
|
newTerm = new Helpers::Add (newTerm.release(), new Helpers::Constant (0, false));
|
|
termToAdjust = Helpers::findTermToAdjust (newTerm, false);
|
|
}
|
|
|
|
bassert (termToAdjust != nullptr);
|
|
|
|
const Term* const parent = Helpers::findDestinationFor (newTerm, termToAdjust);
|
|
|
|
if (parent == nullptr)
|
|
{
|
|
termToAdjust->value = targetValue;
|
|
}
|
|
else
|
|
{
|
|
const Helpers::TermPtr reverseTerm (parent->createTermToEvaluateInput (scope, termToAdjust, targetValue, newTerm));
|
|
|
|
if (reverseTerm == nullptr)
|
|
return Expression (targetValue);
|
|
|
|
termToAdjust->value = reverseTerm->resolve (scope, 0)->toDouble();
|
|
}
|
|
|
|
return Expression (newTerm.release());
|
|
}
|
|
|
|
Expression Expression::withRenamedSymbol (const Expression::Symbol& oldSymbol, const String& newName, const Scope& scope) const
|
|
{
|
|
bassert (newName.toLowerCase().containsOnly ("abcdefghijklmnopqrstuvwxyz0123456789_"));
|
|
|
|
if (oldSymbol.symbolName == newName)
|
|
return *this;
|
|
|
|
Expression e (term->clone());
|
|
e.term->renameSymbol (oldSymbol, newName, scope, 0);
|
|
return e;
|
|
}
|
|
|
|
bool Expression::referencesSymbol (const Expression::Symbol& symbolToCheck, const Scope& scope) const
|
|
{
|
|
Helpers::SymbolCheckVisitor visitor (symbolToCheck);
|
|
|
|
try
|
|
{
|
|
term->visitAllSymbols (visitor, scope, 0);
|
|
}
|
|
catch (Helpers::EvaluationError&)
|
|
{}
|
|
|
|
return visitor.wasFound;
|
|
}
|
|
|
|
void Expression::findReferencedSymbols (Array<Symbol>& results, const Scope& scope) const
|
|
{
|
|
try
|
|
{
|
|
Helpers::SymbolListVisitor visitor (results);
|
|
term->visitAllSymbols (visitor, scope, 0);
|
|
}
|
|
catch (Helpers::EvaluationError&)
|
|
{}
|
|
}
|
|
|
|
String Expression::toString() const { return term->toString(); }
|
|
bool Expression::usesAnySymbols() const { return Helpers::containsAnySymbols (term); }
|
|
Expression::Type Expression::getType() const noexcept { return term->getType(); }
|
|
String Expression::getSymbolOrFunction() const { return term->getName(); }
|
|
int Expression::getNumInputs() const { return term->getNumInputs(); }
|
|
Expression Expression::getInput (int index) const { return Expression (term->getInput (index)); }
|
|
|
|
//==============================================================================
|
|
SharedPtr<Expression::Term> Expression::Term::negated()
|
|
{
|
|
return new Helpers::Negate (this);
|
|
}
|
|
|
|
//==============================================================================
|
|
Expression::ParseError::ParseError (const String& message)
|
|
: description (message)
|
|
{
|
|
DBG ("Expression::ParseError: " + message);
|
|
}
|
|
|
|
//==============================================================================
|
|
Expression::Symbol::Symbol (const String& scopeUID_, const String& symbolName_)
|
|
: scopeUID (scopeUID_), symbolName (symbolName_)
|
|
{
|
|
}
|
|
|
|
bool Expression::Symbol::operator== (const Symbol& other) const noexcept
|
|
{
|
|
return symbolName == other.symbolName && scopeUID == other.scopeUID;
|
|
}
|
|
|
|
bool Expression::Symbol::operator!= (const Symbol& other) const noexcept
|
|
{
|
|
return ! operator== (other);
|
|
}
|
|
|
|
//==============================================================================
|
|
Expression::Scope::Scope() {}
|
|
Expression::Scope::~Scope() {}
|
|
|
|
Expression Expression::Scope::getSymbolValue (const String& symbol) const
|
|
{
|
|
if (symbol.isNotEmpty())
|
|
throw Helpers::EvaluationError ("Unknown symbol: " + symbol);
|
|
|
|
return Expression();
|
|
}
|
|
|
|
double Expression::Scope::evaluateFunction (const String& functionName, const double* parameters, int numParams) const
|
|
{
|
|
if (numParams > 0)
|
|
{
|
|
if (functionName == "min")
|
|
{
|
|
double v = parameters[0];
|
|
for (int i = 1; i < numParams; ++i)
|
|
v = bmin (v, parameters[i]);
|
|
|
|
return v;
|
|
}
|
|
|
|
if (functionName == "max")
|
|
{
|
|
double v = parameters[0];
|
|
for (int i = 1; i < numParams; ++i)
|
|
v = bmax (v, parameters[i]);
|
|
|
|
return v;
|
|
}
|
|
|
|
if (numParams == 1)
|
|
{
|
|
if (functionName == "sin") return sin (parameters[0]);
|
|
if (functionName == "cos") return cos (parameters[0]);
|
|
if (functionName == "tan") return tan (parameters[0]);
|
|
if (functionName == "abs") return std::abs (parameters[0]);
|
|
}
|
|
}
|
|
|
|
throw Helpers::EvaluationError ("Unknown function: \"" + functionName + "\"");
|
|
}
|
|
|
|
void Expression::Scope::visitRelativeScope (const String& scopeName, Visitor&) const
|
|
{
|
|
throw Helpers::EvaluationError ("Unknown symbol: " + scopeName);
|
|
}
|
|
|
|
String Expression::Scope::getScopeUID() const
|
|
{
|
|
return String::empty;
|
|
}
|