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); }
public void CreateAssignment(Parser.AssignmentStatement ass, Block block, Dictionary <string, Yarn.Parser.Node> nodes, Dictionary <string, Block> blocks) { SetVariable setVar = currentFlowchartObj.AddComponent <SetVariable>(); Flowchart currFlowchart = currentFlowchartObj.GetComponent <Flowchart>(); switch (ass.operation) { case Yarn.TokenType.EqualToOrAssign: setVar.SetSetOperator(Fungus.SetOperator.Assign); break; case Yarn.TokenType.AddAssign: setVar.SetSetOperator(Fungus.SetOperator.Add); break; case Yarn.TokenType.MinusAssign: setVar.SetSetOperator(SetOperator.Subtract); break; case Yarn.TokenType.DivideAssign: setVar.SetSetOperator(SetOperator.Divide); break; case Yarn.TokenType.MultiplyAssign: setVar.SetSetOperator(SetOperator.Multiply); break; default: Debug.LogError("Unknown Operator"); break; } switch (ass.valueExpression.value.value.type) { case Value.Type.String: StringDataMulti sdm = new StringDataMulti(ass.valueExpression.value.value.AsString); setVar.SetStringData(sdm); StringVariable sv = null; if (currFlowchart.GetVariable <StringVariable>(ass.destinationVariableName) == null) { sv = currentFlowchartObj.AddComponent <StringVariable>(); sv.Scope = VariableScope.Public; sv.Key = ass.destinationVariableName; sv.Value = ""; currFlowchart.AddVariable(sv); currFlowchart.SetStringVariable(sv.Key, sv.Value); } else { sv = currFlowchart.GetVariable <StringVariable>(ass.destinationVariableName); currFlowchart.SetStringVariable(sv.Key, sv.Value); } setVar.SetAffectedVariable(sv); break; case Value.Type.Number: FloatData fd = new FloatData(ass.valueExpression.value.value.AsNumber); setVar.SetFloatData(fd); FloatVariable fv = null; if (currFlowchart.GetVariable <FloatVariable>(ass.destinationVariableName) == null) { fv = currentFlowchartObj.AddComponent <FloatVariable>(); fv.Scope = VariableScope.Public; fv.Key = ass.destinationVariableName; fv.Value = 0; currFlowchart.AddVariable(fv); currFlowchart.SetFloatVariable(fv.Key, fv.Value); } else { fv = currFlowchart.GetVariable <FloatVariable>(ass.destinationVariableName); currFlowchart.SetFloatVariable(fv.Key, fv.Value); } setVar.SetAffectedVariable(fv); break; case Value.Type.Bool: BooleanData bd = new BooleanData(ass.valueExpression.value.value.AsBool); setVar.SetBooleanData(bd); BooleanVariable bv = null; if (currFlowchart.GetVariable <BooleanVariable>(ass.destinationVariableName) == null) { bv = currentFlowchartObj.AddComponent <BooleanVariable>(); bv.Scope = VariableScope.Public; bv.Key = ass.destinationVariableName; bv.Value = false; currFlowchart.AddVariable(bv); currFlowchart.SetBooleanVariable(bv.Key, bv.Value); } else { bv = currFlowchart.GetVariable <BooleanVariable>(ass.destinationVariableName); currFlowchart.SetBooleanVariable(bv.Key, bv.Value); } setVar.SetAffectedVariable(bv); break; default: Debug.LogError("Unknown type"); break; } block.CommandList.Add(setVar); }