コード例 #1
0
ファイル: main.cpp プロジェクト: LordJZ/exprcmpl
int main(int argc, char** args)
{
    char s[1024+1];
    gets_s(s);

    void* expr;
    int res = ParseExpression(s, 1024, &expr);
    printErr("ParseExpression", res);
    if (res <= 0)
        return 1;

    res = PrintExpression(expr, s, 1024);
    printErr("PrintExpression", res);
    if (res <= 0)
        return 1;
    s[res] = 0;
    printf("%s\n", s);

    uint8 output[1024];
    res = CompileExpression(expr, output, 1024, IdentifierInfoCallback);
    printErr("CompileExpression", res);
    if (res <= 0)
        return 1;

    std::ofstream f("output.bin");
    f.write((char*)output, res);
    f.close();

    res = ReleaseExpression(expr);
    printErr("ReleaseExpression", res);
    if (res <= 0)
        return 1;

    return 0;
}
bool CompileRepeatPreWhileOrUntil(int column, int param)
{
    BlockStack_Write(0, g_pCompilerData->obj_ptr); // set 'next'/reverse address
    if (!CompileExpression()) // compile pre-while/until expression
    {
        return false;
    }
    if (!g_pElementizer->GetElement(type_end))
    {
        return false;
    }
    if (!EnterObj((unsigned char)(param & 0xFF))) // enter the passed in bytecode (jz or jnz)
    {
        return false;
    }
    if (!BlockStack_CompileAddress(1)) // compile forward address
    {
        return false;
    }
    if (!CompileBlock(column)) // compile repeat-while/until block
    {
        return false;
    }
    if (!EnterObj(0x04)) // (jmp)
    {
        return false;
    }
    if (!BlockStack_CompileAddress(0)) // compile reverse address
    {
        return false;
    }
    BlockStack_Write(1, g_pCompilerData->obj_ptr); // set 'quit'/forward address
    return true;
}
コード例 #3
0
dNonDeterministicFiniteAutonata::dNonDeterministicFiniteAutonata(const char* const regularExpression)
	:dFiniteAutomata()
	,m_error(false)
	,m_token (0)
	,m_stateID (0)
	,m_regularExpressionIndex(0)
	,m_startState(NULL) 
	,m_acceptingState(NULL) 
	,m_stack()
{
	CompileExpression(regularExpression);
}
コード例 #4
0
ファイル: CompileUtilities.cpp プロジェクト: redvers/OpenSpin
bool SkipExpression()
{
    int savedObjPtr = g_pCompilerData->obj_ptr;
    bool savedStringPatchEnable = g_pCompilerData->str_patch_enable;
    g_pCompilerData->str_patch_enable = false;
    if (!CompileExpression())
    {
        return false;
    }
    g_pCompilerData->str_patch_enable = savedStringPatchEnable;
    g_pCompilerData->obj_ptr = savedObjPtr;
    return true;
}
bool CompileIfOrIfNot_Condition(int& addressCount, unsigned char byteCode)
{
    if (!CompileExpression())
    {
        return false;
    }
    if (!g_pElementizer->GetElement(type_end))
    {
        return false;
    }
    if (!EnterObj(byteCode))
    {
        return false;
    }
    if (!BlockStack_CompileAddress(addressCount))
    {
        return false;
    }
    return true;
}
bool CompileRepeatCount(int column, int param)
{
    param = param; // stop warning

    if (!CompileExpression()) // compile count expression
    {
        return false;
    }
    if (!g_pElementizer->GetElement(type_end))
    {
        return false;
    }
    if (!EnterObj(0x08)) // (tjz)
    {
        return false;
    }
    if (!BlockStack_CompileAddress(1)) // compile forward address
    {
        return false;
    }
    BlockStack_Write(2, g_pCompilerData->obj_ptr); // set reverse address
    if (!CompileBlock(column)) // compile repeat-count block
    {
        return false;
    }
    BlockStack_Write(0, g_pCompilerData->obj_ptr); // set 'next' address
    if (!EnterObj(0x09)) // (djnz)
    {
        return false;
    }
    if (!BlockStack_CompileAddress(2)) // compile reverse address
    {
        return false;
    }
    BlockStack_Write(1, g_pCompilerData->obj_ptr); // set 'quit'/forward address
    return true;
}
コード例 #7
0
ファイル: WordFilter.cpp プロジェクト: Chero/abcwow
void WordFilter::Load(const char * szTableName)
{
	WordFilterMatch * pMatch;
	size_t i;
	list<WordFilterMatch*> lItems;
	list<WordFilterMatch*>::iterator itr;
	QueryResult * pResult = WorldDatabase.Query("SELECT * FROM %s", szTableName);
	if(pResult==NULL)
		return;

	do 
	{
		pMatch = new WordFilterMatch;
		pMatch->szMatch = (strlen(pResult->Fetch()[0].GetString()) > 1) ? strdup(pResult->Fetch()[0].GetString()) : NULL;
		if(pMatch->szMatch==NULL)
		{
			delete pMatch;
			continue;
		}

		pMatch->szIgnoreMatch = (strlen(pResult->Fetch()[1].GetString()) > 1) ? strdup(pResult->Fetch()[1].GetString()) : NULL;

		// compile the expression
		if(!CompileExpression(pMatch->szMatch, &pMatch->pCompiledExpression, &pMatch->pCompiledExpressionOptions))
		{
			free(pMatch->szMatch);
			if(pMatch->szIgnoreMatch)
				free(pMatch->szIgnoreMatch);
			delete pMatch;
			continue;
		}

		if(pMatch->szIgnoreMatch != NULL)
		{
			if(!CompileExpression(pMatch->szIgnoreMatch, &pMatch->pCompiledIgnoreExpression, &pMatch->pCompiledIgnoreExpressionOptions))
			{
				free(pMatch->szMatch);
				if(pMatch->szIgnoreMatch)
					free(pMatch->szIgnoreMatch);
				delete pMatch;
				continue;
			}
		}
		else
		{
			pMatch->pCompiledIgnoreExpression=NULL;
			pMatch->pCompiledIgnoreExpressionOptions=NULL;
		}

		pMatch->iType = pResult->Fetch()[2].GetUInt32();
		lItems.push_back(pMatch);
	} while (pResult->NextRow());
	delete pResult;

	if(lItems.size()==0)
		return;

	m_filters = new WordFilterMatch*[lItems.size()];
	i = 0;
	for(itr = lItems.begin(); itr != lItems.end(); ++itr)
		m_filters[i++] = (*itr);

	m_filterCount = i;
}
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;
}
bool CompileRepeatPlain(int column, int param)
{
    param = param; // stop warning

    BlockStack_Write(2, g_pCompilerData->obj_ptr); // set revearse address
    if (!s_bHasPost)
    {
        BlockStack_Write(0, g_pCompilerData->obj_ptr); // set plain 'next' address
    }
    if (!CompileBlock(column))
    {
        return false;
    }
    bool bEof = false;
    if (!g_pElementizer->GetNext(bEof))
    {
        return false;
    }
    unsigned char byteCode = 0x04;
    if (!bEof)
    {
        s_column = g_pElementizer->GetColumn();
        if (s_column < column)
        {
            g_pElementizer->Backup();
        }
        else
        {
            // check for post while or until
            int postType = g_pElementizer->GetType();
            if ((postType == type_while) ||
                (postType == type_until))
            {
                s_bHasPost = true;
                BlockStack_Write(0, g_pCompilerData->obj_ptr); // set post-while/until 'next' address
                if (!CompileExpression()) // compile post-while/until expression
                {
                    return false;
                }
                if (!g_pElementizer->GetElement(type_end))
                {
                    return false;
                }
                byteCode = (postType == type_while) ? 0x0B : 0x0A;
            }
            else
            {
                g_pElementizer->Backup();
            }
        }
    }
    if (!EnterObj(byteCode))
    {
        return false;
    }
    if (!BlockStack_CompileAddress(2)) // compile reverse address
    {
        return false;
    }
    BlockStack_Write(1, g_pCompilerData->obj_ptr); // set 'quit' address

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

    if (!BlockStack_CompileConstant())
    {
        return false;
    }
    if (!CompileExpression())
    {
        return false;
    }
    if (!g_pElementizer->GetElement(type_end))
    {
        return false;
    }

    int savedSourcePtr = g_pElementizer->GetSourcePtr();
    int otherSourcePtr = 0;
    bool bOther = false;
    int caseCount = 0;

    bool bEof = false;
    while (!bEof)
    {
        if (!g_pElementizer->GetNext(bEof))
        {
            return false;
        }
        if (bEof)
        {
            break;
        }
        if (g_pElementizer->GetType() == type_end)
        {
            continue;
        }
        s_column = g_pElementizer->GetColumn();
        g_pElementizer->Backup();
        if (s_column <= column)
        {
            break;
        }

        if (bOther) // if we have OTHER: it should have been the last case, so we shouldn't get here again
        {
            g_pCompilerData->error = true;
            g_pCompilerData->error_msg = g_pErrorStrings[error_omblc];
            return false;
        }

        if (g_pElementizer->GetType() == type_other)
        {
            bOther = true;
            if (!g_pElementizer->GetNext(bEof)) // get/skip 'other'
            {
                return false;
            }
            otherSourcePtr = g_pCompilerData->source_start; // save the pointer to the beginning of 'other'
        }
        else
        {
            caseCount++;
            if (caseCount > case_limit)
            {
                g_pCompilerData->error = true;
                g_pCompilerData->error_msg = g_pErrorStrings[error_loxce];
                return false;
            }
            while (1)
            {
                bool bRange = false;
                if (!CompileRange(bRange))
                {
                    return false;
                }
                if (!EnterObj(bRange ? 0x0E : 0x0D)) // enter bytecode for case range or case value into obj
                {
                    return false;
                }
                if (!BlockStack_CompileAddress(caseCount))
                {
                    return false;
                }
                if (!g_pElementizer->CheckElement(type_comma))
                {
                    break;
                }
            }
        }
        if (!g_pElementizer->GetElement(type_colon))
        {
            return false;
        }
        if (!SkipBlock(s_column))
        {
            return false;
        }
    }

    if (caseCount == 0)
    {
        g_pCompilerData->error = true;
        g_pCompilerData->error_msg = g_pErrorStrings[error_nce];
        return false;
    }

    if (bOther)
    {
        // set the source pointer to where the OTHER is at, then get it to set the column
        g_pElementizer->SetSourcePtr(otherSourcePtr);
        if (!g_pElementizer->GetNext(bEof))
        {
            return false;
        }
        int new_column = g_pElementizer->GetColumn();
        // skip the colon
        if (!g_pElementizer->GetNext(bEof))
        {
            return false;
        }
        if (!CompileBlock(new_column))
        {
            return false;
        }
    }
    if (!EnterObj(0x0C)) // casedone, end of range checks
    {
        return false;
    }
    g_pElementizer->SetSourcePtr(savedSourcePtr);
    caseCount = 0;
    bOther = false;
    bEof = false;

    while(!bEof)
    {
        if (!g_pElementizer->GetNext(bEof))
        {
            return false;
        }
        if (bEof)
        {
            break;
        }
        if (g_pElementizer->GetType() == type_end)
        {
            continue;
        }
        s_column = g_pElementizer->GetColumn();
        g_pElementizer->Backup();
        if (s_column <= column)
        {
            break;
        }

        if (g_pElementizer->GetType() == type_other)
        {
            // skip over other, already compiled
            if (!g_pElementizer->GetNext(bEof))
            {
                return false;
            }
            if (!g_pElementizer->GetNext(bEof))
            {
                return false;
            }
            if (!SkipBlock(s_column))
            {
                return false;
            }
        }
        else
        {
            // skip over range/values(s), allready compiled
            while (1)
            {
                if (!SkipRange())
                {
                    return false;
                }
                if (!g_pElementizer->CheckElement(type_comma))
                {
                    break;
                }
            }
            caseCount++;
            BlockStack_Write(caseCount, g_pCompilerData->obj_ptr);
            if (!g_pElementizer->GetElement(type_colon))
            {
                return false;
            }
            if (!CompileBlock(s_column))
            {
                return false;
            }
            if (!EnterObj(0x0C))    // casedone
            {
                return false;
            }
        }
    }
    BlockStack_Write(0, g_pCompilerData->obj_ptr);
    return true;
}