2014-02-06 18:52:13 +00:00
|
|
|
/*
|
|
|
|
* MathInterpreter.hpp, part of LatAnalyze 3
|
|
|
|
*
|
|
|
|
* Copyright (C) 2013 - 2014 Antonin Portelli
|
|
|
|
*
|
|
|
|
* LatAnalyze 3 is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* LatAnalyze 3 is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with LatAnalyze 3. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef Latan_MathInterpreter_hpp_
|
|
|
|
#define Latan_MathInterpreter_hpp_
|
2014-02-06 18:07:27 +00:00
|
|
|
|
|
|
|
#include <iostream>
|
|
|
|
#include <map>
|
|
|
|
#include <queue>
|
|
|
|
#include <string>
|
|
|
|
#include <stack>
|
2014-03-13 18:51:01 +00:00
|
|
|
#include <LatAnalyze/Function.hpp>
|
|
|
|
#include <LatAnalyze/Global.hpp>
|
|
|
|
#include <LatAnalyze/ParserState.hpp>
|
2014-02-06 18:07:27 +00:00
|
|
|
|
|
|
|
#define MAXIDLENGTH 256
|
|
|
|
|
2015-01-28 17:15:05 +00:00
|
|
|
BEGIN_LATAN_NAMESPACE
|
2014-02-06 18:07:27 +00:00
|
|
|
|
|
|
|
/******************************************************************************
|
2014-09-26 18:41:30 +01:00
|
|
|
* Class for runtime context *
|
2014-02-06 18:07:27 +00:00
|
|
|
******************************************************************************/
|
2014-09-26 18:41:30 +01:00
|
|
|
class RunContext
|
2014-02-06 18:07:27 +00:00
|
|
|
{
|
2014-09-26 18:41:30 +01:00
|
|
|
public:
|
|
|
|
typedef std::map<std::string, unsigned int> AddressTable;
|
|
|
|
public:
|
|
|
|
// constructor
|
|
|
|
RunContext(void) = default;
|
|
|
|
// destructor
|
|
|
|
~RunContext(void) = default;
|
|
|
|
// access
|
|
|
|
unsigned int addFunction(const std::string &name,
|
|
|
|
DoubleFunction *init = nullptr);
|
|
|
|
unsigned int addVariable(const std::string &name,
|
|
|
|
const double init = 0.);
|
|
|
|
DoubleFunction * getFunction(const std::string &name) const;
|
|
|
|
DoubleFunction * getFunction(const unsigned int address) const;
|
|
|
|
unsigned int getFunctionAddress(const std::string &name) const;
|
|
|
|
const AddressTable & getFunctionTable(void) const;
|
|
|
|
unsigned int getInsIndex(void) const;
|
|
|
|
double getVariable(const std::string &name) const;
|
|
|
|
double getVariable(const unsigned int address) const;
|
|
|
|
unsigned int getVariableAddress(const std::string &name) const;
|
|
|
|
const AddressTable & getVariableTable(void) const;
|
|
|
|
void incrementInsIndex(const unsigned int inc = 1);
|
|
|
|
void setFunction(const std::string &name,
|
|
|
|
DoubleFunction *f);
|
|
|
|
void setFunction(const unsigned int address,
|
|
|
|
DoubleFunction *f);
|
|
|
|
void setInsIndex(const unsigned index);
|
|
|
|
void setVariable(const std::string &name,
|
|
|
|
const double value);
|
|
|
|
void setVariable(const unsigned int address,
|
|
|
|
const double value);
|
|
|
|
std::stack<double> & stack(void);
|
|
|
|
// reset
|
|
|
|
void reset(void);
|
|
|
|
private:
|
|
|
|
unsigned int insIndex_;
|
|
|
|
std::stack<double> dStack_;
|
|
|
|
std::vector<double> vMem_;
|
|
|
|
std::vector<DoubleFunction *> fMem_;
|
|
|
|
AddressTable vTable_, fTable_;
|
2014-02-06 18:07:27 +00:00
|
|
|
};
|
|
|
|
|
2014-09-26 18:41:30 +01:00
|
|
|
/******************************************************************************
|
|
|
|
* Instruction classes *
|
|
|
|
******************************************************************************/
|
2014-02-06 18:07:27 +00:00
|
|
|
// Abstract base
|
|
|
|
class Instruction
|
|
|
|
{
|
|
|
|
public:
|
2014-02-18 18:11:08 +00:00
|
|
|
// destructor
|
|
|
|
virtual ~Instruction(void) = default;
|
2014-02-06 18:07:27 +00:00
|
|
|
// instruction execution
|
|
|
|
virtual void operator()(RunContext &context) const = 0;
|
2014-02-13 19:23:39 +00:00
|
|
|
friend std::ostream & operator<<(std::ostream &out, const Instruction &ins);
|
2014-02-06 18:07:27 +00:00
|
|
|
private:
|
|
|
|
virtual void print(std::ostream &out) const = 0;
|
|
|
|
};
|
|
|
|
|
2014-02-13 19:23:39 +00:00
|
|
|
std::ostream & operator<<(std::ostream &out, const Instruction &ins);
|
|
|
|
|
2014-02-18 18:11:08 +00:00
|
|
|
// Instruction container
|
|
|
|
typedef std::vector<std::unique_ptr<const Instruction>> Program;
|
|
|
|
|
2014-02-06 18:07:27 +00:00
|
|
|
// Push
|
|
|
|
class Push: public Instruction
|
|
|
|
{
|
|
|
|
private:
|
2014-02-13 19:23:39 +00:00
|
|
|
enum class ArgType
|
2014-02-06 18:07:27 +00:00
|
|
|
{
|
2014-02-13 19:23:39 +00:00
|
|
|
Constant = 0,
|
|
|
|
Variable = 1
|
2014-02-06 18:07:27 +00:00
|
|
|
};
|
|
|
|
public:
|
|
|
|
//constructors
|
|
|
|
explicit Push(const double val);
|
2014-04-08 19:48:52 +01:00
|
|
|
explicit Push(const unsigned int address, const std::string &name);
|
2014-02-06 18:07:27 +00:00
|
|
|
// instruction execution
|
|
|
|
virtual void operator()(RunContext &context) const;
|
|
|
|
private:
|
|
|
|
virtual void print(std::ostream& out) const;
|
|
|
|
private:
|
2014-04-08 19:48:52 +01:00
|
|
|
ArgType type_;
|
|
|
|
double val_;
|
|
|
|
unsigned int address_;
|
|
|
|
std::string name_;
|
2014-02-06 18:07:27 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
// Pop
|
|
|
|
class Pop: public Instruction
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
//constructor
|
2014-04-08 19:48:52 +01:00
|
|
|
explicit Pop(const unsigned int address, const std::string &name);
|
2014-02-06 18:07:27 +00:00
|
|
|
// instruction execution
|
|
|
|
virtual void operator()(RunContext &context) const;
|
|
|
|
private:
|
|
|
|
virtual void print(std::ostream& out) const;
|
|
|
|
private:
|
2014-04-08 19:48:52 +01:00
|
|
|
unsigned int address_;
|
2014-02-06 18:07:27 +00:00
|
|
|
std::string name_;
|
|
|
|
};
|
|
|
|
|
|
|
|
// Store
|
|
|
|
class Store: public Instruction
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
//constructor
|
2014-04-08 19:48:52 +01:00
|
|
|
explicit Store(const unsigned int address, const std::string &name);
|
2014-02-06 18:07:27 +00:00
|
|
|
// instruction execution
|
|
|
|
virtual void operator()(RunContext &context) const;
|
|
|
|
private:
|
|
|
|
virtual void print(std::ostream& out) const;
|
|
|
|
private:
|
2014-04-08 19:48:52 +01:00
|
|
|
unsigned int address_;
|
2014-02-06 18:07:27 +00:00
|
|
|
std::string name_;
|
|
|
|
};
|
|
|
|
|
|
|
|
// Call function
|
|
|
|
class Call: public Instruction
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
//constructor
|
2014-04-08 19:48:52 +01:00
|
|
|
explicit Call(const unsigned int address, const std::string &name);
|
2014-02-06 18:07:27 +00:00
|
|
|
// instruction execution
|
|
|
|
virtual void operator()(RunContext &context) const;
|
|
|
|
private:
|
|
|
|
virtual void print(std::ostream& out) const;
|
|
|
|
private:
|
2014-04-08 19:48:52 +01:00
|
|
|
unsigned int address_;
|
2014-02-06 18:07:27 +00:00
|
|
|
std::string name_;
|
|
|
|
};
|
|
|
|
|
|
|
|
// Floating point operations
|
|
|
|
#define DECL_OP(name)\
|
|
|
|
class name: public Instruction\
|
|
|
|
{\
|
|
|
|
public:\
|
2014-03-03 14:21:37 +00:00
|
|
|
virtual void operator()(RunContext &context) const;\
|
2014-02-06 18:07:27 +00:00
|
|
|
private:\
|
|
|
|
virtual void print(std::ostream &out) const;\
|
|
|
|
}
|
|
|
|
|
|
|
|
DECL_OP(Neg);
|
|
|
|
DECL_OP(Add);
|
|
|
|
DECL_OP(Sub);
|
|
|
|
DECL_OP(Mul);
|
|
|
|
DECL_OP(Div);
|
|
|
|
DECL_OP(Pow);
|
|
|
|
|
2014-02-18 18:11:08 +00:00
|
|
|
/******************************************************************************
|
|
|
|
* Expression node classes *
|
|
|
|
******************************************************************************/
|
|
|
|
class ExprNode
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
// constructors
|
2014-03-03 14:21:37 +00:00
|
|
|
explicit ExprNode(const std::string &name);
|
2014-02-18 18:11:08 +00:00
|
|
|
// destructor
|
|
|
|
virtual ~ExprNode() = default;
|
|
|
|
// access
|
|
|
|
const std::string& getName(void) const;
|
2014-02-26 18:36:29 +00:00
|
|
|
Index getNArg(void) const;
|
2014-02-18 18:11:08 +00:00
|
|
|
const ExprNode * getParent(void) const;
|
2014-02-26 18:36:29 +00:00
|
|
|
Index getLevel(void) const;
|
2014-02-18 18:11:08 +00:00
|
|
|
void setName(const std::string &name);
|
|
|
|
void pushArg(ExprNode *node);
|
|
|
|
// operator
|
2014-02-26 18:36:29 +00:00
|
|
|
const ExprNode &operator[](const Index i) const;
|
2014-02-18 18:11:08 +00:00
|
|
|
// compile
|
2014-09-26 18:41:30 +01:00
|
|
|
virtual void compile(Program &program, RunContext &context) const = 0;
|
2014-02-18 18:11:08 +00:00
|
|
|
private:
|
|
|
|
std::string name_;
|
|
|
|
std::vector<std::unique_ptr<ExprNode>> arg_;
|
|
|
|
const ExprNode * parent_;
|
|
|
|
};
|
|
|
|
|
|
|
|
std::ostream &operator<<(std::ostream &out, const ExprNode &n);
|
|
|
|
|
|
|
|
#define DECL_NODE(base, name) \
|
|
|
|
class name: public base\
|
|
|
|
{\
|
|
|
|
public:\
|
|
|
|
using base::base;\
|
2014-09-26 18:41:30 +01:00
|
|
|
virtual void compile(Program &program, RunContext &context) const;\
|
2014-02-18 18:11:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
DECL_NODE(ExprNode, VarNode);
|
|
|
|
DECL_NODE(ExprNode, CstNode);
|
|
|
|
DECL_NODE(ExprNode, SemicolonNode);
|
|
|
|
DECL_NODE(ExprNode, AssignNode);
|
|
|
|
DECL_NODE(ExprNode, MathOpNode);
|
|
|
|
DECL_NODE(ExprNode, FuncNode);
|
|
|
|
|
|
|
|
class KeywordNode: public ExprNode
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
using ExprNode::ExprNode;
|
2014-09-26 18:41:30 +01:00
|
|
|
virtual void compile(Program &program, RunContext &context) const = 0;
|
2014-02-18 18:11:08 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
DECL_NODE(KeywordNode, ReturnNode);
|
|
|
|
|
2014-02-06 18:07:27 +00:00
|
|
|
/******************************************************************************
|
2014-02-10 11:22:56 +00:00
|
|
|
* Interpreter class *
|
2014-02-06 18:07:27 +00:00
|
|
|
******************************************************************************/
|
|
|
|
class MathInterpreter
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
// parser state
|
2014-02-18 18:11:08 +00:00
|
|
|
class MathParserState: public ParserState<std::unique_ptr<ExprNode>>
|
2014-02-06 18:07:27 +00:00
|
|
|
{
|
|
|
|
public:
|
|
|
|
// constructor
|
|
|
|
explicit MathParserState(std::istream *stream, std::string *name,
|
2014-02-18 18:11:08 +00:00
|
|
|
std::unique_ptr<ExprNode> *data);
|
2014-02-06 18:07:27 +00:00
|
|
|
// destructor
|
|
|
|
virtual ~MathParserState(void);
|
|
|
|
private:
|
|
|
|
// allocation/deallocation functions defined in MathLexer.lpp
|
|
|
|
virtual void initScanner(void);
|
|
|
|
virtual void destroyScanner(void);
|
|
|
|
};
|
|
|
|
private:
|
|
|
|
// status flags
|
|
|
|
class Status
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
enum
|
|
|
|
{
|
|
|
|
none = 0,
|
|
|
|
initialised = 1 << 0,
|
|
|
|
parsed = 1 << 1,
|
|
|
|
compiled = 1 << 2
|
|
|
|
};
|
|
|
|
};
|
|
|
|
public:
|
|
|
|
// constructors
|
2014-03-03 14:21:37 +00:00
|
|
|
MathInterpreter(void) = default;
|
2014-02-06 18:07:27 +00:00
|
|
|
MathInterpreter(const std::string &code);
|
|
|
|
// destructor
|
2014-02-20 22:54:11 +00:00
|
|
|
~MathInterpreter(void) = default;
|
2014-02-06 18:07:27 +00:00
|
|
|
// access
|
2014-02-26 18:36:29 +00:00
|
|
|
const Instruction * operator[](const Index i) const;
|
2014-02-18 18:11:08 +00:00
|
|
|
const ExprNode * getAST(void) const;
|
2014-02-06 18:07:27 +00:00
|
|
|
// initialization
|
|
|
|
void setCode(const std::string &code);
|
|
|
|
// interpreter
|
2014-04-08 19:48:52 +01:00
|
|
|
void compile(RunContext &context);
|
2014-02-06 18:07:27 +00:00
|
|
|
// execution
|
|
|
|
void operator()(RunContext &context);
|
|
|
|
// IO
|
2014-02-13 19:23:39 +00:00
|
|
|
friend std::ostream & operator<<(std::ostream &out,
|
|
|
|
const MathInterpreter &program);
|
2014-02-06 18:07:27 +00:00
|
|
|
private:
|
|
|
|
// initialization
|
|
|
|
void reset(void);
|
|
|
|
// access
|
|
|
|
void push(const Instruction *i);
|
|
|
|
// parser
|
|
|
|
void parse(void);
|
|
|
|
// interpreter
|
2014-02-18 18:11:08 +00:00
|
|
|
void compileNode(const ExprNode &node);
|
2014-02-06 18:07:27 +00:00
|
|
|
// execution
|
|
|
|
void execute(RunContext &context) const;
|
|
|
|
private:
|
2014-03-03 14:21:37 +00:00
|
|
|
std::unique_ptr<std::istream> code_{nullptr};
|
|
|
|
std::string codeName_{"<no_code>"};
|
|
|
|
std::unique_ptr<MathParserState> state_{nullptr};
|
|
|
|
std::unique_ptr<ExprNode> root_{nullptr};
|
2014-02-13 19:23:39 +00:00
|
|
|
Program program_;
|
2014-03-03 14:21:37 +00:00
|
|
|
unsigned int status_{Status::none};
|
2014-02-06 18:07:27 +00:00
|
|
|
};
|
|
|
|
|
2014-02-13 19:23:39 +00:00
|
|
|
std::ostream & operator<<(std::ostream &out, const MathInterpreter &program);
|
|
|
|
|
2015-01-28 17:15:05 +00:00
|
|
|
END_LATAN_NAMESPACE
|
2014-02-06 18:07:27 +00:00
|
|
|
|
2014-02-06 18:52:13 +00:00
|
|
|
#endif // Latan_MathInterpreter_hpp_
|