commands.mm: More cleanups

FossilOrigin-Name: dab8760d1e340f33b5e97238f44ea13a1bc930770a24fe586735a689c06f1951
This commit is contained in:
Jonathan Schleifer 2025-03-08 14:04:23 +00:00
parent f0b20e15d8
commit f01d1163c4

View file

@ -10,19 +10,6 @@
#import "Identifier.h" #import "Identifier.h"
#import "Variable.h" #import "Variable.h"
void
itoa(char *s, int i)
{
sprintf_s(s)("%d", i);
}
char *
exchangestr(char *o, const char *n)
{
free(o);
return strdup(n);
}
// contains ALL vars/commands/aliases // contains ALL vars/commands/aliases
static OFMutableDictionary<OFString *, __kindof Identifier *> *identifiers; static OFMutableDictionary<OFString *, __kindof Identifier *> *identifiers;
@ -113,8 +100,9 @@ addcommand(OFString *name, void (*function)(), int argumentsTypes)
return false; return false;
} }
// parse any nested set of () or []
char * char *
parseexp(char *&p, int right) // parse any nested set of () or [] parseexp(char *&p, int right)
{ {
int left = *p++; int left = *p++;
char *word = p; char *word = p;
@ -134,18 +122,23 @@ parseexp(char *&p, int right) // parse any nested set of () or []
} }
char *s = strndup(word, p - word - 1); char *s = strndup(word, p - word - 1);
if (left == '(') { if (left == '(') {
string t;
// evaluate () exps directly, and substitute result
@autoreleasepool { @autoreleasepool {
itoa(t, execute(@(s))); OFString *t;
@try {
t = [OFString
stringWithFormat:@"%d", execute(@(s))];
} @finally {
free(s);
}
s = strdup(t.UTF8String);
} }
s = exchangestr(s, t);
} }
return s; return s;
} }
// parse single argument, including expressions
char * char *
parseword(char *&p) // parse single argument, including expressions parseword(char *&p)
{ {
p += strspn(p, " \t\r"); p += strspn(p, " \t\r");
if (p[0] == '/' && p[1] == '/') if (p[0] == '/' && p[1] == '/')
@ -188,9 +181,9 @@ lookup(OFString *n) // find value of ident referenced with $ in exp
return n; return n;
} }
// all evaluation happens here, recursively
int int
execute( execute(OFString *string, bool isDown)
OFString *string, bool isdown) // all evaluation happens here, recursively
{ {
@autoreleasepool { @autoreleasepool {
std::unique_ptr<char> copy(strdup(string.UTF8String)); std::unique_ptr<char> copy(strdup(string.UTF8String));
@ -211,13 +204,17 @@ execute(
char *s = parseword(p); char *s = parseword(p);
if (!s) { if (!s) {
numargs = i; numargs = i;
s = ""; s = strdup("");
}
@try {
if (*s == '$')
// substitute variables
w[i] = lookup(@(s));
else
w[i] = @(s);
} @finally {
free(s);
} }
if (*s == '$')
// substitute variables
w[i] = lookup(@(s));
else
w[i] = @(s);
} }
p += strcspn(p, ";\n\0"); p += strcspn(p, ";\n\0");
@ -250,12 +247,12 @@ execute(
count:numargs]; count:numargs];
val = [identifier val = [identifier
callWithArguments:arguments callWithArguments:arguments
isDown:isdown]; isDown:isDown];
} else if ([identifier } else if ([identifier
isKindOfClass:[Variable isKindOfClass:[Variable
class]]) { class]]) {
// game defined variables // game defined variables
if (isdown) { if (isDown) {
if (w[1].length == 0) if (w[1].length == 0)
[identifier printValue]; [identifier printValue];
else else
@ -278,10 +275,8 @@ execute(
i]; i];
alias(t, w[i]); alias(t, w[i]);
} }
// create new string here because alias
// could rebind itself
val = execute( val = execute(
[[identifier action] copy], isdown); [identifier action], isDown);
break; break;
} }
} }