Compare commits

...

2 Commits

Author SHA1 Message Date
ame 2fd48537fb better tests + auto tests 2023-09-12 14:07:46 -05:00
ame e7480397a5 basic arth and returnable statements 2023-09-12 09:13:43 -05:00
13 changed files with 180 additions and 17 deletions

View File

@ -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){

View File

@ -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);

View File

@ -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;
}

View File

@ -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*);

View File

@ -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;

95
src/tests.lua Normal file
View File

@ -0,0 +1,95 @@
binary = "./a.out"
tests = {
{
file = "./test/add.ll",
ex_return = "60",
ex_out = "",
},{
file = "./test/def.ll",
ex_return = "5",
ex_out = "",
},{
file = "./test/exit.ll",
ex_return = "5",
ex_out = "no aaa",
},{
file = "./test/if.ll",
ex_return = "0",
ex_out = "no",
},{
file = "./test/mul.ll",
ex_return = "110",
ex_out = "",
},{
file = "./test/nested.ll",
ex_return = "5",
ex_out = "meow\nnya\nuwu",
},{
file = "./test/redef.ll",
ex_return = "20",
ex_out = "",
},{
file = "./test/sub.ll",
ex_return = "10",
ex_out = "",
},{
file = "./test/arth.ll",
ex_return = "203",
ex_out = "",
},
}
function c_exit(i)
print(i)
if(type(i)==type(true)) then
return i and 1 or 0
end
return i
end
function run_test(test)
local r = io.popen(binary.." "..test.file.."; echo -n $?")
local output = r:read("*all")
local exit
for l in output:gmatch("[^\n]+") do
exit = l
end
local len = 2;
for l in exit:gmatch(".") do
len = len + 1
end
output = output:sub(1,-len)
if(output==test.ex_out) then
if(exit==test.ex_return) then
return true
end
print("wrong exit code "..exit.."!="..test.ex_return)
return false
end
print("wrong output '"..output.."'!='"..test.ex_out.."'")
return false
end
local pass = 0
local fail = 0
local green = "\27[32m"
local red = "\27[31m"
local reset = "\27[0m"
for _,t in pairs(tests) do
print("--- *** "..t.file.."\n\n"..binary.." "..t.file.."\n")
local res = run_test(t)
if(res) then
print("--- "..green.." PASSED\n"..reset)
pass = pass + 1
else
print("--- "..red.."FAILED\n"..reset)
fail = fail + 1
end
end
if(fail==0) then
print(green.."all "..pass.." test(s) passed, all good:3"..reset)
else
print(red.."passed "..pass.." failed "..fail..reset)
end

3
test/add.ll Normal file
View File

@ -0,0 +1,3 @@
(def a 20)
(def a (+ (+ 20 20) a))
(exit a)

1
test/arth.ll Normal file
View File

@ -0,0 +1 @@
(exit (- (* (+ 5 10) 15) 22))

1
test/for.ll Normal file
View File

@ -0,0 +1 @@
()

2
test/mul.ll Normal file
View File

@ -0,0 +1,2 @@
(def uwu 5)
(exit (* uwu 22))

3
test/redef.ll Normal file
View File

@ -0,0 +1,3 @@
(def ex 5)
(def ex 20)
(exit ex)

2
test/sub.ll Normal file
View File

@ -0,0 +1,2 @@
(def meow 5)
(exit (- 15 meow))

View File

@ -1,3 +0,0 @@
(def a "meowwwww")
(write a)
(exit 12)