Skip to content

Commit

Permalink
First Commit
Browse files Browse the repository at this point in the history
  • Loading branch information
doraemonext committed Jan 3, 2016
0 parents commit fdb00c9
Show file tree
Hide file tree
Showing 5 changed files with 104 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
.idea/
7 changes: 7 additions & 0 deletions CMakeLists.txt
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})
64 changes: 64 additions & 0 deletions include/token.h
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
24 changes: 24 additions & 0 deletions include/utils.h
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
8 changes: 8 additions & 0 deletions main.cpp
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;
}

0 comments on commit fdb00c9

Please sign in to comment.