Main Page | Namespace List | Class Hierarchy | Alphabetical List | Class List | Directories | File List | Namespace Members | Class Members | File Members

expression.cpp

Go to the documentation of this file.
00001 // expression.h
00002 //
00003 // Copyright (C) 2002, Chris Laurel <claurel@shatters.net>
00004 //
00005 // This program is free software; you can redistribute it and/or
00006 // modify it under the terms of the GNU General Public License
00007 // as published by the Free Software Foundation; either version 2
00008 // of the License, or (at your option) any later version.
00009 
00010 #include <celscript/expression.h>
00011 #include <celscript/function.h>
00012 
00013 using namespace std;
00014 using namespace celx;
00015 
00016 
00017 typedef Value (BinaryOperatorFunc)(const Value&, const Value&);
00018 typedef Value (UnaryOperatorFunc)(const Value&);
00019 
00020 static BinaryOperatorFunc AddFunc;
00021 static BinaryOperatorFunc SubtractFunc;
00022 static BinaryOperatorFunc MultiplyFunc;
00023 static BinaryOperatorFunc DivideFunc;
00024 static BinaryOperatorFunc EqualFunc;
00025 static BinaryOperatorFunc NotEqualFunc;
00026 static BinaryOperatorFunc LesserFunc;
00027 static BinaryOperatorFunc GreaterFunc;
00028 static BinaryOperatorFunc LesserEqualFunc;
00029 static BinaryOperatorFunc GreaterEqualFunc;
00030 
00031 static Value ErrorValue = Value();
00032 
00033 
00034 static BinaryOperatorFunc* BinaryOperatorFunctions[BinaryExpression::OperatorCount] =
00035 {
00036     AddFunc,
00037     SubtractFunc,
00038     MultiplyFunc,
00039     DivideFunc,
00040     EqualFunc,
00041     NotEqualFunc,
00042     LesserFunc,
00043     GreaterFunc,
00044     LesserEqualFunc,
00045     GreaterEqualFunc,
00046     NULL,
00047 };
00048 
00049 
00050 static UnaryOperatorFunc NegateFunc;
00051 static UnaryOperatorFunc LogicalNotFunc;
00052 static UnaryOperatorFunc* UnaryOperatorFunctions[UnaryExpression::OperatorCount] =
00053 {
00054     NegateFunc,
00055     LogicalNotFunc,
00056     NULL,
00057 };
00058 
00059 
00060 Expression::Expression()
00061 {
00062 }
00063 
00064 Expression::~Expression()
00065 {
00066 }
00067 
00068 bool Expression::isLValue() const
00069 {
00070     return false;
00071 }
00072 
00073 Value* Expression::leval(ExecutionContext&)
00074 {
00075     return NULL;
00076 }
00077 
00078 
00079 BinaryExpression::BinaryExpression(Operator _op,
00080                                    Expression* _left,
00081                                    Expression* _right) :
00082     op(_op),
00083     left(_left),
00084     right(_right)
00085 {
00086 }
00087 
00088 BinaryExpression::~BinaryExpression()
00089 {
00090     if (left != NULL)
00091         delete left;
00092     if (right != NULL)
00093         delete right;
00094 }
00095 
00096 Value BinaryExpression::eval(ExecutionContext& context)
00097 {
00098     Value a = left->eval(context);
00099     Value b = right->eval(context);
00100     return BinaryOperatorFunctions[op](a, b);
00101 }
00102 
00103 
00104 UnaryExpression::UnaryExpression(Operator _op,
00105                                  Expression* _expr) :
00106     op(_op),
00107     expr(_expr)
00108 {
00109 }
00110 
00111 UnaryExpression::~UnaryExpression()
00112 {
00113     if (expr != NULL)
00114         delete expr;
00115 }
00116 
00117 Value UnaryExpression::eval(ExecutionContext& context)
00118 {
00119     Value v = expr->eval(context);
00120     return UnaryOperatorFunctions[op](v);
00121 }
00122 
00123 
00124 ConstantExpression::ConstantExpression(const Value& _value) :
00125     value(_value)
00126 {
00127 }
00128 
00129 ConstantExpression::~ConstantExpression()
00130 {
00131 }
00132 
00133 Value ConstantExpression::eval(ExecutionContext&)
00134 {
00135     return value;
00136 }
00137 
00138 
00139 IdentifierExpression::IdentifierExpression(const string& _name) :
00140     name(_name)
00141 {
00142 }
00143 
00144 IdentifierExpression::~IdentifierExpression()
00145 {
00146 }
00147 
00148 bool IdentifierExpression::isLValue() const
00149 {
00150     return true;
00151 }
00152 
00153 Value IdentifierExpression::eval(ExecutionContext& context)
00154 {
00155     Value* val = context.getEnvironment()->lookup(name);
00156     if (val == NULL)
00157         return Value();
00158     else
00159         return *val;
00160 }
00161 
00162 Value* IdentifierExpression::leval(ExecutionContext& context)
00163 {
00164     return context.getEnvironment()->lookup(name);
00165 }
00166 
00167 
00168 
00169 AssignmentExpression::AssignmentExpression(Expression* _left,
00170                                            Expression* _right) :
00171     left(_left), right(_right)
00172 {
00173 }
00174 
00175 AssignmentExpression::~AssignmentExpression()
00176 {
00177     delete left;
00178     delete right;
00179 }
00180 
00181 bool AssignmentExpression::isLValue() const
00182 {
00183     return true;
00184 }
00185 
00186 Value AssignmentExpression::eval(ExecutionContext& context)
00187 {
00188     Value* v = left->leval(context);
00189     if (v == NULL)
00190     {
00191         context.runtimeError();
00192         return Value();
00193     }
00194 
00195     *v = right->eval(context);
00196 
00197     return *v;
00198 }
00199 
00200 Value* AssignmentExpression::leval(ExecutionContext& context)
00201 {
00202     Value* v = left->leval(context);
00203     if (v == NULL)
00204     {
00205         context.runtimeError();
00206         return NULL;
00207     }
00208 
00209     *v = right->eval(context);
00210 
00211     return v;
00212 }
00213 
00214 
00215 FunctionCallExpression::FunctionCallExpression(Expression* _func) :
00216     func(_func)
00217 {
00218 }
00219 
00220 FunctionCallExpression::~FunctionCallExpression()
00221 {
00222     if (func != NULL)
00223         delete func;
00224     for (vector<Expression*>::iterator iter = arguments.begin();
00225          iter != arguments.end(); iter++)
00226     {
00227         delete *iter;
00228     }
00229 }
00230 
00231 Value FunctionCallExpression::eval(ExecutionContext& context)
00232 {
00233     Value funcVal = func->eval(context);
00234     Function* f;
00235     if (!funcVal.functionValue(f))
00236     {
00237         // Not a function
00238         context.runtimeError();
00239         return Value();
00240     }
00241 
00242     return f->call(context);
00243 }
00244 
00245 void FunctionCallExpression::addArgument(Expression* expr)
00246 {
00247     arguments.insert(arguments.end(), expr);
00248 }
00249 
00250 
00251 
00252 Value AddFunc(const Value& a, const Value& b)
00253 {
00254     if (a.getType() == NumberType && b.getType() == NumberType)
00255     {
00256         double x0 = 0.0;
00257         double x1 = 0.0;
00258         a.numberValue(x0);
00259         b.numberValue(x1);
00260         return Value(x0 + x1);
00261     }
00262     else
00263     {
00264         return ErrorValue;
00265     }
00266 }
00267 
00268 Value SubtractFunc(const Value& a, const Value& b)
00269 {
00270     if (a.getType() == NumberType && b.getType() == NumberType)
00271     {
00272         double x0 = 0.0;
00273         double x1 = 0.0;
00274         a.numberValue(x0);
00275         b.numberValue(x1);
00276         return Value(x0 - x1);
00277     }
00278     else
00279     {
00280         return ErrorValue;
00281     }
00282 }
00283 
00284 Value MultiplyFunc(const Value& a, const Value& b)
00285 {
00286     if (a.getType() == NumberType && b.getType() == NumberType)
00287     {
00288         double x0 = 0.0;
00289         double x1 = 0.0;
00290         a.numberValue(x0);
00291         b.numberValue(x1);
00292         return Value(x0 * x1);
00293     }
00294     else
00295     {
00296         return ErrorValue;
00297     }
00298 }
00299 
00300 Value DivideFunc(const Value& a, const Value& b)
00301 {
00302     if (a.getType() == NumberType && b.getType() == NumberType)
00303     {
00304         double x0 = 0.0;
00305         double x1 = 0.0;
00306         a.numberValue(x0);
00307         b.numberValue(x1);
00308         return Value(x0 / x1);
00309     }
00310     else
00311     {
00312         return ErrorValue;
00313     }
00314 }
00315 
00316 
00317 Value EqualFunc(const Value& a, const Value& b)
00318 {
00319     return Value(a == b);
00320 }
00321 
00322 
00323 Value NotEqualFunc(const Value& a, const Value& b)
00324 {
00325     return Value(a != b);
00326 }
00327 
00328 
00329 Value LesserFunc(const Value& a, const Value& b)
00330 {
00331     return Value(a.toNumber() < b.toNumber());
00332 }
00333 
00334 
00335 Value GreaterFunc(const Value& a, const Value& b)
00336 {
00337     return Value(a.toNumber() > b.toNumber());
00338 }
00339 
00340 
00341 Value LesserEqualFunc(const Value& a, const Value& b)
00342 {
00343     return Value(a.toNumber() <= b.toNumber());
00344 }
00345 
00346 
00347 Value GreaterEqualFunc(const Value& a, const Value& b)
00348 {
00349     return Value(a.toNumber() >= b.toNumber());
00350 }
00351 
00352 
00353 
00354 Value NegateFunc(const Value& v)
00355 {
00356     return Value(-v.toNumber());
00357 }
00358 
00359 Value LogicalNotFunc(const Value& v)
00360 {
00361     return Value(!v.toBoolean());
00362 }

Generated on Sat Jan 14 22:30:32 2006 for Celestia by  doxygen 1.4.1