Exemplo n.º 1
0
bool CheckIndexRange(bool& bIndex, int& expSourcePtr)
{
    bIndex = false;
    if (g_pElementizer->CheckElement(type_leftb))
    {
        expSourcePtr = g_pElementizer->GetSourcePtr();
        if (!SkipExpression())
        {
            return false;
        }
        if (g_pElementizer->CheckElement(type_dotdot))
        {
            if (!SkipExpression())
            {
                return false;
            }
        }
        if (!g_pElementizer->GetElement(type_rightb))
        {
            return false;
        }
        bIndex = true;
    }
    return true;
}
bool CompileBlock_Repeat(int column)
{
    if (!BlockNest_New(type_repeat, 3))
    {
        return false;
    }

    // determine which type of repeat
    bool (*pCompileFunc)(int, int) = 0;
    int param = 0;
    bool bEof = false;
    if (!g_pElementizer->GetNext(bEof))
    {
        return false;
    }
    if (g_pElementizer->GetType() == type_end)
    {
        // repeat
        pCompileFunc = &CompileRepeatPlain;
        s_bHasPost = false; // assume it doesn't have a post while or until (will be detected)
    }
    else if (g_pElementizer->GetType() == type_while)
    {
        // repeat while <exp>
        pCompileFunc = &CompileRepeatPreWhileOrUntil;
        param = 0x0A;
    }
    else if (g_pElementizer->GetType() == type_until)
    {
        // repeat until <exp>
        pCompileFunc = &CompileRepeatPreWhileOrUntil;
        param = 0x0B;
    }
    else
    {
        g_pElementizer->Backup();
        int savedSourcePtr = g_pElementizer->GetSourcePtr();
        if (!SkipExpression())
        {
            return false;
        }
        if (!g_pElementizer->GetNext(bEof))
        {
            return false;
        }
        g_pElementizer->SetSourcePtr(savedSourcePtr);
        if (g_pElementizer->GetType() == type_end)
        {
            // repeat <exp>
            pCompileFunc = &CompileRepeatCount;
            // redo blocknest type
            BlockNest_Redo(type_repeat_count);
        }
        else
        {
            // repeat var from <exp> to <exp> step <exp>
            pCompileFunc = &CompileRepeatVariable;
        }
    }

    if (!OptimizeBlock(column, param, pCompileFunc))
    {
        return false;
    }

    BlockNest_End();
    return true;
}
bool CompileRepeatVariable(int column, int param)
{
    param = param; // stop warning

    unsigned char varType = 0;
    unsigned char varSize = 0;
    int varAddress = 0;
    int varIndexSourcePtr = 0;
    if (!GetVariable(varType, varSize, varAddress, varIndexSourcePtr))
    {
        return false;
    }

    bool bEof = false;
    if (!g_pElementizer->GetNext(bEof)) // get 'from'
    {
        return false;
    }
    if (g_pElementizer->GetType() != type_from)
    {
        g_pCompilerData->error = true;
        g_pCompilerData->error_msg = g_pErrorStrings[error_efrom];
        return false;
    }
    int fromSourcePtr = g_pElementizer->GetSourcePtr();
    g_pCompilerData->str_enable = false;
    if (!CompileExpression()) // compile 'from' expression (string not allowed)
    {
        return false;
    }
    g_pCompilerData->str_enable = true;

    if (!CompileVariable(1, 0, varType, varSize, varAddress, varIndexSourcePtr)) // compile var write
    {
        return false;
    }
    BlockStack_Write(2, g_pCompilerData->obj_ptr); // set reverse address

    if (!g_pElementizer->GetNext(bEof)) // get 'to'
    {
        return false;
    }
    if (g_pElementizer->GetType() != type_to)
    {
        g_pCompilerData->error = true;
        g_pCompilerData->error_msg = g_pErrorStrings[error_eto];
        return false;
    }
    g_pCompilerData->str_enable = false;
    if (!SkipExpression()) // skip 'to' expression (string not allowed)
    {
        return false;
    }
    g_pCompilerData->str_enable = true;

    if (!g_pElementizer->GetNext(bEof)) // check for 'step'
    {
        return false;
    }
    unsigned char byteCode = 0;
    if (g_pElementizer->GetType() == type_step)
    {
        // handle step
        int savedSourcePtr = g_pElementizer->GetSourcePtr();
        g_pCompilerData->str_enable = false;
        if (!SkipExpression()) // skip 'step' expression (string not allowed)
        {
            return false;
        }
        g_pCompilerData->str_enable = true;
        if (!g_pElementizer->GetElement(type_end))
        {
            return false;
        }
        if (!CompileBlock(column))
        {
            return false;
        }
        BlockStack_Write(0, g_pCompilerData->obj_ptr); // set 'next' address
        if (!CompileOutOfSequenceExpression(savedSourcePtr)) // compile the step expression
        {
            return false;
        }
        byteCode = 0x06; // (repeat-var w/step)
    }
    else if (g_pElementizer->GetType() == type_end)
    {
        // no step, compile block
        if (!CompileBlock(column))
        {
            return false;
        }
        BlockStack_Write(0, g_pCompilerData->obj_ptr); // set 'next' address
        byteCode = 0x02; // (repeat-var)
    }
    else
    {
        g_pCompilerData->error = true;
        g_pCompilerData->error_msg = g_pErrorStrings[error_esoeol];
        return false;
    }

    int savedSourcePtr = g_pElementizer->GetSourcePtr();
    g_pElementizer->SetSourcePtr(fromSourcePtr);
    if (!CompileExpression()) // compile 'from' expression
    {
        return false;
    }
    if (!g_pElementizer->GetNext(bEof)) // skip 'to'
    {
        return false;
    }
    if (!CompileExpression()) // compile 'to' expression
    {
        return false;
    }
    g_pElementizer->SetSourcePtr(savedSourcePtr);
    if (!CompileVariable_Assign(byteCode, varType, varSize, varAddress, varIndexSourcePtr)) // compile repeat-var
    {
        return false;
    }
    if (!BlockStack_CompileAddress(2)) // compile reverse address
    {
        return false;
    }
    BlockStack_Write(1, g_pCompilerData->obj_ptr); // set 'quit'/forward address
    return true;
}