Exemple #1
0
        // 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);
        }
Exemple #2
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);
        }
 /// <summary>
 /// Visit a parse tree produced by the <c>setVariableToValue</c>
 /// labeled alternative in <see cref="YarnSpinnerParser.set_statement"/>.
 /// <para>
 /// The default implementation returns the result of calling <see cref="AbstractParseTreeVisitor{Result}.VisitChildren(IRuleNode)"/>
 /// on <paramref name="context"/>.
 /// </para>
 /// </summary>
 /// <param name="context">The parse tree.</param>
 /// <return>The visitor result.</return>
 public virtual Result VisitSetVariableToValue([NotNull] YarnSpinnerParser.SetVariableToValueContext context)
 {
     return(VisitChildren(context));
 }
 /// <summary>
 /// Exit a parse tree produced by the <c>setVariableToValue</c>
 /// labeled alternative in <see cref="YarnSpinnerParser.set_statement"/>.
 /// <para>The default implementation does nothing.</para>
 /// </summary>
 /// <param name="context">The parse tree.</param>
 public virtual void ExitSetVariableToValue([NotNull] YarnSpinnerParser.SetVariableToValueContext context)
 {
 }