示例#1
0
 private static void DoFixups(GearsChunk chunk, int origin, Func <GearsValue, int> makeConstant, Func <string, int> makeConstant2, List <LoxCompiler> fns)
 {
     foreach (LoxCompiler fn in fns)
     {
         int codeBase = chunk.SizeCode;
         chunk.WriteCode(fn._Chunk._Code, fn._Chunk._Lines, fn._Chunk.SizeCode);
         chunk.WriteCodeAt(origin + fn._OriginAddress, (byte)(codeBase >> 8));
         chunk.WriteCodeAt(origin + fn._OriginAddress + 1, (byte)(codeBase & 0xff));
         foreach (LoxCompilerFixup fixup in fn._FixupConstants)
         {
             GearsValue value         = fn._Chunk.ReadConstantValue(fixup.Value);
             int        constantFixup = makeConstant(value); // as fixup
             chunk.WriteCodeAt(codeBase + fixup.Address, (byte)(constantFixup >> 8));
             chunk.WriteCodeAt(codeBase + fixup.Address + 1, (byte)(constantFixup & 0xff));
         }
         foreach (LoxCompilerFixup fixup in fn._FixupStrings)
         {
             string value         = fn._Chunk.Strings.ReadStringConstant(fixup.Value);
             int    constantFixup = makeConstant2(value); // as fixup
             chunk.WriteCodeAt(codeBase + fixup.Address, (byte)(constantFixup >> 8));
             chunk.WriteCodeAt(codeBase + fixup.Address + 1, (byte)(constantFixup & 0xff));
         }
         DoFixups(chunk, codeBase, makeConstant, makeConstant2, fn._FixupFns);
         chunk.Compress();
     }
 }
示例#2
0
        internal int WriteConstantValue(GearsValue value)
        {
            CheckGrowConstantCapacity(1);
            int index = SizeConstant;

            _Constants[index] = (ulong)value;
            SizeConstant     += 1;
            return(index);
        }
示例#3
0
        internal GearsValue ReadConstantValue(int offset)
        {
            if (offset < 0 || offset > SizeConstant)
            {
                return(-1); // todo: runtime error
            }
            GearsValue value = _Constants[offset];

            return(value);
        }
示例#4
0
        /// <summary>
        /// Adds the given value to the chunk's constant table.
        /// Returns the index of that constant in the constant table.
        /// </summary>
        private int MakeValueConstant(GearsValue value)
        {
            for (int i = 0; i < _Chunk.SizeConstant; i++)
            {
                if ((ulong)_Chunk.ReadConstantValue(i) == (ulong)value)
                {
                    return(i);
                }
            }
            int index = _Chunk.WriteConstantValue(value);

            if (index > short.MaxValue)
            {
                throw new CompilerException(Tokens.Peek(), "Too many constants in one chunk.");
            }
            return(index);
        }
示例#5
0
 public override void SetField(ulong name, GearsValue value) => _Fields.Set(name, value);
示例#6
0
 public override bool TryGetField(ulong name, out GearsValue value) => _Fields.TryGet(name, out value);
示例#7
0
 public abstract void SetField(ulong name, GearsValue value);
示例#8
0
 public abstract bool TryGetField(ulong name, out GearsValue value);
示例#9
0
 public GearsObjBoundMethod(GearsValue receiver, GearsObjFunction method)
 {
     Receiver = receiver;
     Method   = method;
 }
示例#10
0
 public override bool TryGetField(ulong name, out GearsValue value)
 {
     return(_Wrapper.TryGetField(_Context, WrappedObject, name, out value));
 }
示例#11
0
 public override void SetField(ulong name, GearsValue value)
 {
     _Wrapper.SetField(_Context, WrappedObject, name, value);
 }
示例#12
0
        internal string ReadConstantValueAsBitStr(int offset)
        {
            GearsValue value = ReadConstantValue(offset);

            return(BitString.GetBitStr((ulong)value));
        }