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 "Lexer.h"
/*
Parse the input characters and return the proper token for each. Get character one by one, and
*/

enmTokens Lex::getNextToken()
{   
    switch(getNextChar())
    {
    case '\0':
        m_eCurToken = eTokenOver;break;//End of file reached
    case '<':
        m_eCurToken = eLeftAngleBracket;break;
    case '=':
        m_eCurToken = eEqualTo;break;
    case '\\':
        switch(getNextChar())
        {
        case '>':
            m_eCurToken = eGreaterThan;break;
        case '<':
            if('>' == getNextChar())
            {
                m_eCurToken = eCompareGeneral;
            }
            else
            {
                m_fileReader.unreadChar( getCurrentChar() );
                m_eCurToken = eLessThan;
            }
            break;
        case '=':
            m_eCurToken = eEquality;break;
        default:
            m_fileReader.unreadChar( getCurrentChar() );
            m_eCurToken = eBackSlash;break;
        }
        break;
    case '.':
        m_eCurToken = eDspCurVal;break;//Display value
    case '*':
        m_eCurToken = eStarChar;break;
    /*case '<':
        m_eCurToken = eDecrHead;break;*/
    case '+'://If we have + it can be either ++ or +*. So see where it falls
        switch(getNextChar())
        {
        case '+':
            m_eCurToken = eIncrContent;break;
        case '*':
            m_eCurToken = eAddWithNext;break;
        default:
            m_eCurToken = eUnknown;break;
        }
        break;
    case '-'://If we have - it can be either -- or -*. So see where it falls
        switch(getNextChar())
        {
        case '-':
            m_eCurToken = eDecrContent;break;
        case '*':
            m_eCurToken = eSubNext;break;
        default:
            m_eCurToken = eUnknown;break;
        }
        break;
    case '>'://If we have >, it can be >> if we have single > then its not avalid token 
        switch(getNextChar())
        {
        case '>':
            m_eCurToken = eRdAndStrVal;break;
        default:
            m_fileReader.unreadChar(getCurrentChar());
            m_eCurToken = eRightAngleBracket;break;
        }
        break;
    case '!':
        m_eCurToken = eExclaimationMark;break;
    case '[':
        m_eCurToken = eLeftSqrParen;break;
    case ']':
        m_eCurToken = eRightSqrParen;break;
    case '(':
        m_eCurToken = eLeftParen;break;
    case ')':
        m_eCurToken = eRightParen;break;
    case 'I':
        switch(getNextChar())
        {
        case 'F':
            m_eCurToken = eIF;break;
        default:
            m_fileReader.unreadChar(getCurrentChar());
            m_eCurToken = eUnknown;break;
        }
        break;
    case 'F':
        switch(getNextChar())
        {
        case 'I':
            m_eCurToken = eFI;break;
        default:
            m_fileReader.unreadChar(getCurrentChar());
            m_eCurToken = eUnknown;break;
        }
        break;
    case 'D':
        switch(getNextChar())
        {
        case 'O':
            m_eCurToken = eDO;break;
        default:
            m_fileReader.unreadChar(getCurrentChar());
            m_eCurToken = eUnknown;break;
        }
        break;
    case '#':
        switch(getNextChar())
        {
        case 'B':
            m_eCurToken = eBlock;break;
        default:
            m_fileReader.unreadChar(getCurrentChar());
            m_eCurToken = eUnknown;break;
        }
        break;
    case '/':
        switch(getNextChar())
        {
        case '*':
            while('/' != getNextChar());break;//TODO need to improve it.
        }
        break;
    default:
        if( isdigit( getCurrentChar() ) )
        {
            m_fileReader.unreadChar(getCurrentChar());
            int val = m_fileReader.getNextNumber();
            m_eCurToken = eIntNumber;
            m_tokAttrib.setAttribVal(val);
        }
        else if( isalpha( getCurrentChar() ) )
        {
            char val = getCurrentChar();
            m_eCurToken = eCharVal;
            m_tokAttrib.setAttribVal(val);
        }
        else
        {
            m_eCurToken = eUnknown;
        }
        break;
    }

    return m_eCurToken;
}

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
Web02 | 2.8.140916.1 | Last Updated 11 Oct 2013
Article Copyright 2010 by BrainlessLabs.com
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid