Compare commits

...

2 Commits

Author SHA1 Message Date
Kelvin Sherlock 91590e92aa add missing (mostly macroman) operators 2022-11-25 16:02:27 -05:00
Kelvin Sherlock 6199944350 script parameters, move shift to the environment 2022-11-23 21:22:00 -05:00
6 changed files with 48 additions and 8 deletions

View File

@ -188,7 +188,8 @@ int builtin_shift(Environment &env, const std::vector<std::string> &tokens, cons
}
if (n == 0) return 0;
env.shift(n);
#if 0
auto argv = load_argv(env);
if (argv.empty()) return 0;
@ -199,7 +200,7 @@ int builtin_shift(Environment &env, const std::vector<std::string> &tokens, cons
} while (--n);
env.set_argv(argv);
#endif
return 0;
}

View File

@ -400,7 +400,7 @@ int simple_command::execute(Environment &env, const fdmask &fds, bool throwup) {
// scripts run with an isolated environment.
Environment new_env = env.subshell_environment();
new_env.set("command", path);
new_env.set_argv(path, p.arguments);
new_env.set_argv(p.arguments);
try {
return read_file(new_env, path, newfds);

View File

@ -112,15 +112,17 @@ namespace {
set_common(k, std::to_string(value), exported);
}
#if 0
void Environment::set_argv(const std::string &argv0, const std::vector<std::string>& argv) {
set_common("0", argv0, false);
set_argv(argv);
}
#endif
void Environment::set_argv(const std::vector<std::string>& argv) {
_pound = argv.size();
set_common("#", std::to_string(argv.size()), false);
_pound = argv.size() - 1;
set_common("#", std::to_string(_pound), false);
int n = 1;
int n = 0;
for (const auto &s : argv) {
set_common(std::to_string(n++), s, false);
}
@ -143,8 +145,22 @@ namespace {
}
p.pop_back();
set_common("parameters", p, false);
}
void Environment::shift(int n) {
if (n < 0) return;
if (_pound < 1) return;
std::vector<std::string> argv;
argv.push_back(get("0"));
for (int i = 1 + n; i <= _pound; ++i) {
argv.push_back(get(std::to_string(i)));
}
for (int i = 0; i < n; ++i) {
unset(std::to_string(_pound - i));
}
set_argv(argv);
}

View File

@ -59,9 +59,11 @@ public:
Environment subshell_environment();
void set_argv(const std::string &argv0, const std::vector<std::string>& argv);
// void set_argv(const std::string &argv0, const std::vector<std::string>& argv);
void set_argv(const std::vector<std::string>& argv);
void shift(int n);
void set(const std::string &k, const std::string &value, bool exported = false);
void set(const std::string &k, long l, bool exported = false);
void unset(const std::string &k);

View File

@ -322,6 +322,8 @@ int expression_parser::precedence(int op) {
case '<=':
case '>':
case '>=':
case 0xb2:
case 0xb3:
return 6;
case '==':
@ -388,9 +390,11 @@ value expression_parser::eval(int op, value &lhs, value &rhs) {
return lhs.to_number() < rhs.to_number();
case '<=':
case 0xb2:
return lhs.to_number() <= rhs.to_number();
case '>=':
case 0xb3:
return lhs.to_number() >= rhs.to_number();
case '>>':

View File

@ -70,6 +70,22 @@
# eval-only.
# macroman ≤
0xb2 when eval
%push_token => { tokens.emplace_back("\xb2", '<='); };
# macroman ≠
0xad when eval
%push_token => { tokens.emplace_back("\xad", '!='); };
# macroman ¬
0xc2 when eval
%push_token => { tokens.emplace_back("\xc2", '!'); };
# macroman ÷
0xd6 when eval
%push_token => { tokens.emplace_back("\xd6", '/'); };
'||' when eval
%push_token => { tokens.emplace_back("||", '||'); };
'|' when eval
@ -78,13 +94,14 @@
'&&' when eval
%push_token => { tokens.emplace_back("&&", '&&'); };
'(' when eval
%push_token => { tokens.emplace_back("(", '('); };
')' when eval
%push_token => { tokens.emplace_back(")", ')'); };
'<>' when eval
%push_token => { tokens.emplace_back("<>", '!='); };
'<<' when eval
%push_token => { tokens.emplace_back("<<", '<<'); };