Exemplo n.º 1
0
        public Variable AddLocal(SSLParser.VariableDefinitionContext ctx, SSLVisitor vis)
        {
            var v = Variable.FromContext(ctx, vis, VariableScope.Local);

            if (!_scopes.Peek().TryAddVariable(v, out var error))
            {
                vis.Error(ctx, error);
            }
            return(v);
        }
        internal static Variable FromContext(SSLParser.VariableDefinitionContext ctx, SSLVisitor vis, VariableScope scope)
        {
            var name = ctx.Name.Text;

            if (name[0] == '$')
            {
                vis.Error(ctx, "Cannot start a variable with the character '$', this is reserved for built-in variables.");
            }
            if (name.Length > 32)
            {
                vis.Error(ctx, "Variable names cannot be longer than 32 characters.");
            }

            var type = ReflectionUtils.TranslateTypeContext(ctx.type());

            if (!type.HasValue)
            {
                vis.Error(ctx, $"Unable to convert variable '{name}' to internal type.");
            }
            if (type.Value == ShaderType.Void)
            {
                vis.Error(ctx, $"The variable '{name}' cannot be of type 'void'.");
            }

            uint?asize = null;

            if (ctx.arrayIndexer() != null)
            {
                if (!vis.TryParseArrayIndexer(ctx.arrayIndexer(), (null, null), out var aidx, out var error))
                {
                    vis.Error(ctx.arrayIndexer(), error);
                }
                if (aidx.Index2 != null)
                {
                    vis.Error(ctx.arrayIndexer(), "Cannot declare multi-dimensional arrays.");
                }
                if (!aidx.Index1.GetIntegerLiteral().HasValue)                 // Must be a literal (OR A SPEC CONSTANT TODO)
                {
                    vis.Error(ctx.arrayIndexer(), "Must use integer literals when declaring arrays.");
                }
                asize = (uint)aidx.Index1.GetIntegerLiteral().Value;
                if (asize.Value > 255)
                {
                    vis.Error(ctx.arrayIndexer(), "Cannot declare arrays larger than 255.");
                }
            }

            return(new Variable(type.Value, name, scope, ctx.KW_CONST() != null, asize));
        }