[DEV] start lexing of C++
This commit is contained in:
parent
cb5a9248d8
commit
7ea86b08f8
BIN
__pycache__/lutin_estyle-test.cpython-36.pyc
Normal file
BIN
__pycache__/lutin_estyle-test.cpython-36.pyc
Normal file
Binary file not shown.
BIN
__pycache__/lutin_estyle.cpython-36.pyc
Normal file
BIN
__pycache__/lutin_estyle.cpython-36.pyc
Normal file
Binary file not shown.
1
author.txt
Normal file
1
author.txt
Normal file
@ -0,0 +1 @@
|
||||
Edouard DUPIN <yui.heero@gmail.com>
|
131
estyle/Generator.cpp
Normal file
131
estyle/Generator.cpp
Normal file
@ -0,0 +1,131 @@
|
||||
/**
|
||||
* @author Edouard DUPIN
|
||||
* @copyright 2017, Edouard DUPIN, all right reserved
|
||||
* @license MPL-2 (see license file)
|
||||
*/
|
||||
#include <estyle/Generator.hpp>
|
||||
#include <estyle/debug.hpp>
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
estyle::Generator::Generator():
|
||||
propertyEndOfLine(this, "end-of-line", true, "true: End with \\n, false \\r\\n"),
|
||||
propertyDoxygenOneLine(this, "doxygen-1-line-type", true, "true: single line doxygen comment is done with '//!', false '///'"),
|
||||
propertyDoxygenMultipleLine(this, "doxygen-N-line-type", 0, "0: /** */ ...") {
|
||||
/*
|
||||
/ **
|
||||
*
|
||||
* /
|
||||
*/
|
||||
propertyDoxygenMultipleLine.add(0, "normal");
|
||||
/*
|
||||
/ *!
|
||||
*
|
||||
* /
|
||||
*/
|
||||
propertyDoxygenMultipleLine.add(1, "normal-exclamation");
|
||||
/*
|
||||
/ **
|
||||
|
||||
* /
|
||||
*/
|
||||
propertyDoxygenMultipleLine.add(2, "no-star");
|
||||
/*
|
||||
/ *!
|
||||
|
||||
* /
|
||||
*/
|
||||
propertyDoxygenMultipleLine.add(3, "no-star-exclamation");
|
||||
/*
|
||||
///
|
||||
///
|
||||
///
|
||||
*/
|
||||
propertyDoxygenMultipleLine.add(4, "single-line");
|
||||
/*
|
||||
//!
|
||||
//!
|
||||
//!
|
||||
*/
|
||||
propertyDoxygenMultipleLine.add(5, "single-line-exclamation");
|
||||
}
|
||||
|
||||
estyle::Generator::~Generator() {
|
||||
|
||||
}
|
||||
|
||||
etk::String estyle::Generator::getEndOfLine() {
|
||||
if (propertyEndOfLine.get() == true) {
|
||||
return "\n";
|
||||
}
|
||||
return "\r\n";
|
||||
}
|
||||
|
||||
etk::String estyle::Generator::getDoxygenOneLine(int32_t _indentation) {
|
||||
if (propertyDoxygenOneLine.get() == true) {
|
||||
return "//!";
|
||||
}
|
||||
return "///";
|
||||
}
|
||||
etk::String estyle::Generator::getDoxygenNLine(int32_t _indentation, const etk::String& _data) {
|
||||
if (propertyDoxygenMultipleLine.get() == 0) {
|
||||
|
||||
|
||||
}
|
||||
etk::String out;
|
||||
out += "/**";
|
||||
out += _data;
|
||||
out += "*/";
|
||||
return out;
|
||||
}
|
||||
|
||||
|
||||
etk::String estyle::Generator::process(const etk::String& _code) {
|
||||
estyle::Lexer lexer(_code);
|
||||
etk::String out;
|
||||
int32_t indentation = 0;
|
||||
for (size_t iii = 0; iii < lexer.size(); ++iii) {
|
||||
enum estyle::lexer::tocken elem = lexer.getTocken(iii);
|
||||
if (elem == estyle::lexer::RESERVED_COMMENT_1_LINE) {
|
||||
out += "//";
|
||||
out += lexer.getData(iii);
|
||||
out += getEndOfLine();
|
||||
continue;
|
||||
}
|
||||
if (elem == estyle::lexer::RESERVED_DOCUMENTATION_1_LINE) {
|
||||
out += getDoxygenOneLine(indentation);
|
||||
out += lexer.getData(iii);
|
||||
out += getEndOfLine();
|
||||
continue;
|
||||
}
|
||||
if (elem == estyle::lexer::RESERVED_COMMENT_N_LINE) {
|
||||
out += "/*";
|
||||
out += lexer.getData(iii);
|
||||
out += "*/";
|
||||
if (iii+1 < lexer.size()) {
|
||||
if (lexer.getTocken(iii+1) == estyle::lexer::RESERVED_NEW_LINE) {
|
||||
out += getEndOfLine();
|
||||
++iii;
|
||||
}
|
||||
}
|
||||
continue;
|
||||
}
|
||||
if (elem == estyle::lexer::RESERVED_DOCUMENTATION_1_LINE) {
|
||||
out += getDoxygenNLine(indentation, lexer.getData(iii));
|
||||
out += getEndOfLine();
|
||||
// TODO : Some mode can create error like /** */ becaming /// ...
|
||||
if (iii+1 < lexer.size()) {
|
||||
if (lexer.getTocken(iii+1) == estyle::lexer::RESERVED_NEW_LINE) {
|
||||
out += getEndOfLine();
|
||||
++iii;
|
||||
}
|
||||
}
|
||||
continue;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
return out;
|
||||
};
|
32
estyle/Generator.hpp
Normal file
32
estyle/Generator.hpp
Normal file
@ -0,0 +1,32 @@
|
||||
/**
|
||||
* @author Edouard DUPIN
|
||||
* @copyright 2017, Edouard DUPIN, all right reserved
|
||||
* @license MPL-2 (see license file)
|
||||
*/
|
||||
#pragma once
|
||||
|
||||
#include <etk/types.hpp>
|
||||
#include <estyle/lexer/Lexer.hpp>
|
||||
#include <ememory/memory.hpp>
|
||||
#include <eproperty/Value.hpp>
|
||||
#include <eproperty/List.hpp>
|
||||
#include <eproperty/Interface.hpp>
|
||||
|
||||
namespace estyle {
|
||||
class Generator : public eproperty::Interface {
|
||||
public:
|
||||
Generator();
|
||||
~Generator();
|
||||
protected:
|
||||
eproperty::Value<bool> propertyEndOfLine;
|
||||
eproperty::Value<bool> propertyDoxygenOneLine;
|
||||
eproperty::List<int32_t> propertyDoxygenMultipleLine;
|
||||
public:
|
||||
etk::String process(const etk::String& _code);
|
||||
private:
|
||||
etk::String getEndOfLine();
|
||||
etk::String getDoxygenOneLine(int32_t _indentation);
|
||||
etk::String getDoxygenNLine(int32_t _indentation, const etk::String& _data);
|
||||
};
|
||||
}
|
||||
|
11
estyle/debug.cpp
Normal file
11
estyle/debug.cpp
Normal file
@ -0,0 +1,11 @@
|
||||
/**
|
||||
* @author Edouard DUPIN
|
||||
* @copyright 2017, Edouard DUPIN, all right reserved
|
||||
* @license MPL-2 (see license file)
|
||||
*/
|
||||
#include "debug.hpp"
|
||||
|
||||
int32_t estyle::getLogId() {
|
||||
static int32_t g_val = elog::registerInstance("lite-JS");
|
||||
return g_val;
|
||||
}
|
41
estyle/debug.hpp
Normal file
41
estyle/debug.hpp
Normal file
@ -0,0 +1,41 @@
|
||||
/**
|
||||
* @author Edouard DUPIN
|
||||
* @copyright 2017, Edouard DUPIN, all right reserved
|
||||
* @license MPL-2 (see license file)
|
||||
*/
|
||||
#pragma once
|
||||
|
||||
#include <elog/log.hpp>
|
||||
|
||||
namespace estyle {
|
||||
int32_t getLogId();
|
||||
};
|
||||
// TODO : Review this problem of multiple intanciation of "etk::Stringbuf sb"
|
||||
#define ESTYLE_BASE(info,data) ELOG_BASE(estyle::getLogId(),info,data)
|
||||
|
||||
#define ESTYLE_PRINT(data) ESTYLE_BASE(-1, data)
|
||||
#define ESTYLE_CRITICAL(data) ESTYLE_BASE(1, data)
|
||||
#define ESTYLE_ERROR(data) ESTYLE_BASE(2, data)
|
||||
#define ESTYLE_WARNING(data) ESTYLE_BASE(3, data)
|
||||
#ifdef DEBUG
|
||||
#define ESTYLE_INFO(data) ESTYLE_BASE(4, data)
|
||||
#define ESTYLE_DEBUG(data) ESTYLE_BASE(5, data)
|
||||
#define ESTYLE_VERBOSE(data) ESTYLE_BASE(6, data)
|
||||
#define ESTYLE_TODO(data) ESTYLE_BASE(4, "TODO : " << data)
|
||||
#else
|
||||
#define ESTYLE_INFO(data) do { } while(false)
|
||||
#define ESTYLE_DEBUG(data) do { } while(false)
|
||||
#define ESTYLE_VERBOSE(data) do { } while(false)
|
||||
#define ESTYLE_TODO(data) do { } while(false)
|
||||
#endif
|
||||
|
||||
#define ESTYLE_ASSERT(cond,data) \
|
||||
do { \
|
||||
if (!(cond)) { \
|
||||
ESTYLE_CRITICAL(data); \
|
||||
assert(!#cond); \
|
||||
} \
|
||||
} while (0)
|
||||
|
||||
|
||||
|
55
estyle/estyle.cpp
Normal file
55
estyle/estyle.cpp
Normal file
@ -0,0 +1,55 @@
|
||||
/**
|
||||
* @author Edouard DUPIN
|
||||
* @copyright 2017, Edouard DUPIN, all right reserved
|
||||
* @license MPL-2 (see license file)
|
||||
*/
|
||||
#include <estyle/estyle.hpp>
|
||||
#include <estyle/debug.hpp>
|
||||
|
||||
static int32_t nbTimeInit = 0;
|
||||
|
||||
void estyle::unInit() {
|
||||
if (nbTimeInit > 1) {
|
||||
nbTimeInit--;
|
||||
// not the time to un-init
|
||||
return;
|
||||
}
|
||||
nbTimeInit--;
|
||||
if (nbTimeInit < 0) {
|
||||
ESTYLE_ERROR("estyle system un-init More un-init than init ...");
|
||||
nbTimeInit = 0;
|
||||
return;
|
||||
}
|
||||
ESTYLE_INFO("estyle system un-init (BEGIN)");
|
||||
|
||||
ESTYLE_INFO("estyle system un-init (END)");
|
||||
}
|
||||
|
||||
void estyle::init(int _argc, const char** _argv) {
|
||||
if (nbTimeInit > 0) {
|
||||
nbTimeInit++;
|
||||
// already init
|
||||
return;
|
||||
}
|
||||
nbTimeInit++;
|
||||
ESTYLE_INFO("estyle system init (BEGIN) ");
|
||||
for (int32_t iii=0; iii<_argc ; ++iii) {
|
||||
etk::String data = _argv[iii];
|
||||
if ( data == "-h"
|
||||
|| data == "--help") {
|
||||
ESTYLE_PRINT("estyle - help : ");
|
||||
if (_argc >= 1) {
|
||||
ESTYLE_PRINT(" " << _argv[0] << " [options]");
|
||||
}
|
||||
/*
|
||||
ESTYLE_PRINT(" --estyle-log-lib=name:X Set a library specific level:");
|
||||
ESTYLE_PRINT(" name Name of the library");
|
||||
ESTYLE_PRINT(" X Log level to set [0..6]");
|
||||
*/
|
||||
ESTYLE_PRINT(" -h/--help: this help");
|
||||
} else if (data.startWith("--estyle") == true) {
|
||||
ESTYLE_ERROR("Can not parse the argument : '" << data << "'");
|
||||
}
|
||||
}
|
||||
ESTYLE_INFO("estyle system init (END)");
|
||||
}
|
22
estyle/estyle.hpp
Normal file
22
estyle/estyle.hpp
Normal file
@ -0,0 +1,22 @@
|
||||
/**
|
||||
* @author Edouard DUPIN
|
||||
* @copyright 2017, Edouard DUPIN, all right reserved
|
||||
* @license MPL-2 (see license file)
|
||||
*/
|
||||
#pragma once
|
||||
|
||||
#include <etk/types.hpp>
|
||||
|
||||
namespace estyle {
|
||||
/**
|
||||
* @brief Initialize estyle
|
||||
* @param[in] _argc Number of argument list
|
||||
* @param[in] _argv List of arguments
|
||||
*/
|
||||
void init(int _argc, const char** _argv);
|
||||
/**
|
||||
* @brief Un-Initialize kikou
|
||||
*/
|
||||
void unInit();
|
||||
}
|
||||
|
606
estyle/lexer/Lexer.cpp
Normal file
606
estyle/lexer/Lexer.cpp
Normal file
@ -0,0 +1,606 @@
|
||||
/**
|
||||
* @author Edouard DUPIN
|
||||
* @copyright 2017, Edouard DUPIN, all right reserved
|
||||
* @license MPL-2 (see license file)
|
||||
*/
|
||||
#include <estyle/lexer/Lexer.hpp>
|
||||
#include <estyle/debug.hpp>
|
||||
|
||||
|
||||
static bool isWhitespace(char _char) {
|
||||
return _char == ' '
|
||||
|| _char == '\t';
|
||||
}
|
||||
|
||||
static bool isNumeric(char _char) {
|
||||
return _char >= '0'
|
||||
&& _char <= '9';
|
||||
}
|
||||
static bool isNumber(const etk::String& _string) {
|
||||
for (size_t i=0;i<_string.size();i++) {
|
||||
if (!isNumeric(_string[i])) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
static bool isHexadecimal(char _char) {
|
||||
return (_char >= '0' && _char <= '9')
|
||||
|| (_char >= 'a' && _char <= 'f')
|
||||
|| (_char >= 'A' && _char <= 'F');
|
||||
}
|
||||
static bool isAlpha(char _char) {
|
||||
return (_char >= 'a' && _char <= 'z')
|
||||
|| (_char >= 'A' && _char <= 'Z')
|
||||
|| _char == '_';
|
||||
}
|
||||
|
||||
|
||||
void estyle::Lexer::getChar(size_t _iii, char& _currentChar, char& _nextChar) {
|
||||
if (_iii < m_stream.size()) {
|
||||
_currentChar = m_stream[_iii];
|
||||
if (_iii+1 < m_stream.size()) {
|
||||
_nextChar = m_stream[_iii+1];
|
||||
} else {
|
||||
_nextChar = 0;
|
||||
}
|
||||
ESTYLE_DEBUG(" parse '" << etk::String(_currentChar) << "'");
|
||||
return;
|
||||
}
|
||||
_currentChar = 0;
|
||||
_nextChar = 0;
|
||||
}
|
||||
|
||||
void estyle::Lexer::parse() {
|
||||
int tokenStart;
|
||||
int tokenEnd;
|
||||
char currentChar = 0;
|
||||
char nextChar = 0;
|
||||
for (size_t iii=0; iii<m_stream.size(); ++iii) {
|
||||
|
||||
getChar(iii, currentChar, nextChar);
|
||||
while (isWhitespace(currentChar) == true) {
|
||||
++iii;
|
||||
getChar(iii, currentChar, nextChar);
|
||||
}
|
||||
if ( ( currentChar == '\n'
|
||||
&& nextChar == '\r')
|
||||
|| ( currentChar == '\r'
|
||||
&& nextChar == '\n') ) {
|
||||
tokenStart = iii;
|
||||
++iii;
|
||||
m_list.pushBack(estyle::LexerElement(estyle::lexer::RESERVED_NEW_LINE, tokenStart, iii));
|
||||
continue;
|
||||
}
|
||||
if ( currentChar == '\n'
|
||||
|| nextChar == '\r' ) {
|
||||
tokenStart = iii;
|
||||
m_list.pushBack(estyle::LexerElement(estyle::lexer::RESERVED_NEW_LINE, tokenStart, iii));
|
||||
continue;
|
||||
}
|
||||
if ( currentChar == '/'
|
||||
&& nextChar == '/') {
|
||||
enum estyle::lexer::tocken tockenElement = estyle::lexer::RESERVED_COMMENT_1_LINE;
|
||||
// remove "//"
|
||||
iii += 2;
|
||||
tokenStart = iii;
|
||||
getChar(iii, currentChar, nextChar);
|
||||
// Check if we have "//!" or "///" ==> doxygen comment
|
||||
if ( currentChar == '!'
|
||||
|| currentChar == '/') {
|
||||
iii++;
|
||||
tokenStart++;
|
||||
getChar(iii, currentChar, nextChar);
|
||||
tockenElement = estyle::lexer::RESERVED_DOCUMENTATION_1_LINE;
|
||||
}
|
||||
etk::String data;
|
||||
while ( currentChar != 0
|
||||
&& currentChar != '\n') {
|
||||
iii++;
|
||||
getChar(iii, currentChar, nextChar);
|
||||
}
|
||||
m_list.pushBack(estyle::LexerElement(tockenElement, tokenStart, iii));
|
||||
iii++;
|
||||
getChar(iii, currentChar, nextChar);
|
||||
continue;
|
||||
}
|
||||
// block comments
|
||||
if ( currentChar == '/'
|
||||
&& nextChar == '*') {
|
||||
enum estyle::lexer::tocken tockenElement = estyle::lexer::RESERVED_COMMENT_N_LINE;
|
||||
// remove "/*"
|
||||
iii+=2;
|
||||
getChar(iii, currentChar, nextChar);
|
||||
tokenStart = iii;
|
||||
// Check if we have "/**" or "/*!" ==> doxygen comment
|
||||
if ( currentChar == '*'
|
||||
|| currentChar == '!') {
|
||||
iii++;
|
||||
tokenStart = iii;
|
||||
getChar(iii, currentChar, nextChar);
|
||||
tockenElement = estyle::lexer::RESERVED_DOCUMENTATION_N_LINE;
|
||||
}
|
||||
while ( currentChar != 0
|
||||
&& ( currentChar != '*'
|
||||
|| nextChar != '/' ) ) {
|
||||
iii++;
|
||||
getChar(iii, currentChar, nextChar);
|
||||
}
|
||||
m_list.pushBack(estyle::LexerElement(tockenElement, tokenStart, iii));
|
||||
iii++;
|
||||
continue;
|
||||
}
|
||||
// tokens
|
||||
if (isAlpha(currentChar)) {
|
||||
tokenStart = iii;
|
||||
etk::String idData;
|
||||
// Parse reserved elements
|
||||
while ( isAlpha(currentChar)
|
||||
|| isNumeric(currentChar)) {
|
||||
idData += currentChar;
|
||||
iii++;
|
||||
getChar(iii, currentChar, nextChar);
|
||||
}
|
||||
enum estyle::lexer::tocken tockenElement = estyle::lexer::ID;
|
||||
if (idData == "if") {
|
||||
tockenElement = estyle::lexer::RESERVED_IF;
|
||||
} else if (idData == "else") {
|
||||
tockenElement = estyle::lexer::RESERVED_ELSE;
|
||||
} else if (idData == "do") {
|
||||
tockenElement = estyle::lexer::RESERVED_DO;
|
||||
} else if (idData == "while") {
|
||||
tockenElement = estyle::lexer::RESERVED_WHILE;
|
||||
} else if (idData == "for") {
|
||||
tockenElement = estyle::lexer::RESERVED_FOR;
|
||||
} else if (idData == "break") {
|
||||
tockenElement = estyle::lexer::RESERVED_BREAK;
|
||||
} else if (idData == "continue") {
|
||||
tockenElement = estyle::lexer::RESERVED_CONTINUE;
|
||||
} else if (idData == "return") {
|
||||
tockenElement = estyle::lexer::RESERVED_RETURN;
|
||||
} else if (idData == "switch") {
|
||||
tockenElement = estyle::lexer::RESERVED_SWITCH;
|
||||
} else if (idData == "case") {
|
||||
tockenElement = estyle::lexer::RESERVED_CASE;
|
||||
} else if (idData == "delete") {
|
||||
tockenElement = estyle::lexer::RESERVED_DELETE;
|
||||
} else if (idData == "new") {
|
||||
tockenElement = estyle::lexer::RESERVED_NEW;
|
||||
} else if (idData == "private") {
|
||||
tockenElement = estyle::lexer::RESERVED_PRIVATE;
|
||||
} else if (idData == "protected") {
|
||||
tockenElement = estyle::lexer::RESERVED_PROTECTED;
|
||||
} else if (idData == "public") {
|
||||
tockenElement = estyle::lexer::RESERVED_PUBLIC;
|
||||
} else if (idData == "int8_t") {
|
||||
tockenElement = estyle::lexer::BASIC_TYPE_INTEGER_008;
|
||||
} else if (idData == "uint8_t") {
|
||||
tockenElement = estyle::lexer::BASIC_TYPE_INTEGER_008_UNSIGNED;
|
||||
} else if (idData == "int16_t") {
|
||||
tockenElement = estyle::lexer::BASIC_TYPE_INTEGER_016;
|
||||
} else if (idData == "uint16_t") {
|
||||
tockenElement = estyle::lexer::BASIC_TYPE_INTEGER_016_UNSIGNED;
|
||||
} else if (idData == "int32_t") {
|
||||
tockenElement = estyle::lexer::BASIC_TYPE_INTEGER_032;
|
||||
} else if (idData == "uint32_t") {
|
||||
tockenElement = estyle::lexer::BASIC_TYPE_INTEGER_032_UNSIGNED;
|
||||
} else if (idData == "int64_t") {
|
||||
tockenElement = estyle::lexer::BASIC_TYPE_INTEGER_064;
|
||||
} else if (idData == "uint64_t") {
|
||||
tockenElement = estyle::lexer::BASIC_TYPE_INTEGER_064_UNSIGNED;
|
||||
} else if (idData == "int128_t") {
|
||||
tockenElement = estyle::lexer::BASIC_TYPE_INTEGER_128;
|
||||
} else if (idData == "uint128_t") {
|
||||
tockenElement = estyle::lexer::BASIC_TYPE_INTEGER_128_UNSIGNED;
|
||||
} else if (idData == "float") {
|
||||
tockenElement = estyle::lexer::BASIC_TYPE_FLOAT_32;
|
||||
} else if (idData == "double") {
|
||||
tockenElement = estyle::lexer::BASIC_TYPE_FLOAT_64;
|
||||
} else if (idData == "triple") {
|
||||
tockenElement = estyle::lexer::BASIC_TYPE_FLOAT_96;
|
||||
} else if (idData == "bool") {
|
||||
tockenElement = estyle::lexer::BASIC_TYPE_BOOLEAN;
|
||||
} else if (idData == "std::string") {
|
||||
tockenElement = estyle::lexer::BASIC_TYPE_STD_STRING;
|
||||
} else if ( idData == "std::nullptr"
|
||||
|| idData == "nullptr") {
|
||||
tockenElement = estyle::lexer::BASIC_TYPE_STD_NULLPTR;
|
||||
} else if (idData == "void") {
|
||||
tockenElement = estyle::lexer::BASIC_TYPE_VOID;
|
||||
|
||||
} else if (idData == "const") {
|
||||
tockenElement = estyle::lexer::RESERVED_CONST;
|
||||
} else if (idData == "static") {
|
||||
tockenElement = estyle::lexer::RESERVED_STATIC;
|
||||
} else if (idData == "mutable") {
|
||||
tockenElement = estyle::lexer::RESERVED_MUTABLE;
|
||||
} else if (idData == "virtual") {
|
||||
tockenElement = estyle::lexer::RESERVED_VIRTUAL;
|
||||
} else if (idData == "inline") {
|
||||
tockenElement = estyle::lexer::RESERVED_INLINE;
|
||||
} else if (idData == "final") {
|
||||
tockenElement = estyle::lexer::RESERVED_FINAL;
|
||||
} else if (idData == "default") {
|
||||
tockenElement = estyle::lexer::RESERVED_DEFAULT;
|
||||
} else if (idData == "int") {
|
||||
tockenElement = estyle::lexer::RESERVED_INT;
|
||||
} else if (idData == "long") {
|
||||
tockenElement = estyle::lexer::RESERVED_LONG;
|
||||
} else if (idData == "short") {
|
||||
tockenElement = estyle::lexer::RESERVED_SHORT;
|
||||
} else if (idData == "signed") {
|
||||
tockenElement = estyle::lexer::RESERVED_SIGNED;
|
||||
} else if (idData == "unsigned") {
|
||||
tockenElement = estyle::lexer::RESERVED_UNSIGNED;
|
||||
} else if (idData == "namespace") {
|
||||
tockenElement = estyle::lexer::RESERVED_NAMESPACE;
|
||||
} else if (idData == "class") {
|
||||
tockenElement = estyle::lexer::RESERVED_CLASS;
|
||||
} else if (idData == "struct") {
|
||||
tockenElement = estyle::lexer::RESERVED_STRUCT;
|
||||
} else if (idData == "true") {
|
||||
tockenElement = estyle::lexer::BOOLEAN;
|
||||
} else if (idData == "false") {
|
||||
tockenElement = estyle::lexer::BOOLEAN;
|
||||
}
|
||||
m_list.pushBack(estyle::LexerElement(tockenElement, tokenStart, iii));
|
||||
iii--;
|
||||
continue;
|
||||
}
|
||||
if (isNumeric(currentChar)) {
|
||||
tokenStart = iii;
|
||||
etk::String numericData;
|
||||
// parse numbers
|
||||
bool isHex = false;
|
||||
if (currentChar == '0') {
|
||||
numericData += currentChar;
|
||||
iii++;
|
||||
getChar(iii, currentChar, nextChar);
|
||||
}
|
||||
if (currentChar == 'x') {
|
||||
isHex = true;
|
||||
numericData += currentChar;
|
||||
iii++;
|
||||
getChar(iii, currentChar, nextChar);
|
||||
}
|
||||
enum estyle::lexer::tocken tockenElement = estyle::lexer::INTEGER_32;
|
||||
while ( isNumeric(currentChar)
|
||||
|| ( isHex == true
|
||||
&& isHexadecimal(currentChar) ) ) {
|
||||
numericData += currentChar;
|
||||
iii++;
|
||||
getChar(iii, currentChar, nextChar);
|
||||
}
|
||||
if ( isHex == false
|
||||
&& currentChar == '.') {
|
||||
tockenElement = estyle::lexer::FLOAT_64;
|
||||
numericData += '.';
|
||||
iii++;
|
||||
getChar(iii, currentChar, nextChar);
|
||||
while (isNumeric(currentChar)) {
|
||||
numericData += currentChar;
|
||||
iii++;
|
||||
getChar(iii, currentChar, nextChar);
|
||||
}
|
||||
}
|
||||
// do fancy e-style floating point
|
||||
if ( isHex == false
|
||||
&& ( currentChar == 'e'
|
||||
|| currentChar == 'E')) {
|
||||
tockenElement = estyle::lexer::FLOAT_64;
|
||||
numericData += currentChar;
|
||||
iii++;
|
||||
getChar(iii, currentChar, nextChar);
|
||||
if (currentChar == '-') {
|
||||
numericData += currentChar;
|
||||
iii++;
|
||||
getChar(iii, currentChar, nextChar);
|
||||
}
|
||||
while (isNumeric(currentChar) == true) {
|
||||
numericData += currentChar;
|
||||
iii++;
|
||||
getChar(iii, currentChar, nextChar);
|
||||
}
|
||||
}
|
||||
// Check the real range of the Values and update the internal value with adapted temporary variable
|
||||
if (tockenElement == estyle::lexer::INTEGER_32) {
|
||||
int64_t value = strtol(numericData.c_str(),0,0);
|
||||
if ( value < INT32_MIN
|
||||
|| value > INT32_MAX) {
|
||||
tockenElement = estyle::lexer::INTEGER_64;
|
||||
}
|
||||
} else if (tockenElement == estyle::lexer::FLOAT_64) {
|
||||
double value = strtod(numericData.c_str(),0);
|
||||
double value2 = float(value);
|
||||
if (etk::abs(value - value2) <= double(FLT_EPSILON)) {
|
||||
tockenElement = estyle::lexer::FLOAT_32;
|
||||
}
|
||||
}
|
||||
m_list.pushBack(estyle::LexerElement(tockenElement, tokenStart, iii));
|
||||
iii--;
|
||||
continue;
|
||||
}
|
||||
if (currentChar == '"') {
|
||||
// strings...
|
||||
iii++;
|
||||
getChar(iii, currentChar, nextChar);
|
||||
tokenStart = iii;
|
||||
etk::String numericData;
|
||||
while ( currentChar != 0
|
||||
&& currentChar != '"') {
|
||||
if ( currentChar == '\\'
|
||||
&& ( nextChar == '\\'
|
||||
|| nextChar == '"') ) {
|
||||
iii++;
|
||||
getChar(iii, currentChar, nextChar);
|
||||
}
|
||||
iii++;
|
||||
getChar(iii, currentChar, nextChar);
|
||||
}
|
||||
if (currentChar == 0) {
|
||||
ESTYLE_ERROR("Arrive at the end of file without '\"' element in string parsing");
|
||||
}
|
||||
m_list.pushBack(estyle::LexerElement(estyle::lexer::STRING, tokenStart, iii));
|
||||
iii++;
|
||||
getChar(iii, currentChar, nextChar);
|
||||
continue;
|
||||
}
|
||||
/*
|
||||
if (currentChar == '\'') {
|
||||
// strings...
|
||||
iii++;
|
||||
getChar(iii, currentChar, nextChar);
|
||||
tokenStart = iii;
|
||||
etk::String numericData;
|
||||
while ( currentChar != 0
|
||||
&& currentChar != '\'') {
|
||||
if ( currentChar == '\\'
|
||||
&& ( nextChar == '\\'
|
||||
|| nextChar == '\'') ) {
|
||||
iii++;
|
||||
getChar(iii, currentChar, nextChar);
|
||||
}
|
||||
iii++;
|
||||
getChar(iii, currentChar, nextChar);
|
||||
}
|
||||
if (currentChar == 0) {
|
||||
ESTYLE_ERROR("Arrive at the end of file without '\'' element in string parsing");
|
||||
}
|
||||
m_list.pushBack(estyle::LexerElement(estyle::lexer::STRING, tokenStart, iii));
|
||||
iii++;
|
||||
getChar(iii, currentChar, nextChar);
|
||||
continue;
|
||||
}
|
||||
*/
|
||||
if (currentChar == '=') {
|
||||
if (nextChar == '=') {
|
||||
m_list.pushBack(estyle::LexerElement(estyle::lexer::EQUAL_EQUAL, iii, iii+2));
|
||||
iii++;
|
||||
} else {
|
||||
m_list.pushBack(estyle::LexerElement(estyle::lexer::EQUAL, iii, iii+1));
|
||||
}
|
||||
continue;
|
||||
}
|
||||
if (currentChar == '!') {
|
||||
if (nextChar == '=') {
|
||||
m_list.pushBack(estyle::LexerElement(estyle::lexer::NOT_EQUAL, iii, iii+2));
|
||||
iii++;
|
||||
} else {
|
||||
m_list.pushBack(estyle::LexerElement(estyle::lexer::NOT, iii, iii+1));
|
||||
}
|
||||
continue;
|
||||
}
|
||||
if (currentChar == '<') {
|
||||
if (nextChar == '=') {
|
||||
m_list.pushBack(estyle::LexerElement(estyle::lexer::LESS_EQUAL, iii, iii+2));
|
||||
iii++;
|
||||
} else {
|
||||
m_list.pushBack(estyle::LexerElement(estyle::lexer::LESS, iii, iii+1));
|
||||
}
|
||||
continue;
|
||||
}
|
||||
if (currentChar == '>') {
|
||||
if (nextChar == '=') {
|
||||
m_list.pushBack(estyle::LexerElement(estyle::lexer::GREATER_EQUAL, iii, iii+2));
|
||||
iii++;
|
||||
} else {
|
||||
m_list.pushBack(estyle::LexerElement(estyle::lexer::GREATER, iii, iii+1));
|
||||
}
|
||||
continue;
|
||||
}
|
||||
if (currentChar == '+') {
|
||||
if (nextChar == '=') {
|
||||
m_list.pushBack(estyle::LexerElement(estyle::lexer::PLUS_EQUAL, iii, iii+2));
|
||||
iii++;
|
||||
} else if (nextChar == '+') {
|
||||
m_list.pushBack(estyle::LexerElement(estyle::lexer::PLUS_PLUS, iii, iii+2));
|
||||
iii++;
|
||||
} else {
|
||||
m_list.pushBack(estyle::LexerElement(estyle::lexer::PLUS, iii, iii+1));
|
||||
}
|
||||
continue;
|
||||
}
|
||||
if (currentChar == '-') {
|
||||
if (nextChar == '=') {
|
||||
m_list.pushBack(estyle::LexerElement(estyle::lexer::MINUS_EQUAL, iii, iii+2));
|
||||
iii++;
|
||||
} else if (nextChar == '-') {
|
||||
m_list.pushBack(estyle::LexerElement(estyle::lexer::MINUS_MINUS, iii, iii+2));
|
||||
iii++;
|
||||
} else {
|
||||
m_list.pushBack(estyle::LexerElement(estyle::lexer::MINUS, iii, iii+1));
|
||||
}
|
||||
continue;
|
||||
}
|
||||
if (currentChar == '&') {
|
||||
if (nextChar == '=') {
|
||||
m_list.pushBack(estyle::LexerElement(estyle::lexer::AND_EQUAL, iii, iii+2));
|
||||
iii++;
|
||||
} else if (nextChar == '&') {
|
||||
m_list.pushBack(estyle::LexerElement(estyle::lexer::AND_AND, iii, iii+2));
|
||||
iii++;
|
||||
} else {
|
||||
m_list.pushBack(estyle::LexerElement(estyle::lexer::AND, iii, iii+1));
|
||||
}
|
||||
continue;
|
||||
}
|
||||
if (currentChar == '|') {
|
||||
if (nextChar == '=') {
|
||||
m_list.pushBack(estyle::LexerElement(estyle::lexer::OR_EQUAL, iii, iii+2));
|
||||
iii++;
|
||||
} else if (nextChar == '|') {
|
||||
m_list.pushBack(estyle::LexerElement(estyle::lexer::OR_OR, iii, iii+2));
|
||||
iii++;
|
||||
} else {
|
||||
m_list.pushBack(estyle::LexerElement(estyle::lexer::OR, iii, iii+1));
|
||||
}
|
||||
continue;
|
||||
}
|
||||
if (currentChar == '-') {
|
||||
if (nextChar == '=') {
|
||||
m_list.pushBack(estyle::LexerElement(estyle::lexer::MINUS_EQUAL, iii, iii+2));
|
||||
iii++;
|
||||
} else if (nextChar == '-') {
|
||||
m_list.pushBack(estyle::LexerElement(estyle::lexer::MINUS_MINUS, iii, iii+2));
|
||||
iii++;
|
||||
} else {
|
||||
m_list.pushBack(estyle::LexerElement(estyle::lexer::MINUS, iii, iii+1));
|
||||
}
|
||||
continue;
|
||||
}
|
||||
if (currentChar == '^') {
|
||||
if (nextChar == '=') {
|
||||
m_list.pushBack(estyle::LexerElement(estyle::lexer::XOR_EQUAL, iii, iii+2));
|
||||
iii++;
|
||||
} else {
|
||||
m_list.pushBack(estyle::LexerElement(estyle::lexer::XOR, iii, iii+1));
|
||||
}
|
||||
continue;
|
||||
}
|
||||
if (currentChar == '/') {
|
||||
if (nextChar == '=') {
|
||||
m_list.pushBack(estyle::LexerElement(estyle::lexer::DEVIDE_EQUAL, iii, iii+2));
|
||||
iii++;
|
||||
} else {
|
||||
m_list.pushBack(estyle::LexerElement(estyle::lexer::DEVIDE, iii, iii+1));
|
||||
}
|
||||
continue;
|
||||
}
|
||||
if (currentChar == '*') {
|
||||
if (nextChar == '=') {
|
||||
m_list.pushBack(estyle::LexerElement(estyle::lexer::MULTIPLY_EQUAL, iii, iii+2));
|
||||
iii++;
|
||||
} else {
|
||||
m_list.pushBack(estyle::LexerElement(estyle::lexer::MULTIPLY, iii, iii+1));
|
||||
}
|
||||
continue;
|
||||
}
|
||||
if (currentChar == ':') {
|
||||
m_list.pushBack(estyle::LexerElement(estyle::lexer::COLON, iii, iii+1));
|
||||
continue;
|
||||
}
|
||||
if (currentChar == ';') {
|
||||
m_list.pushBack(estyle::LexerElement(estyle::lexer::SEMICOLON, iii, iii+1));
|
||||
continue;
|
||||
}
|
||||
if (currentChar == '(') {
|
||||
m_list.pushBack(estyle::LexerElement(estyle::lexer::PARENTHESE_IN, iii, iii+1));
|
||||
continue;
|
||||
}
|
||||
if (currentChar == ')') {
|
||||
m_list.pushBack(estyle::LexerElement(estyle::lexer::PARENTHESE_OUT, iii, iii+1));
|
||||
continue;
|
||||
}
|
||||
if (currentChar == '[') {
|
||||
m_list.pushBack(estyle::LexerElement(estyle::lexer::BRACKET_IN, iii, iii+1));
|
||||
continue;
|
||||
}
|
||||
if (currentChar == ']') {
|
||||
m_list.pushBack(estyle::LexerElement(estyle::lexer::BRACKET_OUT, iii, iii+1));
|
||||
continue;
|
||||
}
|
||||
if (currentChar == '{') {
|
||||
m_list.pushBack(estyle::LexerElement(estyle::lexer::BRACE_IN, iii, iii+1));
|
||||
continue;
|
||||
}
|
||||
if (currentChar == '}') {
|
||||
m_list.pushBack(estyle::LexerElement(estyle::lexer::BRACE_OUT, iii, iii+1));
|
||||
continue;
|
||||
}
|
||||
if (currentChar == '.') {
|
||||
m_list.pushBack(estyle::LexerElement(estyle::lexer::DOT, iii, iii+1));
|
||||
continue;
|
||||
}
|
||||
if (currentChar == ',') {
|
||||
m_list.pushBack(estyle::LexerElement(estyle::lexer::COMA, iii, iii+1));
|
||||
continue;
|
||||
}
|
||||
if (currentChar == '%') {
|
||||
m_list.pushBack(estyle::LexerElement(estyle::lexer::POUCENT, iii, iii+1));
|
||||
continue;
|
||||
}
|
||||
if (currentChar == estyle::lexer::END_OF_FILE) {
|
||||
// remove error
|
||||
continue;
|
||||
}
|
||||
ESTYLE_ERROR(" UNKNOW tocken : '" << char(currentChar) << "'");
|
||||
}
|
||||
}
|
||||
|
||||
estyle::Lexer::Lexer(const etk::String& _input) :
|
||||
m_stream(_input) {
|
||||
ESTYLE_DEBUG("Parse stream");
|
||||
parse();
|
||||
ESTYLE_DEBUG("find:");
|
||||
for (auto &it: m_list) {
|
||||
ESTYLE_DEBUG(" '" << it.getTocken() << "' ==> '" << m_stream.extract(it.getStart(),it.getStop()) << "'");
|
||||
}
|
||||
}
|
||||
|
||||
etk::String estyle::Lexer::getData(int32_t _position) const {
|
||||
return m_stream.extract(m_list[_position].getStart(), m_list[_position].getStop());
|
||||
}
|
||||
|
||||
etk::String estyle::Lexer::getValueString(int32_t _position) const {
|
||||
return m_stream.extract(m_list[_position].getStart(), m_list[_position].getStop());
|
||||
}
|
||||
|
||||
ivec2 estyle::Lexer::getFilePosition(int32_t _position) const {
|
||||
int32_t position = m_list[_position].getStart();
|
||||
int32_t line = 1;
|
||||
int32_t colomn = 1;
|
||||
for (int32_t iii=0; iii< position; ++iii) {
|
||||
colomn++;
|
||||
if (m_stream[iii] == '\n') {
|
||||
line++;
|
||||
colomn = 0;
|
||||
}
|
||||
}
|
||||
return ivec2(colomn, line);
|
||||
}
|
||||
|
||||
etk::String estyle::Lexer::getFileLine(int32_t _position) const {
|
||||
int32_t positionStart = m_list[_position].getStart();
|
||||
int32_t positionStop = m_list[_position].getStart();
|
||||
if (m_stream[positionStart] == '\n') {
|
||||
positionStart--;
|
||||
}
|
||||
if (positionStart < 0) {
|
||||
positionStart = 0;
|
||||
}
|
||||
for (;positionStop< m_stream.size(); ++positionStop) {
|
||||
if (m_stream[positionStop] == '\n') {
|
||||
break;
|
||||
}
|
||||
}
|
||||
for (;positionStart >= 0; --positionStart) {
|
||||
if (m_stream[positionStart] == '\n') {
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (positionStart < 0) {
|
||||
positionStart = 0;
|
||||
}
|
||||
ESTYLE_WARNING("extract " << positionStart << " " << positionStop);
|
||||
return m_stream.extract(positionStart, positionStop);
|
||||
}
|
56
estyle/lexer/Lexer.hpp
Normal file
56
estyle/lexer/Lexer.hpp
Normal file
@ -0,0 +1,56 @@
|
||||
/**
|
||||
* @author Edouard DUPIN
|
||||
* @copyright 2017, Edouard DUPIN, all right reserved
|
||||
* @license MPL-2 (see license file)
|
||||
*/
|
||||
#pragma once
|
||||
|
||||
#include <etk/types.hpp>
|
||||
#include <estyle/lexer/tocken.hpp>
|
||||
#include <etk/math/Vector2D.hpp>
|
||||
namespace estyle {
|
||||
class LexerElement {
|
||||
private:
|
||||
enum estyle::lexer::tocken m_tocken; //!< The type of the token that we have
|
||||
int32_t m_start; //!< Position in the data begin in the stream
|
||||
int32_t m_stop; //!< Position in the data end in the stream
|
||||
public:
|
||||
LexerElement(enum estyle::lexer::tocken _tocken, int32_t _start, int32_t _stop) :
|
||||
m_tocken(_tocken),
|
||||
m_start(_start),
|
||||
m_stop(_stop) {
|
||||
|
||||
}
|
||||
enum estyle::lexer::tocken getTocken() const {
|
||||
return m_tocken;
|
||||
}
|
||||
int32_t getStart() const {
|
||||
return m_start;
|
||||
}
|
||||
int32_t getStop() const {
|
||||
return m_stop;
|
||||
}
|
||||
};
|
||||
class Lexer {
|
||||
private:
|
||||
etk::String m_stream;
|
||||
etk::Vector<estyle::LexerElement> m_list;
|
||||
public:
|
||||
Lexer(const etk::String& _input);
|
||||
|
||||
size_t size() const {
|
||||
return m_list.size();
|
||||
}
|
||||
enum estyle::lexer::tocken getTocken(int32_t _position) const {
|
||||
return m_list[_position].getTocken();
|
||||
}
|
||||
etk::String getData(int32_t _position) const;
|
||||
etk::String getValueString(int32_t _position) const;
|
||||
ivec2 getFilePosition(int32_t _position) const;
|
||||
etk::String getFileLine(int32_t _position) const;
|
||||
private:
|
||||
void getChar(size_t _iii, char& _currentChar, char& _nextChar);
|
||||
void parse();
|
||||
|
||||
};
|
||||
}
|
129
estyle/lexer/tocken.cpp
Normal file
129
estyle/lexer/tocken.cpp
Normal file
@ -0,0 +1,129 @@
|
||||
/**
|
||||
* @author Edouard DUPIN
|
||||
* @copyright 2017, Edouard DUPIN, all right reserved
|
||||
* @license MPL-2 (see license file)
|
||||
*/
|
||||
|
||||
#include <estyle/lexer/tocken.hpp>
|
||||
|
||||
etk::String estyle::lexer::toString(estyle::lexer::tocken _token) {
|
||||
if (_token>32 && _token<128) {
|
||||
char buf[4] = "' '";
|
||||
buf[1] = (char)_token;
|
||||
return buf;
|
||||
}
|
||||
switch (_token) {
|
||||
case estyle::lexer::END_OF_FILE: return "EOF";
|
||||
case estyle::lexer::ID: return "ID";
|
||||
case estyle::lexer::INTEGER: return "INTEGER value";
|
||||
case estyle::lexer::FLOAT: return "FLOAT value";
|
||||
case estyle::lexer::STRING: return "STRING value";
|
||||
case estyle::lexer::BOOLEAN: return "BOOLEAN value";
|
||||
case estyle::lexer::EQUAL: return "=";
|
||||
case estyle::lexer::EQUAL_EQUAL: return "==";
|
||||
case estyle::lexer::NOT: return "!";
|
||||
case estyle::lexer::NOT_EQUAL: return "!=";
|
||||
case estyle::lexer::LESS: return "<";
|
||||
case estyle::lexer::LESS_EQUAL: return "<=";
|
||||
case estyle::lexer::GREATER: return ">";
|
||||
case estyle::lexer::GREATER_EQUAL: return ">=";
|
||||
case estyle::lexer::PLUS: return "+";
|
||||
case estyle::lexer::MINUS: return "-";
|
||||
case estyle::lexer::PLUS_EQUAL: return "+=";
|
||||
case estyle::lexer::MINUS_EQUAL: return "-=";
|
||||
case estyle::lexer::PLUS_PLUS: return "++";
|
||||
case estyle::lexer::MINUS_MINUS: return "--";
|
||||
case estyle::lexer::AND: return "&";
|
||||
case estyle::lexer::AND_EQUAL: return "&=";
|
||||
case estyle::lexer::AND_AND: return "&&";
|
||||
case estyle::lexer::OR: return "|";
|
||||
case estyle::lexer::OR_EQUAL: return "|=";
|
||||
case estyle::lexer::OR_OR: return "||";
|
||||
case estyle::lexer::XOR: return "^";
|
||||
case estyle::lexer::XOR_EQUAL: return "^=";
|
||||
case estyle::lexer::COLON: return ":";
|
||||
case estyle::lexer::SEMICOLON: return ";";
|
||||
case estyle::lexer::PARENTHESE_IN: return "(";
|
||||
case estyle::lexer::PARENTHESE_OUT: return ")";
|
||||
case estyle::lexer::BRACKET_IN: return "[";
|
||||
case estyle::lexer::BRACKET_OUT: return "]";
|
||||
case estyle::lexer::BRACE_IN: return "{";
|
||||
case estyle::lexer::BRACE_OUT: return "}";
|
||||
case estyle::lexer::DEVIDE: return "/";
|
||||
case estyle::lexer::MULTIPLY: return "*";
|
||||
case estyle::lexer::DEVIDE_EQUAL: return "/=";
|
||||
case estyle::lexer::MULTIPLY_EQUAL: return "*=";
|
||||
//case estyle::lexer::QUESTION: return "?";
|
||||
case estyle::lexer::DOT: return ".";
|
||||
case estyle::lexer::COMA: return ",";
|
||||
case estyle::lexer::POUCENT: return "%";
|
||||
// reserved words
|
||||
case estyle::lexer::RESERVED_COMMENT_1_LINE: return "COMMENT 1";
|
||||
case estyle::lexer::RESERVED_COMMENT_N_LINE: return "COMMENT N";
|
||||
case estyle::lexer::RESERVED_DOCUMENTATION_1_LINE: return "DOCUMENTATION 1";
|
||||
case estyle::lexer::RESERVED_DOCUMENTATION_N_LINE: return "DOCUMENTATION N";
|
||||
case estyle::lexer::RESERVED_IF: return "if";
|
||||
case estyle::lexer::RESERVED_ELSE: return "else";
|
||||
case estyle::lexer::RESERVED_DO: return "do";
|
||||
case estyle::lexer::RESERVED_WHILE: return "while";
|
||||
case estyle::lexer::RESERVED_FOR: return "for";
|
||||
case estyle::lexer::RESERVED_SWITCH: return "switch";
|
||||
case estyle::lexer::RESERVED_CASE: return "case";
|
||||
case estyle::lexer::RESERVED_BREAK: return "break";
|
||||
case estyle::lexer::RESERVED_CONTINUE: return "continue";
|
||||
case estyle::lexer::RESERVED_RETURN: return "return";
|
||||
case estyle::lexer::RESERVED_VARIABLE: return "variable";
|
||||
case estyle::lexer::RESERVED_NEW: return "new";
|
||||
case estyle::lexer::RESERVED_DELETE: return "delete";
|
||||
case estyle::lexer::RESERVED_PRIVATE: return "private";
|
||||
case estyle::lexer::RESERVED_PUBLIC: return "public";
|
||||
case estyle::lexer::RESERVED_PROTECTED: return "protected";
|
||||
case estyle::lexer::RESERVED_NEW_LINE: return "\\n";
|
||||
case estyle::lexer::RESERVED_CONST: return "const";
|
||||
case estyle::lexer::RESERVED_STATIC: return "static";
|
||||
case estyle::lexer::RESERVED_MUTABLE: return "mutable";
|
||||
case estyle::lexer::RESERVED_VIRTUAL: return "virtual";
|
||||
case estyle::lexer::RESERVED_INLINE: return "inline";
|
||||
case estyle::lexer::RESERVED_FINAL: return "final";
|
||||
case estyle::lexer::RESERVED_DEFAULT: return "default";
|
||||
case estyle::lexer::RESERVED_INT: return "int";
|
||||
case estyle::lexer::RESERVED_LONG: return "long";
|
||||
case estyle::lexer::RESERVED_SHORT: return "short";
|
||||
case estyle::lexer::RESERVED_SIGNED: return "signed";
|
||||
case estyle::lexer::RESERVED_UNSIGNED: return "unsigned";
|
||||
case estyle::lexer::RESERVED_NAMESPACE: return "namespace";
|
||||
case estyle::lexer::RESERVED_CLASS: return "class";
|
||||
case estyle::lexer::RESERVED_STRUCT: return "struct";
|
||||
|
||||
case estyle::lexer::BASIC_TYPE_INTEGER_008: return "int8_t";
|
||||
case estyle::lexer::BASIC_TYPE_INTEGER_008_UNSIGNED: return "uint8_t";
|
||||
case estyle::lexer::BASIC_TYPE_INTEGER_016: return "int16_t";
|
||||
case estyle::lexer::BASIC_TYPE_INTEGER_016_UNSIGNED: return "uint16_t";
|
||||
case estyle::lexer::BASIC_TYPE_INTEGER_032: return "int32_t";
|
||||
case estyle::lexer::BASIC_TYPE_INTEGER_032_UNSIGNED: return "uint32_t";
|
||||
case estyle::lexer::BASIC_TYPE_INTEGER_064: return "int64_t";
|
||||
case estyle::lexer::BASIC_TYPE_INTEGER_064_UNSIGNED: return "uint64_t";
|
||||
case estyle::lexer::BASIC_TYPE_INTEGER_128: return "int128_t";
|
||||
case estyle::lexer::BASIC_TYPE_INTEGER_128_UNSIGNED: return "uint128_t";
|
||||
case estyle::lexer::BASIC_TYPE_FLOAT_32: return "float";
|
||||
case estyle::lexer::BASIC_TYPE_FLOAT_64: return "double";
|
||||
case estyle::lexer::BASIC_TYPE_FLOAT_96: return "triple";
|
||||
case estyle::lexer::BASIC_TYPE_BOOLEAN: return "bool";
|
||||
case estyle::lexer::BASIC_TYPE_STD_STRING: return "std::string";
|
||||
case estyle::lexer::BASIC_TYPE_NULLPTR: return "std::nullptr";
|
||||
case estyle::lexer::BASIC_TYPE_VOID: return "void";
|
||||
}
|
||||
return etk::String("?[") + etk::toString(int32_t(_token)) + "]";
|
||||
}
|
||||
|
||||
etk::Stream& estyle::operator<<(etk::Stream& _os, const enum estyle::lexer::tocken& _obj) {
|
||||
_os << estyle::lexer::toString(_obj);
|
||||
return _os;
|
||||
}
|
||||
|
||||
namespace etk {
|
||||
template<>
|
||||
etk::String toString(const enum estyle::lexer::tocken& _obj) {
|
||||
return estyle::lexer::toString(_obj);
|
||||
}
|
||||
}
|
128
estyle/lexer/tocken.hpp
Normal file
128
estyle/lexer/tocken.hpp
Normal file
@ -0,0 +1,128 @@
|
||||
/**
|
||||
* @author Edouard DUPIN
|
||||
* @copyright 2017, Edouard DUPIN, all right reserved
|
||||
* @license MPL-2 (see license file)
|
||||
*/
|
||||
#pragma once
|
||||
|
||||
#include <etk/types.hpp>
|
||||
#include <etk/Stream.hpp>
|
||||
#include <etk/String.hpp>
|
||||
|
||||
namespace estyle {
|
||||
namespace lexer {
|
||||
enum tocken {
|
||||
END_OF_FILE = 0, //!< End of file reach
|
||||
ID = 256,
|
||||
INTEGER, //!< integer number
|
||||
FLOAT, //!< Floating point number
|
||||
STRING, //!< string element
|
||||
BOOLEAN, //!< string element
|
||||
|
||||
EQUAL, //!< element "="
|
||||
EQUAL_EQUAL, //!< element "=="
|
||||
NOT, //!< element "!"
|
||||
NOT_EQUAL, //!< element "!="
|
||||
LESS, //!< element "<"
|
||||
LESS_EQUAL, //!< element "<="
|
||||
GREATER, //!< element ">"
|
||||
GREATER_EQUAL, //!< element ">="
|
||||
PLUS, //!< element "+"
|
||||
MINUS, //!< element "-"
|
||||
PLUS_EQUAL, //!< element "+="
|
||||
MINUS_EQUAL, //!< element "-="
|
||||
PLUS_PLUS, //!< element "++"
|
||||
MINUS_MINUS, //!< element "--"
|
||||
AND, //!< element "&"
|
||||
AND_EQUAL, //!< element "&="
|
||||
AND_AND, //!< element "&&"
|
||||
OR, //!< element "|"
|
||||
OR_EQUAL, //!< element "|="
|
||||
OR_OR, //!< element "||"
|
||||
XOR, //!< element "^"
|
||||
XOR_EQUAL, //!< element "^="
|
||||
COLON, //!< element ":"
|
||||
SEMICOLON, //!< element ";"
|
||||
PARENTHESE_IN, //!< element "("
|
||||
PARENTHESE_OUT, //!< element ")"
|
||||
BRACKET_IN, //!< element "["
|
||||
BRACKET_OUT, //!< element "]"
|
||||
BRACE_IN, //!< element "{"
|
||||
BRACE_OUT, //!< element "}"
|
||||
DEVIDE, //!< element "/"
|
||||
DEVIDE_EQUAL, //!< element "/="
|
||||
MULTIPLY, //!< element "*"
|
||||
MULTIPLY_EQUAL, //!< element "*="
|
||||
DOT, //!< element "."
|
||||
COMA, //!< element ","
|
||||
POUCENT, //!< element "%"
|
||||
// reserved words
|
||||
#define RESERVED_LIST_START RESERVED_IF
|
||||
RESERVED_LIST_BEGIN,
|
||||
|
||||
RESERVED_COMMENT_1_LINE, //!< simple comment
|
||||
RESERVED_COMMENT_N_LINE, //!< simple comment
|
||||
RESERVED_DOCUMENTATION_1_LINE, //!< Documentation on the element
|
||||
RESERVED_DOCUMENTATION_N_LINE, //!< Documentation on the element
|
||||
RESERVED_IF, //!< element "if"
|
||||
RESERVED_ELSE, //!< element "else"
|
||||
RESERVED_DO, //!< element "do"
|
||||
RESERVED_WHILE, //!< element "while"
|
||||
RESERVED_FOR, //!< element "for"
|
||||
RESERVED_SWITCH, //!< element "switch"
|
||||
RESERVED_CASE, //!< element "case"
|
||||
RESERVED_BREAK, //!< element "break"
|
||||
RESERVED_CONTINUE, //!< element "continue"
|
||||
RESERVED_RETURN, //!< element "return"
|
||||
RESERVED_NEW, //!< element "new"
|
||||
RESERVED_DELETE, //!< element "delete"
|
||||
RESERVED_PRIVATE, //!< element "private"
|
||||
RESERVED_PUBLIC, //!< element "public"
|
||||
RESERVED_PROTECTED, //!< element "protected"
|
||||
RESERVED_CONST, //!< element "const"
|
||||
RESERVED_STATIC, //!< element "static"
|
||||
RESERVED_MUTABLE, //!< element "mutable"
|
||||
RESERVED_VIRTUAL, //!< element "virtual"
|
||||
RESERVED_INLINE, //!< element "inline"
|
||||
RESERVED_FINAL, //!< element "final"
|
||||
RESERVED_DEFAULT, //!< element "default"
|
||||
RESERVED_INT, //!< element "int"
|
||||
RESERVED_LONG, //!< element "long"
|
||||
RESERVED_SHORT, //!< element "short"
|
||||
RESERVED_SIGNED, //!< element "signed"
|
||||
RESERVED_UNSIGNED, //!< element "unsigned"
|
||||
RESERVED_NAMESPACE, //!< element "namespace"
|
||||
RESERVED_CLASS, //!< element "class"
|
||||
RESERVED_STRUCT, //!< element "struct"
|
||||
RESERVED_NEW_LINE, //!< Neew line
|
||||
RESERVED_LIST_END,
|
||||
|
||||
BASIC_TYPE_LIST_BEGIN,
|
||||
|
||||
BASIC_TYPE_INTEGER_SIZE_T, //!< size_t element number
|
||||
BASIC_TYPE_INTEGER_008, //!< int8_t element number
|
||||
BASIC_TYPE_INTEGER_008_UNSIGNED, //!< uint8_t element number
|
||||
BASIC_TYPE_INTEGER_016, //!< int16_t element number
|
||||
BASIC_TYPE_INTEGER_016_UNSIGNED, //!< uint16_t element number
|
||||
BASIC_TYPE_INTEGER_032, //!< int32_t element number
|
||||
BASIC_TYPE_INTEGER_032_UNSIGNED, //!< uint32_t element number
|
||||
BASIC_TYPE_INTEGER_064, //!< int64_t element number
|
||||
BASIC_TYPE_INTEGER_064_UNSIGNED, //!< uint64_t element number
|
||||
BASIC_TYPE_INTEGER_128, //!< int128_t element number
|
||||
BASIC_TYPE_INTEGER_128_UNSIGNED, //!< uint128_t element number
|
||||
BASIC_TYPE_FLOAT_32, //!<
|
||||
BASIC_TYPE_FLOAT_64, //!<
|
||||
BASIC_TYPE_FLOAT_96, //!<
|
||||
BASIC_TYPE_BOOLEAN, //!<
|
||||
BASIC_TYPE_STD_STRING, //!<
|
||||
BASIC_TYPE_STD_NULLPTR, //!<
|
||||
BASIC_TYPE_VOID, //!<
|
||||
|
||||
BASIC_TYPE_LIST_END,
|
||||
};
|
||||
etk::String toString(estyle::lexer::tocken _token);
|
||||
}
|
||||
//! @not_in_doc
|
||||
etk::Stream& operator <<(etk::Stream& _os, const enum estyle::lexer::tocken& _obj);
|
||||
}
|
||||
|
29
lutin_estyle-test.py
Normal file
29
lutin_estyle-test.py
Normal file
@ -0,0 +1,29 @@
|
||||
#!/usr/bin/python
|
||||
import lutin.module as module
|
||||
import lutin.tools as tools
|
||||
|
||||
def get_type():
|
||||
return "BINARY"
|
||||
|
||||
def get_sub_type():
|
||||
return "TEST"
|
||||
|
||||
def get_name():
|
||||
return "estyle-test"
|
||||
|
||||
def get_desc():
|
||||
return "Stylizer for C++ test"
|
||||
|
||||
def configure(target, my_module):
|
||||
my_module.add_depend(['estyle', 'etest', 'test-debug'])
|
||||
# add extra compilation flags :
|
||||
my_module.add_extra_flags()
|
||||
# add sources files
|
||||
my_module.add_src_file([
|
||||
'test/main.cpp',
|
||||
])
|
||||
my_module.add_path("test")
|
||||
my_module.copy_path('data/*','test')
|
||||
return my_module
|
||||
|
||||
|
35
lutin_estyle.py
Normal file
35
lutin_estyle.py
Normal file
@ -0,0 +1,35 @@
|
||||
#!/usr/bin/python
|
||||
import lutin.module as module
|
||||
import lutin.tools as tools
|
||||
|
||||
def get_type():
|
||||
return "LIBRARY"
|
||||
|
||||
def get_name():
|
||||
return "estyle"
|
||||
|
||||
def get_desc():
|
||||
return "style checker/updater for C++"
|
||||
|
||||
def configure(target, my_module):
|
||||
my_module.add_depend(['etk', 'eproperty', 'cxx'])
|
||||
# add extra compilation flags :
|
||||
my_module.add_extra_flags()
|
||||
# add sources files
|
||||
my_module.add_src_file([
|
||||
'estyle/debug.cpp',
|
||||
'estyle/estyle.cpp',
|
||||
'estyle/Generator.cpp',
|
||||
'estyle/lexer/Lexer.cpp',
|
||||
'estyle/lexer/tocken.cpp',
|
||||
])
|
||||
my_module.add_header_file([
|
||||
'estyle/debug.hpp',
|
||||
'estyle/estyle.hpp',
|
||||
'estyle/Generator.hpp',
|
||||
'estyle/lexer/Lexer.hpp',
|
||||
'estyle/lexer/tocken.hpp',
|
||||
])
|
||||
return my_module
|
||||
|
||||
|
43
test/main.cpp
Normal file
43
test/main.cpp
Normal file
@ -0,0 +1,43 @@
|
||||
/**
|
||||
* @author Edouard DUPIN
|
||||
* @copyright 2017, Edouard DUPIN, all right reserved
|
||||
* @license MPL-2 (see license file)
|
||||
*/
|
||||
#include <estyle/estyle.hpp>
|
||||
#include <estyle/Generator.hpp>
|
||||
#include <etk/etk.hpp>
|
||||
#include <etk/os/FSNode.hpp>
|
||||
#include <test-debug/debug.hpp>
|
||||
#include <etest/etest.hpp>
|
||||
|
||||
|
||||
int main(int _argc, const char** _argv) {
|
||||
// the only one init for etk:
|
||||
etest::init(_argc, _argv);
|
||||
for (int32_t iii=1; iii<_argc ; ++iii) {
|
||||
etk::String data = _argv[iii];
|
||||
if ( data == "-h"
|
||||
|| data == "--help") {
|
||||
ETEST_PRINT("Help : ");
|
||||
ETEST_PRINT(" ./xxx [options]");
|
||||
ETEST_PRINT(" File to test");
|
||||
ETEST_PRINT(" -h/--help: this help");
|
||||
exit(0);
|
||||
}
|
||||
}
|
||||
return RUN_ALL_TESTS();
|
||||
}
|
||||
|
||||
TEST(testParsingJS, test018) {
|
||||
estyle::Generator interface;
|
||||
|
||||
|
||||
etk::String source = "/* simple comment */\nint32_t hello = \"plouf \\n\";\n";
|
||||
etk::String output = interface.process(source);
|
||||
|
||||
TEST_INFO("source:\n" << source);
|
||||
TEST_INFO("output:\n" << output);
|
||||
|
||||
}
|
||||
|
||||
|
0
test/testClass.cpp
Normal file
0
test/testClass.cpp
Normal file
44
test/testComment.cpp
Normal file
44
test/testComment.cpp
Normal file
@ -0,0 +1,44 @@
|
||||
/**
|
||||
* @author Edouard DUPIN
|
||||
* @copyright 2017, Edouard DUPIN, all right reserved
|
||||
* @license MPL-2 (see license file)
|
||||
*/
|
||||
#include <test-debug/debug.hpp>
|
||||
#include <etest/etest.hpp>
|
||||
#include "testInterface.hpp"
|
||||
|
||||
|
||||
TEST(testComment, singleLine) {
|
||||
testInterface system;
|
||||
bool ret = system.execute("// result = true; \n variable hello=9;");
|
||||
EXPECT_EQ(ret, true);
|
||||
EXPECT_EQ(system.exist("result"), false);
|
||||
}
|
||||
|
||||
|
||||
TEST(testComment, multipleLine) {
|
||||
testInterface system;
|
||||
bool ret = system.execute("var result2 = false; \n /* var result \n = \n true; \n result = 8; \n */ var result = 118;");
|
||||
EXPECT_EQ(ret, true);
|
||||
EXPECT_EQ(system.exist("result2"), true);
|
||||
EXPECT_EQ(system.exist("result"), true);
|
||||
EXPECT_EQ(system.getBoolean("result2"), false);
|
||||
EXPECT_EQ(system.getInteger32("result"), 118);
|
||||
}
|
||||
|
||||
TEST(testDocumentation, singleLine) {
|
||||
testInterface system;
|
||||
bool ret = system.execute("//! result = true; \n variable hello=9;");
|
||||
EXPECT_EQ(ret, true);
|
||||
EXPECT_EQ(system.exist("result"), false);
|
||||
}
|
||||
|
||||
TEST(testDocumentation, multipleLine) {
|
||||
testInterface system;
|
||||
bool ret = system.execute("var result2 = false; \n /** var result \n = \n true; \n result = 8; \n */ var result = 118;");
|
||||
EXPECT_EQ(ret, true);
|
||||
EXPECT_EQ(system.exist("result2"), true);
|
||||
EXPECT_EQ(system.exist("result"), true);
|
||||
EXPECT_EQ(system.getBoolean("result2"), false);
|
||||
EXPECT_EQ(system.getInteger32("result"), 118);
|
||||
}
|
81
test/testFor.cpp
Normal file
81
test/testFor.cpp
Normal file
@ -0,0 +1,81 @@
|
||||
/**
|
||||
* @author Edouard DUPIN
|
||||
* @copyright 2017, Edouard DUPIN, all right reserved
|
||||
* @license MPL-2 (see license file)
|
||||
*/
|
||||
#include <test-debug/debug.hpp>
|
||||
#include <etest/etest.hpp>
|
||||
#include "testInterface.hpp"
|
||||
|
||||
|
||||
//////////////////////////////////////
|
||||
// FOR
|
||||
//////////////////////////////////////
|
||||
|
||||
|
||||
TEST(testFor, simple) {
|
||||
testInterface system;
|
||||
bool ret = system.execute("variable result = 0; variable i; for (i=1;i<10;i++) result = result + i;");
|
||||
EXPECT_EQ(ret, true);
|
||||
EXPECT_EQ(system.exist("result"), true);
|
||||
EXPECT_EQ(system.isInteger32("result"), true);
|
||||
EXPECT_EQ(system.getInteger32("result"), 45);
|
||||
}
|
||||
|
||||
TEST(testFor, internal_init) {
|
||||
testInterface system;
|
||||
bool ret = system.execute("variable result = 0; for (variable i=1;i<10;i++) result = result + i;");
|
||||
EXPECT_EQ(ret, true);
|
||||
EXPECT_EQ(system.exist("result"), true);
|
||||
EXPECT_EQ(system.isInteger32("result"), true);
|
||||
EXPECT_EQ(system.getInteger32("result"), 45);
|
||||
}
|
||||
|
||||
TEST(testFor, decrement) {
|
||||
testInterface system;
|
||||
bool ret = system.execute("variable result = 0; for (variable i=10;i>1;i--) result = result + i;");
|
||||
EXPECT_EQ(ret, true);
|
||||
EXPECT_EQ(system.exist("result"), true);
|
||||
EXPECT_EQ(system.isInteger32("result"), true);
|
||||
EXPECT_EQ(system.getInteger32("result"), 54);
|
||||
}
|
||||
|
||||
|
||||
//////////////////////////////////////
|
||||
// while
|
||||
//////////////////////////////////////
|
||||
|
||||
TEST(testWhile, simple) {
|
||||
testInterface system;
|
||||
bool ret = system.execute("variable result = 0; variable i=1; while (i<10) { result = result + i; i++;}");
|
||||
EXPECT_EQ(ret, true);
|
||||
EXPECT_EQ(system.exist("result"), true);
|
||||
EXPECT_EQ(system.isInteger32("result"), true);
|
||||
EXPECT_EQ(system.getInteger32("result"), 45);
|
||||
}
|
||||
|
||||
|
||||
|
||||
//////////////////////////////////////
|
||||
// do .. while
|
||||
//////////////////////////////////////
|
||||
|
||||
TEST(testDoWhile, simple) {
|
||||
testInterface system;
|
||||
bool ret = system.execute("variable result = 0; variable i=1; do { result = result + i; i++;} while (i<10);");
|
||||
EXPECT_EQ(ret, true);
|
||||
EXPECT_EQ(system.exist("result"), true);
|
||||
EXPECT_EQ(system.isInteger32("result"), true);
|
||||
EXPECT_EQ(system.getInteger32("result"), 45);
|
||||
}
|
||||
|
||||
|
||||
TEST(testDoWhile, first) {
|
||||
testInterface system;
|
||||
bool ret = system.execute("variable result = 0; variable i=1; do { result = result + i; i++;} while (i<0);");
|
||||
EXPECT_EQ(ret, true);
|
||||
EXPECT_EQ(system.exist("result"), true);
|
||||
EXPECT_EQ(system.isInteger32("result"), true);
|
||||
EXPECT_EQ(system.getInteger32("result"), 1);
|
||||
}
|
||||
|
56
test/testFunction.cpp
Normal file
56
test/testFunction.cpp
Normal file
@ -0,0 +1,56 @@
|
||||
/**
|
||||
* @author Edouard DUPIN
|
||||
* @copyright 2017, Edouard DUPIN, all right reserved
|
||||
* @license MPL-2 (see license file)
|
||||
*/
|
||||
#include <test-debug/debug.hpp>
|
||||
#include <etest/etest.hpp>
|
||||
#include "testInterface.hpp"
|
||||
|
||||
|
||||
TEST(testFunction, simple) {
|
||||
testInterface system;
|
||||
bool ret = system.execute("function add(a,b) { return a+b; }; variable result = add(222,33);");
|
||||
EXPECT_EQ(ret, true);
|
||||
EXPECT_EQ(system.exist("result"), true);
|
||||
EXPECT_EQ(system.isInteger32("result"), true);
|
||||
EXPECT_EQ(system.getInteger32("result"), 255);
|
||||
}
|
||||
|
||||
TEST(testFunction, function_in_object) {
|
||||
testInterface system;
|
||||
bool ret = system.execute("variable myObject= {};myObject.add = function(a,b) { return a+b; }; variable result = myObject.add(222,33);");
|
||||
EXPECT_EQ(ret, true);
|
||||
EXPECT_EQ(system.exist("result"), true);
|
||||
EXPECT_EQ(system.isInteger32("result"), true);
|
||||
EXPECT_EQ(system.getInteger32("result"), 255);
|
||||
}
|
||||
|
||||
|
||||
TEST(testFunction, function_in_object_in_json) {
|
||||
testInterface system;
|
||||
bool ret = system.execute("variable myObject= { add : function(a,b) { return a+b; } }; variable result = myObject.add(222,33);");
|
||||
EXPECT_EQ(ret, true);
|
||||
EXPECT_EQ(system.exist("result"), true);
|
||||
EXPECT_EQ(system.isInteger32("result"), true);
|
||||
EXPECT_EQ(system.getInteger32("result"), 255);
|
||||
}
|
||||
|
||||
|
||||
TEST(testFunction, multiple_call) {
|
||||
testInterface system;
|
||||
bool ret = system.execute("function first(a) { return a+200; };function second(a) { return first(a)+50; }; variable result = second(5);");
|
||||
EXPECT_EQ(ret, true);
|
||||
EXPECT_EQ(system.exist("result"), true);
|
||||
EXPECT_EQ(system.isInteger32("result"), true);
|
||||
EXPECT_EQ(system.getInteger32("result"), 255);
|
||||
}
|
||||
TEST(testFunction, recursion) {
|
||||
testInterface system;
|
||||
bool ret = system.execute("function recursive(a) { if (a>1) { return a + recursive(a-1); } return 1; }; variable result = recursive(10);");
|
||||
EXPECT_EQ(ret, true);
|
||||
EXPECT_EQ(system.exist("result"), true);
|
||||
EXPECT_EQ(system.isInteger32("result"), true);
|
||||
EXPECT_EQ(system.getInteger32("result"), 10+9+8+7+6+5+4+3+2+1);
|
||||
}
|
||||
|
0
test/testFunctionNative.cpp
Normal file
0
test/testFunctionNative.cpp
Normal file
107
test/testIf.cpp
Normal file
107
test/testIf.cpp
Normal file
@ -0,0 +1,107 @@
|
||||
/**
|
||||
* @author Edouard DUPIN
|
||||
* @copyright 2017, Edouard DUPIN, all right reserved
|
||||
* @license MPL-2 (see license file)
|
||||
*/
|
||||
#include <test-debug/debug.hpp>
|
||||
#include <etest/etest.hpp>
|
||||
#include "testInterface.hpp"
|
||||
|
||||
|
||||
TEST(testIf, direct_1) {
|
||||
testInterface system;
|
||||
bool ret = system.execute("variable result = false; if (true) { result = true;};");
|
||||
EXPECT_EQ(ret, true);
|
||||
EXPECT_EQ(system.exist("result"), true);
|
||||
EXPECT_EQ(system.isBoolean("result"), true);
|
||||
EXPECT_EQ(system.getBoolean("result"), true);
|
||||
}
|
||||
|
||||
TEST(testIf, direct_2) {
|
||||
testInterface system;
|
||||
bool ret = system.execute("variable result = false; if (false) { result = true;};");
|
||||
EXPECT_EQ(ret, true);
|
||||
EXPECT_EQ(system.exist("result"), true);
|
||||
EXPECT_EQ(system.isBoolean("result"), true);
|
||||
EXPECT_EQ(system.getBoolean("result"), false);
|
||||
}
|
||||
|
||||
TEST(testIf, direct_3) {
|
||||
testInterface system;
|
||||
bool ret = system.execute("variable result = false; if (true) result = true;");
|
||||
EXPECT_EQ(ret, true);
|
||||
EXPECT_EQ(system.exist("result"), true);
|
||||
EXPECT_EQ(system.isBoolean("result"), true);
|
||||
EXPECT_EQ(system.getBoolean("result"), true);
|
||||
}
|
||||
|
||||
TEST(testIf, direct_4) {
|
||||
testInterface system;
|
||||
bool ret = system.execute("variable result = false; if (false) result = true;");
|
||||
EXPECT_EQ(ret, true);
|
||||
EXPECT_EQ(system.exist("result"), true);
|
||||
EXPECT_EQ(system.isBoolean("result"), true);
|
||||
EXPECT_EQ(system.getBoolean("result"), false);
|
||||
}
|
||||
|
||||
TEST(testIf, else_case_1) {
|
||||
testInterface system;
|
||||
bool ret = system.execute("variable result = 0; if (false) { result = 1;} else { result = 2;}");
|
||||
EXPECT_EQ(ret, true);
|
||||
EXPECT_EQ(system.exist("result"), true);
|
||||
EXPECT_EQ(system.isInteger32("result"), true);
|
||||
EXPECT_EQ(system.getInteger32("result"), 2);
|
||||
}
|
||||
TEST(testIf, else_case_2) {
|
||||
testInterface system;
|
||||
bool ret = system.execute("variable result = 0; if (false) result = 1; else result = 2;");
|
||||
EXPECT_EQ(ret, true);
|
||||
EXPECT_EQ(system.exist("result"), true);
|
||||
EXPECT_EQ(system.isInteger32("result"), true);
|
||||
EXPECT_EQ(system.getInteger32("result"), 2);
|
||||
}
|
||||
|
||||
TEST(testIf, else_else_case) {
|
||||
testInterface system;
|
||||
bool ret = system.execute("variable result = 0; if (false) { result = 1;} else if (false) { result = 2;} else { result = 3;};");
|
||||
EXPECT_EQ(ret, true);
|
||||
EXPECT_EQ(system.exist("result"), true);
|
||||
EXPECT_EQ(system.isInteger32("result"), true);
|
||||
EXPECT_EQ(system.getInteger32("result"), 3);
|
||||
}
|
||||
|
||||
TEST(testIf, variable_1) {
|
||||
testInterface system;
|
||||
bool ret = system.execute("variable a = true; variable result = false; if (a) { result = true;};");
|
||||
EXPECT_EQ(ret, true);
|
||||
EXPECT_EQ(system.exist("result"), true);
|
||||
EXPECT_EQ(system.isBoolean("result"), true);
|
||||
EXPECT_EQ(system.getBoolean("result"), true);
|
||||
}
|
||||
|
||||
TEST(testIf, variable_2) {
|
||||
testInterface system;
|
||||
bool ret = system.execute("variable a = false; variable result = false; if (a) { result = true;};");
|
||||
EXPECT_EQ(ret, true);
|
||||
EXPECT_EQ(system.exist("result"), true);
|
||||
EXPECT_EQ(system.isBoolean("result"), true);
|
||||
EXPECT_EQ(system.getBoolean("result"), false);
|
||||
}
|
||||
|
||||
TEST(testIf, compare_1) {
|
||||
testInterface system;
|
||||
bool ret = system.execute("variable a = 111; variable b = 333; variable result = false; if (a != b) { result = true;};");
|
||||
EXPECT_EQ(ret, true);
|
||||
EXPECT_EQ(system.exist("result"), true);
|
||||
EXPECT_EQ(system.isBoolean("result"), true);
|
||||
EXPECT_EQ(system.getBoolean("result"), true);
|
||||
}
|
||||
|
||||
TEST(testIf, compare_2) {
|
||||
testInterface system;
|
||||
bool ret = system.execute("variable a = 111; variable b = 333; variable result = false; if (a == b) { result = true;};");
|
||||
EXPECT_EQ(ret, true);
|
||||
EXPECT_EQ(system.exist("result"), true);
|
||||
EXPECT_EQ(system.isBoolean("result"), true);
|
||||
EXPECT_EQ(system.getBoolean("result"), false);
|
||||
}
|
142
test/testInterface.hpp
Normal file
142
test/testInterface.hpp
Normal file
@ -0,0 +1,142 @@
|
||||
/**
|
||||
* @author Edouard DUPIN
|
||||
* @copyright 2017, Edouard DUPIN, all right reserved
|
||||
* @license MPL-2 (see license file)
|
||||
*/
|
||||
#pragma once
|
||||
|
||||
#include <kikou/kikou.hpp>
|
||||
|
||||
class testInterface {
|
||||
public:
|
||||
kikou::Interpreter m_system;
|
||||
public:
|
||||
testInterface() {
|
||||
m_system.import("core");
|
||||
m_system.import("Math");
|
||||
}
|
||||
bool execute(const etk::String& _data) {
|
||||
// TODO : ... m_system.trace();
|
||||
//m_system.addChild("result", new kikou::Var("0", kikou::Var::type::INTEGER));
|
||||
try {
|
||||
m_system.compile(_data);
|
||||
m_system.execute();
|
||||
} catch (etk::exception::RuntimeError& eee) {
|
||||
ETEST_ERROR(eee.what());
|
||||
return false;
|
||||
}
|
||||
// TODO : ... m_system.trace();
|
||||
return true;
|
||||
}
|
||||
bool exist(const etk::String& _value) {
|
||||
return m_system.exist(_value);
|
||||
}
|
||||
bool isBoolean(const etk::String& _value) {
|
||||
if (m_system.exist(_value) == false) {
|
||||
return false;
|
||||
}
|
||||
return m_system.getScriptVariable(_value)->isBoolean();
|
||||
}
|
||||
bool isInteger32(const etk::String& _value) {
|
||||
if (m_system.exist(_value) == false) {
|
||||
return false;
|
||||
}
|
||||
return m_system.getScriptVariable(_value)->isInteger32();
|
||||
}
|
||||
bool isInteger64(const etk::String& _value) {
|
||||
if (m_system.exist(_value) == false) {
|
||||
return false;
|
||||
}
|
||||
return m_system.getScriptVariable(_value)->isInteger64();
|
||||
}
|
||||
bool isFloat(const etk::String& _value) {
|
||||
if (m_system.exist(_value) == false) {
|
||||
return false;
|
||||
}
|
||||
return m_system.getScriptVariable(_value)->isFloat32();
|
||||
}
|
||||
bool isDouble(const etk::String& _value) {
|
||||
if (m_system.exist(_value) == false) {
|
||||
return false;
|
||||
}
|
||||
return m_system.getScriptVariable(_value)->isFloat64();
|
||||
}
|
||||
bool isString(const etk::String& _value) {
|
||||
if (m_system.exist(_value) == false) {
|
||||
return false;
|
||||
}
|
||||
return m_system.getScriptVariable(_value)->isString();
|
||||
}
|
||||
bool isFunction(const etk::String& _value) {
|
||||
if (m_system.exist(_value) == false) {
|
||||
return false;
|
||||
}
|
||||
return m_system.getScriptVariable(_value)->isFunction();
|
||||
}
|
||||
bool isObject(const etk::String& _value) {
|
||||
if (m_system.exist(_value) == false) {
|
||||
return false;
|
||||
}
|
||||
return m_system.getScriptVariable(_value)->isObject();
|
||||
}
|
||||
bool isArray(const etk::String& _value) {
|
||||
if (m_system.exist(_value) == false) {
|
||||
return false;
|
||||
}
|
||||
return m_system.getScriptVariable(_value)->isArray();
|
||||
}
|
||||
bool isFunionNative(const etk::String& _value) {
|
||||
if (m_system.exist(_value) == false) {
|
||||
return false;
|
||||
}
|
||||
return m_system.getScriptVariable(_value)->isFunctionNative();
|
||||
}
|
||||
bool isUndefined(const etk::String& _value) {
|
||||
if (m_system.exist(_value) == false) {
|
||||
return false;
|
||||
}
|
||||
return m_system.getScriptVariable(_value)->isUndefined();
|
||||
}
|
||||
bool isNull(const etk::String& _value) {
|
||||
if (m_system.exist(_value) == false) {
|
||||
return false;
|
||||
}
|
||||
return m_system.getScriptVariable(_value)->isNull();
|
||||
}
|
||||
bool getBoolean(const etk::String& _value) {
|
||||
if (m_system.exist(_value) == false) {
|
||||
return false;
|
||||
}
|
||||
return m_system.getScriptVariable(_value)->toBoolean()->get();
|
||||
}
|
||||
int getInteger32(const etk::String& _value) {
|
||||
if (m_system.exist(_value) == false) {
|
||||
return 0;
|
||||
}
|
||||
return m_system.getScriptVariable(_value)->toInteger32()->get();
|
||||
}
|
||||
int getInteger64(const etk::String& _value) {
|
||||
if (m_system.exist(_value) == false) {
|
||||
return 0;
|
||||
}
|
||||
return m_system.getScriptVariable(_value)->toInteger64()->get();
|
||||
}
|
||||
double getFloat(const etk::String& _value) {
|
||||
if (m_system.exist(_value) == false) {
|
||||
return 0;
|
||||
}
|
||||
return m_system.getScriptVariable(_value)->toFloat32()->get();
|
||||
}
|
||||
double getDouble(const etk::String& _value) {
|
||||
if (m_system.exist(_value) == false) {
|
||||
return 0;
|
||||
}
|
||||
return m_system.getScriptVariable(_value)->toFloat64()->get();
|
||||
}
|
||||
etk::String getString(const etk::String& _value) {
|
||||
if (m_system.exist(_value) == false) {
|
||||
return "ERROR";
|
||||
}
|
||||
return m_system.getScriptVariable(_value)->toString()->get();
|
||||
}
|
||||
};
|
0
test/testStructure.cpp
Normal file
0
test/testStructure.cpp
Normal file
486
test/testVariable.cpp
Normal file
486
test/testVariable.cpp
Normal file
@ -0,0 +1,486 @@
|
||||
/**
|
||||
* @author Edouard DUPIN
|
||||
* @copyright 2017, Edouard DUPIN, all right reserved
|
||||
* @license MPL-2 (see license file)
|
||||
*/
|
||||
#include <test-debug/debug.hpp>
|
||||
#include <etest/etest.hpp>
|
||||
#include "testInterface.hpp"
|
||||
|
||||
|
||||
TEST(testVariable, noVariable) {
|
||||
testInterface system;
|
||||
bool ret = system.execute("// plop");
|
||||
EXPECT_EQ(ret, true);
|
||||
EXPECT_EQ(system.exist("result"), false);
|
||||
}
|
||||
|
||||
TEST(testVariable, declare_int) {
|
||||
testInterface system;
|
||||
bool ret = system.execute("variable int result;");
|
||||
EXPECT_EQ(ret, true);
|
||||
EXPECT_EQ(system.exist("result"), true);
|
||||
EXPECT_EQ(system.isInteger32("result"), true);
|
||||
}
|
||||
|
||||
|
||||
|
||||
TEST(testVariable, declare_int_init_1) {
|
||||
testInterface system;
|
||||
bool ret = system.execute("variable int result(55);");
|
||||
EXPECT_EQ(ret, true);
|
||||
EXPECT_EQ(system.exist("result"), true);
|
||||
EXPECT_EQ(system.isInteger32("result"), true);
|
||||
EXPECT_EQ(system.getInteger32("result"), 55);
|
||||
}
|
||||
|
||||
TEST(testVariable, declare_int_init_2) {
|
||||
testInterface system;
|
||||
bool ret = system.execute("variable int result = 55;");
|
||||
EXPECT_EQ(ret, true);
|
||||
EXPECT_EQ(system.exist("result"), true);
|
||||
EXPECT_EQ(system.isInteger32("result"), true);
|
||||
EXPECT_EQ(system.getInteger32("result"), 55);
|
||||
}
|
||||
|
||||
TEST(testVariable, declare_int32_init_1) {
|
||||
testInterface system;
|
||||
bool ret = system.execute("variable int32 result(55);");
|
||||
EXPECT_EQ(ret, true);
|
||||
EXPECT_EQ(system.exist("result"), true);
|
||||
EXPECT_EQ(system.isInteger32("result"), true);
|
||||
EXPECT_EQ(system.getInteger32("result"), 55);
|
||||
}
|
||||
|
||||
TEST(testVariable, declare_int32_init_2) {
|
||||
testInterface system;
|
||||
bool ret = system.execute("variable int32 result = 55;");
|
||||
EXPECT_EQ(ret, true);
|
||||
EXPECT_EQ(system.exist("result"), true);
|
||||
EXPECT_EQ(system.isInteger32("result"), true);
|
||||
EXPECT_EQ(system.getInteger32("result"), 55);
|
||||
}
|
||||
|
||||
TEST(testVariable, declare_int64_init_1) {
|
||||
testInterface system;
|
||||
bool ret = system.execute("variable int64 result(55);");
|
||||
EXPECT_EQ(ret, true);
|
||||
EXPECT_EQ(system.exist("result"), true);
|
||||
EXPECT_EQ(system.isInteger64("result"), true);
|
||||
EXPECT_EQ(system.getInteger64("result"), 55);
|
||||
}
|
||||
|
||||
TEST(testVariable, declare_int64_init_2) {
|
||||
testInterface system;
|
||||
bool ret = system.execute("variable int64 result = 55;");
|
||||
EXPECT_EQ(ret, true);
|
||||
EXPECT_EQ(system.exist("result"), true);
|
||||
EXPECT_EQ(system.isInteger64("result"), true);
|
||||
EXPECT_EQ(system.getInteger64("result"), 55);
|
||||
}
|
||||
|
||||
|
||||
TEST(testVariable, boolean_true_init_1) {
|
||||
testInterface system;
|
||||
bool ret = system.execute("variable bool result(true);");
|
||||
EXPECT_EQ(ret, true);
|
||||
EXPECT_EQ(system.exist("result"), true);
|
||||
EXPECT_EQ(system.isBoolean("result"), true);
|
||||
EXPECT_EQ(system.getBoolean("result"), true);
|
||||
}
|
||||
TEST(testVariable, boolean_true_init_2) {
|
||||
testInterface system;
|
||||
bool ret = system.execute("variable bool result = true;");
|
||||
EXPECT_EQ(ret, true);
|
||||
EXPECT_EQ(system.exist("result"), true);
|
||||
EXPECT_EQ(system.isBoolean("result"), true);
|
||||
EXPECT_EQ(system.getBoolean("result"), true);
|
||||
}
|
||||
|
||||
TEST(testVariable, boolean_false_init_1) {
|
||||
testInterface system;
|
||||
bool ret = system.execute("variable bool result(false);");
|
||||
EXPECT_EQ(ret, true);
|
||||
EXPECT_EQ(system.exist("result"), true);
|
||||
EXPECT_EQ(system.isBoolean("result"), true);
|
||||
EXPECT_EQ(system.getBoolean("result"), false);
|
||||
}
|
||||
|
||||
TEST(testVariable, boolean_false_init_2) {
|
||||
testInterface system;
|
||||
bool ret = system.execute("variable bool result = false;");
|
||||
EXPECT_EQ(ret, true);
|
||||
EXPECT_EQ(system.exist("result"), true);
|
||||
EXPECT_EQ(system.isBoolean("result"), true);
|
||||
EXPECT_EQ(system.getBoolean("result"), false);
|
||||
}
|
||||
|
||||
TEST(testVariable, integer_min_int32) {
|
||||
testInterface system;
|
||||
bool ret = system.execute("variable int32 result = -2147483648;");
|
||||
EXPECT_EQ(ret, true);
|
||||
EXPECT_EQ(system.exist("result"), true);
|
||||
EXPECT_EQ(system.isInteger32("result"), true);
|
||||
EXPECT_EQ(system.getInteger32("result"), -2147483648);
|
||||
}
|
||||
|
||||
TEST(testVariable, integer_max_int32) {
|
||||
testInterface system;
|
||||
bool ret = system.execute("variable int32 result = 2147483647;");
|
||||
EXPECT_EQ(ret, true);
|
||||
EXPECT_EQ(system.exist("result"), true);
|
||||
EXPECT_EQ(system.isInteger32("result"), true);
|
||||
EXPECT_EQ(system.getInteger32("result"), 2147483647);
|
||||
}
|
||||
|
||||
TEST(testVariable, integer_hexadecmal_1) {
|
||||
testInterface system;
|
||||
bool ret = system.execute("variable int32 result = 0x12345678;");
|
||||
EXPECT_EQ(ret, true);
|
||||
EXPECT_EQ(system.exist("result"), true);
|
||||
EXPECT_EQ(system.isInteger32("result"), true);
|
||||
EXPECT_EQ(system.getInteger32("result"), 0x12345678);
|
||||
}
|
||||
|
||||
TEST(testVariable, integer_hexadecmal_2) {
|
||||
testInterface system;
|
||||
bool ret = system.execute("variable int32 result = 0xABCDEF00;");
|
||||
EXPECT_EQ(ret, true);
|
||||
EXPECT_EQ(system.exist("result"), true);
|
||||
EXPECT_EQ(system.isInteger32("result"), true);
|
||||
EXPECT_EQ(system.getInteger32("result"), 0xABCDEF00);
|
||||
}
|
||||
|
||||
TEST(testVariable, integer_hexadecmal_3) {
|
||||
testInterface system;
|
||||
bool ret = system.execute("variable int32 result = -0x5;");
|
||||
EXPECT_EQ(ret, true);
|
||||
EXPECT_EQ(system.exist("result"), true);
|
||||
EXPECT_EQ(system.isInteger32("result"), true);
|
||||
EXPECT_EQ(system.getInteger32("result"), -5);
|
||||
}
|
||||
TEST(testVariable, integer_hexadecmal_4) {
|
||||
testInterface system;
|
||||
bool ret = system.execute("variable int32 result = 0xFFFFFFFF;");
|
||||
EXPECT_EQ(ret, true);
|
||||
EXPECT_EQ(system.exist("result"), true);
|
||||
EXPECT_EQ(system.isInteger32("result"), true);
|
||||
EXPECT_EQ(system.getInteger32("result"), -1);
|
||||
}
|
||||
|
||||
|
||||
TEST(testVariable, integer_float32_0) {
|
||||
testInterface system;
|
||||
bool ret = system.execute("variable float32 result(0.32452);");
|
||||
EXPECT_EQ(ret, true);
|
||||
EXPECT_EQ(system.exist("result"), true);
|
||||
EXPECT_EQ(system.isFloat("result"), true);
|
||||
EXPECT_EQ(system.getFloat("result"), 0.32452);
|
||||
}
|
||||
|
||||
TEST(testVariable, integer_float32_1) {
|
||||
testInterface system;
|
||||
bool ret = system.execute("variable float32 result = 0.32452;");
|
||||
EXPECT_EQ(ret, true);
|
||||
EXPECT_EQ(system.exist("result"), true);
|
||||
EXPECT_EQ(system.isFloat("result"), true);
|
||||
EXPECT_EQ(system.getFloat("result"), 0.32452);
|
||||
}
|
||||
|
||||
TEST(testVariable, integer_float32_2) {
|
||||
testInterface system;
|
||||
bool ret = system.execute("variable float32 result = -992345.23;");
|
||||
EXPECT_EQ(ret, true);
|
||||
EXPECT_EQ(system.exist("result"), true);
|
||||
EXPECT_EQ(system.isDouble("result"), true);
|
||||
EXPECT_EQ(system.getDouble("result"), -992345.23);
|
||||
}
|
||||
|
||||
TEST(testVariable, integer_float32_3) {
|
||||
testInterface system;
|
||||
bool ret = system.execute("variable float32 result = 3.45e2;");
|
||||
EXPECT_EQ(ret, true);
|
||||
EXPECT_EQ(system.exist("result"), true);
|
||||
EXPECT_EQ(system.isDouble("result"), true);
|
||||
EXPECT_EQ(system.getDouble("result"), 3.45e2);
|
||||
}
|
||||
|
||||
TEST(testVariable, integer_float_1) {
|
||||
testInterface system;
|
||||
bool ret = system.execute("variable float result = 3.45e2;");
|
||||
EXPECT_EQ(ret, true);
|
||||
EXPECT_EQ(system.exist("result"), true);
|
||||
EXPECT_EQ(system.isDouble("result"), true);
|
||||
EXPECT_EQ(system.getDouble("result"), 3.45e2);
|
||||
}
|
||||
|
||||
|
||||
|
||||
TEST(testVariable, integer_float64_0) {
|
||||
testInterface system;
|
||||
bool ret = system.execute("variable float64 result(0.32452);");
|
||||
EXPECT_EQ(ret, true);
|
||||
EXPECT_EQ(system.exist("result"), true);
|
||||
EXPECT_EQ(system.isDouble("result"), true);
|
||||
EXPECT_EQ(system.getDouble("result"), 0.32452);
|
||||
}
|
||||
TEST(testVariable, integer_float64_1) {
|
||||
testInterface system;
|
||||
bool ret = system.execute("variable float64 result = 0.32452;");
|
||||
EXPECT_EQ(ret, true);
|
||||
EXPECT_EQ(system.exist("result"), true);
|
||||
EXPECT_EQ(system.isDouble("result"), true);
|
||||
EXPECT_EQ(system.getDouble("result"), 0.32452);
|
||||
}
|
||||
|
||||
TEST(testVariable, integer_float64_2) {
|
||||
testInterface system;
|
||||
bool ret = system.execute("variable float64 result = -992345.23;");
|
||||
EXPECT_EQ(ret, true);
|
||||
EXPECT_EQ(system.exist("result"), true);
|
||||
EXPECT_EQ(system.isDouble("result"), true);
|
||||
EXPECT_EQ(system.getDouble("result"), -992345.23);
|
||||
}
|
||||
|
||||
TEST(testVariable, integer_float64_3) {
|
||||
testInterface system;
|
||||
bool ret = system.execute("variable float64 result = 3.45e2;");
|
||||
EXPECT_EQ(ret, true);
|
||||
EXPECT_EQ(system.exist("result"), true);
|
||||
EXPECT_EQ(system.isDouble("result"), true);
|
||||
EXPECT_EQ(system.getDouble("result"), 3.45e2);
|
||||
}
|
||||
|
||||
TEST(testVariable, integer_octal_1) {
|
||||
testInterface system;
|
||||
bool ret = system.execute("variable int32 result = 0377;");
|
||||
EXPECT_EQ(ret, true);
|
||||
EXPECT_EQ(system.exist("result"), true);
|
||||
EXPECT_EQ(system.isInteger32("result"), true);
|
||||
EXPECT_EQ(system.getInteger32("result"), 255);
|
||||
}
|
||||
/*
|
||||
TEST(testVariable, null_element) {
|
||||
testInterface system;
|
||||
bool ret = system.execute("variable int32 result = null;");
|
||||
EXPECT_EQ(ret, true);
|
||||
EXPECT_EQ(system.exist("result"), true);
|
||||
EXPECT_EQ(system.isNull("result"), true);
|
||||
}
|
||||
*/
|
||||
|
||||
TEST(testVariable, string_0) {
|
||||
testInterface system;
|
||||
bool ret = system.execute("variable string result(\"hello\");");
|
||||
EXPECT_EQ(ret, true);
|
||||
EXPECT_EQ(system.exist("result"), true);
|
||||
EXPECT_EQ(system.isString("result"), true);
|
||||
EXPECT_EQ(system.getString("result"), "hello");
|
||||
}
|
||||
TEST(testVariable, string_1) {
|
||||
testInterface system;
|
||||
bool ret = system.execute("variable string result = \"hello\";");
|
||||
EXPECT_EQ(ret, true);
|
||||
EXPECT_EQ(system.exist("result"), true);
|
||||
EXPECT_EQ(system.isString("result"), true);
|
||||
EXPECT_EQ(system.getString("result"), "hello");
|
||||
}
|
||||
|
||||
TEST(testVariable, string_2) {
|
||||
testInterface system;
|
||||
bool ret = system.execute("variable string result = 'hello';");
|
||||
EXPECT_EQ(ret, true);
|
||||
EXPECT_EQ(system.exist("result"), true);
|
||||
EXPECT_EQ(system.isString("result"), true);
|
||||
EXPECT_EQ(system.getString("result"), "hello");
|
||||
}
|
||||
|
||||
TEST(testVariable, string_1_special_element) {
|
||||
testInterface system;
|
||||
bool ret = system.execute("variable string result = \"h\\\\n\\\\r\\\"'ello\";");
|
||||
//TEST_INFO(" '" << system.getString("result") << "'");
|
||||
EXPECT_EQ(ret, true);
|
||||
EXPECT_EQ(system.exist("result"), true);
|
||||
EXPECT_EQ(system.isString("result"), true);
|
||||
EXPECT_EQ(system.getString("result"), "h\\n\\r\"'ello");
|
||||
}
|
||||
|
||||
TEST(testVariable, string_2_special_element) {
|
||||
testInterface system;
|
||||
bool ret = system.execute("variable string result = 'h\\\\n\\\\r\"\\\'ello';");
|
||||
//TEST_INFO(" '" << system.getString("result") << "'");
|
||||
EXPECT_EQ(ret, true);
|
||||
EXPECT_EQ(system.exist("result"), true);
|
||||
EXPECT_EQ(system.isString("result"), true);
|
||||
EXPECT_EQ(system.getString("result"), "h\\n\\r\"'ello");
|
||||
}
|
||||
|
||||
|
||||
//////////////////////////////////////////////////////
|
||||
// undefined
|
||||
//////////////////////////////////////////////////////
|
||||
/*
|
||||
TEST(testVariable, undef_1) {
|
||||
testInterface system;
|
||||
bool ret = system.execute("variable result;");
|
||||
//TEST_INFO(" '" << system.getString("result") << "'");
|
||||
EXPECT_EQ(ret, true);
|
||||
EXPECT_EQ(system.exist("result"), true);
|
||||
EXPECT_EQ(system.isUndefined("result"), true);
|
||||
}
|
||||
TEST(testVariable, undef_2) {
|
||||
testInterface system;
|
||||
bool ret = system.execute("variable result; if ((\"\"+result) != \"undefined\") result=1;");
|
||||
//TEST_INFO(" '" << system.getString("result") << "'");
|
||||
EXPECT_EQ(ret, true);
|
||||
EXPECT_EQ(system.exist("result"), true);
|
||||
EXPECT_EQ(system.isUndefined("result"), true);
|
||||
}
|
||||
|
||||
TEST(testVariable, undef_property) {
|
||||
testInterface system;
|
||||
bool ret = system.execute("variable result={}; if ((\"\"+result.noProperty) != \"undefined\") result=1;");
|
||||
//TEST_INFO(" '" << system.getString("result") << "'");
|
||||
EXPECT_EQ(ret, true);
|
||||
EXPECT_EQ(system.exist("result"), true);
|
||||
EXPECT_EQ(system.isInteger32("result"), false);
|
||||
}
|
||||
|
||||
TEST(testVariable, undef_to_null) {
|
||||
testInterface system;
|
||||
bool ret = system.execute("variable result; if (result == null) result=1;");
|
||||
//TEST_INFO(" '" << system.getString("result") << "'");
|
||||
EXPECT_EQ(ret, true);
|
||||
EXPECT_EQ(system.exist("result"), true);
|
||||
EXPECT_EQ(system.isInteger32("result"), true);
|
||||
}
|
||||
|
||||
TEST(testVariable, undef_to_null_type) {
|
||||
testInterface system;
|
||||
bool ret = system.execute("variable result; if (result === null) result=1;");
|
||||
//TEST_INFO(" '" << system.getString("result") << "'");
|
||||
EXPECT_EQ(ret, true);
|
||||
EXPECT_EQ(system.exist("result"), true);
|
||||
EXPECT_EQ(system.isInteger32("result"), false);
|
||||
}
|
||||
*/
|
||||
//////////////////////////////////////////////////////
|
||||
// null
|
||||
//////////////////////////////////////////////////////
|
||||
/*
|
||||
TEST(testVariable, undefined_and_null_1) {
|
||||
testInterface system;
|
||||
bool ret = system.execute("variable result = 0; if (undefined == null) result=1;");
|
||||
EXPECT_EQ(ret, true);
|
||||
EXPECT_EQ(system.exist("result"), true);
|
||||
EXPECT_EQ(system.isInteger32("result"), true);
|
||||
EXPECT_EQ(system.getInteger32("result"), 1);
|
||||
}
|
||||
TEST(testVariable, undefined_and_null_2) {
|
||||
testInterface system;
|
||||
bool ret = system.execute("variable result = 0; if (undefined === null) result=1;");
|
||||
EXPECT_EQ(ret, true);
|
||||
EXPECT_EQ(system.exist("result"), true);
|
||||
EXPECT_EQ(system.isInteger32("result"), true);
|
||||
EXPECT_EQ(system.getInteger32("result"), 0);
|
||||
}
|
||||
TEST(testVariable, undefined_and_null_3) {
|
||||
testInterface system;
|
||||
bool ret = system.execute("variable result = 0; if (null != undefined) result=1;");
|
||||
EXPECT_EQ(ret, true);
|
||||
EXPECT_EQ(system.exist("result"), true);
|
||||
EXPECT_EQ(system.isInteger32("result"), true);
|
||||
EXPECT_EQ(system.getInteger32("result"), 0);
|
||||
}
|
||||
|
||||
TEST(testVariable, undefined_and_null_4) {
|
||||
testInterface system;
|
||||
bool ret = system.execute("variable result = 0; if (null === undefined) result=1;");
|
||||
EXPECT_EQ(ret, true);
|
||||
EXPECT_EQ(system.exist("result"), true);
|
||||
EXPECT_EQ(system.isInteger32("result"), true);
|
||||
EXPECT_EQ(system.getInteger32("result"), 0);
|
||||
}
|
||||
*/
|
||||
|
||||
//////////////////////////////////////////////////////
|
||||
// array
|
||||
//////////////////////////////////////////////////////
|
||||
/*
|
||||
TEST(testVariable, array_declare_1) {
|
||||
testInterface system;
|
||||
bool ret = system.execute("variable result = [];");
|
||||
EXPECT_EQ(ret, true);
|
||||
EXPECT_EQ(system.exist("result"), true);
|
||||
EXPECT_EQ(system.isArray("result"), true);
|
||||
}
|
||||
|
||||
TEST(testVariable, array_declare_2) {
|
||||
testInterface system;
|
||||
bool ret = system.execute("variable result = [10,11,12]; variable a=result[0]; variable b=result[2];");
|
||||
EXPECT_EQ(ret, true);
|
||||
EXPECT_EQ(system.exist("result"), true);
|
||||
EXPECT_EQ(system.isArray("result"), true);
|
||||
EXPECT_EQ(system.exist("a"), true);
|
||||
EXPECT_EQ(system.isInteger32("a"), true);
|
||||
EXPECT_EQ(system.getInteger32("a"), 10);
|
||||
EXPECT_EQ(system.exist("b"), true);
|
||||
EXPECT_EQ(system.isInteger32("b"), true);
|
||||
EXPECT_EQ(system.getInteger32("b"), 12);
|
||||
}
|
||||
|
||||
TEST(testVariable, array_declare_polymorph) {
|
||||
testInterface system;
|
||||
bool ret = system.execute("variable result = [10,11.55,\"hello\"]; variable a=result[0]; variable b=result[1]; variable c=result[2];");
|
||||
EXPECT_EQ(ret, true);
|
||||
EXPECT_EQ(system.exist("result"), true);
|
||||
EXPECT_EQ(system.isArray("result"), true);
|
||||
EXPECT_EQ(system.exist("a"), true);
|
||||
EXPECT_EQ(system.isInteger32("a"), true);
|
||||
EXPECT_EQ(system.getInteger32("a"), 10);
|
||||
EXPECT_EQ(system.exist("b"), true);
|
||||
EXPECT_EQ(system.isDouble("b"), true);
|
||||
EXPECT_EQ(system.getDouble("b"), 11.55);
|
||||
EXPECT_EQ(system.exist("c"), true);
|
||||
EXPECT_EQ(system.isString("c"), true);
|
||||
EXPECT_EQ(system.getString("c"), "hello");
|
||||
}
|
||||
|
||||
|
||||
|
||||
TEST(testVariable, array_asign) {
|
||||
testInterface system;
|
||||
bool ret = system.execute("variable result = [10,11]; result[0] = 5; result[5] = 99; variable a=result[0]; variable b=result[5]; variable c=result[1]; result[1] = 4;");
|
||||
EXPECT_EQ(ret, true);
|
||||
EXPECT_EQ(system.exist("result"), true);
|
||||
EXPECT_EQ(system.isArray("result"), true);
|
||||
EXPECT_EQ(system.exist("a"), true);
|
||||
EXPECT_EQ(system.isInteger32("a"), true);
|
||||
EXPECT_EQ(system.getInteger32("a"), 5);
|
||||
EXPECT_EQ(system.exist("b"), true);
|
||||
EXPECT_EQ(system.isInteger32("b"), true);
|
||||
EXPECT_EQ(system.getInteger32("b"), 99);
|
||||
EXPECT_EQ(system.exist("c"), true);
|
||||
EXPECT_EQ(system.isInteger32("c"), true);
|
||||
EXPECT_EQ(system.getInteger32("c"), 11);
|
||||
}
|
||||
|
||||
|
||||
TEST(testVariable, array_asign_reference) {
|
||||
testInterface system;
|
||||
bool ret = system.execute("variable result = [10,11]; variable result2=result; result2[0] = 5; variable a=result[0];");
|
||||
EXPECT_EQ(ret, true);
|
||||
EXPECT_EQ(system.exist("result"), true);
|
||||
EXPECT_EQ(system.isArray("result"), true);
|
||||
EXPECT_EQ(system.exist("a"), true);
|
||||
EXPECT_EQ(system.isInteger32("a"), true);
|
||||
EXPECT_EQ(system.getInteger32("a"), 5);
|
||||
}
|
||||
*/
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
230
test/testVariableComparaison.cpp
Normal file
230
test/testVariableComparaison.cpp
Normal file
@ -0,0 +1,230 @@
|
||||
/**
|
||||
* @author Edouard DUPIN
|
||||
* @copyright 2017, Edouard DUPIN, all right reserved
|
||||
* @license MPL-2 (see license file)
|
||||
*/
|
||||
#include <test-debug/debug.hpp>
|
||||
#include <etest/etest.hpp>
|
||||
#include "testInterface.hpp"
|
||||
|
||||
|
||||
///////////////////////////////////////////////
|
||||
// BOOLEAN
|
||||
///////////////////////////////////////////////
|
||||
|
||||
TEST(testVariableCompare, boolean_equal_1) {
|
||||
testInterface system;
|
||||
bool ret = system.execute("variable a = true;\nvariable b = false;\nvariable result = a == b;");
|
||||
EXPECT_EQ(ret, true);
|
||||
EXPECT_EQ(system.exist("result"), true);
|
||||
EXPECT_EQ(system.isBoolean("result"), true);
|
||||
EXPECT_EQ(system.getBoolean("result"), false);
|
||||
}
|
||||
|
||||
TEST(testVariableCompare, boolean_equal_2) {
|
||||
testInterface system;
|
||||
bool ret = system.execute("variable a = true;\nvariable b = true;\nvariable result = a == b;");
|
||||
EXPECT_EQ(ret, true);
|
||||
EXPECT_EQ(system.exist("result"), true);
|
||||
EXPECT_EQ(system.isBoolean("result"), true);
|
||||
EXPECT_EQ(system.getBoolean("result"), true);
|
||||
}
|
||||
|
||||
TEST(testVariableCompare, boolean_not_equal_1) {
|
||||
testInterface system;
|
||||
bool ret = system.execute("variable a = true;\nvariable b = false;\nvariable result = a != b;");
|
||||
EXPECT_EQ(ret, true);
|
||||
EXPECT_EQ(system.exist("result"), true);
|
||||
EXPECT_EQ(system.isBoolean("result"), true);
|
||||
EXPECT_EQ(system.getBoolean("result"), true);
|
||||
}
|
||||
|
||||
TEST(testVariableCompare, boolean_not_equal_2) {
|
||||
testInterface system;
|
||||
bool ret = system.execute("variable a = true;\nvariable b = true;\nvariable result = a != b;");
|
||||
EXPECT_EQ(ret, true);
|
||||
EXPECT_EQ(system.exist("result"), true);
|
||||
EXPECT_EQ(system.isBoolean("result"), true);
|
||||
EXPECT_EQ(system.getBoolean("result"), false);
|
||||
}
|
||||
|
||||
TEST(testVariableCompare, boolean_equal_unexisting) {
|
||||
testInterface system;
|
||||
bool ret = system.execute("variable a = true;\nvariable result = a == b;");
|
||||
TEST_TODO("NOT implemented");
|
||||
EXPECT_EQ(ret, false); // ==> must have an error on the parsing/execution
|
||||
EXPECT_EQ(system.exist("result"), true);
|
||||
EXPECT_EQ(system.isBoolean("result"), true);
|
||||
EXPECT_EQ(system.getBoolean("result"), false);
|
||||
}
|
||||
|
||||
///////////////////////////////////////////////
|
||||
// INTEGER
|
||||
///////////////////////////////////////////////
|
||||
|
||||
// ==
|
||||
|
||||
TEST(testVariableCompare, integer_equal_1) {
|
||||
testInterface system;
|
||||
bool ret = system.execute("variable a = 45;\nvariable b = 55;\nvariable result = a == b;");
|
||||
EXPECT_EQ(ret, true);
|
||||
EXPECT_EQ(system.exist("result"), true);
|
||||
EXPECT_EQ(system.isBoolean("result"), true);
|
||||
EXPECT_EQ(system.getBoolean("result"), false);
|
||||
}
|
||||
|
||||
TEST(testVariableCompare, integer_equal_2) {
|
||||
testInterface system;
|
||||
bool ret = system.execute("variable a = 45;\nvariable b = 45;\nvariable result = a == b;");
|
||||
EXPECT_EQ(ret, true);
|
||||
EXPECT_EQ(system.exist("result"), true);
|
||||
EXPECT_EQ(system.isBoolean("result"), true);
|
||||
EXPECT_EQ(system.getBoolean("result"), true);
|
||||
}
|
||||
|
||||
// !=
|
||||
|
||||
TEST(testVariableCompare, integer_not_equal_1) {
|
||||
testInterface system;
|
||||
bool ret = system.execute("variable a = 234;\nvariable b = 345;\nvariable result = a != b;");
|
||||
EXPECT_EQ(ret, true);
|
||||
EXPECT_EQ(system.exist("result"), true);
|
||||
EXPECT_EQ(system.isBoolean("result"), true);
|
||||
EXPECT_EQ(system.getBoolean("result"), true);
|
||||
}
|
||||
|
||||
TEST(testVariableCompare, integer_not_equal_2) {
|
||||
testInterface system;
|
||||
bool ret = system.execute("variable a = 567;\nvariable b = 567;\nvariable result = a != b;");
|
||||
EXPECT_EQ(ret, true);
|
||||
EXPECT_EQ(system.exist("result"), true);
|
||||
EXPECT_EQ(system.isBoolean("result"), true);
|
||||
EXPECT_EQ(system.getBoolean("result"), false);
|
||||
}
|
||||
|
||||
TEST(testVariableCompare, integer_not_equal_3) {
|
||||
testInterface system;
|
||||
bool ret = system.execute("variable a = 567;\nvariable b = 567;\nvariable result = !(a == b);");
|
||||
EXPECT_EQ(ret, true);
|
||||
EXPECT_EQ(system.exist("result"), true);
|
||||
EXPECT_EQ(system.isBoolean("result"), true);
|
||||
EXPECT_EQ(system.getBoolean("result"), false);
|
||||
}
|
||||
|
||||
// <
|
||||
|
||||
TEST(testVariableCompare, integer_less_1) {
|
||||
testInterface system;
|
||||
bool ret = system.execute("variable a = 45;\nvariable b = 55;\nvariable result = a < b;");
|
||||
EXPECT_EQ(ret, true);
|
||||
EXPECT_EQ(system.exist("result"), true);
|
||||
EXPECT_EQ(system.isBoolean("result"), true);
|
||||
EXPECT_EQ(system.getBoolean("result"), true);
|
||||
}
|
||||
|
||||
TEST(testVariableCompare, integer_less_2) {
|
||||
testInterface system;
|
||||
bool ret = system.execute("variable a = 45;\nvariable b = -5;\nvariable result = a < b;");
|
||||
EXPECT_EQ(ret, true);
|
||||
EXPECT_EQ(system.exist("result"), true);
|
||||
EXPECT_EQ(system.isBoolean("result"), true);
|
||||
EXPECT_EQ(system.getBoolean("result"), false);
|
||||
}
|
||||
|
||||
TEST(testVariableCompare, integer_less_3) {
|
||||
testInterface system;
|
||||
bool ret = system.execute("variable a = 45;\nvariable b = 45;\nvariable result = a < b;");
|
||||
EXPECT_EQ(ret, true);
|
||||
EXPECT_EQ(system.exist("result"), true);
|
||||
EXPECT_EQ(system.isBoolean("result"), true);
|
||||
EXPECT_EQ(system.getBoolean("result"), false);
|
||||
}
|
||||
|
||||
// <=
|
||||
|
||||
TEST(testVariableCompare, integer_less_equal_1) {
|
||||
testInterface system;
|
||||
bool ret = system.execute("variable a = 45;\nvariable b = 55;\nvariable result = a <= b;");
|
||||
EXPECT_EQ(ret, true);
|
||||
EXPECT_EQ(system.exist("result"), true);
|
||||
EXPECT_EQ(system.isBoolean("result"), true);
|
||||
EXPECT_EQ(system.getBoolean("result"), true);
|
||||
}
|
||||
|
||||
TEST(testVariableCompare, integer_less_equal_2) {
|
||||
testInterface system;
|
||||
bool ret = system.execute("variable a = 45;\nvariable b = -5;\nvariable result = a <= b;");
|
||||
EXPECT_EQ(ret, true);
|
||||
EXPECT_EQ(system.exist("result"), true);
|
||||
EXPECT_EQ(system.isBoolean("result"), true);
|
||||
EXPECT_EQ(system.getBoolean("result"), false);
|
||||
}
|
||||
|
||||
TEST(testVariableCompare, integer_less_equal_3) {
|
||||
testInterface system;
|
||||
bool ret = system.execute("variable a = 45;\nvariable b = 45;\nvariable result = a <= b;");
|
||||
EXPECT_EQ(ret, true);
|
||||
EXPECT_EQ(system.exist("result"), true);
|
||||
EXPECT_EQ(system.isBoolean("result"), true);
|
||||
EXPECT_EQ(system.getBoolean("result"), true);
|
||||
}
|
||||
|
||||
|
||||
// >
|
||||
|
||||
TEST(testVariableCompare, integer_greater_1) {
|
||||
testInterface system;
|
||||
bool ret = system.execute("variable a = 45;\nvariable b = 55;\nvariable result = a > b;");
|
||||
EXPECT_EQ(ret, true);
|
||||
EXPECT_EQ(system.exist("result"), true);
|
||||
EXPECT_EQ(system.isBoolean("result"), true);
|
||||
EXPECT_EQ(system.getBoolean("result"), false);
|
||||
}
|
||||
|
||||
TEST(testVariableCompare, integer_greater_2) {
|
||||
testInterface system;
|
||||
bool ret = system.execute("variable a = 45;\nvariable b = -5;\nvariable result = a > b;");
|
||||
EXPECT_EQ(ret, true);
|
||||
EXPECT_EQ(system.exist("result"), true);
|
||||
EXPECT_EQ(system.isBoolean("result"), true);
|
||||
EXPECT_EQ(system.getBoolean("result"), true);
|
||||
}
|
||||
|
||||
TEST(testVariableCompare, integer_greater_3) {
|
||||
testInterface system;
|
||||
bool ret = system.execute("variable a = 45;\nvariable b = 45;\nvariable result = a > b;");
|
||||
EXPECT_EQ(ret, true);
|
||||
EXPECT_EQ(system.exist("result"), true);
|
||||
EXPECT_EQ(system.isBoolean("result"), true);
|
||||
EXPECT_EQ(system.getBoolean("result"), false);
|
||||
}
|
||||
|
||||
// >=
|
||||
|
||||
TEST(testVariableCompare, integer_greater_equal_1) {
|
||||
testInterface system;
|
||||
bool ret = system.execute("variable a = 45;\nvariable b = 55;\nvariable result = a >= b;");
|
||||
EXPECT_EQ(ret, true);
|
||||
EXPECT_EQ(system.exist("result"), true);
|
||||
EXPECT_EQ(system.isBoolean("result"), true);
|
||||
EXPECT_EQ(system.getBoolean("result"), false);
|
||||
}
|
||||
|
||||
TEST(testVariableCompare, integer_greater_equal_2) {
|
||||
testInterface system;
|
||||
bool ret = system.execute("variable a = 45;\nvariable b = -5;\nvariable result = a >= b;");
|
||||
EXPECT_EQ(ret, true);
|
||||
EXPECT_EQ(system.exist("result"), true);
|
||||
EXPECT_EQ(system.isBoolean("result"), true);
|
||||
EXPECT_EQ(system.getBoolean("result"), true);
|
||||
}
|
||||
|
||||
TEST(testVariableCompare, integer_greater_equal_3) {
|
||||
testInterface system;
|
||||
bool ret = system.execute("variable a = 45;\nvariable b = 45;\nvariable result = a >= b;");
|
||||
EXPECT_EQ(ret, true);
|
||||
EXPECT_EQ(system.exist("result"), true);
|
||||
EXPECT_EQ(system.isBoolean("result"), true);
|
||||
EXPECT_EQ(system.getBoolean("result"), true);
|
||||
}
|
||||
|
45
test/testVariableScope.cpp
Normal file
45
test/testVariableScope.cpp
Normal file
@ -0,0 +1,45 @@
|
||||
/**
|
||||
* @author Edouard DUPIN
|
||||
* @copyright 2017, Edouard DUPIN, all right reserved
|
||||
* @license MPL-2 (see license file)
|
||||
*/
|
||||
#include <test-debug/debug.hpp>
|
||||
#include <etest/etest.hpp>
|
||||
#include "testInterface.hpp"
|
||||
|
||||
|
||||
TEST(testVariableScope, solo_block_local) {
|
||||
testInterface system;
|
||||
bool ret = system.execute("variable result = true; { variable result = false; };");
|
||||
EXPECT_EQ(ret, true);
|
||||
EXPECT_EQ(system.exist("result"), true);
|
||||
EXPECT_EQ(system.isBoolean("result"), true);
|
||||
EXPECT_EQ(system.getBoolean("result"), true);
|
||||
}
|
||||
|
||||
TEST(testVariableScope, solo_block_global) {
|
||||
testInterface system;
|
||||
bool ret = system.execute("variable result = true; { result = false; };");
|
||||
EXPECT_EQ(ret, true);
|
||||
EXPECT_EQ(system.exist("result"), true);
|
||||
EXPECT_EQ(system.isBoolean("result"), true);
|
||||
EXPECT_EQ(system.getBoolean("result"), false);
|
||||
}
|
||||
|
||||
TEST(testVariableScope, function_block_global) {
|
||||
testInterface system;
|
||||
bool ret = system.execute("variable result = true; variable call = function () { result = false; }; call();");
|
||||
EXPECT_EQ(ret, true);
|
||||
EXPECT_EQ(system.exist("result"), true);
|
||||
EXPECT_EQ(system.isBoolean("result"), true);
|
||||
EXPECT_EQ(system.getBoolean("result"), false);
|
||||
}
|
||||
|
||||
TEST(testVariableScope, function_block_local) {
|
||||
testInterface system;
|
||||
bool ret = system.execute("variable result = true; variable call = function () { variable result = false; }; call();");
|
||||
EXPECT_EQ(ret, true);
|
||||
EXPECT_EQ(system.exist("result"), true);
|
||||
EXPECT_EQ(system.isBoolean("result"), true);
|
||||
EXPECT_EQ(system.getBoolean("result"), true);
|
||||
}
|
1
version.txt
Normal file
1
version.txt
Normal file
@ -0,0 +1 @@
|
||||
0.1.0-dev
|
Loading…
Reference in New Issue
Block a user