Added ret, break and also rand

This commit is contained in:
Mike Nolan 2023-05-05 23:03:12 -05:00
parent 45af9b9d39
commit 362924c107
45 changed files with 261 additions and 137 deletions

View File

@ -42,7 +42,12 @@ struct string {
int length;
};
typedef tobject_t* (*node_exec_t)(node_t*,scope_t*);
typedef struct
{
bool isReturning;
bool isBreaking;
} retarg_t;
typedef tobject_t* (*node_exec_t)(node_t*,scope_t*,retarg_t*);
typedef void (*node_set_t)(node_t*,tobject_t*,scope_t*);
typedef tobject_t* (*external_method_t)(runtime_t*,void* ptr,list_tobject_t* objs);
typedef enum {tnull,tundef,tdict,texternalmethod,tinternalmethod,tlist,tstring,tnumber,tbool,tchar} tobject_type_t;
@ -62,7 +67,7 @@ node_t* node_not_create(node_t* left);
node_t* node_const_object_create(tobject_t*);
node_t* node_postfix_decrement_create(node_t* left);
node_t* node_postfix_increment_create(node_t* left);
typedef enum {postfixincrementnode,postfixdecrementnode,methodcallnode,bnotnode,negnode,notnode,notequalsnode,equalsnode,whileloopnode,eachloopnode,forloopnode,ifnode,getarraynode,logicalornode,logicalandnode,xornode,closurenode,scopenode,bitwiseornode,bitwiseandnode,addnode,lessthannode,lessthanequalnode,greaterthannode,greaterthanequalnode,subnode,multiplynode,dividenode,modulonode,leftshiftnode,rightshiftnode,constobjectnode,constnumbernode,constnullnode,constundefnode,conststringnode,constboolnode,constcharnode,functioncallnode,setvariablenode,getvariablenode,getmembernode} node_type_t;
typedef enum {postfixincrementnode,postfixdecrementnode,methodcallnode,bnotnode,negnode,notnode,notequalsnode,equalsnode,whileloopnode,eachloopnode,forloopnode,ifnode,getarraynode,logicalornode,logicalandnode,xornode,closurenode,scopenode,bitwiseornode,bitwiseandnode,addnode,lessthannode,lessthanequalnode,greaterthannode,greaterthanequalnode,subnode,multiplynode,dividenode,modulonode,leftshiftnode,rightshiftnode,constobjectnode,constnumbernode,constnullnode,constundefnode,conststringnode,constboolnode,constcharnode,functioncallnode,setvariablenode,getvariablenode,getmembernode,breaknode,returnnode} node_type_t;
typedef struct {
char* key;
@ -455,4 +460,6 @@ void runtime_create_string_on_dict(dict_t* dict,char* name,runtime_t* rt,const c
void runtime_create_method_on_object(tobject_t* tdict_obj,char* name,runtime_t* rt,void* ptr,external_method_t cb,texternalmethod_free_t free);
void runtime_create_number_on_object(tobject_t* tdict_obj,char* name,runtime_t* rt,double number);
void runtime_create_bool_on_object(tobject_t* tdict_obj,char* name,runtime_t* rt,bool value);
void runtime_create_string_on_object(tobject_t* tdict_obj,char* name,runtime_t* rt,const char* text);
void runtime_create_string_on_object(tobject_t* tdict_obj,char* name,runtime_t* rt,const char* text);
node_t* node_break_create();
node_t* node_return_create(node_t* arg);

View File

@ -1,14 +1,14 @@
#include "tlang.h"
extern void __node_two_free(node_t* n);
tobject_t* __add_node_exec(node_t* n,scope_t* sc)
tobject_t* __add_node_exec(node_t* n,scope_t* sc,retarg_t* retArg)
{
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* l=left->execute(left,sc,retArg);
tobject_t* r=right->execute(right,sc,retArg);
tobject_t* out=NULL;
tobject_type_t ltype=l->type;
tobject_type_t rtype =r->type;

View File

@ -1,14 +1,14 @@
#include "tlang.h"
extern void __node_two_free(node_t* n);
tobject_t* __bitwiseand_node_exec(node_t* n,scope_t* sc)
tobject_t* __bitwiseand_node_exec(node_t* n,scope_t* sc,retarg_t* retArg)
{
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* l=left->execute(left,sc,retArg);
tobject_t* r=right->execute(right,sc,retArg);
tobject_t* out=NULL;
tobject_type_t ltype=l->type;
tobject_type_t rtype =r->type;

View File

@ -1,14 +1,14 @@
#include "tlang.h"
extern void __node_two_free(node_t* n);
tobject_t* __bitwiseor_node_exec(node_t* n,scope_t* sc)
tobject_t* __bitwiseor_node_exec(node_t* n,scope_t* sc,retarg_t* retArg)
{
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* l=left->execute(left,sc,retArg);
tobject_t* r=right->execute(right,sc,retArg);
tobject_t* out=NULL;
tobject_type_t ltype=l->type;
tobject_type_t rtype =r->type;

21
libtlang/src/breaknode.c Normal file
View File

@ -0,0 +1,21 @@
#include "tlang.h"
tobject_t* __break_node_exec(node_t* n,scope_t* sc,retarg_t* retArg)
{
retArg->isBreaking=true;
return tobject_basic(sc->rt,tnull);
}
void __node_none_free(node_t* n)
{
}
node_t* node_break_create()
{
node_t* node = (node_t*)malloc(sizeof(node_t));
node->type = breaknode;
node->execute = __break_node_exec;
node->free = __node_none_free;
return node;
}

View File

@ -1,12 +1,12 @@
#include "tlang.h"
extern void __node_single_free(node_t* n);
tobject_t* __bwnot_node_exec(node_t* n,scope_t* sc)
tobject_t* __bwnot_node_exec(node_t* n,scope_t* sc,retarg_t* retArg)
{
bool freeleftifzero=true;
node_t* left = n->data.single_node_node;
tobject_t* l=left->execute(left,sc);
tobject_t* l=left->execute(left,sc,retArg);
tobject_t* out=NULL;
tobject_type_t ltype=l->type;

View File

@ -1,6 +1,6 @@
#include "tlang.h"
tobject_t* __closure_node_exec(node_t* n,scope_t* sc)
tobject_t* __closure_node_exec(node_t* n,scope_t* sc,retarg_t* retArg)
{
tobject_t* c = tobject_create(sc->rt);
c->type = tinternalmethod;

View File

@ -1,5 +1,5 @@
#include "tlang.h"
tobject_t* __const_number_exec(node_t* n,scope_t* sc)
tobject_t* __const_number_exec(node_t* n,scope_t* sc,retarg_t* retArg)
{
tobject_t* obj=tobject_create(sc->rt);
obj->data.number = n->data.const_number;
@ -23,7 +23,7 @@ node_t* node_const_number_create(double num)
n->free = __const_number_free;
return n;
}
tobject_t* __const_bool_exec(node_t* n,scope_t* sc)
tobject_t* __const_bool_exec(node_t* n,scope_t* sc,retarg_t* retArg)
{
tobject_t* obj=tobject_create(sc->rt);
obj->data.boolean = n->data.const_bool;
@ -31,7 +31,7 @@ tobject_t* __const_bool_exec(node_t* n,scope_t* sc)
obj->type =tbool;
return obj;
}
tobject_t* __const_string_exec(node_t* n,scope_t* sc)
tobject_t* __const_string_exec(node_t* n,scope_t* sc,retarg_t* retArg)
{
tobject_t* obj=tobject_create(sc->rt);
obj->data.string = string_dups(n->data.const_string);
@ -39,7 +39,7 @@ tobject_t* __const_string_exec(node_t* n,scope_t* sc)
obj->type =tstring;
return obj;
}
tobject_t* __const_char_exec(node_t* n,scope_t* sc)
tobject_t* __const_char_exec(node_t* n,scope_t* sc,retarg_t* retArg)
{
tobject_t* obj=tobject_create(sc->rt);
obj->data.chr = n->data.const_char;
@ -64,7 +64,7 @@ node_t* node_const_char_create(char c)
n->free = __const_number_free;
return n;
}
tobject_t* __const_object_exec(node_t* n,scope_t* sc)
tobject_t* __const_object_exec(node_t* n,scope_t* sc,retarg_t* retArg)
{
return n->data.const_obj;
}
@ -86,7 +86,7 @@ node_t* node_const_bool_create(bool c)
n->free = __const_number_free;
return n;
}
tobject_t* __const_undef_exec(node_t* n,scope_t* s)
tobject_t* __const_undef_exec(node_t* n,scope_t* s,retarg_t* retArg)
{
return tobject_basic(s->rt,tundef);
}
@ -99,7 +99,7 @@ node_t* node_const_undef_create()
n->free = __const_number_free;
return n;
}
tobject_t* __const_null_exec(node_t* n,scope_t* s)
tobject_t* __const_null_exec(node_t* n,scope_t* s,retarg_t* retArg)
{
return tobject_basic(s->rt,tnull);
}

View File

@ -1,14 +1,14 @@
#include "tlang.h"
extern void __node_two_free(node_t* n);
tobject_t* __divide_node_exec(node_t* n,scope_t* sc)
tobject_t* __divide_node_exec(node_t* n,scope_t* sc,retarg_t* retArg)
{
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* l=left->execute(left,sc,retArg);
tobject_t* r=right->execute(right,sc,retArg);
tobject_t* out=NULL;
tobject_type_t ltype=l->type;
tobject_type_t rtype =r->type;

View File

@ -1,21 +1,22 @@
#include "tlang.h"
extern tobject_t* last_ittr_loop(runtime_t* rt,void* ptr,list_tobject_t* obj);
tobject_t* __each_loop_exec(node_t* n,scope_t* s)
tobject_t* __each_loop_exec(node_t* n,scope_t* s,retarg_t* retArg)
{
tobject_t* ls=NULL;
s=scope_begin(s);
if(n->data.eachloop_node.ls != NULL)
ls= n->data.eachloop_node.ls->execute(n->data.eachloop_node.ls,s);
ls= n->data.eachloop_node.ls->execute(n->data.eachloop_node.ls,s,retArg);
bool isRunning=true;
s->setvariable(s,"last_ittr",tobject_fromexternalmethod(s->rt,&isRunning,last_ittr_loop,NULL));
tobject_t* res = tobject_basic(s->rt,tundef);
res->count=1;
retArg->isBreaking=false;
if(ls != NULL)
{
ittr_t* ittr = ittr_obj(ls,s);
if(ittr != NULL)
{
while(ittr_movenext(ittr) && isRunning)
while(ittr_movenext(ittr) && isRunning && !retArg->isBreaking && !retArg->isReturning)
{
tobject_t* obj= ittr->getcurrent(ittr);
if(n->data.eachloop_node.variable->type == getvariablenode || n->data.eachloop_node.variable->type == getmembernode)
@ -26,7 +27,7 @@ tobject_t* __each_loop_exec(node_t* n,scope_t* s)
{
n->data.eachloop_node.variable->data.array_node.set(n->data.eachloop_node.variable,obj,s);
}
tobject_t* myRes=n->data.eachloop_node.body->execute(n->data.eachloop_node.body,s);
tobject_t* myRes=n->data.eachloop_node.body->execute(n->data.eachloop_node.body,s,retArg);
tobject_addref(myRes);
tobject_rmref(res);
@ -36,6 +37,7 @@ tobject_t* __each_loop_exec(node_t* n,scope_t* s)
ittr_free(ittr);
}
}
retArg->isBreaking=false;
s=scope_end(s);
tobject_freeifzero(ls);
res->count--;

View File

@ -2,14 +2,14 @@
#include "tlang.h"
extern void __node_two_free(node_t* n);
tobject_t* __eq_node_exec(node_t* n,scope_t* sc)
tobject_t* __eq_node_exec(node_t* n,scope_t* sc,retarg_t* retArg)
{
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* l=left->execute(left,sc,retArg);
tobject_t* r=right->execute(right,sc,retArg);
tobject_t* out=tobject_same(sc->rt,l,r,&freeleftifzero);
if(out != NULL) out->count++;
if(freeleftifzero)

View File

@ -7,39 +7,41 @@ tobject_t* last_ittr_loop(runtime_t* rt,void* ptr,list_tobject_t* obj)
*flag=false;
return tobject_bool(rt,true);
}
tobject_t* __loop_exec2(node_t* n,scope_t* s)
tobject_t* __loop_exec2(node_t* n,scope_t* s,retarg_t* retArg)
{
if(n == NULL) return tobject_bool(s->rt,true);
return n->execute(n,s);
return n->execute(n,s,retArg);
}
bool __loop_bool(node_t* n,scope_t* s)
bool __loop_bool(node_t* n,scope_t* s,retarg_t* retArg)
{
tobject_t* o = __loop_exec2(n,s);
tobject_t* o = __loop_exec2(n,s,retArg);
bool r = tobject_tobool(o);
tobject_freeifzero(o);
return r;
}
tobject_t* __for_loop_exec(node_t* a,scope_t* s)
tobject_t* __for_loop_exec(node_t* a,scope_t* s,retarg_t* retArg)
{
//do for loop crap
s=scope_begin(s);
if(a->data.forloop_node.init != NULL)
tobject_freeifzero(a->data.forloop_node.init->execute(a->data.forloop_node.init,s));
tobject_freeifzero(a->data.forloop_node.init->execute(a->data.forloop_node.init,s,retArg));
bool isRunning=true;
s->setvariable(s,"last_ittr",tobject_fromexternalmethod(s->rt,&isRunning,last_ittr_loop,NULL));
tobject_t* res = tobject_basic(s->rt,tundef);
res->count=1;
while(__loop_bool(a->data.forloop_node.condition,s) && isRunning)
retArg->isBreaking=false;
while(__loop_bool(a->data.forloop_node.condition,s,retArg) && isRunning && !retArg->isBreaking && !retArg->isReturning)
{
tobject_t* _r = __loop_exec2(a->data.forloop_node.body,s);
tobject_t* _r = __loop_exec2(a->data.forloop_node.body,s,retArg);
tobject_addref(_r);
tobject_rmref(res);
res=_r;
__loop_bool(a->data.forloop_node.inc,s);
__loop_bool(a->data.forloop_node.inc,s,retArg);
}
retArg->isBreaking=false;
s=scope_end(s);
res->count--;

View File

@ -1,5 +1,5 @@
#include "tlang.h"
tobject_t* __node_function_call_execute(node_t* n,scope_t* s)
tobject_t* __node_function_call_execute(node_t* n,scope_t* s,retarg_t* retArg)
{
list_tobject_t args;
@ -9,7 +9,8 @@ tobject_t* __node_function_call_execute(node_t* n,scope_t* s)
for(i = 0;i< n->data.function_call_node.count;i++)
{
node_t* a =n->data.function_call_node.args[i];
list_add(&args,a->execute(a,s));
retarg_t retArg2;
list_add(&args,a->execute(a,s,&retArg2));
}
char* name = string_dupp(n->data.function_call_node.name);
tobject_t* fname = s->getvariable(s,name);

View File

@ -20,13 +20,13 @@ tobject_t* __kvp_as_str(tobject_t* parent_obj,tobject_t* argument_obj)
return NULL;
}
tobject_t* __node_getarray_execute(node_t* n,scope_t* s)
tobject_t* __node_getarray_execute(node_t* n,scope_t* s,retarg_t* retArg)
{
node_t* parent=n->data.array_node.parent;
tobject_t* parent_obj = parent->execute(parent,s);
tobject_t* parent_obj = parent->execute(parent,s,retArg);
tobject_t* o = NULL;
node_t* argument =n->data.array_node.argument;
tobject_t* argument_obj = argument->execute(argument,s);
tobject_t* argument_obj = argument->execute(argument,s,retArg);
if(parent_obj->type == tlist)
{
@ -115,10 +115,13 @@ void __kvp_set(tobject_t* parent_obj,tobject_t* argument_obj,tobject_t* variable
void __node_setarray_execute(node_t* n,tobject_t* variable_obj,scope_t* s)
{
node_t* parent=n->data.array_node.parent;
tobject_t* parent_obj = parent->execute(parent,s);
retarg_t arg;
arg.isBreaking=false;
arg.isReturning=false;
tobject_t* parent_obj = parent->execute(parent,s,&arg);
node_t* argument =n->data.array_node.argument;
tobject_t* argument_obj = argument->execute(argument,s);
tobject_t* argument_obj = argument->execute(argument,s,&arg);
if(parent_obj->type == tlist)
{

View File

@ -31,10 +31,10 @@ tobject_t* __ls_free(runtime_t* rt,void* ptr,list_tobject_t* ls)
return tobject_basic(rt,tnull);
}
tobject_t* __node_getmember_execute(node_t* n,scope_t* s)
tobject_t* __node_getmember_execute(node_t* n,scope_t* s,retarg_t* retArg)
{
node_t* parent=n->data.variable_node.parent;
tobject_t* parent_obj = parent->execute(parent,s);
tobject_t* parent_obj = parent->execute(parent,s,retArg);
char* vn = string_dupp(n->data.variable_node.name);
string_t* getvars = string_create();
@ -166,7 +166,10 @@ void __node_setmember_execute(node_t* n,tobject_t* o,scope_t* s)
{
node_t* parent=n->data.variable_node.parent;
//node_t* myN = n->data.variable_node.value;
tobject_t* parent_obj = parent->execute(parent,s);
retarg_t arg;
arg.isBreaking=false;
arg.isReturning=false;
tobject_t* parent_obj = parent->execute(parent,s,&arg);
char* vn = string_dupp(n->data.variable_node.name);
string_t* getvars = string_create();

View File

@ -1,5 +1,5 @@
#include "tlang.h"
tobject_t* __node_getvariable_execute(node_t* n,scope_t* s)
tobject_t* __node_getvariable_execute(node_t* n,scope_t* s,retarg_t* retArg)
{
char* vn = string_dupp(n->data.variable_node.name);

View File

@ -1,14 +1,14 @@
#include "tlang.h"
extern void __node_two_free(node_t* n);
tobject_t* __greaterthan_node_exec(node_t* n,scope_t* sc)
tobject_t* __greaterthan_node_exec(node_t* n,scope_t* sc,retarg_t* retArg)
{
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* l=left->execute(left,sc,retArg);
tobject_t* r=right->execute(right,sc,retArg);
tobject_t* out=NULL;
tobject_type_t ltype=l->type;
tobject_type_t rtype =r->type;

View File

@ -1,14 +1,14 @@
#include "tlang.h"
extern void __node_two_free(node_t* n);
tobject_t* __greaterthanequal_node_exec(node_t* n,scope_t* sc)
tobject_t* __greaterthanequal_node_exec(node_t* n,scope_t* sc,retarg_t* retArg)
{
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* l=left->execute(left,sc,retArg);
tobject_t* r=right->execute(right,sc,retArg);
tobject_t* out=NULL;
tobject_type_t ltype=l->type;
tobject_type_t rtype =r->type;

View File

@ -1,12 +1,12 @@
#include "tlang.h"
extern bool __loop_bool(node_t* n,scope_t* s);
tobject_t* __if_exec(node_t* n,scope_t* s)
tobject_t* __if_exec(node_t* n,scope_t* s,retarg_t* retArg)
{
if(__loop_bool(n->data.if_node.condition,s))
{
return n->data.if_node.yes->execute(n->data.if_node.yes,s);
return n->data.if_node.yes->execute(n->data.if_node.yes,s,retArg);
} else {
return n->data.if_node.no->execute(n->data.if_node.no,s);
return n->data.if_node.no->execute(n->data.if_node.no,s,retArg);
}
}
void __if_free(node_t* n)

View File

@ -1,14 +1,14 @@
#include "tlang.h"
#include <stdint.h>
extern void __node_two_free(node_t* n);
tobject_t* __leftshift_node_exec(node_t* n,scope_t* sc)
tobject_t* __leftshift_node_exec(node_t* n,scope_t* sc,retarg_t* retArg)
{
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* l=left->execute(left,sc,retArg);
tobject_t* r=right->execute(right,sc,retArg);
tobject_t* out=NULL;
tobject_type_t ltype=l->type;
tobject_type_t rtype =r->type;

View File

@ -1,14 +1,14 @@
#include "tlang.h"
extern void __node_two_free(node_t* n);
tobject_t* __lessthan_node_exec(node_t* n,scope_t* sc)
tobject_t* __lessthan_node_exec(node_t* n,scope_t* sc,retarg_t* retArg)
{
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* l=left->execute(left,sc,retArg);
tobject_t* r=right->execute(right,sc,retArg);
tobject_t* out=NULL;
tobject_type_t ltype=l->type;
tobject_type_t rtype =r->type;

View File

@ -1,14 +1,14 @@
#include "tlang.h"
extern void __node_two_free(node_t* n);
tobject_t* __lessthanequal_node_exec(node_t* n,scope_t* sc)
tobject_t* __lessthanequal_node_exec(node_t* n,scope_t* sc,retarg_t* retArg)
{
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* l=left->execute(left,sc,retArg);
tobject_t* r=right->execute(right,sc,retArg);
tobject_t* out=NULL;
tobject_type_t ltype=l->type;
tobject_type_t rtype =r->type;

View File

@ -1,14 +1,14 @@
#include "tlang.h"
extern void __node_two_free(node_t* n);
tobject_t* __logicaland_node_exec(node_t* n,scope_t* sc)
tobject_t* __logicaland_node_exec(node_t* n,scope_t* sc,retarg_t* retArg)
{
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* l=left->execute(left,sc,retArg);
tobject_t* r=right->execute(right,sc,retArg);
tobject_t* out=NULL;
tobject_type_t ltype=l->type;
tobject_type_t rtype =r->type;

View File

@ -1,14 +1,14 @@
#include "tlang.h"
extern void __node_two_free(node_t* n);
tobject_t* __logicalor_node_exec(node_t* n,scope_t* sc)
tobject_t* __logicalor_node_exec(node_t* n,scope_t* sc,retarg_t* retArg)
{
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* l=left->execute(left,sc,retArg);
tobject_t* r=right->execute(right,sc,retArg);
tobject_t* out=NULL;
tobject_type_t ltype=l->type;
tobject_type_t rtype =r->type;

View File

@ -2,10 +2,10 @@
#include <ctype.h>
#include <stdint.h>
#include <math.h>
tobject_t* __node_method_call_execute(node_t* n,scope_t* s)
tobject_t* __node_method_call_execute(node_t* n,scope_t* s,retarg_t* retArg)
{
node_t* parent=n->data.variable_node.parent;
tobject_t* parent_obj = parent->execute(parent,s);
tobject_t* parent_obj = parent->execute(parent,s,retArg);
tobject_t* o=NULL;
char* name = string_dupp(n->data.function_call_node.name);
@ -22,7 +22,10 @@ tobject_t* __node_method_call_execute(node_t* n,scope_t* s)
for(i = 0;i< n->data.function_call_node.count;i++)
{
node_t* a =n->data.function_call_node.args[i];
list_add(&args,a->execute(a,s));
retarg_t _arg;
_arg.isBreaking=false;
_arg.isReturning=false;
list_add(&args,a->execute(a,s,&_arg));
}
o = tobject_call(s,fname,&args);
@ -78,8 +81,8 @@ tobject_t* __node_method_call_execute(node_t* n,scope_t* s)
{
if(argcount >= 2)
{
tobject_t* a0=args[0]->execute(args[0],s);
tobject_t* a1=args[1]->execute(args[1],s);
tobject_t* a0=args[0]->execute(args[0],s,retArg);
tobject_t* a1=args[1]->execute(args[1],s,retArg);
string_t* s1=tobject_tostring(s,a0);
string_t* s2=tobject_tostring(s,a1);
@ -94,7 +97,7 @@ tobject_t* __node_method_call_execute(node_t* n,scope_t* s)
if(argcount >= 1)
{
tobject_t* sOn=args[0]->execute(args[0],s);
tobject_t* sOn=args[0]->execute(args[0],s,retArg);
string_t* splitOn=tobject_tostring(s,sOn);
int count = 2147483647;
@ -103,14 +106,14 @@ tobject_t* __node_method_call_execute(node_t* n,scope_t* s)
if(argcount >= 2)
{
tobject_t* sOn=args[1]->execute(args[1],s);
tobject_t* sOn=args[1]->execute(args[1],s,retArg);
splitOp = tobject_tobool(sOn);
//tobject_freeifzero(sOn);
}
if(argcount >= 3)
{
tobject_t* _count=args[2]->execute(args[2],s);
tobject_t* _count=args[2]->execute(args[2],s,retArg);
if(_count->type == tnumber)
{
count = (int)_count->data.number;
@ -128,7 +131,7 @@ tobject_t* __node_method_call_execute(node_t* n,scope_t* s)
{
if(argcount >= 1)
{
tobject_t* sOn=args[0]->execute(args[0],s);
tobject_t* sOn=args[0]->execute(args[0],s,retArg);
string_t* starts=tobject_tostring(s,sOn);
o = tobject_bool(s->rt,string_startswith(parent_obj->data.string,starts));
@ -140,7 +143,7 @@ tobject_t* __node_method_call_execute(node_t* n,scope_t* s)
{
if(argcount >= 1)
{
tobject_t* sOn=args[0]->execute(args[0],s);
tobject_t* sOn=args[0]->execute(args[0],s,retArg);
string_t* starts=tobject_tostring(s,sOn);
o = tobject_bool(s->rt,string_endswith(parent_obj->data.string,starts));
@ -157,7 +160,7 @@ tobject_t* __node_method_call_execute(node_t* n,scope_t* s)
char c = ' ';
if(argcount >= 1)
{
tobject_t* sOn=args[0]->execute(args[0],s);
tobject_t* sOn=args[0]->execute(args[0],s,retArg);
if(sOn->type == tchar)
{
c = sOn->data.chr;
@ -174,7 +177,7 @@ tobject_t* __node_method_call_execute(node_t* n,scope_t* s)
char c = ' ';
if(argcount >= 1)
{
tobject_t* sOn=args[0]->execute(args[0],s);
tobject_t* sOn=args[0]->execute(args[0],s,retArg);
if(sOn->type == tchar)
{
c = sOn->data.chr;
@ -224,7 +227,7 @@ tobject_t* __node_method_call_execute(node_t* n,scope_t* s)
{
if(argcount >= 1)
{
tobject_t* o2=args[0]->execute(args[0],s);
tobject_t* o2=args[0]->execute(args[0],s,retArg);
list_add(&parent_obj->data.list,o2);
o=parent_obj;
@ -234,7 +237,7 @@ tobject_t* __node_method_call_execute(node_t* n,scope_t* s)
{
if(argcount >= 1)
{
tobject_t* o2=args[0]->execute(args[0],s);
tobject_t* o2=args[0]->execute(args[0],s,retArg);
list_remove(&parent_obj->data.list,o2);
o=parent_obj;
@ -244,7 +247,7 @@ tobject_t* __node_method_call_execute(node_t* n,scope_t* s)
{
if(argcount >= 1)
{
tobject_t* o2=args[0]->execute(args[0],s);
tobject_t* o2=args[0]->execute(args[0],s,retArg);
if(o2->type != tnumber)
{
int index=(int)o2->data.number;
@ -262,7 +265,7 @@ tobject_t* __node_method_call_execute(node_t* n,scope_t* s)
tobject_t* val = NULL;
if(argcount >= 2)
{
val = args[1]->execute(args[1],s);
val = args[1]->execute(args[1],s,retArg);
o=val;
}
else if(argcount == 1)
@ -272,7 +275,7 @@ tobject_t* __node_method_call_execute(node_t* n,scope_t* s)
}
if(argcount >= 1)
{
tobject_t* o2=args[0]->execute(args[0],s);
tobject_t* o2=args[0]->execute(args[0],s,retArg);
if(o2->type != tnumber)
{
int index=(int)o2->data.number;

View File

@ -2,14 +2,14 @@
#include <math.h>
extern void __node_two_free(node_t* n);
tobject_t* __modulo_node_exec(node_t* n,scope_t* sc)
tobject_t* __modulo_node_exec(node_t* n,scope_t* sc,retarg_t* retArg)
{
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* l=left->execute(left,sc,retArg);
tobject_t* r=right->execute(right,sc,retArg);
tobject_t* out=NULL;
tobject_type_t ltype=l->type;
tobject_type_t rtype =r->type;

View File

@ -1,14 +1,14 @@
#include "tlang.h"
extern void __node_two_free(node_t* n);
tobject_t* __multiply_node_exec(node_t* n,scope_t* sc)
tobject_t* __multiply_node_exec(node_t* n,scope_t* sc,retarg_t* retArg)
{
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* l=left->execute(left,sc,retArg);
tobject_t* r=right->execute(right,sc,retArg);
tobject_t* out=NULL;
tobject_type_t ltype=l->type;
tobject_type_t rtype =r->type;

View File

@ -1,11 +1,11 @@
#include "tlang.h"
extern void __node_single_free(node_t* n);
tobject_t* __neg_node_exec(node_t* n,scope_t* sc)
tobject_t* __neg_node_exec(node_t* n,scope_t* sc,retarg_t* retArg)
{
bool freeleftifzero=true;
node_t* left = n->data.single_node_node;
tobject_t* l=left->execute(left,sc);
tobject_t* l=left->execute(left,sc,retArg);
tobject_t* out=NULL;
tobject_type_t ltype=l->type;

View File

@ -2,14 +2,14 @@
#include "tlang.h"
extern void __node_two_free(node_t* n);
tobject_t* __neq_node_exec(node_t* n,scope_t* sc)
tobject_t* __neq_node_exec(node_t* n,scope_t* sc,retarg_t* retArg)
{
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* l=left->execute(left,sc,retArg);
tobject_t* r=right->execute(right,sc,retArg);
tobject_t* out=NULL;
tobject_type_t ltype=l->type;
tobject_type_t rtype =r->type;

View File

@ -4,12 +4,12 @@ void __node_single_free(node_t* n)
{
node_free(n->data.single_node_node);
}
tobject_t* __not_node_exec(node_t* n,scope_t* sc)
tobject_t* __not_node_exec(node_t* n,scope_t* sc,retarg_t* retArg)
{
bool freeleftifzero=true;
node_t* left = n->data.single_node_node;
tobject_t* l=left->execute(left,sc);
tobject_t* l=left->execute(left,sc,retArg);
tobject_t* out=NULL;
tobject_type_t ltype=l->type;

View File

@ -376,6 +376,14 @@ node_t* parse_value(parser_t* p)
}
return node_const_char_create(c);
}
if(string_samep(token.text,"brk") || string_samep(token.text,"break"))
{
return node_break_create();
}
if(string_samep(token.text,"ret") || string_samep(token.text,"return"))
{
return node_return_create(parse_expression(p));
}
if(string_samep(token.text,"-"))
{

View File

@ -1,13 +1,13 @@
#include "tlang.h"
extern void __node_single_free(node_t* n);
tobject_t* __postdec_node_exec(node_t* n,scope_t* sc)
tobject_t* __postdec_node_exec(node_t* n,scope_t* sc,retarg_t* retArg)
{
bool freeleftifzero=true;
node_t* left = n->data.single_node_node;
tobject_t* l=left->execute(left,sc);
tobject_t* l=left->execute(left,sc,retArg);
tobject_t* out=NULL;
@ -15,7 +15,7 @@ tobject_t* __postdec_node_exec(node_t* n,scope_t* sc)
l->count++;
node_t* n3=node_setvariable_create(left,node_sub_create(node_const_object_create(l),node_const_number_create(1)),true);
n3->execute(n3,sc);
n3->execute(n3,sc,retArg);
n3->data.two_node_node.left = node_const_number_create(0);
node_free(n3);

View File

@ -1,13 +1,13 @@
#include "tlang.h"
extern void __node_single_free(node_t* n);
tobject_t* __postinc_node_exec(node_t* n,scope_t* sc)
tobject_t* __postinc_node_exec(node_t* n,scope_t* sc,retarg_t* retArg)
{
bool freeleftifzero=true;
node_t* left = n->data.single_node_node;
tobject_t* l=left->execute(left,sc);
tobject_t* l=left->execute(left,sc,retArg);
tobject_t* out=NULL;
@ -16,7 +16,8 @@ tobject_t* __postinc_node_exec(node_t* n,scope_t* sc)
node_t* n3=node_setvariable_create(left,node_add_create(node_const_object_create(l),node_const_number_create(1)),true);
n3->execute(n3,sc);
n3->execute(n3,sc,retArg);
n3->data.two_node_node.left = node_const_number_create(0);
node_free(n3);

35
libtlang/src/rand.c Normal file
View File

@ -0,0 +1,35 @@
#include "tlang.h"
#include <stdlib.h>
#include <stdint.h>
tobject_t* rand_getnext_external_function(runtime_t* rt,void* ptr,list_tobject_t* args)
{
if(args->length >= 0)
{
double _res = (double)rand();
return tobject_number(rt,_res);
}
return tobject_basic(rt,tnull);
}
tobject_t* rand_setseed_external_function(runtime_t* rt,void* ptr,list_tobject_t* args)
{
if(args->length >= 1 && (args->items[0]->type == tnumber || args->items[0]->type == tnull))
{
int _arg0 = (int)args->items[0]->data.number;
srand(_arg0);
}
return tobject_basic(rt,tnull);
}
void runtime_register_rand(runtime_t* rt)
{
tobject_t* _rand = tobject_create(rt);
_rand->type = tdict;
_rand->data.dict = dict_create();
runtime_create_method_on_dict(_rand->data.dict,"getnext",rt,NULL,rand_getnext_external_function,NULL);
runtime_create_method_on_dict(_rand->data.dict,"setseed",rt,NULL,rand_setseed_external_function,NULL);
rt->globals->setvariable(rt->globals,"rand",_rand);
}

22
libtlang/src/returnnode.c Normal file
View File

@ -0,0 +1,22 @@
#include "tlang.h"
extern void __node_single_free(node_t*);
tobject_t* __return_node_exec(node_t* n,scope_t* sc,retarg_t* retArg)
{
node_t* left = n->data.single_node_node;
tobject_t* l=left->execute(left,sc,retArg);
retArg->isReturning=true;
return l;
}
node_t* node_return_create(node_t* arg)
{
node_t* node = (node_t*)malloc(sizeof(node_t));
node->type = returnnode;
node->data.single_node_node = arg;
node->execute = __return_node_exec;
node->free = __node_single_free;
return node;
}

View File

@ -1,14 +1,14 @@
#include "tlang.h"
#include <stdint.h>
extern void __node_two_free(node_t* n);
tobject_t* __rightshift_node_exec(node_t* n,scope_t* sc)
tobject_t* __rightshift_node_exec(node_t* n,scope_t* sc,retarg_t* retArg)
{
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* l=left->execute(left,sc,retArg);
tobject_t* r=right->execute(right,sc,retArg);
tobject_t* out=NULL;
tobject_type_t ltype=l->type;
tobject_type_t rtype =r->type;

View File

@ -281,7 +281,10 @@ tobject_t* __execute_code_rt(runtime_t* rt,void* ptr,list_tobject_t* args)
}
tobject_t* runtime_exec(runtime_t* rt)
{
tobject_t* o = rt->program->execute(rt->program,rt->globals);
retarg_t _arg;
_arg.isBreaking=false;
_arg.isReturning=false;
tobject_t* o = rt->program->execute(rt->program,rt->globals,&_arg);
if(rt->globals->hasvariable(rt->globals,"main"))
{
tobject_t* myObj=rt->globals->getvariable(rt->globals,"main");
@ -838,6 +841,7 @@ void runtime_register(runtime_t* rt,runtime_reg_t reg)
rt->reg = reg;
reg(rt);
}
extern void runtime_register_rand(runtime_t* rt);
void runtime_register_std(runtime_t* rt)
{
@ -925,7 +929,7 @@ void runtime_register_std(runtime_t* rt)
#if defined(USE_MBED)
runtime_register_mbed(rt);
#endif
runtime_register_rand(rt);
}
runtime_t* runtime_init()

View File

@ -1,5 +1,5 @@
#include "tlang.h"
tobject_t* __node_scope_execute(node_t* n,scope_t* s)
tobject_t* __node_scope_execute(node_t* n,scope_t* s,retarg_t* retArg)
{
if(n->data.scope_node.isSub)
{
@ -8,12 +8,18 @@ tobject_t* __node_scope_execute(node_t* n,scope_t* s)
tobject_t* obj_data = tobject_basic(s->rt,tnull);
tobject_addref(obj_data);
int i;
for(i=0;i<n->data.scope_node.count;i++)
{
tobject_t* d=n->data.scope_node.items[i]->execute(n->data.scope_node.items[i],s);
tobject_t* d=n->data.scope_node.items[i]->execute(n->data.scope_node.items[i],s,retArg);
tobject_addref(d);
tobject_rmref(obj_data);
obj_data=d;
if(retArg->isBreaking || retArg->isReturning)
{
break;
}
}
if(n->data.scope_node.isSub)
{

View File

@ -1,10 +1,10 @@
#include "tlang.h"
tobject_t* _node_setvariable_exec(node_t* n,scope_t* sc)
tobject_t* _node_setvariable_exec(node_t* n,scope_t* sc,retarg_t* retArg)
{
node_t* left=n->data.variable_set_node.variable;
node_t* right = n->data.variable_set_node.value;
tobject_t* o = right->execute(right,sc);
tobject_t* o = right->execute(right,sc,retArg);
o->count++;
if(left->type == getarraynode)
{

View File

@ -1,14 +1,14 @@
#include "tlang.h"
extern void __node_two_free(node_t* n);
tobject_t* __sub_node_exec(node_t* n,scope_t* sc)
tobject_t* __sub_node_exec(node_t* n,scope_t* sc,retarg_t* retArg)
{
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* l=left->execute(left,sc,retArg);
tobject_t* r=right->execute(right,sc,retArg);
tobject_t* out=NULL;
tobject_type_t ltype=l->type;
tobject_type_t rtype =r->type;

View File

@ -300,7 +300,10 @@ tobject_t* tobject_call(scope_t* scope,tobject_t* func,list_tobject_t* args)
}
}
res=func->data.internal_method.closure->data.closure_node.node->execute(func->data.internal_method.closure->data.closure_node.node,scope2);
retarg_t _arg;
_arg.isBreaking=false;
_arg.isReturning=false;
res=func->data.internal_method.closure->data.closure_node.node->execute(func->data.internal_method.closure->data.closure_node.node,scope2,&_arg);
scope_end(scope2);
return res;
}

View File

@ -1,8 +1,8 @@
#include "tlang.h"
extern tobject_t* __loop_exec2(node_t* n,scope_t* s);
extern bool __loop_bool(node_t* n,scope_t* s);
extern tobject_t* __loop_exec2(node_t* n,scope_t* s,retarg_t* retArg);
extern bool __loop_bool(node_t* n,scope_t* s,retarg_t* retArg);
extern tobject_t* last_ittr_loop(runtime_t* rt,void* ptr,list_tobject_t* obj);
node_t* __while_loop_exec(node_t* n,scope_t* s)
node_t* __while_loop_exec(node_t* n,scope_t* s,retarg_t* retArg)
{
bool isRunning=true;
s=scope_begin(s);
@ -13,19 +13,21 @@ node_t* __while_loop_exec(node_t* n,scope_t* s)
res->count=1;
if(!n->data.whileloop_node.isDoLoop)
{
isRunning = __loop_bool(n->data.whileloop_node.condition,s);
isRunning = __loop_bool(n->data.whileloop_node.condition,s,retArg);
}
while(isRunning)
retArg->isBreaking=false;
while(isRunning && !retArg->isBreaking && !retArg->isReturning)
{
tobject_t* _r = __loop_exec2(n->data.whileloop_node.body,s);
tobject_t* _r = __loop_exec2(n->data.whileloop_node.body,s,retArg);
tobject_addref(_r);
tobject_rmref(res);
res=_r;
if(isRunning)
if(isRunning && !retArg->isBreaking && !retArg->isReturning)
{
isRunning = __loop_bool(n->data.whileloop_node.condition,s);
isRunning = __loop_bool(n->data.whileloop_node.condition,s,retArg);
}
}
retArg->isBreaking=false;
s=scope_end(s);
res->count--;
return res;

View File

@ -1,14 +1,14 @@
#include "tlang.h"
extern void __node_two_free(node_t* n);
tobject_t* __xor_node_exec(node_t* n,scope_t* sc)
tobject_t* __xor_node_exec(node_t* n,scope_t* sc,retarg_t* retArg)
{
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* l=left->execute(left,sc,retArg);
tobject_t* r=right->execute(right,sc,retArg);
tobject_t* out=NULL;
tobject_type_t ltype=l->type;
tobject_type_t rtype =r->type;

View File

@ -26,7 +26,7 @@ int main(int argc,char** argv)
}
if(string_samep(cmd,"exit")) break;
runtime_load(rt,cmd);
tobject_t* o = rt->program->execute(rt->program,rt->globals);
tobject_t* o = runtime_exec(rt);
tobject_freeifzero(o);
node_free(rt->program);
rt->program=NULL;

View File

@ -5,6 +5,7 @@ build-tlang-external:
tlang src/wrappergen.tlang src/data.txt dest --external
sdl:
tlang src/wrappergen.tlang src/sdl.txt wrappers --omit
PHONY: install
install: sdl
cp wrappers/sdl.c ../libtlang/src/sdl2.c ; true