// A set command: explicitly setting a value to an expression // <<set $foo to 1>> public override int VisitSetVariableToValue(YarnSpinnerParser.SetVariableToValueContext context) { // add the expression (whatever it resolves to) Visit(context.expression()); // now store the variable and clean up the stack string variableName = context.VAR_ID().GetText(); compiler.Emit(OpCode.StoreVariable, new Operand(variableName)); compiler.Emit(OpCode.Pop); return(0); }
public override Yarn.Type VisitSetVariableToValue([NotNull] YarnSpinnerParser.SetVariableToValueContext context) { var expressionType = Visit(context.expression()); var variableType = Visit(context.variable()); var variableName = context.variable().GetText(); if (expressionType == Yarn.Type.Undefined) { // We don't know what this is set to, so we'll have to // assume it's ok. Return the variable type, if known. return(variableType); } if (variableType == Yarn.Type.Undefined) { // We don't have a type for this variable. Create an // implicit declaration that it's this type. // Get the position of this reference in the file int positionInFile = context.Start.Line; // The start line of the body is the line after the delimiter int nodePositionInFile = this.currentNodeContext.BODY_START().Symbol.Line + 1; var decl = new Declaration { Name = variableName, DeclarationType = Declaration.Type.Variable, Description = $"{System.IO.Path.GetFileName(sourceFileName)}, node {currentNodeName}, line {positionInFile - nodePositionInFile}", ReturnType = expressionType, DefaultValue = DefaultValueForType(expressionType), SourceFileName = sourceFileName, SourceFileLine = positionInFile, SourceNodeName = currentNodeName, SourceNodeLine = positionInFile - nodePositionInFile, IsImplicit = true, }; NewDeclarations.Add(decl); } else if (expressionType != variableType) { throw new TypeException(context, $"{variableName} ({variableType}) cannot be assigned a {expressionType}", sourceFileName); } return(expressionType); }