Click here to Skip to main content
Click here to Skip to main content
Add your own
alternative version
Go to top

Building a Programming Language: Part II (Adding Conditions, Loop and Blocks to BrainLess)

, 11 Oct 2013
In this article, we will discuss implementing conditional statements, loops and blocks.
/*
Author : Shakti Misra
All are welcome to use these files, distribute, modify and release. But This notice should be included.
This software is given as is, for use and modification.
*/
#include "Helper.h"
#include <ctype.h>
#include <stdlib.h>

int FileRead::getNextCharInternal()
{
    if(NULL == m_pFile)
    {
        m_cCurChar = '\0';
        return m_cCurChar;
    }

/*    if( (0 == m_iCount) && (false == m_bEOFReached) )
    {
        m_iCount = fread(m_szBuffer, sizeof(char), DIM(m_szBuffer) - 1, m_pFile);
        m_iIndex = 0;
        if(m_iCount < DIM(m_szBuffer) - 1)
        {
            m_bEOFReached = (feof(m_pFile) >= 1) ? true : false;
        }
    }

    if(0 < m_iCount)
    {
        m_cCurChar = m_szBuffer[m_iIndex];
        m_szBuffer[--m_iCount, m_iIndex++] = '\0';
    }
    else if(TRUE == m_bEOFReached)
    {
        m_cCurChar = '\0';
    }
*/ //Comment it till we get a better logic for file buffer
    if(false == m_bEOFReached)
    {
        m_cCurChar = getc(m_pFile);
        if(EOF == m_cCurChar)
        {
            m_bEOFReached = (feof(m_pFile) >= 1) ? true : false;
            m_cCurChar = '\0';
        }
    }

    return (char)m_cCurChar;
}

int FileRead::getNextNumber()
{
    char numStr[20] = {0};
    int index = 0;

    while( isdigit( numStr[index++] = (char)getNextCharInternal() ) )
        ;//Empty while loop to get numbers
    numStr[--index] = '\0';

    unreadChar(m_cCurChar);

    return atoi(numStr);
}

bool FileRead::unreadCharInternal(const int ch)
{
/*    if(0 > --m_iIndex)
        return false;

    m_szBuffer[++m_iCount, m_iIndex] = ch;
*/ 
    return (ungetc(ch, m_pFile) ==  ch) ? true : false;
}

long FileRead::getFileIndexInternal()
{
    if(NULL == m_pFile)
        return -1L;// IF the file is not open then no need to add another function call

    return ftell(m_pFile);
}

long FileRead::setRestorePointInternal()
{
    m_restorePoints.insert(pair<long, long int>(m_iNextNum++, getFileIndexInternal()));
    return (m_iNextNum - 1);
}

bool FileRead::positionToRestorePointInternal(const long index, const bool remove)
{
    if(!m_restorePoints.empty())
    {
        fseek(m_pFile, m_restorePoints[index], SEEK_SET);
        if(true == remove)
            m_restorePoints.erase(index);
        return true;
    }

    return false;
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The GNU Lesser General Public License (LGPLv3)

Share

About the Author

BrainlessLabs.com
Engineer
India India
I like to explore different aspects of technology. Try new things, and get delighted. My interests are programming language, and Imaging. But its not hard to work on other things also. Algorithms delight me over a coffee break.
 
I basically code in C++, but JAVA is not so alien for me. I know few scripting languages also. Basically I feel that knowing a programing language is just a matter of getting introduced to it.
 
For my other articles check my blog on homepage:
 
http://brainlesslabs.com/
 
https://github.com/BrainlessLabs
 
http://www.luxrender.net/en_GB/authors_contributors - SMISRA
Follow on   Twitter   LinkedIn

| Advertise | Privacy | Mobile
Web04 | 2.8.140926.1 | Last Updated 11 Oct 2013
Article Copyright 2010 by BrainlessLabs.com
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid