TreeLang/compiler.cpp

63 lines
1.3 KiB
C++

#include "global.h"
#include <string>
// derived from https://llvm.org/docs/tutorial/MyFirstLanguageFrontend/LangImpl01.html
// any non tokens get to be returned as ascii val from 0 to -255
enum token {
eof, // End of File
external, // C stuff
variable, // Name of a variable
tree, // tree{tree, tree{tree{tree, tree}, tree, tree{tree}}}
integer, // integer ._.
string, // string O_O
};
std::string identifierStr; // filled if string
long numVal; // filled if integer
int getTok() {
int lastChar = ' ';
// skip le whitespace
while( isspace(lastChar) )
lastChar = getchar();
if( isalpha(lastChar) ) { // identifier: [a-zA-Z][a-zA-Z0-9]*
identifierStr = lastChar;
while( isalnum((lastChar = getchar())) )
identifierStr += lastChar;
if( identifierStr == "external" )
return external;
if( isdigit(lastChar) ) {
std::string numStr;
do {
numStr += lastChar;
lastChar = getchar();
} while( isdigit(lastChar) );
numVal = atol(numStr.c_str());
return integer;
}
if( lastChar == '#' ) {
do
lastChar = getchar();
while (lastChar != EOF && lastChar != '\n' && lastChar != '\r');
if (lastChar != EOF)
return getTok();
}
return variable;
}
}
void compiler( char filename[] ) {
getTok();
}