1
0
mirror of https://github.com/aportelli/LatAnalyze.git synced 2024-11-10 00:45:36 +00:00

math compiler fixes and improvements, now working

This commit is contained in:
Antonin Portelli 2014-02-04 17:33:58 +00:00
parent 36bc0bc9b0
commit 50d21ae904
7 changed files with 249 additions and 146 deletions

2
.gitignore vendored
View File

@ -3,7 +3,7 @@
*.a
*.so
*.dylib
examples/ex_test
examples/exMathCompiler
sandbox/*
# Apple stuffs

View File

@ -15,10 +15,10 @@ endif
endif
noinst_PROGRAMS = \
ex_test
exMathCompiler
ex_test_SOURCES = ex_test.cpp
ex_test_CFLAGS = -g -O2
ex_test_LDFLAGS = -L../latan/.libs -llatan
exMathCompiler_SOURCES = exMathCompiler.cpp
exMathCompiler_CFLAGS = -g -O2
exMathCompiler_LDFLAGS = -L../latan/.libs -llatan
ACLOCAL_AMFLAGS = -I .buildutils/m4

View File

@ -1,5 +1,5 @@
#include <iostream>
#include <latan/Function.hpp>
#include <latan/Math.hpp>
#include <latan/MathCompiler.hpp>
using namespace std;
@ -7,22 +7,31 @@ using namespace Latan;
int main(int argc, char* argv[])
{
MathCompiler C(argv[1]);
string source;
if (argc != 2)
{
cerr << "usage: " << argv[0] << " <program>" << endl;
}
source = argv[1];
MathCompiler C(source);
VarTable vtable;
FunctionTable ftable;
stack<double> dstack;
const VirtualProgram& P = C();
ftable["exp"] = &StdMath::exp;
ftable["atan2"] = &StdMath::atan2;
cout << P << endl;
cout << "-- Source code:" << endl << source << endl << endl;
cout << "-- Abstract Syntax Tree:" << endl << *C.getAST() << endl;
cout << "-- Program:" << endl << P << endl;
StdMath::addStdMathFunc(ftable);
for (unsigned int i=0;i<P.size();++i)
{
(*(P[i]))(dstack,vtable,ftable);
(*(P[i]))(dstack, vtable, ftable);
}
if (!dstack.empty())
{
cout << "result= " << dstack.top() << endl;
cout << "-- Result: " << dstack.top() << endl;
}
return EXIT_SUCCESS;

View File

@ -62,6 +62,23 @@ unsigned int MathNode::getNArg(void) const
return static_cast<unsigned int>(arg_.size());
}
const MathNode * MathNode::getParent(void) const
{
return parent_;
}
unsigned int MathNode::getLevel(void) const
{
if (getParent())
{
return getParent()->getLevel() + 1;
}
else
{
return 0;
}
}
void MathNode::setName(const std::string &name)
{
name_ = name;
@ -72,16 +89,38 @@ void MathNode::pushArg(MathNode *node)
arg_.push_back(node);
}
// operator ////////////////////////////////////////////////////////////////////
// operators ///////////////////////////////////////////////////////////////////
const MathNode &MathNode::operator[](const unsigned int i) const
{
return *arg_[i];
}
// test ////////////////////////////////////////////////////////////////////////
bool MathNode::isRoot(void) const
ostream &Latan::operator<<(ostream &out, const MathNode &n)
{
return (parent_ == NULL);
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() << " (type " << n.getType() << ")" << endl;
for (unsigned int i = 0; i < n.getNArg(); ++i)
{
out << n[i];
}
return out;
}
/******************************************************************************
@ -228,10 +267,6 @@ 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")
VirtualProgram::VirtualProgram(void)
: vector<Instruction *>(0)
{}
ostream &Latan::operator<<(ostream &out, const VirtualProgram &prog)
{
for (unsigned int i = 0; i < prog.size(); ++i)
@ -247,8 +282,8 @@ ostream &Latan::operator<<(ostream &out, const VirtualProgram &prog)
******************************************************************************/
// MathParserState constructor /////////////////////////////////////////////////
MathCompiler::MathParserState::MathParserState(istream *stream, string *name,
vector<MathNode *> *data)
: ParserState<vector<MathNode *> >(stream, name, data)
MathNode **data)
: ParserState<MathNode *>(stream, name, data)
{
initScanner();
}
@ -264,11 +299,16 @@ MathCompiler::MathCompiler(void)
: code_(NULL)
, codeName_("<no_code>")
, state_(NULL)
, root_(NULL)
, gotReturn_(false)
, out_()
, status_(Status::none)
{}
MathCompiler::MathCompiler(const std::string &code)
: root_(NULL)
, gotReturn_(false)
, out_()
{
init(code);
}
@ -279,6 +319,19 @@ MathCompiler::~MathCompiler(void)
reset();
}
// access //////////////////////////////////////////////////////////////////////
const MathNode * MathCompiler::getAST(void) const
{
if (root_)
{
return root_;
}
else
{
return NULL;
}
}
// public methods //////////////////////////////////////////////////////////////
void MathCompiler::init(const std::string &code)
{
@ -288,7 +341,7 @@ void MathCompiler::init(const std::string &code)
}
code_ = new stringstream(code);
codeName_ = "<string>";
state_ = new MathParserState(code_, &codeName_, &expr_);
state_ = new MathParserState(code_, &codeName_, &root_);
status_ = Status::initialised;
}
@ -302,7 +355,16 @@ const VirtualProgram& MathCompiler::operator()(void)
}
if (!(status_ & Status::compiled))
{
compile(expr_);
if (root_)
{
gotReturn_ = false;
compile(*root_);
if (!gotReturn_)
{
LATAN_ERROR(Syntax, "expected 'return' in program '" +
codeName_ + "'");
}
}
status_ |= Status::compiled;
}
@ -315,95 +377,114 @@ void MathCompiler::parse(void)
_math_parse(state_);
}
void MathCompiler::compile(const vector<MathNode *> &expr)
{
string lastNode;
for (unsigned int i = 0; i < expr.size(); ++i)
{
lastNode = expr[i]->getName();
if ((lastNode == "=")||(lastNode == "return"))
{
compile(*expr[i]);
if (lastNode == "return")
{
break;
}
}
}
if (lastNode != "return")
{
LATAN_ERROR(Syntax, "expected 'return' in program '" + codeName_ + "'");
}
}
#define IFNODE(name, nArg) if ((n.getName() == (name))&&(n.getNArg() == nArg))
#define ELIFNODE(name, nArg) else IFNODE(name, nArg)
#define ELSE else
void MathCompiler::compile(const MathNode& n)
{
switch (n.getType())
if (!gotReturn_)
{
case MathNode::Type::cst:
out_.push_back(new Push(strTo<double>(n.getName())));
break;
case MathNode::Type::var:
out_.push_back(new Push(n.getName()));
break;
case MathNode::Type::op:
if (n.getName() == "=")
{
if (n[0].getType() == MathNode::Type::var)
switch (n.getType())
{
case MathNode::Type::cst:
out_.push_back(new Push(strTo<double>(n.getName())));
break;
case MathNode::Type::var:
out_.push_back(new Push(n.getName()));
break;
case MathNode::Type::op:
// semicolon
if (n.getName() == ";")
{
compile(n[1]);
if (n.isRoot())
// compile relevant statements
for (unsigned int i = 0; i < n.getNArg(); ++i)
{
out_.push_back(new Pop(n[0].getName()));
bool isAssign =
((n[i].getType() == MathNode::Type::op)&&
(n[i].getName() == "="));
bool isSemiColumn =
((n[i].getType() == MathNode::Type::op)&&
(n[i].getName() == ";"));
bool isKeyword =
(n[i].getType() == MathNode::Type::keyw);
if (isAssign||isSemiColumn||isKeyword)
{
compile(n[i]);
}
}
}
// assignment
else if (n.getName() == "=")
{
// variable assignement
if (n[0].getType() == MathNode::Type::var)
{
bool hasSemicolonParent = ((n.getParent() != NULL) &&
(n.getParent()->getType()
== MathNode::Type::op)&&
(n.getParent()->getName()
== ";"));
// compile the RHS
compile(n[1]);
// pop instruction if at the end of a statement
if (hasSemicolonParent)
{
out_.push_back(new Pop(n[0].getName()));
}
// store instruction else
else
{
out_.push_back(new Store(n[0].getName()));
}
}
else
{
out_.push_back(new Store(n[0].getName()));
LATAN_ERROR(Compilation, "invalid LHS for '='");
}
}
// arithmetic operators
else
{
for (unsigned int i = 0; i < n.getNArg(); ++i)
{
compile(n[i]);
}
IFNODE("-", 1) out_.push_back(new Neg);
ELIFNODE("+", 2) out_.push_back(new Add);
ELIFNODE("-", 2) out_.push_back(new Sub);
ELIFNODE("*", 2) out_.push_back(new Mul);
ELIFNODE("/", 2) out_.push_back(new Div);
ELIFNODE("^", 2) out_.push_back(new Pow);
ELSE LATAN_ERROR(Compilation, "unknown operator '"
+ n.getName() + "'");
}
break;
case MathNode::Type::keyw:
if (n.getName() == "return")
{
compile(n[0]);
gotReturn_ = true;
}
else
{
LATAN_ERROR(Compilation, "invalid LHS for '='");
LATAN_ERROR(Compilation, "unknown keyword '" + n.getName()
+ "'");
}
}
else
{
break;
case MathNode::Type::func:
for (unsigned int i = 0; i < n.getNArg(); ++i)
{
compile(n[i]);
}
IFNODE("-", 1) out_.push_back(new Neg);
ELIFNODE("+", 2) out_.push_back(new Add);
ELIFNODE("-", 2) out_.push_back(new Sub);
ELIFNODE("*", 2) out_.push_back(new Mul);
ELIFNODE("/", 2) out_.push_back(new Div);
ELIFNODE("^", 2) out_.push_back(new Pow);
ELSE LATAN_ERROR(Compilation, "unknown operator (node '"
+ n.getName() + "')");
}
break;
case MathNode::Type::keyw:
for (unsigned int i = 0; i < n.getNArg(); ++i)
{
compile(n[i]);
}
break;
case MathNode::Type::func:
for (unsigned int i = 0; i < n.getNArg(); ++i)
{
compile(n[i]);
}
out_.push_back(new Call(n.getName()));
break;
default:
LATAN_ERROR(Compilation,
"unknown node type (node '" + n.getName() + "')");
break;
out_.push_back(new Call(n.getName()));
break;
default:
LATAN_ERROR(Compilation,
"unknown node type (node named '" + n.getName()
+ "')");
break;
}
}
}
@ -412,11 +493,7 @@ void MathCompiler::reset(void)
delete code_;
codeName_ = "<no_code>";
delete state_;
for (vector<MathNode *>::iterator i = expr_.begin(); i != expr_.end(); ++i)
{
delete *i;
}
expr_.clear();
delete root_;
for (VirtualProgram::iterator i = out_.begin(); i != out_.end(); ++i)
{
delete *i;

View File

@ -25,11 +25,12 @@ public:
public:
enum
{
cst = 0,
op = 1,
var = 2,
keyw = 3,
func = 4
undef = -1,
cst = 0,
op = 1,
var = 2,
keyw = 3,
func = 4
};
};
public:
@ -40,15 +41,18 @@ public:
// destructor
virtual ~MathNode();
// access
const std::string& getName(void) const;
unsigned int getType(void) const;
unsigned int getNArg(void) const;
const std::string& getName(void) const;
unsigned int getType(void) const;
unsigned int getNArg(void) const;
const MathNode * getParent(void) const;
unsigned int getLevel(void) const;
void setName(const std::string &name);
void pushArg(MathNode *node);
// operator
const MathNode &operator[](const unsigned int i) const;
// test
bool isRoot(void) const;
private:
// IO
std::ostream &print(std::ostream &out) const;
private:
std::string name_;
unsigned int type_;
@ -56,6 +60,8 @@ private:
const MathNode * parent_;
};
std::ostream &operator<<(std::ostream &out, const MathNode &n);
/******************************************************************************
* Virtual machine code classes *
******************************************************************************/
@ -166,13 +172,10 @@ DECL_OP(Mul);
DECL_OP(Div);
DECL_OP(Pow);
class VirtualProgram: public std::vector<Instruction *>
{
public:
VirtualProgram(void);
friend std::ostream &operator<<(std::ostream &out,
const VirtualProgram &program);
};
// Virtual program type
typedef std::vector<Instruction *> VirtualProgram;
std::ostream &operator<<(std::ostream &out, const VirtualProgram &program);
/******************************************************************************
* Compiler class *
@ -181,12 +184,12 @@ class MathCompiler
{
public:
// parser state
class MathParserState: public ParserState<std::vector<MathNode *> >
class MathParserState: public ParserState<MathNode *>
{
public:
// constructor
explicit MathParserState(std::istream *stream, std::string *name,
std::vector<MathNode *> *data);
MathNode **data);
// destructor
virtual ~MathParserState(void);
private:
@ -213,21 +216,24 @@ public:
MathCompiler(const std::string &code);
// destructor
~MathCompiler(void);
// access
const MathNode * getAST(void) const;
// initialization
void init(const std::string &code);
// compilation
const VirtualProgram &operator()(void);
private:
void parse(void);
void compile(const std::vector<MathNode *> &expr);
void compile(const MathNode &node);
void reset(void);
private:
std::istream *code_;
std::string codeName_;
MathParserState *state_;
std::vector<MathNode *> expr_;
VirtualProgram out_;
unsigned int status_;
std::istream *code_;
std::string codeName_;
MathParserState *state_;
MathNode *root_;
bool gotReturn_;
VirtualProgram out_;
unsigned int status_;
};
LATAN_END_CPPDECL

View File

@ -41,10 +41,7 @@
DIGIT [0-9]
ALPHA [a-zA-Z_]
FLOAT (({DIGIT}+(\.{DIGIT}*)?)|({DIGIT}*\.{DIGIT}+))([eE][+-]?{DIGIT}+)?
KEYWORD return
PUNC [;,()]
OP [+\-*/^=]
PAR []
SPECIAL [;,()+\-*/^={}]
BLANK [ \t]
%%
@ -53,9 +50,8 @@ BLANK [ \t]
strncpy(yylval->val_str,yytext,MAXIDLENGTH);
RETTOK(FLOAT);
}
{OP} {RET(*yytext);}
{KEYWORD} {RET(*yytext);}
{PUNC} {RET(*yytext);}
{SPECIAL} {RET(*yytext);}
return {RETTOK(RETURN);}
{ALPHA}({ALPHA}|{DIGIT})* {
strncpy(yylval->val_str,yytext,MAXIDLENGTH);
RETTOK(ID);

View File

@ -29,7 +29,7 @@
%token <val_char> ERR
%token <val_str> FLOAT
%token <val_str> ID
%token <val_str> KEYWORD
%token RETURN
%left '='
%left '+' '-'
@ -37,8 +37,7 @@
%left '^'
%nonassoc UMINUS
%type <val_node> expr
%type <val_node> funcargs
%type <val_node> stmt stmt_list expr func_args
%{
int _math_lex(YYSTYPE *lvalp, YYLTYPE *llocp, void *scanner);
@ -60,21 +59,37 @@
program:
/* empty string */
| program expr ';' {state->data->push_back($2);}
| stmt_list {*(state->data) = $1;}
;
stmt:
';'
{$$ = new MathNode(";", MathNode::Type::op, 0);}
| expr ';'
{$$ = $1;}
| ID '=' expr ';'
{
$$ = new MathNode("=", MathNode::Type::op, 2, \
new MathNode($ID,MathNode::Type::var), $3);
}
| RETURN expr ';'
{$$ = new MathNode("return", MathNode::Type::keyw, 1, $2);}
| '{' stmt_list '}'
{$$ = $2;}
;
stmt_list:
stmt
{$$ = $1;}
| stmt_list stmt
{$$ = new MathNode(";", MathNode::Type::op, 2, $1, $2);}
;
expr:
FLOAT
{$$ = new MathNode($FLOAT, MathNode::Type::cst);}
| ID
{$$ = new MathNode($ID,MathNode::Type::var);}
| ID '=' expr
{
$$ = new MathNode("=", MathNode::Type::op, 2, \
new MathNode($ID,MathNode::Type::var), $3);
}
| 'r' expr
{$$ = new MathNode("return", MathNode::Type::keyw, 1, $2);}
{$$ = new MathNode($ID, MathNode::Type::var);}
| '-' expr %prec UMINUS
{$$ = new MathNode("-", MathNode::Type::op, 1, $2);}
| expr '+' expr
@ -89,16 +104,16 @@ expr:
{$$ = new MathNode("^", MathNode::Type::op, 2, $1, $3);}
| '(' expr ')'
{$$ = $2;}
| ID '(' funcargs ')'
| ID '(' func_args ')'
{$$ = $3; $$->setName($ID);}
;
funcargs:
func_args:
/* empty string */
{$$ = new MathNode("", MathNode::Type::func);}
| expr
{$$ = new MathNode("", MathNode::Type::func, 1, $1);}
| funcargs ',' expr
| func_args ',' expr
{$$ = $1; $$->pushArg($3);}
;