basic arth and returnable statements
This commit is contained in:
parent
7bb03cb399
commit
e7480397a5
|
@ -7,7 +7,7 @@ namespace builtin {
|
|||
return false;
|
||||
for(int i = 0; i!=l.args.size(); i++){
|
||||
if(args[i]==ANY) break;
|
||||
if(l.args[i].type!=args[i]){
|
||||
if(l.args[i].type!=args[i]&&l.args[i].type!=STATEMENT){
|
||||
if(l.args[i].type==NONE){
|
||||
if(!s->names.count(l.args[i].ident))
|
||||
p_ferr("undefined refrence to "+l.args[i].ident,1);
|
||||
|
@ -50,9 +50,25 @@ namespace builtin {
|
|||
return 0;
|
||||
}
|
||||
std::any _def(lexi l,state *s){
|
||||
s->names[l.args[0].ident] =l.args[1];
|
||||
s->names[l.args[0].ident] = l.args[1];
|
||||
return 0;
|
||||
}
|
||||
|
||||
std::any _add(lexi l, state* s){
|
||||
return std::get<int>(l.args[0].value) + std::get<int>(l.args[1].value);
|
||||
}
|
||||
std::any _sub(lexi l, state* s){
|
||||
return std::get<int>(l.args[0].value) - std::get<int>(l.args[1].value);
|
||||
}
|
||||
std::any _mul(lexi l, state* s){
|
||||
return std::get<int>(l.args[0].value) * std::get<int>(l.args[1].value);
|
||||
}
|
||||
std::any _div(lexi l, state* s){
|
||||
return std::get<int>(l.args[0].value) / std::get<int>(l.args[1].value);
|
||||
}
|
||||
std::any _mod(lexi l, state* s){
|
||||
return std::get<int>(l.args[0].value) % std::get<int>(l.args[1].value);
|
||||
}
|
||||
}
|
||||
|
||||
void ttest(lexi l){
|
||||
|
|
|
@ -46,18 +46,29 @@ namespace builtin {
|
|||
std::any _if(lexi l, state* s);
|
||||
std::any __anon(lexi l, state* s);
|
||||
std::any _def(lexi l, state* s);
|
||||
std::any _add(lexi, state*);
|
||||
std::any _sub(lexi, state*);
|
||||
std::any _mul(lexi, state*);
|
||||
std::any _div(lexi, state*);
|
||||
std::any _mod(lexi, state*);
|
||||
|
||||
}
|
||||
|
||||
|
||||
static builtin::builtin _write("write",{STRING},NONE,builtin::write);
|
||||
static builtin::builtin _exit("exit",{INT},NONE,builtin::a_exit);
|
||||
static builtin::builtin _if("if",{INT,STATEMENT,STATEMENT},NONE,builtin::_if);
|
||||
static builtin::builtin _if("if",{INT,ANON,ANON},NONE,builtin::_if);
|
||||
static builtin::builtin __anon("if",{ANY},NONE,builtin::__anon);
|
||||
static builtin::builtin _def("def",{ANY},NONE,builtin::_def);
|
||||
static builtin::builtin _def("def",{NONE,ANY},NONE,builtin::_def);
|
||||
static builtin::builtin _add("+",{INT,INT},INT,builtin::_add);
|
||||
static builtin::builtin _sub("-",{INT,INT},INT,builtin::_sub);
|
||||
static builtin::builtin _mul("*",{INT,INT},INT,builtin::_mul);
|
||||
static builtin::builtin _div("/",{INT,INT},INT,builtin::_div);
|
||||
static builtin::builtin _mod("%",{INT,INT},INT,builtin::_mod);
|
||||
|
||||
static std::map<std::string, builtin::builtin*> builtins = {
|
||||
{"write",&_write},{"exit",&_exit},{"if",&_if},{"__anon",&__anon},{"def",&_def}
|
||||
|
||||
{"write",&_write},{"exit",&_exit},{"if",&_if},{"__anon",&__anon},{"def",&_def},{"+",&_add}
|
||||
,{"-",&_sub},{"*",&_mul},{"/",&_div},{"%",&_mod}
|
||||
};
|
||||
|
||||
void ttest(lexi);
|
||||
|
|
|
@ -1,15 +1,19 @@
|
|||
#include "interp.hh"
|
||||
|
||||
std::any single_ex(lexi l, state*s){
|
||||
if(!builtins.count(l.oper)&&l.oper!="__anon")
|
||||
p_ferr("undefined refrence to "+l.oper+" on line:"+std::to_string(l.line)+":"+std::to_string(l._char),1);
|
||||
if(!builtins[l.oper]->check_args(l,s))
|
||||
p_ferr("incorrect usage on "+l.oper,1);
|
||||
return builtins[l.oper]->exec(s->gen_args(builtins[l.oper]->args,l),s);
|
||||
}
|
||||
|
||||
void interp(std::vector<lexi> inp, state* s){
|
||||
//std::cout<<batch_format(inp, 0);
|
||||
//std::cout<<inp[0].args[0].value.index();
|
||||
//return;
|
||||
for(lexi l : inp){
|
||||
if(!builtins.count(l.oper)&&l.oper!="__anon")
|
||||
p_ferr("undefined refrence to "+l.oper+" on line:"+std::to_string(l.line)+":"+std::to_string(l._char),1);
|
||||
if(!builtins[l.oper]->check_args(l,s))
|
||||
p_ferr("incorrect usage on "+l.oper,1);
|
||||
builtins[l.oper]->exec(s->gen_args(builtins[l.oper]->args,l),s);
|
||||
single_ex(l,s);
|
||||
//std::cout<<s->names[l.args[0].ident].ident;
|
||||
}
|
||||
|
||||
|
|
|
@ -2,6 +2,7 @@
|
|||
#include "parser.hh"
|
||||
#include <vector>
|
||||
#include <iostream>
|
||||
#include <any>
|
||||
|
||||
#ifndef __pretty_hh
|
||||
#include "pretty.hh"
|
||||
|
@ -16,4 +17,5 @@
|
|||
#endif
|
||||
|
||||
void interp(std::vector<lexi>, state*);
|
||||
std::any single_ex(lexi, state*);
|
||||
|
||||
|
|
32
src/state.cc
32
src/state.cc
|
@ -1,11 +1,37 @@
|
|||
#include "state.hh"
|
||||
|
||||
object dtype(object l, std::any a, type t){
|
||||
switch(t){
|
||||
case INT:
|
||||
l.value = std::any_cast<int>(a);
|
||||
l.type = INT;
|
||||
break;
|
||||
case DOUBLE:
|
||||
case STRING:
|
||||
case CHAR:
|
||||
case NONE:
|
||||
case STATEMENT:
|
||||
case STATEMENT_UNEX:
|
||||
case REF:
|
||||
case ANON:
|
||||
case ARRAY:
|
||||
case ANY:
|
||||
p_warn("AAAA");
|
||||
break;
|
||||
}
|
||||
return l;
|
||||
}
|
||||
|
||||
lexi state::gen_args(std::vector<type> b, lexi i){
|
||||
if(b[0]==ANY) return i;
|
||||
//if(b[0]==ANY) return i;
|
||||
if(i.oper=="__anon")return i;
|
||||
for(int x = 0; x!=i.args.size();x++){
|
||||
if(i.args[x].type==NONE){
|
||||
if(!names.count(i.args[x].ident)) p_ferr("undefined refrence to "+i.args[x].ident,1);
|
||||
//std::cout<<"AA"<<std::endl;
|
||||
if(i.args[x].type==NONE&&b[x]!=NONE){
|
||||
if(!names.count(i.args[x].ident)) continue;//p_ferr("undefined refrence to "+i.args[x].ident,1);
|
||||
i.args[x] = names[i.args[x].ident];
|
||||
} else if(b[x]!=ANON&&i.args[x].type==STATEMENT){
|
||||
i.args[x] = dtype(i.args[x],single_ex(i.args[x].n_value[0],this),builtins[i.args[x].n_value[0].oper]->_return);
|
||||
}
|
||||
}
|
||||
return i;
|
||||
|
|
|
@ -0,0 +1,3 @@
|
|||
(def a 20)
|
||||
(def a (+ (+ 20 20) a))
|
||||
(exit a)
|
|
@ -0,0 +1 @@
|
|||
()
|
|
@ -0,0 +1,2 @@
|
|||
(def uwu 5)
|
||||
(exit (* uwu 22))
|
|
@ -0,0 +1,3 @@
|
|||
(def ex 5)
|
||||
(def ex 20)
|
||||
(exit ex)
|
|
@ -0,0 +1,2 @@
|
|||
(def meow 5)
|
||||
(exit (- 15 meow))
|
Loading…
Reference in New Issue