Click here to Skip to main content
15,886,258 members
Articles / Containers / Virtual Machine

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

Rate me:
Please Sign up or sign in to vote.
4.75/5 (15 votes)
11 Oct 2013LGPL311 min read 56.9K   1.6K   66  
In this article, we will discuss implementing conditional statements, loops and blocks.
#ifndef __BRAINLESS_TAPEMACHINE__
#define __BRAINLESS_TAPEMACHINE__
/*
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 <memory.h>
#include "constNDefs.h"

/*This is a virtual machine  for which we are writing the programing language.*/
template< typename storageType, typename IO, ulong iTapeSize>
class TapeMachine
{
private:
    storageType m_iTapeStore[iTapeSize];
    IO m_inAndout;
    ulong m_iHeadPos;
    const ulong m_iSpecialRegStart;

public:
    TapeMachine():
      m_iHeadPos(0),
      m_iSpecialRegStart( iTapeSize - REGISTER_FILE ),
      m_inAndout()
    {
        memset(m_iTapeStore, 'a'/*HEX_0*/, sizeof(iTapeSize) * DIM(m_iTapeStore));
    }

    void moveHead(ulong num = 1, bool foreward = true)
    {
        if(false == foreward)
        {
            m_iHeadPos -= num;
        }
        else
        {
            m_iHeadPos += num;
        }
    }

    void moveHeadForeward(ulong num = 1)
    {moveHead(num, true);}
    void moveHeadBackward(ulong num = 1)
    {moveHead(num, false);}

    void addWithNext()
    {m_iTapeStore[m_iHeadPos] += m_iTapeStore[m_iHeadPos + 1];}
    void substractFromNext()
    {m_iTapeStore[m_iHeadPos] -= m_iTapeStore[m_iHeadPos + 1];}

    void getCharAndStore();
    void displayChar();
    void displayMultiChar(const ulong);

    void incrementContent()
    {m_iTapeStore[m_iHeadPos] += 1;}
    void decrementContent()
    {m_iTapeStore[m_iHeadPos] -= 1;}

    void setFlagRegister(ulong iFlag)
    {
        m_iTapeStore[m_iSpecialRegStart] &= ~COMPARE_FLAG_MASK;
        m_iTapeStore[m_iSpecialRegStart] |= iFlag;
    }

    ulong getFlagRegister()
    {
        return COMPARE_FLAG_MASK & m_iTapeStore[m_iSpecialRegStart];
    }

    char* getVMVersion()
    {return BCHAR(VM_VERSION);}

    storageType& getCurrentHeadValue();

    storageType& getNthPosValFrmHead(const long  iPos = 0);

    ulong getCurHeadPos()
    {return m_iHeadPos;}

};//End of class TapeMachine


template< typename storageType, typename IO, ulong iTapeSize>
inline void TapeMachine< storageType, IO, iTapeSize >::getCharAndStore()
{
    int val = 0;
    m_inAndout.putStringOnScreen("\nEnter a character>>");
    val = m_inAndout.getChar();
    m_iTapeStore[m_iHeadPos] = (storageType)val;
}

template< typename storageType, typename IO, ulong iTapeSize>
inline void TapeMachine< storageType, IO, iTapeSize >::displayChar()
{
    m_inAndout.printToScreen("\nValue at TapePosition[%d] = %c", m_iHeadPos, (char)m_iTapeStore[m_iHeadPos]);
}

template< typename storageType, typename IO, ulong iTapeSize>
inline storageType& TapeMachine< storageType, IO, iTapeSize >::getCurrentHeadValue()
{
    return getNthPosValFrmHead(/*default is 0*/);
}

template< typename storageType, typename IO, ulong iTapeSize>
inline storageType& TapeMachine< storageType, IO, iTapeSize >::getNthPosValFrmHead(const long iPos)
{
    return m_iTapeStore[m_iHeadPos + iPos];
}

template< typename storageType, typename IO, ulong iTapeSize>
inline void TapeMachine< storageType, IO, iTapeSize >::displayMultiChar(const ulong iSize)
{
    /*TODO improve using m_inAndout*/
	printf("Displaying %d elements::\n", iSize);
    for(ulong i = 0; i < iSize; i++)
    {
        printf("| %c |\n", (char)m_iTapeStore[m_iHeadPos + i]);
    }
	printf("Stop Displaying %d elements::\n", iSize);
}

#endif //__BRAINLESS_TAPEMACHINE__

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)


Written By
Architect
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/BrainlessLabsInc

http://www.luxrender.net/en_GB/authors_contributors - SMISRA

Comments and Discussions