Пример #1
0
        public ObjectExpression evalSimpleExpression(SimpleExpression exp)
        {
            if (exp is LiteralExpression)
            {
                lit = (LiteralExpression)exp;

                switch (lit.value.type)
                {
                case ParserUtils.Token.TokenValue.STRING:
                    return(new ObjectExpression(ObjectType.stringType, lit.value.stringValue));

                case ParserUtils.Token.TokenValue.INT:
                    return(new ObjectExpression(ObjectType.intType, lit.value.intValue));

                case ParserUtils.Token.TokenValue.CHAR:
                    return(new ObjectExpression(ObjectType.charType, lit.value.charValue));

                case ParserUtils.Token.TokenValue.BOOLEAN:
                    return(new ObjectExpression(ObjectType.booleanType, lit.value.booleanValue));

                default:
                    break;
                }
            }
            else if (exp is SubSimpleExpression)
            {
                return(evalSimpleExpression(((SubSimpleExpression)exp).exp));
            }
            else if (exp is NullExpression)
            {
                return(null);
            }
            else if (exp is BinaryOperatorExpression)
            {
                BinaryOperatorExpression binExp = (BinaryOperatorExpression)exp;
                ObjectExpression         oLeft = evalSimpleExpression(binExp.e1), oRight;

                switch (binExp.op)
                {
                case ParserUtils.Token.TokenValue.AND:
                    if ((bool)oLeft.value)
                    {
                        oRight = evalSimpleExpression(binExp.e2);
                        return(new ObjectExpression(ObjectType.booleanType, (bool)oRight.value));
                    }

                    return(new ObjectExpression(ObjectType.booleanType, false));

                case ParserUtils.Token.TokenValue.OR:
                    if ((bool)oLeft.value)
                    {
                        return(new ObjectExpression(ObjectType.booleanType, true));
                    }

                    oRight = evalSimpleExpression(binExp.e2);
                    return(new ObjectExpression(ObjectType.booleanType, (bool)oRight.value));

                case ParserUtils.Token.TokenValue.PLUS:
                    oRight = evalSimpleExpression(binExp.e2);

                    if (oLeft.type == ObjectType.stringType || oRight.type == ObjectType.stringType)
                    {
                        return(new ObjectExpression(ObjectType.stringType, oLeft.value.ToString() + oRight.value.ToString()));
                    }
                    if (oLeft.type == ObjectType.intType && oRight.type == ObjectType.intType)
                    {
                        return(new ObjectExpression(ObjectType.intType, (int)oLeft.value + (int)oRight.value));
                    }
                    break;

                case ParserUtils.Token.TokenValue.MINUS:
                    oRight = evalSimpleExpression(binExp.e2);
                    return(new ObjectExpression(ObjectType.intType, (int)oLeft.value - (int)oRight.value));

                case ParserUtils.Token.TokenValue.TIMES:
                    oRight = evalSimpleExpression(binExp.e2);
                    return(new ObjectExpression(ObjectType.intType, (int)oLeft.value * (int)oRight.value));

                case ParserUtils.Token.TokenValue.DIV:
                    oRight = evalSimpleExpression(binExp.e2);
                    return(new ObjectExpression(ObjectType.intType, (int)oLeft.value / (int)oRight.value));

                case ParserUtils.Token.TokenValue.LT:
                    oRight = evalSimpleExpression(binExp.e2);
                    return(new ObjectExpression(ObjectType.booleanType, (int)oLeft.value < (int)oRight.value));

                case ParserUtils.Token.TokenValue.GT:
                    oRight = evalSimpleExpression(binExp.e2);
                    return(new ObjectExpression(ObjectType.booleanType, (int)oLeft.value > (int)oRight.value));

                case ParserUtils.Token.TokenValue.LEQ:
                    oRight = evalSimpleExpression(binExp.e2);
                    return(new ObjectExpression(ObjectType.booleanType, (int)oLeft.value <= (int)oRight.value));

                case ParserUtils.Token.TokenValue.GEQ:
                    oRight = evalSimpleExpression(binExp.e2);
                    return(new ObjectExpression(ObjectType.booleanType, (int)oLeft.value >= (int)oRight.value));

                case ParserUtils.Token.TokenValue.EQBOOL:
                    oRight = evalSimpleExpression(binExp.e2);

                    if (oLeft.type == ObjectType.intType)
                    {
                        return(new ObjectExpression(ObjectType.booleanType, (int)oLeft.value == (int)oRight.value));
                    }
                    if (oLeft.type == ObjectType.stringType)
                    {
                        return(new ObjectExpression(ObjectType.booleanType, ((string)oLeft.value).Equals((string)oRight.value)));
                    }
                    if (oLeft.type == ObjectType.booleanType)
                    {
                        return(new ObjectExpression(ObjectType.booleanType, (bool)oLeft.value == (bool)oRight.value));
                    }
                    if (oLeft.type == ObjectType.charType)
                    {
                        return(new ObjectExpression(ObjectType.booleanType, (char)oLeft.value == (char)oRight.value));
                    }
                    break;

                default:
                    break;
                }
            }
            else if (exp is UnaryOperatorExpression)
            {
                unExp = (UnaryOperatorExpression)exp;

                switch (unExp.op)
                {
                case ParserUtils.Token.TokenValue.NEG:
                    o1 = evalSimpleExpression(((UnaryOperatorExpression)exp).exp);
                    return(new ObjectExpression(ObjectType.booleanType, !((bool)o1.value)));
                }
            }
            else if (exp is VariableExpression)
            {
                return(variables[((VariableExpression)exp).name]);
            }
            else if (exp is CharacterVariableExpression)
            {
                return(new ObjectExpression(ObjectType.intType, IrisData.getPlayerVariable(((CharacterVariableExpression)exp).name)));
            }

            return(null);
        }
Пример #2
0
        public void evalInstruction(InstructionExpression instr)
        {
            if (instr is ValueExpression)
            {
                valueExp = (ValueExpression)instr;

                if (valueExp.type == ObjectType.intType)
                {
                    variables.Add(valueExp.name, new ObjectExpression(valueExp.type, 0));
                }
                else if (valueExp.type == ObjectType.booleanType)
                {
                    variables.Add(valueExp.name, new ObjectExpression(valueExp.type, false));
                }
                else if (valueExp.type == ObjectType.stringType)
                {
                    variables.Add(valueExp.name, new ObjectExpression(valueExp.type, ""));
                }

                if (valueExp.value != null)
                {
                    variables[valueExp.name] = new ObjectExpression(valueExp.type, evalSimpleExpression(valueExp.value).value);
                }

                return;
            }
            else if (instr is AssignExpression)
            {
                assignExp = (AssignExpression)instr;

                variables[assignExp.name] = evalSimpleExpression(assignExp.value);

                return;
            }
            else if (instr is OpenMsgDialogInstruction)
            {
                gScreen.msgBox.openBox();
                return;
            }
            else if (instr is MsgDisplayInstruction)
            {
                msgExp = (MsgDisplayInstruction)instr;

                o1 = evalSimpleExpression(msgExp.msg);

                if (o1.type != ObjectType.stringType)
                {
                    return;
                }

                gScreen.msgBox.setText((string)o1.value);

                return;
            }
            else if (instr is NextMsgDialogInstruction)
            {
                shouldEvalSeq = false;
                gScreen.msgBox.showNextButton();
                return;
            }
            else if (instr is CloseMsgDialogInstruction)
            {
                shouldEvalSeq = false;
                gScreen.msgBox.showCloseButton();
                return;
            }
            else if (instr is SwitchCharacterInstruction)
            {
                try
                {
                    gScreen.msgBox.switchCharacter(IrisData.characters[((SwitchCharacterInstruction)instr).character.name]);
                }
                catch (Exception e)
                {
                    return;
                }

                return;
            }
            else if (instr is RemoveCharacter)
            {
                gScreen.msgBox.removeCharacter();
                return;
            }
            else if (instr is SetVariableInstruction)
            {
                setExp = (SetVariableInstruction)instr;

                /*if (!playerVariables.ContainsKey(setExp.variable.name))
                 * {
                 *  playerVariables.Add(setExp.variable.name, -1);
                 * }
                 *
                 * playerVariables[setExp.variable.name] = (int)(evalSimpleExpression(setExp.value).value);*/
                IrisData.setPlayerVariable(setExp.variable.name, (int)(evalSimpleExpression(setExp.value).value));

                return;
            }
            else if (instr is InputInstruction)
            {
                inputExp = (InputInstruction)instr;

                o1 = variables[inputExp.name];

                shouldEvalSeq = false;

                if (o1.type == ObjectType.intType)
                {
                    gScreen.iBox.openBox(InputBox.InputType.INT_INPUT);

                    lastInstr = inputExp;
                }
                else if (o1.type == ObjectType.stringType)
                {
                    gScreen.iBox.openBox(InputBox.InputType.STRING_INPUT);

                    lastInstr = inputExp;
                }

                return;
            }
            else if (instr is MenuInstruction)
            {
                menuExp       = (MenuInstruction)instr;
                shouldEvalSeq = false;

                gScreen.menBox.setChoices(((MenuInstruction)instr).choices);
                gScreen.menBox.openMenu();

                lastInstr = menuExp;

                return;
            }
            else if (instr is PlayMediaInstruction)
            {
                playExp = (PlayMediaInstruction)instr;

                /*if (!IrisData.sounds.ContainsKey(playExp.name))
                 * {
                 *  return;
                 * }*/
                //else if(IrisData.
                try
                {
                    gScreen.playMedia(IrisData.sounds[playExp.name]);
                }
                catch (Exception e)
                {
                    return;
                }

                return;
            }
            else if (instr is StopMediaInstruction)
            {
                gScreen.stopMedia();
                return;
            }
            else if (instr is ShowImageInstruction)
            {
                showExp = (ShowImageInstruction)instr;

                if (showExp.image is VariableExpression)
                {
                    img = IrisData.images[((VariableExpression)showExp.image).name];
                }
                else
                {
                    memberExp = (MemberAccessExpression)showExp.image;

                    charac = IrisData.characters[memberExp.name];
                    img    = charac.getImage(memberExp.field);
                }

                gScreen.showImage(img, showExp.position);

                return;
            }
            else if (instr is SetBackgroundInstruction)
            {
                bgdInstr = (SetBackgroundInstruction)instr;

                try
                {
                    gScreen.setBackground(IrisData.backgrounds[bgdInstr.image.name]);
                }
                catch (Exception e)
                {
                    return;
                }

                return;
            }
            else if (instr is CleanBackgroundInstruction)
            {
                gScreen.clearBackground();
                return;
            }
            else if (instr is CleanForegroundInstruction)
            {
                gScreen.clearForeGround();
                return;
            }
            else if (instr is GotoInstruction)
            {
                gotoExp = (GotoInstruction)instr;

                npcExp = gotoExp.npc;

                evalExpression(IrisData.npcs[npcExp.npc].labels[npcExp.label]);

                return;
            }
        }