mirror of
https://github.com/aportelli/LatAnalyze.git
synced 2025-04-11 03:20:46 +01:00
big refactoring for cleaner code conventions
This commit is contained in:
parent
967f1da061
commit
278bc59a33
2
INSTALL
2
INSTALL
@ -1,7 +1,7 @@
|
|||||||
Installation Instructions
|
Installation Instructions
|
||||||
*************************
|
*************************
|
||||||
|
|
||||||
Copyright (C) 1994-1996, 1999-2002, 2004-2012 Free Software Foundation,
|
Copyright (C) 1994-1996, 1999-2002, 2004-2013 Free Software Foundation,
|
||||||
Inc.
|
Inc.
|
||||||
|
|
||||||
Copying and distribution of this file, with or without modification,
|
Copying and distribution of this file, with or without modification,
|
||||||
|
4
build.sh
4
build.sh
@ -4,7 +4,7 @@ PREFIX=`cat Makefile | grep '^prefix =' | awk '{print $3}'`
|
|||||||
case $1 in
|
case $1 in
|
||||||
'')
|
'')
|
||||||
echo '-- building...'
|
echo '-- building...'
|
||||||
make -j3
|
make -j5
|
||||||
echo '-- installing...'
|
echo '-- installing...'
|
||||||
make uninstall 1>/dev/null
|
make uninstall 1>/dev/null
|
||||||
make install 1>/dev/null
|
make install 1>/dev/null
|
||||||
@ -15,7 +15,7 @@ case $1 in
|
|||||||
fi;;
|
fi;;
|
||||||
'clean')
|
'clean')
|
||||||
echo '-- cleaning...'
|
echo '-- cleaning...'
|
||||||
make -j3 clean;;
|
make -j5 clean;;
|
||||||
*)
|
*)
|
||||||
echo 'error: unknown action' 1>&2
|
echo 'error: unknown action' 1>&2
|
||||||
exit 1;;
|
exit 1;;
|
||||||
|
@ -2,7 +2,7 @@
|
|||||||
#include <latan/includes.hpp>
|
#include <latan/includes.hpp>
|
||||||
|
|
||||||
#ifndef ERR_PREF
|
#ifndef ERR_PREF
|
||||||
#define ERR_PREF "[" + Env::Name + " v" + Env::Version + "] "
|
#define ERR_PREF "[" + Env::name + " v" + Env::version + "] "
|
||||||
#endif
|
#endif
|
||||||
#ifndef ERR_SUFF
|
#ifndef ERR_SUFF
|
||||||
#define ERR_SUFF " (" + loc + ")"
|
#define ERR_SUFF " (" + loc + ")"
|
||||||
@ -24,7 +24,7 @@ CONST_EXC(Range,Logic("range error: "+msg,loc))
|
|||||||
// runtime errors
|
// runtime errors
|
||||||
CONST_EXC(Runtime,runtime_error(ERR_PREF+msg+ERR_SUFF))
|
CONST_EXC(Runtime,runtime_error(ERR_PREF+msg+ERR_SUFF))
|
||||||
CONST_EXC(Compilation,Runtime("compilation error: "+msg,loc))
|
CONST_EXC(Compilation,Runtime("compilation error: "+msg,loc))
|
||||||
CONST_EXC(IO,Runtime("IO error: "+msg,loc))
|
CONST_EXC(Io,Runtime("IO error: "+msg,loc))
|
||||||
CONST_EXC(Parsing,Runtime(msg,loc))
|
CONST_EXC(Parsing,Runtime(msg,loc))
|
||||||
CONST_EXC(Syntax,Runtime("syntax error: "+msg,loc))
|
CONST_EXC(Syntax,Runtime("syntax error: "+msg,loc))
|
||||||
|
|
||||||
|
@ -28,7 +28,7 @@ namespace Exceptions
|
|||||||
// runtime errors
|
// runtime errors
|
||||||
DECL_EXC(Runtime,std::runtime_error);
|
DECL_EXC(Runtime,std::runtime_error);
|
||||||
DECL_EXC(Compilation,Runtime);
|
DECL_EXC(Compilation,Runtime);
|
||||||
DECL_EXC(IO,Runtime);
|
DECL_EXC(Io,Runtime);
|
||||||
DECL_EXC(Parsing,Runtime);
|
DECL_EXC(Parsing,Runtime);
|
||||||
DECL_EXC(Syntax,Runtime);
|
DECL_EXC(Syntax,Runtime);
|
||||||
}
|
}
|
||||||
|
@ -4,6 +4,6 @@
|
|||||||
using namespace std;
|
using namespace std;
|
||||||
using namespace Latan;
|
using namespace Latan;
|
||||||
|
|
||||||
const string Env::FullName = PACKAGE_STRING;
|
const string Env::fullName = PACKAGE_STRING;
|
||||||
const string Env::Name = PACKAGE_NAME;
|
const string Env::name = PACKAGE_NAME;
|
||||||
const string Env::Version = PACKAGE_VERSION;
|
const string Env::version = PACKAGE_VERSION;
|
||||||
|
@ -19,9 +19,9 @@ LATAN_BEGIN_CPPDECL
|
|||||||
// Environment
|
// Environment
|
||||||
namespace Env
|
namespace Env
|
||||||
{
|
{
|
||||||
extern const std::string FullName;
|
extern const std::string fullName;
|
||||||
extern const std::string Name;
|
extern const std::string name;
|
||||||
extern const std::string Version;
|
extern const std::string version;
|
||||||
}
|
}
|
||||||
|
|
||||||
// string conversions
|
// string conversions
|
||||||
|
162
latan/IO.cpp
162
latan/IO.cpp
@ -1,162 +0,0 @@
|
|||||||
#include <latan/IO.hpp>
|
|
||||||
#include <latan/includes.hpp>
|
|
||||||
|
|
||||||
using namespace std;
|
|
||||||
using namespace Latan;
|
|
||||||
|
|
||||||
// ASCII data format Bison/Flex parser declaration
|
|
||||||
int _IOASCII_parse(ASCIIParserState* state);
|
|
||||||
|
|
||||||
/******************************************************************************
|
|
||||||
* File implementation *
|
|
||||||
******************************************************************************/
|
|
||||||
// constructors ////////////////////////////////////////////////////////////////
|
|
||||||
File::File(void)
|
|
||||||
: name(""), mode(FileMode::Null), data()
|
|
||||||
{}
|
|
||||||
|
|
||||||
File::File(const string init_name, const FileMode::Type init_mode)
|
|
||||||
: name(init_name), mode(init_mode), data()
|
|
||||||
{}
|
|
||||||
|
|
||||||
// destructor //////////////////////////////////////////////////////////////////
|
|
||||||
File::~File(void)
|
|
||||||
{}
|
|
||||||
|
|
||||||
// access //////////////////////////////////////////////////////////////////////
|
|
||||||
string File::Name(void) const
|
|
||||||
{
|
|
||||||
return name;
|
|
||||||
}
|
|
||||||
|
|
||||||
FileMode::Type File::Mode(void) const
|
|
||||||
{
|
|
||||||
return mode;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Internal functions //////////////////////////////////////////////////////////
|
|
||||||
void File::DeleteData(void)
|
|
||||||
{
|
|
||||||
IODataTable::iterator i;
|
|
||||||
|
|
||||||
for (i=data.begin();i!=data.end();++i)
|
|
||||||
{
|
|
||||||
delete i->second;
|
|
||||||
}
|
|
||||||
data.clear();
|
|
||||||
}
|
|
||||||
|
|
||||||
/******************************************************************************
|
|
||||||
* ASCIIParserState implementation *
|
|
||||||
******************************************************************************/
|
|
||||||
// constructor /////////////////////////////////////////////////////////////////
|
|
||||||
ASCIIParserState::ASCIIParserState(istream* pt_istream, string* pt_name,\
|
|
||||||
IODataTable* pt_data)
|
|
||||||
: ParserState<IODataTable>(pt_istream, pt_name, pt_data)
|
|
||||||
{
|
|
||||||
init_scanner();
|
|
||||||
}
|
|
||||||
|
|
||||||
// destructor //////////////////////////////////////////////////////////////////
|
|
||||||
ASCIIParserState::~ASCIIParserState(void)
|
|
||||||
{
|
|
||||||
destroy_scanner();
|
|
||||||
}
|
|
||||||
|
|
||||||
/******************************************************************************
|
|
||||||
* ASCIIFile implementation *
|
|
||||||
******************************************************************************/
|
|
||||||
// constructor /////////////////////////////////////////////////////////////////
|
|
||||||
ASCIIFile::ASCIIFile(void)
|
|
||||||
: File(), file_stream(), is_parsed(false), state(NULL)
|
|
||||||
{}
|
|
||||||
|
|
||||||
ASCIIFile::ASCIIFile(const string in_name, const FileMode::Type in_mode)
|
|
||||||
{
|
|
||||||
OpenASCII(in_name,in_mode);
|
|
||||||
}
|
|
||||||
|
|
||||||
// destructor //////////////////////////////////////////////////////////////////
|
|
||||||
ASCIIFile::~ASCIIFile(void)
|
|
||||||
{
|
|
||||||
Clear();
|
|
||||||
}
|
|
||||||
|
|
||||||
// tests ///////////////////////////////////////////////////////////////////////
|
|
||||||
bool ASCIIFile::IsOpen() const
|
|
||||||
{
|
|
||||||
return file_stream.is_open();
|
|
||||||
}
|
|
||||||
|
|
||||||
// IO //////////////////////////////////////////////////////////////////////////
|
|
||||||
void ASCIIFile::Close(void)
|
|
||||||
{
|
|
||||||
Clear();
|
|
||||||
}
|
|
||||||
|
|
||||||
void ASCIIFile::Open(const string new_name, const FileMode::Type new_mode)
|
|
||||||
{
|
|
||||||
if (IsOpen())
|
|
||||||
{
|
|
||||||
LATAN_ERROR(IO,"file already opened with name '"+name+"'");
|
|
||||||
}
|
|
||||||
OpenASCII(new_name,new_mode);
|
|
||||||
}
|
|
||||||
|
|
||||||
void ASCIIFile::Save(void)
|
|
||||||
{
|
|
||||||
LATAN_ERROR(Implementation,"saving ASCII files not implemented yet");
|
|
||||||
}
|
|
||||||
|
|
||||||
void ASCIIFile::SaveAs(const string new_name __dumb)
|
|
||||||
{
|
|
||||||
LATAN_ERROR(Implementation,"saving ASCII files not implemented yet");
|
|
||||||
}
|
|
||||||
|
|
||||||
// Internal functions //////////////////////////////////////////////////////////
|
|
||||||
void ASCIIFile::Clear()
|
|
||||||
{
|
|
||||||
DeleteData();
|
|
||||||
CloseASCII();
|
|
||||||
}
|
|
||||||
|
|
||||||
void ASCIIFile::OpenASCII(const string new_name, const FileMode::Type new_mode)
|
|
||||||
{
|
|
||||||
if (!IsOpen())
|
|
||||||
{
|
|
||||||
name = new_name;
|
|
||||||
mode = new_mode;
|
|
||||||
is_parsed = false;
|
|
||||||
file_stream.open(name.c_str());
|
|
||||||
if (mode & FileMode::Read)
|
|
||||||
{
|
|
||||||
state = new ASCIIParserState(&file_stream,&name,&data);
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
state = NULL;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
void ASCIIFile::CloseASCII(void)
|
|
||||||
{
|
|
||||||
if (state)
|
|
||||||
{
|
|
||||||
delete state;
|
|
||||||
state = NULL;
|
|
||||||
}
|
|
||||||
if (IsOpen())
|
|
||||||
{
|
|
||||||
file_stream.close();
|
|
||||||
}
|
|
||||||
name = "";
|
|
||||||
mode = FileMode::Null;
|
|
||||||
is_parsed = false;
|
|
||||||
}
|
|
||||||
|
|
||||||
void ASCIIFile::Parse()
|
|
||||||
{
|
|
||||||
_IOASCII_parse(state);
|
|
||||||
is_parsed = true;
|
|
||||||
}
|
|
154
latan/IO.hpp
154
latan/IO.hpp
@ -1,154 +0,0 @@
|
|||||||
#ifndef LATAN_IO_HPP_
|
|
||||||
#define LATAN_IO_HPP_
|
|
||||||
|
|
||||||
#include <fstream>
|
|
||||||
#include <map>
|
|
||||||
#include <stack>
|
|
||||||
#include <sstream>
|
|
||||||
#include <string>
|
|
||||||
#include <latan/Global.hpp>
|
|
||||||
#include <latan/IOObject.hpp>
|
|
||||||
#include <latan/Mat.hpp>
|
|
||||||
#include <latan/ParserState.hpp>
|
|
||||||
#include <latan/Sample.hpp>
|
|
||||||
|
|
||||||
LATAN_BEGIN_CPPDECL
|
|
||||||
|
|
||||||
/******************************************************************************
|
|
||||||
* Generic datafile class *
|
|
||||||
******************************************************************************/
|
|
||||||
typedef std::map<std::string,IOObject*> IODataTable;
|
|
||||||
|
|
||||||
namespace FileMode
|
|
||||||
{
|
|
||||||
typedef enum
|
|
||||||
{
|
|
||||||
Null = 0,
|
|
||||||
Write = 1 << 0,
|
|
||||||
Read = 1 << 1,
|
|
||||||
Append = 1 << 2
|
|
||||||
} Type;
|
|
||||||
}
|
|
||||||
|
|
||||||
class File
|
|
||||||
{
|
|
||||||
public:
|
|
||||||
// constructors
|
|
||||||
File(void);
|
|
||||||
File(const std::string in_name, const FileMode::Type in_mode);
|
|
||||||
// destructor
|
|
||||||
virtual ~File(void);
|
|
||||||
// access
|
|
||||||
std::string Name(void) const;
|
|
||||||
FileMode::Type Mode(void) const;
|
|
||||||
// tests
|
|
||||||
virtual bool IsOpen(void) const = 0;
|
|
||||||
// IO
|
|
||||||
virtual void Close(void) = 0;
|
|
||||||
virtual void Open(const std::string new_name,\
|
|
||||||
const FileMode::Type new_mode) = 0;
|
|
||||||
virtual void Save(void) = 0;
|
|
||||||
virtual void SaveAs(const std::string new_name) = 0;
|
|
||||||
protected:
|
|
||||||
// protected members
|
|
||||||
std::string name;
|
|
||||||
FileMode::Type mode;
|
|
||||||
IODataTable data;
|
|
||||||
// protected methods
|
|
||||||
void DeleteData(void);
|
|
||||||
template <typename IOObj> const IOObj& GetData(const std::string data_name);
|
|
||||||
};
|
|
||||||
|
|
||||||
// Template implementations
|
|
||||||
template <typename IOObj>
|
|
||||||
const IOObj& File::GetData(const std::string data_name)
|
|
||||||
{
|
|
||||||
try
|
|
||||||
{
|
|
||||||
return dynamic_cast<const IOObj&>(*(data.at(data_name)));
|
|
||||||
}
|
|
||||||
catch (std::out_of_range& e)
|
|
||||||
{
|
|
||||||
LATAN_ERROR(Range,"no data with name '"+data_name+"'");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/******************************************************************************
|
|
||||||
* ASCII datafile class *
|
|
||||||
******************************************************************************/
|
|
||||||
class ASCIIParserState: public ParserState<IODataTable>
|
|
||||||
{
|
|
||||||
public:
|
|
||||||
// constructor
|
|
||||||
explicit ASCIIParserState(std::istream* pt_stream, std::string* name,\
|
|
||||||
IODataTable* pt_data);
|
|
||||||
// destructor
|
|
||||||
virtual ~ASCIIParserState(void);
|
|
||||||
// public members
|
|
||||||
std::stack<DMat> dmat_buf;
|
|
||||||
std::stack<double> double_buf;
|
|
||||||
private:
|
|
||||||
// allocation/deallocation functions defined in IOASCIILexer.lpp
|
|
||||||
void init_scanner(void);
|
|
||||||
void destroy_scanner(void);
|
|
||||||
};
|
|
||||||
|
|
||||||
class ASCIIFile: public File
|
|
||||||
{
|
|
||||||
public:
|
|
||||||
// constructors
|
|
||||||
ASCIIFile(void);
|
|
||||||
ASCIIFile(const std::string in_name, const FileMode::Type in_mode);
|
|
||||||
// destructor
|
|
||||||
virtual ~ASCIIFile(void);
|
|
||||||
// access
|
|
||||||
template <typename IOObj> const IOObj& Read(const std::string name);
|
|
||||||
// tests
|
|
||||||
virtual bool IsOpen(void) const;
|
|
||||||
// IO
|
|
||||||
virtual void Close(void);
|
|
||||||
virtual void Open(const std::string new_name,\
|
|
||||||
const FileMode::Type new_mode);
|
|
||||||
virtual void Save(void);
|
|
||||||
virtual void SaveAs(const std::string new_name);
|
|
||||||
private:
|
|
||||||
// private members
|
|
||||||
std::fstream file_stream;
|
|
||||||
bool is_parsed;
|
|
||||||
ASCIIParserState* state;
|
|
||||||
// private methods
|
|
||||||
void Clear(void);
|
|
||||||
void OpenASCII(const std::string in_name, const FileMode::Type in_mode);
|
|
||||||
void CloseASCII(void);
|
|
||||||
void Parse(void);
|
|
||||||
};
|
|
||||||
|
|
||||||
// Template implementations
|
|
||||||
template <typename IOObj>
|
|
||||||
const IOObj& ASCIIFile::Read(const std::string data_name)
|
|
||||||
{
|
|
||||||
if ((mode & FileMode::Read)&&(IsOpen()))
|
|
||||||
{
|
|
||||||
if (!is_parsed)
|
|
||||||
{
|
|
||||||
Parse();
|
|
||||||
}
|
|
||||||
|
|
||||||
return GetData<IOObj>(data_name);
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
if (IsOpen())
|
|
||||||
{
|
|
||||||
LATAN_ERROR(IO,"file '"+name+"' is not opened in read mode");
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
LATAN_ERROR(IO,"file not opened");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
LATAN_END_CPPDECL
|
|
||||||
|
|
||||||
#endif
|
|
@ -1,12 +0,0 @@
|
|||||||
#include <latan/IOObject.hpp>
|
|
||||||
#include <latan/includes.hpp>
|
|
||||||
|
|
||||||
using namespace Latan;
|
|
||||||
|
|
||||||
IOObject::~IOObject(void)
|
|
||||||
{}
|
|
||||||
|
|
||||||
IOTypes::Type IOObject::IOType(void)
|
|
||||||
{
|
|
||||||
return IOTypes::NoType;
|
|
||||||
}
|
|
@ -1,25 +0,0 @@
|
|||||||
#ifndef LATAN_IOOBJECT_HPP_
|
|
||||||
#define LATAN_IOOBJECT_HPP_
|
|
||||||
|
|
||||||
#include <latan/Global.hpp>
|
|
||||||
|
|
||||||
LATAN_BEGIN_CPPDECL
|
|
||||||
|
|
||||||
// Abstract base for IO objects
|
|
||||||
class IOObject
|
|
||||||
{
|
|
||||||
public:
|
|
||||||
enum IOType
|
|
||||||
{
|
|
||||||
noType = 0,
|
|
||||||
dMat = 1,
|
|
||||||
sample = 2
|
|
||||||
};
|
|
||||||
public:
|
|
||||||
virtual ~IOObject(void) = 0;
|
|
||||||
virtual IOType getType(void) = 0;
|
|
||||||
};
|
|
||||||
|
|
||||||
LATAN_END_CPPDECL
|
|
||||||
|
|
||||||
#endif
|
|
168
latan/Io.cpp
Normal file
168
latan/Io.cpp
Normal file
@ -0,0 +1,168 @@
|
|||||||
|
#include <latan/Io.hpp>
|
||||||
|
#include <latan/includes.hpp>
|
||||||
|
|
||||||
|
using namespace std;
|
||||||
|
using namespace Latan;
|
||||||
|
|
||||||
|
// ASCII data format Bison/Flex parser declaration
|
||||||
|
int _ioAscii_parse(AsciiParserState* state);
|
||||||
|
|
||||||
|
/******************************************************************************
|
||||||
|
* File implementation *
|
||||||
|
******************************************************************************/
|
||||||
|
// constructors ////////////////////////////////////////////////////////////////
|
||||||
|
File::File(void)
|
||||||
|
: name_("")
|
||||||
|
, mode_(FileMode::null)
|
||||||
|
, data_()
|
||||||
|
{}
|
||||||
|
|
||||||
|
File::File(const string name, const unsigned int mode)
|
||||||
|
: name_(name)
|
||||||
|
, mode_(mode)
|
||||||
|
, data_()
|
||||||
|
{}
|
||||||
|
|
||||||
|
// destructor //////////////////////////////////////////////////////////////////
|
||||||
|
File::~File(void)
|
||||||
|
{}
|
||||||
|
|
||||||
|
// access //////////////////////////////////////////////////////////////////////
|
||||||
|
string File::getName(void) const
|
||||||
|
{
|
||||||
|
return name_;
|
||||||
|
}
|
||||||
|
|
||||||
|
unsigned int File::getMode(void) const
|
||||||
|
{
|
||||||
|
return mode_;
|
||||||
|
}
|
||||||
|
|
||||||
|
// internal functions //////////////////////////////////////////////////////////
|
||||||
|
void File::deleteData(void)
|
||||||
|
{
|
||||||
|
IoDataTable::iterator i;
|
||||||
|
|
||||||
|
for (i=data_.begin();i!=data_.end();++i)
|
||||||
|
{
|
||||||
|
delete i->second;
|
||||||
|
}
|
||||||
|
data_.clear();
|
||||||
|
}
|
||||||
|
|
||||||
|
/******************************************************************************
|
||||||
|
* AsciiParserState implementation *
|
||||||
|
******************************************************************************/
|
||||||
|
// constructor /////////////////////////////////////////////////////////////////
|
||||||
|
AsciiParserState::AsciiParserState(istream* stream, string* name,
|
||||||
|
IoDataTable* data)
|
||||||
|
: ParserState<IoDataTable>(stream, name, data)
|
||||||
|
{
|
||||||
|
initScanner();
|
||||||
|
}
|
||||||
|
|
||||||
|
// destructor //////////////////////////////////////////////////////////////////
|
||||||
|
AsciiParserState::~AsciiParserState(void)
|
||||||
|
{
|
||||||
|
destroyScanner();
|
||||||
|
}
|
||||||
|
|
||||||
|
/******************************************************************************
|
||||||
|
* AsciiFile implementation *
|
||||||
|
******************************************************************************/
|
||||||
|
// constructor /////////////////////////////////////////////////////////////////
|
||||||
|
AsciiFile::AsciiFile(void)
|
||||||
|
: File(), fileStream_()
|
||||||
|
, isParsed_(false)
|
||||||
|
, state_(NULL)
|
||||||
|
{}
|
||||||
|
|
||||||
|
AsciiFile::AsciiFile(const string name, const unsigned int mode)
|
||||||
|
{
|
||||||
|
openAscii(name, mode);
|
||||||
|
}
|
||||||
|
|
||||||
|
// destructor //////////////////////////////////////////////////////////////////
|
||||||
|
AsciiFile::~AsciiFile(void)
|
||||||
|
{
|
||||||
|
clear();
|
||||||
|
}
|
||||||
|
|
||||||
|
// tests ///////////////////////////////////////////////////////////////////////
|
||||||
|
bool AsciiFile::isOpen() const
|
||||||
|
{
|
||||||
|
return fileStream_.is_open();
|
||||||
|
}
|
||||||
|
|
||||||
|
// IO //////////////////////////////////////////////////////////////////////////
|
||||||
|
void AsciiFile::close(void)
|
||||||
|
{
|
||||||
|
clear();
|
||||||
|
}
|
||||||
|
|
||||||
|
void AsciiFile::open(const string name, const unsigned int mode)
|
||||||
|
{
|
||||||
|
if (isOpen())
|
||||||
|
{
|
||||||
|
LATAN_ERROR(Io, "file already opened with name '" + name_ + "'");
|
||||||
|
}
|
||||||
|
openAscii(name, mode);
|
||||||
|
}
|
||||||
|
|
||||||
|
void AsciiFile::save(void)
|
||||||
|
{
|
||||||
|
LATAN_ERROR(Implementation, "saving Ascii files not implemented yet");
|
||||||
|
}
|
||||||
|
|
||||||
|
void AsciiFile::saveAs(const string name __dumb)
|
||||||
|
{
|
||||||
|
LATAN_ERROR(Implementation, "saving Ascii files not implemented yet");
|
||||||
|
}
|
||||||
|
|
||||||
|
// internal functions //////////////////////////////////////////////////////////
|
||||||
|
void AsciiFile::clear()
|
||||||
|
{
|
||||||
|
deleteData();
|
||||||
|
closeAscii();
|
||||||
|
}
|
||||||
|
|
||||||
|
void AsciiFile::openAscii(const string name, const unsigned int mode)
|
||||||
|
{
|
||||||
|
if (!isOpen())
|
||||||
|
{
|
||||||
|
name_ = name;
|
||||||
|
mode_ = mode;
|
||||||
|
isParsed_ = false;
|
||||||
|
fileStream_.open(name_.c_str());
|
||||||
|
if (mode_ & FileMode::read)
|
||||||
|
{
|
||||||
|
state_ = new AsciiParserState(&fileStream_, &name_, &data_);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
state_ = NULL;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void AsciiFile::closeAscii(void)
|
||||||
|
{
|
||||||
|
if (state_)
|
||||||
|
{
|
||||||
|
delete state_;
|
||||||
|
state_ = NULL;
|
||||||
|
}
|
||||||
|
if (isOpen())
|
||||||
|
{
|
||||||
|
fileStream_.close();
|
||||||
|
}
|
||||||
|
name_ = "";
|
||||||
|
mode_ = FileMode::null;
|
||||||
|
isParsed_ = false;
|
||||||
|
}
|
||||||
|
|
||||||
|
void AsciiFile::parse()
|
||||||
|
{
|
||||||
|
_ioAscii_parse(state_);
|
||||||
|
isParsed_ = true;
|
||||||
|
}
|
154
latan/Io.hpp
Normal file
154
latan/Io.hpp
Normal file
@ -0,0 +1,154 @@
|
|||||||
|
#ifndef LATAN_IO_HPP_
|
||||||
|
#define LATAN_IO_HPP_
|
||||||
|
|
||||||
|
#include <fstream>
|
||||||
|
#include <map>
|
||||||
|
#include <stack>
|
||||||
|
#include <sstream>
|
||||||
|
#include <string>
|
||||||
|
#include <latan/Global.hpp>
|
||||||
|
#include <latan/IoObject.hpp>
|
||||||
|
#include <latan/Mat.hpp>
|
||||||
|
#include <latan/ParserState.hpp>
|
||||||
|
#include <latan/Sample.hpp>
|
||||||
|
|
||||||
|
LATAN_BEGIN_CPPDECL
|
||||||
|
|
||||||
|
/******************************************************************************
|
||||||
|
* Generic datafile class *
|
||||||
|
******************************************************************************/
|
||||||
|
typedef std::map<std::string, IoObject*> IoDataTable;
|
||||||
|
|
||||||
|
class File
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
class FileMode
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
enum
|
||||||
|
{
|
||||||
|
null = 0,
|
||||||
|
write = 1 << 0,
|
||||||
|
read = 1 << 1,
|
||||||
|
append = 1 << 2
|
||||||
|
};
|
||||||
|
};
|
||||||
|
public:
|
||||||
|
// constructors
|
||||||
|
File(void);
|
||||||
|
File(const std::string name, const unsigned int mode);
|
||||||
|
// destructor
|
||||||
|
virtual ~File(void);
|
||||||
|
// access
|
||||||
|
std::string getName(void) const;
|
||||||
|
unsigned int getMode(void) const;
|
||||||
|
// tests
|
||||||
|
virtual bool isOpen(void) const = 0;
|
||||||
|
// Io
|
||||||
|
virtual void close(void) = 0;
|
||||||
|
virtual void open(const std::string name, const unsigned int mode) = 0;
|
||||||
|
virtual void save(void) = 0;
|
||||||
|
virtual void saveAs(const std::string name) = 0;
|
||||||
|
protected:
|
||||||
|
// data access
|
||||||
|
void deleteData(void);
|
||||||
|
template <typename IoObj>
|
||||||
|
const IoObj& getData(const std::string dataName);
|
||||||
|
protected:
|
||||||
|
std::string name_;
|
||||||
|
unsigned int mode_;
|
||||||
|
IoDataTable data_;
|
||||||
|
};
|
||||||
|
|
||||||
|
// Template implementations
|
||||||
|
template <typename IoObj>
|
||||||
|
const IoObj& File::getData(const std::string dataName)
|
||||||
|
{
|
||||||
|
if (data_.find(dataName) != data_.end())
|
||||||
|
{
|
||||||
|
return dynamic_cast<const IoObj&>(*(data_[dataName]));
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
LATAN_ERROR(Range, "no data with name '" + dataName + "'");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/******************************************************************************
|
||||||
|
* ASCII datafile class *
|
||||||
|
******************************************************************************/
|
||||||
|
class AsciiParserState: public ParserState<IoDataTable>
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
// constructor
|
||||||
|
explicit AsciiParserState(std::istream* stream, std::string* name,\
|
||||||
|
IoDataTable* data);
|
||||||
|
// destructor
|
||||||
|
virtual ~AsciiParserState(void);
|
||||||
|
// public members
|
||||||
|
std::stack<DMat> dMatBuf;
|
||||||
|
std::stack<double> doubleBuf;
|
||||||
|
private:
|
||||||
|
// allocation/deallocation functions defined in IoAsciiLexer.lpp
|
||||||
|
virtual void initScanner(void);
|
||||||
|
virtual void destroyScanner(void);
|
||||||
|
};
|
||||||
|
|
||||||
|
class AsciiFile: public File
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
// constructors
|
||||||
|
AsciiFile(void);
|
||||||
|
AsciiFile(const std::string name, const unsigned int mode);
|
||||||
|
// destructor
|
||||||
|
virtual ~AsciiFile(void);
|
||||||
|
// access
|
||||||
|
template <typename IoObj>
|
||||||
|
const IoObj& read(const std::string name);
|
||||||
|
// tests
|
||||||
|
virtual bool isOpen(void) const;
|
||||||
|
// Io
|
||||||
|
virtual void close(void);
|
||||||
|
virtual void open(const std::string name, const unsigned int mode);
|
||||||
|
virtual void save(void);
|
||||||
|
virtual void saveAs(const std::string name);
|
||||||
|
private:
|
||||||
|
void clear(void);
|
||||||
|
void openAscii(const std::string name, const unsigned int mode);
|
||||||
|
void closeAscii(void);
|
||||||
|
void parse(void);
|
||||||
|
private:
|
||||||
|
std::fstream fileStream_;
|
||||||
|
bool isParsed_;
|
||||||
|
AsciiParserState* state_;
|
||||||
|
};
|
||||||
|
|
||||||
|
// Template implementations
|
||||||
|
template <typename IoObj>
|
||||||
|
const IoObj& AsciiFile::read(const std::string dataName)
|
||||||
|
{
|
||||||
|
if ((mode_ & FileMode::read)&&(isOpen()))
|
||||||
|
{
|
||||||
|
if (!isParsed_)
|
||||||
|
{
|
||||||
|
parse();
|
||||||
|
}
|
||||||
|
|
||||||
|
return getData<IoObj>(dataName);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
if (isOpen())
|
||||||
|
{
|
||||||
|
LATAN_ERROR(Io,"file '" + name_ + "' is not opened in read mode");
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
LATAN_ERROR(Io,"file not opened");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
LATAN_END_CPPDECL
|
||||||
|
|
||||||
|
#endif
|
@ -1,5 +1,5 @@
|
|||||||
%option reentrant
|
%option reentrant
|
||||||
%option prefix="_IOASCII_"
|
%option prefix="_ioAscii_"
|
||||||
%option bison-bridge
|
%option bison-bridge
|
||||||
%option bison-locations
|
%option bison-locations
|
||||||
%option noyywrap
|
%option noyywrap
|
||||||
@ -16,7 +16,7 @@
|
|||||||
using namespace std;
|
using namespace std;
|
||||||
using namespace Latan;
|
using namespace Latan;
|
||||||
|
|
||||||
#define YY_EXTRA_TYPE ASCIIParserState*
|
#define YY_EXTRA_TYPE AsciiParserState*
|
||||||
#define YY_USER_ACTION \
|
#define YY_USER_ACTION \
|
||||||
yylloc->first_line = yylloc->last_line = yylineno;\
|
yylloc->first_line = yylloc->last_line = yylineno;\
|
||||||
yylloc->first_column = yylloc->last_column + 1;\
|
yylloc->first_column = yylloc->last_column + 1;\
|
||||||
@ -62,13 +62,13 @@ BLANK [ \t]
|
|||||||
|
|
||||||
%%
|
%%
|
||||||
|
|
||||||
void ASCIIParserState::init_scanner()
|
void AsciiParserState::initScanner()
|
||||||
{
|
{
|
||||||
yylex_init(&scanner);
|
yylex_init(&scanner);
|
||||||
yyset_extra(this, scanner);
|
yyset_extra(this, scanner);
|
||||||
}
|
}
|
||||||
|
|
||||||
void ASCIIParserState::destroy_scanner()
|
void AsciiParserState::destroyScanner()
|
||||||
{
|
{
|
||||||
yylex_destroy(scanner);
|
yylex_destroy(scanner);
|
||||||
}
|
}
|
@ -11,11 +11,11 @@
|
|||||||
%}
|
%}
|
||||||
|
|
||||||
%pure-parser
|
%pure-parser
|
||||||
%name-prefix="_IOASCII_"
|
%name-prefix="_ioAscii_"
|
||||||
%locations
|
%locations
|
||||||
%defines
|
%defines
|
||||||
%error-verbose
|
%error-verbose
|
||||||
%parse-param { Latan::ASCIIParserState* state }
|
%parse-param { Latan::AsciiParserState* state }
|
||||||
%initial-action {yylloc.last_column = 0;}
|
%initial-action {yylloc.last_column = 0;}
|
||||||
%lex-param { void* scanner }
|
%lex-param { void* scanner }
|
||||||
|
|
||||||
@ -36,15 +36,15 @@
|
|||||||
%token OPEN
|
%token OPEN
|
||||||
|
|
||||||
%{
|
%{
|
||||||
int _IOASCII_lex(YYSTYPE* lvalp, YYLTYPE* llocp, void* scanner);
|
int _ioAscii_lex(YYSTYPE* lvalp, YYLTYPE* llocp, void* scanner);
|
||||||
|
|
||||||
void _IOASCII_error(YYLTYPE* locp, ASCIIParserState* state, const char* err)
|
void _ioAscii_error(YYLTYPE* locp, AsciiParserState* state, const char* err)
|
||||||
{
|
{
|
||||||
stringstream buf;
|
stringstream buf;
|
||||||
|
|
||||||
buf << *state->stream_name << ":" << locp->first_line << ":"\
|
buf << *state->streamName << ":" << locp->first_line << ":"\
|
||||||
<< locp->first_column << ": " << err;
|
<< locp->first_column << ": " << err;
|
||||||
LATAN_ERROR(Parsing,buf.str());
|
LATAN_ERROR(Parsing, buf.str());
|
||||||
}
|
}
|
||||||
|
|
||||||
#define scanner state->scanner
|
#define scanner state->scanner
|
||||||
@ -60,26 +60,26 @@ datas:
|
|||||||
mat:
|
mat:
|
||||||
OPEN MAT ID INT floats CLOSE MAT
|
OPEN MAT ID INT floats CLOSE MAT
|
||||||
{
|
{
|
||||||
const int nrow = state->double_buf.size()/$INT, ncol = $INT;
|
const int nRow = state->doubleBuf.size()/$INT, nCol = $INT;
|
||||||
(*state->data)[$ID] = new DMat(nrow,ncol);
|
(*state->data)[$ID] = new DMat(nRow,nCol);
|
||||||
DMat& M = static_cast<DMat&>(*((*state->data)[$ID]));
|
DMat &M = static_cast<DMat &>(*((*state->data)[$ID]));
|
||||||
int r,i,j;
|
int r,i,j;
|
||||||
|
|
||||||
r = 0;
|
r = 0;
|
||||||
while (!state->double_buf.empty())
|
while (!state->doubleBuf.empty())
|
||||||
{
|
{
|
||||||
j = r%ncol;
|
j = r % nCol;
|
||||||
i = (r-j)/ncol;
|
i = (r - j)/nCol;
|
||||||
M(i,j) = state->double_buf.top();
|
M(i,j) = state->doubleBuf.top();
|
||||||
state->double_buf.pop();
|
state->doubleBuf.pop();
|
||||||
++r;
|
++r;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
;
|
;
|
||||||
|
|
||||||
floats:
|
floats:
|
||||||
FLOAT floats {state->double_buf.push($1);}
|
FLOAT floats {state->doubleBuf.push($1);}
|
||||||
| INT floats {state->double_buf.push(static_cast<double>($1));}
|
| INT floats {state->doubleBuf.push(static_cast<double>($1));}
|
||||||
| FLOAT {state->double_buf.push($1);}
|
| FLOAT {state->doubleBuf.push($1);}
|
||||||
| INT {state->double_buf.push(static_cast<double>($1));}
|
| INT {state->doubleBuf.push(static_cast<double>($1));}
|
||||||
;
|
;
|
29
latan/IoObject.hpp
Normal file
29
latan/IoObject.hpp
Normal file
@ -0,0 +1,29 @@
|
|||||||
|
#ifndef LATAN_IOOBJECT_HPP_
|
||||||
|
#define LATAN_IOOBJECT_HPP_
|
||||||
|
|
||||||
|
#include <latan/Global.hpp>
|
||||||
|
|
||||||
|
LATAN_BEGIN_CPPDECL
|
||||||
|
|
||||||
|
// Abstract base for IO objects
|
||||||
|
class IoObject
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
class IoType
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
enum
|
||||||
|
{
|
||||||
|
noType = 0,
|
||||||
|
dMat = 1,
|
||||||
|
sample = 2
|
||||||
|
};
|
||||||
|
};
|
||||||
|
public:
|
||||||
|
virtual ~IoObject(void) = 0;
|
||||||
|
virtual unsigned int getType(void) = 0;
|
||||||
|
};
|
||||||
|
|
||||||
|
LATAN_END_CPPDECL
|
||||||
|
|
||||||
|
#endif
|
@ -20,7 +20,7 @@ AM_YFLAGS = -d
|
|||||||
include eigen_files.mk
|
include eigen_files.mk
|
||||||
nobase_dist_pkginclude_HEADERS = $(eigen_files)
|
nobase_dist_pkginclude_HEADERS = $(eigen_files)
|
||||||
|
|
||||||
BUILT_SOURCES = IOASCIIParser.hpp MathParser.hpp
|
BUILT_SOURCES = IoAsciiParser.hpp MathParser.hpp
|
||||||
|
|
||||||
lib_LTLIBRARIES = liblatan.la
|
lib_LTLIBRARIES = liblatan.la
|
||||||
|
|
||||||
@ -28,9 +28,9 @@ liblatan_la_SOURCES = \
|
|||||||
Exceptions.cpp \
|
Exceptions.cpp \
|
||||||
Global.cpp \
|
Global.cpp \
|
||||||
includes.hpp \
|
includes.hpp \
|
||||||
IO.cpp \
|
Io.cpp \
|
||||||
IOASCIIParser.ypp \
|
IoAsciiParser.ypp \
|
||||||
IOASCIILexer.lpp \
|
IoAsciiLexer.lpp \
|
||||||
Mat.cpp \
|
Mat.cpp \
|
||||||
MathCompiler.cpp \
|
MathCompiler.cpp \
|
||||||
MathParser.ypp \
|
MathParser.ypp \
|
||||||
@ -40,8 +40,8 @@ liblatan_la_SOURCES = \
|
|||||||
liblatan_ladir = $(pkgincludedir)
|
liblatan_ladir = $(pkgincludedir)
|
||||||
liblatan_la_HEADERS = \
|
liblatan_la_HEADERS = \
|
||||||
Global.hpp \
|
Global.hpp \
|
||||||
IO.hpp \
|
Io.hpp \
|
||||||
IOObject.hpp \
|
IoObject.hpp \
|
||||||
Mat.hpp \
|
Mat.hpp \
|
||||||
MathCompiler.hpp \
|
MathCompiler.hpp \
|
||||||
Sample.hpp
|
Sample.hpp
|
||||||
|
@ -16,11 +16,11 @@ DMat::DMat(const DMat& M)
|
|||||||
: DMatBase(M)
|
: DMatBase(M)
|
||||||
{}
|
{}
|
||||||
|
|
||||||
DMat::DMat(unsigned int nrow, unsigned int ncol)
|
DMat::DMat(const unsigned int nRow, const unsigned int nCol)
|
||||||
: DMatBase(nrow,ncol)
|
: DMatBase(nRow,nCol)
|
||||||
{}
|
{}
|
||||||
|
|
||||||
IOObject::IOType DMat::getType(void)
|
unsigned int DMat::getType(void)
|
||||||
{
|
{
|
||||||
return dMat;
|
return IoType::dMat;
|
||||||
}
|
}
|
||||||
|
@ -13,15 +13,15 @@ typedef Eigen::MatrixXcd CMatBase;
|
|||||||
typedef Eigen::VectorXd DVecBase;
|
typedef Eigen::VectorXd DVecBase;
|
||||||
typedef Eigen::VectorXcd CVecBase;
|
typedef Eigen::VectorXcd CVecBase;
|
||||||
|
|
||||||
class DMat: public DMatBase, public IOObject
|
class DMat: public DMatBase, public IoObject
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
// constructors
|
// constructors
|
||||||
DMat(void);
|
DMat(void);
|
||||||
DMat(const DMat& M);
|
DMat(const DMat& M);
|
||||||
DMat(unsigned int nrow, unsigned int ncol);
|
DMat(const unsigned int nRow, const unsigned int nCol);
|
||||||
// IO
|
// IO
|
||||||
virtual IOType getType(void);
|
virtual unsigned int getType(void);
|
||||||
};
|
};
|
||||||
|
|
||||||
LATAN_END_CPPDECL
|
LATAN_END_CPPDECL
|
||||||
|
@ -5,26 +5,29 @@ using namespace std;
|
|||||||
using namespace Latan;
|
using namespace Latan;
|
||||||
|
|
||||||
// Math Bison/Flex parser declaration
|
// Math Bison/Flex parser declaration
|
||||||
int _Math_parse(MathParserState* state);
|
int _math_parse(MathParserState* state);
|
||||||
|
|
||||||
/******************************************************************************
|
/******************************************************************************
|
||||||
* MathNode implementation *
|
* MathNode implementation *
|
||||||
******************************************************************************/
|
******************************************************************************/
|
||||||
// constructor /////////////////////////////////////////////////////////////////
|
// constructor /////////////////////////////////////////////////////////////////
|
||||||
MathNode::MathNode(const string& init_str, NodeType::Type init_type)
|
MathNode::MathNode(const string &name, const unsigned int type)
|
||||||
: str(init_str), type(init_type)
|
: name_(name)
|
||||||
|
, type_(type)
|
||||||
{}
|
{}
|
||||||
|
|
||||||
MathNode::MathNode(const std::string& init_str, NodeType::Type init_type,\
|
MathNode::MathNode(const std::string &name, const unsigned int type,\
|
||||||
const unsigned int narg, ...)
|
const unsigned int nArg, ...)
|
||||||
: str(init_str), type(init_type), arg(narg)
|
: name_(name)
|
||||||
|
, type_(type)
|
||||||
|
, arg_(nArg)
|
||||||
{
|
{
|
||||||
va_list va;
|
va_list va;
|
||||||
|
|
||||||
va_start(va,narg);
|
va_start(va, nArg);
|
||||||
for (unsigned int i=0;i<narg;++i)
|
for (unsigned int i = 0; i < nArg; ++i)
|
||||||
{
|
{
|
||||||
arg[i] = va_arg(va,MathNode*);
|
arg_[i] = va_arg(va, MathNode*);
|
||||||
}
|
}
|
||||||
va_end(va);
|
va_end(va);
|
||||||
}
|
}
|
||||||
@ -34,49 +37,49 @@ MathNode::~MathNode(void)
|
|||||||
{
|
{
|
||||||
vector<MathNode*>::iterator i;
|
vector<MathNode*>::iterator i;
|
||||||
|
|
||||||
for (i=arg.begin();i!=arg.end();++i)
|
for (i = arg_.begin(); i != arg_.end(); ++i)
|
||||||
{
|
{
|
||||||
delete *i;
|
delete *i;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// access //////////////////////////////////////////////////////////////////////
|
// access //////////////////////////////////////////////////////////////////////
|
||||||
const string& MathNode::String(void) const
|
const string &MathNode::getName(void) const
|
||||||
{
|
{
|
||||||
return str;
|
return name_;
|
||||||
}
|
}
|
||||||
|
|
||||||
NodeType::Type MathNode::Type(void) const
|
unsigned int MathNode::getType(void) const
|
||||||
{
|
{
|
||||||
return type;
|
return type_;
|
||||||
}
|
}
|
||||||
|
|
||||||
unsigned int MathNode::NArg(void) const
|
unsigned int MathNode::getNArg(void) const
|
||||||
{
|
{
|
||||||
return static_cast<unsigned int>(arg.size());
|
return static_cast<unsigned int>(arg_.size());
|
||||||
}
|
}
|
||||||
|
|
||||||
// operator ////////////////////////////////////////////////////////////////////
|
// operator ////////////////////////////////////////////////////////////////////
|
||||||
const MathNode& MathNode::operator[](const unsigned int i) const
|
const MathNode &MathNode::operator[](const unsigned int i) const
|
||||||
{
|
{
|
||||||
return *arg[i];
|
return *arg_[i];
|
||||||
}
|
}
|
||||||
|
|
||||||
/******************************************************************************
|
/******************************************************************************
|
||||||
* MathParserState implementation *
|
* MathParserState implementation *
|
||||||
******************************************************************************/
|
******************************************************************************/
|
||||||
// constructor /////////////////////////////////////////////////////////////////
|
// constructor /////////////////////////////////////////////////////////////////
|
||||||
MathParserState::MathParserState(std::istream* pt_stream, std::string* pt_name,\
|
MathParserState::MathParserState(std::istream *stream, std::string *name,
|
||||||
MathNode** pt_data)
|
MathNode **data)
|
||||||
: ParserState<MathNode*>(pt_stream,pt_name,pt_data)
|
: ParserState<MathNode *>(stream, name, data)
|
||||||
{
|
{
|
||||||
init_scanner();
|
initScanner();
|
||||||
}
|
}
|
||||||
|
|
||||||
// destructor //////////////////////////////////////////////////////////////////
|
// destructor //////////////////////////////////////////////////////////////////
|
||||||
MathParserState::~MathParserState(void)
|
MathParserState::~MathParserState(void)
|
||||||
{
|
{
|
||||||
destroy_scanner();
|
destroyScanner();
|
||||||
}
|
}
|
||||||
|
|
||||||
/******************************************************************************
|
/******************************************************************************
|
||||||
@ -88,97 +91,95 @@ MathParserState::~MathParserState(void)
|
|||||||
Instruction::~Instruction(void)
|
Instruction::~Instruction(void)
|
||||||
{}
|
{}
|
||||||
|
|
||||||
void Instruction::Print(std::ostream &out) const
|
ostream &Latan::operator<<(ostream& out, const Instruction& ins)
|
||||||
{
|
{
|
||||||
out << CODE_MOD << "<null>" << CODE_MOD << "ABSTRACT INSTRUCTION!";
|
ins.print(out);
|
||||||
}
|
|
||||||
|
|
||||||
ostream& Latan::operator<<(ostream& out, const Instruction& ins)
|
|
||||||
{
|
|
||||||
ins.Print(out);
|
|
||||||
|
|
||||||
return out;
|
return out;
|
||||||
}
|
}
|
||||||
|
|
||||||
Push::Push(const double& init_cst)
|
Push::Push(const double val)
|
||||||
: type(Constant), cst(init_cst), vname("")
|
: type_(ArgType::Constant)
|
||||||
|
, val_(val)
|
||||||
|
, name_("")
|
||||||
{}
|
{}
|
||||||
|
|
||||||
Push::Push(const string& init_vname)
|
Push::Push(const string &name)
|
||||||
: type(Variable), cst(0.0), vname(init_vname)
|
: type_(ArgType::Variable)
|
||||||
|
, val_(0.0)
|
||||||
|
, name_(name)
|
||||||
{}
|
{}
|
||||||
|
|
||||||
void Push::operator()(std::stack<double> &dstack, VarTable &vtable)
|
void Push::operator()(std::stack<double> &dStack, VarTable &vTable)
|
||||||
{
|
{
|
||||||
if (type == Constant)
|
if (type_ == ArgType::Constant)
|
||||||
{
|
{
|
||||||
dstack.push(cst);
|
dStack.push(val_);
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
dstack.push(vtable[vname]);
|
dStack.push(vTable[name_]);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void Push::Print(std::ostream &out) const
|
void Push::print(std::ostream &out) const
|
||||||
{
|
{
|
||||||
out << CODE_MOD << "push";
|
out << CODE_MOD << "push";
|
||||||
if (type == Constant)
|
if (type_ == ArgType::Constant)
|
||||||
{
|
{
|
||||||
out << CODE_MOD << cst;
|
out << CODE_MOD << val_;
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
out << CODE_MOD << vname;
|
out << CODE_MOD << name_;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
Pop::Pop(const string& init_vname)
|
Pop::Pop(const string &name)
|
||||||
: vname(init_vname)
|
: name_(name)
|
||||||
{}
|
{}
|
||||||
|
|
||||||
void Pop::operator()(std::stack<double> &dstack, VarTable &vtable)
|
void Pop::operator()(std::stack<double> &dStack, VarTable &vTable)
|
||||||
{
|
{
|
||||||
vtable[vname] = dstack.top();
|
vTable[name_] = dStack.top();
|
||||||
dstack.pop();
|
dStack.pop();
|
||||||
}
|
}
|
||||||
|
|
||||||
void Pop::Print(std::ostream &out) const
|
void Pop::print(std::ostream &out) const
|
||||||
{
|
{
|
||||||
out << CODE_MOD << "pop" << CODE_MOD << vname;
|
out << CODE_MOD << "pop" << CODE_MOD << name_;
|
||||||
}
|
}
|
||||||
|
|
||||||
#define DEF_OP(name,narg,exp,code_name)\
|
#define DEF_OP(name, nArg, exp, insName)\
|
||||||
void name::operator()(stack<double>& dstack, VarTable& vtable __dumb)\
|
void name::operator()(stack<double> &dStack, VarTable &vTable __dumb)\
|
||||||
{\
|
{\
|
||||||
double x[narg];\
|
double x[nArg];\
|
||||||
for (int i=0;i<narg;++i)\
|
for (int i = 0; i < nArg; ++i)\
|
||||||
{\
|
{\
|
||||||
x[narg-1-i] = dstack.top();\
|
x[nArg-1-i] = dStack.top();\
|
||||||
dstack.pop();\
|
dStack.pop();\
|
||||||
}\
|
}\
|
||||||
dstack.push(exp);\
|
dStack.push(exp);\
|
||||||
}\
|
}\
|
||||||
\
|
void name::print(std::ostream &out) const\
|
||||||
void name::Print(std::ostream &out) const\
|
|
||||||
{\
|
{\
|
||||||
out << CODE_MOD << code_name;\
|
out << CODE_MOD << insName;\
|
||||||
}
|
}
|
||||||
|
|
||||||
DEF_OP(Neg,1,-x[0],"neg")
|
DEF_OP(Neg, 1, -x[0], "neg")
|
||||||
DEF_OP(Add,2,x[0]+x[1],"add")
|
DEF_OP(Add, 2, x[0]+x[1], "add")
|
||||||
DEF_OP(Sub,2,x[0]-x[1],"sub")
|
DEF_OP(Sub, 2, x[0]-x[1], "sub")
|
||||||
DEF_OP(Mul,2,x[0]*x[1],"mul")
|
DEF_OP(Mul, 2, x[0]*x[1], "mul")
|
||||||
DEF_OP(Div,2,x[0]/x[1],"div")
|
DEF_OP(Div, 2, x[0]/x[1], "div")
|
||||||
DEF_OP(Pow,2,pow(x[0],x[1]),"pow")
|
DEF_OP(Pow, 2, pow(x[0],x[1]), "pow")
|
||||||
|
|
||||||
VirtualProgram::VirtualProgram(void)
|
VirtualProgram::VirtualProgram(void)
|
||||||
: vector<Instruction*>(0)
|
: vector<Instruction *>(0)
|
||||||
{}
|
{}
|
||||||
|
|
||||||
ostream& Latan::operator<<(ostream& out,const VirtualProgram& prog)
|
ostream &Latan::operator<<(ostream &out, const VirtualProgram &prog)
|
||||||
{
|
{
|
||||||
for (unsigned int i=0;i<prog.size();++i)
|
for (unsigned int i = 0; i < prog.size(); ++i)
|
||||||
{
|
{
|
||||||
out << *(prog[i]) << endl;
|
out << *(prog[i]) << endl;
|
||||||
}
|
}
|
||||||
@ -191,103 +192,108 @@ ostream& Latan::operator<<(ostream& out,const VirtualProgram& prog)
|
|||||||
******************************************************************************/
|
******************************************************************************/
|
||||||
// constructors ////////////////////////////////////////////////////////////////
|
// constructors ////////////////////////////////////////////////////////////////
|
||||||
MathCompiler::MathCompiler(void)
|
MathCompiler::MathCompiler(void)
|
||||||
: code(NULL), code_name("<no_code>"), state(NULL), root(NULL), out(), status(0)
|
: code_(NULL)
|
||||||
|
, codeName_("<no_code>")
|
||||||
|
, state_(NULL)
|
||||||
|
, root_(NULL)
|
||||||
|
, out_()
|
||||||
|
, status_(Status::None)
|
||||||
{}
|
{}
|
||||||
|
|
||||||
MathCompiler::MathCompiler(const std::string& code_str)
|
MathCompiler::MathCompiler(const std::string &code)
|
||||||
{
|
{
|
||||||
Init(code_str);
|
init(code);
|
||||||
}
|
}
|
||||||
|
|
||||||
// destructor //////////////////////////////////////////////////////////////////
|
// destructor //////////////////////////////////////////////////////////////////
|
||||||
MathCompiler::~MathCompiler(void)
|
MathCompiler::~MathCompiler(void)
|
||||||
{
|
{
|
||||||
Reset();
|
reset();
|
||||||
}
|
}
|
||||||
|
|
||||||
// public methods //////////////////////////////////////////////////////////////
|
// public methods //////////////////////////////////////////////////////////////
|
||||||
void MathCompiler::Init(const std::string &code_str)
|
void MathCompiler::init(const std::string &code)
|
||||||
{
|
{
|
||||||
if (status)
|
if (status_)
|
||||||
{
|
{
|
||||||
Reset();
|
reset();
|
||||||
}
|
}
|
||||||
code = new stringstream(code_str);
|
code_ = new stringstream(code);
|
||||||
code_name = "<string>";
|
codeName_ = "<string>";
|
||||||
state = new MathParserState(code,&code_name,&root);
|
state_ = new MathParserState(code_, &codeName_, &root_);
|
||||||
status |= Initialised;
|
status_ |= Status::Initialised;
|
||||||
}
|
}
|
||||||
|
|
||||||
const VirtualProgram& MathCompiler::operator()(void)
|
const VirtualProgram& MathCompiler::operator()(void)
|
||||||
{
|
{
|
||||||
if (!(status & Parsed))
|
if (!(status_ & Status::Parsed))
|
||||||
{
|
{
|
||||||
Parse();
|
parse();
|
||||||
}
|
}
|
||||||
if (!(status & Compiled))
|
if (!(status_ & Status::Compiled))
|
||||||
{
|
{
|
||||||
Compile(*root);
|
compile(*root_);
|
||||||
}
|
}
|
||||||
|
|
||||||
return out;
|
return out_;
|
||||||
}
|
}
|
||||||
|
|
||||||
// private methods /////////////////////////////////////////////////////////////
|
// private methods /////////////////////////////////////////////////////////////
|
||||||
void MathCompiler::Parse(void)
|
void MathCompiler::parse(void)
|
||||||
{
|
{
|
||||||
_Math_parse(state);
|
_math_parse(state_);
|
||||||
status |= Parsed;
|
status_ |= Status::Parsed;
|
||||||
status -= status & Compiled;
|
status_ -= status_ & Status::Compiled;
|
||||||
}
|
}
|
||||||
|
|
||||||
#define IFOP(name,narg) if ((N.String() == name)&&(N.NArg() == narg))
|
#define IFOP(name, nArg) if ((n.getName() == (name))&&(n.getNArg() == nArg))
|
||||||
#define ELIFOP(name,narg) else IFOP(name,narg)
|
#define ELIFOP(name, nArg) else IFOP(name, nArg)
|
||||||
#define ELSE else
|
#define ELSE else
|
||||||
void MathCompiler::Compile(const MathNode& N)
|
void MathCompiler::compile(const MathNode& n)
|
||||||
{
|
{
|
||||||
switch (N.Type())
|
switch (n.getType())
|
||||||
{
|
{
|
||||||
case NodeType::Constant:
|
case MathNode::Type::Constant:
|
||||||
out.push_back(new Push(ato<double>(N.String())));
|
out_.push_back(new Push(ato<double>(n.getName())));
|
||||||
break;
|
break;
|
||||||
case NodeType::Variable:
|
case MathNode::Type::Variable:
|
||||||
out.push_back(new Push(N.String()));
|
out_.push_back(new Push(n.getName()));
|
||||||
break;
|
break;
|
||||||
case NodeType::Operator:
|
case MathNode::Type::Operator:
|
||||||
for (unsigned int i=0;i<N.NArg();++i)
|
for (unsigned int i = 0; i < n.getNArg(); ++i)
|
||||||
{
|
{
|
||||||
Compile(N[i]);
|
compile(n[i]);
|
||||||
}
|
}
|
||||||
IFOP("-",1) out.push_back(new Neg);
|
IFOP("-",1) out_.push_back(new Neg);
|
||||||
ELIFOP("+",2) out.push_back(new Add);
|
ELIFOP("+",2) out_.push_back(new Add);
|
||||||
ELIFOP("-",2) out.push_back(new Sub);
|
ELIFOP("-",2) out_.push_back(new Sub);
|
||||||
ELIFOP("*",2) out.push_back(new Mul);
|
ELIFOP("*",2) out_.push_back(new Mul);
|
||||||
ELIFOP("/",2) out.push_back(new Div);
|
ELIFOP("/",2) out_.push_back(new Div);
|
||||||
ELIFOP("^",2) out.push_back(new Pow);
|
ELIFOP("^",2) out_.push_back(new Pow);
|
||||||
ELSE LATAN_ERROR(Compilation, \
|
ELSE LATAN_ERROR(Compilation,
|
||||||
"unknown operator (node '"+N.String()+"')");
|
"unknown operator (node '" + n.getName() + "')");
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
LATAN_ERROR(Compilation, \
|
LATAN_ERROR(Compilation,
|
||||||
"unknown node type (node '"+N.String()+"')");
|
"unknown node type (node '" + n.getName() + "')");
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
status |= Compiled;
|
status_ |= Status::Compiled;
|
||||||
}
|
}
|
||||||
|
|
||||||
void MathCompiler::Reset(void)
|
void MathCompiler::reset(void)
|
||||||
{
|
{
|
||||||
VirtualProgram::iterator i;
|
VirtualProgram::iterator i;
|
||||||
|
|
||||||
delete code;
|
delete code_;
|
||||||
code_name = "<no_code>";
|
codeName_ = "<no_code>";
|
||||||
delete state;
|
delete state_;
|
||||||
delete root;
|
delete root_;
|
||||||
for (i=out.begin();i!=out.end();++i)
|
for (i = out_.begin(); i != out_.end(); ++i)
|
||||||
{
|
{
|
||||||
delete *i;
|
delete *i;
|
||||||
}
|
}
|
||||||
out.clear();
|
out_.clear();
|
||||||
status = 0;
|
status_ = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -16,56 +16,57 @@ LATAN_BEGIN_CPPDECL
|
|||||||
/******************************************************************************
|
/******************************************************************************
|
||||||
* Parser classes *
|
* Parser classes *
|
||||||
******************************************************************************/
|
******************************************************************************/
|
||||||
namespace NodeType
|
|
||||||
{
|
|
||||||
typedef enum
|
|
||||||
{
|
|
||||||
Constant = 0,\
|
|
||||||
Operator = 1,\
|
|
||||||
Variable = 2
|
|
||||||
} Type;
|
|
||||||
}
|
|
||||||
|
|
||||||
class MathNode
|
class MathNode
|
||||||
{
|
{
|
||||||
|
public:
|
||||||
|
class Type
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
enum
|
||||||
|
{
|
||||||
|
Constant = 0,
|
||||||
|
Operator = 1,
|
||||||
|
Variable = 2
|
||||||
|
};
|
||||||
|
};
|
||||||
public:
|
public:
|
||||||
// constructor
|
// constructor
|
||||||
MathNode(const std::string& init_str, NodeType::Type init_type);
|
MathNode(const std::string &name, const unsigned int type);
|
||||||
MathNode(const std::string& init_str, NodeType::Type init_type,\
|
MathNode(const std::string &name, const unsigned int type,
|
||||||
const unsigned int narg, ...);
|
const unsigned int nArg, ...);
|
||||||
// destructor
|
// destructor
|
||||||
virtual ~MathNode();
|
virtual ~MathNode();
|
||||||
// access
|
// access
|
||||||
const std::string& String(void) const;
|
const std::string& getName(void) const;
|
||||||
NodeType::Type Type(void) const;
|
unsigned int getType(void) const;
|
||||||
unsigned int NArg(void) const;
|
unsigned int getNArg(void) const;
|
||||||
// operators
|
// operators
|
||||||
const MathNode& operator[](const unsigned int i) const;
|
const MathNode &operator[](const unsigned int i) const;
|
||||||
private:
|
private:
|
||||||
// private members
|
// private members
|
||||||
std::string str;
|
std::string name_;
|
||||||
NodeType::Type type;
|
unsigned int type_;
|
||||||
std::vector<MathNode*> arg;
|
std::vector<MathNode*> arg_;
|
||||||
};
|
};
|
||||||
|
|
||||||
class MathParserState: public ParserState<MathNode*>
|
class MathParserState: public ParserState<MathNode *>
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
// constructor
|
// constructor
|
||||||
explicit MathParserState(std::istream* pt_stream, std::string* name,\
|
explicit MathParserState(std::istream *stream, std::string *name,
|
||||||
MathNode** pt_data);
|
MathNode **data);
|
||||||
// destructor
|
// destructor
|
||||||
virtual ~MathParserState(void);
|
virtual ~MathParserState(void);
|
||||||
private:
|
private:
|
||||||
// allocation/deallocation functions defined in IOASCIILexer.lpp
|
// allocation/deallocation functions defined in MathLexer.lpp
|
||||||
void init_scanner(void);
|
virtual void initScanner(void);
|
||||||
void destroy_scanner(void);
|
virtual void destroyScanner(void);
|
||||||
};
|
};
|
||||||
|
|
||||||
/******************************************************************************
|
/******************************************************************************
|
||||||
* Virtual machine code classes *
|
* Virtual machine code classes *
|
||||||
******************************************************************************/
|
******************************************************************************/
|
||||||
typedef std::map<std::string,double> VarTable;
|
typedef std::map<std::string, double> VarTable;
|
||||||
|
|
||||||
// Abstract base
|
// Abstract base
|
||||||
class Instruction
|
class Instruction
|
||||||
@ -73,39 +74,50 @@ class Instruction
|
|||||||
public:
|
public:
|
||||||
virtual ~Instruction();
|
virtual ~Instruction();
|
||||||
// instruction execution
|
// instruction execution
|
||||||
virtual void operator()(std::stack<double>& dstack, VarTable& vtable) = 0;
|
virtual void operator()(std::stack<double> &dStack, VarTable &vTable) = 0;
|
||||||
friend std::ostream& operator<<(std::ostream& out, const Instruction& ins);
|
friend std::ostream& operator<<(std::ostream &out, const Instruction &ins);
|
||||||
private:
|
private:
|
||||||
virtual void Print(std::ostream& out) const;
|
virtual void print(std::ostream &out) const = 0;
|
||||||
};
|
};
|
||||||
|
|
||||||
// push and pop
|
// push and pop
|
||||||
class Push: public Instruction
|
class Push: public Instruction
|
||||||
{
|
{
|
||||||
|
private:
|
||||||
|
class ArgType
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
enum
|
||||||
|
{
|
||||||
|
Constant = 0,
|
||||||
|
Variable = 1
|
||||||
|
};
|
||||||
|
};
|
||||||
public:
|
public:
|
||||||
//constructors
|
//constructors
|
||||||
explicit Push(const double& init_cst);
|
explicit Push(const double val);
|
||||||
explicit Push(const std::string& init_vname);
|
explicit Push(const std::string &name);
|
||||||
// instruction execution
|
// instruction execution
|
||||||
virtual void operator()(std::stack<double>& dstack, VarTable& vtable);
|
virtual void operator()(std::stack<double> &dStack, VarTable &vTable);
|
||||||
private:
|
private:
|
||||||
typedef enum {Constant = 0, Variable = 1} ArgType;
|
virtual void print(std::ostream& out) const;
|
||||||
ArgType type;
|
private:
|
||||||
double cst;
|
unsigned int type_;
|
||||||
std::string vname;
|
double val_;
|
||||||
virtual void Print(std::ostream& out) const;
|
std::string name_;
|
||||||
};
|
};
|
||||||
|
|
||||||
class Pop: public Instruction
|
class Pop: public Instruction
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
//constructor
|
//constructor
|
||||||
explicit Pop(const std::string& init_vname);
|
explicit Pop(const std::string &name);
|
||||||
// instruction execution
|
// instruction execution
|
||||||
virtual void operator()(std::stack<double>& dstack, VarTable& vtable);
|
virtual void operator()(std::stack<double> &dStack, VarTable &vTable);
|
||||||
private:
|
private:
|
||||||
std::string vname;
|
virtual void print(std::ostream& out) const;
|
||||||
virtual void Print(std::ostream& out) const;
|
private:
|
||||||
|
std::string name_;
|
||||||
};
|
};
|
||||||
|
|
||||||
// Float operations
|
// Float operations
|
||||||
@ -113,9 +125,9 @@ private:
|
|||||||
class name: public Instruction\
|
class name: public Instruction\
|
||||||
{\
|
{\
|
||||||
public:\
|
public:\
|
||||||
virtual void operator()(std::stack<double>& dstack, VarTable& vtable);\
|
virtual void operator()(std::stack<double> &dStack, VarTable &vTable);\
|
||||||
private:\
|
private:\
|
||||||
virtual void Print(std::ostream& out) const;\
|
virtual void print(std::ostream &out) const;\
|
||||||
}
|
}
|
||||||
|
|
||||||
DECL_OP(Neg);
|
DECL_OP(Neg);
|
||||||
@ -125,12 +137,12 @@ DECL_OP(Mul);
|
|||||||
DECL_OP(Div);
|
DECL_OP(Div);
|
||||||
DECL_OP(Pow);
|
DECL_OP(Pow);
|
||||||
|
|
||||||
class VirtualProgram: public std::vector<Instruction*>
|
class VirtualProgram: public std::vector<Instruction *>
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
VirtualProgram(void);
|
VirtualProgram(void);
|
||||||
friend std::ostream& operator<<(std::ostream& out, \
|
friend std::ostream &operator<<(std::ostream &out,
|
||||||
const VirtualProgram& prog);
|
const VirtualProgram &program);
|
||||||
};
|
};
|
||||||
|
|
||||||
/******************************************************************************
|
/******************************************************************************
|
||||||
@ -138,34 +150,38 @@ public:
|
|||||||
******************************************************************************/
|
******************************************************************************/
|
||||||
class MathCompiler
|
class MathCompiler
|
||||||
{
|
{
|
||||||
|
private:
|
||||||
|
class Status
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
enum
|
||||||
|
{
|
||||||
|
None = 0,
|
||||||
|
Initialised = 1 << 0,
|
||||||
|
Parsed = 1 << 1,
|
||||||
|
Compiled = 1 << 2
|
||||||
|
};
|
||||||
|
};
|
||||||
public:
|
public:
|
||||||
// constructors
|
// constructors
|
||||||
MathCompiler(void);
|
MathCompiler(void);
|
||||||
MathCompiler(const std::string& code_str);
|
MathCompiler(const std::string &code);
|
||||||
// destructor
|
// destructor
|
||||||
~MathCompiler(void);
|
~MathCompiler(void);
|
||||||
// initialization
|
// initialization
|
||||||
void Init(const std::string& code_str);
|
void init(const std::string &code);
|
||||||
const VirtualProgram& operator()(void);
|
const VirtualProgram &operator()(void);
|
||||||
private:
|
private:
|
||||||
// status flags
|
void parse(void);
|
||||||
enum
|
void compile(const MathNode& node);
|
||||||
{
|
void reset(void);
|
||||||
Initialised = 1 << 0,
|
private:
|
||||||
Parsed = 1 << 1,
|
std::istream *code_;
|
||||||
Compiled = 1 << 2
|
std::string codeName_;
|
||||||
};
|
MathParserState *state_;
|
||||||
// private members
|
MathNode *root_;
|
||||||
std::istream* code;
|
VirtualProgram out_;
|
||||||
std::string code_name;
|
unsigned int status_;
|
||||||
MathParserState* state;
|
|
||||||
MathNode* root;
|
|
||||||
VirtualProgram out;
|
|
||||||
unsigned int status;
|
|
||||||
// private methods
|
|
||||||
void Parse(void);
|
|
||||||
void Compile(const MathNode& N);
|
|
||||||
void Reset(void);
|
|
||||||
};
|
};
|
||||||
|
|
||||||
LATAN_END_CPPDECL
|
LATAN_END_CPPDECL
|
||||||
|
@ -1,5 +1,5 @@
|
|||||||
%option reentrant
|
%option reentrant
|
||||||
%option prefix="_Math_"
|
%option prefix="_math_"
|
||||||
%option bison-bridge
|
%option bison-bridge
|
||||||
%option bison-locations
|
%option bison-locations
|
||||||
%option noyywrap
|
%option noyywrap
|
||||||
@ -63,13 +63,13 @@ BLANK [ \t]
|
|||||||
|
|
||||||
%%
|
%%
|
||||||
|
|
||||||
void MathParserState::init_scanner()
|
void MathParserState::initScanner()
|
||||||
{
|
{
|
||||||
yylex_init(&scanner);
|
yylex_init(&scanner);
|
||||||
yyset_extra(this, scanner);
|
yyset_extra(this, scanner);
|
||||||
}
|
}
|
||||||
|
|
||||||
void MathParserState::destroy_scanner()
|
void MathParserState::destroyScanner()
|
||||||
{
|
{
|
||||||
yylex_destroy(scanner);
|
yylex_destroy(scanner);
|
||||||
}
|
}
|
||||||
|
@ -10,7 +10,7 @@
|
|||||||
%}
|
%}
|
||||||
|
|
||||||
%pure-parser
|
%pure-parser
|
||||||
%name-prefix="_Math_"
|
%name-prefix="_math_"
|
||||||
%locations
|
%locations
|
||||||
%defines
|
%defines
|
||||||
%error-verbose
|
%error-verbose
|
||||||
@ -38,13 +38,13 @@
|
|||||||
%type <val_nodept> expr
|
%type <val_nodept> expr
|
||||||
|
|
||||||
%{
|
%{
|
||||||
int _Math_lex(YYSTYPE* lvalp, YYLTYPE* llocp, void* scanner);
|
int _math_lex(YYSTYPE* lvalp, YYLTYPE* llocp, void* scanner);
|
||||||
|
|
||||||
void _Math_error(YYLTYPE* locp, MathParserState* state, const char* err)
|
void _math_error(YYLTYPE* locp, MathParserState* state, const char* err)
|
||||||
{
|
{
|
||||||
stringstream buf;
|
stringstream buf;
|
||||||
|
|
||||||
buf << *state->stream_name << ":" << locp->first_line << ":"\
|
buf << *state->streamName << ":" << locp->first_line << ":"\
|
||||||
<< locp->first_column << ": " << err;
|
<< locp->first_column << ": " << err;
|
||||||
LATAN_ERROR(Parsing,buf.str());
|
LATAN_ERROR(Parsing,buf.str());
|
||||||
}
|
}
|
||||||
@ -56,17 +56,25 @@
|
|||||||
|
|
||||||
program:
|
program:
|
||||||
/* empty string */
|
/* empty string */
|
||||||
| expr {*state->data = $1;}
|
| expr {*state->data = $1;}
|
||||||
;
|
;
|
||||||
|
|
||||||
expr:
|
expr:
|
||||||
FLOAT {$$ = new MathNode($FLOAT,NodeType::Constant);}
|
FLOAT
|
||||||
| ID {$$ = new MathNode($ID,NodeType::Variable);}
|
{$$ = new MathNode($FLOAT, MathNode::Type::Constant);}
|
||||||
| '-' expr %prec UMINUS {$$ = new MathNode("-",NodeType::Operator,1,$2);}
|
| ID
|
||||||
| expr '+' expr {$$ = new MathNode("+",NodeType::Operator,2,$1,$3);}
|
{$$ = new MathNode($ID,MathNode::Type::Variable);}
|
||||||
| expr '-' expr {$$ = new MathNode("-",NodeType::Operator,2,$1,$3);}
|
| '-' expr %prec UMINUS
|
||||||
| expr '*' expr {$$ = new MathNode("*",NodeType::Operator,2,$1,$3);}
|
{$$ = new MathNode("-", MathNode::Type::Operator, 1,$2);}
|
||||||
| expr '/' expr {$$ = new MathNode("/",NodeType::Operator,2,$1,$3);}
|
| expr '+' expr
|
||||||
| expr '^' expr {$$ = new MathNode("^",NodeType::Operator,2,$1,$3);}
|
{$$ = new MathNode("+", MathNode::Type::Operator, 2, $1, $3);}
|
||||||
| '(' expr ')' {$$ = $2;}
|
| expr '-' expr
|
||||||
|
{$$ = new MathNode("-", MathNode::Type::Operator, 2, $1, $3);}
|
||||||
|
| expr '*' expr
|
||||||
|
{$$ = new MathNode("*", MathNode::Type::Operator, 2, $1, $3);}
|
||||||
|
| expr '/' expr
|
||||||
|
{$$ = new MathNode("/", MathNode::Type::Operator, 2, $1, $3);}
|
||||||
|
| expr '^' expr
|
||||||
|
{$$ = new MathNode("^", MathNode::Type::Operator, 2, $1, $3);}
|
||||||
|
| '(' expr ')' {$$ = $2;}
|
||||||
;
|
;
|
||||||
|
@ -12,22 +12,29 @@ class ParserState
|
|||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
// constructor
|
// constructor
|
||||||
explicit ParserState(std::istream* pt_stream, std::string* pt_name,\
|
explicit ParserState(std::istream *streamPt, std::string *namePt,
|
||||||
DataObj* pt_data);
|
DataObj *dataPt);
|
||||||
// destructor
|
// destructor
|
||||||
virtual ~ParserState(void);
|
virtual ~ParserState(void);
|
||||||
// public members
|
private:
|
||||||
|
// scanner allocation/deallocation
|
||||||
|
virtual void initScanner(void) = 0;
|
||||||
|
virtual void destroyScanner(void) = 0;
|
||||||
|
public:
|
||||||
DataObj* data;
|
DataObj* data;
|
||||||
void* scanner;
|
void* scanner;
|
||||||
std::istream* stream;
|
std::istream* stream;
|
||||||
std::string* stream_name;
|
std::string* streamName;
|
||||||
|
|
||||||
};
|
};
|
||||||
|
|
||||||
template <typename DataObj>
|
template <typename DataObj>
|
||||||
ParserState<DataObj>::ParserState(std::istream* pt_stream,\
|
ParserState<DataObj>::ParserState(std::istream *streamPt, std::string *namePt,
|
||||||
std::string* pt_name, \
|
DataObj *dataPt)
|
||||||
DataObj* pt_data)
|
: data(dataPt)
|
||||||
: data(pt_data), scanner(NULL), stream(pt_stream), stream_name(pt_name)
|
, scanner(NULL)
|
||||||
|
, stream(streamPt)
|
||||||
|
, streamName(namePt)
|
||||||
{}
|
{}
|
||||||
|
|
||||||
template <typename DataObj>
|
template <typename DataObj>
|
||||||
|
@ -3,28 +3,28 @@
|
|||||||
|
|
||||||
using namespace Latan;
|
using namespace Latan;
|
||||||
|
|
||||||
Sample::Sample(void)
|
DSample::DSample(void)
|
||||||
: central(0,0), sample(static_cast<ArrayType::Index>(0))
|
: DSampleBase(static_cast<Index>(0))
|
||||||
{}
|
{}
|
||||||
|
|
||||||
Sample::Sample(const unsigned int init_nsample, const unsigned int init_nrow,\
|
DSample::DSample(const unsigned int nSample, const unsigned int nRow,
|
||||||
const unsigned int init_ncol)
|
const unsigned int nCol)
|
||||||
: central(init_nrow,init_ncol), sample(init_nsample)
|
: DSampleBase(static_cast<Index>(nSample + 1))
|
||||||
{
|
{
|
||||||
for (unsigned int s=0;s<init_nsample;++s)
|
for (int s = 0; s < size(); ++s)
|
||||||
{
|
{
|
||||||
sample(s).resize(init_nrow,init_ncol);
|
(*this)(s).resize(nRow, nCol);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
DMat& Sample::operator()(const int s)
|
DMat& DSample::operator()(const int s)
|
||||||
{
|
{
|
||||||
if (s >= 0)
|
if (s >= 0)
|
||||||
{
|
{
|
||||||
return sample(s);
|
return (*this)(s + 1);
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
return central;
|
return (*this)(0);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -8,25 +8,18 @@ LATAN_BEGIN_CPPDECL
|
|||||||
|
|
||||||
const int Central = -1;
|
const int Central = -1;
|
||||||
|
|
||||||
class Sample
|
typedef Eigen::Array<DMat, Eigen::Dynamic, 1> DSampleBase;
|
||||||
|
|
||||||
|
class DSample: public DSampleBase
|
||||||
{
|
{
|
||||||
private:
|
|
||||||
// type alias for disambiguation
|
|
||||||
typedef Eigen::Array<DMat,Eigen::Dynamic,1> ArrayType;
|
|
||||||
|
|
||||||
public:
|
public:
|
||||||
// Constructors/destructor
|
// Constructors/destructor
|
||||||
Sample(void);
|
DSample(void);
|
||||||
Sample(const unsigned int init_nsample, const unsigned int init_nrow,\
|
DSample(const unsigned int nSample, const unsigned int nRow,
|
||||||
const unsigned int init_ncol);
|
const unsigned int nCol);
|
||||||
~Sample(void);
|
~DSample(void);
|
||||||
// Operators
|
// Operators
|
||||||
DMat& operator()(const int s);
|
DMat& operator()(const int s);
|
||||||
|
|
||||||
private:
|
|
||||||
// type aliases
|
|
||||||
DMat central;
|
|
||||||
ArrayType sample;
|
|
||||||
};
|
};
|
||||||
|
|
||||||
LATAN_END_CPPDECL
|
LATAN_END_CPPDECL
|
||||||
|
Loading…
x
Reference in New Issue
Block a user