tlang-c/libtlang/include/tlang.h

473 lines
16 KiB
C

#pragma once
#include "tlang_version.h"
#ifndef CMAKE_TLANG_BUILD
#include "myfeatures.h"
#endif
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <stdatomic.h>
#include <string.h>
#include <stdint.h>
typedef enum {
NONEXIST=0,
REGULAR = 0b0001,
FOLDER = 0b0010,
SYM = 0b0100,
CHARDEV = 0b01000,
BLOCKDEV = 0b10000,
DEV = CHARDEV | BLOCKDEV,
FIFO = 0b0100000,
OTHER = 0b1000000,
NORMAL = REGULAR | FOLDER,
SPECIAL = SYM | DEV | FIFO | OTHER,
ANY = NORMAL | SPECIAL
} fs_entry_filter_t;
typedef struct stream stream_t;
typedef struct ittr ittr_t;
typedef struct scope scope_t;
typedef struct node node_t;
typedef struct lextoken lextoken_t;
typedef struct lextokenlist lextokenlist_t;
typedef struct runtime runtime_t;
typedef void (*action_t)(runtime_t*);
typedef struct list_tobject list_tobject_t;
typedef struct list_string list_string_t;
typedef struct dictinary dict_t;
typedef struct runtime runtime_t;
typedef struct tobject tobject_t;
typedef struct string string_t;
struct string {
char* text;
int capacity;
int length;
};
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*);
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;
typedef void (*texternalmethod_free_t)(tobject_t*);
typedef scope_t* (*get_scope_t)(scope_t*);
typedef void (*runtime_reg_t)(runtime_t* rt);
typedef tobject_t* (*get_variable_t)(scope_t*,char*);
typedef void (*set_variable_t)(scope_t*,char*,tobject_t*);
typedef bool (*has_variable_t)(scope_t*,char*);
typedef void (*ittr_reset_t)(ittr_t*);
typedef tobject_t* (*ittr_getcurrent_t)(ittr_t*);
typedef bool (*ittr_movenext_t)(ittr_t*);
void list_set(list_tobject_t* a,int index,tobject_t* item);
node_t* node_negative_create(node_t* left);
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,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;
tobject_t* value;
} kvp_t;
struct dictinary{
kvp_t* items;
int capacity;
int length;
} ;
struct list_tobject {
runtime_t* rt;
tobject_t** items;
int capacity;
int length;
};
struct tobject
{
tobject_type_t type;
atomic_int count;
texternalmethod_free_t free;
void* ptr_data;
union {
struct {
void* data;
external_method_t method;
} external_method;
struct {
node_t* closure;
scope_t* scope;
} internal_method;
double number;
string_t* string;
list_tobject_t list;
dict_t* dict;
bool boolean;
char chr;
} data;
};
struct runtime
{
bool hasStd;
runtime_reg_t reg;
node_t* program;
scope_t* globals;
list_tobject_t args;
};
struct list_string
{
string_t** strings;
int count;
int capacity;
};
struct scope {
void* data;
runtime_t* rt;
bool isRoot;
atomic_int count;
get_scope_t root;
get_scope_t subscope;
get_variable_t getvariable;
set_variable_t setvariable;
has_variable_t hasvariable;
};
void scope_create_root(runtime_t* rt);
scope_t* scope_begin(scope_t* s);
scope_t* scope_end(scope_t* s);
dict_t* dict_create();
void dict_setkvp(dict_t* dict,kvp_t kvp);
kvp_t* dict_getkvp(dict_t* dict,char* key);
bool dict_haskey(dict_t* dict,char* key);
void dict_rm(dict_t* dict,char* key);
void dict_free(dict_t* dict);
typedef void (*node_free_t)(node_t*);
struct node
{
node_type_t type;
node_exec_t execute;
node_free_t free;
union {
struct {
list_string_t argNames;
node_t* node;
} closure_node;
struct {
node_t* condition;
node_t* body;
bool isDoLoop;
} whileloop_node;
struct {
node_t** items;
int count;
int capacity;
bool isSub;
node_t* switch_arg;
} scope_node;
node_t* single_node_node;
struct {
node_t* left;
node_t* right;
} two_node_node;
struct {
node_t* variable;
node_t* value;
bool deleteVariableNode;
} variable_set_node;
double const_number;
string_t* const_string;
bool const_bool;
char const_char;
tobject_t* const_obj;
struct {
node_t* parent;
string_t* name;
node_t** args;
int count;
int capacity;
} function_call_node;
struct {
node_t* parent;
string_t* name;
node_set_t set;
} variable_node;
struct {
node_t* parent;
node_t* argument;
node_set_t set;
} array_node;
struct {
node_t* init;
node_t* condition;
node_t* inc;
node_t* body;
} forloop_node;
struct {
node_t* variable;
node_t* ls;
node_t* body;
} eachloop_node;
struct {
node_t* condition;
node_t* yes;
node_t* no;
} if_node;
} data;
};
tobject_t* tobject_create(runtime_t* rt);
tobject_t* tobject_number(runtime_t* rt,double num);
tobject_t* tobject_string(runtime_t* rt,string_t* str);
tobject_t* tobject_bool(runtime_t* rt,bool b);
tobject_t* tobject_char(runtime_t* rt,char c);
tobject_t* tobject_basic(runtime_t* rt,tobject_type_t type);
tobject_t* tobject_charp(runtime_t* rt,const char* text);
void tobject_addref(tobject_t*);
void tobject_rmref(tobject_t*);
void tscope_addref(scope_t*);
void tscope_rmref(scope_t*);
runtime_t* runtime_init();
void runtime_free(runtime_t* rt);
void runtime_load(runtime_t* rt,string_t* str);
tobject_t* runtime_exec(runtime_t* rt);
void string_free(string_t*);
string_t* string_create();
void string_appendc(string_t*,char);
void string_clear(string_t*);
char* string_dupp(string_t*);
string_t* string_dups(string_t*);
void string_appends(string_t*,string_t*);
void string_read(string_t* str,void* ptr,size_t(*_cb)(void*,size_t,size_t,void*));
ittr_t* string_ittr(runtime_t* rt,string_t* str);
void string_appendp(string_t* a,const char* str);
tobject_t* tobject_stringp(runtime_t* rt,const char* ptr);
struct lextokenlist
{
lextoken_t* tokens;
int count;
int capacity;
};
struct lextoken
{
bool isChar;
bool isString;
string_t* text;
};
lextokenlist_t* lextokenlist_create();
void lextokenlist_addstring(lextokenlist_t* ls,string_t* str);
void lextokenlist_additem(lextokenlist_t* ls,string_t* str);
void lextokenlist_addchar(lextokenlist_t* ls,char c);
bool lextoken_isitem(lextoken_t t,char*);
lextokenlist_t* lexer_lex(string_t* text);
void lextokenlist_free(lextokenlist_t* t);
const char* string_trimstart(const char* ptr,char c);
void tobject_freeifzero(tobject_t* obj);
void node_free(node_t*);
node_t* node_scope_create(bool);
node_t* node_add_create(node_t*,node_t*);
node_t* node_sub_create(node_t*,node_t*);
void node_scope_append(node_t* sc,node_t* n);
tobject_t* tobject_call(scope_t* scope,tobject_t* func,list_tobject_t* args);
void list_create(runtime_t* rt,list_tobject_t* ls,int count);
void list_add(list_tobject_t* a,tobject_t* item);
void string_appendn(string_t* s,double val);
bool string_samep(string_t* s,const char* text);
string_t* string_replace(string_t* text,string_t* old,string_t* new);
tobject_t* string_split(runtime_t* rt,string_t* text,string_t* del,int max,bool empty);
bool string_startswith(string_t* haystack,string_t* needle);
bool string_endswith(string_t* haystack,string_t* needle);
node_t* node_eq_create(node_t* left,node_t* right);
string_t* string_trimstarts(string_t* s,char c);
string_t* string_trimends(string_t* s,char c);
bool string_islong(string_t* s,int64_t* number);
bool string_isnumber(string_t* s,double* number);
string_t* tobject_tostring(scope_t* sc,tobject_t* s);
node_t* node_const_number_create(double num);
node_t* node_const_string_create(string_t* s);
node_t* node_const_null_create();
node_t* node_const_undef_create();
tobject_t* tobject_fromexternalmethod(runtime_t* rt,void* data,external_method_t method,texternalmethod_free_t free);
tobject_t* tobject_create_array(runtime_t* rt,int count);
void list_free(list_tobject_t* a);
node_t* node_function_call_create(string_t*);
void node_function_call_add(node_t* fc,node_t* arg);
node_t* node_getvariable_create(string_t* name);
node_t* node_setvariable_create(node_t* n1,node_t* n2,bool);
node_t* node_getmember_create(string_t* name,node_t* parent);
node_t* node_bitwiseand_create(node_t* left,node_t* right);
node_t* node_bitwiseor_create(node_t* left,node_t* right);
node_t* node_xor_create(node_t* left,node_t* right);
node_t* node_neq_create(node_t* left,node_t* right);
struct ittr
{
void* ptr;
ittr_movenext_t movenext;
ittr_reset_t reset;
ittr_getcurrent_t getcurrent;
ittr_reset_t free;
};
ittr_t* ittr_create(void* ptr,ittr_movenext_t movenext,ittr_reset_t reset,ittr_getcurrent_t current,ittr_reset_t free);
void ittr_reset(ittr_t* ittr);
ittr_t* ittr_obj(tobject_t* obj,scope_t* s);
tobject_t* ittr_current(ittr_t* ittr);
bool ittr_movenext(ittr_t* ittr);
void ittr_free(ittr_t* ittr);
ittr_t* list_ittr(runtime_t* rt,tobject_t* ls);
void runtime_create_method_on_dict(dict_t* dict,char* name,runtime_t* rt,void* ptr,external_method_t cb,texternalmethod_free_t free);
node_t* node_method_call_create(node_t* parent,string_t*);
void node_method_call_add(node_t* fc,node_t* arg);
node_t* node_modulo_create(node_t* left,node_t* right);
node_t* node_sub_create(node_t* left,node_t* right);
node_t* node_multiply_create(node_t* left,node_t* right);
node_t* node_divide_create(node_t* left,node_t* right);
node_t* node_leftshift_create(node_t* left,node_t* right);
node_t* node_rightshift_create(node_t* left,node_t* right);
node_t* node_lessthan_create(node_t* left,node_t* right);
node_t* node_greaterthan_create(node_t* left,node_t* right);
node_t* node_lessthanequal_create(node_t* left,node_t* right);
node_t* node_greaterthanequal_create(node_t* left,node_t* right);
node_t* node_xor_create(node_t* left,node_t* right);
node_t* node_logicaland_create(node_t* left,node_t* right);
node_t* node_logicalor_create(node_t* left,node_t* right);
node_t* node_closure_create(list_string_t argNames,node_t* node);
node_t* node_const_bool_create(bool c);
node_t* node_const_char_create(char c);
bool string_sames(string_t* s,string_t* b);
//node_t* node_setarray_create(node_t* parent,node_t* argument,node_t* variable);
node_t* node_getarray_create(node_t* parent,node_t* argument);
typedef int64_t (*stream_read_t)(stream_t*,void*,int64_t);
typedef void (*stream_write_t)(stream_t*,void*,int64_t);
typedef int64_t (*stream_get_t)(stream_t*);
typedef void (*stream_set_t)(stream_t*,int64_t);
typedef bool (*stream_bool_t)(stream_t*);
typedef void (*stream_free_t)(stream_t*);
typedef void (*set_extra_methods_t)(stream_t*,tobject_t*,runtime_t*,void*);
struct stream
{
void* ptr;
stream_read_t read;
stream_write_t write;
stream_get_t getpos;
stream_get_t getlength;
stream_set_t setpos;
stream_bool_t canread;
stream_bool_t canwrite;
stream_bool_t canseek;
stream_free_t free;
stream_free_t flush;
set_extra_methods_t set_extra_methods;
void* set_extra_methods_data;
};
stream_t* stream_create(
void* ptr,
stream_read_t read,
stream_write_t write,
stream_get_t getpos,
stream_get_t getlength,
stream_set_t setpos,
stream_bool_t canread,
stream_bool_t canwrite,
stream_bool_t canseek,
stream_free_t free,
stream_free_t flush
);
void stream_flush(stream_t* strm);
stream_t* tobject_tostream(scope_t* s,tobject_t* obj,bool closeStream);
tobject_t* tobject_fromstream(runtime_t* rt,stream_t* strm);
int64_t stream_read(stream_t*,void*,int64_t);
void stream_write(stream_t*,void*,int64_t);
int64_t stream_getpos(stream_t*);
int64_t stream_getlength(stream_t*);
void stream_setpos(stream_t*,int64_t);
bool stream_canread(stream_t*);
bool stream_canwrite(stream_t*);
bool stream_canseek(stream_t*);
stream_t* stream_stdin();
stream_t* stream_stdout();
stream_t* stream_stderr();
void stream_free(stream_t*);
bool stream_true(stream_t*);
bool stream_false(stream_t*);
stream_t* stream_file_create(string_t* str);
stream_t* stream_file_openread(string_t* str);
stream_t* stream_file_openreadwrite(string_t* str);
stream_t* stream_file_readappend(string_t* str);
stream_t* stream_file_append(string_t* str);
ittr_t* fs_dir_ittr(runtime_t*,string_t*,fs_entry_filter_t);
bool fs_file_exists(string_t* f);
bool fs_dir_exists(string_t* d);
void runtime_register(runtime_t* rt,runtime_reg_t reg);
bool tobject_tobool(tobject_t* o);
node_t* node_while_loop_create(node_t* condition,node_t* body,bool isDoLoop);
node_t* node_for_loop_create(node_t* init,node_t* condition,node_t* inc,node_t* body);
node_t* node_eachloop_create(node_t* var,node_t* ls,node_t* body);
node_t* node_if_create(node_t* condition,node_t* y,node_t* n);
tobject_t* tobject_same(runtime_t* rt,tobject_t* l,tobject_t* r,bool* freeleftifzero);
bool tobject_sameb(runtime_t* rt,tobject_t* l,tobject_t* r);
int list_indexof(list_tobject_t* a,tobject_t* item);
bool list_contains(list_tobject_t* a,tobject_t* item);
void list_remove(list_tobject_t* a,tobject_t* item);
void list_rm(list_tobject_t* a,int offset);
tobject_t* list_at(list_tobject_t* a,int offset);
node_t* node_bitwisenot_create(node_t* left);
typedef void* tmutex_t;
void Global_Mutex_Lock();
void Global_Mutex_Unlock();
#if defined(USE_THREADS)
int64_t Mutex_Lock(tmutex_t mtx);
int64_t Mutex_Unlock(tmutex_t mtx);
#endif
string_t* path_join(string_t* a,string_t* b);
fs_entry_filter_t fs_type(string_t* _f);
string_t* string_substring(string_t* s,int start,int length);
string_t* string_remove(string_t* s,int start,int length);
int string_lastindexof(string_t* s,char c);
int string_indexof(string_t* s,char c);
string_t* path_getextension(string_t* a);
string_t* path_filename(string_t* a);
string_t* path_parent(string_t* a);
void runtime_add_argument(runtime_t* rt,tobject_t* arg);
char* fs_tlang_config_dir();
void runtime_register_std(runtime_t* rt);
void runtime_create_number_on_dict(dict_t* dict,char* name,runtime_t* rt,double number);
void runtime_create_bool_on_dict(dict_t* dict,char* name,runtime_t* rt,bool value);
void runtime_create_string_on_dict(dict_t* dict,char* name,runtime_t* rt,const char* value);
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);
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);