summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRichard Braun <syn@sceen.net>2006-05-28 00:43:35 +0000
committerRichard Braun <syn@sceen.net>2006-05-28 00:43:35 +0000
commitf20026322c976e52984f246c072d3518f5663448 (patch)
treebe50040b4f0e3f0d328e8f33c1f4c614cbb5350f
parent4ee52c4d087ced84a6bdc042d8a2a8510f11e721 (diff)
Implemented assignment operator.
-rw-r--r--mtxexpression.cc105
-rw-r--r--mtxexpression.h27
-rw-r--r--mtxlanguage.cc5
-rw-r--r--mtxsymbol.cc27
-rw-r--r--mtxsymbol.h6
5 files changed, 141 insertions, 29 deletions
diff --git a/mtxexpression.cc b/mtxexpression.cc
index 72dc761..26e341a 100644
--- a/mtxexpression.cc
+++ b/mtxexpression.cc
@@ -32,14 +32,19 @@
using namespace std;
-MtxExpression::MtxExpression()
+struct mtx_operator MtxExpression::operators[] =
{
-}
+ {"=", MTXEXPRESSION_FUNCTION_ASSIGNMENT, MTXEXPRESSION_RIGHT_TO_LEFT},
+ {NULL, MTXEXPRESSION_FUNCTION_NULL, MTXEXPRESSION_ASSOCIATIVITY_NULL},
+};
MtxExpression::MtxExpression(MtxLanguage *language,
mtx_expression_t &lexical_elements,
bool silent)
{
+ if (lexical_elements.empty())
+ throw MtxException("syntax error");
+
/*
* See mtxsymbol.h.
*/
@@ -65,19 +70,85 @@ MtxExpression::MtxExpression(MtxLanguage *language,
else
expressions.push_back(new MtxSymbol(language, *lexical_elements.begin()));
+
+ function = MTXEXPRESSION_FUNCTION_NULL;
}
else
{
- mtx_expression_t::iterator iterator;
+ mtx_expression_t first_operand, second_operand;
+ size_t i;
+
+ i = 0;
- for (iterator = lexical_elements.begin();
- iterator != lexical_elements.end();
- iterator++)
+ while (operators[i].str != NULL)
{
+ if (operators[i].associativity == MTXEXPRESSION_RIGHT_TO_LEFT)
+ {
+ mtx_expression_t::iterator iterator, tmp;
+
+ iterator = find(lexical_elements.begin(), lexical_elements.end(),
+ operators[i].str);
+
+ if (iterator == lexical_elements.end())
+ {
+ i++;
+ continue;
+ }
+
+ for (tmp = lexical_elements.begin();
+ tmp != iterator;
+ tmp++)
+ first_operand.push_back(*tmp);
+
+ for (tmp = iterator + 1;
+ tmp != lexical_elements.end();
+ tmp++)
+ second_operand.push_back(*tmp);
+ }
+
+ else
+ {
+ mtx_expression_t::reverse_iterator iterator, tmp;
+
+ iterator = find(lexical_elements.rbegin(),
+ lexical_elements.rend(),
+ operators[i].str);
+
+ if (iterator == lexical_elements.rend())
+ {
+ i++;
+ continue;
+ }
+
+ for (tmp = lexical_elements.rbegin();
+ tmp != iterator;
+ tmp++)
+ second_operand.push_back(*tmp);
+
+ for (tmp = iterator + 1;
+ tmp != lexical_elements.rend();
+ tmp++)
+ first_operand.push_back(*tmp);
+ }
+
+ break;
}
- throw MtxException("interpretation not yet implemented");
+ /*
+ * An operator was found.
+ */
+ if (operators[i].str != NULL)
+ {
+ function = operators[i].id;
+ expressions.push_back(new MtxExpression(language, first_operand,
+ silent));
+ expressions.push_back(new MtxExpression(language, second_operand,
+ silent));
+ }
+
+ else
+ throw MtxException("operator/function not yet implemented");
}
}
@@ -92,19 +163,25 @@ MtxExpression::~MtxExpression()
}
MtxSymbol
-MtxExpression::getSymbol() const
+MtxExpression::getSymbol(bool create) const
{
- if (function.length() == 0 && expressions.size() == 1)
- return (*expressions.begin())->getSymbol();
+ if (function == MTXEXPRESSION_FUNCTION_NULL && expressions.size() == 1)
+ return (*expressions.begin())->getSymbol(create);
else
{
- MtxRational array[] = {MtxRational(54.254), MtxRational(84.36), MtxRational(46.23), MtxRational(-89.15)};
- MtxMatrix matrix(array, 2, 2);
MtxSymbol symbol;
- symbol = MtxSymbol(language, "matrix", matrix);
- return symbol;
+ switch ((size_t)function)
+ {
+ case MTXEXPRESSION_FUNCTION_ASSIGNMENT:
+ symbol = expressions[1]->getSymbol();
+ symbol.setName(expressions[0]->getSymbol(true).getName());
+ language->setSymbol(symbol);
+ return symbol;
+ }
+
+ throw MtxException("nonexistent function");
}
}
diff --git a/mtxexpression.h b/mtxexpression.h
index d14d44f..2f46c5f 100644
--- a/mtxexpression.h
+++ b/mtxexpression.h
@@ -28,19 +28,40 @@
#include "mtxsymbol.h"
#include "mtxlanguage.h"
+enum mtx_function_id
+{
+ MTXEXPRESSION_FUNCTION_NULL,
+ MTXEXPRESSION_FUNCTION_ASSIGNMENT
+};
+
+enum mtx_operator_associativity
+{
+ MTXEXPRESSION_ASSOCIATIVITY_NULL,
+ MTXEXPRESSION_RIGHT_TO_LEFT,
+ MTXEXPRESSION_LEFT_TO_RIGHT
+};
+
+struct mtx_operator
+{
+ char *str;
+ enum mtx_function_id id;
+ enum mtx_operator_associativity associativity;
+};
+
class MtxExpression: public MtxSymbol
{
private:
- std::string function;
+ enum mtx_function_id function;
std::vector<MtxSymbol *> expressions;
bool silent;
+ static struct mtx_operator operators[];
+
public:
- MtxExpression();
MtxExpression(MtxLanguage *language, mtx_expression_t &lexical_elements,
bool silent);
virtual ~MtxExpression();
- virtual MtxSymbol getSymbol() const;
+ virtual MtxSymbol getSymbol(bool create = false) const;
virtual std::string getName() const;
virtual std::string toString() const;
virtual bool isSilent() const;
diff --git a/mtxlanguage.cc b/mtxlanguage.cc
index 5fc9a1a..531f8eb 100644
--- a/mtxlanguage.cc
+++ b/mtxlanguage.cc
@@ -262,11 +262,6 @@ MtxLanguage::parse(const string &expression)
if (lexical_elements.empty())
return answers;
- /*
- * DEBUG.
- */
- setSymbol(MtxSymbol(this, "a", MtxInteger(123)));
-
expressions = parseExpressions(lexical_elements);
for (expression_iterator = expressions.begin();
diff --git a/mtxsymbol.cc b/mtxsymbol.cc
index 9801c06..4b53e96 100644
--- a/mtxsymbol.cc
+++ b/mtxsymbol.cc
@@ -26,9 +26,13 @@
#include "mtxsymbol.h"
#include "mtxexception.h"
+#include <sys/types.h>
+#include <signal.h>
+#include <unistd.h>
+
using namespace std;
-struct special_name MtxSymbol::special_names[] =
+struct mtx_special_name MtxSymbol::special_names[] =
{
{"about", about},
{"list", list},
@@ -203,13 +207,28 @@ MtxSymbol::getSpecial() const
}
MtxSymbol
-MtxSymbol::getSymbol() const
+MtxSymbol::getSymbol(bool create) const
{
- if (name.length() == 0 || special)
+ if (special)
return *this;
+ if (name.length() == 0 && create)
+ throw MtxException("internal error: unable to create anonymous symbols");
+
+ if (!create)
+ {
+ if (name.length() == 0)
+ return *this;
+
+ else
+ return language->getSymbol(name);
+ }
+
else
- return language->getSymbol(name);
+ {
+ language->setSymbol(*this);
+ return *this;
+ }
}
string
diff --git a/mtxsymbol.h b/mtxsymbol.h
index dc135b2..e35de70 100644
--- a/mtxsymbol.h
+++ b/mtxsymbol.h
@@ -28,7 +28,7 @@
#include "mtxvalue.h"
#include "mtxlanguage.h"
-struct special_name
+struct mtx_special_name
{
char *name;
std::string (*function)(const MtxSymbol *);
@@ -56,7 +56,7 @@ class MtxSymbol
* List of special names and function pointers associated to them.
* Don't call it if special is false.
*/
- static struct special_name special_names[];
+ static struct mtx_special_name special_names[];
protected:
/*
@@ -75,7 +75,7 @@ class MtxSymbol
MtxSymbol(const MtxSymbol &symbol);
virtual ~MtxSymbol();
MtxSymbol & operator=(const MtxSymbol &symbol);
- virtual MtxSymbol getSymbol() const;
+ virtual MtxSymbol getSymbol(bool create = false) const;
virtual std::string getName() const;
void setName(const std::string &name);
virtual std::string toString() const;