[script] Add support for an "else" after an "if"

This commit is contained in:
Charlie Brej 2009-06-30 20:29:16 +01:00 committed by Ray Strode
parent e0774cc8f1
commit 156d12b87a
3 changed files with 23 additions and 5 deletions

View file

@ -847,6 +847,7 @@ script_return script_execute_function (script_state* state, script_function* fun
script_return script_execute (script_state* state, script_op* op)
{
script_return reply = {SCRIPT_RETURN_TYPE_NORMAL, NULL};
if (!op) return reply;
switch (op->type){
case SCRIPT_OP_TYPE_EXPRESSION:
{
@ -864,7 +865,10 @@ script_return script_execute (script_state* state, script_op* op)
{
script_obj* obj = script_evaluate (state, op->data.cond_op.cond);
if (script_obj_as_bool(obj)){
reply = script_execute (state, op->data.cond_op.op); // FIXME propagate break
reply = script_execute (state, op->data.cond_op.op1);
}
else {
reply = script_execute (state, op->data.cond_op.op2);
}
script_obj_unref(obj);
break;
@ -875,7 +879,7 @@ script_return script_execute (script_state* state, script_op* op)
while (1){
obj = script_evaluate (state, op->data.cond_op.cond);
if (script_obj_as_bool(obj)){
reply = script_execute (state, op->data.cond_op.op); // FIXME handle break
reply = script_execute (state, op->data.cond_op.op1);
script_obj_unref(obj);
switch (reply.type) {
case SCRIPT_RETURN_TYPE_NORMAL:

View file

@ -536,11 +536,22 @@ static script_op* script_parse_if_while (ply_scan_t* scan)
curtoken = ply_scan_get_next_token(scan);
script_op* cond_op = script_parse_op(scan);
script_op* else_op = NULL;
curtoken = ply_scan_get_current_token(scan);
if (type == SCRIPT_OP_TYPE_IF &&
curtoken->type == PLY_SCAN_TOKEN_TYPE_IDENTIFIER &&
!strcmp(curtoken->data.string, "else")){
curtoken = ply_scan_get_next_token(scan);
else_op = script_parse_op(scan);
}
script_op* op = malloc(sizeof(script_op));
op->type = type;
op->data.cond_op.cond = cond;
op->data.cond_op.op = cond_op;
op->data.cond_op.op1 = cond_op;
op->data.cond_op.op2 = else_op;
return op;
}
@ -773,6 +784,7 @@ static void script_parse_exp_free (script_exp* exp)
void script_parse_op_free (script_op* op)
{
if (!op) return;
switch (op->type){
case SCRIPT_OP_TYPE_EXPRESSION:
{
@ -788,7 +800,8 @@ void script_parse_op_free (script_op* op)
case SCRIPT_OP_TYPE_WHILE:
{
script_parse_exp_free (op->data.cond_op.cond);
script_parse_op_free (op->data.cond_op.op);
script_parse_op_free (op->data.cond_op.op1);
script_parse_op_free (op->data.cond_op.op2);
break;
}
{

View file

@ -177,7 +177,8 @@ typedef struct script_op
ply_list_t* list;
struct {
script_exp* cond;
struct script_op* op;
struct script_op* op1;
struct script_op* op2;
} cond_op;
struct {
script_exp* name;