Permalink
Browse files

added execution of comparison operations

  • Loading branch information...
1 parent b63b01a commit bf1a1f14f1f96df199afa4bd0cfbbba8698f9c93 @sporgj committed Feb 1, 2012
Showing with 155 additions and 5 deletions.
  1. +47 −3 src/backbone/executor/e_expressions.cpp
  2. +90 −2 src/backbone/executor/executor.cpp
  3. +18 −0 src/backbone/executor/executor.h
@@ -10,14 +10,14 @@
namespace _Y_BACKBONE_ {
-void Executor::expression() {
- eExpr();
+void Executor::eAssign(){
+ expression();
if (curr->type == EQUAL_SIGN || is_assignment(curr->type)) {
TObject* token = curr;
get();
- expression();
+ eAssign();
SNode* entry = nodes->front();
nodes->pop_front();
@@ -45,6 +45,32 @@ void Executor::expression() {
}
}
+void Executor::expression() {
+ eExpr();
+
+ while(curr->type == LESS_THAN || curr->type==GREATER_THAN ||
+ curr->type == LESS_THAN_OR_EQUALS || curr->type==GREATER_THAN_OR_EQUALS){
+ TObject* token = curr;
+
+ get();
+
+ eExpr();
+
+ Object* d1 = stack->back();
+ stack->pop_back();
+ Object* d2 = stack->back();
+ stack->pop_back();
+
+ switch(token->type){
+ case LESS_THAN: stack->push_back(lessthan(d2, d1)); break;
+ case GREATER_THAN: stack->push_back(greaterthan(d2, d1)); break;
+ case LESS_THAN_OR_EQUALS: stack->push_back(lessthanor(d2, d1)); break;
+ case GREATER_THAN_OR_EQUALS: stack->push_back(greaterthanor(d2, d1)); break;
+ default: break;
+ }
+ }
+}
+
void Executor::eExpr() {
char c = '\0';
@@ -168,6 +194,24 @@ void Executor::eFactor() {
//check if it's an equal sign, *=, -= etc
if (curr->type == EQUAL_SIGN) {
//then nothing
+ } else if(curr->type == L_PAREN) {
+ //then we have a function
+ while(curr->type != R_PAREN){
+ get();
+
+ expression();
+ //put it in the arguments stack
+ Object* o = stack->back();
+ stack->pop_back();
+ args->push_back(o);
+
+ if(curr->type == COMMA)
+ get();
+ }
+ //move away from the right parenthesis
+ get();
+ //now we can call the function object
+ Function::call_funx(*token->str);
} else {
SNode* node = table->search(token->str);
//if it's null throw an exception
@@ -7,6 +7,7 @@
*/
#include "executor.h"
+
#include <iostream>
using namespace std;
@@ -24,7 +25,11 @@ Executor::Executor(Parser* p, ostream* out){
//FIXME, this is temporary. Normally the currently used symboltable would vary
table = p->table;
- stack = new list<Object*>();
+ Function::args = args = new list<Object*>();
+ Function::stack = stack = new list<Object*>();
+
+ //initiating libraries
+ _Y_LIBRARY_::install_math_lib();
}
void Executor::exec(){
@@ -35,7 +40,7 @@ void Executor::exec(){
//TODO, start parsing IF
}else{
//parse the expression
- expression();
+ eAssign();
}
while(curr->type==SEMICOLON){
@@ -75,6 +80,9 @@ Object* Executor::add(Object* o1, Object* o2, bool copy){
return n1;
}
+ //report an error
+ error_reporting(ERROR_MESSAGES[2]);
+
return 0;
}
@@ -111,6 +119,7 @@ Object* Executor::multiply(Object* o1, Object* o2, bool copy){
return n1;
}
+ error_reporting(ERROR_MESSAGES[2]);
return 0;
}
@@ -129,6 +138,7 @@ Object* Executor::divide(Object* o1, Object* o2, bool copy){
return n1;
}
+ error_reporting(ERROR_MESSAGES[2]);
return 0;
}
@@ -147,6 +157,7 @@ Object* Executor::modulo(Object* o1, Object* o2, bool copy){
return n1;
}
+ error_reporting(ERROR_MESSAGES[2]);
return 0;
}
@@ -165,6 +176,83 @@ Object* Executor::expo(Object* o1, Object* o2, bool copy){
return n1;
}
+ error_reporting(ERROR_MESSAGES[2]);
+ return 0;
+}
+
+Object* Executor::lessthan(Object* o1, Object* o2){
+ if(o1->get_type()==NUMERIC_OBJECT && o2->get_type()==NUMERIC_OBJECT){
+ Numeric* n1 = Numeric::fromObject(o1);
+ Numeric* n2 = Numeric::fromObject(o2);
+
+ Numeric* rv = new Numeric(1, 1);
+
+ if(n1->operator <(*n2)){
+ rv->operator[](0) = Number(1, 0);
+ }else{
+ rv->operator[](0) = Number(0, 0);
+ }
+ return rv;
+ }
+
+ error_reporting(ERROR_MESSAGES[2]);
+ return 0;
+}
+
+Object* Executor::greaterthan(Object* o1, Object* o2){
+ if(o1->get_type()==NUMERIC_OBJECT && o2->get_type()==NUMERIC_OBJECT){
+ Numeric* n1 = Numeric::fromObject(o1);
+ Numeric* n2 = Numeric::fromObject(o2);
+
+ Numeric* rv = new Numeric(1, 1);
+
+ if(n1->operator >(*n2)){
+ rv->operator[](0) = Number(1, 0);
+ }else{
+ rv->operator[](0) = Number(0, 0);
+ }
+ return rv;
+ }
+
+ error_reporting(ERROR_MESSAGES[2]);
+ return 0;
+}
+
+Object* Executor::lessthanor(Object* o1, Object* o2){
+ if(o1->get_type()==NUMERIC_OBJECT && o2->get_type()==NUMERIC_OBJECT){
+ Numeric* n1 = Numeric::fromObject(o1);
+ Numeric* n2 = Numeric::fromObject(o2);
+
+ Numeric* rv = new Numeric(1, 1);
+
+ if(n1->operator <=(*n2)){
+ rv->operator[](0) = Number(1, 0);
+ }else{
+ rv->operator[](0) = Number(0, 0);
+ }
+ return rv;
+ }
+
+ error_reporting(ERROR_MESSAGES[2]);
+ return 0;
+}
+
+Object* Executor::greaterthanor(Object* o1, Object* o2){
+ if(o1->get_type()==NUMERIC_OBJECT && o2->get_type()==NUMERIC_OBJECT){
+ Numeric* n1 = Numeric::fromObject(o1);
+ Numeric* n2 = Numeric::fromObject(o2);
+
+ Numeric* rv = new Numeric(1, 1);
+
+ if(n1->operator >=(*n2)){
+ rv->operator[](0) = Number(1, 0);
+ }else{
+ rv->operator[](0) = Number(0, 0);
+ }
+ return rv;
+ }
+
+ error_reporting(ERROR_MESSAGES[2]);
return 0;
}
@@ -14,14 +14,22 @@
#include <cctype>
#include <ostream>
+#include "../global.h"
#include "../parser/parser.h"
#include "../../objects/object.h"
#include "../../objects/numeric.h"
+#include "../../library/function.h"
+
+#include "../../library/math.h"
using namespace _Y_OBJECT_;
+using _Y_LIBRARY_::Function;
+
namespace _Y_BACKBONE_ {
+extern size_t CURRENT_LINE;
+
class Executor {
public:
/**
@@ -67,8 +75,14 @@ class Executor {
*/
TObject* curr;
+ /**
+ * the arguments in the current function call
+ */
+ list<Object*>* args;
+
void get(){
curr = ycode->read();
+ CURRENT_LINE = curr->line;
}
void expression();
@@ -91,6 +105,10 @@ class Executor {
Object* divide(Object* o1, Object* o2, bool copy=true);
Object* modulo(Object* o1, Object* o2, bool copy=true);
Object* expo(Object* o1, Object* o2, bool copy=true);
+ Object* lessthan(Object* o1, Object* o2);
+ Object* greaterthan(Object* o1, Object* o2);
+ Object* lessthanor(Object* o1, Object* o2);
+ Object* greaterthanor(Object* o1, Object* o2);
};
}

0 comments on commit bf1a1f1

Please sign in to comment.