Skip to content

Commit

Permalink
Fixed command line arguments
Browse files Browse the repository at this point in the history
  • Loading branch information
stuin committed Sep 28, 2023
1 parent 15cda09 commit d927279
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 16 deletions.
25 changes: 13 additions & 12 deletions src/lisp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ void Enviroment::print_env() {
for(int i = 0; i < (int)vars.size(); i++) {
std::cout << "Layer " << i << "\n";
for(auto mit = vars[i].begin(); mit != vars[i].end(); ++mit)
std::cout << mit->first << " = " << str_eval(mit->second) << "\n";
std::cout << mit->first << " = " << str_eval(mit->second, true) << "\n";
}
}

Expand Down Expand Up @@ -112,13 +112,19 @@ string Enviroment::name_eval_cont(cell const &c) {
}

//Convert to string
string Enviroment::str_eval(cell const &c) {
string Enviroment::str_eval(cell const &c, bool literal) {
string output;
sexpr array;
cell *var;
switch(c.type) {
case EXPR:
return str_eval(eval(c));
if(!literal)
return str_eval(eval(c));
case LIST:
array = std::get<sexpr>(c.content);
for(cell s : array)
output += str_eval(s);
return output;
case STRING:
return std::get<string>(c.content);
case BOOL:
Expand All @@ -127,28 +133,23 @@ string Enviroment::str_eval(cell const &c) {
return std::to_string(std::get<int>(c.content));
case FUNCTION:
return "<func>";
case LIST:
array = std::get<sexpr>(c.content);
for(cell s : array)
output += str_eval(s);
return output;
case CHAR:
return string(1, std::get<char>(c.content));
case NAME:
output = std::get<string>(c.content);

//Check if variable
var = get(output);
if(var != NULL)
if(!literal && var != NULL)
return str_eval(*var);

return output;
}

return str_eval_cont(c);
return str_eval_cont(c, literal);
}

string Enviroment::str_eval_cont(cell const &c) {
string Enviroment::str_eval_cont(cell const &c, bool literal) {
CONVERTERROR("String");
}

Expand Down Expand Up @@ -275,7 +276,7 @@ builtin Enviroment::function_eval(cell const &c) {
return std::get<builtin>(c.content);
case LIST:
return build_function(std::get<sexpr>(c.content));
case NAME:
case NAME: case STRING:
var = get(std::get<string>(c.content));
if(var != NULL)
return function_eval(*var);
Expand Down
4 changes: 2 additions & 2 deletions src/lisp.h
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ class Enviroment {

//Convert types
string name_eval(cell const &c);
string str_eval(cell const &c);
string str_eval(cell const &c, bool literal=false);
bool bool_eval(cell const &c);
int num_eval(cell const &c);
char char_eval(cell const &c);
Expand All @@ -115,7 +115,7 @@ class Enviroment {

//Allow additional type conversions
virtual string name_eval_cont(cell const &c);
virtual string str_eval_cont(cell const &c);
virtual string str_eval_cont(cell const &c, bool literal=false);
virtual bool bool_eval_cont(cell const &c);
virtual int num_eval_cont(cell const &c);
virtual char char_eval_cont(cell const &c);
Expand Down
6 changes: 4 additions & 2 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,10 @@ int main(int argc, char const *argv[])
int i = 2;

//Copy arguments into env variable
while(i < argc)
args.push_back(env.read(argv[i++]));
while(i < argc) {
string s = argv[i++];
args.push_back(env.read('"' + s + '"'));
}
env.set("args", cell(args, LIST));

//Run actual file
Expand Down

0 comments on commit d927279

Please sign in to comment.