Click here to Skip to main content
15,867,330 members
Articles / Programming Languages / C++
Article

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 227K   4.5K   46   17
A Custom-BASIC language interpreter written in C++

Introduction

In my recent project I needed a smart and light application language , so I wrote MyBasic a custom-BASIC language interpreter, in about 3 days. I think MyBasic has a lot of shortcuts and it is enough for my requirements. From time to time I got code from CodeProject and so now I would like to share my code with you.

Details

This interpreter uses Zoly Farkas's CStringTokenizer as it's tokenizer but it does not work for 16-bit character sets, for example Chinese, and the interpreter needs to locate tokens in any place of the source code. Therefore I made some changes to CStringTokenizer as you can see below :

void Locate(int nLocation);
int GetLocation(){ return m_iPChar;}

void LoadCode(CString &strCode);

private:
inline int GetChar()
{ 
  if(m_iChar == m_sString.GetLength())
    return -1;
  else
    return (unsigned char)m_sString[m_iChar++];
}
CStringTokenizer::CStringTokenizer()
{
  m_bEolIsSignificant = FALSE;
  m_bSlSlComments = TRUE;
  m_bSlStComments = TRUE;
  m_bPascalComments = TRUE;

  Locate(0);

  // Reset syntax
  ResetSyntax();
  // Set the word Chars
  WordChars('a','z');
  WordChars('A','Z');
  WordChars(128,255);
  QuoteChar('"');
  QuoteChar('\'');
  WhiteSpaceChars(0,' ');
  ParseNumbers();
}

void CStringTokenizer::LoadCode(CString &strCode)
{
  m_sString = strCode;
  m_sString += -1;
}

void CStringTokenizer::Locate(int nLocation)
{
  m_bPushedBack = FALSE;
  m_iLineNo = 1;  // the first line
  m_iChar = nLocation;
  m_iPChar= nLocation;
  m_peekc = ' ';
}

In the main loop the interpreter gets the keywords one by one and calls the function accordingly, that's all.

do{
nToken = m_BasicLex.NextToken();
switch(nToken)
{
case TT_INTEGER://LABLE
exec_lable();
break;
case TT_WORD://ASSIGNMENT
m_BasicLex.PushBack();
exec_assignment();
break;
case TT_KW_PRINT://PRINT
exec_print();
break;
case TT_KW_INPUT:
m_BasicLex.PushBack();
exec_default();
break;
case TT_KW_IF://IF
exec_if();
break;
case TT_KW_ENDIF://ENDIF
exec_endif();
break;
case TT_KW_FOR://FOR
exec_for();
break;
case TT_KW_NEXT://NEXT
exec_next();
break;
case TT_KW_GOSUB:
exec_gosub();//gosub
break;
case TT_KW_RETURN://return
exec_return();
break;
case TT_KW_GOTO://GOTO
exec_goto();
break;
case TT_KW_END://END 
m_BasicLex.PushBack();
exec_end();
case TT_EOF:
goto ex;
default://
Error(0,"not except begin of line !");
};
}while(nToken!=TT_EOF);
ex:;

Custom-BASIC keywords

The keywords Custom-BASIC language interpreter supports.

VBScript
PRINT
IF
THEN
ENDIF
FOR
NEXT
TO
GOTO
GOSUB
RETURN
END

Sample BASIC program

VBScript
PRINT "Hello world!\r\nHello to all of you!"

FOR X=1 To 2 
    FOR Y=1 To 2 
      PRINT "X=",X,"Y=",Y 
    NEXT 
NEXT


A = 12.5*2+(100+100)-2^2+4/6 
PRINT A

IF A >= 0 THEN 
GOTO 100 
ENDIF

PRINT "..."

100 PRINT "GOTO 100" 
200 PRINT "GOTO 200..."

GOSUB 300

END

300 PRINT "GOSUB 300" 
RETURN

How to use the code

Include header file in your project and use like this :

#include "Basic.h"
public:
  CBasic m_Basic;
//
// Load source code and parse it .
//

CString strCode(pBuffer);
m_Basic.Init();
m_Basic.Parse(strCode);

Conclusion

All comments and feedback and criticism are welcome. I hope someone can benefit from my interpreter.

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

 
Questionhelp Pin
2010122204-Oct-12 6:09
2010122204-Oct-12 6:09 
SQL
Hello
I want to help to run the interpreter

GeneralFehler beim Kompilieren oder Erstellen beim Einfügen Pin
Stebe Franz5-Dec-07 21:54
Stebe Franz5-Dec-07 21:54 
General謝謝! Pin
Newdraw24-Oct-06 16:37
Newdraw24-Oct-06 16:37 
GeneralBug " x = x + 78 does not work Pin
pkgvrpdm28-Apr-05 3:00
pkgvrpdm28-Apr-05 3:00 
GeneralMinor glitch Pin
prcarp4-Jul-04 2:58
prcarp4-Jul-04 2:58 
GeneralOne thing Pin
2-Nov-03 19:06
suss2-Nov-03 19:06 
GeneralRe: One thing Pin
Kronuz22-Dec-03 7:54
Kronuz22-Dec-03 7:54 
QuestionHave Bug? Pin
p1ayer14-Oct-03 23:21
p1ayer14-Oct-03 23:21 
AnswerRe: Have Bug? Pin
Liu Xue Song15-Oct-03 14:22
Liu Xue Song15-Oct-03 14:22 
GeneralThe source of all BASIC ! Pin
Kochise14-Oct-03 4:02
Kochise14-Oct-03 4:02 
GeneralThe source of all BASIC 2 ! Pin
Kochise22-Oct-03 4:37
Kochise22-Oct-03 4:37 
GeneralRe: The source of all BASIC ! Pin
Southmountain6-Aug-22 7:35
Southmountain6-Aug-22 7:35 
GeneralNice, but can't read Pin
quzi13-Oct-03 4:00
quzi13-Oct-03 4:00 
Generalit 's so good Pin
yfmain13-Oct-03 3:00
yfmain13-Oct-03 3:00 
GeneralLexx, Yapp and Spirit Pin
Jonathan de Halleux12-Oct-03 21:37
Jonathan de Halleux12-Oct-03 21:37 
GeneralRe: Lexx, Yapp and Spirit Pin
Vadim Tabakman12-Oct-03 21:49
Vadim Tabakman12-Oct-03 21:49 
GeneralRe: Lexx, Yapp and Spirit Pin
Rede13-Oct-03 0:47
Rede13-Oct-03 0:47 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.