diff --git a/src/mathpresso/mathpresso.cpp b/src/mathpresso/mathpresso.cpp index 51374dd..06b8c8d 100644 --- a/src/mathpresso/mathpresso.cpp +++ b/src/mathpresso/mathpresso.cpp @@ -22,9 +22,8 @@ namespace mathpresso { -// ============================================================================ -// [mathpresso::mpOpInfo] -// ============================================================================ +// mathPresso - OpInfo +// =================== // Operator information, precedence and association. The table is mostly based // on the C-language standard, but also adjusted to support MATHPRESSO specific @@ -103,9 +102,8 @@ const OpInfo mpOpInfo[kOpCount] = { #undef LTR #undef ROW -// ============================================================================ -// [mathpresso::mpAssertionFailure] -// ============================================================================ +// MathPresso - Assertions +// ======================= void mpAssertionFailure(const char* file, int line, const char* msg) { fprintf(stderr, @@ -115,17 +113,15 @@ void mpAssertionFailure(const char* file, int line, const char* msg) { ::abort(); } -// ============================================================================ -// [mathpresso::mpTraceError] -// ============================================================================ +// MathPresso - Tracing +// ==================== MATHPRESSO_NOAPI Error mpTraceError(Error error) { return error; } -// ============================================================================ -// [mathpresso::mpDummyFunc] -// ============================================================================ +// MathPresso - Dummy Function +// =========================== //! \internal //! @@ -136,9 +132,8 @@ static void mpDummyFunc(double* result, void*) { *result = mpGetNan(); } -// ============================================================================ -// [mathpresso::ContextInternalImpl] -// ============================================================================ +// MathPresso - Context Impl +// ========================= //! \internal //! @@ -245,9 +240,8 @@ static Error mpContextMutable(Context* self, ContextInternalImpl** out) { } } -// ============================================================================ -// [mathpresso::Context - Construction / Destruction] -// ============================================================================ +// MathPresso - Context API +// ======================== Context::Context() : _d(const_cast(&mpContextNull)) {} @@ -259,10 +253,6 @@ Context::~Context() { mpContextRelease(_d); } -// ============================================================================ -// [mathpresso::Context - Copy / Reset] -// ============================================================================ - Error Context::reset() { mpContextRelease( mpAtomicSetXchgT( @@ -278,10 +268,6 @@ Context& Context::operator=(const Context& other) { return *this; } -// ============================================================================ -// [mathpresso::Context - Interface] -// ============================================================================ - struct GlobalConstant { char name[8]; double value; @@ -414,17 +400,12 @@ Error Context::delSymbol(const char* name) { return kErrorOk; } -// ============================================================================ -// [mathpresso::Expression - Construction / Destruction] -// ============================================================================ +// MathPresso - Expression API +// =========================== Expression::Expression() : _func(mpDummyFunc) {} Expression::~Expression() { reset(); } -// ============================================================================ -// [mathpresso::Expression - Interface] -// ============================================================================ - Error Expression::compile(const Context& ctx, const char* body, unsigned int options, OutputLog* log) { // Init options first. options &= _kOptionsMask; @@ -491,16 +472,14 @@ void Expression::reset() { } } -// ============================================================================ -// [mathpresso::OutputLog - Construction / Destruction] -// ============================================================================ +// MathPresso - OutputLog - API +// ============================ OutputLog::OutputLog() {} OutputLog::~OutputLog() {} -// ============================================================================ -// [mathpresso::ErrorReporter - Interface] -// ============================================================================ +// MathPresso - Error Reporter API +// =============================== void ErrorReporter::getLineAndColumn(uint32_t position, uint32_t& line, uint32_t& column) { // Should't happen, but be defensive. @@ -583,4 +562,4 @@ Error ErrorReporter::onError(Error error, uint32_t position, const String& msg) return MATHPRESSO_TRACE_ERROR(error); } -} // mathpresso namespace +} // {mathpresso} diff --git a/src/mathpresso/mathpresso.h b/src/mathpresso/mathpresso.h index 15a8884..5c34a39 100644 --- a/src/mathpresso/mathpresso.h +++ b/src/mathpresso/mathpresso.h @@ -13,9 +13,8 @@ namespace mathpresso { -// ============================================================================ -// [mathpresso::Configuration] -// ============================================================================ +// MathPresso Configuration +// ======================== // DEPRECATED: Will be removed in the future. #if defined(MATHPRESSO_BUILD_EMBED) || defined(MATHPRESSO_BUILD_STATIC) @@ -31,13 +30,12 @@ namespace mathpresso { #endif #endif -// ============================================================================ -// [mathpresso::PPDefs] -// ============================================================================ +// MathPresso Definitions +// ====================== //! \def MATHPRESSO_API //! -//! Mathpresso API decorator. +//! MathPresso API decorator. #if !defined(MATHPRESSO_STATIC) #if defined(_WIN32) && (defined(_MSC_VER) || defined(__MINGW32__)) #if defined(MATHPRESSO_BUILD_EXPORT) @@ -62,12 +60,12 @@ namespace mathpresso { //! \def MATHPRESSO_NOAPI //! -//! Mathpresso hidden API decorator. +//! MathPresso hidden API decorator. #define MATHPRESSO_NOAPI //! \def MATHPRESSO_INLINE //! -//! Mathpresso inline decorator. +//! MathPresso inline decorator. #if defined(__clang__) #define MATHPRESSO_INLINE inline __attribute__((__always_inline__, __visibility__("hidden"))) #elif defined(__GNUC__) @@ -91,16 +89,14 @@ private: \ #define MATHPRESSO_ARRAY_SIZE(array) \ (sizeof(array) / sizeof(array[0])) -// ============================================================================ -// [Forward Declarations] -// ============================================================================ +// Forward Declarations +// ==================== struct OutputLog; struct Expression; -// ============================================================================ -// [mathpresso::TypeDefs] -// ============================================================================ +// MathPresso Typedefs +// =================== //! MathPresso result type (signed integer). typedef unsigned int Error; @@ -118,9 +114,8 @@ typedef double (*Arg6Func)(double, double, double, double, double, double); typedef double (*Arg7Func)(double, double, double, double, double, double, double); typedef double (*Arg8Func)(double, double, double, double, double, double, double, double); -// ============================================================================ -// [mathpresso::ErrorCode] -// ============================================================================ +// MathPresso Error Codes +// ====================== //! MathPresso error codes. enum ErrorCode { @@ -144,9 +139,9 @@ enum ErrorCode { kErrorSymbolAlreadyExists }; -// ============================================================================ -// [mathpresso::Options] -// ============================================================================ + +// MathPresso Options +// ================== //! MathPresso options. enum Options { @@ -177,9 +172,8 @@ enum Options { _kOptionsMask = 0xFFFFu }; -// ============================================================================ -// [mathpresso::VariableFlags] -// ============================================================================ +// MathPresso Variable Flags +// ========================= //! Variable flags. enum VariableFlags { @@ -187,9 +181,8 @@ enum VariableFlags { kVariableRO = 0x00000001u }; -// ============================================================================ -// [mathpresso::FunctionFlags] -// ============================================================================ +// MathPresso Function Flags +// ========================= enum FunctionFlags { //! Function has 0 arguments. @@ -224,19 +217,13 @@ enum FunctionFlags { kFunctionNoSideEffects = 0x80000000u }; -// ============================================================================ -// [mathpresso::ContextImpl] -// ============================================================================ +// MathPresso Context +// ================== struct ContextImpl { //! Reference count (atomic). uintptr_t _refCount; }; - -// ============================================================================ -// [mathpresso::Context] -// ============================================================================ - //! MathPresso context. //! //! Context is an environment where you can add/remove constants, variables and @@ -245,9 +232,14 @@ struct ContextImpl { //! (reference counting is atomic). It is possible to create one master context //! and use it from different threads to compile many expressions. struct Context { - // -------------------------------------------------------------------------- - // [Construction / Destruction] - // -------------------------------------------------------------------------- + // Members + // ------- + + //! Private data not available to the MathPresso public API. + ContextImpl* _d; + + // Construction & Destruction + // -------------------------- //! Create a new `Context` instance. MATHPRESSO_API Context(); @@ -256,18 +248,16 @@ struct Context { //! Destroy the `Context` instance. MATHPRESSO_API ~Context(); - // -------------------------------------------------------------------------- - // [Copy / Reset] - // -------------------------------------------------------------------------- + // Copy & Reset + // ------------ //! Delete all symbols. MATHPRESSO_API Error reset(); //! Assignement operator. MATHPRESSO_API Context& operator=(const Context& other); - // -------------------------------------------------------------------------- - // [Interface] - // -------------------------------------------------------------------------- + // Interface + // --------- //! Add built-in intrinsics and constants. MATHPRESSO_API Error addBuiltIns(void); @@ -281,35 +271,31 @@ struct Context { //! Delete symbol from this context. MATHPRESSO_API Error delSymbol(const char* name); - - // -------------------------------------------------------------------------- - // [Members] - // -------------------------------------------------------------------------- - - //! Private data not available to the MathPresso public API. - ContextImpl* _d; }; -// ============================================================================ -// [mathpresso::Expression] -// ============================================================================ +// MathPresso Expresion +// ==================== //! MathPresso expression. struct Expression { MATHPRESSO_NONCOPYABLE(Expression) - // -------------------------------------------------------------------------- - // [Construction / Destruction] - // -------------------------------------------------------------------------- + // Members + // ------- + + //! Compiled function. + CompiledFunc _func; + + // Construction & Destruction + // -------------------------- //! Create a new `Expression` instance. MATHPRESSO_API Expression(); //! Destroy the `Expression` instance. MATHPRESSO_API ~Expression(); - // -------------------------------------------------------------------------- - // [Interface] - // -------------------------------------------------------------------------- + // Interface + // --------- //! Parse and compile a given expression. //! @@ -336,18 +322,10 @@ struct Expression { _func(&result, data); return result; } - - // -------------------------------------------------------------------------- - // [Members] - // -------------------------------------------------------------------------- - - //! Compiled function. - CompiledFunc _func; }; -// ============================================================================ -// [mpsl::OutputLog] -// ============================================================================ +// MathPresso OutputLog +// ==================== //! Interface that can be used to catch compiler warnings and errors. struct MATHPRESSO_API OutputLog { @@ -367,20 +345,18 @@ struct MATHPRESSO_API OutputLog { kMessageAsm }; - // -------------------------------------------------------------------------- - // [Construction / Destruction] - // -------------------------------------------------------------------------- + // Construction & Destruction + // -------------------------- OutputLog(); virtual ~OutputLog(); - // -------------------------------------------------------------------------- - // [Interface] - // -------------------------------------------------------------------------- + // Interface + // --------- virtual void log(unsigned int type, unsigned int line, unsigned int column, const char* message, size_t size) = 0; }; -} // mathpresso namespace +} // {mathpresso} #endif // _MATHPRESSO_H diff --git a/src/mathpresso/mathpresso_p.h b/src/mathpresso/mathpresso_p.h index edaa9c2..4a2c9ba 100644 --- a/src/mathpresso/mathpresso_p.h +++ b/src/mathpresso/mathpresso_p.h @@ -29,9 +29,8 @@ # endif // _MSC_VER >= 1400 #endif // _MSC_VER -// ============================================================================ -// [mathpresso::Architecture] -// ============================================================================ +// MathPresso Architecture +// ======================= #if (defined(_M_X64 ) || defined(__x86_64) || defined(__x86_64__) || \ defined(_M_AMD64) || defined(__amd64 ) || defined(__amd64__ )) @@ -68,9 +67,8 @@ # define MATHPRESSO_ARCH_LE (1) #endif -// ============================================================================ -// [MathPresso::Likely / Unlikely] -// ============================================================================ +// MathPresso Likely & Unlikely +// ============================ #if defined(__GNUC__) || defined(__clang__) # define MATHPRESSO_LIKELY(exp) __builtin_expect(!!(exp), 1) @@ -80,9 +78,8 @@ # define MATHPRESSO_UNLIKELY(exp) exp #endif -// ============================================================================ -// [MathPresso::Error Handling] -// ============================================================================ +// MathPresso Error Handling +// ========================== //! \internal #define MATHPRESSO_ASSERT(exp) do { \ @@ -132,16 +129,8 @@ #define MATHPRESSO_TRACE_ERROR(error) \ ::mathpresso::mpTraceError(error) -// ============================================================================ -// [mathpresso::] -// ============================================================================ - namespace mathpresso { -// ============================================================================ -// [Reuse] -// ============================================================================ - // Reuse these classes - we depend on asmjit anyway and these are internal. using asmjit::String; using asmjit::StringTmp; @@ -150,9 +139,8 @@ using asmjit::Zone; using asmjit::ZoneVector; using asmjit::ZoneAllocator; -// ============================================================================ -// [mathpresso::OpType] -// ============================================================================ +// MathPresso OpType +// ================= //! \internal //! @@ -221,9 +209,8 @@ enum OpType { kOpCount }; -// ============================================================================ -// [mathpresso::OpFlags] -// ============================================================================ +// MathPresso OpFlags +// ================== //! Operator flags. enum OpFlags { @@ -257,17 +244,15 @@ enum OpFlags { kOpFlagNopIfOne = kOpFlagNopIfLOne | kOpFlagNopIfROne }; -// ============================================================================ -// [mpsl::InternalConsts] -// ============================================================================ +// MathPresso Internal Consts +// ========================== enum InternalConsts { kInvalidSlot = 0xFFFFFFFFu }; -// ============================================================================ -// [mpsl::InternalOptions] -// ============================================================================ +// MathPresso Internal Options +// =========================== //! \internal //! @@ -277,36 +262,41 @@ enum InternalOptions { kInternalOptionLog = 0x00010000 }; -// ============================================================================ -// [mathpresso::mpAssertionFailure] -// ============================================================================ +// MathPresso - Assertions +// ======================= //! \internal //! //! MathPresso assertion handler. MATHPRESSO_NOAPI void mpAssertionFailure(const char* file, int line, const char* msg); -// ============================================================================ -// [mathpresso::mpTraceError] -// ============================================================================ +// MathPresso - Tracing +// ==================== MATHPRESSO_NOAPI Error mpTraceError(Error error); -// ============================================================================ -// [mpsl::OpInfo] -// ============================================================================ +// MathPresso - OpInfo +// =================== //! Operator information. struct OpInfo { - // -------------------------------------------------------------------------- - // [Statics] - // -------------------------------------------------------------------------- + // Members + // ------- + + uint8_t type; + uint8_t altType; + uint8_t precedence; + uint8_t reserved; + uint32_t flags; + char name[12]; + + // Statics + // ------- static MATHPRESSO_INLINE const OpInfo& get(uint32_t opType); - // -------------------------------------------------------------------------- - // [Accessors] - // -------------------------------------------------------------------------- + // Accessors + // --------- MATHPRESSO_INLINE bool isUnary() const { return (flags & kOpFlagUnary) != 0; } MATHPRESSO_INLINE bool isBinary() const { return (flags & kOpFlagBinary) != 0; } @@ -327,17 +317,6 @@ struct OpInfo { MATHPRESSO_INLINE bool rightAssociate(uint32_t rPrec) const { return precedence > rPrec || (precedence == rPrec && isRightToLeft()); } - - // -------------------------------------------------------------------------- - // [Members] - // -------------------------------------------------------------------------- - - uint8_t type; - uint8_t altType; - uint8_t precedence; - uint8_t reserved; - uint32_t flags; - char name[12]; }; extern const OpInfo mpOpInfo[kOpCount]; @@ -346,9 +325,8 @@ MATHPRESSO_INLINE const OpInfo& OpInfo::get(uint32_t op) { return mpOpInfo[op]; } -// ============================================================================ -// [mathpresso::StringRef] -// ============================================================================ +// MathPresso - StringRef +// ====================== //! String reference (pointer to string data data and size). //! @@ -356,9 +334,14 @@ MATHPRESSO_INLINE const OpInfo& OpInfo::get(uint32_t op) { //! the other hand MATHPRESSO doesn't require NULL terminated strings when passed //! to MATHPRESSO APIs. struct StringRef { - // -------------------------------------------------------------------------- - // [Construction / Destruction] - // -------------------------------------------------------------------------- + // Members + // ------- + + const char* _data; + size_t _size; + + // Construction & Destruction + // -------------------------- MATHPRESSO_INLINE StringRef() : _data(NULL), @@ -372,9 +355,8 @@ struct StringRef { : _data(data), _size(size) {} - // -------------------------------------------------------------------------- - // [Reset / Setup] - // -------------------------------------------------------------------------- + // Reset & Setup + // ------------- MATHPRESSO_INLINE void reset() { set(NULL, 0); @@ -389,18 +371,16 @@ struct StringRef { _size = size; } - // -------------------------------------------------------------------------- - // [Accessors] - // -------------------------------------------------------------------------- + // Accessors + // --------- //! Get the string data. MATHPRESSO_INLINE const char* data() const { return _data; } //! Get the string size. MATHPRESSO_INLINE size_t size() const { return _size; } - // -------------------------------------------------------------------------- - // [Eq] - // -------------------------------------------------------------------------- + // Equality + // -------- MATHPRESSO_INLINE bool eq(const char* s) const { const char* a = _data; @@ -417,21 +397,25 @@ struct StringRef { MATHPRESSO_INLINE bool eq(const char* s, size_t size) const { return size == _size && ::memcmp(_data, s, size) == 0; } - - // -------------------------------------------------------------------------- - // [Members] - // -------------------------------------------------------------------------- - - const char* _data; - size_t _size; }; -// ============================================================================ -// [mpsl::ErrorReporter] -// ============================================================================ +// MathPresso - Error Reporter +// =========================== //! Error reporter. struct ErrorReporter { + // Members + // ------- + + const char* _body; + size_t _size; + + uint32_t _options; + OutputLog* _log; + + // Construction & Destruction + // -------------------------- + MATHPRESSO_INLINE ErrorReporter(const char* body, size_t size, uint32_t options, OutputLog* log) : _body(body), _size(size), @@ -443,9 +427,8 @@ struct ErrorReporter { (log != NULL && (_options & kInternalOptionLog) != 0) ); } - // -------------------------------------------------------------------------- - // [Error Handling] - // -------------------------------------------------------------------------- + // Interface + // --------- MATHPRESSO_INLINE bool reportsErrors() const { return (_options & kInternalOptionLog) != 0; } MATHPRESSO_INLINE bool reportsWarnings() const { return (_options & kOptionVerbose) != 0; } @@ -457,18 +440,8 @@ struct ErrorReporter { Error onError(Error error, uint32_t position, const char* fmt, ...); Error onError(Error error, uint32_t position, const String& msg); - - // -------------------------------------------------------------------------- - // [Members] - // -------------------------------------------------------------------------- - - const char* _body; - size_t _size; - - uint32_t _options; - OutputLog* _log; }; -} // mathpresso namespace +} // {mathpresso} #endif // _MATHPRESSO_MATHPRESSO_P_H diff --git a/src/mathpresso/mpast.cpp b/src/mathpresso/mpast.cpp index f07ef79..8e81dac 100644 --- a/src/mathpresso/mpast.cpp +++ b/src/mathpresso/mpast.cpp @@ -12,25 +12,22 @@ namespace mathpresso { -// ============================================================================ -// [mathpresso::mpAstNodeSize] -// ============================================================================ +// MathPresso - AstNodeSize +// ======================== struct AstNodeSize { - // -------------------------------------------------------------------------- - // [Accessors] - // -------------------------------------------------------------------------- - - MATHPRESSO_INLINE uint32_t nodeType() const { return _nodeType; } - MATHPRESSO_INLINE uint32_t nodeSize() const { return _nodeSize; } - - // -------------------------------------------------------------------------- - // [Members] - // -------------------------------------------------------------------------- + // Members + // ------- uint8_t _nodeType; uint8_t _reserved; uint16_t _nodeSize; + + // Accessors + // --------- + + MATHPRESSO_INLINE uint32_t nodeType() const { return _nodeType; } + MATHPRESSO_INLINE uint32_t nodeSize() const { return _nodeSize; } }; #define ROW(type, size) { type, 0, static_cast(size) } @@ -47,9 +44,8 @@ static const AstNodeSize mpAstNodeSize[] = { }; #undef ROW -// ============================================================================ -// [mathpresso::AstBuilder - Construction / Destruction] -// ============================================================================ +// MathPresso - AstBuilder +// ======================= AstBuilder::AstBuilder(ZoneAllocator* allocator) : _allocator(allocator), @@ -58,10 +54,6 @@ AstBuilder::AstBuilder(ZoneAllocator* allocator) _numSlots(0) {} AstBuilder::~AstBuilder() {} -// ============================================================================ -// [mathpresso::AstBuilder - Factory] -// ============================================================================ - AstScope* AstBuilder::newScope(AstScope* parent, uint32_t scopeType) { void* p = _allocator->alloc(sizeof(AstScope)); if (p == NULL) @@ -149,10 +141,6 @@ void AstBuilder::deleteNode(AstNode* node) { _allocator->release(node, mpAstNodeSize[nodeType].nodeSize()); } -// ============================================================================ -// [mathpresso::AstBuilder - Initialization] -// ============================================================================ - Error AstBuilder::initProgramScope() { if (_rootScope == NULL) { _rootScope = newScope(NULL, kAstScopeGlobal); @@ -167,17 +155,12 @@ Error AstBuilder::initProgramScope() { return kErrorOk; } -// ============================================================================ -// [mathpresso::AstBuilder - Dump] -// ============================================================================ - Error AstBuilder::dump(String& sb) { return AstDump(this, sb).onProgram(programNode()); } -// ============================================================================ -// [mathpresso::AstScope - Construction / Destruction] -// ============================================================================ +// MathPresso - AstScope +// ===================== struct AstScopeReleaseHandler { MATHPRESSO_INLINE AstScopeReleaseHandler(AstBuilder* ast) : _ast(ast) {} @@ -197,10 +180,6 @@ AstScope::~AstScope() { _symbols.reset(handler); } -// ============================================================================ -// [mathpresso::AstScope - Ops] -// ============================================================================ - AstSymbol* AstScope::resolveSymbol(const StringRef& name, uint32_t hashCode, AstScope** scopeOut) { AstScope* scope = this; AstSymbol* symbol; @@ -215,9 +194,8 @@ AstSymbol* AstScope::resolveSymbol(const StringRef& name, uint32_t hashCode, Ast return symbol; } -// ============================================================================ -// [mathpresso::AstNode - Ops] -// ============================================================================ +// MathPresso - AstNode +// ==================== AstNode* AstNode::replaceNode(AstNode* refNode, AstNode* node) { MATHPRESSO_ASSERT(refNode != NULL); @@ -298,9 +276,8 @@ AstNode* AstNode::injectAt(uint32_t index, AstUnary* node) { return child; } -// ============================================================================ -// [mathpresso::AstBlock - Ops] -// ============================================================================ +// MathPresso - AstBlock +// ===================== static Error mpBlockNodeGrow(AstBlock* self) { size_t oldCapacity = self->_capacity; @@ -392,18 +369,13 @@ AstNode* AstBlock::removeAt(uint32_t index) { return oldNode; } -// ============================================================================ -// [mathpresso::AstVisitor - Construction / Destruction] -// ============================================================================ +// MathPresso - AstVisitor +// ======================= AstVisitor::AstVisitor(AstBuilder* ast) : _ast(ast) {} AstVisitor::~AstVisitor() {} -// ============================================================================ -// [mathpresso::AstVisitor - OnNode] -// ============================================================================ - Error AstVisitor::onNode(AstNode* node) { switch (node->nodeType()) { case kAstNodeProgram : return onProgram (static_cast(node)); @@ -424,9 +396,8 @@ Error AstVisitor::onProgram(AstProgram* node) { return onBlock(node); } -// ============================================================================ -// [mathpresso::AstDump - Construction / Destruction] -// ============================================================================ +// MathPresso - AstDump +// ==================== AstDump::AstDump(AstBuilder* ast, String& sb) : AstVisitor(ast), @@ -434,10 +405,6 @@ AstDump::AstDump(AstBuilder* ast, String& sb) _level(0) {} AstDump::~AstDump() {} -// ============================================================================ -// [mathpresso::AstDump - OnNode] -// ============================================================================ - Error AstDump::onBlock(AstBlock* node) { AstNode** children = node->children(); uint32_t i, size = node->size(); @@ -490,10 +457,6 @@ Error AstDump::onCall(AstCall* node) { return denest(); } -// ============================================================================ -// [mathpresso::AstDump - Helpers] -// ============================================================================ - Error AstDump::info(const char* fmt, ...) { va_list ap; va_start(ap, fmt); @@ -527,4 +490,4 @@ Error AstDump::denest() { return kErrorOk; } -} // mathpresso namespace +} // {mathpresso} diff --git a/src/mathpresso/mpast_p.h b/src/mathpresso/mpast_p.h index aebfb4a..2f8dda1 100644 --- a/src/mathpresso/mpast_p.h +++ b/src/mathpresso/mpast_p.h @@ -13,10 +13,6 @@ namespace mathpresso { -// ============================================================================ -// [Forward Declarations] -// ============================================================================ - struct AstBuilder; struct AstScope; struct AstSymbol; @@ -25,9 +21,8 @@ struct AstNode; struct AstProgram; struct AstUnary; -// ============================================================================ -// [mathpresso::AstScopeType] -// ============================================================================ +// MathPresso -AstScopeType +// ======================== enum AstScopeType { //! Global scope. @@ -47,9 +42,8 @@ enum AstScopeType { kAstScopeNested = 3 }; -// ============================================================================ -// [mathpresso::AstSymbolType] -// ============================================================================ +// MathPresso - AstSymbolType +// ========================== //! \internal //! @@ -65,9 +59,8 @@ enum AstSymbolType { kAstSymbolFunction }; -// ============================================================================ -// [mathpresso::AstSymbolFlags] -// ============================================================================ +// MathPresso - AstSymbolFlags +// =========================== enum AstSymbolFlags { //! The symbol was declared in global scope. @@ -93,9 +86,8 @@ enum AstSymbolFlags { kAstSymbolIsAltered = 0x0010 }; -// ============================================================================ -// [mathpresso::AstNodeType] -// ============================================================================ +// MathPresso - AstNodeType +// ======================== //! \internal //! @@ -104,18 +96,16 @@ enum AstNodeType { //! Not used. kAstNodeNone = 0, - // -------------------------------------------------------------------------- - // [Block] - // -------------------------------------------------------------------------- + // Block + // ----- //! Node is `AstProgram`. kAstNodeProgram, //! Node is `AstBlock`. kAstNodeBlock, - // -------------------------------------------------------------------------- - // [Variable, Immediate] - // -------------------------------------------------------------------------- + // Variable & Immediate + // -------------------- //! Node is `AstVarDecl`. kAstNodeVarDecl, @@ -124,9 +114,8 @@ enum AstNodeType { //! Node is `AstImm`. kAstNodeImm, - // -------------------------------------------------------------------------- - // [Op] - // -------------------------------------------------------------------------- + // Operator & Call + // --------------- //! Node is `AstUnaryOp`. kAstNodeUnaryOp, @@ -136,9 +125,8 @@ enum AstNodeType { kAstNodeCall }; -// ============================================================================ -// [mathpresso::AstNodeFlags] -// ============================================================================ +// MathPresso - AstNodeFlags +// ========================= //! \internal //! @@ -147,32 +135,44 @@ enum AstNodeFlags { kAstNodeHasSideEffect = 0x01 }; -// ============================================================================ -// [mathpresso::AstBuilder] -// ============================================================================ +// MathPresso - AstBuilder +// ======================= //! \internal struct AstBuilder { MATHPRESSO_NONCOPYABLE(AstBuilder) - // -------------------------------------------------------------------------- - // [Construction / Destruction] - // -------------------------------------------------------------------------- + // Members + // ------- + + //! Zone allocator. + ZoneAllocator* _allocator; + //! String builder to build possible output messages. + String _sb; + + //! Root scope. + AstScope* _rootScope; + //! Root node. + AstProgram* _programNode; + + //! Number of variable slots used. + uint32_t _numSlots; + + // Construction & Destruction + // -------------------------- AstBuilder(ZoneAllocator* allocator); ~AstBuilder(); - // -------------------------------------------------------------------------- - // [Accessors] - // -------------------------------------------------------------------------- + // Accessors + // --------- MATHPRESSO_INLINE ZoneAllocator* allocator() const { return _allocator; } MATHPRESSO_INLINE AstScope* rootScope() const { return _rootScope; } MATHPRESSO_INLINE AstProgram* programNode() const { return _programNode; } - // -------------------------------------------------------------------------- - // [Factory] - // -------------------------------------------------------------------------- + // Factory + // ------- AstScope* newScope(AstScope* parent, uint32_t scopeType); void deleteScope(AstScope* scope); @@ -209,46 +209,66 @@ struct AstBuilder { MATHPRESSO_INLINE uint32_t newSlotId() { return _numSlots++; } - // -------------------------------------------------------------------------- - // [Init] - // -------------------------------------------------------------------------- + // Init + // ---- Error initProgramScope(); - // -------------------------------------------------------------------------- - // [Dump] - // -------------------------------------------------------------------------- + // Dump + // ---- Error dump(String& sb); +}; - // -------------------------------------------------------------------------- - // [Members] - // -------------------------------------------------------------------------- +// MathPresso - AstSymbol +// ====================== - //! Zone allocator. - ZoneAllocator* _allocator; - //! String builder to build possible output messages. - String _sb; +struct AstSymbol : public HashNode { + MATHPRESSO_NONCOPYABLE(AstSymbol) - //! Root scope. - AstScope* _rootScope; - //! Root node. - AstProgram* _programNode; + // Members + // ------- - //! Number of variable slots used. - uint32_t _numSlots; -}; + //! Symbol name (key). + const char* _name; + //! Symbol name size. + uint32_t _nameSize; -// ============================================================================ -// [mathpresso::AstSymbol] -// ============================================================================ + //! Type of the symbol, see \ref AstSymbolType. + uint8_t _symbolType; + //! Operator type (if the symbol is \ref kAstSymbolIntrinsic). + uint8_t _opType; + //! Flags, see \ref AstSymbolFlags. + uint16_t _symbolFlags; -struct AstSymbol : public HashNode { - MATHPRESSO_NONCOPYABLE(AstSymbol) + //! Number of times the variable is used (both read and write count). + uint32_t _usedCount; + //! Number of times the variable is written. + uint32_t _writeCount; - // -------------------------------------------------------------------------- - // [Construction / Destruction] - // -------------------------------------------------------------------------- + //! Node where the symbol is defined. + AstNode* _node; + + union { + struct { + //! Variable slot id. + uint32_t _varSlotId; + //! Variable offset in data structure (in case the symbol is a global variable). + int32_t _varOffset; + //! The current value of the symbol (in case the symbol is an immediate). + double _value; + }; + + struct { + //! Function pointer (in case the symbol is a function). + void* _funcPtr; + //! Number of function arguments (in case the symbol is a function). + uint32_t _funcArgs; + }; + }; + + // Construction & Destruction + // -------------------------- MATHPRESSO_INLINE AstSymbol(const char* name, uint32_t nameSize, uint32_t hashCode, uint32_t symbolType, uint32_t scopeType) : HashNode(hashCode), @@ -262,9 +282,8 @@ struct AstSymbol : public HashNode { _node(nullptr), _value() {} - // -------------------------------------------------------------------------- - // [Accessors] - // -------------------------------------------------------------------------- + // Accessors + // --------- MATHPRESSO_INLINE bool eq(const StringRef& s) const { return eq(s.data(), s.size()); @@ -364,70 +383,39 @@ struct AstSymbol : public HashNode { MATHPRESSO_INLINE void decUsedCount(uint32_t n = 1) { _usedCount -= n; } MATHPRESSO_INLINE void decWriteCount(uint32_t n = 1) { _writeCount -= n; } - - // -------------------------------------------------------------------------- - // [Members] - // -------------------------------------------------------------------------- - - //! Symbol name (key). - const char* _name; - //! Symbol name size. - uint32_t _nameSize; - - //! Type of the symbol, see \ref AstSymbolType. - uint8_t _symbolType; - //! Operator type (if the symbol is \ref kAstSymbolIntrinsic). - uint8_t _opType; - //! Flags, see \ref AstSymbolFlags. - uint16_t _symbolFlags; - - //! Number of times the variable is used (both read and write count). - uint32_t _usedCount; - //! Number of times the variable is written. - uint32_t _writeCount; - - //! Node where the symbol is defined. - AstNode* _node; - - union { - struct { - //! Variable slot id. - uint32_t _varSlotId; - //! Variable offset in data structure (in case the symbol is a global variable). - int32_t _varOffset; - //! The current value of the symbol (in case the symbol is an immediate). - double _value; - }; - - struct { - //! Function pointer (in case the symbol is a function). - void* _funcPtr; - //! Number of function arguments (in case the symbol is a function). - uint32_t _funcArgs; - }; - }; }; typedef Hash AstSymbolHash; typedef HashIterator AstSymbolHashIterator; -// ============================================================================ -// [mathpresso::AstScope] -// ============================================================================ +// MathPresso - AstScope +// ===================== struct AstScope { MATHPRESSO_NONCOPYABLE(AstScope) - // -------------------------------------------------------------------------- - // [Construction / Destruction] - // -------------------------------------------------------------------------- + // Members + // ------- + + //! Context. + AstBuilder* _ast; + //! Parent scope. + AstScope* _parent; + + //! Symbols defined in this scope. + AstSymbolHash _symbols; + + //! Scope type, see \ref AstScopeType. + uint32_t _scopeType; + + // Construction & Destruction + // -------------------------- MATHPRESSO_NOAPI AstScope(AstBuilder* ast, AstScope* parent, uint32_t scopeType); MATHPRESSO_NOAPI ~AstScope(); - // -------------------------------------------------------------------------- - // [Accessors] - // -------------------------------------------------------------------------- + // Accessors + // --------- //! Get the scope context. MATHPRESSO_INLINE AstBuilder* ast() const { return _ast; } @@ -447,9 +435,8 @@ struct AstScope { _scopeType = kAstScopeShadow; } - // -------------------------------------------------------------------------- - // [Ops] - // -------------------------------------------------------------------------- + // Operations + // ---------- //! Get the symbol defined only in this scope. MATHPRESSO_INLINE AstSymbol* getSymbol(const StringRef& name, uint32_t hashCode) { @@ -478,26 +465,10 @@ struct AstScope { MATHPRESSO_NOAPI AstSymbol* removeSymbol(AstSymbol* symbol) { return _symbols.del(symbol); } - - // -------------------------------------------------------------------------- - // [Members] - // -------------------------------------------------------------------------- - - //! Context. - AstBuilder* _ast; - //! Parent scope. - AstScope* _parent; - - //! Symbols defined in this scope. - AstSymbolHash _symbols; - - //! Scope type, see \ref AstScopeType. - uint32_t _scopeType; }; -// ============================================================================ -// [mathpresso::AstNode] -// ============================================================================ +// MathPresso - AstNode +// ==================== #define MATHPRESSO_AST_CHILD(INDEX, NODE_T, NAME, NAME_UP) \ MATHPRESSO_INLINE NODE_T* NAME() const { return _##NAME; } \ @@ -520,9 +491,33 @@ struct AstScope { struct AstNode { MATHPRESSO_NONCOPYABLE(AstNode) - // -------------------------------------------------------------------------- - // [Construction / Destruction] - // -------------------------------------------------------------------------- + // Members + // ------- + + //! AST builder. + AstBuilder* _ast; + //! Parent node. + AstNode* _parent; + //! Child nodes. + AstNode** _children; + + //! Node type, see `AstNodeType`. + uint8_t _nodeType; + //! Node flags, see `AstNodeFlags`. + uint8_t _nodeFlags; + //! Node size in bytes for `ZoneAllocator`. + uint8_t _nodeSize; + //! Operator type, see `OpType`. + uint8_t _opType; + + //! Position (in characters) to the beginning of the program (default -1). + uint32_t _position; + + //! Count of child-nodes. + uint32_t _size; + + // Construction & Destruction + // -------------------------- MATHPRESSO_INLINE AstNode(AstBuilder* ast, uint32_t nodeType, AstNode** children = NULL, uint32_t size = 0) : _ast(ast), @@ -537,9 +532,8 @@ struct AstNode { MATHPRESSO_INLINE void destroy(AstBuilder*) {} - // -------------------------------------------------------------------------- - // [Accessors] - // -------------------------------------------------------------------------- + // Accessors + // --------- //! Get the `AstBuilder` instance that created this node. MATHPRESSO_INLINE AstBuilder* ast() const { return _ast; } @@ -589,9 +583,8 @@ struct AstNode { //! Reset source code position of the node. MATHPRESSO_INLINE void resetPosition() { _position = ~static_cast(0); } - // -------------------------------------------------------------------------- - // [Children] - // -------------------------------------------------------------------------- + // Children + // -------- MATHPRESSO_INLINE AstNode* childAt(uint32_t index) const { MATHPRESSO_ASSERT(index < _size); @@ -607,52 +600,28 @@ struct AstNode { AstNode* injectNode(AstNode* refNode, AstUnary* node); //! Inject `node` between this node and node at index `index`. AstNode* injectAt(uint32_t index, AstUnary* node); - - // -------------------------------------------------------------------------- - // [Members] - // -------------------------------------------------------------------------- - - //! AST builder. - AstBuilder* _ast; - //! Parent node. - AstNode* _parent; - //! Child nodes. - AstNode** _children; - - //! Node type, see `AstNodeType`. - uint8_t _nodeType; - //! Node flags, see `AstNodeFlags`. - uint8_t _nodeFlags; - //! Node size in bytes for `ZoneAllocator`. - uint8_t _nodeSize; - //! Operator type, see `OpType`. - uint8_t _opType; - - //! Position (in characters) to the beginning of the program (default -1). - uint32_t _position; - - //! Count of child-nodes. - uint32_t _size; }; -// ============================================================================ -// [mathpresso::AstBlock] -// ============================================================================ +// MathPresso - AstBlock +// ===================== struct AstBlock : public AstNode { MATHPRESSO_NONCOPYABLE(AstBlock) - // -------------------------------------------------------------------------- - // [Construction / Destruction] - // -------------------------------------------------------------------------- + // Members + // ------- + + uint32_t _capacity; + + // Construction & Destruction + // -------------------------- MATHPRESSO_INLINE AstBlock(AstBuilder* ast, uint32_t nodeType = kAstNodeBlock) : AstNode(ast, nodeType), _capacity(0) {} - // -------------------------------------------------------------------------- - // [Ops] - // -------------------------------------------------------------------------- + // Operations + // ---------- //! Reserve the capacity of the AstBlock so one more node can be added into it. //! @@ -707,97 +676,84 @@ struct AstBlock : public AstNode { AstNode* removeNode(AstNode* node); //! Remove the node at index `index`. AstNode* removeAt(uint32_t index); - - // -------------------------------------------------------------------------- - // [Members] - // -------------------------------------------------------------------------- - - uint32_t _capacity; }; -// ============================================================================ -// [mathpresso::AstUnary] -// ============================================================================ +// MathPresso - AstUnary +// ===================== struct AstUnary : public AstNode { MATHPRESSO_NONCOPYABLE(AstUnary) - // -------------------------------------------------------------------------- - // [Construction / Destruction] - // -------------------------------------------------------------------------- + // Members + // ------- + + MATHPRESSO_AST_CHILD(0, AstNode, child, Child); + + // Construction & Destruction + // -------------------------- MATHPRESSO_INLINE AstUnary(AstBuilder* ast, uint32_t nodeType) : AstNode(ast, nodeType, &_child, 1), _child(NULL) {} - // -------------------------------------------------------------------------- - // [Accessors] - // -------------------------------------------------------------------------- + // Accessors + // --------- MATHPRESSO_INLINE AstNode** children() const { return (AstNode**)&_child; } - - // -------------------------------------------------------------------------- - // [Members] - // -------------------------------------------------------------------------- - - MATHPRESSO_AST_CHILD(0, AstNode, child, Child); }; -// ============================================================================ -// [mathpresso::AstBinary] -// ============================================================================ +// MathPresso - AstBinary +// ====================== struct AstBinary : public AstNode { MATHPRESSO_NONCOPYABLE(AstBinary) - // -------------------------------------------------------------------------- - // [Construction / Destruction] - // -------------------------------------------------------------------------- + // Members + // ------- + + MATHPRESSO_AST_CHILD(0, AstNode, left, Left); + MATHPRESSO_AST_CHILD(1, AstNode, right, Right); + + // Construction & Destruction + // -------------------------- MATHPRESSO_INLINE AstBinary(AstBuilder* ast, uint32_t nodeType) : AstNode(ast, nodeType, &_left, 2), _left(NULL), _right(NULL) {} - // -------------------------------------------------------------------------- - // [Accessors] - // -------------------------------------------------------------------------- + // Accessors + // --------- MATHPRESSO_INLINE AstNode** children() const { return (AstNode**)&_left; } - - // -------------------------------------------------------------------------- - // [Members] - // -------------------------------------------------------------------------- - - MATHPRESSO_AST_CHILD(0, AstNode, left, Left); - MATHPRESSO_AST_CHILD(1, AstNode, right, Right); }; -// ============================================================================ -// [mathpresso::AstProgram] -// ============================================================================ +// MathPresso - AstProgram +// ======================= struct AstProgram : public AstBlock { MATHPRESSO_NONCOPYABLE(AstProgram) - // -------------------------------------------------------------------------- - // [Construction / Destruction] - // -------------------------------------------------------------------------- + // Construction & Destruction + // -------------------------- MATHPRESSO_INLINE AstProgram(AstBuilder* ast) : AstBlock(ast, kAstNodeProgram) {} }; -// ============================================================================ -// [mathpresso::AstVarDecl] -// ============================================================================ +// MathPresso - AstVarDecl +// ======================= struct AstVarDecl : public AstUnary { MATHPRESSO_NONCOPYABLE(AstVarDecl) - // -------------------------------------------------------------------------- - // [Construction / Destruction] - // -------------------------------------------------------------------------- + // Members + // ------- + + AstSymbol* _symbol; + + // Construction & Destruction + // -------------------------- MATHPRESSO_INLINE AstVarDecl(AstBuilder* ast) : AstUnary(ast, kAstNodeVarDecl), @@ -808,103 +764,84 @@ struct AstVarDecl : public AstUnary { symbol()->decUsedCount(); } - // -------------------------------------------------------------------------- - // [Accessors] - // -------------------------------------------------------------------------- + // Accessors + // --------- MATHPRESSO_INLINE AstSymbol* symbol() const { return _symbol; } MATHPRESSO_INLINE void setSymbol(AstSymbol* symbol) { _symbol = symbol; } - - // -------------------------------------------------------------------------- - // [Members] - // -------------------------------------------------------------------------- - - AstSymbol* _symbol; }; -// ============================================================================ -// [mathpresso::AstVar] -// ============================================================================ +// MathPresso - AstVar +// =================== struct AstVar : public AstNode { MATHPRESSO_NONCOPYABLE(AstVar) - // -------------------------------------------------------------------------- - // [Construction / Destruction] - // -------------------------------------------------------------------------- + // Members + // ------- + + AstSymbol* _symbol; + + // Construction & Destruction + // -------------------------- MATHPRESSO_INLINE AstVar(AstBuilder* ast) : AstNode(ast, kAstNodeVar), _symbol(NULL) {} - // -------------------------------------------------------------------------- - // [Accessors] - // -------------------------------------------------------------------------- + // Accessors + // --------- MATHPRESSO_INLINE AstSymbol* symbol() const { return _symbol; } MATHPRESSO_INLINE void setSymbol(AstSymbol* symbol) { _symbol = symbol; } - - // -------------------------------------------------------------------------- - // [Members] - // -------------------------------------------------------------------------- - - AstSymbol* _symbol; }; -// ============================================================================ -// [mathpresso::AstImm] -// ============================================================================ +// MathPresso - AstImm +// =================== struct AstImm : public AstNode { MATHPRESSO_NONCOPYABLE(AstImm) - // -------------------------------------------------------------------------- - // [Construction / Destruction] - // -------------------------------------------------------------------------- + // Members + // ------- + + double _value; + + // Construction & Destruction + // -------------------------- MATHPRESSO_INLINE AstImm(AstBuilder* ast, double value = 0.0) : AstNode(ast, kAstNodeImm), _value(value) {} - // -------------------------------------------------------------------------- - // [Accessors] - // -------------------------------------------------------------------------- + // Accessors + // --------- MATHPRESSO_INLINE double value() const { return _value; } MATHPRESSO_INLINE void setValue(double value) { _value = value; } - - // -------------------------------------------------------------------------- - // [Members] - // -------------------------------------------------------------------------- - - double _value; }; -// ============================================================================ -// [mathpresso::AstUnaryOp] -// ============================================================================ +// MathPresso - AstUnaryOp +// ======================= struct AstUnaryOp : public AstUnary { MATHPRESSO_NONCOPYABLE(AstUnaryOp) - // -------------------------------------------------------------------------- - // [Construction / Destruction] - // -------------------------------------------------------------------------- + // Construction & Destruction + // -------------------------- MATHPRESSO_INLINE AstUnaryOp(AstBuilder* ast, uint32_t opType) : AstUnary(ast, kAstNodeUnaryOp) { setOpType(opType); } }; -// ============================================================================ -// [mathpresso::AstBinaryOp] -// ============================================================================ +// MathPresso - AstBinaryOp +// ======================== struct AstBinaryOp : public AstBinary { MATHPRESSO_NONCOPYABLE(AstBinaryOp) - // -------------------------------------------------------------------------- - // [Construction / Destruction] - // -------------------------------------------------------------------------- + // Construction & Destruction + // -------------------------- MATHPRESSO_INLINE AstBinaryOp(AstBuilder* ast, uint32_t opType) : AstBinary(ast, kAstNodeBinaryOp) { setOpType(opType); } @@ -918,58 +855,55 @@ struct AstBinaryOp : public AstBinary { } }; -// ============================================================================ -// [mathpresso::AstCall] -// ============================================================================ +// MathPresso - AstCall +// ==================== struct AstCall : public AstBlock { MATHPRESSO_NONCOPYABLE(AstCall) - // -------------------------------------------------------------------------- - // [Construction / Destruction] - // -------------------------------------------------------------------------- + // Members + // ------- + + AstSymbol* _symbol; + + // Construction & Destruction + // -------------------------- MATHPRESSO_INLINE AstCall(AstBuilder* ast) : AstBlock(ast, kAstNodeCall), _symbol(NULL) {} - // -------------------------------------------------------------------------- - // [Accessors] - // -------------------------------------------------------------------------- + // Accessors + // --------- MATHPRESSO_INLINE AstSymbol* symbol() const { return _symbol; } MATHPRESSO_INLINE void setSymbol(AstSymbol* symbol) { _symbol = symbol; } - - // -------------------------------------------------------------------------- - // [Members] - // -------------------------------------------------------------------------- - - AstSymbol* _symbol; }; -// ============================================================================ -// [mathpresso::AstVisitor] -// ============================================================================ +// MathPresso - AstVisitor +// ======================= struct AstVisitor { MATHPRESSO_NONCOPYABLE(AstVisitor) - // -------------------------------------------------------------------------- - // [Construction / Destruction] - // -------------------------------------------------------------------------- + // Members + // ------- + + AstBuilder* _ast; + + // Construction & Destruction + // -------------------------- AstVisitor(AstBuilder* ast); virtual ~AstVisitor(); - // -------------------------------------------------------------------------- - // [Accessors] - // -------------------------------------------------------------------------- + // Accessors + // --------- MATHPRESSO_INLINE AstBuilder* ast() const { return _ast; } - // -------------------------------------------------------------------------- - // [OnNode] - // -------------------------------------------------------------------------- + // OnNode + // ------ virtual Error onNode(AstNode* node); @@ -981,31 +915,28 @@ struct AstVisitor { virtual Error onUnaryOp(AstUnaryOp* node) = 0; virtual Error onBinaryOp(AstBinaryOp* node) = 0; virtual Error onCall(AstCall* node) = 0; - - // -------------------------------------------------------------------------- - // [Members] - // -------------------------------------------------------------------------- - - AstBuilder* _ast; }; -// ============================================================================ -// [mathpresso::AstDump] -// ============================================================================ +// MathPresso - AstDump +// ==================== struct AstDump : public AstVisitor { MATHPRESSO_NONCOPYABLE(AstDump) - // -------------------------------------------------------------------------- - // [Construction / Destruction] - // -------------------------------------------------------------------------- + // Members + // ------- + + String& _sb; + uint32_t _level; + + // Construction & Destruction + // -------------------------- AstDump(AstBuilder* ast, String& sb); virtual ~AstDump(); - // -------------------------------------------------------------------------- - // [OnNode] - // -------------------------------------------------------------------------- + // OnNode + // ------ virtual Error onBlock(AstBlock* node); virtual Error onVarDecl(AstVarDecl* node); @@ -1015,23 +946,15 @@ struct AstDump : public AstVisitor { virtual Error onBinaryOp(AstBinaryOp* node); virtual Error onCall(AstCall* node); - // -------------------------------------------------------------------------- - // [Helpers] - // -------------------------------------------------------------------------- + // Helpers + // ------- Error info(const char* fmt, ...); Error nest(const char* fmt, ...); Error denest(); - - // -------------------------------------------------------------------------- - // [Members] - // -------------------------------------------------------------------------- - - String& _sb; - uint32_t _level; }; -} // mathpresso namespace +} // {mathpresso} // [Guard] #endif // _MATHPRESSO_MPAST_P_H diff --git a/src/mathpresso/mpatomic_p.h b/src/mathpresso/mpatomic_p.h index 72ffebd..ec93160 100644 --- a/src/mathpresso/mpatomic_p.h +++ b/src/mathpresso/mpatomic_p.h @@ -13,9 +13,8 @@ namespace mathpresso { -// ============================================================================ -// [mathpresso::mpAtomic] -// ============================================================================ +// MathPresso - Atomics +// ==================== //! \internal static MATHPRESSO_INLINE uintptr_t mpAtomicGet(const uintptr_t* atomic) { @@ -75,7 +74,7 @@ MATHPRESSO_INLINE T mpAtomicSetXchgT(T* atomic, T value) { return (T)mpAtomicSetXchg((uintptr_t *)atomic, (uintptr_t)value); } -} // mathpresso namespace +} // {mathpresso} // [Guard] #endif // _MATHPRESSO_MPATOMIC_P_H diff --git a/src/mathpresso/mpcompiler.cpp b/src/mathpresso/mpcompiler.cpp index 5def52f..c8b14d8 100644 --- a/src/mathpresso/mpcompiler.cpp +++ b/src/mathpresso/mpcompiler.cpp @@ -16,18 +16,16 @@ namespace mathpresso { using namespace asmjit; -// ============================================================================ -// [mathpresso::JitGlobal] -// ============================================================================ +// MathPresso - JIT Globals +// ======================== struct JitGlobal { JitRuntime runtime; }; static JitGlobal jitGlobal; -// ============================================================================ -// [mathpresso::JitUtils] -// ============================================================================ +// MathPresso - JIT Utilities +// ========================== struct JitUtils { static void* funcByOp(uint32_t op) { @@ -77,9 +75,8 @@ struct JitUtils { } }; -// ============================================================================ -// [mathpresso::JitVar] -// ============================================================================ +// MathPresso - JIT Variable +// ========================= struct MATHPRESSO_NOAPI JitVar { enum FLAGS { @@ -127,9 +124,8 @@ struct MATHPRESSO_NOAPI JitVar { uint32_t _flags; }; -// ============================================================================ -// [mathpresso::JitCompiler] -// ============================================================================ +// MathPresso - JIT Compiler +// ========================= struct MATHPRESSO_NOAPI JitCompiler { JitCompiler(ZoneAllocator* allocator, x86::Compiler* c); @@ -550,7 +546,7 @@ emitInst: { vl = writableVar(vl); vr = registerVar(vr); - cc->movsd(result, vl.xmm()); + cc->movapd(result, vl.xmm()); cc->divsd(vl.xmm(), vr.xmm()); inlineRound(vl.xmm(), vl.xmm(), kOpTrunc); cc->mulsd(vl.xmm(), vr.xmm()); @@ -610,7 +606,7 @@ void JitCompiler::inlineRound(const x86::Xmm& dst, const x86::Xmm& src, uint32_t cc->roundsd(tmp, src, x86::RoundImm::kDown | x86::RoundImm::kSuppress); if (dst.id() != src.id()) - cc->movsd(dst, src); + cc->movapd(dst, src); cc->subsd(dst, tmp); cc->cmpsd(dst, getConstantD64(0.5).mem(), x86::CmpImm::kNLT); @@ -646,12 +642,12 @@ void JitCompiler::inlineRound(const x86::Xmm& dst, const x86::Xmm& src, uint32_t if (op == kOpRoundEven) { x86::Xmm tmp = cc->newXmmSd(); - cc->movsd(tmp, src); + cc->movapd(tmp, src); cc->cmpsd(tmp, getConstantD64(maxn).mem(), x86::CmpImm::kLT); cc->andpd(tmp, getConstantD64Aligned(magic0).mem()); if (dst.id() != src.id()) - cc->movsd(dst, src); + cc->movapd(dst, src); cc->addsd(dst, tmp); cc->subsd(dst, tmp); @@ -666,18 +662,18 @@ void JitCompiler::inlineRound(const x86::Xmm& dst, const x86::Xmm& src, uint32_t x86::Xmm t2 = cc->newXmmSd(); x86::Xmm t3 = cc->newXmmSd(); - cc->movsd(t2, src); - cc->movsd(t3, src); + cc->movapd(t2, src); + cc->movapd(t3, src); if (dst.id() != src.id()) - cc->movsd(dst, src); + cc->movapd(dst, src); switch (op) { case kOpRound: cc->addsd(t2, getConstantD64(magic0).mem()); cc->addsd(t3, getConstantD64(magic1).mem()); - cc->movsd(t1, src); + cc->movapd(t1, src); cc->subsd(t2, getConstantD64(magic0).mem()); cc->subsd(t3, getConstantD64(magic1).mem()); @@ -689,7 +685,7 @@ void JitCompiler::inlineRound(const x86::Xmm& dst, const x86::Xmm& src, uint32_t case kOpFloor: cc->addsd(t2, getConstantD64(magic0).mem()); - cc->movsd(t1, src); + cc->movapd(t1, src); cc->subsd(t2, getConstantD64(magic0).mem()); cc->cmpsd(t1, getConstantD64(maxn).mem(), x86::CmpImm::kNLT); @@ -703,7 +699,7 @@ void JitCompiler::inlineRound(const x86::Xmm& dst, const x86::Xmm& src, uint32_t case kOpCeil: cc->addsd(t2, getConstantD64(magic0).mem()); - cc->movsd(t1, src); + cc->movapd(t1, src); cc->subsd(t2, getConstantD64(magic0).mem()); cc->cmpsd(t1, getConstantD64(maxn).mem(), x86::CmpImm::kNLT); @@ -723,33 +719,54 @@ void JitCompiler::inlineRound(const x86::Xmm& dst, const x86::Xmm& src, uint32_t } if (op == kOpTrunc) { - x86::Xmm t1 = cc->newXmmSd(); - x86::Xmm t2 = cc->newXmmSd(); - x86::Xmm t3 = cc->newXmmSd(); + if (cc->is64Bit()) { + // In 64-bit mode we can just do a roundtrip with CVTTSD2SI and CVTSI2SD + // and use such value if the floating point was not already an integer. + x86::Gp r = cc->newInt64(); + x86::Xmm t = cc->newXmmSd(); + x86::Xmm d = dst.id() == src.id() ? cc->newXmmSd() : dst; + + cc->cvttsd2si(r, src); + cc->movsd(t, getConstantU64(0x7FFFFFFFFFFFFFFFu).mem()); + cc->andpd(t, src); + + cc->cvtsi2sd(d, r); + cc->cmpsd(t, getConstantD64(maxn).mem(), x86::CmpImm::kLT); + cc->andpd(d, t); + cc->andnpd(t, src); + cc->orpd(d, t); + + if (dst.id() != d.id()) + cc->movapd(dst, d); + } + else { + x86::Xmm t1 = cc->newXmmSd(); + x86::Xmm t2 = cc->newXmmSd(); + x86::Xmm t3 = cc->newXmmSd(); - cc->movsd(t2, getConstantU64(0x7FFFFFFFFFFFFFFFu).mem()); - cc->andpd(t2, src); + cc->movsd(t2, getConstantU64(0x7FFFFFFFFFFFFFFFu).mem()); + cc->andpd(t2, src); - if (dst.id() != src.id()) - cc->movsd(dst, src); + if (dst.id() != src.id()) + cc->movapd(dst, src); - cc->movsd(t1, t2); - cc->addsd(t2, getConstantD64(magic0).mem()); - cc->movsd(t3, t1); + cc->movapd(t1, t2); + cc->addsd(t2, getConstantD64(magic0).mem()); + cc->movapd(t3, t1); - cc->subsd(t2, getConstantD64(magic0).mem()); - cc->cmpsd(t1, getConstantD64(maxn).mem(), x86::CmpImm::kNLT); + cc->subsd(t2, getConstantD64(magic0).mem()); + cc->cmpsd(t1, getConstantD64(maxn).mem(), x86::CmpImm::kNLT); - cc->cmpsd(t3, t2, x86::CmpImm::kLT); - cc->orpd(t1, getConstantU64AsPD(0x8000000000000000u).mem()); - cc->andpd(t3, getConstantD64AsPD(1.0).mem()); + cc->cmpsd(t3, t2, x86::CmpImm::kLT); + cc->orpd(t1, getConstantU64AsPD(0x8000000000000000u).mem()); + cc->andpd(t3, getConstantD64AsPD(1.0).mem()); - cc->andpd(dst, t1); - cc->subpd(t2, t3); - - cc->andnpd(t1, t2); - cc->orpd(dst, t1); + cc->andpd(dst, t1); + cc->subpd(t2, t3); + cc->andnpd(t1, t2); + cc->orpd(dst, t1); + } return; } @@ -878,4 +895,4 @@ void mpFreeFunction(void* fn) { jitGlobal.runtime.release(fn); } -} // mathpresso namespace +} // {mathpresso} diff --git a/src/mathpresso/mpcompiler_p.h b/src/mathpresso/mpcompiler_p.h index dd2aa78..0aa6312 100644 --- a/src/mathpresso/mpcompiler_p.h +++ b/src/mathpresso/mpcompiler_p.h @@ -16,7 +16,7 @@ namespace mathpresso { MATHPRESSO_NOAPI CompiledFunc mpCompileFunction(AstBuilder* ast, uint32_t options, OutputLog* log); MATHPRESSO_NOAPI void mpFreeFunction(void* fn); -} // mathpresso namespace +} // {mathpresso} // [Guard] #endif // _MATHPRESSO_MPCOMPILER_P_H diff --git a/src/mathpresso/mpeval_p.h b/src/mathpresso/mpeval_p.h index ace5afd..8f68a7d 100644 --- a/src/mathpresso/mpeval_p.h +++ b/src/mathpresso/mpeval_p.h @@ -13,9 +13,8 @@ namespace mathpresso { -// ============================================================================ -// [mathpresso::Evaluation] -// ============================================================================ +// MathPresso - Evaluation +// ======================= //! DP-FP binary representation and utilities. union DoubleBits { @@ -104,6 +103,6 @@ static MATHPRESSO_INLINE double mpAtan(double x) { return ::atan(x); } static MATHPRESSO_INLINE double mpAtan2(double y, double x) { return ::atan2(y, x); } static MATHPRESSO_INLINE double mpHypot(double x, double y) { return ::hypot(x, y); } -} // mathpresso namespace +} // {mathpresso} #endif // _MATHPRESSO_MPEVAL_P_H diff --git a/src/mathpresso/mphash.cpp b/src/mathpresso/mphash.cpp index a9fc742..33d28cd 100644 --- a/src/mathpresso/mphash.cpp +++ b/src/mathpresso/mphash.cpp @@ -12,17 +12,12 @@ namespace mathpresso { -// ============================================================================ -// [Helpers] -// ============================================================================ - static const uint32_t mpPrimeTable[] = { 19, 53, 97, 193, 389, 769, 1543, 3079, 6151, 12289, 24593 }; -// ============================================================================ -// [mathpresso::HashUtils] -// ============================================================================ +// MathPresso - HashUtils +// ====================== uint32_t HashUtils::hashString(const char* data, size_t size) { if (size == 0) @@ -50,9 +45,8 @@ uint32_t HashUtils::closestPrime(uint32_t x) { return p; } -// ============================================================================ -// [mathpresso::HashBase - Reset / Rehash] -// ============================================================================ +// MathPresso - HashBase +// ===================== void HashBase::_rehash(uint32_t newCount) { ZoneAllocator* allocator = _allocator; @@ -140,10 +134,6 @@ void HashBase::_mergeToInvisibleSlot(HashBase& other) { } } -// ============================================================================ -// [mathpresso::HashBase - Ops] -// ============================================================================ - HashNode* HashBase::_put(HashNode* node) { uint32_t hMod = node->_hashCode % _bucketsCount; HashNode* next = _data[hMod]; @@ -179,4 +169,4 @@ HashNode* HashBase::_del(HashNode* node) { return NULL; } -} // mathpresso namespace +} // {mathpresso} diff --git a/src/mathpresso/mphash_p.h b/src/mathpresso/mphash_p.h index 3b603ee..77cbf4f 100644 --- a/src/mathpresso/mphash_p.h +++ b/src/mathpresso/mphash_p.h @@ -13,9 +13,8 @@ namespace mathpresso { -// ============================================================================ -// [mathpresso::HashUtils] -// ============================================================================ +// MathPresso - HashUtils +// ====================== namespace HashUtils { // \internal @@ -42,9 +41,8 @@ namespace HashUtils { MATHPRESSO_NOAPI uint32_t closestPrime(uint32_t x); }; -// ============================================================================ -// [mathpresso::HashNode] -// ============================================================================ +// MathPresso - HashNode +// ===================== struct HashNode { MATHPRESSO_INLINE HashNode(uint32_t hashCode = 0) : _next(NULL), _hashCode(hashCode) {} @@ -55,9 +53,8 @@ struct HashNode { uint32_t _hashCode; }; -// ============================================================================ -// [mathpresso::HashBase] -// ============================================================================ +// MathPresso - HashBase +// ===================== struct HashBase { MATHPRESSO_NONCOPYABLE(HashBase) @@ -67,9 +64,20 @@ struct HashBase { kExtraCount = 1 }; - // -------------------------------------------------------------------------- - // [Construction / Destruction] - // -------------------------------------------------------------------------- + // Members + // ------- + + ZoneAllocator* _allocator; + + uint32_t _size; + uint32_t _bucketsCount; + uint32_t _bucketsGrow; + + HashNode** _data; + HashNode* _embedded[1 + kExtraCount]; + + // Construction & Destruction + // -------------------------- MATHPRESSO_INLINE HashBase(ZoneAllocator* allocator) { _allocator = allocator; @@ -88,39 +96,23 @@ struct HashBase { _allocator->release(_data, static_cast(_bucketsCount + kExtraCount) * sizeof(void*)); } - // -------------------------------------------------------------------------- - // [Accessors] - // -------------------------------------------------------------------------- + // Accessors + // --------- MATHPRESSO_INLINE ZoneAllocator* allocator() const { return _allocator; } - // -------------------------------------------------------------------------- - // [Ops] - // -------------------------------------------------------------------------- + // Operations + // ---------- MATHPRESSO_NOAPI void _rehash(uint32_t newCount); MATHPRESSO_NOAPI void _mergeToInvisibleSlot(HashBase& other); MATHPRESSO_NOAPI HashNode* _put(HashNode* node); MATHPRESSO_NOAPI HashNode* _del(HashNode* node); - - // -------------------------------------------------------------------------- - // [Reset / Rehash] - // -------------------------------------------------------------------------- - - ZoneAllocator* _allocator; - - uint32_t _size; - uint32_t _bucketsCount; - uint32_t _bucketsGrow; - - HashNode** _data; - HashNode* _embedded[1 + kExtraCount]; }; -// ============================================================================ -// [mathpresso::Hash] -// ============================================================================ +// MathPresso - Hash +// ============================ //! \internal //! @@ -143,16 +135,14 @@ struct HashBase { //! symbols and by AST to IR translator to associate IR specific data with AST. template struct Hash : public HashBase { - // -------------------------------------------------------------------------- - // [Construction / Destruction] - // -------------------------------------------------------------------------- + // Construction & Destruction + // -------------------------- MATHPRESSO_INLINE Hash(ZoneAllocator* allocator) : HashBase(allocator) {} - // -------------------------------------------------------------------------- - // [Ops] - // -------------------------------------------------------------------------- + // Operations + // ---------- template void reset(ReleaseHandler& handler) { @@ -203,9 +193,8 @@ struct Hash : public HashBase { MATHPRESSO_INLINE Node* del(Node* node) { return static_cast(_del(node)); } }; -// ============================================================================ -// [mathpresso::HashIterator] -// ============================================================================ +// MathPresso - HashIterator +// ==================================== template struct HashIterator { @@ -260,9 +249,8 @@ struct HashIterator { uint32_t _count; }; -// ============================================================================ -// [mathpresso::Map] -// ============================================================================ +// MathPresso - Map +// ============================ //! \internal template @@ -287,9 +275,13 @@ struct Map { ZoneAllocator* _allocator; }; - // -------------------------------------------------------------------------- - // [Construction / Destruction] - // -------------------------------------------------------------------------- + // Members + // ------- + + Hash _hash; + + // Construction & Destruction + // -------------------------- MATHPRESSO_INLINE Map(ZoneAllocator* allocator) : _hash(allocator) {} @@ -299,9 +291,8 @@ struct Map { _hash.reset(releaseHandler); } - // -------------------------------------------------------------------------- - // [Ops] - // -------------------------------------------------------------------------- + // Operations + // ---------- MATHPRESSO_INLINE Value get(const Key& key) const { uint32_t hashCode = HashUtils::hashPointer(key); @@ -327,11 +318,9 @@ struct Map { return kErrorOk; } - - Hash _hash; }; -} // mathpresso namespace +} // {mathpresso} // [Guard] #endif // _MATHPRESSO_MPHASH_P_H diff --git a/src/mathpresso/mpoptimizer.cpp b/src/mathpresso/mpoptimizer.cpp index 3aa33da..6628f26 100644 --- a/src/mathpresso/mpoptimizer.cpp +++ b/src/mathpresso/mpoptimizer.cpp @@ -14,19 +14,14 @@ namespace mathpresso { -// ============================================================================ -// [mpsl::AstOptimizer - Construction / Destruction] -// ============================================================================ +// MathPresso - AstOptimizer +// ========================= AstOptimizer::AstOptimizer(AstBuilder* ast, ErrorReporter* errorReporter) : AstVisitor(ast), _errorReporter(errorReporter) {} AstOptimizer::~AstOptimizer() {} -// ============================================================================ -// [mpsl::AstOptimizer - OnNode] -// ============================================================================ - Error AstOptimizer::onBlock(AstBlock* node) { // Prevent removing nodes that are not stored in pure `AstBlock`. For example // function call inherits from `AstBlock`, but it needs each expression passed. @@ -295,4 +290,4 @@ Error AstOptimizer::onCall(AstCall* node) { return kErrorOk; } -} // mathpresso namespace +} // {mathpresso} diff --git a/src/mathpresso/mpoptimizer_p.h b/src/mathpresso/mpoptimizer_p.h index 4778b0a..2b27eeb 100644 --- a/src/mathpresso/mpoptimizer_p.h +++ b/src/mathpresso/mpoptimizer_p.h @@ -13,24 +13,23 @@ namespace mathpresso { -// ============================================================================ -// [mpsl::AstOptimizer] -// ============================================================================ +// MathPresso - AstOptimizer +// ========================= struct AstOptimizer : public AstVisitor { MATHPRESSO_NONCOPYABLE(AstOptimizer) - // -------------------------------------------------------------------------- - // [Construction / Destruction] - // -------------------------------------------------------------------------- + // Members + // ------- + + ErrorReporter* _errorReporter; + + // Construction & Destruction + // -------------------------- AstOptimizer(AstBuilder* ast, ErrorReporter* errorReporter); virtual ~AstOptimizer(); - // -------------------------------------------------------------------------- - // [OnNode] - // -------------------------------------------------------------------------- - virtual Error onBlock(AstBlock* node); virtual Error onVarDecl(AstVarDecl* node); virtual Error onVar(AstVar* node); @@ -38,15 +37,9 @@ struct AstOptimizer : public AstVisitor { virtual Error onUnaryOp(AstUnaryOp* node); virtual Error onBinaryOp(AstBinaryOp* node); virtual Error onCall(AstCall* node); - - // -------------------------------------------------------------------------- - // [Members] - // -------------------------------------------------------------------------- - - ErrorReporter* _errorReporter; }; -} // mathpresso namespace +} // {mathpresso} // [Guard] #endif // _MATHPRESSO_MPOPTIMIZER_P_H diff --git a/src/mathpresso/mpparser.cpp b/src/mathpresso/mpparser.cpp index b3b8242..cefe706 100644 --- a/src/mathpresso/mpparser.cpp +++ b/src/mathpresso/mpparser.cpp @@ -12,21 +12,8 @@ namespace mathpresso { -// ============================================================================ -// [mathpresso::Parser - Syntax Error] -// ============================================================================ - -#define MATHPRESSO_PARSER_ERROR(_Token_, ...) \ - return _errorReporter->onError( \ - kErrorInvalidSyntax, static_cast((size_t)(_Token_.position())), __VA_ARGS__) - -#define MATHPRESSO_PARSER_WARNING(_Token_, ...) \ - _errorReporter->onWarning( \ - static_cast((size_t)(_Token_.position())), __VA_ARGS__) - -// ============================================================================ -// [mathpresso::AstNestedScope] -// ============================================================================ +// MathPresso - AstNestedScope +// =========================== //! \internal //! @@ -34,9 +21,13 @@ namespace mathpresso { struct AstNestedScope : public AstScope { MATHPRESSO_NONCOPYABLE(AstNestedScope) - // -------------------------------------------------------------------------- - // [Construction / Destruction] - // -------------------------------------------------------------------------- + // Members + // ------- + + Parser* _parser; + + // Construction & Destruction + // -------------------------- MATHPRESSO_INLINE AstNestedScope(Parser* parser) : AstScope(parser->_ast, parser->_currentScope, kAstScopeNested), @@ -51,17 +42,21 @@ struct AstNestedScope : public AstScope { _parser->_currentScope = p; p->_symbols.mergeToInvisibleSlot(this->_symbols); } +}; - // -------------------------------------------------------------------------- - // [Members] - // -------------------------------------------------------------------------- +// MathPresso - Parser Errors +// ========================== - Parser* _parser; -}; +#define MATHPRESSO_PARSER_ERROR(_Token_, ...) \ + return _errorReporter->onError( \ + kErrorInvalidSyntax, static_cast((size_t)(_Token_.position())), __VA_ARGS__) + +#define MATHPRESSO_PARSER_WARNING(_Token_, ...) \ + _errorReporter->onWarning( \ + static_cast((size_t)(_Token_.position())), __VA_ARGS__) -// ============================================================================ -// [mathpresso::Parser - Parse] -// ============================================================================ +// MathPresso - Parser +// =================== Error Parser::parseProgram(AstProgram* block) { for (;;) { @@ -681,4 +676,4 @@ Error Parser::parseCall(AstNode** pNodeOut) { } } -} // mathpresso namespace +} // {mathpresso} diff --git a/src/mathpresso/mpparser_p.h b/src/mathpresso/mpparser_p.h index 60d6aea..7497ce7 100644 --- a/src/mathpresso/mpparser_p.h +++ b/src/mathpresso/mpparser_p.h @@ -14,19 +14,14 @@ namespace mathpresso { -// ============================================================================ -// [Forward Declaration] -// ============================================================================ - struct AstBuilder; struct AstBlock; struct AstNode; struct AstProgram; struct AstVar; -// ============================================================================ -// [mathpresso::Parser] -// ============================================================================ +// MathPresso - Parser +// =================== struct Parser { MATHPRESSO_NONCOPYABLE(Parser) @@ -37,9 +32,17 @@ struct Parser { kEnableNestedBlock = 0x02 }; - // -------------------------------------------------------------------------- - // [Construction / Destruction] - // -------------------------------------------------------------------------- + // Members + // ------- + + AstBuilder* _ast; + ErrorReporter* _errorReporter; + + AstScope* _currentScope; + Tokenizer _tokenizer; + + // Construction & Destruction + // -------------------------- MATHPRESSO_INLINE Parser(AstBuilder* ast, ErrorReporter* errorReporter, const char* body, size_t size) : _ast(ast), @@ -48,15 +51,13 @@ struct Parser { _tokenizer(body, size) {} MATHPRESSO_INLINE ~Parser() {} - // -------------------------------------------------------------------------- - // [Accessors] - // -------------------------------------------------------------------------- + // Accessors + // --------- MATHPRESSO_INLINE AstScope* currentScope() const { return _currentScope; } - // -------------------------------------------------------------------------- - // [Parse] - // -------------------------------------------------------------------------- + // Parse + // ----- MATHPRESSO_NOAPI Error parseProgram(AstProgram* block); @@ -66,19 +67,9 @@ struct Parser { MATHPRESSO_NOAPI Error parseVariableDecl(AstBlock* block); MATHPRESSO_NOAPI Error parseExpression(AstNode** pNodeOut, bool isNested); MATHPRESSO_NOAPI Error parseCall(AstNode** pNodeOut); - - // -------------------------------------------------------------------------- - // [Members] - // -------------------------------------------------------------------------- - - AstBuilder* _ast; - ErrorReporter* _errorReporter; - - AstScope* _currentScope; - Tokenizer _tokenizer; }; -} // mathpresso namespace +} // {mathpresso} // [Guard] #endif // _MATHPRESSO_MPPARSER_P_H diff --git a/src/mathpresso/mpstrtod_p.h b/src/mathpresso/mpstrtod_p.h index 9ece1e4..f636cb5 100644 --- a/src/mathpresso/mpstrtod_p.h +++ b/src/mathpresso/mpstrtod_p.h @@ -27,9 +27,8 @@ namespace mathpresso { -// ============================================================================ -// [mathpresso::Parser] -// ============================================================================ +// MathPresso - String to Double +// ============================= struct StrToD { MATHPRESSO_NONCOPYABLE(StrToD) @@ -60,7 +59,7 @@ struct StrToD { #endif }; -} // mathpresso namespace +} // {mathpresso} // [Guard] #endif // _MATHPRESSO_MPSTRTOD_P_H diff --git a/src/mathpresso/mptokenizer.cpp b/src/mathpresso/mptokenizer.cpp index b869b2c..ac5ebda 100644 --- a/src/mathpresso/mptokenizer.cpp +++ b/src/mathpresso/mptokenizer.cpp @@ -14,9 +14,8 @@ namespace mathpresso { -// ============================================================================ -// [mathpresso::Tokenizer] -// ============================================================================ +// MathPresso - Tokenizer +// ====================== //! \internal //! @@ -170,9 +169,8 @@ uint32_t Tokenizer::next(Token* token) { const uint8_t* pStart = reinterpret_cast(_start); const uint8_t* pEnd = reinterpret_cast(_end); - // -------------------------------------------------------------------------- - // [Spaces] - // -------------------------------------------------------------------------- + // Spaces + // ------ _Repeat: for (;;) { @@ -190,9 +188,8 @@ uint32_t Tokenizer::next(Token* token) { // Save the first character of the token. pToken = p; - // -------------------------------------------------------------------------- - // [Number | Dot] - // -------------------------------------------------------------------------- + // Number | Dot + // ------------ if (c <= kTokenChar0x9 || c == kTokenCharDot) { // Parsing floating point is not that simple as it looks. To simplify the @@ -328,9 +325,8 @@ uint32_t Tokenizer::next(Token* token) { return kTokenNumber; } - // -------------------------------------------------------------------------- - // [Symbol | Keyword] - // -------------------------------------------------------------------------- + // Symbol | Keyword + // ---------------- else if (c <= kTokenCharSym) { // We always calculate hashCode during tokenization to improve performance. @@ -347,18 +343,16 @@ uint32_t Tokenizer::next(Token* token) { return token->setData((size_t)(pToken - pStart), size, hashCode, mpGetKeyword(pToken, size)); } - // -------------------------------------------------------------------------- - // [Single-Char] - // -------------------------------------------------------------------------- + // Single-Char + // ----------- else if (c <= kTokenCharSingleCharTokenEnd) { _p = reinterpret_cast(++p); return token->setData((size_t)(pToken - pStart), (size_t)(p - pToken), 0, c); } - // -------------------------------------------------------------------------- - // [Single-Char | Multi-Char] - // -------------------------------------------------------------------------- + // Single-Char | Multi-Char + // ------------------------ else if (c < kTokenCharSpc) { uint32_t c1 = (++p != pEnd) ? static_cast(p[0]) : static_cast(0); @@ -455,17 +449,15 @@ uint32_t Tokenizer::next(Token* token) { return token->setData((size_t)(pToken - pStart), (size_t)(p - pToken), 0, c); } - // -------------------------------------------------------------------------- - // [Invalid] - // -------------------------------------------------------------------------- + // Invalid + // ------- _Invalid: _p = reinterpret_cast(pToken); return token->setData((size_t)(pToken - pStart), (size_t)(p - pToken), 0, kTokenInvalid); - // -------------------------------------------------------------------------- - // [Comment] - // -------------------------------------------------------------------------- + // Comment + // ------- _Comment: for (;;) { @@ -477,13 +469,12 @@ uint32_t Tokenizer::next(Token* token) { goto _Repeat; } - // -------------------------------------------------------------------------- - // [EOI] - // -------------------------------------------------------------------------- + // End of Input + // ------------ _EndOfInput: _p = _end; return token->setData((size_t)(pToken - pStart), (size_t)(p - pToken), 0, kTokenEnd); } -} // mathpresso namespace +} // {mathpresso} diff --git a/src/mathpresso/mptokenizer_p.h b/src/mathpresso/mptokenizer_p.h index 00226be..d1488e1 100644 --- a/src/mathpresso/mptokenizer_p.h +++ b/src/mathpresso/mptokenizer_p.h @@ -14,9 +14,8 @@ namespace mathpresso { -// ============================================================================ -// [mathpresso::Token] -// ============================================================================ +// MathPress - Token Type +// ====================== //! \internal //! @@ -93,17 +92,29 @@ enum TokenType { kTokenEnd // }; -// ============================================================================ -// [mathpresso::Token] -// ============================================================================ +// MathPress - Token +// ================= //! \internal //! //! Token. struct Token { - // -------------------------------------------------------------------------- - // [Reset] - // -------------------------------------------------------------------------- + // Members + // ------- + + //! Token type. + uint32_t _tokenType; + //! Token hash-code (only if the token is symbol or keyword). + uint32_t _hashCode; + //! Token position from the beginning of the input. + size_t _position; + //! Token size. + size_t _size; + //! Token value (if the token is a number). + double _value; + + // Reset + // ----- MATHPRESSO_INLINE void reset() { _position = 0; @@ -112,9 +123,8 @@ struct Token { _tokenType = kTokenInvalid; } - // -------------------------------------------------------------------------- - // [Accessors] - // -------------------------------------------------------------------------- + // Accessors + // --------- inline uint32_t setData(size_t position, size_t size, uint32_t hashCode, uint32_t tokenType) { _position = position; @@ -137,28 +147,26 @@ struct Token { } inline double value() const noexcept { return _value; } - - // -------------------------------------------------------------------------- - // [Members] - // -------------------------------------------------------------------------- - - uint32_t _tokenType; //!< Token type. - uint32_t _hashCode; //!< Token hash-code (only if the token is symbol or keyword). - size_t _position; //!< Token position from the beginning of the input. - size_t _size; //!< Token size. - double _value; //!< Token value (if the token is a number). }; -// ============================================================================ -// [mathpresso::Tokenizer] -// ============================================================================ +// MathPresso - Tokenizer +// ====================== struct Tokenizer { MATHPRESSO_NONCOPYABLE(Tokenizer) - // -------------------------------------------------------------------------- - // [Construction / Destruction] - // -------------------------------------------------------------------------- + // Members + // ------- + + const char* _p; + const char* _start; + const char* _end; + + StrToD _strtod; + Token _token; + + // Construction & Destruction + // -------------------------- MATHPRESSO_INLINE Tokenizer(const char* str, size_t size) : _p(str), @@ -168,9 +176,8 @@ struct Tokenizer { _token.reset(); } - // -------------------------------------------------------------------------- - // [Ops] - // -------------------------------------------------------------------------- + // Operations + // ---------- //! Get the current token. uint32_t peek(Token* token); @@ -204,20 +211,9 @@ struct Tokenizer { consume(); return next(token); } - - // -------------------------------------------------------------------------- - // [Members] - // -------------------------------------------------------------------------- - - const char* _p; - const char* _start; - const char* _end; - - StrToD _strtod; - Token _token; }; -} // mathpresso namespace +} // {mathpresso} // [Guard] #endif // _MATHPRESSO_MPTOKENIZER_P_H diff --git a/test/mptest.cpp b/test/mptest.cpp index a63dd46..8d1f801 100644 --- a/test/mptest.cpp +++ b/test/mptest.cpp @@ -39,9 +39,8 @@ static inline void set_x87_mode(unsigned short mode) { } #endif -// ============================================================================ -// [DoubleBits] -// ============================================================================ +// Double Bits +// =========== // This is a copy-pase from `mpeval_p.h`, we can't include it here since it's // private. @@ -59,18 +58,16 @@ union DoubleBits { struct { unsigned int lo, hi; }; }; -// ============================================================================ -// [TestOption] -// ============================================================================ +// Test Option +// =========== struct TestOption { const char* name; unsigned int options; }; -// ============================================================================ -// [TestExpression] -// ============================================================================ +// Test Expression +// =============== struct TestExpression { const char* expression; @@ -78,9 +75,8 @@ struct TestExpression { double xyz[3]; }; -// ============================================================================ -// [TestLog] -// ============================================================================ +// Test Logger +// =========== struct TestOutputLog : public mathpresso::OutputLog { TestOutputLog() {} @@ -98,23 +94,27 @@ struct TestOutputLog : public mathpresso::OutputLog { } }; -// ============================================================================ -// [TestFunc] -// ============================================================================ +// Test Functions +// ============== static double custom1(double x) { return x; } static double custom2(double x, double y) { return x + y; } -// ============================================================================ -// [TestApp] -// ============================================================================ +// Test Application +// ================ // The reason for TestApp is that we want to replace all functions the // expression can use. struct TestApp { - // -------------------------------------------------------------------------- - // [Compatibility with the MathPresso Language] - // -------------------------------------------------------------------------- + // Members + // ------- + + int argc; + char** argv; + + + // Compatibility with the MathPresso Language + // ------------------------------------------ // Constants / Variables. double E, PI; @@ -139,19 +139,18 @@ struct TestApp { } inline double roundeven(double x) { return ::rint(x); } - // -------------------------------------------------------------------------- - // [TestApp] - // -------------------------------------------------------------------------- + // TestApp + // ------- TestApp(int argc, char* argv[]) - : E(2.7182818284590452354), + : argc(argc), + argv(argv), + E(2.7182818284590452354), PI(3.14159265358979323846), x(1.5), y(2.5), z(9.9), - big(4503599627370496.0), - argc(argc), - argv(argv) {} + big(4503599627370496.0) {} bool hasArg(const char* arg) { for (int i = 1; i < argc; i++) { @@ -365,6 +364,10 @@ struct TestApp { TEST_INLINE(trunc(-x)), TEST_INLINE(trunc(-y)), TEST_INLINE(trunc(-big)), + TEST_INLINE(trunc(0.11)), + TEST_INLINE(trunc(-0.11)), + TEST_INLINE(trunc(1232323232.11)), + TEST_INLINE(trunc(-1232323232.11)), TEST_INLINE(floor(x)), TEST_INLINE(floor(y)), @@ -507,19 +510,8 @@ struct TestApp { return failed ? 1 : 0; } - - // -------------------------------------------------------------------------- - // [Members] - // -------------------------------------------------------------------------- - - int argc; - char** argv; }; -// ============================================================================ -// [Main] -// ============================================================================ - int main(int argc, char* argv[]) { return TestApp(argc, argv).run(); }