Exemplo n.º 1
0
        public override bool VisitAssignmentStatement(QuestScriptParser.AssignmentStatementContext context)
        {
            var success = base.VisitAssignmentStatement(context);

            var identifier         = context.LVal.GetText();
            var variable           = _current.GetVariable(identifier);
            var variableDefined    = variable != null;
            var isMemberAssignment = identifier.Contains(".");

            if (!isMemberAssignment && !variableDefined)
            {
                DeclareLocalVariable(identifier, context.LVal, context.RVal);
            }
            else if (variableDefined && !isMemberAssignment) //do a type check, since we are not declaring but assigning
            {
                //do type checking, since this is not a declaration but an assignment
                var rValueType = TypeInferenceVisitor.Visit(context.RVal);
                var lValueType = TypeInferenceVisitor.Visit(context.LVal);
                if (lValueType != rValueType && !TypeUtil.CanConvert(rValueType, lValueType))
                {
                    Errors.Add(new UnexpectedTypeException(context, lValueType, rValueType, context.RVal,
                                                           "Also, tried to find suitable implicit casting, but didn't find anything."));
                }

                variable.Value = ValueResolverVisitor.Visit(context.RVal);
            }

            return(success);
        }
Exemplo n.º 2
0
        private void DeclareLocalVariable(string name, ParserRuleContext variableContext,
                                          ParserRuleContext valueContext, bool isEnumerationVariable = false, bool isIterationVariable = false)
        {
            var type = TypeInferenceVisitor.Visit(valueContext);

            _current.LocalVariables.Add(new Variable
            {
                Name = name,
                Type = type,
                IsEnumerationVariable = isEnumerationVariable,
                IsIterationVariable   = isIterationVariable,
                Context = variableContext,
                Value   = ValueResolverVisitor.Visit(valueContext)
            });
        }
Exemplo n.º 3
0
        public override bool VisitForStatement(QuestScriptParser.ForStatementContext context)
        {
            _current = _current.CreateChild(context); //push

            if (!_current.IsVariableDefined(context.iterationVariable.Text))
            {
                DeclareLocalVariable(context.iterationVariable.Text, context, ObjectType.Integer,
                                     ValueResolverVisitor.Visit(context.iterationStart), isIterationVariable: true);
            }
            else
            {
                Errors.Add(
                    new ConflictingVariableName(context, context.iterationVariable.Text,
                                                "Iteration variable names in 'for' statements must not conflict with already defined variables."));
            }

            var success = base.VisitForStatement(context);

            _current = _current.Parent; //pop
            return(success);
        }
Exemplo n.º 4
0
 public ScriptEnvironmentBuilder()
 {
     TypeInferenceVisitor = new TypeInferenceVisitor(this);
     ValueResolverVisitor = new ValueResolverVisitor(this);
 }