Example #1
0
        private void Button_Click(object sender, RoutedEventArgs e)
        {
            string expression = this.expressionTextBox.Text;

            function = new Parser.Expression(expression);
            MessageBox.Show(function.Calculate(new System.Collections.Generic.List <double>()
            {
                0.0, 0.0
            }).ToString());
        }
    public void CreateConditional(List <Parser.IfStatement.Clause> clauses, Block block, Dictionary <string, Yarn.Parser.Node> nodes, Dictionary <string, Block> blocks)
    {
        /**
         * NOTES:
         *  - clause.expression will be null if it's an else statement
         *  - refer to DialogueRunner for examples of parsing
         */
        for (int i = 0; i < clauses.Count; i++)
        {
            Parser.IfStatement.Clause clause = clauses[i];
            //if the expression is null, it is an else statement
            if (clause.expression == null)
            {
                Else elseStatement = currentFlowchartObj.AddComponent <Else>();
                block.CommandList.Add(elseStatement);
            }
            // if the clause is the first entry in clauses, then it is an if statement
            else if (i == 0)
            {
                If ifstatement = currentFlowchartObj.AddComponent <If>();
                block.CommandList.Add(ifstatement);
                if (clause.expression.function != null)
                {
                    //it is an operator statement
                    switch (clause.expression.function.name)
                    {
                    case "LessThanOrEqualTo":
                        ifstatement.SetCompareOperator(CompareOperator.LessThanOrEquals);
                        break;

                    case "GreaterThanOrEqualTo":
                        ifstatement.SetCompareOperator(CompareOperator.GreaterThanOrEquals);
                        break;

                    case "LessThan":
                        ifstatement.SetCompareOperator(CompareOperator.LessThan);
                        break;

                    case "GreaterThan":
                        ifstatement.SetCompareOperator(CompareOperator.GreaterThan);
                        break;

                    case "EqualTo":
                        ifstatement.SetCompareOperator(CompareOperator.Equals);
                        break;

                    case "NotEqualTo":
                        ifstatement.SetCompareOperator(CompareOperator.NotEquals);
                        break;

                    default:
                        Debug.LogError("NEW FUNCTION NAME: " + clause.expression.function.name);
                        break;
                    }

                    Flowchart currFlowchart = currentFlowchartObj.GetComponent <Flowchart>();

                    Parser.Expression secondExpression = clause.expression.parameters[1];
                    switch (secondExpression.value.value.type)
                    {
                    case Value.Type.String:
                        StringVariable sv = null;
                        if (currFlowchart.GetVariable <StringVariable>(clause.expression.parameters[0].value.value.GetVariableName()) == null)
                        {
                            sv       = currentFlowchartObj.AddComponent <StringVariable>();
                            sv.Scope = VariableScope.Public;
                            sv.Key   = clause.expression.parameters[0].value.value.GetVariableName();
                            sv.Value = "";
                            currFlowchart.AddVariable(sv);
                            currFlowchart.SetStringVariable(sv.Key, "");
                        }
                        else
                        {
                            sv = currFlowchart.GetVariable <StringVariable>(clause.expression.parameters[0].value.value.GetVariableName());
                        }
                        StringDataMulti data = new StringDataMulti();
                        data.stringVal = secondExpression.value.value.GetStringValue();
                        ifstatement.SetVariable(sv);
                        ifstatement.SetStringData(data);
                        break;

                    case Value.Type.Number:
                        FloatVariable fv = null;
                        if (currFlowchart.GetVariable <FloatVariable>(clause.expression.parameters[0].value.value.GetVariableName()) == null)
                        {
                            fv       = currentFlowchartObj.AddComponent <FloatVariable>();
                            fv.Scope = VariableScope.Public;
                            fv.Key   = clause.expression.parameters[0].value.value.GetVariableName();
                            fv.Value = 0;
                            currFlowchart.AddVariable(fv);
                            currFlowchart.SetFloatVariable(fv.Key, 0);
                        }
                        else
                        {
                            fv = currFlowchart.GetVariable <FloatVariable>(clause.expression.parameters[0].value.value.GetVariableName());
                        }
                        FloatData fdata = new FloatData();
                        fdata.floatVal = secondExpression.value.value.GetNumberValue();
                        ifstatement.SetVariable(fv);
                        ifstatement.SetFloatData(fdata);
                        break;

                    case Value.Type.Bool:
                        BooleanVariable bv = null;
                        if (currFlowchart.GetVariable <BooleanVariable>(clause.expression.parameters[0].value.value.GetVariableName()) == null)
                        {
                            bv       = currentFlowchartObj.AddComponent <BooleanVariable>();
                            bv.Scope = VariableScope.Public;
                            bv.Key   = clause.expression.parameters[0].value.value.GetVariableName();
                            bv.Value = false;
                            currFlowchart.AddVariable(bv);
                            currFlowchart.SetBooleanVariable(bv.Key, false);
                        }
                        else
                        {
                            bv = currFlowchart.GetVariable <BooleanVariable>(clause.expression.parameters[0].value.value.GetVariableName());
                        }
                        BooleanData bdata = new BooleanData();
                        bdata.booleanVal = secondExpression.value.value.GetBoolValue();
                        ifstatement.SetVariable(bv);
                        ifstatement.SetBooleanData(bdata);
                        break;

                    default:
                        Debug.LogError("Unknown Parser Value Type");
                        break;
                    }
                }
            }
            //otherwise, it's an elseif statement
            else
            {
                ElseIf ifstatement = currentFlowchartObj.AddComponent <ElseIf>();
                block.CommandList.Add(ifstatement);
                if (clause.expression.function != null)
                {
                    //it is an operator statement
                    switch (clause.expression.function.name)
                    {
                    case "LessThanOrEqualTo":
                        ifstatement.SetCompareOperator(CompareOperator.LessThanOrEquals);
                        break;

                    case "GreaterThanOrEqualTo":
                        ifstatement.SetCompareOperator(CompareOperator.GreaterThanOrEquals);
                        break;

                    case "LessThan":
                        ifstatement.SetCompareOperator(CompareOperator.LessThan);
                        break;

                    case "GreaterThan":
                        ifstatement.SetCompareOperator(CompareOperator.GreaterThan);
                        break;

                    case "EqualTo":
                        ifstatement.SetCompareOperator(CompareOperator.Equals);
                        break;

                    case "NotEqualTo":
                        ifstatement.SetCompareOperator(CompareOperator.NotEquals);
                        break;

                    default:
                        Debug.LogError("NEW FUNCTION NAME: " + clause.expression.function.name);
                        break;
                    }

                    Flowchart currFlowchart = currentFlowchartObj.GetComponent <Flowchart>();

                    Parser.Expression secondExpression = clause.expression.parameters[1];
                    switch (secondExpression.value.value.type)
                    {
                    case Value.Type.String:
                        StringVariable sv = null;
                        if (currFlowchart.GetVariable <StringVariable>(clause.expression.parameters[0].value.value.GetVariableName()) == null)
                        {
                            sv       = currentFlowchartObj.AddComponent <StringVariable>();
                            sv.Scope = VariableScope.Public;
                            sv.Key   = clause.expression.parameters[0].value.value.GetVariableName();
                            sv.Value = "";
                            currFlowchart.AddVariable(sv);
                            currFlowchart.SetStringVariable(sv.Key, "");
                        }
                        else
                        {
                            sv = currFlowchart.GetVariable <StringVariable>(clause.expression.parameters[0].value.value.GetVariableName());
                        }
                        StringDataMulti data = new StringDataMulti();
                        data.stringVal = secondExpression.value.value.GetStringValue();
                        ifstatement.SetVariable(sv);
                        ifstatement.SetStringData(data);
                        break;

                    case Value.Type.Number:
                        FloatVariable fv = null;
                        if (currFlowchart.GetVariable <FloatVariable>(clause.expression.parameters[0].value.value.GetVariableName()) == null)
                        {
                            fv       = currentFlowchartObj.AddComponent <FloatVariable>();
                            fv.Scope = VariableScope.Public;
                            fv.Key   = clause.expression.parameters[0].value.value.GetVariableName();
                            fv.Value = 0;
                            currFlowchart.AddVariable(fv);
                            currFlowchart.SetFloatVariable(fv.Key, 0);
                        }
                        else
                        {
                            fv = currFlowchart.GetVariable <FloatVariable>(clause.expression.parameters[0].value.value.GetVariableName());
                        }
                        FloatData fdata = new FloatData();
                        fdata.floatVal = secondExpression.value.value.GetNumberValue();
                        ifstatement.SetVariable(fv);
                        ifstatement.SetFloatData(fdata);
                        break;

                    case Value.Type.Bool:
                        BooleanVariable bv = null;
                        if (currFlowchart.GetVariable <BooleanVariable>(clause.expression.parameters[0].value.value.GetVariableName()) == null)
                        {
                            bv       = currentFlowchartObj.AddComponent <BooleanVariable>();
                            bv.Scope = VariableScope.Public;
                            bv.Key   = clause.expression.parameters[0].value.value.GetVariableName();
                            bv.Value = false;
                            currFlowchart.AddVariable(bv);
                            currFlowchart.SetBooleanVariable(bv.Key, false);
                        }
                        else
                        {
                            bv = currFlowchart.GetVariable <BooleanVariable>(clause.expression.parameters[0].value.value.GetVariableName());
                        }
                        BooleanData bdata = new BooleanData();
                        bdata.booleanVal = secondExpression.value.value.GetBoolValue();
                        ifstatement.SetVariable(bv);
                        ifstatement.SetBooleanData(bdata);
                        break;

                    default:
                        Debug.LogError("Unknown Parser Value Type");
                        break;
                    }
                }
            }

            //Parse the statements once you figure out what kind of if-else to use
            ParseNodeHelper(clause.statements, block, nodes, blocks);
        }

        //Put in an end-if
        End end = currentFlowchartObj.AddComponent <End>();

        block.CommandList.Add(end);
    }