diff --git a/lexer.cpp b/lexer.cpp new file mode 100644 index 0000000..5e27841 --- /dev/null +++ b/lexer.cpp @@ -0,0 +1,84 @@ +#include "global.h" + +// 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 + assignment, // <- + tree, // tree{tree, tree{tree{tree, tree}, tree, tree{tree}}} + integer, // integer ._. + doubleTok, // double o_o + string, // string O_O +}; + +std::string identifierStr; // filled if string +long intVal; // filled if integer +double doubleVal; // filled if decimal + +int getTok() { + int lastChar = ' '; + + // skip le whitespace + while( isspace(lastChar) ) + lastChar = getchar(); + + // letters + if( isalpha(lastChar) ) { + identifierStr = lastChar; + while( isalnum((lastChar = getchar())) ) + identifierStr += lastChar; + + // im not sure what this + if( identifierStr == "external" ) + return external; + + // varble + return variable; + } + + // digits + if( isdigit(lastChar) || lastChar == '.' ) { + std::string numStr; + + // get numbers + do { + numStr += lastChar; + lastChar = getchar(); + } while( isdigit(lastChar) || lastChar == '.' ); + + // numbered + if( numStr.find('.') == std::string::npos ) { + intVal = atol(numStr.c_str()); + return integer; + } else { + doubleVal = strtod(numStr.c_str(), 0); + return doubleTok; + } + } + + // <----------------------------------------------------- + if( lastChar == '<' ) { + if( (lastChar = getchar()) == '-' ) + return assignment; + } + + // comments + if( lastChar == '#' ) { + do + lastChar = getchar(); + while (lastChar != EOF && lastChar != '\n' && lastChar != '\r'); + + if (lastChar != EOF) + return getTok(); + } + + // EOF? + if (lastChar == EOF) + return getTok(); + + // return negative ascii value if none match + int thisChar = lastChar; + lastChar = getchar(); + return -thisChar; +} \ No newline at end of file diff --git a/parser.cpp b/parser.cpp new file mode 100644 index 0000000..dc4f98a --- /dev/null +++ b/parser.cpp @@ -0,0 +1 @@ +#include "global.h"