Files
BlitzNext/compiler/lib/exprnode.cpp
T

960 lines
20 KiB
C++
Raw Normal View History

2019-01-19 18:28:07 +01:00
#include "exprnode.hpp"
#include <cfloat>
#include <cmath>
#include "codegen.hpp"
#include "environ.hpp"
#include "toker.hpp"
#include "varnode.hpp"
2014-01-31 08:23:00 +13:00
2019-01-19 18:28:07 +01:00
#include <stdutil.hpp>
2014-01-31 08:23:00 +13:00
2019-01-19 18:28:07 +01:00
ExprNode::ExprNode() : sem_type(0) {}
ExprNode::ExprNode(Type* t) : sem_type(t) {}
2014-01-31 08:23:00 +13:00
//////////////////////////////////
// Cast an expression to a type //
//////////////////////////////////
2019-01-18 17:04:57 +01:00
ExprNode* ExprNode::castTo(Type* ty, Environ* e)
{
if (!sem_type->canCastTo(ty)) {
ex("Illegal type conversion");
2014-01-31 08:23:00 +13:00
}
2019-01-18 17:04:57 +01:00
ExprNode* cast = new CastNode(this, ty);
cast->semant(e);
return cast;
2014-01-31 08:23:00 +13:00
}
2019-01-19 18:28:07 +01:00
CastNode::CastNode(ExprNode* ex, Type* ty) : expr(ex), type(ty) {}
CastNode::~CastNode()
{
delete expr;
}
2019-01-18 17:04:57 +01:00
ExprNode* CastNode::semant(Environ* e)
{
if (!expr->sem_type) {
expr = expr->semant(e);
2014-01-31 08:23:00 +13:00
}
2019-01-18 17:04:57 +01:00
if (ConstNode* c = expr->constNode()) {
ExprNode* e;
if (type == Type::int_type)
e = new IntConstNode(c->intValue());
else if (type == Type::float_type)
e = new FloatConstNode(c->floatValue());
else
e = new StringConstNode(c->stringValue());
2014-01-31 08:23:00 +13:00
delete this;
return e;
}
sem_type = type;
2014-01-31 08:23:00 +13:00
return this;
}
//////////////////////////////////
// Cast an expression to a type //
//////////////////////////////////
2019-01-18 17:04:57 +01:00
TNode* CastNode::translate(Codegen* g)
{
TNode* t = expr->translate(g);
if (expr->sem_type == Type::float_type && sem_type == Type::int_type) {
2014-01-31 08:23:00 +13:00
//float->int
2016-10-03 17:11:15 +02:00
return new TNode(IR_CAST, t, 0);
2014-01-31 08:23:00 +13:00
}
if (expr->sem_type == Type::int_type && sem_type == Type::float_type) {
2014-01-31 08:23:00 +13:00
//int->float
2016-10-03 17:11:15 +02:00
return new TNode(IR_FCAST, t, 0);
2014-01-31 08:23:00 +13:00
}
if (expr->sem_type == Type::string_type && sem_type == Type::int_type) {
2014-01-31 08:23:00 +13:00
//str->int
return call("__bbStrToInt", t);
2014-01-31 08:23:00 +13:00
}
if (expr->sem_type == Type::int_type && sem_type == Type::string_type) {
2014-01-31 08:23:00 +13:00
//int->str
return call("__bbStrFromInt", t);
2014-01-31 08:23:00 +13:00
}
if (expr->sem_type == Type::string_type && sem_type == Type::float_type) {
2014-01-31 08:23:00 +13:00
//str->float
return fcall("__bbStrToFloat", t);
2014-01-31 08:23:00 +13:00
}
if (expr->sem_type == Type::float_type && sem_type == Type::string_type) {
2014-01-31 08:23:00 +13:00
//float->str
return call("__bbStrFromFloat", t);
2014-01-31 08:23:00 +13:00
}
if (expr->sem_type->structType() && sem_type == Type::string_type) {
2014-01-31 08:23:00 +13:00
//obj->str
return call("__bbObjToStr", t);
2014-01-31 08:23:00 +13:00
}
return t;
}
/////////////////////////////
// Sequence of Expressions //
/////////////////////////////
2019-01-19 18:28:07 +01:00
ExprSeqNode::~ExprSeqNode() {}
void ExprSeqNode::push_back(std::shared_ptr<ExprNode> e)
{
exprs.push_back(e);
}
int ExprSeqNode::size()
{
return exprs.size();
}
2019-01-18 17:04:57 +01:00
void ExprSeqNode::semant(Environ* e)
{
for (int k = 0; k < exprs.size(); ++k) {
2019-01-18 17:04:57 +01:00
if (exprs[k])
exprs[k] = exprs[k]->semant(e);
2014-01-31 08:23:00 +13:00
}
}
2019-01-18 17:04:57 +01:00
TNode* ExprSeqNode::translate(Codegen* g, bool cfunc)
{
TNode *t = 0, *l = 0;
for (int k = 0; k < exprs.size(); ++k) {
2019-01-18 17:04:57 +01:00
TNode* q = exprs[k]->translate(g);
2014-01-31 08:23:00 +13:00
if (cfunc) {
2019-01-18 17:04:57 +01:00
Type* ty = exprs[k]->sem_type;
if (ty->stringType()) {
q = call("__bbStrToCStr", q);
} else if (ty->structType()) {
2016-10-03 17:11:15 +02:00
q = new TNode(IR_MEM, q);
} else if (ty == Type::void_type) {
2016-10-03 17:11:15 +02:00
q = new TNode(IR_MEM, add(q, iconst(4)));
2014-01-31 08:23:00 +13:00
}
}
2019-01-18 17:04:57 +01:00
TNode* p;
2016-10-03 17:11:15 +02:00
p = new TNode(IR_ARG, 0, 0, k * 4);
p = new TNode(IR_MEM, p, 0);
p = new TNode(IR_MOVE, q, p);
p = new TNode(IR_SEQ, p, 0);
2019-01-18 17:04:57 +01:00
if (l)
l->r = p;
else
t = p;
l = p;
2014-01-31 08:23:00 +13:00
}
return t;
}
2019-01-18 17:04:57 +01:00
void ExprSeqNode::castTo(DeclSeq* decls, Environ* e, bool cfunc)
{
if (exprs.size() > decls->size())
ex("Too many parameters");
for (int k = 0; k < decls->size(); ++k) {
2019-01-18 17:04:57 +01:00
Decl* d = decls->decls[k];
if (k < exprs.size() && exprs[k]) {
if (cfunc && d->type->structType()) {
if (exprs[k]->sem_type->structType()) {
} else if (exprs[k]->sem_type->intType()) {
exprs[k]->sem_type = Type::void_type;
} else {
ex("Illegal type conversion");
2014-01-31 08:23:00 +13:00
}
continue;
}
exprs[k] = exprs[k]->castTo(d->type, e);
2014-01-31 08:23:00 +13:00
} else {
2019-01-18 17:04:57 +01:00
if (!d->defType)
ex("Not enough parameters");
ExprNode* expr = constValue(d->defType);
if (k < exprs.size())
exprs[k] = expr;
else
exprs.push_back(expr);
2014-01-31 08:23:00 +13:00
}
}
}
2019-01-18 17:04:57 +01:00
void ExprSeqNode::castTo(Type* t, Environ* e)
{
for (int k = 0; k < exprs.size(); ++k) {
exprs[k] = exprs[k]->castTo(t, e);
2014-01-31 08:23:00 +13:00
}
}
2019-01-19 18:28:07 +01:00
CallNode::CallNode(const std::string& i, const std::string& t, ExprSeqNode* e) : ident(i), tag(t), exprs(e) {}
CallNode::~CallNode()
{
delete exprs;
}
2014-01-31 08:23:00 +13:00
///////////////////
// Function call //
///////////////////
2019-01-18 17:04:57 +01:00
ExprNode* CallNode::semant(Environ* e)
{
Type* t = e->findType(tag);
sem_decl = e->findFunc(ident);
2019-01-18 17:04:57 +01:00
if (!sem_decl || !(sem_decl->kind & DECL_FUNC))
ex("Function '" + ident + "' not found");
FuncType* f = sem_decl->type->funcType();
if (t && f->returnType != t)
ex("incorrect function return type");
exprs->semant(e);
exprs->castTo(f->params, e, f->cfunc);
sem_type = f->returnType;
2014-01-31 08:23:00 +13:00
return this;
}
2019-01-18 17:04:57 +01:00
TNode* CallNode::translate(Codegen* g)
{
FuncType* f = sem_decl->type->funcType();
2014-01-31 08:23:00 +13:00
2019-01-18 17:04:57 +01:00
TNode* t;
TNode* l = global("_f" + ident);
TNode* r = exprs->translate(g, f->cfunc);
2014-01-31 08:23:00 +13:00
if (f->userlib) {
2016-10-03 17:11:15 +02:00
l = new TNode(IR_MEM, l);
usedfuncs.insert(ident);
2014-01-31 08:23:00 +13:00
}
if (sem_type == Type::float_type) {
2016-10-03 17:11:15 +02:00
t = new TNode(IR_FCALL, l, r, exprs->size() * 4);
} else {
2016-10-03 17:11:15 +02:00
t = new TNode(IR_CALL, l, r, exprs->size() * 4);
2014-01-31 08:23:00 +13:00
}
if (f->returnType->stringType()) {
if (f->cfunc) {
t = call("__bbCStrToStr", t);
2014-01-31 08:23:00 +13:00
}
}
return t;
}
2019-01-19 18:28:07 +01:00
VarExprNode::VarExprNode(VarNode* v) : var(v) {}
VarExprNode::~VarExprNode()
{
delete var;
}
2014-01-31 08:23:00 +13:00
/////////////////////////
// Variable expression //
/////////////////////////
2019-01-18 17:04:57 +01:00
ExprNode* VarExprNode::semant(Environ* e)
{
var->semant(e);
2019-01-18 17:04:57 +01:00
sem_type = var->sem_type;
ConstType* c = sem_type->constType();
if (!c)
return this;
ExprNode* expr = constValue(c);
delete this;
return expr;
2014-01-31 08:23:00 +13:00
}
2019-01-18 17:04:57 +01:00
TNode* VarExprNode::translate(Codegen* g)
{
return var->load(g);
2014-01-31 08:23:00 +13:00
}
//////////////////////
// Integer constant //
//////////////////////
2019-01-18 17:04:57 +01:00
IntConstNode::IntConstNode(int n) : value(n)
{
sem_type = Type::int_type;
2014-01-31 08:23:00 +13:00
}
2019-01-18 17:04:57 +01:00
TNode* IntConstNode::translate(Codegen* g)
{
2016-10-03 17:11:15 +02:00
return new TNode(IR_CONST, 0, 0, value);
2014-01-31 08:23:00 +13:00
}
2019-01-18 17:04:57 +01:00
int IntConstNode::intValue()
{
2014-01-31 08:23:00 +13:00
return value;
}
2019-01-18 17:04:57 +01:00
float IntConstNode::floatValue()
{
2014-01-31 08:23:00 +13:00
return value;
}
2019-01-19 18:28:07 +01:00
std::string IntConstNode::stringValue()
2019-01-18 17:04:57 +01:00
{
return itoa(value);
2014-01-31 08:23:00 +13:00
}
////////////////////
// Float constant //
////////////////////
2019-01-18 17:04:57 +01:00
FloatConstNode::FloatConstNode(float f) : value(f)
{
sem_type = Type::float_type;
2014-01-31 08:23:00 +13:00
}
2019-01-18 17:04:57 +01:00
TNode* FloatConstNode::translate(Codegen* g)
{
2016-10-03 17:11:15 +02:00
return new TNode(IR_CONST, 0, 0, *(int*)&value);
2014-01-31 08:23:00 +13:00
}
2019-01-18 17:04:57 +01:00
int FloatConstNode::intValue()
{
float flt = value;
2019-01-18 17:04:57 +01:00
int temp;
_control87(_RC_NEAR | _PC_24 | _EM_INVALID | _EM_ZERODIVIDE | _EM_OVERFLOW | _EM_UNDERFLOW | _EM_INEXACT
| _EM_DENORMAL,
0xfffff);
_asm {
fld[flt];
fistp[temp];
2014-01-31 08:23:00 +13:00
}
_control87(_CW_DEFAULT, 0xfffff);
2014-01-31 08:23:00 +13:00
return temp;
}
2019-01-18 17:04:57 +01:00
float FloatConstNode::floatValue()
{
2014-01-31 08:23:00 +13:00
return value;
}
2019-01-19 18:28:07 +01:00
std::string FloatConstNode::stringValue()
2019-01-18 17:04:57 +01:00
{
return ftoa(value);
2014-01-31 08:23:00 +13:00
}
/////////////////////
// String constant //
/////////////////////
2019-01-19 18:28:07 +01:00
StringConstNode::StringConstNode(const std::string& s) : value(s)
2019-01-18 17:04:57 +01:00
{
sem_type = Type::string_type;
2014-01-31 08:23:00 +13:00
}
2019-01-18 17:04:57 +01:00
TNode* StringConstNode::translate(Codegen* g)
{
2019-01-19 18:28:07 +01:00
std::string lab = genLabel();
g->s_data(value, lab);
return call("__bbStrConst", global(lab));
2014-01-31 08:23:00 +13:00
}
2019-01-18 17:04:57 +01:00
int StringConstNode::intValue()
{
return atoi(value);
2014-01-31 08:23:00 +13:00
}
2019-01-18 17:04:57 +01:00
float StringConstNode::floatValue()
{
return (float)atof(value);
2014-01-31 08:23:00 +13:00
}
2019-01-19 18:28:07 +01:00
std::string StringConstNode::stringValue()
2019-01-18 17:04:57 +01:00
{
2014-01-31 08:23:00 +13:00
return value;
}
2019-01-19 18:28:07 +01:00
UniExprNode::UniExprNode(int op, ExprNode* expr) : op(op), expr(expr) {}
UniExprNode::~UniExprNode()
{
delete expr;
}
2014-01-31 08:23:00 +13:00
////////////////////
// Unary operator //
////////////////////
2019-01-18 17:04:57 +01:00
ExprNode* UniExprNode::semant(Environ* e)
{
expr = expr->semant(e);
sem_type = expr->sem_type;
2019-01-18 17:04:57 +01:00
if (sem_type != Type::int_type && sem_type != Type::float_type)
ex("Illegal operator for type");
if (ConstNode* c = expr->constNode()) {
ExprNode* e;
if (sem_type == Type::int_type) {
switch (op) {
2019-01-18 17:04:57 +01:00
case '+':
e = new IntConstNode(+c->intValue());
break;
case '-':
e = new IntConstNode(-c->intValue());
break;
case ABS:
e = new IntConstNode(c->intValue() >= 0 ? c->intValue() : -c->intValue());
break;
case SGN:
e = new IntConstNode(c->intValue() > 0 ? 1 : (c->intValue() < 0 ? -1 : 0));
break;
2014-01-31 08:23:00 +13:00
}
} else {
switch (op) {
2019-01-18 17:04:57 +01:00
case '+':
e = new FloatConstNode(+c->floatValue());
break;
case '-':
e = new FloatConstNode(-c->floatValue());
break;
case ABS:
e = new FloatConstNode(c->floatValue() >= 0 ? c->floatValue() : -c->floatValue());
break;
case SGN:
e = new FloatConstNode(c->floatValue() > 0 ? 1 : (c->floatValue() < 0 ? -1 : 0));
break;
2014-01-31 08:23:00 +13:00
}
}
delete this;
return e = nullptr;
2014-01-31 08:23:00 +13:00
}
return this;
}
2019-01-18 17:04:57 +01:00
TNode* UniExprNode::translate(Codegen* g)
{
int n = 0;
TNode* l = expr->translate(g);
if (sem_type == Type::int_type) {
switch (op) {
2019-01-18 17:04:57 +01:00
case '+':
return l;
case '-':
n = IR_NEG;
break;
case ABS:
return call("__bbAbs", l);
case SGN:
return call("__bbSgn", l);
2014-01-31 08:23:00 +13:00
}
} else {
switch (op) {
2019-01-18 17:04:57 +01:00
case '+':
return l;
case '-':
n = IR_FNEG;
break;
case ABS:
return fcall("__bbFAbs", l);
case SGN:
return fcall("__bbFSgn", l);
2014-01-31 08:23:00 +13:00
}
}
2016-10-03 17:11:15 +02:00
return new TNode(n, l, 0);
2014-01-31 08:23:00 +13:00
}
2019-01-19 18:28:07 +01:00
BinExprNode::BinExprNode(int op, ExprNode* lhs, ExprNode* rhs) : op(op), lhs(lhs), rhs(rhs) {}
BinExprNode::~BinExprNode()
{
delete lhs;
delete rhs;
}
2014-01-31 08:23:00 +13:00
/////////////////////////////////////////////////////
// boolean expression - accepts ints, returns ints //
/////////////////////////////////////////////////////
2019-01-18 17:04:57 +01:00
ExprNode* BinExprNode::semant(Environ* e)
{
lhs = lhs->semant(e);
lhs = lhs->castTo(Type::int_type, e);
rhs = rhs->semant(e);
rhs = rhs->castTo(Type::int_type, e);
ConstNode *lc = lhs->constNode(), *rc = rhs->constNode();
if (lc && rc) {
2019-01-18 17:04:57 +01:00
ExprNode* expr = nullptr;
switch (op) {
2019-01-18 17:04:57 +01:00
case AND:
expr = new IntConstNode(lc->intValue() & rc->intValue());
break;
case OR:
expr = new IntConstNode(lc->intValue() | rc->intValue());
break;
case XOR:
expr = new IntConstNode(lc->intValue() ^ rc->intValue());
break;
case SHL:
expr = new IntConstNode(lc->intValue() << rc->intValue());
break;
case SHR:
expr = new IntConstNode((unsigned)lc->intValue() >> rc->intValue());
break;
case SAR:
expr = new IntConstNode(lc->intValue() >> rc->intValue());
break;
2014-01-31 08:23:00 +13:00
}
delete this;
return expr;
}
sem_type = Type::int_type;
2014-01-31 08:23:00 +13:00
return this;
}
2019-01-18 17:04:57 +01:00
TNode* BinExprNode::translate(Codegen* g)
{
TNode* l = lhs->translate(g);
TNode* r = rhs->translate(g);
int n = 0;
switch (op) {
2019-01-18 17:04:57 +01:00
case AND:
n = IR_AND;
break;
case OR:
n = IR_OR;
break;
case XOR:
n = IR_XOR;
break;
case SHL:
n = IR_SHL;
break;
case SHR:
n = IR_SHR;
break;
case SAR:
n = IR_SAR;
break;
2014-01-31 08:23:00 +13:00
}
2016-10-03 17:11:15 +02:00
return new TNode(n, l, r);
2014-01-31 08:23:00 +13:00
}
2019-01-19 18:28:07 +01:00
ArithExprNode::ArithExprNode(int op, ExprNode* lhs, ExprNode* rhs) : op(op), lhs(lhs), rhs(rhs) {}
ArithExprNode::~ArithExprNode()
{
delete lhs;
delete rhs;
}
2014-01-31 08:23:00 +13:00
///////////////////////////
// arithmetic expression //
///////////////////////////
2019-01-18 17:04:57 +01:00
ExprNode* ArithExprNode::semant(Environ* e)
{
lhs = lhs->semant(e);
rhs = rhs->semant(e);
if (lhs->sem_type->structType() || rhs->sem_type->structType()) {
ex("Arithmetic operator cannot be applied to custom type objects");
2014-01-31 08:23:00 +13:00
}
if (lhs->sem_type == Type::string_type || rhs->sem_type == Type::string_type) {
2014-01-31 08:23:00 +13:00
//one side is a string - only + operator...
2019-01-18 17:04:57 +01:00
if (op != '+')
ex("Operator cannot be applied to strings");
sem_type = Type::string_type;
} else if (op == '^' || lhs->sem_type == Type::float_type || rhs->sem_type == Type::float_type) {
2014-01-31 08:23:00 +13:00
//It's ^, or one side is a float
sem_type = Type::float_type;
} else {
2014-01-31 08:23:00 +13:00
//must be 2 ints
sem_type = Type::int_type;
2014-01-31 08:23:00 +13:00
}
2019-01-18 17:04:57 +01:00
lhs = lhs->castTo(sem_type, e);
rhs = rhs->castTo(sem_type, e);
ConstNode *lc = lhs->constNode(), *rc = rhs->constNode();
if (rc && (op == '/' || op == MOD)) {
if ((sem_type == Type::int_type && !rc->intValue()) || (sem_type == Type::float_type && !rc->floatValue())) {
ex("Division by zero");
2014-01-31 08:23:00 +13:00
}
}
if (lc && rc) {
2019-01-18 17:04:57 +01:00
ExprNode* expr = nullptr;
if (sem_type == Type::string_type) {
2016-10-03 17:11:15 +02:00
expr = new StringConstNode(lc->stringValue() + rc->stringValue());
} else if (sem_type == Type::int_type) {
switch (op) {
2019-01-18 17:04:57 +01:00
case '+':
expr = new IntConstNode(lc->intValue() + rc->intValue());
break;
case '-':
expr = new IntConstNode(lc->intValue() - rc->intValue());
break;
case '*':
expr = new IntConstNode(lc->intValue() * rc->intValue());
break;
case '/':
expr = new IntConstNode(lc->intValue() / rc->intValue());
break;
case MOD:
expr = new IntConstNode(lc->intValue() % rc->intValue());
break;
2014-01-31 08:23:00 +13:00
}
} else {
switch (op) {
2019-01-18 17:04:57 +01:00
case '+':
expr = new FloatConstNode(lc->floatValue() + rc->floatValue());
break;
case '-':
expr = new FloatConstNode(lc->floatValue() - rc->floatValue());
break;
case '*':
expr = new FloatConstNode(lc->floatValue() * rc->floatValue());
break;
case '/':
expr = new FloatConstNode(lc->floatValue() / rc->floatValue());
break;
case MOD:
expr = new FloatConstNode(fmod(lc->floatValue(), rc->floatValue()));
break;
case '^':
expr = new FloatConstNode(pow(lc->floatValue(), rc->floatValue()));
break;
2014-01-31 08:23:00 +13:00
}
}
delete this;
return expr;
}
return this;
}
2019-01-18 17:04:57 +01:00
TNode* ArithExprNode::translate(Codegen* g)
{
TNode* l = lhs->translate(g);
TNode* r = rhs->translate(g);
if (sem_type == Type::string_type) {
return call("__bbStrConcat", l, r);
2014-01-31 08:23:00 +13:00
}
int n = 0;
if (sem_type == Type::int_type) {
switch (op) {
2019-01-18 17:04:57 +01:00
case '+':
n = IR_ADD;
break;
case '-':
n = IR_SUB;
break;
case '*':
n = IR_MUL;
break;
case '/':
n = IR_DIV;
break;
case MOD:
return call("__bbMod", l, r);
2014-01-31 08:23:00 +13:00
}
} else {
switch (op) {
2019-01-18 17:04:57 +01:00
case '+':
n = IR_FADD;
break;
case '-':
n = IR_FSUB;
break;
case '*':
n = IR_FMUL;
break;
case '/':
n = IR_FDIV;
break;
case MOD:
return fcall("__bbFMod", l, r);
case '^':
return fcall("__bbFPow", l, r);
2014-01-31 08:23:00 +13:00
}
}
2016-10-03 17:11:15 +02:00
return new TNode(n, l, r);
2014-01-31 08:23:00 +13:00
}
2019-01-19 18:28:07 +01:00
RelExprNode::RelExprNode(int op, ExprNode* lhs, ExprNode* rhs) : op(op), lhs(lhs), rhs(rhs) {}
RelExprNode::~RelExprNode()
{
delete lhs;
delete rhs;
}
2014-01-31 08:23:00 +13:00
/////////////////////////
// relation expression //
/////////////////////////
2019-01-18 17:04:57 +01:00
ExprNode* RelExprNode::semant(Environ* e)
{
lhs = lhs->semant(e);
rhs = rhs->semant(e);
if (lhs->sem_type->structType() || rhs->sem_type->structType()) {
2019-01-18 17:04:57 +01:00
if (op != '=' && op != NE)
ex("Illegal operator for custom type objects");
opType = lhs->sem_type != Type::null_type ? lhs->sem_type : rhs->sem_type;
} else if (lhs->sem_type == Type::string_type || rhs->sem_type == Type::string_type) {
opType = Type::string_type;
} else if (lhs->sem_type == Type::float_type || rhs->sem_type == Type::float_type) {
opType = Type::float_type;
} else {
opType = Type::int_type;
2014-01-31 08:23:00 +13:00
}
2019-01-18 17:04:57 +01:00
sem_type = Type::int_type;
lhs = lhs->castTo(opType, e);
rhs = rhs->castTo(opType, e);
ConstNode *lc = lhs->constNode(), *rc = rhs->constNode();
if (lc && rc) {
2019-01-18 17:04:57 +01:00
ExprNode* expr = nullptr;
if (opType == Type::string_type) {
switch (op) {
2019-01-18 17:04:57 +01:00
case '<':
expr = new IntConstNode(lc->stringValue() < rc->stringValue());
break;
case '=':
expr = new IntConstNode(lc->stringValue() == rc->stringValue());
break;
case '>':
expr = new IntConstNode(lc->stringValue() > rc->stringValue());
break;
case LE:
expr = new IntConstNode(lc->stringValue() <= rc->stringValue());
break;
case NE:
expr = new IntConstNode(lc->stringValue() != rc->stringValue());
break;
case GE:
expr = new IntConstNode(lc->stringValue() >= rc->stringValue());
break;
2014-01-31 08:23:00 +13:00
}
} else if (opType == Type::float_type) {
switch (op) {
2019-01-18 17:04:57 +01:00
case '<':
expr = new IntConstNode(lc->floatValue() < rc->floatValue());
break;
case '=':
expr = new IntConstNode(lc->floatValue() == rc->floatValue());
break;
case '>':
expr = new IntConstNode(lc->floatValue() > rc->floatValue());
break;
case LE:
expr = new IntConstNode(lc->floatValue() <= rc->floatValue());
break;
case NE:
expr = new IntConstNode(lc->floatValue() != rc->floatValue());
break;
case GE:
expr = new IntConstNode(lc->floatValue() >= rc->floatValue());
break;
2014-01-31 08:23:00 +13:00
}
} else {
switch (op) {
2019-01-18 17:04:57 +01:00
case '<':
expr = new IntConstNode(lc->intValue() < rc->intValue());
break;
case '=':
expr = new IntConstNode(lc->intValue() == rc->intValue());
break;
case '>':
expr = new IntConstNode(lc->intValue() > rc->intValue());
break;
case LE:
expr = new IntConstNode(lc->intValue() <= rc->intValue());
break;
case NE:
expr = new IntConstNode(lc->intValue() != rc->intValue());
break;
case GE:
expr = new IntConstNode(lc->intValue() >= rc->intValue());
break;
2014-01-31 08:23:00 +13:00
}
}
delete this;
return expr;
}
return this;
}
2019-01-18 17:04:57 +01:00
TNode* RelExprNode::translate(Codegen* g)
{
TNode* l = lhs->translate(g);
TNode* r = rhs->translate(g);
return compare(op, l, r, opType);
2014-01-31 08:23:00 +13:00
}
2019-01-19 18:28:07 +01:00
NewNode::NewNode(const std::string& i) : ident(i) {}
2014-01-31 08:23:00 +13:00
////////////////////
// New expression //
////////////////////
2019-01-18 17:04:57 +01:00
ExprNode* NewNode::semant(Environ* e)
{
sem_type = e->findType(ident);
2019-01-18 17:04:57 +01:00
if (!sem_type)
ex("custom type name not found");
if (sem_type->structType() == 0)
ex("type is not a custom type");
2014-01-31 08:23:00 +13:00
return this;
}
2019-01-18 17:04:57 +01:00
TNode* NewNode::translate(Codegen* g)
{
return call("__bbObjNew", global("_t" + ident));
2014-01-31 08:23:00 +13:00
}
2019-01-19 18:28:07 +01:00
FirstNode::FirstNode(const std::string& i) : ident(i) {}
2014-01-31 08:23:00 +13:00
////////////////////
// First of class //
////////////////////
2019-01-18 17:04:57 +01:00
ExprNode* FirstNode::semant(Environ* e)
{
sem_type = e->findType(ident);
2019-01-18 17:04:57 +01:00
if (!sem_type)
ex("custom type name name not found");
2014-01-31 08:23:00 +13:00
return this;
}
2019-01-18 17:04:57 +01:00
TNode* FirstNode::translate(Codegen* g)
{
return call("__bbObjFirst", global("_t" + ident));
2014-01-31 08:23:00 +13:00
}
2019-01-19 18:28:07 +01:00
LastNode::LastNode(const std::string& i) : ident(i) {}
2014-01-31 08:23:00 +13:00
///////////////////
// Last of class //
///////////////////
2019-01-18 17:04:57 +01:00
ExprNode* LastNode::semant(Environ* e)
{
sem_type = e->findType(ident);
2019-01-18 17:04:57 +01:00
if (!sem_type)
ex("custom type name not found");
2014-01-31 08:23:00 +13:00
return this;
}
2019-01-18 17:04:57 +01:00
TNode* LastNode::translate(Codegen* g)
{
return call("__bbObjLast", global("_t" + ident));
2014-01-31 08:23:00 +13:00
}
2019-01-19 18:28:07 +01:00
AfterNode::AfterNode(ExprNode* e) : expr(e) {}
AfterNode::~AfterNode()
{
delete expr;
}
2014-01-31 08:23:00 +13:00
////////////////////
// Next of object //
////////////////////
2019-01-18 17:04:57 +01:00
ExprNode* AfterNode::semant(Environ* e)
{
expr = expr->semant(e);
2019-01-18 17:04:57 +01:00
if (expr->sem_type == Type::null_type)
ex("'After' cannot be used on 'Null'");
if (expr->sem_type->structType() == 0)
ex("'After' must be used with a custom type object");
sem_type = expr->sem_type;
2014-01-31 08:23:00 +13:00
return this;
}
2019-01-18 17:04:57 +01:00
TNode* AfterNode::translate(Codegen* g)
{
TNode* t = expr->translate(g);
if (g->debug)
t = jumpf(t, "__bbNullObjEx");
return call("__bbObjNext", t);
2014-01-31 08:23:00 +13:00
}
2019-01-19 18:28:07 +01:00
BeforeNode::BeforeNode(ExprNode* e) : expr(e) {}
BeforeNode::~BeforeNode()
{
delete expr;
}
2014-01-31 08:23:00 +13:00
////////////////////
// Prev of object //
////////////////////
2019-01-18 17:04:57 +01:00
ExprNode* BeforeNode::semant(Environ* e)
{
expr = expr->semant(e);
2019-01-18 17:04:57 +01:00
if (expr->sem_type == Type::null_type)
ex("'Before' cannot be used with 'Null'");
if (expr->sem_type->structType() == 0)
ex("'Before' must be used with a custom type object");
sem_type = expr->sem_type;
2014-01-31 08:23:00 +13:00
return this;
}
2019-01-18 17:04:57 +01:00
TNode* BeforeNode::translate(Codegen* g)
{
TNode* t = expr->translate(g);
if (g->debug)
t = jumpf(t, "__bbNullObjEx");
return call("__bbObjPrev", t);
2014-01-31 08:23:00 +13:00
}
/////////////////
// Null object //
/////////////////
2019-01-18 17:04:57 +01:00
ExprNode* NullNode::semant(Environ* e)
{
sem_type = Type::null_type;
2014-01-31 08:23:00 +13:00
return this;
}
2019-01-18 17:04:57 +01:00
TNode* NullNode::translate(Codegen* g)
{
2016-10-03 17:11:15 +02:00
return new TNode(IR_CONST, 0, 0, 0);
2014-01-31 08:23:00 +13:00
}
2019-01-19 18:28:07 +01:00
ObjectCastNode::ObjectCastNode(ExprNode* e, const std::string& t) : expr(e), type_ident(t) {}
ObjectCastNode::~ObjectCastNode()
{
delete expr;
}
2014-01-31 08:23:00 +13:00
/////////////////
// Object cast //
/////////////////
2019-01-18 17:04:57 +01:00
ExprNode* ObjectCastNode::semant(Environ* e)
{
expr = expr->semant(e);
expr = expr->castTo(Type::int_type, e);
sem_type = e->findType(type_ident);
2019-01-18 17:04:57 +01:00
if (!sem_type)
ex("custom type name not found");
if (!sem_type->structType())
ex("type is not a custom type");
2014-01-31 08:23:00 +13:00
return this;
}
2019-01-18 17:04:57 +01:00
TNode* ObjectCastNode::translate(Codegen* g)
{
TNode* t = expr->translate(g);
t = call("__bbObjFromHandle", t, global("_t" + sem_type->structType()->ident));
2014-01-31 08:23:00 +13:00
return t;
}
2019-01-19 18:28:07 +01:00
ObjectHandleNode::ObjectHandleNode(ExprNode* e) : expr(e) {}
ObjectHandleNode::~ObjectHandleNode()
{
delete expr;
}
2014-01-31 08:23:00 +13:00
///////////////////
// Object Handle //
///////////////////
2019-01-18 17:04:57 +01:00
ExprNode* ObjectHandleNode::semant(Environ* e)
{
expr = expr->semant(e);
2019-01-18 17:04:57 +01:00
if (!expr->sem_type->structType())
ex("'ObjectHandle' must be used with an object");
sem_type = Type::int_type;
2014-01-31 08:23:00 +13:00
return this;
}
2019-01-18 17:04:57 +01:00
TNode* ObjectHandleNode::translate(Codegen* g)
{
TNode* t = expr->translate(g);
return call("__bbObjToHandle", t);
2014-01-31 08:23:00 +13:00
}
2019-01-19 18:28:07 +01:00
ExprNode* ConstNode::semant(Environ* e)
{
return this;
}
ConstNode* ConstNode::constNode()
{
return this;
}