-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit fdb00c9
Showing
5 changed files
with
104 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
.idea/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
cmake_minimum_required(VERSION 3.4) | ||
project(cmm) | ||
|
||
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11") | ||
|
||
set(SOURCE_FILES main.cpp include/token.h include/utils.h) | ||
add_executable(cmm ${SOURCE_FILES}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
#ifndef CMM_TOKEN_H | ||
#define CMM_TOKEN_H | ||
|
||
#include <string> | ||
#include "utils.h" | ||
|
||
// Token 枚举类型 | ||
enum TokenType { | ||
kEOF = 0, // 文件结束 | ||
|
||
kRead, // read | ||
kWrite, // write | ||
kWhile, // while | ||
kIf, // if | ||
kElse, // else | ||
|
||
kInt, // int | ||
kReal, // real | ||
kVoid, // void | ||
kFunc, // func | ||
kReturn, // return | ||
|
||
kLeftParen, // ( | ||
kRightParen, // ) | ||
kLeftBracket, // [ | ||
kRightBracket, // ] | ||
kLeftBrace, // { | ||
kRightBrace, // } | ||
kSemicolon, // ; | ||
kComma, // , | ||
kAssign, // = | ||
kPlus, // + | ||
kMinus, // - | ||
kTimes, // * | ||
kDivide, // / | ||
kLT, // < | ||
kLTE, // <= | ||
kGT, // > | ||
kGTE, // >= | ||
kEqual, // == | ||
kNotEqual, // <> | ||
kLineComment, // // | ||
kLeftBlockComment, // /* | ||
kRightBlockComment, // */ | ||
|
||
kIdentity, // ID | ||
kIntegerLiteral, // int 类型 | ||
kRealLiteral, // real 类型 | ||
kWhiteSpace, // 空白字符类型 | ||
}; | ||
|
||
// Token 类 | ||
class Token { | ||
public: | ||
Token(const TokenType &type, const std::string &content, const Position &position) : | ||
type_(type), content_(content), position_(position) { } | ||
|
||
private: | ||
TokenType type_; // Token 类型 | ||
std::string content_; // Token 内容 | ||
Position position_; // Token 所处位置 | ||
}; | ||
|
||
#endif //CMM_TOKEN_H |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
#ifndef CMM_UTILS_H | ||
#define CMM_UTILS_H | ||
|
||
// 位置类, 用于描述 Token 所在源文件的位置信息, 便于显示出错信息 | ||
class Position { | ||
public: | ||
Position(int row, int col, int length) : row_(row), col_(col), length_(length) { } | ||
|
||
int row() const { return row_; } | ||
void set_row(int row) { row_ = row; } | ||
|
||
int col() const { return col_; } | ||
void set_col(int col) { col_ = col; } | ||
|
||
int length() const { return length_; } | ||
void set_length(int length) { length_ = length; } | ||
|
||
private: | ||
int row_; // 行 | ||
int col_; // 列 | ||
int length_; // 长度 | ||
}; | ||
|
||
#endif //CMM_UTILS_H |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
#include <iostream> | ||
|
||
using namespace std; | ||
|
||
int main() { | ||
cout << "Hello, World!" << endl; | ||
return 0; | ||
} |