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

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

, 12 Oct 2003
Rate this:
Please Sign up or sign in to vote.
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.

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

Share

About the Author

Liu Xue Song
Web Developer
China China
No Biography provided

Comments and Discussions

 
Questionhelp Pinmember2010122204-Oct-12 6:09 
GeneralFehler beim Kompilieren oder Erstellen beim Einfügen PinmemberStebe Franz5-Dec-07 21:54 
General謝謝! PinmemberNewdraw24-Oct-06 16:37 
GeneralBug " x = x + 78 does not work Pinmemberpkgvrpdm28-Apr-05 3:00 
GeneralMinor glitch Pinmemberprcarp4-Jul-04 2:58 
In help_arith, the case for '%' (mod), change:
 
if(result->m_nVariableType != typeVariableFloat || hold->m_nVariableType != typeVariableFloat)
 
to
 
if(result->m_nVariableType != typeVariableInt || hold->m_nVariableType != typeVariableInt)
 
The % operator works only on integers and the actual code in help_arith is correctly using ints.
GeneralOne thing PinmemberOscar-12-Nov-03 19:06 
GeneralRe: One thing PinmemberKronuz22-Dec-03 7:54 
QuestionHave Bug? Pinmemberp1ayer14-Oct-03 23:21 
AnswerRe: Have Bug? PinmemberLiu Xue Song15-Oct-03 14:22 
GeneralThe source of all BASIC ! PinmemberKochise14-Oct-03 4:02 
GeneralThe source of all BASIC 2 ! PinmemberKochise22-Oct-03 4:37 
GeneralNice, but can't read Pinmemberquzi13-Oct-03 4:00 
Generalit 's so good Pinmemberyfmain13-Oct-03 3:00 
GeneralLexx, Yapp and Spirit PinmemberJonathan de Halleux12-Oct-03 21:37 
GeneralRe: Lexx, Yapp and Spirit PinmemberJubjub12-Oct-03 21:49 
GeneralRe: Lexx, Yapp and Spirit PinmemberRede13-Oct-03 0:47 

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

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

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