Compare commits

...

3 Commits

Author SHA1 Message Date
ame 9dd65d10b3 working variables 2023-09-12 07:08:36 -05:00
ame 838148dc8e progress on variables 2023-09-11 13:07:27 -05:00
ame fab0e9846c progress on variables 2023-09-11 12:48:11 -05:00
18 changed files with 169 additions and 49 deletions

1
.gitignore vendored Normal file
View File

@ -0,0 +1 @@
./a.out

BIN
a.out Executable file

Binary file not shown.

3
readme.md Normal file
View File

@ -0,0 +1,3 @@
todo:
* clean header files (please)

View File

@ -1,43 +1,63 @@
#include "builtins.hh"
#include "pretty.hh"
namespace builtin {
bool builtin::check_args(lexi l){
if(l.args.size()!=args.size())
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(l.args[i].type!=args[i]) return false;
if(args[i]==ANY) break;
if(l.args[i].type!=args[i]){
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])
continue;
return false;
}
return false;
};
}
return true;
}
std::any a_exit(lexi l){
exit(std::get<int>(l.args[0].value));
std::any a_exit(lexi l, state* s){
int ex;
//if(l.args[0].type==NONE)
// ex = std::get<int>(s->names[l.args[0].ident].value);
//else
ex = std::get<int>(l.args[0].value);
exit(ex);
}
std::any write(lexi l){
std::any write(lexi l, state* s){
std::cout<<std::get<std::string>(l.args[0].value)<<std::endl;
return 0;
}
std::any _if(lexi l){
std::any _if(lexi l, state* s){
if(std::get<int>(l.args[0].value)==1)
interp(l.args[1].to_larray());
interp(l.args[1].to_larray(),s);
else
interp(l.args[2].to_larray());
interp(l.args[2].to_larray(),s);
//std::cout<<batch_format(l.args[1].to_larray(),0);
return 0;
}
std::any __anon(lexi l){
std::any __anon(lexi l,state* s){
for(object o : l.args)
interp(o.n_value);
interp(o.n_value,s);
return 0;
}
std::any _def(lexi l,state *s){
s->names[l.args[0].ident] =l.args[1];
return 0;
}
}
void ttest(lexi l){
//std::cout<<write.check_args(l)<<std::endl;
(void)builtins["write"]->exec(l);
//(void)builtins["write"]->exec(l);
//a.exec = tes;
//std::cout<<std::any_cast<typeid(typemap[INT])>(a.exec({5}));
}

View File

@ -1,24 +1,39 @@
#include "parser.hh"
#define __builtins_hh
#include <typeinfo>
#include <iostream>
#include <map>
#include <typeindex>
#include <any>
#include <functional>
#include "stdarg.h"
namespace builtin{
class builtin;
}
#ifndef __interp_hh
#include "interp.hh"
#endif
#ifndef __state_hh
#include "state.hh"
#endif
//#ifndef __builtins_hh
//#define __builtins_hh
namespace builtin {
class builtin {
public:
std::string call = "";
std::vector<type> args = {};
type _return = NONE;
std::any(*exec)(lexi);
std::any(*exec)(lexi, state*);
bool check_args(lexi l);
bool check_args(lexi, state*);
builtin(std::string _call, std::vector<type> _args, type __return, std::any(*e)(lexi)){
builtin(std::string _call, std::vector<type> _args, type __return, std::any(*e)(lexi, state*)){
call = _call;
args = _args;
_return = __return;
@ -26,10 +41,11 @@ namespace builtin {
}
};
std::any a_exit(lexi l);
std::any write(lexi l);
std::any _if(lexi l);
std::any __anon(lexi l);
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);
}
@ -37,10 +53,13 @@ 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 __anon("if",{ANY},NONE,builtin::__anon);
static builtin::builtin _def("def",{ANY},NONE,builtin::_def);
static std::map<std::string, builtin::builtin*> builtins = {
{"write",&_write},{"exit",&_exit},{"if",&_if},{"__anon",&__anon}
{"write",&_write},{"exit",&_exit},{"if",&_if},{"__anon",&__anon},{"def",&_def}
};
void ttest(lexi);
//#endif

View File

@ -1,4 +1,5 @@
#include "files.hh"
#include "pretty.hh"
std::string f_read(std::string path){
std::stringstream r;

View File

@ -2,7 +2,6 @@
#include <iostream>
#include <fstream>
#include <sstream>
#include "pretty.hh"
#ifndef __files_hh
#define __files_hh

View File

@ -1,14 +1,16 @@
#include "interp.hh"
#include "pretty.hh"
#include "builtins.hh"
void interp(std::vector<lexi> inp){
void interp(std::vector<lexi> inp, state* s){
//std::cout<<batch_format(inp, 0);
//std::cout<<inp[0].args[0].value.index();
//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);
builtins[l.oper]->exec(l);
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);
//std::cout<<s->names[l.args[0].ident].ident;
}
}

View File

@ -1,5 +1,19 @@
#define __interp_hh
#include "parser.hh"
#include <vector>
#include <iostream>
void interp(std::vector<lexi>);
#ifndef __pretty_hh
#include "pretty.hh"
#endif
#ifndef __builtins_hh
#include "builtins.hh"
#endif
#ifndef __state_hh
#include "state.hh"
#endif
void interp(std::vector<lexi>, state*);

View File

@ -1,5 +1,7 @@
#include "parser.hh"
void object::clear(){
ident="";
value = NULL;

View File

@ -1,15 +1,16 @@
#define __parser_hh
#include <iostream>
#include <vector>
#include <map>
#include <algorithm>
#include <variant>
#include <any>
#ifndef __parser_cc
#define __parser_cc
//#include "state.hh"
#define INDENT_ITER 5
enum symbols {
enum s_symbols {
P_OPENING = '(',
P_CLOSING = ')',
};
@ -41,14 +42,14 @@ class lexi {
std::string oper;
std::vector<object> args;
std::string format(int);
void clear();
};
static std::map<type, std::any> typemap = {
{NONE,NULL}, {INT,(int)1}, {DOUBLE,(double)5.5}, {CHAR,(char)'5'},
{STRING,(std::string)"a"},{STATEMENT,new lexi}};
//static std::map<type, std::any> typemap = {
// {NONE,NULL}, {INT,(int)1}, {DOUBLE,(double)5.5}, {CHAR,(char)'5'},
// {STRING,(std::string)"a"},{STATEMENT,new lexi}};
std::string batch_format(std::vector<lexi>, int);
std::vector<lexi> test(std::string);
#endif

View File

@ -1,7 +1,6 @@
#include <string>
#ifndef __pretty_cc
#define __pretty_cc
#define __pretty_hh
#define color_black "\e[30m"
#define color_red "\e[31m"
@ -33,4 +32,4 @@ void _p_err(std::string, int, std::string);
#define p_ferr(X,Y) _p_ferr(X, Y, __LINE__, __FILE__);
void _p_ferr(std::string, int, int, std::string);
#endif

16
src/state.cc Normal file
View File

@ -0,0 +1,16 @@
#include "state.hh"
lexi state::gen_args(std::vector<type> b, lexi i){
if(b[0]==ANY) 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);
i.args[x] = names[i.args[x].ident];
}
}
return i;
}
void tttest(){
}

24
src/state.hh Normal file
View File

@ -0,0 +1,24 @@
#define __state_hh
#include <iostream>
#include <map>
class state;
void tttest();
#ifndef __builtins_hh
#include "builtins.hh"
#endif
#ifndef __parser_hh
#include "parser.hh"
#endif
class state {
public:
std::map<std::string, object> names;
lexi gen_args(std::vector<type>, lexi);
};

View File

@ -1,14 +1,13 @@
#include <iostream>
#include "parser.hh"
#include "files.hh"
#include "pretty.hh"
#include "interp.hh"
#include "builtins.hh"
#include <string>
#include "test.hh"
int main(int argc, char* argv[]){
//std::cout<<batch_format(test("(exit 5 22)(meow \"wharrrr\" (a 5 (aaa 5.5) ) 445)"),0);
//std::cout<<batch_format(test(f_read("./test/if.ll")),0);
interp(test(f_read(argv[1])));
state state;
interp(test(f_read(argv[1])),&state);
//interp(test(f_read("./test/exit.ll")));
return 0;
}

18
src/test.hh Normal file
View File

@ -0,0 +1,18 @@
#include "files.hh"
#include "pretty.hh"
#ifndef __interp_hh
#include "interp.hh"
#endif
#ifndef __parser_hh
#include "parser.hh"
#endif
#ifndef __state_hh
#include "state.hh"
#endif

2
test/def.ll Normal file
View File

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

View File

@ -1,3 +1,3 @@
(what)
(write "hi uwu")
(def a "meowwwww")
(write a)
(exit 12)