casting & tests

This commit is contained in:
ame 2023-09-14 13:25:25 -05:00
parent 620c6efdd4
commit e147e8648a
6 changed files with 82 additions and 0 deletions

57
src/cast.cc Normal file
View File

@ -0,0 +1,57 @@
#include "cast.hh"
#include "pretty.hh"
object cast_o(object i, type t, state* s){
if(i.type==STATEMENT&&t!=STATEMENT&&t!=ANON&&i.ident!="__anon"){
i = dtype(i,single_ex(i.n_value[0],s),builtins[i.n_value[0].oper]->_return,t);
//i.type = STATEMENTC;
if(i.type!=t)
i = cast_o(i,t,s);
return i;
}
switch(t){
case INT:
if(i.type==DOUBLE){
i.type = INT;
i.value = (int)std::get<double>(i.value);
break;
}
if(i.type==STRING){
i.type = INT;
i.value = std::stoi(std::get<std::string>(i.value));
break;
}
p_ferr("no avaliable converstion from "+std::to_string(i.type)+" to "+std::to_string(t),1);
break;
case NONE:
case CHAR:
break;
case STRING:
if(i.type==DOUBLE){
i.type = STRING;
i.value = std::to_string(std::get<double>(i.value));
break;
}
if(i.type==INT){
i.type = STRING;
i.value = std::to_string(std::get<int>(i.value));
break;
}
p_ferr("no avaliable converstion from "+std::to_string(i.type)+" to "+std::to_string(t),1);
break;
case STATEMENT:
break;
case STATEMENT_UNEX:
case STATEMENTC:
case DOUBLE:
case REF:
case ARRAY:
case ANON:
case ANY:
case NUMBER:
break;
}
return i;
}

13
src/cast.hh Normal file
View File

@ -0,0 +1,13 @@
#include <iostream>
#ifndef __state_hh
#include "state.hh"
#endif
#ifndef __parser_hh
#include "parser.hh"
#endif
object cast_o(object, type, state*);

6
test/array.ll Normal file
View File

@ -0,0 +1,6 @@
(def meow {1,(+ 2 2),{5},4,5,6})
(write (get meow 5))
(set meow 5 2222)
(write (get (get meow 2) 0))
(write (get meow 5))
(exit (get meow 1))

3
test/defn.ll Normal file
View File

@ -0,0 +1,3 @@
(defn name 1
((write "test")(write "whar")))
(name)

1
test/logic.ll Normal file
View File

@ -0,0 +1 @@
(write (&& 1 1))

2
test/outint.ll Normal file
View File

@ -0,0 +1,2 @@
(def uu (+ 2 2))
(write (* uu 9))