early function implementation

This commit is contained in:
ame 2023-09-14 13:24:43 -05:00
parent 65f0a57f28
commit 620c6efdd4
5 changed files with 44 additions and 3 deletions

View File

@ -129,6 +129,15 @@ namespace builtin {
std::any _xor(lexi*l,state*s){
return (int)(std::get<int>(l->args[0].value)^std::get<int>(l->args[1].value));
}
std::any _defn(lexi*l, state*s){
func f;
f.call = l->args[0].ident;
f.args = {};
f._return = NONE;
f.calls = l->args[2].n_value;
s->functions.insert({l->args[0].ident,f});
return 0;
}
}
void ttest(lexi l){

View File

@ -65,6 +65,8 @@ namespace builtin {
std::any _or(lexi*, state*);
std::any _xor(lexi*, state*);
std::any _defn(lexi*, state*);
}
@ -92,13 +94,14 @@ static builtin::builtin _not("!",{INT},INT,builtin::_not);
static builtin::builtin _or("||",{INT,INT},INT,builtin::_or);
static builtin::builtin _xor("^",{INT,INT},INT,builtin::_xor);
static builtin::builtin _defn("defn",{NONE,INT,ANON},ANY,builtin::_defn);
static std::map<std::string, builtin::builtin*> builtins = {
{"write",&_write},{"exit",&_exit},{"if",&_if},{"__anon",&__anon},{"def",&_def},{"+",&_add}
,{"-",&_sub},{"*",&_mul},{"/",&_div},{"%",&_mod},{"get",&_get},{"set",&_set},{"==",&_equ},
{">",&_great},{"<",&_less},{">=",&_great_o_equ},{"<=",&_less_o_equ},{"!=",&_nequ},{"&&",&_and},
{"!",&_not},{"||",&_or},{"^",&_xor},
{"!",&_not},{"||",&_or},{"^",&_xor},{"defn",&_defn}
};
void ttest(lexi);

View File

@ -1,8 +1,14 @@
#include "interp.hh"
std::any single_ex(lexi l, state*s){
if(!builtins.count(l.oper)&&l.oper!="__anon")
//std::cout<<l.format(0);
if(!builtins.count(l.oper)&&l.oper!="__anon"){
if(!s->functions.count(l.oper))
p_ferr("undefined refrence to "+l.oper+" on line:"+std::to_string(l.line)+":"+std::to_string(l._char),1);
lexi gen = s->gen_args(s->functions[l.oper].args,l);
return s->functions[l.oper].exec(gen,s);
return 0;
}
if(!builtins[l.oper]->check_args(l,s))
p_ferr("incorrect usage on "+l.oper,1);
lexi gen = s->gen_args(builtins[l.oper]->args,l);

View File

@ -31,6 +31,14 @@ object dtype(object l, std::any a, type t, type wants){
return l;
}
std::any func::exec(lexi l, state*s) {
//return 0l
for(lexi ss : calls){
single_ex(ss,s);
}
return 0;//single_ex(l,s);
};
lexi state::gen_args(std::vector<type> b, lexi i){
//if(b[0]==ANY) return i;
if(i.oper=="__anon")return i;

View File

@ -14,11 +14,26 @@ void tttest();
#include "parser.hh"
#endif
object dtype(object l, std::any a, type t, type wants);
class func {
public:
std::string call = "";
std::vector<type> args = {};
type _return = NONE;
std::vector<lexi> calls;
std::any exec(lexi l, state*s);
/*func(std::string _call, std::vector<type> _args, type __return, std::vector<lexi>e){
call = _call;
args = _args;
_return = __return;
calls = e;
}*/
};
class state {
public:
std::map<std::string, object> names;
std::map<std::string, func> functions;
lexi gen_args(std::vector<type>, lexi);
};