Beispiel #1
0
        public static ObjString tableFindString(ref Table_t table, char[] chars, int _start, int length, uint hash)
        {
            if (table.count == 0)
            {
                return(null);
            }

            uint index = (uint)(hash & table.capacity);

            for (;;)
            {
                Entry entry = table.entries[index];

                if (entry.key == null)
                {
                    // Stop if we find an empty non-tombstone entry.
                    if (Value.IS_NIL(entry.value))
                    {
                        return(null);
                    }
                }
                else if (entry.key.length == length &&
                         entry.key.hash == hash &&
                         Cfun._memcmp(entry.key.chars, entry.key._start, chars, _start, length))
                {
                    // We found it.
                    return(entry.key);
                }


                index = (uint)((index + 1) & table.capacity);
            }
        }
Beispiel #2
0
        static bool identifiersEqual(ref Token a, ref Token b)
        {
            if (a.length != b.length)
            {
                return(false);
            }

            return(Cfun._memcmp(a._char_ptr, a.start, b._char_ptr, b.start, a.length));
        }
Beispiel #3
0
        private static void method()
        {
            consume(TokenType.TOKEN_IDENTIFIER, "Expect method name.");
            byte constant = identifierConstant(ref parser.previous);

            FunctionType type = FunctionType.TYPE_METHOD;

            if (parser.previous.length == 4 && Cfun._memcmp(parser.previous._char_ptr, parser.previous.start, "init", 4))
            {
                type = FunctionType.TYPE_INITIALIZER;
            }

            function(type);
            emitBytes((byte)OpCode.OP_METHOD, constant);
        }
Beispiel #4
0
        public static ObjString copyString(char[] chars, int _start, int length)
        {
            uint      hash     = hashString(chars, _start, length);
            ObjString interned = Table.tableFindString(ref VM.vm.strings, chars, _start, length, hash);

            if (interned != null)
            {
                return(interned);
            }

            char[] heapChars = Memory.ALLOCATE <char>(length + 1);
            Cfun._memcpy <char>(heapChars, chars, _start, length);
            heapChars[length] = '\0';

            return(allocateString(heapChars, length, hash));
        }
Beispiel #5
0
        private static void concatenate()
        {
            ObjString b = Object.AS_STRING(peek(0));
            ObjString a = Object.AS_STRING(peek(1));

            int length = a.length + b.length;

            char[] chars = Memory.ALLOCATE <char>(length + 1);
            Cfun._memcpy <char>(chars, a.chars, a._start, a.length);
            Cfun._memcpy <char>(chars, a.length, b.chars, b._start, b.length);
            chars[length] = '\0';

            ObjString result = Object.takeString(chars, 0, length);

            pop();
            pop();
            push(Value.OBJ_VAL(result));
        }
Beispiel #6
0
        private static void number(bool canAssign)
        {
            double value = Cfun._strtod(parser.previous._char_ptr, parser.previous.start, parser.previous.length);

            emitConstant(Value.NUMBER_VAL(value));
        }