Click here to Skip to main content
Click here to Skip to main content

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

By , 12 Oct 2003
 

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.

PRINT
IF
THEN
ENDIF
FOR
NEXT
TO
GOTO
GOSUB
RETURN
END

Sample BASIC program

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

About the Author

Liu Xue Song
Web Developer
China China
Member
No Biography provided

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
Hint: For improved responsiveness ensure Javascript is enabled and choose 'Normal' from the Layout dropdown and hit 'Update'.
You must Sign In to use this message board.
Search this forum  
    Spacing  Noise  Layout  Per page   
Questionhelpmember2010122204 Oct '12 - 6:09 
GeneralFehler beim Kompilieren oder Erstellen beim EinfügenmemberStebe Franz5 Dec '07 - 21:54 
General謝謝!memberNewdraw24 Oct '06 - 16:37 
GeneralBug " x = x + 78 does not workmemberpkgvrpdm28 Apr '05 - 3:00 
GeneralMinor glitchmemberprcarp4 Jul '04 - 2:58 
GeneralOne thingmemberOscar-12 Nov '03 - 19:06 
GeneralRe: One thingmemberKronuz22 Dec '03 - 7:54 
QuestionHave Bug?memberp1ayer14 Oct '03 - 23:21 
AnswerRe: Have Bug?memberLiu Xue Song15 Oct '03 - 14:22 
GeneralThe source of all BASIC !memberKochise14 Oct '03 - 4:02 
GeneralThe source of all BASIC 2 !memberKochise22 Oct '03 - 4:37 
GeneralNice, but can't readmemberquzi13 Oct '03 - 4:00 
Generalit 's so goodmemberyfmain13 Oct '03 - 3:00 
GeneralLexx, Yapp and SpiritmemberJonathan de Halleux12 Oct '03 - 21:37 
GeneralRe: Lexx, Yapp and SpiritmemberJubjub12 Oct '03 - 21:49 
GeneralRe: Lexx, Yapp and SpiritmemberRede13 Oct '03 - 0:47 

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

Permalink | Advertise | Privacy | Mobile
Web01 | 2.6.130516.1 | Last Updated 13 Oct 2003
Article Copyright 2003 by Liu Xue Song
Everything else Copyright © CodeProject, 1999-2013
Terms of Use
Layout: fixed | fluid