Loading...
Searching...
No Matches
Tokens.h
Go to the documentation of this file.
// Copyright Contributors to the OpenVDB Project
// SPDX-License-Identifier: MPL-2.0
/// @file ast/Tokens.h
///
/// @authors Nick Avramoussis
///
/// @brief Various function and operator tokens used throughout the
/// AST and code generation
///
#ifndef OPENVDB_AX_AST_TOKENS_HAS_BEEN_INCLUDED
#define OPENVDB_AX_AST_TOKENS_HAS_BEEN_INCLUDED
#include "../Exceptions.h"
#include <openvdb/version.h>
#include <openvdb/Types.h>
#include <stdexcept>
namespace openvdb {
OPENVDB_USE_VERSION_NAMESPACE
namespace OPENVDB_VERSION_NAME {
namespace ax {
namespace ast {
namespace tokens {
enum CoreType
{
BOOL = 0,
CHAR,
INT16,
INT32,
INT64,
FLOAT,
DOUBLE,
//
VEC2I,
VEC2F,
VEC2D,
//
VEC3I,
VEC3F,
VEC3D,
//
VEC4I,
VEC4F,
VEC4D,
//
MAT3F,
MAT3D,
//
MAT4F,
MAT4D,
//
QUATF,
QUATD,
//
STRING,
UNKNOWN
};
inline CoreType tokenFromTypeString(const std::string& type)
{
if (type[0] == 'v') {
if (type == "vec2i") return VEC2I;
if (type == "vec2f") return VEC2F;
if (type == "vec2d") return VEC2D;
if (type == "vec3i") return VEC3I;
if (type == "vec3f") return VEC3F;
if (type == "vec3d") return VEC3D;
if (type == "vec4i") return VEC4I;
if (type == "vec4f") return VEC4F;
if (type == "vec4d") return VEC4D;
}
else if (type[0] == 'm') {
if (type == "mat3f") return MAT3F;
if (type == "mat3d") return MAT3D;
if (type == "mat4f") return MAT4F;
if (type == "mat4d") return MAT4D;
}
else if (type[0] == 'q') {
if (type == "quatf") return QUATF;
if (type == "quatd") return QUATD;
}
else if (type[0] == 'i') {
if (type == "int16") return INT16;
if (type == "int") return INT32;
if (type == "int32") return INT32;
if (type == "int64") return INT64;
}
else if (type == "bool") return BOOL;
else if (type == "char") return CHAR;
else if (type == "float") return FLOAT;
else if (type == "double") return DOUBLE;
else if (type == "string") return STRING;
// also handle vdb types that have different type strings to our tokens
// @todo These should probably be separated out. The executables currently
// use this function to guarantee conversion
if (type[0] == 'v') {
if (type == "vec2s") return VEC2F;
if (type == "vec3s") return VEC3F;
if (type == "vec4s") return VEC4F;
}
else if (type[0] == 'm') {
if (type == "mat3s") return MAT3F;
if (type == "mat4s") return MAT4F;
}
else if (type == "quats") return QUATF;
return UNKNOWN;
}
inline std::string typeStringFromToken(const CoreType type)
{
switch (type) {
case BOOL : return "bool";
case CHAR : return "char";
case INT16 : return "int16";
case INT32 : return "int32";
case INT64 : return "int64";
case FLOAT : return "float";
case DOUBLE : return "double";
case VEC2I : return "vec2i";
case VEC2F : return "vec2f";
case VEC2D : return "vec2d";
case VEC3I : return "vec3i";
case VEC3F : return "vec3f";
case VEC3D : return "vec3d";
case VEC4I : return "vec4i";
case VEC4F : return "vec4f";
case VEC4D : return "vec4d";
case MAT3F : return "mat3f";
case MAT3D : return "mat3d";
case MAT4F : return "mat4f";
case MAT4D : return "mat4d";
case QUATF : return "quatf";
case QUATD : return "quatd";
case STRING : return "string";
case UNKNOWN :
default :
return "unknown";
}
}
enum OperatorToken
{
////////////////////////////////////////////////////////////////
/// ARITHMETIC
////////////////////////////////////////////////////////////////
PLUS = 0,
MINUS,
MULTIPLY,
DIVIDE,
MODULO,
////////////////////////////////////////////////////////////////
/// LOGICAL
////////////////////////////////////////////////////////////////
AND,
OR,
NOT,
////////////////////////////////////////////////////////////////
/// RELATIONAL
////////////////////////////////////////////////////////////////
EQUALSEQUALS,
NOTEQUALS,
MORETHAN,
LESSTHAN,
MORETHANOREQUAL,
LESSTHANOREQUAL,
////////////////////////////////////////////////////////////////
/// BITWISE
////////////////////////////////////////////////////////////////
SHIFTLEFT,
SHIFTRIGHT,
BITAND,
BITOR,
BITXOR,
BITNOT,
////////////////////////////////////////////////////////////////
/// ASSIGNMENT
////////////////////////////////////////////////////////////////
EQUALS,
PLUSEQUALS,
MINUSEQUALS,
MULTIPLYEQUALS,
DIVIDEEQUALS,
MODULOEQUALS,
SHIFTLEFTEQUALS,
SHIFTRIGHTEQUALS,
BITANDEQUALS,
BITXOREQUALS,
BITOREQUALS
};
enum OperatorType
{
ARITHMETIC = 0,
LOGICAL,
RELATIONAL,
BITWISE,
ASSIGNMENT,
UNKNOWN_OPERATOR
};
inline OperatorType operatorType(const OperatorToken token)
{
const size_t idx = static_cast<size_t>(token);
if (idx <= static_cast<size_t>(MODULO)) return ARITHMETIC;
if (idx <= static_cast<size_t>(NOT)) return LOGICAL;
if (idx <= static_cast<size_t>(LESSTHANOREQUAL)) return RELATIONAL;
if (idx <= static_cast<size_t>(BITNOT)) return BITWISE;
if (idx <= static_cast<size_t>(BITOREQUALS)) return ASSIGNMENT;
return UNKNOWN_OPERATOR;
}
inline OperatorToken operatorTokenFromName(const std::string& name)
{
if (name == "+") return PLUS;
if (name == "-") return MINUS;
if (name == "*") return MULTIPLY;
if (name == "/") return DIVIDE;
if (name == "%") return MODULO;
if (name == "&&") return AND;
if (name == "||") return OR;
if (name == "!") return NOT;
if (name == "==") return EQUALSEQUALS;
if (name == "!=") return NOTEQUALS;
if (name == ">") return MORETHAN;
if (name == "<") return LESSTHAN;
if (name == ">=") return MORETHANOREQUAL;
if (name == "<=") return LESSTHANOREQUAL;
if (name == "<<") return SHIFTLEFT;
if (name == ">>") return SHIFTRIGHT;
if (name == "&") return BITAND;
if (name == "|") return BITOR;
if (name == "^") return BITXOR;
if (name == "~") return BITNOT;
if (name == "=") return EQUALS;
if (name == "+=") return PLUSEQUALS;
if (name == "-=") return MINUSEQUALS;
if (name == "*=") return MULTIPLYEQUALS;
if (name == "/=") return DIVIDEEQUALS;
if (name == "%=") return MODULOEQUALS;
if (name == "<<=") return SHIFTLEFTEQUALS;
if (name == ">>=") return SHIFTRIGHTEQUALS;
if (name == "&=") return BITANDEQUALS;
if (name == "^=") return BITXOREQUALS;
if (name == "|=") return BITOREQUALS;
OPENVDB_THROW(AXTokenError, "Unsupported op \"" + name + "\"");
}
inline std::string operatorNameFromToken(const OperatorToken token)
{
switch (token) {
case PLUS : return "+";
case MINUS : return "-";
case MULTIPLY : return "*";
case DIVIDE : return "/";
case MODULO : return "%";
case AND : return "&&";
case OR : return "||";
case NOT : return "!";
case EQUALSEQUALS : return "==";
case NOTEQUALS : return "!=";
case MORETHAN : return ">";
case LESSTHAN : return "<";
case MORETHANOREQUAL : return ">=";
case LESSTHANOREQUAL : return "<=";
case SHIFTLEFT : return "<<";
case SHIFTRIGHT : return ">>";
case BITAND : return "&";
case BITOR : return "|";
case BITXOR : return "^";
case BITNOT : return "~";
case EQUALS : return "=";
case PLUSEQUALS : return "+=";
case MINUSEQUALS : return "-=";
case MULTIPLYEQUALS : return "*=";
case DIVIDEEQUALS : return "/=";
case MODULOEQUALS : return "%=";
case SHIFTLEFTEQUALS : return "<<=";
case SHIFTRIGHTEQUALS : return ">>=";
case BITANDEQUALS : return "&=";
case BITXOREQUALS : return "^=";
case BITOREQUALS : return "|=";
default :
OPENVDB_THROW(AXTokenError, "Unsupported op");
}
}
enum LoopToken
{
FOR = 0,
DO,
WHILE
};
inline std::string loopNameFromToken(const LoopToken loop)
{
switch (loop) {
case FOR : return "for";
case DO : return "do";
case WHILE : return "while";
default :
OPENVDB_THROW(AXTokenError, "Unsupported loop");
}
}
enum KeywordToken
{
RETURN = 0,
BREAK,
CONTINUE
};
inline std::string keywordNameFromToken(const KeywordToken keyw)
{
switch (keyw) {
case RETURN : return "return";
case BREAK : return "break";
case CONTINUE : return "continue";
default :
OPENVDB_THROW(AXTokenError, "Unsupported keyword");
}
}
} // namespace tokens
} // namespace ast
} // namespace ax
} // namespace OPENVDB_VERSION_NAME
} // namespace openvdb
#endif // OPENVDB_AX_AST_TOKENS_HAS_BEEN_INCLUDED
Exceptions.h
OpenVDB AX Exceptions.
openvdb::v10_0::AXTokenError
Definition Exceptions.h:36
openvdb::v10_0::ax::ast::tokens
Definition Tokens.h:29
openvdb::v10_0::ax::ast::tokens::operatorNameFromToken
std::string operatorNameFromToken(const OperatorToken token)
Definition Tokens.h:257
openvdb::v10_0::ax::ast::tokens::OperatorToken
OperatorToken
Definition Tokens.h:151
openvdb::v10_0::ax::ast::tokens::NOT
@ NOT
Definition Tokens.h:165
openvdb::v10_0::ax::ast::tokens::BITOR
@ BITOR
Definition Tokens.h:181
openvdb::v10_0::ax::ast::tokens::DIVIDE
@ DIVIDE
Definition Tokens.h:158
openvdb::v10_0::ax::ast::tokens::LESSTHANOREQUAL
@ LESSTHANOREQUAL
Definition Tokens.h:174
openvdb::v10_0::ax::ast::tokens::SHIFTRIGHT
@ SHIFTRIGHT
Definition Tokens.h:179
openvdb::v10_0::ax::ast::tokens::BITNOT
@ BITNOT
Definition Tokens.h:183
openvdb::v10_0::ax::ast::tokens::MORETHANOREQUAL
@ MORETHANOREQUAL
Definition Tokens.h:173
openvdb::v10_0::ax::ast::tokens::MINUSEQUALS
@ MINUSEQUALS
Definition Tokens.h:189
openvdb::v10_0::ax::ast::tokens::SHIFTLEFTEQUALS
@ SHIFTLEFTEQUALS
Definition Tokens.h:193
openvdb::v10_0::ax::ast::tokens::BITOREQUALS
@ BITOREQUALS
Definition Tokens.h:197
openvdb::v10_0::ax::ast::tokens::BITXOREQUALS
@ BITXOREQUALS
Definition Tokens.h:196
openvdb::v10_0::ax::ast::tokens::MODULOEQUALS
@ MODULOEQUALS
Definition Tokens.h:192
openvdb::v10_0::ax::ast::tokens::PLUSEQUALS
@ PLUSEQUALS
Definition Tokens.h:188
openvdb::v10_0::ax::ast::tokens::EQUALSEQUALS
@ EQUALSEQUALS
RELATIONAL.
Definition Tokens.h:169
openvdb::v10_0::ax::ast::tokens::BITXOR
@ BITXOR
Definition Tokens.h:182
openvdb::v10_0::ax::ast::tokens::BITAND
@ BITAND
Definition Tokens.h:180
openvdb::v10_0::ax::ast::tokens::AND
@ AND
LOGICAL.
Definition Tokens.h:163
openvdb::v10_0::ax::ast::tokens::PLUS
@ PLUS
ARITHMETIC.
Definition Tokens.h:155
openvdb::v10_0::ax::ast::tokens::LESSTHAN
@ LESSTHAN
Definition Tokens.h:172
openvdb::v10_0::ax::ast::tokens::DIVIDEEQUALS
@ DIVIDEEQUALS
Definition Tokens.h:191
openvdb::v10_0::ax::ast::tokens::EQUALS
@ EQUALS
ASSIGNMENT.
Definition Tokens.h:187
openvdb::v10_0::ax::ast::tokens::OR
@ OR
Definition Tokens.h:164
openvdb::v10_0::ax::ast::tokens::MODULO
@ MODULO
Definition Tokens.h:159
openvdb::v10_0::ax::ast::tokens::MORETHAN
@ MORETHAN
Definition Tokens.h:171
openvdb::v10_0::ax::ast::tokens::MULTIPLYEQUALS
@ MULTIPLYEQUALS
Definition Tokens.h:190
openvdb::v10_0::ax::ast::tokens::BITANDEQUALS
@ BITANDEQUALS
Definition Tokens.h:195
openvdb::v10_0::ax::ast::tokens::NOTEQUALS
@ NOTEQUALS
Definition Tokens.h:170
openvdb::v10_0::ax::ast::tokens::SHIFTRIGHTEQUALS
@ SHIFTRIGHTEQUALS
Definition Tokens.h:194
openvdb::v10_0::ax::ast::tokens::MULTIPLY
@ MULTIPLY
Definition Tokens.h:157
openvdb::v10_0::ax::ast::tokens::SHIFTLEFT
@ SHIFTLEFT
BITWISE.
Definition Tokens.h:178
openvdb::v10_0::ax::ast::tokens::MINUS
@ MINUS
Definition Tokens.h:156
openvdb::v10_0::ax::ast::tokens::typeStringFromToken
std::string typeStringFromToken(const CoreType type)
Definition Tokens.h:118
openvdb::v10_0::ax::ast::tokens::loopNameFromToken
std::string loopNameFromToken(const LoopToken loop)
Definition Tokens.h:303
openvdb::v10_0::ax::ast::tokens::operatorTokenFromName
OperatorToken operatorTokenFromName(const std::string &name)
Definition Tokens.h:221
openvdb::v10_0::ax::ast::tokens::operatorType
OperatorType operatorType(const OperatorToken token)
Definition Tokens.h:210
openvdb::v10_0::ax::ast::tokens::KeywordToken
KeywordToken
Definition Tokens.h:315
openvdb::v10_0::ax::ast::tokens::CONTINUE
@ CONTINUE
Definition Tokens.h:318
openvdb::v10_0::ax::ast::tokens::RETURN
@ RETURN
Definition Tokens.h:316
openvdb::v10_0::ax::ast::tokens::BREAK
@ BREAK
Definition Tokens.h:317
openvdb::v10_0::ax::ast::tokens::OperatorType
OperatorType
Definition Tokens.h:201
openvdb::v10_0::ax::ast::tokens::ASSIGNMENT
@ ASSIGNMENT
Definition Tokens.h:206
openvdb::v10_0::ax::ast::tokens::LOGICAL
@ LOGICAL
Definition Tokens.h:203
openvdb::v10_0::ax::ast::tokens::BITWISE
@ BITWISE
Definition Tokens.h:205
openvdb::v10_0::ax::ast::tokens::ARITHMETIC
@ ARITHMETIC
Definition Tokens.h:202
openvdb::v10_0::ax::ast::tokens::RELATIONAL
@ RELATIONAL
Definition Tokens.h:204
openvdb::v10_0::ax::ast::tokens::UNKNOWN_OPERATOR
@ UNKNOWN_OPERATOR
Definition Tokens.h:207
openvdb::v10_0::ax::ast::tokens::LoopToken
LoopToken
Definition Tokens.h:297
openvdb::v10_0::ax::ast::tokens::WHILE
@ WHILE
Definition Tokens.h:300
openvdb::v10_0::ax::ast::tokens::FOR
@ FOR
Definition Tokens.h:298
openvdb::v10_0::ax::ast::tokens::DO
@ DO
Definition Tokens.h:299
openvdb::v10_0::ax::ast::tokens::tokenFromTypeString
CoreType tokenFromTypeString(const std::string &type)
Definition Tokens.h:66
openvdb::v10_0::ax::ast::tokens::keywordNameFromToken
std::string keywordNameFromToken(const KeywordToken keyw)
Definition Tokens.h:321
openvdb::v10_0::ax::ast::tokens::CoreType
CoreType
Definition Tokens.h:32
openvdb::v10_0::ax::ast::tokens::VEC3D
@ VEC3D
Definition Tokens.h:47
openvdb::v10_0::ax::ast::tokens::MAT3F
@ MAT3F
Definition Tokens.h:53
openvdb::v10_0::ax::ast::tokens::MAT3D
@ MAT3D
Definition Tokens.h:54
openvdb::v10_0::ax::ast::tokens::MAT4D
@ MAT4D
Definition Tokens.h:57
openvdb::v10_0::ax::ast::tokens::DOUBLE
@ DOUBLE
Definition Tokens.h:39
openvdb::v10_0::ax::ast::tokens::INT64
@ INT64
Definition Tokens.h:37
openvdb::v10_0::ax::ast::tokens::CHAR
@ CHAR
Definition Tokens.h:34
openvdb::v10_0::ax::ast::tokens::VEC3F
@ VEC3F
Definition Tokens.h:46
openvdb::v10_0::ax::ast::tokens::VEC2I
@ VEC2I
Definition Tokens.h:41
openvdb::v10_0::ax::ast::tokens::UNKNOWN
@ UNKNOWN
Definition Tokens.h:63
openvdb::v10_0::ax::ast::tokens::MAT4F
@ MAT4F
Definition Tokens.h:56
openvdb::v10_0::ax::ast::tokens::VEC4F
@ VEC4F
Definition Tokens.h:50
openvdb::v10_0::ax::ast::tokens::VEC4I
@ VEC4I
Definition Tokens.h:49
openvdb::v10_0::ax::ast::tokens::FLOAT
@ FLOAT
Definition Tokens.h:38
openvdb::v10_0::ax::ast::tokens::VEC2D
@ VEC2D
Definition Tokens.h:43
openvdb::v10_0::ax::ast::tokens::VEC3I
@ VEC3I
Definition Tokens.h:45
openvdb::v10_0::ax::ast::tokens::VEC4D
@ VEC4D
Definition Tokens.h:51
openvdb::v10_0::ax::ast::tokens::INT32
@ INT32
Definition Tokens.h:36
openvdb::v10_0::ax::ast::tokens::INT16
@ INT16
Definition Tokens.h:35
openvdb::v10_0::ax::ast::tokens::QUATF
@ QUATF
Definition Tokens.h:59
openvdb::v10_0::ax::ast::tokens::QUATD
@ QUATD
Definition Tokens.h:60
openvdb::v10_0::ax::ast::tokens::BOOL
@ BOOL
Definition Tokens.h:33
openvdb::v10_0::ax::ast::tokens::VEC2F
@ VEC2F
Definition Tokens.h:42
openvdb::v10_0::ax::ast::tokens::STRING
@ STRING
Definition Tokens.h:62
openvdb
Definition Exceptions.h:13
OPENVDB_THROW
#define OPENVDB_THROW(exception, message)
Definition Exceptions.h:74
Types.h
OPENVDB_VERSION_NAME
#define OPENVDB_VERSION_NAME
The version namespace name for this library version.
Definition version.h.in:121
OPENVDB_USE_VERSION_NAMESPACE
#define OPENVDB_USE_VERSION_NAMESPACE
Definition version.h.in:212