Example #1
0
        // decodes a parameter and adds it to bytecode stream.
        // return either DataElement, int, double, or String  (for type checking)
        private Object DecodeAndAddParameter(DataArea locals, String p)
        {
            // this must be a label address difference
            if ((!p.StartsWith("'")) && p.Contains(':'))
            {
                currentobject.AddLabelDifference(p);
                return(999999999);  // only compatible with 32bit numbers
            }
            // this must be a variable
            else if ((p[0] >= 'A' && p[0] <= 'Z') || p[0] == '_')
            {
                // check if have offset suffix
                int offset  = 0;
                int plusidx = p.IndexOf('+');
                if (plusidx >= 0)
                {
                    if (Int32.TryParse(p.Substring(plusidx + 1), NumberStyles.Integer, CultureInfo.InvariantCulture, out offset))
                    {
                        p = p.Substring(0, plusidx);
                    }
                }

                DataElement e     = null;
                bool        local = true;
                if (locals != null)
                {
                    e = locals.Get(p);
                }
                if (e == null)
                {
                    e     = globals.Get(p);
                    local = false;
                }
                if (e == null)
                {
                    throw new AssemblerException("Unknown identifier " + p);
                }

                currentobject.AddVariableReference(e.position + offset, local);
                return(e);
            }
            // this must be a constant numeric value
            else if ((p[0] >= '0' && p[0] <= '9') || p[0] == '-')
            {
                Int32  c;
                double d;
                if (Int32.TryParse(p, NumberStyles.Integer, CultureInfo.InvariantCulture, out c))
                {
                    currentobject.AddConstant(c);
                    return(c);
                }
                if (double.TryParse(p, NumberStyles.Float, CultureInfo.InvariantCulture, out d))
                {
                    currentobject.AddFloatConstant(d);
                    return(d);
                }
                throw new AssemblerException("Can not decode number: " + p);
            }
            // this must be a string literal
            else if (p[0] == '\'')
            {
                String l = p.Substring(1, p.Length - 2);
                currentobject.AddStringLiteral(l);
                return(l);
            }
            else
            {
                throw new AssemblerException("Invalid parameter value");
            }
        }