I am still at the video series, but someone from the sdlBasic forums wanted to know if I would make a video on creating a BASIC dialect so I decided to do it. I am having to split it up into multiple videos as it is a lot of info to cover. I have posted the first video here:
https://youtu.be/vmN2HBBXK4Y. Sorry for the bad quality but youtube can seem to keep this video at the quality I recorded it at. Anyone who watches it let me know what you think.
Here is the code from the video:
#include <iostream>
#include <string>
using namespace std;
string keywords[]=
{
"IF",
"THEN",
"ELSE",
"FOR",
"NEXT",
"STEP",
"TO",
"DO",
"LOOP",
"WHILE",
"UNTIL",
"WEND",
"GOTO",
"DEF",
"PRINT",
"INPUT",
"REM",
"END"
};
int keyword_count = 18;
string spChars[]=
{
"^",
"&",
"*",
"(",
")",
"-",
"+",
"=",
"[",
"]",
"\"",
"<",
">",
",",
" "
};
int spChar_count = 15;
string tokens[100];
int token_count = 0;
bool isSpecialCharacter(string c)
{
for(int i = 0; i < spChar_count; i++)
{
if(c.compare(spChars[i])==0)
return true;
}
return false;
}
bool isNumber(string c)
{
if(c.compare("0")==0 || c.compare("1")==0 || c.compare("2")==0 || c.compare("3")==0 || c.compare("4")==0 ||
c.compare("5")==0 || c.compare("6")==0 || c.compare("7")==0 || c.compare("8")==0 || c.compare("9")==0)
return true;
return false;
}
bool tokenizer(string src_line)
{
token_count = 0;
src_line += " ";
string src_token = "";
for(int i = 0; i < src_line.length(); i++)
{
if(!isSpecialCharacter(src_line.substr(i,1)))
{
if(isNumber(src_line.substr(i,1)))
{
src_token = "<num>";
int d = 0;
for(; i < src_line.length(); i++)
{
if(isNumber(src_line.substr(i,1)))
{
src_token += src_line.substr(i,1);
}
else if(src_line.substr(i,1).compare(".")==0)
{
if(d==0)
src_token += ".";
else
{
cout << "Can only have one decimal in a number" << endl;
return false;
}
}
else if(isSpecialCharacter(src_line.substr(i,1)))
{
break;
}
else
{
return false;
}
}
tokens[token_count] = src_token;
token_count++;
src_token = "";
i--;
}
else
{
src_token = "<id>";
for(; i < src_line.length(); i++)
{
if(isSpecialCharacter(src_line.substr(i,1)))
break;
else
src_token += src_line.substr(i,1);
}
tokens[token_count] = src_token;
token_count++;
src_token = "";
i--;
}
}
else if(src_line.substr(i,1).compare("\"")==0)
{
//cout << "DEBUG: " << i << endl;
bool str_close = false;
src_token = "<string>";
for(i=i+1; i < src_line.length(); i++)
{
if(src_line.substr(i,1).compare("\"")==0)
{
//cout << "someting" << endl;
str_close = true;
break;
}
else
src_token += src_line.substr(i,1);
//cout << "str: " << src_line.substr(i,1) << endl;
}
if(str_close == false)
{
cout << "Did not close string" << endl;
return false;
}
tokens[token_count] = src_token;
token_count++;
src_token = "";
}
else if(src_line.substr(i,1).compare("+")==0)
{
tokens[token_count] = "<add>";
token_count++;
}
else if(src_line.substr(i,1).compare("-")==0)
{
tokens[token_count] = "<sub>";
token_count++;
}
else if(src_line.substr(i,1).compare("*")==0)
{
tokens[token_count] = "<mul>";
token_count++;
}
else if(src_line.substr(i,1).compare("/")==0)
{
tokens[token_count] = "<div>";
token_count++;
}
else if(src_line.substr(i,1).compare("^")==0)
{
tokens[token_count] = "<pow>";
token_count++;
}
else if(src_line.substr(i,1).compare("(")==0)
{
tokens[token_count] = "<par_open>";
token_count++;
}
else if(src_line.substr(i,1).compare(")")==0)
{
tokens[token_count] = "<par_close>";
token_count++;
}
else if(src_line.substr(i,1).compare("[")==0)
{
tokens[token_count] = "<square_open>";
token_count++;
}
else if(src_line.substr(i,1).compare("]")==0)
{
tokens[token_count] = "<square_close>";
token_count++;
}
else if(src_line.substr(i,1).compare("=")==0)
{
tokens[token_count] = "<equal>";
token_count++;
}
else if(src_line.substr(i,1).compare("&")==0)
{
tokens[token_count] = "<amp>";
token_count++;
}
else if(src_line.substr(i,1).compare(",")==0)
{
tokens[token_count] = "<sep>";
token_count++;
}
else if(src_line.substr(i,1).compare("<")==0)
{
tokens[token_count] = "<less>";
if(src_line.substr(i).length() > 1)
{
if(src_line.substr(i,2).compare("<=")==0)
tokens[token_count] = "<less_equal>";
else if(src_line.substr(i,2).compare("<>")==0)
tokens[token_count] = "<not_equal>";
}
token_count++;
}
else if(src_line.substr(i,1).compare(">")==0)
{
tokens[token_count] = "<greater>";
if(src_line.substr(i).length() > 1)
{
if(src_line.substr(i,2).compare("<=")==0)
tokens[token_count] = "<greater_equal>";
}
token_count++;
}
}
return true;
}
int main()
{
string src_line = "";
while(src_line.compare("exit")!=0)
{
cout << "->";
getline(cin, src_line);
tokenizer(src_line);
for(int i = 0; i < token_count; i++)
cout << tokens[i] << endl;
cout << endl;
}
return 0;
}