2014-02-06 18:52:13 +00:00
|
|
|
/*
|
|
|
|
* MathInterpreter.cpp, 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/>.
|
|
|
|
*/
|
|
|
|
|
2014-02-06 18:07:27 +00:00
|
|
|
#include <latan/MathInterpreter.hpp>
|
|
|
|
#include <latan/includes.hpp>
|
|
|
|
|
|
|
|
using namespace std;
|
|
|
|
using namespace Latan;
|
|
|
|
|
|
|
|
/******************************************************************************
|
|
|
|
* Instruction set *
|
|
|
|
******************************************************************************/
|
|
|
|
#define CODE_WIDTH 6
|
|
|
|
#define CODE_MOD setw(CODE_WIDTH) << left
|
|
|
|
|
2014-02-18 18:11:08 +00:00
|
|
|
// Instruction operator ////////////////////////////////////////////////////////
|
2014-02-06 18:07:27 +00:00
|
|
|
ostream &Latan::operator<<(ostream& out, const Instruction& ins)
|
|
|
|
{
|
|
|
|
ins.print(out);
|
|
|
|
|
|
|
|
return out;
|
|
|
|
}
|
|
|
|
|
2014-02-18 18:11:08 +00:00
|
|
|
// Push constructors ///////////////////////////////////////////////////////////
|
2014-02-06 18:07:27 +00:00
|
|
|
Push::Push(const double val)
|
|
|
|
: type_(ArgType::Constant)
|
|
|
|
, val_(val)
|
|
|
|
, name_("")
|
|
|
|
{}
|
|
|
|
|
|
|
|
Push::Push(const string &name)
|
|
|
|
: type_(ArgType::Variable)
|
|
|
|
, val_(0.0)
|
|
|
|
, name_(name)
|
|
|
|
{}
|
|
|
|
|
2014-02-18 18:11:08 +00:00
|
|
|
// Push execution //////////////////////////////////////////////////////////////
|
2014-02-06 18:07:27 +00:00
|
|
|
void Push::operator()(RunContext &context) const
|
|
|
|
{
|
|
|
|
if (type_ == ArgType::Constant)
|
|
|
|
{
|
|
|
|
context.dStack.push(val_);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2014-02-13 19:23:39 +00:00
|
|
|
try
|
2014-02-06 18:07:27 +00:00
|
|
|
{
|
2014-02-13 19:23:39 +00:00
|
|
|
context.dStack.push(context.vTable.at(name_));
|
2014-02-06 18:07:27 +00:00
|
|
|
}
|
2014-02-13 19:23:39 +00:00
|
|
|
catch (out_of_range)
|
2014-02-06 18:07:27 +00:00
|
|
|
{
|
|
|
|
LATAN_ERROR(Range, "unknown variable '" + name_ + "'");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
context.insIndex++;
|
|
|
|
}
|
|
|
|
|
2014-02-18 18:11:08 +00:00
|
|
|
// Push print //////////////////////////////////////////////////////////////////
|
2014-02-13 19:23:39 +00:00
|
|
|
void Push::print(ostream &out) const
|
2014-02-06 18:07:27 +00:00
|
|
|
{
|
|
|
|
out << CODE_MOD << "push";
|
|
|
|
if (type_ == ArgType::Constant)
|
|
|
|
{
|
|
|
|
out << CODE_MOD << val_;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
out << CODE_MOD << name_;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-02-18 18:11:08 +00:00
|
|
|
// Pop constructor /////////////////////////////////////////////////////////////
|
2014-02-06 18:07:27 +00:00
|
|
|
Pop::Pop(const string &name)
|
|
|
|
: name_(name)
|
|
|
|
{}
|
|
|
|
|
2014-02-18 18:11:08 +00:00
|
|
|
// Pop execution ///////////////////////////////////////////////////////////////
|
2014-02-06 18:07:27 +00:00
|
|
|
void Pop::operator()(RunContext &context) const
|
|
|
|
{
|
|
|
|
if (!name_.empty())
|
|
|
|
{
|
|
|
|
context.vTable[name_] = context.dStack.top();
|
|
|
|
}
|
|
|
|
context.dStack.pop();
|
|
|
|
context.insIndex++;
|
|
|
|
}
|
|
|
|
|
2014-02-18 18:11:08 +00:00
|
|
|
// Pop print ///////////////////////////////////////////////////////////////////
|
2014-02-13 19:23:39 +00:00
|
|
|
void Pop::print(ostream &out) const
|
2014-02-06 18:07:27 +00:00
|
|
|
{
|
|
|
|
out << CODE_MOD << "pop" << CODE_MOD << name_;
|
|
|
|
}
|
|
|
|
|
2014-02-18 18:11:08 +00:00
|
|
|
// Store constructor ///////////////////////////////////////////////////////////
|
2014-02-06 18:07:27 +00:00
|
|
|
Store::Store(const string &name)
|
|
|
|
: name_(name)
|
|
|
|
{}
|
|
|
|
|
2014-02-18 18:11:08 +00:00
|
|
|
// Store execution /////////////////////////////////////////////////////////////
|
2014-02-06 18:07:27 +00:00
|
|
|
void Store::operator()(RunContext &context) const
|
|
|
|
{
|
|
|
|
if (!name_.empty())
|
|
|
|
{
|
|
|
|
context.vTable[name_] = context.dStack.top();
|
|
|
|
}
|
|
|
|
context.insIndex++;
|
|
|
|
}
|
|
|
|
|
2014-02-18 18:11:08 +00:00
|
|
|
// Store print /////////////////////////////////////////////////////////////////
|
2014-02-13 19:23:39 +00:00
|
|
|
void Store::print(ostream &out) const
|
2014-02-06 18:07:27 +00:00
|
|
|
{
|
|
|
|
out << CODE_MOD << "store" << CODE_MOD << name_;
|
|
|
|
}
|
|
|
|
|
2014-02-18 18:11:08 +00:00
|
|
|
// Call constructor ////////////////////////////////////////////////////////////
|
2014-02-06 18:07:27 +00:00
|
|
|
Call::Call(const string &name)
|
|
|
|
: name_(name)
|
|
|
|
{}
|
|
|
|
|
2014-02-18 18:11:08 +00:00
|
|
|
// Call execution //////////////////////////////////////////////////////////////
|
2014-02-06 18:07:27 +00:00
|
|
|
void Call::operator()(RunContext &context) const
|
|
|
|
{
|
2014-02-13 19:23:39 +00:00
|
|
|
try
|
2014-02-06 18:07:27 +00:00
|
|
|
{
|
2014-02-13 19:23:39 +00:00
|
|
|
context.dStack.push((*context.fTable.at(name_))(context.dStack));
|
2014-02-06 18:07:27 +00:00
|
|
|
}
|
2014-02-13 19:23:39 +00:00
|
|
|
catch (out_of_range)
|
2014-02-06 18:07:27 +00:00
|
|
|
{
|
|
|
|
LATAN_ERROR(Range, "unknown function '" + name_ + "'");
|
|
|
|
}
|
|
|
|
context.insIndex++;
|
|
|
|
}
|
|
|
|
|
2014-02-18 18:11:08 +00:00
|
|
|
// Call print //////////////////////////////////////////////////////////////////
|
2014-02-13 19:23:39 +00:00
|
|
|
void Call::print(ostream &out) const
|
2014-02-06 18:07:27 +00:00
|
|
|
{
|
|
|
|
out << CODE_MOD << "call" << CODE_MOD << name_;
|
|
|
|
}
|
|
|
|
|
2014-02-18 18:11:08 +00:00
|
|
|
// Math operations /////////////////////////////////////////////////////////////
|
2014-02-06 18:07:27 +00:00
|
|
|
#define DEF_OP(name, nArg, exp, insName)\
|
|
|
|
void name::operator()(RunContext &context) const\
|
|
|
|
{\
|
|
|
|
double x[nArg];\
|
|
|
|
for (int i = 0; i < nArg; ++i)\
|
|
|
|
{\
|
|
|
|
x[nArg-1-i] = context.dStack.top();\
|
|
|
|
context.dStack.pop();\
|
|
|
|
}\
|
|
|
|
context.dStack.push(exp);\
|
|
|
|
context.insIndex++;\
|
|
|
|
}\
|
2014-02-13 19:23:39 +00:00
|
|
|
void name::print(ostream &out) const\
|
2014-02-06 18:07:27 +00:00
|
|
|
{\
|
|
|
|
out << CODE_MOD << insName;\
|
|
|
|
}
|
|
|
|
|
|
|
|
DEF_OP(Neg, 1, -x[0], "neg")
|
|
|
|
DEF_OP(Add, 2, x[0] + x[1], "add")
|
|
|
|
DEF_OP(Sub, 2, x[0] - x[1], "sub")
|
|
|
|
DEF_OP(Mul, 2, x[0]*x[1], "mul")
|
|
|
|
DEF_OP(Div, 2, x[0]/x[1], "div")
|
|
|
|
DEF_OP(Pow, 2, pow(x[0],x[1]), "pow")
|
|
|
|
|
2014-02-18 18:11:08 +00:00
|
|
|
/******************************************************************************
|
|
|
|
* ExprNode implementation *
|
|
|
|
******************************************************************************/
|
|
|
|
// ExprNode constructors ///////////////////////////////////////////////////////
|
|
|
|
ExprNode::ExprNode(const string &name)
|
|
|
|
: name_(name)
|
|
|
|
, parent_(nullptr)
|
|
|
|
{}
|
|
|
|
|
|
|
|
// ExprNode access /////////////////////////////////////////////////////////////
|
|
|
|
const string &ExprNode::getName(void) const
|
|
|
|
{
|
|
|
|
return name_;
|
|
|
|
}
|
|
|
|
|
|
|
|
unsigned int ExprNode::getNArg(void) const
|
|
|
|
{
|
|
|
|
return static_cast<unsigned int>(arg_.size());
|
|
|
|
}
|
|
|
|
|
|
|
|
const ExprNode * ExprNode::getParent(void) const
|
|
|
|
{
|
|
|
|
return parent_;
|
|
|
|
}
|
|
|
|
|
|
|
|
unsigned int ExprNode::getLevel(void) const
|
|
|
|
{
|
|
|
|
if (getParent())
|
|
|
|
{
|
|
|
|
return getParent()->getLevel() + 1;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void ExprNode::setName(const std::string &name)
|
|
|
|
{
|
|
|
|
name_ = name;
|
|
|
|
}
|
|
|
|
|
|
|
|
void ExprNode::pushArg(ExprNode *node)
|
|
|
|
{
|
|
|
|
if (node)
|
|
|
|
{
|
|
|
|
node->parent_ = this;
|
|
|
|
arg_.push_back(unique_ptr<ExprNode>(node));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// ExprNode operators //////////////////////////////////////////////////////////
|
|
|
|
const ExprNode &ExprNode::operator[](const unsigned int i) const
|
|
|
|
{
|
|
|
|
return *arg_[i];
|
|
|
|
}
|
|
|
|
|
|
|
|
ostream &Latan::operator<<(ostream &out, const ExprNode &n)
|
|
|
|
{
|
|
|
|
unsigned int level = n.getLevel();
|
|
|
|
|
|
|
|
for (unsigned int i = 0; i <= level; ++i)
|
|
|
|
{
|
|
|
|
if (i == level)
|
|
|
|
{
|
|
|
|
out << "_";
|
|
|
|
}
|
|
|
|
else if (i == level - 1)
|
|
|
|
{
|
|
|
|
out << "|";
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
out << " ";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
out << " " << n.getName() << endl;
|
|
|
|
for (unsigned int i = 0; i < n.getNArg(); ++i)
|
|
|
|
{
|
|
|
|
out << n[i];
|
|
|
|
}
|
|
|
|
|
|
|
|
return out;
|
|
|
|
}
|
|
|
|
|
|
|
|
#define PUSH_INS(program, type, ...) \
|
|
|
|
program.push_back(unique_ptr<type>(new type(__VA_ARGS__)))
|
|
|
|
|
|
|
|
// VarNode compile /////////////////////////////////////////////////////////////
|
|
|
|
void VarNode::compile(Program &program) const
|
|
|
|
{
|
|
|
|
PUSH_INS(program, Push, getName());
|
|
|
|
}
|
|
|
|
|
|
|
|
// CstNode compile /////////////////////////////////////////////////////////////
|
|
|
|
void CstNode::compile(Program &program) const
|
|
|
|
{
|
|
|
|
PUSH_INS(program, Push, strTo<double>(getName()));
|
|
|
|
}
|
|
|
|
|
|
|
|
// SemicolonNode compile ///////////////////////////////////////////////////////
|
|
|
|
void SemicolonNode::compile(Program &program) const
|
|
|
|
{
|
|
|
|
auto &n = *this;
|
|
|
|
|
|
|
|
for (unsigned int i = 0; i < getNArg(); ++i)
|
|
|
|
{
|
|
|
|
bool isAssign = isDerivedFrom<AssignNode>(&n[i]);
|
|
|
|
bool isSemiColumn = isDerivedFrom<SemicolonNode>(&n[i]);
|
|
|
|
bool isKeyword = isDerivedFrom<KeywordNode>(&n[i]);
|
|
|
|
|
|
|
|
if (isAssign||isSemiColumn||isKeyword)
|
|
|
|
{
|
|
|
|
n[i].compile(program);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// AssignNode compile //////////////////////////////////////////////////////////
|
|
|
|
void AssignNode::compile(Program &program) const
|
|
|
|
{
|
|
|
|
auto &n = *this;
|
|
|
|
|
|
|
|
if (isDerivedFrom<VarNode>(&n[0]))
|
|
|
|
{
|
|
|
|
bool hasSemicolonParent = isDerivedFrom<SemicolonNode>(getParent());
|
|
|
|
|
|
|
|
n[1].compile(program);
|
|
|
|
if (hasSemicolonParent)
|
|
|
|
{
|
|
|
|
PUSH_INS(program, Pop, n[0].getName());
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
PUSH_INS(program, Store, n[0].getName());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
LATAN_ERROR(Compilation, "invalid LHS for '='");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// MathOpNode compile //////////////////////////////////////////////////////////
|
|
|
|
#define IFNODE(name, nArg) if ((n.getName() == (name))&&(n.getNArg() == nArg))
|
|
|
|
#define ELIFNODE(name, nArg) else IFNODE(name, nArg)
|
|
|
|
#define ELSE else
|
|
|
|
|
|
|
|
void MathOpNode::compile(Program &program) const
|
|
|
|
{
|
|
|
|
auto &n = *this;
|
|
|
|
|
|
|
|
for (unsigned int i = 0; i < n.getNArg(); ++i)
|
|
|
|
{
|
|
|
|
n[i].compile(program);
|
|
|
|
}
|
|
|
|
IFNODE("-", 1) PUSH_INS(program, Neg,);
|
|
|
|
ELIFNODE("+", 2) PUSH_INS(program, Add,);
|
|
|
|
ELIFNODE("-", 2) PUSH_INS(program, Sub,);
|
|
|
|
ELIFNODE("*", 2) PUSH_INS(program, Mul,);
|
|
|
|
ELIFNODE("/", 2) PUSH_INS(program, Div,);
|
|
|
|
ELIFNODE("^", 2) PUSH_INS(program, Pow,);
|
|
|
|
ELSE LATAN_ERROR(Compilation, "unknown operator '" + getName() + "'");
|
|
|
|
}
|
|
|
|
|
|
|
|
// FuncNode compile ////////////////////////////////////////////////////////////
|
|
|
|
void FuncNode::compile(Program &program) const
|
|
|
|
{
|
|
|
|
auto &n = *this;
|
|
|
|
|
|
|
|
for (unsigned int i = 0; i < n.getNArg(); ++i)
|
|
|
|
{
|
|
|
|
n[i].compile(program);
|
|
|
|
}
|
|
|
|
PUSH_INS(program, Call, getName());
|
|
|
|
}
|
|
|
|
|
|
|
|
// ReturnNode compile ////////////////////////////////////////////////////////////
|
|
|
|
void ReturnNode::compile(Program &program) const
|
|
|
|
{
|
|
|
|
auto &n = *this;
|
|
|
|
|
|
|
|
n[0].compile(program);
|
|
|
|
program.push_back(nullptr);
|
|
|
|
}
|
|
|
|
|
2014-02-06 18:07:27 +00:00
|
|
|
/******************************************************************************
|
|
|
|
* MathInterpreter implementation *
|
|
|
|
******************************************************************************/
|
|
|
|
// MathParserState constructor /////////////////////////////////////////////////
|
2014-02-13 19:23:39 +00:00
|
|
|
MathInterpreter::MathParserState::MathParserState
|
2014-02-18 18:11:08 +00:00
|
|
|
(istream *stream, string *name, std::unique_ptr<ExprNode> *data)
|
|
|
|
: ParserState<std::unique_ptr<ExprNode>>(stream, name, data)
|
2014-02-06 18:07:27 +00:00
|
|
|
{
|
|
|
|
initScanner();
|
|
|
|
}
|
|
|
|
|
|
|
|
// MathParserState destructor //////////////////////////////////////////////////
|
|
|
|
MathInterpreter::MathParserState::~MathParserState(void)
|
|
|
|
{
|
|
|
|
destroyScanner();
|
|
|
|
}
|
|
|
|
|
|
|
|
// constructors ////////////////////////////////////////////////////////////////
|
|
|
|
MathInterpreter::MathInterpreter(void)
|
2014-02-13 19:23:39 +00:00
|
|
|
: code_(nullptr)
|
2014-02-06 18:07:27 +00:00
|
|
|
, codeName_("<no_code>")
|
2014-02-13 19:23:39 +00:00
|
|
|
, state_(nullptr)
|
|
|
|
, root_(nullptr)
|
2014-02-06 18:07:27 +00:00
|
|
|
, status_(Status::none)
|
|
|
|
{}
|
|
|
|
|
|
|
|
MathInterpreter::MathInterpreter(const std::string &code)
|
2014-02-13 19:23:39 +00:00
|
|
|
: code_(nullptr)
|
2014-02-06 18:07:27 +00:00
|
|
|
, codeName_("<string>")
|
2014-02-13 19:23:39 +00:00
|
|
|
, state_(nullptr)
|
|
|
|
, root_(nullptr)
|
2014-02-06 18:07:27 +00:00
|
|
|
, status_(Status::none)
|
|
|
|
{
|
|
|
|
setCode(code);
|
|
|
|
}
|
|
|
|
|
|
|
|
// destructor //////////////////////////////////////////////////////////////////
|
|
|
|
MathInterpreter::~MathInterpreter(void)
|
2014-02-13 19:23:39 +00:00
|
|
|
{}
|
2014-02-06 18:07:27 +00:00
|
|
|
|
|
|
|
// access //////////////////////////////////////////////////////////////////////
|
|
|
|
const Instruction * MathInterpreter::operator[](const unsigned int i) const
|
|
|
|
{
|
2014-02-13 19:23:39 +00:00
|
|
|
return program_[i].get();
|
2014-02-06 18:07:27 +00:00
|
|
|
}
|
|
|
|
|
2014-02-18 18:11:08 +00:00
|
|
|
const ExprNode * MathInterpreter::getAST(void) const
|
2014-02-06 18:07:27 +00:00
|
|
|
{
|
2014-02-13 19:23:39 +00:00
|
|
|
return root_.get();
|
2014-02-06 18:07:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void MathInterpreter::push(const Instruction *i)
|
|
|
|
{
|
2014-02-13 19:23:39 +00:00
|
|
|
program_.push_back(unique_ptr<const Instruction>(i));
|
2014-02-06 18:07:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// initialization //////////////////////////////////////////////////////////////
|
|
|
|
void MathInterpreter::setCode(const std::string &code)
|
|
|
|
{
|
|
|
|
if (status_)
|
|
|
|
{
|
|
|
|
reset();
|
|
|
|
}
|
2014-02-13 19:23:39 +00:00
|
|
|
code_.reset(new stringstream(code));
|
2014-02-06 18:07:27 +00:00
|
|
|
codeName_ = "<string>";
|
2014-02-13 19:23:39 +00:00
|
|
|
state_.reset(new MathParserState(code_.get(), &codeName_, &root_));
|
2014-02-18 18:11:08 +00:00
|
|
|
program_.clear();
|
2014-02-06 18:07:27 +00:00
|
|
|
status_ = Status::initialised;
|
|
|
|
}
|
|
|
|
|
|
|
|
void MathInterpreter::reset(void)
|
|
|
|
{
|
2014-02-13 23:14:28 +00:00
|
|
|
code_.reset();
|
2014-02-06 18:07:27 +00:00
|
|
|
codeName_ = "<no_code>";
|
2014-02-13 23:14:28 +00:00
|
|
|
state_.reset();
|
|
|
|
root_.reset();
|
2014-02-06 18:07:27 +00:00
|
|
|
program_.clear();
|
|
|
|
status_ = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
// parser //////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
// Bison/Flex parser declaration
|
2014-02-13 19:23:39 +00:00
|
|
|
int _math_parse(MathInterpreter::MathParserState *state);
|
2014-02-06 18:07:27 +00:00
|
|
|
|
|
|
|
void MathInterpreter::parse(void)
|
|
|
|
{
|
2014-02-13 19:23:39 +00:00
|
|
|
_math_parse(state_.get());
|
2014-02-06 18:07:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// interpreter /////////////////////////////////////////////////////////////////
|
|
|
|
void MathInterpreter::compile(void)
|
|
|
|
{
|
2014-02-18 18:11:08 +00:00
|
|
|
bool gotReturn = false;
|
|
|
|
|
2014-02-06 18:07:27 +00:00
|
|
|
if (!(status_ & Status::parsed))
|
|
|
|
{
|
|
|
|
parse();
|
|
|
|
status_ |= Status::parsed;
|
|
|
|
status_ -= status_ & Status::compiled;
|
|
|
|
}
|
|
|
|
if (root_)
|
|
|
|
{
|
2014-02-18 18:11:08 +00:00
|
|
|
root_->compile(program_);
|
|
|
|
for (unsigned int i = 0; i < program_.size(); ++i)
|
|
|
|
{
|
|
|
|
if (!program_[i])
|
|
|
|
{
|
|
|
|
gotReturn = true;
|
|
|
|
program_.resize(i);
|
|
|
|
program_.shrink_to_fit();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-02-06 18:07:27 +00:00
|
|
|
}
|
2014-02-18 18:11:08 +00:00
|
|
|
if (!root_||!gotReturn)
|
2014-02-06 18:07:27 +00:00
|
|
|
{
|
|
|
|
LATAN_ERROR(Syntax, "expected 'return' in program '" + codeName_ + "'");
|
|
|
|
}
|
|
|
|
status_ |= Status::compiled;
|
|
|
|
}
|
|
|
|
|
|
|
|
// execution ///////////////////////////////////////////////////////////////////
|
|
|
|
void MathInterpreter::operator()(RunContext &context)
|
|
|
|
{
|
|
|
|
if (!(status_ & Status::compiled))
|
|
|
|
{
|
|
|
|
compile();
|
|
|
|
}
|
|
|
|
execute(context);
|
|
|
|
}
|
|
|
|
|
|
|
|
void MathInterpreter::execute(RunContext &context) const
|
|
|
|
{
|
|
|
|
context.insIndex = 0;
|
|
|
|
while (context.insIndex != program_.size())
|
|
|
|
{
|
|
|
|
(*(program_[context.insIndex]))(context);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// IO //////////////////////////////////////////////////////////////////////////
|
|
|
|
ostream &Latan::operator<<(ostream &out, const MathInterpreter &program)
|
|
|
|
{
|
|
|
|
for (unsigned int i = 0; i < program.program_.size(); ++i)
|
|
|
|
{
|
|
|
|
out << *(program.program_[i]) << endl;
|
|
|
|
}
|
|
|
|
|
|
|
|
return out;
|
|
|
|
}
|