編寫toy.cpp
#include <string>
#include <iostream>
enum Token_type {
EOF_TOKEN = 0, // states the end of file
NUMERIC_TOKEN, // current token is numeric type
IDENTIFIER_TOKEN, // current token is identifier
PARAN_TOKEN, // parenthesis
DEF_TOKEN // states whatever follows is function definition
};
static int Numeric_Val;
static std::string Identifier_string;
FILE *file;
static int get_token() {
static int LastChar = ' ';
while (isspace(LastChar))
LastChar = fgetc(file);
if (isalpha(LastChar)) {
Identifier_string = LastChar;
while (isalnum((LastChar = fgetc(file)))) {
Identifier_string += LastChar;
}
if (Identifier_string == "def") {
return DEF_TOKEN;
}
return IDENTIFIER_TOKEN;
}
if (isdigit(LastChar)) {
std::string NumStr;
do {
NumStr += LastChar;
LastChar = fgetc(file);
} while (isdigit(LastChar));
Numeric_Val = strtod(NumStr.c_str(), 0);
return NUMERIC_TOKEN;
}
if (LastChar == '#') {
do {
LastChar = fgetc(file);
} while (LastChar != EOF && LastChar != '\n'
&& LastChar != '\r');
if (LastChar != EOF) {
return get_token();
}
}
if (LastChar != EOF) {
return EOF_TOKEN;
}
int ThisChar = LastChar;
LastChar = fgetc(file);
return ThisChar;
}
int main(int argc, char* argv[]) {
file = fopen(argv[1], "r");
if (file == NULL) perror("Error opening file");
else {
std::cout << get_token() << std::endl;
}
return 0;
}
編譯
g++ -std=c++11 toy.cpp -o toy
測試代碼source.ty
:
def foo(x, y)
x + y * 16
運(yùn)行
./toy source.ty