Skip to content

Commit 31b092a

Browse files
committed
Fixed while not creating its own scope; implemented exitWith
1 parent 86ba8bd commit 31b092a

File tree

3 files changed

+54
-1
lines changed

3 files changed

+54
-1
lines changed

SQF-VM/Entry.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -196,6 +196,7 @@ void register_commmands(PVM vm)
196196
create_cmd(vm, L"vectorDistance", 'b', cmd_vectordistance, 4, L"<VECTOR3D> vectorDistance <VECTOR3D>", L"_euclideanDist = getPosASL player vectorDistance [0,0,0];", L"Distance between two 3D vectors.");
197197
create_cmd(vm, L"vectorDistanceSqr", 'b', cmd_vectordistancesqr, 4, L"<VECTOR3D> vectorDistanceSqr <VECTOR3D>", L"_distSqr = getPos player vectorDistanceSqr [0,0,2];", L"Squared distance between two 3D vectors.");
198198
create_cmd(vm, L">>", 'b', cmd_navigateconfig, 4, L"<CONFIG> >> <STRING>", L"", L"Returns subentry of config entry with given name.");
199+
create_cmd(vm, L"exitWith", 'b', cmd_exitwith, 4, L"<IF> exitWith <CODE>", L"", L"Exits current scope {...} it is executed from if condition evaluates true, creates new scope {...code...} and executes the given code in it.");
199200

200201
create_cmd(vm, L"diag_log", 'u', cmd_diag_LOG, 4, L"diag_log <ANY>", L"", L"Dumps the argument's value. Each call creates a new line.");
201202
create_cmd(vm, L"systemChat", 'u', cmd_systemchat, 4, L"systemChat <STRING>", L"", L"Writes the argument's value plaintext. Each call creates a new line.");

SQF-VM/sqf_commands.c

Lines changed: 52 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1933,6 +1933,10 @@ void cmd_do(void* input, CPCMD self)
19331933
}
19341934
if (left_val->type == WHILE_TYPE())
19351935
{
1936+
if (vm->stack->data[vm->stack->top - 1]->type != INST_SCOPE)
1937+
{
1938+
push_stack(vm, vm->stack, inst_scope(L"loop"));
1939+
}
19361940
push_stack(vm, vm->stack, inst_command(find_command(vm, L"do", 'b')));
19371941
push_stack(vm, vm->stack, left);
19381942
push_stack(vm, vm->stack, right);
@@ -1961,11 +1965,14 @@ void cmd_do(void* input, CPCMD self)
19611965
pfor->current < pfor->end :
19621966
pfor->current > pfor->end)
19631967
{
1968+
if (vm->stack->data[vm->stack->top - 1]->type != INST_SCOPE)
1969+
{
1970+
push_stack(vm, vm->stack, inst_scope(L"loop"));
1971+
}
19641972
push_stack(vm, vm->stack,
19651973
inst_command(find_command(vm, L"do", 'b')));
19661974
push_stack(vm, vm->stack, left);
19671975
push_stack(vm, vm->stack, right);
1968-
push_stack(vm, vm->stack, inst_scope(L"loop"));
19691976
push_stack(vm, vm->stack, inst_code_load(0));
19701977
push_stack(vm, vm->stack,
19711978
inst_value(value(CODE_TYPE(), right_val->val)));
@@ -6921,4 +6928,48 @@ void cmd_tolower(void* input, CPCMD self)
69216928
}
69226929
push_stack(vm, vm->stack, inst_value(value(STRING_TYPE(), base_voidptr(outstring))));
69236930
inst_destroy(right);
6931+
}
6932+
void cmd_exitwith(void* input, CPCMD self)
6933+
{
6934+
PVM vm = input;
6935+
PINST left;
6936+
PINST right;
6937+
PVALUE left_val;
6938+
PVALUE right_val;
6939+
PSTRING outstring;
6940+
unsigned int i;
6941+
left = pop_stack(vm, vm->work);
6942+
right = pop_stack(vm, vm->work);
6943+
left_val = get_value(vm, vm->stack, left);
6944+
right_val = get_value(vm, vm->stack, right);
6945+
if (left_val == 0 || right_val == 0)
6946+
{
6947+
inst_destroy(left);
6948+
inst_destroy(right);
6949+
return;
6950+
}
6951+
if (left_val->type != IF_TYPE())
6952+
{
6953+
vm->error(vm, ERR_LEFT_TYPE ERR_IF, vm->stack);
6954+
inst_destroy(left);
6955+
inst_destroy(right);
6956+
push_stack(vm, vm->stack,
6957+
inst_value(value(NOTHING_TYPE(), base_int(0))));
6958+
return;
6959+
}
6960+
if (right_val->type != CODE_TYPE())
6961+
{
6962+
vm->error(vm, ERR_RIGHT_TYPE ERR_CODE, vm->stack);
6963+
inst_destroy(left);
6964+
inst_destroy(right);
6965+
push_stack(vm, vm->stack,
6966+
inst_value(value(NOTHING_TYPE(), base_int(0))));
6967+
return;
6968+
}
6969+
push_stack(vm, vm->stack, inst_scope_dropout(0));
6970+
push_stack(vm, vm->stack, inst_code_load(true));
6971+
push_stack(vm, vm->stack, right);
6972+
push_stack(vm, vm->stack, inst_pop_eval(3, false));
6973+
push_stack(vm, vm->stack, inst_value(value(BOOL_TYPE(), left_val->val)));
6974+
inst_destroy(left);
69246975
}

SQF-VM/sqf_commands.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -188,5 +188,6 @@ void cmd_isclass(void* input, CPCMD self);
188188

189189
void cmd_toupper(void* input, CPCMD self);
190190
void cmd_tolower(void* input, CPCMD self);
191+
void cmd_exitwith(void* input, CPCMD self);
191192

192193
#endif // !_SQF_COMMANDS_H_

0 commit comments

Comments
 (0)