some simple (automated) casting

This commit is contained in:
ame 2023-09-13 11:26:15 -05:00
parent 2fd48537fb
commit 2554a1ec80
9 changed files with 52 additions and 15 deletions

View File

@ -1,19 +1,31 @@
#include "builtins.hh"
#include "pretty.hh"
#include "cast.hh"
namespace builtin {
bool builtin::check_args(lexi l, state* s){
if(l.args.size()!=args.size()&&args[0]!=ANY)
return false;
for(int i = 0; i!=l.args.size(); i++){
if(args[i]==ANY) break;
if(args[i]==NUMBER&&(l.args[i].type==INT||l.args[i].type==DOUBLE)) continue;
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);
if(s->names[l.args[i].ident].type==args[i])
if(s->names[l.args[i].ident].type==args[i])//||args[i]==NUMBER&&(s->names[l.args[i].ident].type==INT||s->names[l.args[i].ident].type==DOUBLE))
continue;
else {
object tt = cast_o(s->names[l.args[i].ident],args[i]);
if(tt.type==args[i]){
continue;
}
}
return false;
} else {
object tt = cast_o(l.args[i],args[i]);
if(tt.type==args[i]){
continue;
}
}
return false;
};

View File

@ -41,11 +41,11 @@ namespace builtin {
}
};
std::any a_exit(lexi l, state* s);
std::any write(lexi l, state* s);
std::any _if(lexi l, state* s);
std::any __anon(lexi l, state* s);
std::any _def(lexi l, state* s);
std::any a_exit(lexi, state*);
std::any write(lexi, state*);
std::any _if(lexi, state*);
std::any __anon(lexi, state*);
std::any _def(lexi, state*);
std::any _add(lexi, state*);
std::any _sub(lexi, state*);
std::any _mul(lexi, state*);

View File

@ -14,6 +14,7 @@ void interp(std::vector<lexi> inp, state* s){
//return;
for(lexi l : inp){
single_ex(l,s);
//std::cout<<batch_format(inp, 0);
//std::cout<<s->names[l.args[0].ident].ident;
}

View File

@ -36,6 +36,7 @@ void object::gen_value(){
case STATEMENT:
case STATEMENT_UNEX:
case REF:
case NUMBER:
case ANON:
case ARRAY:
case ANY:

View File

@ -15,7 +15,7 @@ enum s_symbols {
P_CLOSING = ')',
};
enum type {
NONE, INT, DOUBLE, CHAR, STRING, STATEMENT_UNEX, STATEMENT, REF, ARRAY, ANON, ANY
NONE, INT, DOUBLE, CHAR, STRING, STATEMENT_UNEX, STATEMENT, REF, ARRAY, ANON, ANY, NUMBER
};
enum reading_symbol {
LOOKING, READING_O, READING_A, READING_CON

View File

@ -1,4 +1,5 @@
#include "state.hh"
#include "cast.hh"
object dtype(object l, std::any a, type t){
switch(t){
@ -7,6 +8,7 @@ object dtype(object l, std::any a, type t){
l.type = INT;
break;
case DOUBLE:
case NUMBER:
case STRING:
case CHAR:
case NONE:
@ -16,7 +18,7 @@ object dtype(object l, std::any a, type t){
case ANON:
case ARRAY:
case ANY:
p_warn("AAAA");
//p_warn("AAAA");
break;
}
return l;
@ -29,9 +31,17 @@ lexi state::gen_args(std::vector<type> b, lexi i){
//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];
if(names[i.args[x].ident].type==b[x])
i.args[x] = names[i.args[x].ident];
else {
i.args[x] = cast_o(names[i.args[x].ident],b[x]);
}
} 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);
if(i.args[x].type!=b[x])
i.args[x] = cast_o(i.args[x],b[x]);
} else if(b[x]!=ANY&&b[x]!=i.args[x].type){
i.args[x] = cast_o(i.args[x],b[x]);
}
}
return i;

View File

@ -36,6 +36,10 @@ tests = {
file = "./test/arth.ll",
ex_return = "203",
ex_out = "",
},{
file = "./test/outint.ll",
ex_return = "0",
ex_out = "36",
},
}
@ -62,7 +66,7 @@ function run_test(test)
if(exit==test.ex_return) then
return true
end
print("wrong exit code "..exit.."!="..test.ex_return)
print("\nwrong exit code "..exit.."!="..test.ex_return)
return false
end
print("wrong output '"..output.."'!='"..test.ex_out.."'")
@ -76,11 +80,18 @@ local green = "\27[32m"
local red = "\27[31m"
local reset = "\27[0m"
local dir = io.popen("ls test")
local test_count = 0
local rtest_count = 0
for t in dir:read("*all"):gmatch("[^\n]+") do
test_count = test_count + 1
end
for _,t in pairs(tests) do
print("--- *** "..t.file.."\n\n"..binary.." "..t.file.."\n")
rtest_count = rtest_count + 1
print("--- *** "..t.file)--.."\n\n"..binary.." "..t.file.."\n")
local res = run_test(t)
if(res) then
print("--- "..green.." PASSED\n"..reset)
--print("--- "..green.." PASSED\n"..reset)
pass = pass + 1
else
print("--- "..red.."FAILED\n"..reset)
@ -88,6 +99,9 @@ for _,t in pairs(tests) do
end
end
if(test_count>rtest_count) then
print(red.."missing "..test_count - rtest_count.." tests (from ./test/)")
end
if(fail==0) then
print(green.."all "..pass.." test(s) passed, all good:3"..reset)
else

View File

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

View File

@ -1 +0,0 @@
()