Click here to Skip to main content
15,886,873 members
Articles / Programming Languages / C++

MyBasic - A Custom-BASIC language interpreter written in C++

Rate me:
Please Sign up or sign in to vote.
4.75/5 (18 votes)
12 Oct 2003 227.4K   4.5K   46  
A Custom-BASIC language interpreter written in C++
// BasicTokenizer.cpp: implementation of the CBasicTokenizer class.
//
//////////////////////////////////////////////////////////////////////

#include "stdafx.h"
#include "MyBasic.h"
#include "BasicTokenizer.h"

#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif

//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////

CBasicTokenizer::CBasicTokenizer()
{
	m_bPushedBack = FALSE;
	m_Keywords.InitHashTable(MAX_KW);
	LowerCaseMode(TRUE);

	InsertKeyword(CString("print"),TT_KW_PRINT);
	InsertKeyword(CString("input"),TT_KW_INPUT);
	InsertKeyword(CString("if"),TT_KW_IF);
	InsertKeyword(CString("then"),TT_KW_THEN);
	InsertKeyword(CString("endif"),TT_KW_ENDIF);
	InsertKeyword(CString("for"),TT_KW_FOR);
	InsertKeyword(CString("next"),TT_KW_NEXT);
	InsertKeyword(CString("to"),TT_KW_TO);
	InsertKeyword(CString("goto"),TT_KW_GOTO);
	InsertKeyword(CString("gosub"),TT_KW_GOSUB);
	InsertKeyword(CString("return"),TT_KW_RETURN);
	InsertKeyword(CString("end"),TT_KW_END);

}

CBasicTokenizer::~CBasicTokenizer()
{

}


void CBasicTokenizer::InsertKeyword(CString &str, int code)
{
	m_Keywords[str] = code;
}

int CBasicTokenizer::NextToken()
{
	if (m_bPushedBack)
	{
		m_bPushedBack = FALSE;
		return m_tType;
	}
	int code;
	int val = CStringTokenizer::NextToken ();
	if (val == TT_WORD && m_Keywords.Lookup(m_sVal,code))
		return m_tType = code;

	if (val == '<')
		if (CStringTokenizer::NextToken () == '=')
			return m_tType = TT_LWE;
		else
		{
			CStringTokenizer::PushBack();
			if (CStringTokenizer::NextToken () == '>')
				return m_tType = TT_NE;
			else
			{
				CStringTokenizer::PushBack();
				return m_tType =val;
			}
		}
	if (val == '>')
		if (CStringTokenizer::NextToken () == '=')
			return m_tType = TT_GTE;
		else
		{
			CStringTokenizer::PushBack();
			return m_tType =val;
		}
	if (val == '=')
		if (CStringTokenizer::NextToken () == '=')
			return m_tType =TT_IS;
		else
			CStringTokenizer::PushBack();
	return m_tType =val;
}

CString CBasicTokenizer::GetStrValue()
{
	CString ret;
	switch (m_tType)
	{
	case TT_EOF:
		ret = "EOF";
		break;
	case TT_EOL:
		ret = "EOL";
		break;
	case TT_WORD:
		ret = m_sVal;
		break;
	case TT_LWE:
		ret = "<=";
		break;
	case TT_GTE:
		ret = ">=";
		break;
	case TT_IS:
		ret = "==";
		break;
	case TT_NE:
		ret = "<>";
		break;
	case TT_STRING:
		ret = m_sVal;
		break;
	case TT_INTEGER:
	case TT_REAL:
		ret.Format("%f",m_dVal);
		break;
	default:
		ret.Format ( "\'%c\'",(char) m_tType) ;
	}
	return ret;
}


void CBasicTokenizer::PushBack()
{
	m_bPushedBack = TRUE;
}

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 has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Web Developer
China China
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions