tlang-c/libtlang/src/addnode.c

114 lines
3.5 KiB
C

#include "tlang.h"
extern void __node_two_free(node_t* n);
tobject_t* __add_node_exec(node_t* n,scope_t* sc)
{
bool freeleftifzero=true;
node_t* left = n->data.two_node_node.left;
node_t* right = n->data.two_node_node.right;
tobject_t* l=left->execute(left,sc);
tobject_t* r=right->execute(right,sc);
tobject_t* out=NULL;
tobject_type_t ltype=l->type;
tobject_type_t rtype =r->type;
if(ltype == tdict)
{
if(dict_haskey(l->data.dict,"add"))
{
kvp_t* kvp=dict_getkvp(l->data.dict,"add");
if(kvp->value->type == texternalmethod || kvp->value->type == tinternalmethod)
{
list_tobject_t ls;
list_create(sc->rt,&ls,0);
list_add(&ls,r);
out=tobject_call(sc->rt->globals,kvp->value,&ls);
out->count++; //ensure i dont get deleted
tobject_freeifzero(l);
out->count--; //set me back bitch
r->count++;
list_free(&ls);
r->count--;
freeleftifzero = false;
}
}
}
if(ltype == tnumber && rtype == tnumber)
{
out = tobject_create(sc->rt);
out->type = tnumber;
out->data.number = l->data.number + r->data.number;
}
if(ltype == tchar && rtype == tstring)
{
string_t* newStr = string_create();
string_appendc(newStr,l->data.chr);
string_appends(newStr,r->data.string);
out=tobject_create(sc->rt);
out->type = tstring;
out->data.string = newStr;
}
if(ltype == tstring && rtype == tchar)
{
string_t* newStr = string_create();
string_appends(newStr,l->data.string);
string_appendc(newStr,r->data.chr);
out=tobject_create(sc->rt);
out->type = tstring;
out->data.string = newStr;
}
if(ltype == tstring && rtype == tstring)
{
string_t* newStr = string_create();
string_appends(newStr,l->data.string);
string_appends(newStr,r->data.string);
out=tobject_create(sc->rt);
out->type = tstring;
out->data.string = newStr;
}
if(ltype == tstring && rtype == tnumber)
{
string_t* newStr = string_create();
string_appends(newStr,l->data.string);
string_appendn(newStr,r->data.number);
out=tobject_create(sc->rt);
out->type = tstring;
out->data.string = newStr;
}
if(ltype == tnumber && rtype == tstring)
{
string_t* newStr = string_create();
string_appendn(newStr,l->data.number);
string_appends(newStr,r->data.string);
out=tobject_create(sc->rt);
out->type = tstring;
out->data.string = newStr;
}
if(freeleftifzero)
{
tobject_freeifzero(l);
}
tobject_freeifzero(r);
if(out == NULL)
{
return tobject_basic(sc->rt,tnull);
}
return out;
}
node_t* node_add_create(node_t* left,node_t* right)
{
node_t* node = (node_t*)malloc(sizeof(node_t));
node->type = addnode;
node->data.two_node_node.left = left;
node->data.two_node_node.right = right;
node->execute = __add_node_exec;
node->free = __node_two_free;
return node;
}