コード例 #1
0
        public override object VisitAssign([NotNull] TinyScriptParser.AssignContext context)
        {
            var      type = (Type)VisitExpression(context.expression());
            var      name = context.Identifier().GetText();
            Variable value;

            if (_variables.TryGetValue(name, out value))
            {
                if (value.Value == null)
                {
                    LocalBuilder variable;
                    if (value.Type == null)
                    {
                        variable   = _builder.DeclareLocal(type);
                        value.Type = type;
                    }
                    else if (value.Type != type)
                    {
                        throw context.Exception("Cannot convert type [{0}] to [{1}].", type.Name, value.Type.Name);
                    }
                    else
                    {
                        variable = _builder.DeclareLocal(type);
                    }
                    value.Value = variable;
                }
                _builder.SetLocal(value.Value);
                return(null);
            }
            throw context.Exception("Variable [{0}] not defined.", name);
        }
コード例 #2
0
        public override object VisitAssign([NotNull] TinyScriptParser.AssignContext context)
        {
            var      name = context.Identifier().GetText();
            VarValue obj;

            if (!Variables.TryGetValue(name, out obj))
            {
                throw context.Exception("Variable [{0}] not defined.", name);
            }
            var r = base.VisitAssign(context);

            if (obj != null && obj.Value != null)
            {
                if (obj.Value.GetType() != r.GetType())
                {
                    throw context.Exception("Cannot convert type [{1}] to [{0}].", obj.Value.GetType().Name, r.GetType().Name);
                }
            }
            Variables[name] = new VarValue(obj.StartIndex, r);
            return(null);
        }