Switch to CMake

This commit is contained in:
Mike Nolan 2023-05-10 06:17:11 -05:00
parent 362924c107
commit 679b0e4564
36 changed files with 12400 additions and 141 deletions

1
.gitignore vendored
View File

@ -8,3 +8,4 @@ tlang/tlang
tlang/my_junk_scripts
wrappergen/dest
wrappergen/wrappers
working/

2
.vscode/launch.json vendored
View File

@ -11,7 +11,7 @@
"program": "${workspaceFolder}/tlang/tlang",
"args": ["${workspaceFolder}/tlang/my_junk_scripts/https_tesses.tlang"],
"stopAtEntry": false,
"cwd": "${fileDirname}",
"cwd": "${workspaceFolder}/working/",
"environment": [],
"externalConsole": false,
"MIMode": "gdb",

45
libtlang/CMakeLists.txt Normal file
View File

@ -0,0 +1,45 @@
project(tlanginterp)
add_library(tlanginterp STATIC src/addnode.c src/bit.c src/bitwiseand.c src/bitwiseor.c src/breaknode.c src/bwnot.c src/closurenode.c src/constnode.c src/curl.c src/dict.c src/dirittr.c src/dividenode.c src/eachnode.c src/eq.c src/forloop.c src/fs.c src/functioncallnode.c src/getarraynode.c src/getmembernode.c src/getvariablenode.c src/getvariablenode.c src/greaterthan.c src/greaterthanequal.c src/if.c src/ittr.c src/json.c src/leftshift.c src/lessthan.c src/lessthanequal.c src/lexer.c src/lextokenlist.c src/listittr.c src/logicaland.c src/logicalor.c src/mbed.c src/methodcallnode.c src/modulonode.c src/multiplynode.c src/neg.c src/neq.c src/net.c src/nodetwo.c src/not.c src/parser.c src/postfixdecrementnode.c src/postfixincrementnode.c src/rand.c src/returnnode.c src/rightshift.c src/runtime.c src/scope.c src/scopenode.c src/sdl2.c src/setvariablenode.c src/stream.c src/string.c src/stringittr.c src/subnode.c src/switch.c src/threading.c src/tobject.c src/whileloop.c src/wpad-wii.c src/xor.c include/FreeMonoBold.h include/myfeatures.h include/tlang_version.h include/tlang.h)
option(USE_SDL "Enable SDL2" ON)
option(USE_NETWORK "Enable Networking" ON)
option(USE_CURL "Enable curl" ON)
option(USE_JANSSON "Use JANSSON (JSON)" ON)
option(USE_THREAD "Use Threading" ON)
if(USE_THREAD)
add_definitions(-DUSE_THREADS)
target_link_libraries(tlanginterp pthread)
endif()
if(USE_NETWORK)
add_definitions(-DUSE_NETWORK)
endif()
if(USE_SDL)
add_definitions(-DUSE_SDL2)
find_package(SDL2 REQUIRED)
target_link_libraries(tlanginterp SDL2_gfx SDL2_image SDL2_mixer SDL2_ttf)
include_directories(${SDL2_INCLUDE_DIRS})
target_link_libraries(tlanginterp ${SDL2_LIBRARIES})
endif()
if(USE_CURL)
add_definitions(-DUSE_CURL)
find_package(CURL REQUIRED)
include_directories(${CURL_INCLUDE_DIRS})
target_link_libraries(tlanginterp ${CURL_LIBRARIES})
endif()
if(USE_JANSSON)
add_definitions(-DUSE_JANSSON)
target_link_libraries(tlanginterp jansson)
endif()
target_link_libraries(tlanginterp m)
target_include_directories(tlanginterp PUBLIC "${PROJECT_BINARY_DIR}"
"${PROJECT_SOURCE_DIR}/include")

View File

@ -1,18 +0,0 @@
CC :=gcc
AR := ar
CFLAGS := -Wall -g -c
target := libtlang.a
tlang_c_source_files := $(shell find src/ -name *.c)
tlang_c_object_files := $(patsubst src/%.c, build/%.o, $(tlang_c_source_files))
$(target): $(tlang_c_object_files)
$(AR) -rcs $(target) $(tlang_c_object_files)
$(tlang_c_object_files): build/%.o : src/%.c
mkdir -p $(dir $@) && \
$(CC) $(CFLAGS) -I include $(patsubst build/%.o, src/%.c, $@) -o $@
.PHONY: clean
clean:
rm -rf build; rm $(target)

File diff suppressed because it is too large Load Diff

View File

@ -3,4 +3,6 @@
//#define USE_WIISOCKETS_ON_WII //comment out for #include <network.h>
//#define USE_THREADS
//#define USE_MBED
//#define USE_CURL
//#define USE_JANSSON
//#define USE_GTK

View File

@ -1,4 +1,5 @@
#pragma once
#include "tlang_version.h"
#include "myfeatures.h"
#include <stdio.h>
#include <stdlib.h>
@ -46,6 +47,7 @@ typedef struct
{
bool isReturning;
bool isBreaking;
bool isContinue;
} 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*);
@ -67,7 +69,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,breaknode,returnnode} node_type_t;
typedef enum {postfixincrementnode,postfixdecrementnode,methodcallnode,bnotnode,negnode,notnode,casenode,defaultnode,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,switchnode,functioncallnode,setvariablenode,getvariablenode,getmembernode,breaknode,returnnode,continuenode} node_type_t;
typedef struct {
char* key;
@ -166,6 +168,7 @@ struct node
int count;
int capacity;
bool isSub;
node_t* switch_arg;
} scope_node;
node_t* single_node_node;
struct {
@ -461,5 +464,8 @@ void runtime_create_method_on_object(tobject_t* tdict_obj,char* name,runtime_t*
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);
node_t* node_break_create();
node_t* node_break_create(bool isBreak);
node_t* node_return_create(node_t* arg);
node_t* node_case_create(node_t* left,node_t* right);
node_t* node_default_create(node_t* body);
tobject_t* runtime_eval(runtime_t* rt,string_t* str,scope_t* sc);

View File

@ -19,7 +19,7 @@ tobject_t* __add_node_exec(node_t* n,scope_t* sc,retarg_t* retArg)
kvp_t* kvp=dict_getkvp(l->data.dict,"add");
if(kvp->value->type == texternalmethod || kvp->value->type == tinternalmethod)
{
list_tobject_t ls;
list_tobject_t ls;
list_create(sc->rt,&ls,0);
list_add(&ls,r);
out=tobject_call(sc->rt->globals,kvp->value,&ls);

View File

@ -3,17 +3,20 @@
tobject_t* __break_node_exec(node_t* n,scope_t* sc,retarg_t* retArg)
{
if(n->type == breaknode)
retArg->isBreaking=true;
else
retArg->isContinue=true;
return tobject_basic(sc->rt,tnull);
}
void __node_none_free(node_t* n)
{
}
node_t* node_break_create()
node_t* node_break_create(bool isBreak)
{
node_t* node = (node_t*)malloc(sizeof(node_t));
node->type = breaknode;
node->type = isBreak ? breaknode : continuenode;
node->execute = __break_node_exec;
node->free = __node_none_free;

164
libtlang/src/curl.c Normal file
View File

@ -0,0 +1,164 @@
#include <tlang.h>
#if defined(USE_NETWORK) && defined(USE_CURL)
#include <curl/curl.h>
#include <curl/easy.h>
typedef struct
{
CURL* curl;
dict_t* dict;
bool hasWriteFunction;
runtime_t* rt;
} curl_ctx;
tobject_t* __net_curl_perform_external_method(runtime_t* rt,void* ptr,list_tobject_t* args)
{
curl_ctx* curl = (curl_ctx*)ptr;
return tobject_number(rt,(double)curl_easy_perform(curl->curl));
}
tobject_t* __net_curl_seturl_external_method(runtime_t* rt,void* ptr,list_tobject_t* args)
{
if(args->length == 1 && args->items[0]->type == tstring)
{
curl_ctx* curl = (curl_ctx*)ptr;
char* url = string_dupp(args->items[0]->data.string);
curl_easy_setopt(curl->curl,CURLOPT_URL,url);
free(url);
}
return tobject_basic(rt,tnull);
}
void __net_curl_free(tobject_t* obj)
{
curl_ctx* curl = (curl_ctx*)obj->ptr_data;
curl_easy_cleanup(curl->curl);
dict_free(curl->dict);
free(curl);
}
extern bool tobject_write_call(tobject_t* f,runtime_t* rt,void* ptr,int64_t len);
size_t __curl_writefn(char* ptr,size_t size,size_t nmemb,void* userdata)
{
curl_ctx* ctx2 = (curl_ctx*)userdata;
bool retVal = false; //need to set to true at end
if(ctx2->hasWriteFunction && dict_haskey(ctx2->dict,"writefunction"))
{
tobject_t* write_function = dict_getkvp(ctx2->dict,"writefunction")->value;
if(write_function != NULL && (write_function->type == tinternalmethod || write_function->type == texternalmethod))
{
retVal = tobject_write_call(write_function,ctx2->rt,ptr,size * nmemb);
}
}
return retVal ? size * nmemb : 0;
}
tobject_t* __net_curl_setwritefunction_external_method(runtime_t* rt,void* ptr,list_tobject_t* args)
{
if(args->length == 1 && args->items[0]->type == tinternalmethod || args->items[0]->type == texternalmethod)
{
curl_ctx* ctx2 = (curl_ctx*)ptr;
if(!ctx2->hasWriteFunction)
{
curl_easy_setopt(ctx2->curl,CURLOPT_WRITEFUNCTION,__curl_writefn);
curl_easy_setopt(ctx2->curl,CURLOPT_WRITEDATA,ctx2);
ctx2->hasWriteFunction=true;
}
kvp_t kvp;
kvp.key = "writefunction";
kvp.value = args->items[0];
dict_setkvp(ctx2->dict,kvp);
}
return tobject_basic(rt,tnull);
}
tobject_t* __Net_curl_setfollowlocation_external_method(runtime_t* rt,void* ptr,list_tobject_t* args)
{
if(args->length > 0)
{
curl_ctx* ctx2 = (curl_ctx*)ptr;
long fredirect=0;
if( args->items[0]->type == tbool)
{
fredirect = args->items[0]->data.boolean ? 1 : 0;
}
if(args->items[0]->type == tnumber)
{
fredirect = (long)args->items[0]->data.number;
}
curl_easy_setopt(ctx2->curl, CURLOPT_FOLLOWLOCATION, fredirect);
}
return tobject_basic(rt,tnull);
}
tobject_t* __net_curl_setpostfields_external_method(runtime_t* rt,void* ptr,list_tobject_t* args)
{
if(args->length > 0 && args->items[0]->type == tstring)
{
curl_ctx* ctx2 = (curl_ctx*)ptr;
char* meth = string_dupp(args->items[0]->data.string);
curl_easy_setopt(ctx2->curl, CURLOPT_POSTFIELDS, meth);
}
return tobject_basic(rt,tnull);
}
tobject_t* __net_curl_setmethod_external_method(runtime_t* rt,void* ptr,list_tobject_t* args)
{
if(args->length > 0 && args->items[0]->type == tstring)
{
curl_ctx* ctx2 = (curl_ctx*)ptr;
char* meth = string_dupp(args->items[0]->data.string);
curl_easy_setopt(ctx2->curl, CURLOPT_CUSTOMREQUEST, meth);
free(meth);
}
return tobject_basic(rt,tnull);
}
tobject_t* __net_curl_setrequestheaders_external_method(runtime_t* rt,void* ptr,list_tobject_t* args)
{
if(args->length > 0 && args->items[0]->type == tlist)
{
curl_ctx* ctx2 = (curl_ctx*)ptr;
struct curl_slist *headers = NULL;
int i;
for(i=0;i<args->items[0]->data.list.length;i++)
{
string_t* str=tobject_tostring(rt->globals,args->items[0]->data.list.items[i]);
char* data= string_dupp(str);
string_free(str);
headers=curl_slist_append(headers,data);
free(data);
}
curl_easy_setopt(ctx2->curl, CURLOPT_HTTPHEADER, headers);
}
return tobject_basic(rt,tnull);
}
tobject_t* __net_curl_external_method(runtime_t* rt,void* ptr,list_tobject_t* args)
{
CURL* curl = curl_easy_init();
if(curl != NULL){
tobject_t* ctx = tobject_create(rt);
ctx->type = tdict;
ctx->data.dict = dict_create();
ctx->free = __net_curl_free;
curl_ctx* ctx2 = (curl_ctx*)malloc(sizeof(curl_ctx));
ctx2->rt=rt;
ctx2->curl=curl;
ctx2->dict = dict_create();
ctx2->hasWriteFunction = false;
ctx->ptr_data = ctx2;
runtime_create_method_on_object(ctx,"seturl",rt,ctx2,__net_curl_seturl_external_method,NULL);
runtime_create_method_on_object(ctx,"perform",rt,ctx2,__net_curl_perform_external_method,NULL);
runtime_create_method_on_object(ctx,"setwritefunction",rt,ctx2,__net_curl_setwritefunction_external_method,NULL);
runtime_create_method_on_object(ctx,"setmethod",rt,ctx2,__net_curl_setmethod_external_method,NULL);
runtime_create_method_on_object(ctx,"setpostfields",rt,ctx2,__net_curl_setpostfields_external_method,NULL);
runtime_create_method_on_object(ctx,"setrequestheaders",rt,ctx2,__net_curl_setrequestheaders_external_method,NULL);
runtime_create_method_on_object(ctx,"setfollowlocation",rt,ctx2,__Net_curl_setfollowlocation_external_method,NULL);
return ctx;
}
return tobject_basic(rt,tnull);
}
void runtime_register_curl(runtime_t* rt,dict_t* net)
{
runtime_create_method_on_dict(net,"curl",rt,NULL,__net_curl_external_method,NULL);
}
#endif

View File

@ -20,7 +20,7 @@ void _dict_setkvp(dict_t* dict,kvp_t kvp)
return;
}
}
if(dict->length + 128 > dict->capacity)
if(dict->length + 1 > dict->capacity)
{
dict->capacity = dict->length + 128;
dict->items=(kvp_t*)realloc(dict,dict->capacity * sizeof(kvp_t));

View File

@ -1,22 +1,21 @@
#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,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,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;
retArg->isContinue=false;
if(ls != NULL)
{
ittr_t* ittr = ittr_obj(ls,s);
if(ittr != NULL)
{
while(ittr_movenext(ittr) && isRunning && !retArg->isBreaking && !retArg->isReturning)
while(ittr_movenext(ittr) && !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)
@ -28,6 +27,7 @@ tobject_t* __each_loop_exec(node_t* n,scope_t* s,retarg_t* retArg)
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,retArg);
retArg->isContinue=false;
tobject_addref(myRes);
tobject_rmref(res);
@ -38,6 +38,7 @@ tobject_t* __each_loop_exec(node_t* n,scope_t* s,retarg_t* retArg)
}
}
retArg->isBreaking=false;
retArg->isContinue=false;
s=scope_end(s);
tobject_freeifzero(ls);
res->count--;

View File

@ -1,12 +1,6 @@
#include "tlang.h"
tobject_t* last_ittr_loop(runtime_t* rt,void* ptr,list_tobject_t* obj)
{
bool* flag = (bool*)ptr;
*flag=false;
return tobject_bool(rt,true);
}
tobject_t* __loop_exec2(node_t* n,scope_t* s,retarg_t* retArg)
{
if(n == NULL) return tobject_bool(s->rt,true);
@ -26,15 +20,15 @@ tobject_t* __for_loop_exec(node_t* a,scope_t* s,retarg_t* retArg)
if(a->data.forloop_node.init != NULL)
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);
tobject_t* res = tobject_basic(s->rt,tundef);
res->count=1;
retArg->isBreaking=false;
while(__loop_bool(a->data.forloop_node.condition,s,retArg) && isRunning && !retArg->isBreaking && !retArg->isReturning)
retArg->isContinue=false;
while(__loop_bool(a->data.forloop_node.condition,s,retArg) && !retArg->isBreaking && !retArg->isReturning)
{
tobject_t* _r = __loop_exec2(a->data.forloop_node.body,s,retArg);
retArg->isContinue=false;
tobject_addref(_r);
tobject_rmref(res);
res=_r;
@ -42,7 +36,7 @@ tobject_t* __for_loop_exec(node_t* a,scope_t* s,retarg_t* retArg)
__loop_bool(a->data.forloop_node.inc,s,retArg);
}
retArg->isBreaking=false;
retArg->isContinue=false;
s=scope_end(s);
res->count--;
return res;

View File

@ -36,9 +36,9 @@ char* fs_tlang_config_dir()
{
config_dir = "TLangConfig";
}else{
size_t len=strlen(cfg2) + strlen("/.config") + 1;
size_t len=strlen(cfg2) + strlen("/.config/TLang") + 1;
cfg = (char*)malloc(len * sizeof(char));
snprintf(cfg,len,"%s/.config",cfg2);
snprintf(cfg,len,"%s/.config/TLang",cfg2);
config_dir = cfg;
}
}

View File

@ -116,6 +116,7 @@ void __node_setarray_execute(node_t* n,tobject_t* variable_obj,scope_t* s)
{
node_t* parent=n->data.array_node.parent;
retarg_t arg;
arg.isContinue=false;
arg.isBreaking=false;
arg.isReturning=false;
tobject_t* parent_obj = parent->execute(parent,s,&arg);

View File

@ -167,6 +167,7 @@ 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;
retarg_t arg;
arg.isContinue=false;
arg.isBreaking=false;
arg.isReturning=false;
tobject_t* parent_obj = parent->execute(parent,s,&arg);

182
libtlang/src/json.c Normal file
View File

@ -0,0 +1,182 @@
#include <tlang.h>
#if defined(USE_JANSSON)
#include <jansson.h>
tobject_t* json_totobject(runtime_t* rt,json_t* token);
tobject_t* json_array_totobject(runtime_t* rt,json_t* token)
{
tobject_t* obj = tobject_create_array(rt,0);
size_t i;
for(i=0;i<json_array_size(token);i++)
{
list_add(&obj->data.list,json_totobject(rt,json_array_get(token,i)));
}
return obj;
}
tobject_t* json_string_totobject(runtime_t* rt,json_t* token)
{
string_t s;
s.length = json_string_length(token);
s.capacity = s.length;
s.text = json_string_value(token);
return tobject_string(rt,string_dups(&s));
}
tobject_t* json_object_totobject(runtime_t* rt,json_t* token)
{
tobject_t* o = tobject_create(rt);
o->type=tdict;
o->data.dict = dict_create();
const char* key;
json_t* value;
json_object_foreach(token,key,value) {
kvp_t kvp;
kvp.key = key;
kvp.value = json_totobject(rt,value);
dict_setkvp(o->data.dict,kvp);
}
return o;
}
tobject_t* json_totobject(runtime_t* rt,json_t* token)
{
if(json_is_false(token))
{
return tobject_bool(rt,false);
}
if(json_is_true(token))
{
return tobject_bool(rt,true);
}
if(json_is_number(token))
{
return tobject_number(rt,json_number_value(token));
}
if(json_is_null(token))
{
return tobject_basic(rt,tnull);
}
if(json_is_array(token))
{
return json_array_totobject(rt,token);
}
if(json_is_string(token))
{
return json_string_totobject(rt,token);
}
if(json_is_object(token))
{
return json_object_totobject(rt,token);
}
return tobject_basic(rt,tnull);
}
tobject_t* json_decode(runtime_t* rt,string_t* s)
{
char* data=string_dupp(s);
json_t* token = json_loads(data,0,NULL);
tobject_t* data2 = json_totobject(rt,token);
json_decref(token);
return data2;
}
json_t* json_fromtobject(tobject_t* obj);
json_t* json_fromdict(tobject_t* obj)
{
json_t* obj2=json_object();
int i;
for(i=0;i<obj->data.dict->length;i++)
{
json_t* jsonObj=json_fromtobject(obj->data.dict->items[i].value);
if(jsonObj != NULL)
{
json_object_set_new(obj2,obj->data.dict->items[i].key,jsonObj);
}
}
return obj;
}
json_t* json_fromlist(tobject_t* obj)
{
json_t* obj2 = json_array();
int i;
for(i=0;i<obj->data.list.length;i++)
{
json_t* jsonObj=json_fromtobject(obj->data.list.items[i]);
if(jsonObj != NULL)
{
json_array_append_new(obj2,jsonObj);
}
}
return obj2;
}
json_t* json_fromtobject(tobject_t* obj)
{
if(obj->type == tnull)
{
return json_null();
}else
if(obj->type == tnumber)
{
return json_real(obj->data.number);
}else if(obj->type == tbool)
{
return json_boolean(obj->data.boolean);
}
else if(obj->type == tchar)
{
return json_integer(obj->data.chr);
}
else if(obj->type == tstring)
{
return json_stringn(obj->data.string->text,(size_t)obj->data.string->length);
}
else if(obj->type == tdict)
{
return json_fromdict(obj);
}
else if(obj->type == tlist)
{
return json_fromlist(obj);
}
return NULL;
}
string_t* json_encode(tobject_t* obj,bool indent)
{
string_t* s = string_create();
json_t* j= json_fromtobject(obj);
if(j != NULL){
char* _tmp= json_dumps(j,JSON_INDENT(indent ? 4 : 0));
string_appendp(s,_tmp);
json_decref(j);
}
return s;
}
tobject_t* __json_encode_external_method(runtime_t* rt,void* ptr,list_tobject_t* args)
{
if(args->length > 0)
{
bool indent = false;
if(args->length > 1 && args->items[1]->type == tbool)
{
indent = args->items[1]->data.boolean;
}
return tobject_string(rt,json_encode(args->items[0],indent));
}
return tobject_basic(rt,tnull);
}
tobject_t* __json_decode_external_method(runtime_t* rt,void* ptr,list_tobject_t* args)
{
if(args->length > 0 && args->items[0]->type == tstring)
{
return json_decode(rt,args->items[0]->data.string);
}
return tobject_basic(rt,tundef);
}
void runtime_register_json(runtime_t* rt)
{
tobject_t* json = tobject_create(rt);
json->type = tdict;
json->data.dict = dict_create();
runtime_create_method_on_object(json,"encode",rt,NULL,__json_encode_external_method,NULL);
runtime_create_method_on_object(json,"decode",rt,NULL,__json_decode_external_method,NULL);
rt->globals->setvariable(rt->globals,"json",json);
}
#endif

View File

@ -24,9 +24,9 @@ int mbedsend_tstream(void* ptr,const unsigned char* data,size_t len)
int mbedrecv_tstream(void* ptr,unsigned char* data,size_t len)
{
stream_t* strm = (stream_t*)ptr;
return (int)stream_read(strm,data,len);
int64_t readcnt=stream_read(strm,data,len);
return (int)readcnt;
}
typedef struct {
stream_t* baseStrm;
bool ignoreSSL;
@ -53,9 +53,10 @@ runtime_t* security_sslstream_asclient(runtime_t* rt,void* ptr,list_tobject_t* a
if(args->length > 0 && args->items[0]->type == tstring)
{
mbedtls_tlang_handle_t* handle = (mbedtls_tlang_handle_t*)ptr;
mbedtls_debug_set_threshold(4);
mbedtls_ssl_init(&handle->ssl);
mbedtls_ssl_config_init(&handle->conf);
mbedtls_ssl_config_defaults(&handle->conf,MBEDTLS_SSL_IS_CLIENT,MBEDTLS_SSL_TRANSPORT_STREAM,MBEDTLS_SSL_PRESET_DEFAULT);
mbedtls_x509_crt_init(&handle->cacert);
mbedtls_ctr_drbg_init(&handle->ctr_drbg);
mbedtls_entropy_init(&handle->entropy);
@ -85,7 +86,7 @@ runtime_t* security_sslstream_asclient(runtime_t* rt,void* ptr,list_tobject_t* a
{
string_t* text = string_create();
char* name = string_dupp(fs);
FILE* f = fopen(fs,"r");
FILE* f = fopen(name,"r");
free(name);
string_read(text,f,fread);
fclose(f);

View File

@ -23,6 +23,7 @@ tobject_t* __node_method_call_execute(node_t* n,scope_t* s,retarg_t* retArg)
{
node_t* a =n->data.function_call_node.args[i];
retarg_t _arg;
_arg.isContinue=false;
_arg.isBreaking=false;
_arg.isReturning=false;
list_add(&args,a->execute(a,s,&_arg));
@ -91,7 +92,71 @@ tobject_t* __node_method_call_execute(node_t* n,scope_t* s,retarg_t* retArg)
string_free(s2);
o=tobject_string(s->rt,s3);
}
}
}else if(strcmp(name,"substring") == 0)
{
if(argcount>=1)
{
tobject_t* a0=args[0]->execute(args[0],s,retArg);
int i = 0;
int length = parent_obj->data.string->length;
if(a0->type == tnumber)
{
i = (int)a0->data.number;
if(i<0) i = 0;
if(i>length) i = length-1;
length = length - i;
}
if(argcount >= 2)
{
tobject_t* a1=args[1]->execute(args[1],s,retArg);
if(a1->type == tnumber)
{
int len = (int)a1->data.number;
if(len < length) length = len;
}
}
string_t* s3=string_substring(parent_obj->data.string,i,length);
o=tobject_string(s->rt,s3);
}
}else if(strcmp(name,"remove") == 0)
{
if(argcount>=1)
{
tobject_t* a0=args[0]->execute(args[0],s,retArg);
int i = 0;
int length = parent_obj->data.string->length;
if(a0->type == tnumber)
{
i = (int)a0->data.number;
if(i<0) i = 0;
if(i>length) i = length-1;
length = length - i;
}
if(argcount >= 2)
{
tobject_t* a1=args[1]->execute(args[1],s,retArg);
if(a1->type == tnumber)
{
int len = (int)a1->data.number;
if(len < length) length = len;
}
}
string_t* s3=string_remove(parent_obj->data.string,i,length);
o=tobject_string(s->rt,s3);
}
}else
if(strcmp(name,"split") == 0)
{
if(argcount >= 1)

View File

@ -1,6 +1,7 @@
#include "tlang.h"
extern void __node_single_free(node_t* n);
tobject_t* __neg_node_exec(node_t* n,scope_t* sc,retarg_t* retArg)
{
bool freeleftifzero=true;

View File

@ -8,18 +8,19 @@
#else
#define USE_NETWORK_H
#include <network.h>
#define read net_read
#define write net_write
#define connect net_connect
#define listen net_listen
#define bind net_bind
#define close net_close
#define accept net_accept
#define socket net_socket
char localip[16] = {0};
char gateway[16] = {0};
char netmask[16] = {0};
int getsockname(int sockfd, struct sockaddr *restrict addr,
socklen_t *restrict addrlen)
{
if(addrlen != sizeof(struct sockaddr_in)) {return -1; errno = EINVAL;}
struct sockaddr_in* myAddr = (struct sockaddr_in*)addr;
myAddr->sin_addr.s_addr = (in_addr_t)net_gethostip();
return 0;
}
#endif
#else
#define USE_NORMAL_SOCKETS
@ -33,12 +34,26 @@
#include <netdb.h>
#endif
#ifndef GEKKO
#define net_read read
#define net_write write
#define net_connect connect
#define net_listen listen
#define net_bind bind
#define net_close close
#define net_accept accept
#define net_socket socket
#define net_shutdown shutdown
#define net_gethostbyname gethostbyname
#endif
bool networking_init()
{
#if defined(HW_RVL) && defined(USE_WIISOCKETS_ON_WII)
return wiisocket_init() >= 0;
#elif defined(USE_NETWORK_H)
return if_config ( localip, netmask, gateway, TRUE, 20) >= 0;
return if_config ( localip, netmask, gateway, true, 20) >= 0;
#else
#endif
@ -48,16 +63,26 @@
{
int fd;
} myfd_t;
void __net_tcpserver_free(tobject_t* obj)
{
free(obj->ptr_data);
}
typedef struct
{
int fd;
struct sockaddr_in local;
struct sockaddr_in remote;
} myserveracceptfd_t;
int64_t __fd_read(stream_t* strm,void* buff,int64_t len)
static int64_t __fd_read(stream_t* strm,void* buff,int64_t len)
{
myfd_t* j = (myfd_t*)strm->ptr;
return (ssize_t) read(j->fd,buff,len);
return (ssize_t) net_read(j->fd,buff,(size_t)len);
}
void __fd_write(stream_t* strm,void* buff,int64_t len)
{
myfd_t* j = (myfd_t*)strm->ptr;
if(write(j->fd,buff,len) == -1)
if(net_write(j->fd,buff,(size_t)len) == -1)
{
printf("FAILED TO SEND\n");
}
@ -65,7 +90,7 @@
void __fd_close(stream_t* strm)
{
myfd_t* j = (myfd_t*)strm->ptr;
close(j->fd);
net_close(j->fd);
free(j);
}
void __fd_flush(stream_t* strm)
@ -81,19 +106,18 @@
return stream_create(_fd,__fd_read,__fd_write,NULL,NULL,NULL,stream_true,stream_true,stream_false,__fd_close,__fd_flush);
}
stream_t* create_tcp_client(string_t* ipOrFqdn,int port)
int create_tcp_server(string_t* ipOrFqdn,int port)
{
int fd= socket(AF_INET,SOCK_STREAM,0);
int fd= net_socket(AF_INET,SOCK_STREAM,0);
if(fd == -1)
{return NULL;}
{return -1;}
struct sockaddr_in addr;
memset(&addr,0,sizeof(struct sockaddr_in));
char* ipAddress = string_dupp(ipOrFqdn);
if (inet_aton(ipAddress,&addr.sin_addr) <=0)
{
Global_Mutex_Lock();
struct hostent* host=gethostbyname(ipAddress);
struct hostent* host=net_gethostbyname(ipAddress);
if(host != NULL)
{
memcpy(&addr.sin_addr,host->h_addr_list[0],sizeof(struct sockaddr_in));
@ -103,7 +127,32 @@
free(ipAddress);
addr.sin_port = htons((uint16_t)port);
addr.sin_family = AF_INET;
int res = connect(fd,&addr,sizeof(struct sockaddr_in));
net_bind(fd,(struct sockaddr*)&addr,(socklen_t)sizeof(addr));
return fd;
}
stream_t* create_tcp_client(string_t* ipOrFqdn,int port)
{
int fd= net_socket(AF_INET,SOCK_STREAM,0);
if(fd == -1)
{return NULL;}
struct sockaddr_in addr;
memset(&addr,0,sizeof(struct sockaddr_in));
char* ipAddress = string_dupp(ipOrFqdn);
if (inet_aton(ipAddress,&addr.sin_addr) <=0)
{
Global_Mutex_Lock();
struct hostent* host=net_gethostbyname(ipAddress);
if(host != NULL)
{
memcpy(&addr.sin_addr,host->h_addr_list[0],sizeof(struct sockaddr_in));
}
Global_Mutex_Unlock();
}
free(ipAddress);
addr.sin_port = htons((uint16_t)port);
addr.sin_family = AF_INET;
int res = net_connect(fd,&addr,sizeof(struct sockaddr_in));
if(res != 0)
{
printf("ERROR AT NET: %s\n",strerror(errno));
@ -125,7 +174,141 @@
}
return tobject_basic(rt,tnull);
}
tobject_t* __net_tcpserver_start(runtime_t* rt,void* ptr,list_tobject_t* args)
{
myfd_t* fd=(myfd_t*)ptr;
int backLog = 128;
if(args->length > 0 && args->items[0]->type == tnumber)
{
backLog=(int)args->items[0]->data.number;
}
net_listen(fd->fd,backLog);
return tobject_basic(rt,tnull);
}
tobject_t* __net_tcpserver_stop(runtime_t* rt,void* ptr,list_tobject_t* args)
{
myfd_t* fd=(myfd_t*)ptr;
//net_shutdown(fd->fd,SHUT_RDWR);
return tobject_basic(rt,tnull);
}
tobject_t* __net_tcpserver_getclient_getstream(runtime_t* rt,void* ptr,list_tobject_t* args)
{
myserveracceptfd_t* acceptFd = (myserveracceptfd_t*)ptr;
return tobject_fromstream(rt,create_netfd_strm(acceptFd->fd));
}
tobject_t* __net_tcpserver_getendpoint_getip(runtime_t* rt,void* ptr,list_tobject_t* args)
{
struct sockaddr_in* addr = (struct sockaddr_in*)ptr;
string_t* str = string_create();
uint8_t* bytes=(uint8_t*)&addr->sin_addr.s_addr;
string_appendn(str,bytes[0]);
string_appendc(str,'.');
string_appendn(str,bytes[1]);
string_appendc(str,'.');
string_appendn(str,bytes[2]);
string_appendc(str,'.');
string_appendn(str,bytes[3]);
return tobject_string(rt,str);
}
tobject_t* __net_tcpserver_getendpoint_getport(runtime_t* rt,void* ptr,list_tobject_t* args)
{
struct sockaddr_in* addr = (struct sockaddr_in*)ptr;
return tobject_number(rt,addr->sin_port);
}
tobject_t* __net_tcpserver_getendpoint(runtime_t* rt,void* ptr,list_tobject_t* args)
{
tobject_t* __ctx = tobject_create(rt);
__ctx->type = tdict;
__ctx->data.dict = dict_create();
runtime_create_method_on_object(__ctx,"getip",rt,ptr,__net_tcpserver_getendpoint_getip,NULL);
runtime_create_method_on_object(__ctx,"getport",rt,ptr,__net_tcpserver_getendpoint_getport,NULL);
return __ctx;
}
tobject_t* __net_tcpserver_getclient(runtime_t* rt,void* ptr,list_tobject_t* args)
{
myfd_t* fd=(myfd_t*)ptr;
myserveracceptfd_t* acceptFd = (myserveracceptfd_t*)malloc(sizeof(myserveracceptfd_t));
socklen_t len=(socklen_t)sizeof(acceptFd->remote);
acceptFd->fd = net_accept(fd->fd,&acceptFd->remote,&len);
if(acceptFd->fd == -1)
{
free(acceptFd);
return tobject_basic(rt,tnull);
}
len = (socklen_t)sizeof(acceptFd->local);
getsockname(acceptFd->fd,&acceptFd->remote,&len);
tobject_t* __client_ctx = tobject_create(rt);
__client_ctx->type =tdict;
__client_ctx->data.dict = dict_create();
__client_ctx->free = __net_tcpserver_free;
__client_ctx->ptr_data = acceptFd;
runtime_create_method_on_object(__client_ctx,"getstream",rt,acceptFd,__net_tcpserver_getclient_getstream,NULL);
runtime_create_method_on_object(__client_ctx,"getlocal",rt,&acceptFd->local,__net_tcpserver_getendpoint,NULL);
runtime_create_method_on_object(__client_ctx,"getremote",rt,&acceptFd->remote,__net_tcpserver_getendpoint,NULL);
return __client_ctx;
}
tobject_t* __net_tcpserver(runtime_t* rt,void* ptr,list_tobject_t* args)
{
if(args->length >1 && args->items[0]->type == tstring && args->items[1]->type == tnumber)
{
int _fd = create_tcp_server(args->items[0]->data.string,(int)args->items[1]->data.number);
printf("FD: %i\n",_fd);
if(_fd == -1)
return tobject_basic(rt,tnull);
myfd_t* fd = (myfd_t*)malloc(sizeof(myfd_t));
fd->fd = _fd;
tobject_t* server_res = tobject_create(rt);
server_res->type=tdict;
server_res->data.dict = dict_create();
server_res->ptr_data = fd;
server_res->free = __net_tcpserver_free;
runtime_create_method_on_object(server_res,"start",rt,fd,__net_tcpserver_start,NULL);
runtime_create_method_on_object(server_res,"stop",rt,fd,__net_tcpserver_stop,NULL);
runtime_create_method_on_object(server_res,"getclient",rt,fd,__net_tcpserver_getclient,NULL);
return server_res;
}
return tobject_basic(rt,tnull);
}
#if defined(USE_CURL)
extern void runtime_register_curl(runtime_t* rt,dict_t* net);
#endif
tobject_t* __net_gethostbyname(runtime_t* rt,void* ptr,list_tobject_t* args)
{
if(args->length > 0 && args->items[0]->type == tstring)
{
struct sockaddr_in addr;
memset(&addr,0,sizeof(struct sockaddr_in));
char* ipAddress = string_dupp(args->items[0]->data.string);
Global_Mutex_Lock();
struct hostent* host=net_gethostbyname(ipAddress);
if(host != NULL)
{
memcpy(&addr.sin_addr,host->h_addr_list[0],sizeof(struct sockaddr_in));
}
Global_Mutex_Unlock();
free(ipAddress);
addr.sin_port = 0;
addr.sin_family = AF_INET;
string_t* str = string_create();
uint8_t* bytes=(uint8_t*)&addr.sin_addr.s_addr;
string_appendn(str,bytes[0]);
string_appendc(str,'.');
string_appendn(str,bytes[1]);
string_appendc(str,'.');
string_appendn(str,bytes[2]);
string_appendc(str,'.');
string_appendn(str,bytes[3]);
return tobject_string(rt,str);
}
return tobject_basic(rt,tnull);
}
void runtime_register_network(runtime_t* rt)
{
networking_init();
@ -133,8 +316,14 @@
__network->type = tdict;
__network->data.dict = dict_create();
runtime_create_method_on_dict(__network->data.dict,"tcpclient",rt,NULL,__net_tcpclient,NULL);
runtime_create_method_on_dict(__network->data.dict,"tcpserver",rt,NULL,__net_tcpserver,NULL);
runtime_create_method_on_dict(__network->data.dict,"gethostbyname",rt,NULL,__net_gethostbyname,NULL);
#if defined(USE_CURL)
runtime_register_curl(rt,__network->data.dict);
#endif
rt->globals->setvariable(rt->globals,"net",__network);
}
#endif

View File

@ -15,7 +15,7 @@ void liststr_create(list_string_t* l)
void liststr_add(list_string_t* l,string_t* s)
{
if(l->count + 1 > l->capacity)
if(l->count + 2 > l->capacity)
{
l->capacity = l->count + 128;
l->strings=(tobject_t**)realloc(l->strings,l->capacity * sizeof(string_t*));
@ -38,7 +38,7 @@ node_t* parse_sum(parser_t* p);
node_t* parse_factor(parser_t* p);
node_t* parse_value(parser_t* p);
node_t* __parser_parse(parser_t* p,bool isFirst)
node_t* __parser_parse2(parser_t* p,bool isFirst,bool inCase)
{
if(p->i>= p->ls->count) return node_const_number_create(0);
if(lextoken_isitem(p->ls->tokens[p->i],"{") || isFirst)
@ -48,6 +48,11 @@ node_t* __parser_parse(parser_t* p,bool isFirst)
node_t* scope = node_scope_create(isFirst);
while(p->i<p->ls->count && !lextoken_isitem(p->ls->tokens[p->i],"}"))
{
if(inCase && (lextoken_isitem(p->ls->tokens[p->i],"case") || lextoken_isitem(p->ls->tokens[p->i],"default")))
{
break;
}
node_scope_append(scope,parse_expression(p));
//scopeNode.Body.Add(ParseExpression(tokens,ref i));
if(p->i<p->ls->count && lextoken_isitem(p->ls->tokens[p->i],";"))
@ -56,12 +61,16 @@ node_t* __parser_parse(parser_t* p,bool isFirst)
}
}
if(!inCase)
p->i++;
return scope;
}
return parse_assignable(p);
}
node_t* __parser_parse(parser_t* p,bool isFirst)
{
return __parser_parse2(p,isFirst,false);
}
node_t* parse_assignable(parser_t* p)
{
node_t* myExpression = parse_lor(p);
@ -378,12 +387,36 @@ node_t* parse_value(parser_t* p)
}
if(string_samep(token.text,"brk") || string_samep(token.text,"break"))
{
return node_break_create();
return node_break_create(true);
}
if(string_samep(token.text,"cont") || string_samep(token.text,"continue"))
{
return node_break_create(false);
}
if(string_samep(token.text,"ret") || string_samep(token.text,"return"))
{
return node_return_create(parse_expression(p));
}
if(string_samep(token.text,"case"))
{
//case <expression> : body
node_t* exp= parse_expression(p);
if(p->i < p->ls->count && string_samep(p->ls->tokens[p->i].text,":"))
{
p->i++;
}
return node_case_create(exp,__parser_parse2(p,true,true));
}
if(string_samep(token.text,"default"))
{
//default: body
if(p->i < p->ls->count && string_samep(p->ls->tokens[p->i].text,":"))
{
p->i++;
}
return node_default_create(__parser_parse2(p,true,true));
}
if(string_samep(token.text,"-"))
{
@ -504,6 +537,28 @@ node_t* parse_value(parser_t* p)
return node_eachloop_create(name,ls,parse_expression(p));
}
}
else if(string_samep(token.text,"switch"))
{
p->i++;
node_t* condition = parse_expression(p);
p->i++;
node_t* body=NULL;
if(p->i<p->ls->count && !string_samep(p->ls->tokens[p->i].text,";"))
{
body = parse_expression(p);
}else{
body = node_const_undef_create();
}
if(body->type == scopenode)
{
body->data.scope_node.switch_arg = condition;
}else{
node_free(condition);
}
return body;
}
else if(string_samep(token.text,"while") || string_samep(token.text,"do"))
{
p->i++;
@ -797,7 +852,23 @@ node_t* parse_expression(parser_t* p)
{
return __parser_parse(p,false);
}
tobject_t* runtime_eval(runtime_t* rt,string_t* str,scope_t* sc)
{
if(sc == NULL) sc=rt->globals;
lextokenlist_t* list=lexer_lex(str);
parser_t p;
p.rt = rt;
p.ls = list;
p.i = 0;
node_t* program=__parser_parse(&p,true);
retarg_t retArg;
retArg.isBreaking=false;
retArg.isReturning=false;
retArg.isContinue=false;
tobject_t* obj=program->execute(program,sc,&retArg);
node_free(program);
return obj;
}
void runtime_load(runtime_t* rt,string_t* str)
{

View File

@ -282,6 +282,7 @@ tobject_t* __execute_code_rt(runtime_t* rt,void* ptr,list_tobject_t* args)
tobject_t* runtime_exec(runtime_t* rt)
{
retarg_t _arg;
_arg.isContinue=false;
_arg.isBreaking=false;
_arg.isReturning=false;
tobject_t* o = rt->program->execute(rt->program,rt->globals,&_arg);
@ -722,7 +723,7 @@ tobject_t* __console_writetypeln_external_method(runtime_t* rt,void* ptr,list_to
printf("number\n");
break;
case tlist:
printf("list\n");
printf("array\n");
break;
case tdict:
printf("dict\n");
@ -842,7 +843,138 @@ void runtime_register(runtime_t* rt,runtime_reg_t reg)
reg(rt);
}
extern void runtime_register_rand(runtime_t* rt);
typedef struct
{
tobject_t* ls;
char* key;
} reflection_get_dict_entries_entry_t;
tobject_t* __reflection_get_dict_entry(runtime_t* rt,void* ptr,list_tobject_t* args)
{
if(args->length >= 2 && args->items[0]->type == tdict && args->items[1]->type == tstring)
{
char* kvp = string_dupp(args->items[1]->data.string);
if(dict_haskey(args->items[0]->data.dict,kvp))
{
tobject_t* v=dict_getkvp(args->items[0]->data.dict,kvp)->value;
free(kvp);
return v;
}
free(kvp);
return tobject_basic(rt,tundef);
}
return tobject_basic(rt,tnull);
}
tobject_t* __reflection_set_dict_entry(runtime_t* rt,void* ptr,list_tobject_t* args)
{
if(args->length >= 3 && args->items[0]->type == tdict && args->items[1]->type == tstring)
{
char* _kvp = string_dupp(args->items[1]->data.string);
kvp_t kvp;
kvp.key=_kvp;
kvp.value = args->items[2];
dict_setkvp(args->items[0]->data.dict,kvp);
free(_kvp);
}
return tobject_basic(rt,tnull);
}
tobject_t* __reflection_get_dict_entries_getname(runtime_t* rt,void* ptr,list_tobject_t* args)
{
if(ptr != NULL)
{
reflection_get_dict_entries_entry_t* da = (reflection_get_dict_entries_entry_t*)ptr;
return tobject_stringp(rt,da->key);
}
return tobject_stringp(rt,"");
}
tobject_t* __reflection_get_dict_entries_getvalue(runtime_t* rt,void* ptr,list_tobject_t* args)
{
if(ptr != NULL)
{
reflection_get_dict_entries_entry_t* da = (reflection_get_dict_entries_entry_t*)ptr;
if(dict_haskey(da->ls->data.dict,da->key))
{
return dict_getkvp(da->ls->data.dict,da->key)->value;
}
}
return tobject_basic(rt,tundef);
}
tobject_t* __reflection_get_dict_entries_setvalue(runtime_t* rt,void* ptr,list_tobject_t* args)
{
if(ptr != NULL && args->length > 0)
{
reflection_get_dict_entries_entry_t* da = (reflection_get_dict_entries_entry_t*)ptr;
kvp_t kvp;
kvp.key = da->key;
kvp.value = args->items[0];
dict_setkvp(da->ls->data.dict,kvp);
}
return tobject_basic(rt,tnull);
}
void __reflection_get_dictentries_drop_ptr_data(tobject_t* d)
{
tobject_rmref((tobject_t*)d->ptr_data);
}
void __reflection_get_dict_entries_entry_free(tobject_t* d)
{
free(d->ptr_data);
}
tobject_t* __reflection_get_dict_entries(runtime_t* rt,void* ptr,list_tobject_t* args)
{
tobject_t* ls = tobject_create_array(rt,0);
ls->free=NULL;
ls->ptr_data=NULL;
if(args->length >= 1 && args->items[0]->type == tdict)
{
tobject_t* data = args->items[0];
ls->ptr_data = data;
ls->free = __reflection_get_dictentries_drop_ptr_data;
tobject_addref(data);
int i;
for(i = 0;i<data->data.dict->length;i++)
{
tobject_t* kvp = tobject_create(rt);
kvp->type = tdict;
kvp->data.dict = dict_create();
reflection_get_dict_entries_entry_t* __entry_data = (reflection_get_dict_entries_entry_t*)malloc(sizeof(reflection_get_dict_entries_entry_t));
__entry_data->key = data->data.dict->items[i].key;
__entry_data->ls = data;
kvp->ptr_data = __entry_data;
runtime_create_method_on_object(kvp,"getname",rt,__entry_data,__reflection_get_dict_entries_getname,NULL);
runtime_create_method_on_object(kvp,"setvalue",rt,__entry_data,__reflection_get_dict_entries_setvalue,NULL);
runtime_create_method_on_object(kvp,"getvalue",rt,__entry_data,__reflection_get_dict_entries_getvalue,NULL);
kvp->free = __reflection_get_dict_entries_entry_free;
list_add(&ls->data.list,kvp);
}
}
return ls;
}
tobject_t* __reflection_arg_count(runtime_t* rt,void* ptr,list_tobject_t* args)
{
if(args->length >= 1 && args->items[0]->type == tinternalmethod)
{
return tobject_number(rt,(double)args->items[0]->data.internal_method.closure->data.closure_node.argNames.count);
}
if(args->length >= 1 && args->items[0]->type == texternalmethod)
{
return tobject_number(rt,-1);
}
return tobject_basic(rt,tnull);
}
tobject_t* __reflection_call(runtime_t* rt,void* ptr,list_tobject_t* args)
{
if(args->length >= 2 && (args->items[0]->type == tinternalmethod || args->items[0]->type == texternalmethod) && args->items[1]->type == tlist)
{
return tobject_call(rt->globals,args->items[0],&args->items[1]->data.list);
}
return tobject_basic(rt,tnull);
}
#if defined(USE_JANSSON)
extern void runtime_register_json(runtime_t* rt);
#endif
void runtime_register_std(runtime_t* rt)
{
if(rt->hasStd) return;
@ -850,7 +982,7 @@ void runtime_register_std(runtime_t* rt)
tobject_t* __fs = tobject_create(rt);
__fs->type = tdict;
__fs->data.dict= dict_create();
tobject_t* __console = tobject_create(rt);
tobject_t* __console = tobject_create(rt);
__console->type = tdict;
__console->data.dict= dict_create();
tobject_t* __reflection = tobject_create(rt);
@ -859,7 +991,9 @@ void runtime_register_std(runtime_t* rt)
tobject_t* __math = tobject_create(rt);
__math->type = tdict;
__math->data.dict = dict_create();
tobject_t* __rt = tobject_create(rt);
__rt->type = tdict;
__rt->data.dict = dict_create();
runtime_create_method_on_dict(__console->data.dict,"clear",rt,NULL,__console_clear_external_method,NULL);
runtime_create_method_on_dict(__console->data.dict,"readln",rt,NULL,__console_readln_external_method,NULL);
@ -895,6 +1029,12 @@ void runtime_register_std(runtime_t* rt)
runtime_create_method_on_dict(__fs->data.dict,"openreadwrite",rt,NULL,__fs_openreadwrite_external_method,NULL);
runtime_create_method_on_dict(__reflection->data.dict,"parse_code",rt,NULL,__reflection_parse_code,NULL);
runtime_create_method_on_object(__reflection,"get_dict_entries",rt,NULL,__reflection_get_dict_entries,NULL);
runtime_create_method_on_object(__reflection,"get_dict_entry",rt,NULL,__reflection_get_dict_entry,NULL);
runtime_create_method_on_object(__reflection,"set_dict_entry",rt,NULL,__reflection_set_dict_entry,NULL);
runtime_create_method_on_object(__reflection,"arg_count",rt,NULL,__reflection_arg_count,NULL);
runtime_create_method_on_object(__reflection,"call",rt,NULL,__reflection_call,NULL);
runtime_create_method_on_dict(__math->data.dict,"sin",rt,NULL,__math_sin,NULL);
runtime_create_method_on_dict(__math->data.dict,"cos",rt,NULL,__math_cos,NULL);
runtime_create_method_on_dict(__math->data.dict,"asin",rt,NULL,__math_asin,NULL);
@ -911,6 +1051,17 @@ void runtime_register_std(runtime_t* rt)
runtime_create_method_on_dict(__math->data.dict,"ceiling",rt,NULL,__math_ceiling,NULL);
runtime_create_method_on_dict(__math->data.dict,"sqrt",rt,NULL,__math_sqrt,NULL);
runtime_create_method_on_dict(__math->data.dict,"pow",rt,NULL,__math_pow,NULL);
tobject_t* __version = tobject_create(rt);
__version->type = tdict;
__version->data.dict = dict_create();
runtime_create_number_on_object(__version,"major",rt,TLANG_MAJOR);
runtime_create_number_on_object(__version,"minor",rt,TLANG_MINOR);
runtime_create_number_on_object(__version,"patch",rt,TLANG_PATCH);
runtime_create_number_on_object(__version,"build",rt,TLANG_BUILD);
kvp_t kvp2;
kvp2.key="version";
kvp2.value = __version;
dict_setkvp(__rt->data.dict,kvp2);
rt->globals->setvariable(rt->globals,"fs",__fs);
@ -918,6 +1069,7 @@ void runtime_register_std(runtime_t* rt)
rt->globals->setvariable(rt->globals,"math",__math);
rt->globals->setvariable(rt->globals,"reflection",__reflection);
rt->globals->setvariable(rt->globals,"rt",__rt);
#if defined(USE_SDL2)
runtime_register_sdl(rt);
@ -929,8 +1081,61 @@ void runtime_register_std(runtime_t* rt)
#if defined(USE_MBED)
runtime_register_mbed(rt);
#endif
#if defined(USE_JANSSON)
runtime_register_json(rt);
#endif
runtime_register_rand(rt);
}
tobject_t* __typeof_external_method(runtime_t* rt,void* ptr,list_tobject_t* args)
{
if(args->length >= 1)
{
switch(args->items[0]->type)
{
case tundef:
return tobject_stringp(rt,"undefined");
case tnull:
return tobject_stringp(rt,"null");
case tnumber:
return tobject_stringp(rt,"number");
case tlist:
return tobject_stringp(rt,"array");
case tdict:
return tobject_stringp(rt,"dict");
case tstring:
return tobject_stringp(rt,"string");
case tinternalmethod:
return tobject_stringp(rt,"internal_method");
case texternalmethod:
return tobject_stringp(rt,"external_method");
case tchar:
return tobject_stringp(rt,"char");
case tbool:
return tobject_stringp(rt,"bool");
default:
{
string_t* __s = string_create();
string_appendp(__s,"Unknown: ");
string_appendn(__s,(int)args->items[0]->type);
return tobject_string(rt,__s);
}
}
}
return tobject_stringp(rt,"missing_argument");
}
#if defined(USE_THREADS)
extern void runtime_register_threads(runtime_t* rt,dict_t* create);
#endif
runtime_t* runtime_init()
{
@ -951,7 +1156,8 @@ runtime_t* runtime_init()
runtime_register_threads(rt,__create->data.dict);
#endif
rt->globals->setvariable(rt->globals,"create",__create);
tobject_t* typeof_func = tobject_fromexternalmethod(rt,NULL,__typeof_external_method,NULL);
rt->globals->setvariable(rt->globals,"typeof",typeof_func);
return rt;
}
void runtime_add_argument(runtime_t* rt,tobject_t* arg)

View File

@ -1,10 +1,21 @@
#include "tlang.h"
tobject_t* __node_scope_execute(node_t* n,scope_t* s,retarg_t* retArg)
{
tobject_t* switch_data = NULL;
int state=1;
if(n->data.scope_node.isSub)
{
s=scope_begin(s);
}
if(n->data.scope_node.switch_arg != NULL)
{
state = 2;
switch_data = n->data.scope_node.switch_arg->execute(n->data.scope_node.switch_arg,s,retArg);
tobject_addref(switch_data);
retArg->isBreaking=false;
}
tobject_t* obj_data = tobject_basic(s->rt,tnull);
tobject_addref(obj_data);
int i;
@ -12,14 +23,52 @@ tobject_t* __node_scope_execute(node_t* n,scope_t* s,retarg_t* retArg)
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,retArg);
tobject_addref(d);
tobject_rmref(obj_data);
obj_data=d;
if(retArg->isBreaking || retArg->isReturning)
{
break;
node_t* no = n->data.scope_node.items[i];
if(no->type == casenode)
{
//check whether it is what we want
if(state != 1 && switch_data != NULL)
{
if(tobject_sameb(s->rt,switch_data,no->data.two_node_node.left->execute(no->data.two_node_node.left,s,retArg)))
{
state = 1;
}else {
state = 0;
}
}
}
if(no->type == defaultnode)
{
state = 1;
}
if(state > 0)
{
tobject_t* d=no->execute(no,s,retArg);
tobject_addref(d);
tobject_rmref(obj_data);
obj_data=d;
if(retArg->isBreaking || retArg->isReturning || retArg->isContinue)
{
if(switch_data != NULL)
{
retArg->isBreaking=false;
}
break;
}
}
}
if(switch_data != NULL)
{
tobject_rmref(switch_data);
}
if(n->data.scope_node.isSub)
{
@ -37,6 +86,10 @@ void __node_scope_free(node_t* node)
{
node_free(items[i]);
}
if(node->data.scope_node.switch_arg)
{
node_free(node->data.scope_node.switch_arg);
}
free(items);
}
@ -47,12 +100,14 @@ node_t* node_scope_create(bool first)
n->data.scope_node.count = 0;
n->data.scope_node.isSub=!first;
n->data.scope_node.capacity=64;
n->data.scope_node.switch_arg = NULL;
n->data.scope_node.items=(node_t**)malloc(sizeof(node_t*) * n->data.scope_node.capacity);
n->execute = __node_scope_execute;
n->free = __node_scope_free;
return n;
}
void node_scope_append(node_t* sc,node_t* n)
{
if(sc->data.scope_node.count + 128 >sc->data.scope_node.capacity)

View File

@ -1,4 +1,4 @@
/*
#include <SDL2/SDL.h>
#include <SDL2/SDL_image.h>
#include <SDL2/SDL_ttf.h>
@ -790,4 +790,3 @@ dict_setkvp(_sdl->data.dict,kvp_WINDOWPOS_CENTERED);
rt->globals->setvariable(rt->globals,"sdl",_sdl);
}
*/

View File

@ -10,6 +10,7 @@ int64_t _file_read(stream_t* strm,void* buff,int64_t len)
void _file_write(stream_t* strm,void* buff,int64_t len)
{
FILE* f = (FILE*)strm->ptr;
if(buff != NULL)
fwrite(buff,1,(size_t)len,f);
}
int64_t _file_getpos(stream_t* strm)
@ -109,28 +110,37 @@ int64_t __tobject_read(stream_t* strm,void* ptr,int64_t len)
}
return 0;
}
void __tobject_write(stream_t* strm,void* ptr,int64_t len)
bool tobject_write_call(tobject_t* f,runtime_t* rt,void* ptr,int64_t len)
{
uint8_t* pt = (uint8_t*)ptr;
__toStrm* _strm= (__toStrm*)strm->ptr;
tobject_t* r=tobject_create_array(_strm->s->rt,0);
if(_strm->o->type == tdict && dict_haskey(_strm->o->data.dict,"write"))
{
tobject_t* f = dict_getkvp(_strm->o->data.dict,"write")->value;
list_tobject_t g;
list_create(_strm->s->rt,&g,0);
uint8_t* pt = (uint8_t*)ptr;
list_tobject_t g;
tobject_t* r=tobject_create_array(rt,0);
list_create(rt,&g,0);
list_add(&g,r);
list_add(&g,tobject_number(_strm->s->rt,len));
list_add(&g,tobject_number(rt,len));
int i;
for(i=0;i<len;i++)
{
list_add(&r->data.list,tobject_number(_strm->s->rt,(double)pt[i]));
list_add(&r->data.list,tobject_number(rt,(double)pt[i]));
}
tobject_t* j=tobject_call(_strm->s,f,&g);
tobject_t* j=tobject_call(rt->globals,f,&g);
bool retVal=true;
if(j->type == tbool && !j->data.boolean) retVal=false;
tobject_freeifzero(j);
list_free(&g);
return retVal;
}
void __tobject_write(stream_t* strm,void* ptr,int64_t len)
{
__toStrm* _strm= (__toStrm*)strm->ptr;
if(_strm->o->type == tdict && dict_haskey(_strm->o->data.dict,"write"))
{
tobject_t* f = dict_getkvp(_strm->o->data.dict,"write")->value;
tobject_write_call(f,_strm->s->rt,ptr,len);
}
}
@ -474,7 +484,7 @@ tobject_t* tobject_fromstream(runtime_t* rt,stream_t* strm)
int64_t stream_read(stream_t* strm,void* buff,int64_t len)
{
if(strm->read != NULL)
strm->read(strm,buff,len);
return strm->read(strm,buff,len);
return 0;
}
void stream_write(stream_t* strm,void* buffer,int64_t len)

31
libtlang/src/switch.c Normal file
View File

@ -0,0 +1,31 @@
#include <tlang.h>
extern void __node_two_free(node_t* n);
extern void __node_single_free(node_t* n);
node_t* __case_node_exec(node_t* n,scope_t* s,retarg_t* retArg)
{
return n->data.two_node_node.right->execute(n->data.two_node_node.right,s,retArg);
}
node_t* __default_node_exec(node_t* n,scope_t* s,retarg_t* retArg)
{
return n->data.single_node_node->execute(n->data.single_node_node,s,retArg);
}
node_t* node_case_create(node_t* left,node_t* right)
{
node_t* node = (node_t*)malloc(sizeof(node_t));
node->type = casenode;
node->data.two_node_node.left = left;
node->data.two_node_node.right = right;
node->execute = __case_node_exec;
node->free = __node_two_free;
return node;
}
node_t* node_default_create(node_t* body)
{
node_t* node = (node_t*)malloc(sizeof(node_t));
node->type = defaultnode;
node->data.single_node_node = body;
node->execute = __default_node_exec;
node->free = __node_single_free;
return node;
}

View File

@ -4,7 +4,7 @@ tobject_t* tobject_same(runtime_t* rt,tobject_t* l,tobject_t* r,bool* freeleftif
{
tobject_t* out=NULL;
tobject_type_t ltype=l->type;
tobject_type_t ltype=l->type;
tobject_type_t rtype =r->type;
if(ltype == tdict)
{
@ -267,7 +267,8 @@ tobject_t* tobject_call(scope_t* scope,tobject_t* func,list_tobject_t* args)
{
//execute_internal_method
tobject_t* res;
scope_t* scope2=scope_begin(scope);
scope_t* scope2=scope_begin(func->data.internal_method.scope);
list_string_t* strs=&func->data.internal_method.closure->data.closure_node.argNames;
int argCountClosure = strs->count;//Body.Arguments.Count;
int argCountCaller = args->length;
@ -280,7 +281,6 @@ tobject_t* tobject_call(scope_t* scope,tobject_t* func,list_tobject_t* args)
{
tobject_t* _obj=args->items[i];
char* t = string_dupp(strs->strings[i]);
scope2->setvariable(scope2,string_trimstart(t,'$'),_obj);
free(t);
}

View File

@ -1,14 +1,11 @@
#include "tlang.h"
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,retarg_t* retArg)
{
bool isRunning=true;
s=scope_begin(s);
s->setvariable(s,"last_ittr",tobject_fromexternalmethod(s->rt,&isRunning,last_ittr_loop,NULL));
bool isRunning =true;
tobject_t* res = tobject_basic(s->rt,tundef);
res->count=1;
if(!n->data.whileloop_node.isDoLoop)
@ -16,9 +13,11 @@ node_t* __while_loop_exec(node_t* n,scope_t* s,retarg_t* retArg)
isRunning = __loop_bool(n->data.whileloop_node.condition,s,retArg);
}
retArg->isBreaking=false;
retArg->isContinue=false;
while(isRunning && !retArg->isBreaking && !retArg->isReturning)
{
tobject_t* _r = __loop_exec2(n->data.whileloop_node.body,s,retArg);
retArg->isContinue=false;
tobject_addref(_r);
tobject_rmref(res);
res=_r;
@ -28,6 +27,7 @@ node_t* __while_loop_exec(node_t* n,scope_t* s,retarg_t* retArg)
}
}
retArg->isBreaking=false;
retArg->isContinue=false;
s=scope_end(s);
res->count--;
return res;

3
tlang/CMakeLists.txt Normal file
View File

@ -0,0 +1,3 @@
project(tlang)
add_executable(tlang src/main.c)
target_link_libraries(tlang tlanginterp)

View File

@ -1,22 +0,0 @@
CC :=gcc
AR := ar
CFLAGS := -Wall -g
target := tlang
tlang_c_source_files := $(shell find src/ -name *.c)
tlang_c_object_files := $(patsubst src/%.c, build/%.o, $(tlang_c_source_files))
$(target): $(tlang_c_object_files)
$(CC) -o $(target) $(CFLAGS) -L../libtlang $(tlang_c_object_files) -ltlang -lpthread -lSDL2 -lSDL2_gfx -lSDL2_image -lSDL2_mixer -lSDL2_ttf -lmbedtls -lmbedx509 -lmbedcrypto -lpng -ljpeg -lm
$(tlang_c_object_files): build/%.o : src/%.c
mkdir -p $(dir $@) && \
$(CC) $(CFLAGS) -c -I../libtlang/include $(patsubst build/%.o, src/%.c, $@) -o $@
.PHONY: clean
.PHONY: install
install: $(target)
rm /usr/local/bin/tlang > /dev/null; true
cp $(target) /usr/local/bin/tlang
clean:
rm -rf build; rm $(target)

View File

@ -1,16 +1,66 @@
#include "tlang.h"
#include <tlang.h>
#include <stdio.h>
void register_funcs(runtime_t* rt)
{
}
#if defined(GEKKO)
#include <fat.h>
#include <stdio.h>
#include <stdlib.h>
#include <gccore.h>
void init_ogc(runtime_t* rt)
{
#if defined(HW_RVL)
__reg_wpad(rt);
#endif
VIDEO_Init();
fatInitDefault();
// Obtain the preferred video mode from the system
// This will correspond to the settings in the Wii menu
rmode = VIDEO_GetPreferredMode(NULL);
// Allocate memory for the display in the uncached region
xfb = MEM_K0_TO_K1(SYS_AllocateFramebuffer(rmode));
// Initialise the console, required for printf
console_init(xfb,20,20,rmode->fbWidth,rmode->xfbHeight,rmode->fbWidth*VI_DISPLAY_PIX_SZ);
// Set up the video registers with the chosen mode
VIDEO_Configure(rmode);
// Tell the video hardware where our display memory is
VIDEO_SetNextFramebuffer(xfb);
// Make the display visible
VIDEO_SetBlack(FALSE);
// Flush the video register changes to the hardware
VIDEO_Flush();
// Wait for Video setup to complete
VIDEO_WaitVSync();
if(rmode->viTVMode&VI_NON_INTERLACE) VIDEO_WaitVSync();
// The console understands VT terminal escape codes
// This positions the cursor on row 2, column 0
// we can use variables for this with format codes too
// e.g. printf ("\x1b[%d;%dH", row, column );
printf("\x1b[2;0H");
}
#endif
int main(int argc,char** argv)
{
runtime_t* rt = runtime_init();
runtime_register_std(rt);
runtime_register(rt,register_funcs);
#if defined(GEKKO)
init_ogc(rt);
#endif
if(argc < 2)
{
string_t* cmd=string_create();

69
tlang/tlrun Executable file
View File

@ -0,0 +1,69 @@
#!/usr/local/bin/tlang
func relto(appfile,path)
{
f=fs.parent(getabs(appfile));
while(path.startsWith("../"))
{
path = path.substring(3);
f = fs.parent(f);
}
ret fs.combine(f,path);
}
func getabs(path)
{
d = path.split(":",false);
if(d.length == 2 && d[0].length == 1)
{
if(d[1].startsWith('\\') || d[1].startsWith('/'))
{
ret path;
}
}
else
{
if(path.startsWith('\\') || path.startsWith('/'))
{
ret path;
}else{
ret fs.combine(fs.working,path);
}
}
}
filesRead = create.array();
func contains(f)
{
each(filesRead)
{
if(item == f) ret true;
}
ret false;
}
__includeStr = "#include ";
func readFile(file)
{
if(contains(file)) ret "";
filesRead.add(file);
f = fs.readalltext(file).replace("\r","").split("\n",false);
myText = "";
each(f)
{
if(item.startsWith(__includeStr))
{
myText += readFile(relto(file,item.substring(__includeStr.length))) + "\n";
}
else
{
myText += item + "\n";
}
}
ret myText;
}
func main(src,$$args)
{
code=reflection.parse_code(readFile(src));
each(args) code.add(item);
code.run();
}

86
wrappergen/src/sdl.txt Normal file
View File

@ -0,0 +1,86 @@
NAME: sdl
INCLUDE_NOW: src/sdl_funcs.c
EXEC_FUNC: __init_sdl
FUNCTION: number acos(number) = double SDL_acos(double x)
FUNCTION: number audioInit(string) = int SDL_AudioInit(const char* driver_name)
FUNCTION: null audioQuit() = void SDL_AudioQuit()
FUNCTION: number audioStreamAvailable(ptr) = int SDL_AudioStreamAvailable(SDL_AudioStream* stream)
FUNCTION: null audioStreamClear(ptr) = void SDL_AudioStreamClear(SDL_AudioStream* stream)
FUNCTION: number audioStreamFlush(ptr) = int SDL_AudioStreamFlush(SDL_AudioStream* stream)
FUNCTION: number captureMouse(bool) = int SDL_CaptureMouse(SDL_bool enabled)
FUNCTION: null clearError() = void SDL_ClearError()
FUNCTION: null clearHints() = void SDL_ClearHints()
FUNCTION: null clearQueuedAudio(number) = void SDL_ClearQueuedAudio(SDL_AudioDeviceID dev)
FUNCTION: null closeAudio() = void SDL_CloseAudio()
FUNCTION: null closeAudioDevice(number) = void SDL_CloseAudioDevice(SDL_AudioDeviceID dev)
FUNCTION: null compilerBarrier() = void SDL_CompilerBarrier()
FUNCTION: ptr createRenderer(ptr,number,number) = SDL_Renderer* SDL_CreateRenderer(SDL_Window* window,int index, Uint32 flags)
FUNCTION: ptr createRGBSurface(number,number,number,number,number,number,number,number) = SDL_Surface* SDL_CreateRGBSurface(Uint32 flags, int width, int height,int depth,Uint32 Rmask, Uint32 Gmask, Uint32 Bmask, Uint32 Amask)
FUNCTION: ptr createRGBSurfaceWithFormat(number,number,number,number,number) = SDL_Surface* SDL_CreateRGBSurfaceWithFormat(Uint32 flags, int width, int height, int depth, Uint32 format)
FUNCTION: ptr createShapedWindow(string,number,number,number,number,number) = SDL_Window* SDL_CreateShapedWindow(const char* title,int x, int y,int w,int h,Uint32 flags)
FUNCTION: ptr createSoftwareRenderer(ptr) = SDL_Renderer* SDL_CreateSoftwareRenderer(SDL_Surface* surface)
FUNCTION: ptr createSystemCursor(number) = SDL_Cursor* SDL_CreateSystemCursor(SDL_SystemCursor id)
FUNCTION: ptr createWindow(string,number,number,number,number,number)= SDL_Window* SDL_CreateWindow(const char* title,int x, int y, int w,int h, Uint32 flags)
FUNCTION: null delay(number) = void SDL_Delay(Uint32 ms)
FUNCTION: null destroyRenderer(ptr) = void SDL_DestroyRenderer(SDL_Renderer* renderer)
FUNCTION: null destroyTexture(ptr) = void SDL_DestroyTexture(SDL_Texture* texture)
FUNCTION: null destroyWindow(ptr) = void SDL_DestroyWindow(SDL_Window* window)
FUNCTION: string getClipboardText() = char* SDL_GetClipboardText()
FUNCTION: ptr imgLoad(string) = SDL_Surface* IMG_Load(const char* file)
FUNCTION: number renderDrawPoint(ptr,number,number) = int SDL_RenderDrawPoint(SDL_Renderer* renderer,int x, int y)
FUNCTION: number renderFlush(ptr) = int SDL_RenderFlush(SDL_Renderer* renderer)
FUNCTION: null renderPresent(ptr) = void SDL_RenderPresent(SDL_Renderer* renderer)
FUNCTION: null restoreWindow(ptr) = void SDL_RestoreWindow(SDL_Window* window)
FUNCTION: number saveBMP(ptr,string) = int SDL_SaveBMP(SDL_Surface* surface,const char* file)
FUNCTION: null setWindowResizable(ptr,bool) = void SDL_SetWindowResizable(SDL_Window* window,SDL_bool resizable)
FUNCTION: null setWindowTitle(ptr,string) = void SDL_SetWindowTitle(SDL_Window* window,const char* title)
FUNCTION: null showWindow(ptr) = void SDL_ShowWindow(SDL_Window* window)
FUNCTION: null setWindowSize(ptr,number,number) = void SDL_SetWindowSize(SDL_Window* window, int w,int h)
FUNCTION: number setRenderDrawColor(ptr,number,number,number,number) = int SDL_SetRenderDrawColor(SDL_Renderer* renderer,Uint8 r, Uint8 g, Uint8 b,Uint8 a)
FUNCTION: number renderDrawLine(ptr,number,number,number,number) = int SDL_RenderDrawLine(SDL_Renderer* renderer,int x1, int y1, int x2, int y2)
FUNCTION: number renderClear(ptr) = int SDL_RenderClear(SDL_Renderer* renderer)
FUNCTION: number setClipboardText(string) = int SDL_SetClipboardText(const char* text)
FUNCTION: ptr createTextureFromSurface(ptr,ptr) = SDL_Texture* SDL_CreateTextureFromSurface(SDL_Renderer* renderer, SDL_Surface* surface)
FUNCTION: null ttfCloseFont(ptr) = void TTF_CloseFont(TTF_Font* font)
ADD_FUNC: fillRect
ADD_FUNC: renderFillRect
ADD_FUNC: renderCopy
ADD_FUNC: ttfRenderTextSolid
ADD_FUNC: ttfMonoBold
ADD_FUNC: rect
ADD_FUNC: color
NUMBER: WINDOWPOS_CENTERED SDL_WINDOWPOS_CENTERED
END:
Temp:
SDL_PixelFormat * SDL_AllocFormat(Uint32 pixel_format);
SDL_Palette* SDL_AllocPalette(int ncolors);
SDL_RWops* SDL_AllocRW(void);
int SDL_BlitScaled(SDL_Surface* src,
const SDL_Rect* srcrect,
SDL_Surface* dst,
SDL_Rect* dstrect);
int SDL_BlitSurface(SDL_Surface* src,
const SDL_Rect* srcrect,
SDL_Surface* dst,
SDL_Rect* dstrect);
int SDL_BuildAudioCVT(SDL_AudioCVT * cvt,
SDL_AudioFormat src_format,
Uint8 src_channels,
int src_rate,
SDL_AudioFormat dst_format,
Uint8 dst_channels,
int dst_rate);
What cant be added using this:
void SDL_AddEventWatch(SDL_EventFilter filter,
void *userdata);
void SDL_AddHintCallback(const char *name,
SDL_HintCallback callback,
void *userdata);
SDL_TimerID SDL_AddTimer(Uint32 interval,
SDL_TimerCallback callback,
void *param);
int SDL_AudioStreamGet(SDL_AudioStream *stream, void *buf, int len);
int SDL_AudioStreamPut(SDL_AudioStream *stream, const void *buf, int len);
void SDL_CalculateGammaRamp(float gamma, Uint16 * ramp);

209
wrappergen/src/sdl_funcs.c Normal file
View File

@ -0,0 +1,209 @@
#include <SDL2/SDL.h>
#include <SDL2/SDL_image.h>
#include <SDL2/SDL_ttf.h>
#include "tlang.h"
#include "FreeMonoBold.h"
void __init_sdl(runtime_t* rt,tobject_t* obj)
{
SDL_Init(SDL_INIT_EVERYTHING);
IMG_Init(IMG_INIT_JPG | IMG_INIT_PNG);
TTF_Init();
}
void tdict_setvalue(tobject_t* obj,const char* key,tobject_t* val)
{
if(obj->type == tdict)
{
kvp_t kvp;
kvp.key = key;
kvp.value = val;
dict_setkvp(obj->data.dict,kvp);
}else{
tobject_freeifzero(val);
}
}
bool tdict_getvalue(tobject_t* obj,const char* key,tobject_t** res)
{
if(obj->type == tdict)
{
if(dict_haskey(obj->data.dict,key))
{
*res = dict_getkvp(obj->data.dict,key)->value;
return true;
}
}
return false;
}
void SDLColor_SetTobject(runtime_t* rt,tobject_t* obj,SDL_Color* c)
{
tdict_setvalue(obj,"r",tobject_number(rt,c->r));
tdict_setvalue(obj,"g",tobject_number(rt,c->g));
tdict_setvalue(obj,"b",tobject_number(rt,c->b));
tdict_setvalue(obj,"a",tobject_number(rt,c->a));
}
void SDLRect_SetTobject(runtime_t* rt,tobject_t* obj,SDL_Rect* rect)
{
tdict_setvalue(obj,"x",tobject_number(rt,rect->x));
tdict_setvalue(obj,"y",tobject_number(rt,rect->y));
tdict_setvalue(obj,"w",tobject_number(rt,rect->w));
tdict_setvalue(obj,"h",tobject_number(rt,rect->h));
}
void SDLColor_GetTobject(tobject_t* obj,SDL_Color* color)
{
color->r = 0;
color->g = 0;
color->b = 0;
color->a = 255;
tobject_t* res;
if(tdict_getvalue(obj,"r",&res) && res->type == tnumber)
{
color->r=(Uint8)res->data.number;
}
if(tdict_getvalue(obj,"g",&res) && res->type == tnumber)
{
color->g=(Uint8)res->data.number;
}
if(tdict_getvalue(obj,"b",&res) && res->type == tnumber)
{
color->b=(Uint8)res->data.number;
}
if(tdict_getvalue(obj,"a",&res) && res->type == tnumber)
{
color->a=(Uint8)res->data.number;
}
}
void SDLRect_GetTobject(tobject_t* obj,SDL_Rect* rect)
{
rect->x = 0;
rect->y = 0;
rect->w = 0;
rect->h = 0;
tobject_t* res;
if(tdict_getvalue(obj,"x",&res) && res->type == tnumber)
{
rect->x=(int)res->data.number;
}
if(tdict_getvalue(obj,"y",&res) && res->type == tnumber)
{
rect->y=(int)res->data.number;
}
if(tdict_getvalue(obj,"w",&res) && res->type == tnumber)
{
rect->w=(int)res->data.number;
}
if(tdict_getvalue(obj,"h",&res) && res->type == tnumber)
{
rect->h=(int)res->data.number;
}
}
tobject_t* sdl_ttfRenderTextSolid_external_function(runtime_t* rt,void* ptr,list_tobject_t* args)
{
if(args->length >=3 && args->items[0]->type == tnumber && args->items[1]->type == tstring && args->items[2]->type == tdict)
{
SDL_Color color;
SDLColor_GetTobject(args->items[2],&color);
char* text = string_dupp(args->items[1]->data.string);
tobject_t* o=tobject_number(rt,(double)(uintptr_t)(void*)TTF_RenderText_Solid((TTF_Font*)(void*)(uintptr_t)args->items[0]->data.number,text,color));
free(text);
return o;
}
return tobject_basic(rt,tnull);
}
tobject_t* sdl_color_external_function(runtime_t* rt,void* ptr,list_tobject_t* args)
{
//sdl.color(r,g,b,$a)
if(args->length >= 3 && args->items[0]->type == tnumber && args->items[1]->type == tnumber && args->items[2]->type == tnumber)
{
tobject_t* obj = tobject_create(rt);
obj->type = tdict;
obj->data.dict = dict_create();
tdict_setvalue(obj,"r",args->items[0]);
tdict_setvalue(obj,"g",args->items[1]);
tdict_setvalue(obj,"b",args->items[2]);
if(args->length >= 4 && args->items[3]->type == tnumber)
tdict_setvalue(obj,"a",args->items[3]);
return obj;
}
return tobject_basic(rt,tnull);
}
tobject_t* sdl_rect_external_function(runtime_t* rt,void* ptr,list_tobject_t* args)
{
//sdl.rect(x,y,w,h)
if(args->length >= 4 && args->items[0]->type == tnumber && args->items[1]->type == tnumber && args->items[2]->type == tnumber && args->items[3]->type == tnumber)
{
tobject_t* obj = tobject_create(rt);
obj->type = tdict;
obj->data.dict = dict_create();
tdict_setvalue(obj,"x",args->items[0]);
tdict_setvalue(obj,"y",args->items[1]);
tdict_setvalue(obj,"w",args->items[2]);
tdict_setvalue(obj,"h",args->items[3]);
return obj;
}
return tobject_basic(rt,tnull);
}
tobject_t* sdl_ttfMonoBold_external_function(runtime_t* rt,void* ptr,list_tobject_t* args)
{
if(args->length >= 1 && args->items[0]->type == tnumber)
{
SDL_RWops* pFontMem = SDL_RWFromConstMem(FreeMonoBold, sizeof(FreeMonoBold));
return tobject_number(rt,(double)(uintptr_t)(void*)TTF_OpenFontRW(pFontMem,1,args->items[0]->data.number));
}
return tobject_basic(rt,tnull);
}
tobject_t* sdl_fillRect_external_function(runtime_t* rt,void* ptr,list_tobject_t* args)
{
if(args->length >= 3 && args->items[0]->type == tnumber && args->items[1]->type == tdict && args->items[2]->type == tnumber)
{
SDL_Rect rect;
SDLRect_GetTobject(args->items[1],&rect);
return tobject_number(rt,SDL_FillRect((SDL_Surface*)(void*)(uintptr_t)args->items[0]->data.number,&rect,(Uint32)args->items[2]->data.number ));
}
return tobject_basic(rt,tnull);
}
tobject_t* sdl_renderFillRect_external_function(runtime_t* rt,void* ptr,list_tobject_t* args)
{
if(args->length >=2 && args->items[0]->type == tnumber && args->items[1]->type == tdict)
{
SDL_Rect rect;
SDLRect_GetTobject(args->items[1],&rect);
return tobject_number(rt,SDL_RenderFillRect((SDL_Renderer*)(void*)(uintptr_t)args->items[0]->data.number,&rect ));
}
return tobject_basic(rt,tnull);
}
tobject_t* sdl_renderCopy_external_function(runtime_t* rt,void* ptr,list_tobject_t* args)
{
if(args->length >=3 && args->items[0]->type == tnumber && args->items[1]->type == tnumber && (args->items[2]->type == tdict || args->items[2]->type == tnull) && (args->items[3]->type == tdict || args->items[3]->type == tnull))
{
SDL_Rect src;
SDL_Rect dest;
SDL_Rect* srcPtr = &src;
SDL_Rect* destPtr = &dest;
if( args->items[2]->type == tnull)
{
srcPtr = NULL;
}else{
SDLRect_GetTobject(args->items[2],srcPtr);
}
if( args->items[3]->type == tnull)
{
destPtr = NULL;
}else{
SDLRect_GetTobject(args->items[3],destPtr);
}
return tobject_number(rt, SDL_RenderCopy((SDL_Renderer*)(void*)(uintptr_t)args->items[0]->data.number,
(SDL_Texture*)(void*)(uintptr_t)args->items[1]->data.number,
srcPtr,
destPtr));
}
return tobject_basic(rt,tnull);
}