├── .gitattributes ├── .gitignore ├── README.md ├── TinyCompiler.sln └── TinyCompiler ├── KeyWord ├── KeyWord.cpp └── KeyWord.h ├── Profiler ├── Profiler.cpp └── Profiler.h ├── Scanner ├── Scanner.cpp └── Scanner.h ├── StatementNode ├── CheckStatementNode │ ├── CheckStatementNode.h │ └── IFCheckStatementNode.h ├── LoopStatementNode │ ├── FORLoopStatementNode.h │ ├── LoopStatementNode.h │ └── WHILELoopStatementNode.h ├── SquenceStatementNode │ ├── ArithmeticSquenceStatementNode.h │ ├── AssignSquenceStatementNode.h │ ├── DefineSquenceStatementNode.h │ └── SquenceStatementNode.h ├── StatementNode.h ├── StatementNodeAttr.h └── StatementNodeInclude.h ├── TinyCompiler.vcxproj ├── TinyCompiler.vcxproj.filters ├── Token ├── Token.cpp ├── Token.h ├── TokenAttr.cpp └── TokenAttr.h └── main.cpp /.gitattributes: -------------------------------------------------------------------------------- 1 | ############################################################################### 2 | # Set default behavior to automatically normalize line endings. 3 | ############################################################################### 4 | * text=auto 5 | 6 | ############################################################################### 7 | # Set default behavior for command prompt diff. 8 | # 9 | # This is need for earlier builds of msysgit that does not have it on by 10 | # default for csharp files. 11 | # Note: This is only used by command line 12 | ############################################################################### 13 | #*.cs diff=csharp 14 | 15 | ############################################################################### 16 | # Set the merge driver for project and solution files 17 | # 18 | # Merging from the command prompt will add diff markers to the files if there 19 | # are conflicts (Merging from VS is not affected by the settings below, in VS 20 | # the diff markers are never inserted). Diff markers may cause the following 21 | # file extensions to fail to load in VS. An alternative would be to treat 22 | # these files as binary and thus will always conflict and require user 23 | # intervention with every merge. To do so, just uncomment the entries below 24 | ############################################################################### 25 | #*.sln merge=binary 26 | #*.csproj merge=binary 27 | #*.vbproj merge=binary 28 | #*.vcxproj merge=binary 29 | #*.vcproj merge=binary 30 | #*.dbproj merge=binary 31 | #*.fsproj merge=binary 32 | #*.lsproj merge=binary 33 | #*.wixproj merge=binary 34 | #*.modelproj merge=binary 35 | #*.sqlproj merge=binary 36 | #*.wwaproj merge=binary 37 | 38 | ############################################################################### 39 | # behavior for image files 40 | # 41 | # image files are treated as binary by default. 42 | ############################################################################### 43 | #*.jpg binary 44 | #*.png binary 45 | #*.gif binary 46 | 47 | ############################################################################### 48 | # diff behavior for common document formats 49 | # 50 | # Convert binary document formats to text before diffing them. This feature 51 | # is only available from the command line. Turn it on by uncommenting the 52 | # entries below. 53 | ############################################################################### 54 | #*.doc diff=astextplain 55 | #*.DOC diff=astextplain 56 | #*.docx diff=astextplain 57 | #*.DOCX diff=astextplain 58 | #*.dot diff=astextplain 59 | #*.DOT diff=astextplain 60 | #*.pdf diff=astextplain 61 | #*.PDF diff=astextplain 62 | #*.rtf diff=astextplain 63 | #*.RTF diff=astextplain 64 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | ## Ignore Visual Studio temporary files, build results, and 2 | ## files generated by popular Visual Studio add-ons. 3 | 4 | # User-specific files 5 | *.suo 6 | *.user 7 | *.sln.docstates 8 | 9 | # Build results 10 | 11 | [Dd]ebug/ 12 | [Rr]elease/ 13 | x64/ 14 | build/ 15 | [Bb]in/ 16 | [Oo]bj/ 17 | 18 | # Enable "build/" folder in the NuGet Packages folder since NuGet packages use it for MSBuild targets 19 | !packages/*/build/ 20 | 21 | # MSTest test Results 22 | [Tt]est[Rr]esult*/ 23 | [Bb]uild[Ll]og.* 24 | 25 | *_i.c 26 | *_p.c 27 | *.ilk 28 | *.meta 29 | *.obj 30 | *.pch 31 | *.pdb 32 | *.pgc 33 | *.pgd 34 | *.rsp 35 | *.sbr 36 | *.tlb 37 | *.tli 38 | *.tlh 39 | *.tmp 40 | *.tmp_proj 41 | *.log 42 | *.vspscc 43 | *.vssscc 44 | .builds 45 | *.pidb 46 | *.log 47 | *.scc 48 | 49 | # Visual C++ cache files 50 | ipch/ 51 | *.aps 52 | *.ncb 53 | *.opensdf 54 | *.sdf 55 | *.cachefile 56 | 57 | # Visual Studio profiler 58 | *.psess 59 | *.vsp 60 | *.vspx 61 | 62 | # Guidance Automation Toolkit 63 | *.gpState 64 | 65 | # ReSharper is a .NET coding add-in 66 | _ReSharper*/ 67 | *.[Rr]e[Ss]harper 68 | 69 | # TeamCity is a build add-in 70 | _TeamCity* 71 | 72 | # DotCover is a Code Coverage Tool 73 | *.dotCover 74 | 75 | # NCrunch 76 | *.ncrunch* 77 | .*crunch*.local.xml 78 | 79 | # Installshield output folder 80 | [Ee]xpress/ 81 | 82 | # DocProject is a documentation generator add-in 83 | DocProject/buildhelp/ 84 | DocProject/Help/*.HxT 85 | DocProject/Help/*.HxC 86 | DocProject/Help/*.hhc 87 | DocProject/Help/*.hhk 88 | DocProject/Help/*.hhp 89 | DocProject/Help/Html2 90 | DocProject/Help/html 91 | 92 | # Click-Once directory 93 | publish/ 94 | 95 | # Publish Web Output 96 | *.Publish.xml 97 | 98 | # NuGet Packages Directory 99 | ## TODO: If you have NuGet Package Restore enabled, uncomment the next line 100 | #packages/ 101 | 102 | # Windows Azure Build Output 103 | csx 104 | *.build.csdef 105 | 106 | # Windows Store app package directory 107 | AppPackages/ 108 | 109 | # Others 110 | sql/ 111 | *.Cache 112 | ClientBin/ 113 | [Ss]tyle[Cc]op.* 114 | ~$* 115 | *~ 116 | *.dbmdl 117 | *.[Pp]ublish.xml 118 | *.pfx 119 | *.publishsettings 120 | 121 | # RIA/Silverlight projects 122 | Generated_Code/ 123 | 124 | # Backup & report files from converting an old project file to a newer 125 | # Visual Studio version. Backup files are not needed, because we have git ;-) 126 | _UpgradeReport_Files/ 127 | Backup*/ 128 | UpgradeLog*.XML 129 | UpgradeLog*.htm 130 | 131 | # SQL Server files 132 | App_Data/*.mdf 133 | App_Data/*.ldf 134 | 135 | 136 | #LightSwitch generated files 137 | GeneratedArtifacts/ 138 | _Pvt_Extensions/ 139 | ModelManifest.xml 140 | 141 | # ========================= 142 | # Windows detritus 143 | # ========================= 144 | 145 | # Windows image file caches 146 | Thumbs.db 147 | ehthumbs.db 148 | 149 | # Folder config file 150 | Desktop.ini 151 | 152 | # Recycle Bin used on file shares 153 | $RECYCLE.BIN/ 154 | 155 | # Mac desktop service store files 156 | .DS_Store 157 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | TinyCompiler 2 | ============ 3 | ####TinyCompiler是一款能编译一个经过裁剪的C语言的子的简易编译器,采用了C++11编写 4 | 5 | ####编译环境:VS2013或以上 6 | 7 | ####词法说明:在C98的基础上 8 | 9 | 1.不支持声明语句,只支持定义语句 10 | 2.不支持科学计数法 11 | 3.不支持数字加前后缀和八进制、十六进制表示法 12 | 4.字符串中不支持转义 13 | 5.无三元表达式 14 | 15 | 16 | #####词法测试代码(选择了lua源码中的lmem.c文件并经过了删除不符合TinyCompiler词法的部分): 17 |

 18 | void *luaM_growaux_ (lua_State *L, void *block, int *size, size_t size_elems,
 19 |                      int limit, const char *errormsg) {
 20 |   void *newblock;
 21 |   int newsize;
 22 |   if (*size >= limit/2) {  
 23 |     if (*size >= limit)  
 24 |       luaG_runerror(L, errormsg);
 25 |     newsize = limit;  
 26 |   }
 27 |   else {
 28 |     newsize = (*size)*2;
 29 |     if (newsize < MINSIZEARRAY)
 30 |       newsize = MINSIZEARRAY;  
 31 |   }
 32 |   newblock = luaM_reallocv(L, block, *size, newsize, size_elems);
 33 |   *size = newsize; 
 34 |   return newblock;
 35 | }
 36 | void *luaM_toobig (lua_State *L) {
 37 |   luaG_runerror(L, "memory allocation error: block too big");
 38 |   return NULL;  
 39 | }
 40 | void *luaM_realloc_ (lua_State *L, void *block, size_t osize, size_t nsize) {
 41 |   global_State *g = G(L);
 42 |   lua_assert((osize == 0) == (block == NULL));
 43 |   block = (*g->frealloc)(g->ud, block, osize, nsize);
 44 |   if (block == NULL && nsize > 0)
 45 |     luaD_throw(L, LUA_ERRMEM);
 46 |   lua_assert((nsize == 0) == (block == NULL));
 47 |   g->totalbytes = (g->totalbytes - osize) + nsize;
 48 |   return block;
 49 | }
 50 | 
51 | #####分词结果: 52 |

 53 | Token: {name = void attr = KEYWORD loc = 1 file = C:\Users\zxh\Desktop\lmem.c }
 54 | Token: {name = * attr = DELIMITER loc = 1 file = C:\Users\zxh\Desktop\lmem.c }
 55 | Token: {name = luaM_growaux_ attr = VARIABLE loc = 1 file = C:\Users\zxh\Desktop\lmem.c }
 56 | Token: {name = ( attr = DELIMITER loc = 1 file = C:\Users\zxh\Desktop\lmem.c }
 57 | Token: {name = lua_State attr = VARIABLE loc = 1 file = C:\Users\zxh\Desktop\lmem.c }
 58 | Token: {name = * attr = DELIMITER loc = 1 file = C:\Users\zxh\Desktop\lmem.c }
 59 | Token: {name = L attr = VARIABLE loc = 1 file = C:\Users\zxh\Desktop\lmem.c }
 60 | Token: {name = , attr = DELIMITER loc = 1 file = C:\Users\zxh\Desktop\lmem.c }
 61 | Token: {name = void attr = KEYWORD loc = 1 file = C:\Users\zxh\Desktop\lmem.c }
 62 | Token: {name = * attr = DELIMITER loc = 1 file = C:\Users\zxh\Desktop\lmem.c }
 63 | Token: {name = block attr = VARIABLE loc = 1 file = C:\Users\zxh\Desktop\lmem.c }
 64 | Token: {name = , attr = DELIMITER loc = 1 file = C:\Users\zxh\Desktop\lmem.c }
 65 | Token: {name = int attr = KEYWORD loc = 1 file = C:\Users\zxh\Desktop\lmem.c }
 66 | Token: {name = * attr = DELIMITER loc = 1 file = C:\Users\zxh\Desktop\lmem.c }
 67 | Token: {name = size attr = VARIABLE loc = 1 file = C:\Users\zxh\Desktop\lmem.c }
 68 | Token: {name = , attr = DELIMITER loc = 1 file = C:\Users\zxh\Desktop\lmem.c }
 69 | Token: {name = size_t attr = VARIABLE loc = 1 file = C:\Users\zxh\Desktop\lmem.c }
 70 | Token: {name = size_elems attr = VARIABLE loc = 1 file = C:\Users\zxh\Desktop\lmem.c }
 71 | Token: {name = , attr = DELIMITER loc = 1 file = C:\Users\zxh\Desktop\lmem.c }
 72 | Token: {name = int attr = KEYWORD loc = 2 file = C:\Users\zxh\Desktop\lmem.c }
 73 | Token: {name = limit attr = VARIABLE loc = 2 file = C:\Users\zxh\Desktop\lmem.c }
 74 | Token: {name = , attr = DELIMITER loc = 2 file = C:\Users\zxh\Desktop\lmem.c }
 75 | Token: {name = const attr = KEYWORD loc = 2 file = C:\Users\zxh\Desktop\lmem.c }
 76 | Token: {name = char attr = KEYWORD loc = 2 file = C:\Users\zxh\Desktop\lmem.c }
 77 | Token: {name = * attr = DELIMITER loc = 2 file = C:\Users\zxh\Desktop\lmem.c }
 78 | Token: {name = errormsg attr = VARIABLE loc = 2 file = C:\Users\zxh\Desktop\lmem.c }
 79 | Token: {name = ) attr = DELIMITER loc = 2 file = C:\Users\zxh\Desktop\lmem.c }
 80 | Token: {name = { attr = DELIMITER loc = 2 file = C:\Users\zxh\Desktop\lmem.c }
 81 | Token: {name = void attr = KEYWORD loc = 3 file = C:\Users\zxh\Desktop\lmem.c }
 82 | Token: {name = * attr = DELIMITER loc = 3 file = C:\Users\zxh\Desktop\lmem.c }
 83 | Token: {name = newblock attr = VARIABLE loc = 3 file = C:\Users\zxh\Desktop\lmem.c }
 84 | Token: {name = ; attr = DELIMITER loc = 3 file = C:\Users\zxh\Desktop\lmem.c }
 85 | Token: {name = int attr = KEYWORD loc = 4 file = C:\Users\zxh\Desktop\lmem.c }
 86 | Token: {name = newsize attr = VARIABLE loc = 4 file = C:\Users\zxh\Desktop\lmem.c }
 87 | Token: {name = ; attr = DELIMITER loc = 4 file = C:\Users\zxh\Desktop\lmem.c }
 88 | Token: {name = if attr = KEYWORD loc = 5 file = C:\Users\zxh\Desktop\lmem.c }
 89 | Token: {name = ( attr = DELIMITER loc = 5 file = C:\Users\zxh\Desktop\lmem.c }
 90 | Token: {name = * attr = DELIMITER loc = 5 file = C:\Users\zxh\Desktop\lmem.c }
 91 | Token: {name = size attr = VARIABLE loc = 5 file = C:\Users\zxh\Desktop\lmem.c }
 92 | Token: {name = >= attr = DELIMITER loc = 5 file = C:\Users\zxh\Desktop\lmem.c }
 93 | Token: {name = limit attr = VARIABLE loc = 5 file = C:\Users\zxh\Desktop\lmem.c }
 94 | Token: {name = / attr = DELIMITER loc = 5 file = C:\Users\zxh\Desktop\lmem.c }
 95 | Token: {name = 2 attr = INTEGER loc = 5 file = C:\Users\zxh\Desktop\lmem.c }
 96 | Token: {name = ) attr = DELIMITER loc = 5 file = C:\Users\zxh\Desktop\lmem.c }
 97 | Token: {name = { attr = DELIMITER loc = 5 file = C:\Users\zxh\Desktop\lmem.c }
 98 | Token: {name = if attr = KEYWORD loc = 6 file = C:\Users\zxh\Desktop\lmem.c }
 99 | Token: {name = ( attr = DELIMITER loc = 6 file = C:\Users\zxh\Desktop\lmem.c }
100 | Token: {name = * attr = DELIMITER loc = 6 file = C:\Users\zxh\Desktop\lmem.c }
101 | Token: {name = size attr = VARIABLE loc = 6 file = C:\Users\zxh\Desktop\lmem.c }
102 | Token: {name = >= attr = DELIMITER loc = 6 file = C:\Users\zxh\Desktop\lmem.c }
103 | Token: {name = limit attr = VARIABLE loc = 6 file = C:\Users\zxh\Desktop\lmem.c }
104 | Token: {name = ) attr = DELIMITER loc = 6 file = C:\Users\zxh\Desktop\lmem.c }
105 | Token: {name = luaG_runerror attr = VARIABLE loc = 7 file = C:\Users\zxh\Desktop\lmem.c }
106 | Token: {name = ( attr = DELIMITER loc = 7 file = C:\Users\zxh\Desktop\lmem.c }
107 | Token: {name = L attr = VARIABLE loc = 7 file = C:\Users\zxh\Desktop\lmem.c }
108 | Token: {name = , attr = DELIMITER loc = 7 file = C:\Users\zxh\Desktop\lmem.c }
109 | Token: {name = errormsg attr = VARIABLE loc = 7 file = C:\Users\zxh\Desktop\lmem.c }
110 | Token: {name = ) attr = DELIMITER loc = 7 file = C:\Users\zxh\Desktop\lmem.c }
111 | Token: {name = ; attr = DELIMITER loc = 7 file = C:\Users\zxh\Desktop\lmem.c }
112 | Token: {name = newsize attr = VARIABLE loc = 8 file = C:\Users\zxh\Desktop\lmem.c }
113 | Token: {name = = attr = DELIMITER loc = 8 file = C:\Users\zxh\Desktop\lmem.c }
114 | Token: {name = limit attr = VARIABLE loc = 8 file = C:\Users\zxh\Desktop\lmem.c }
115 | Token: {name = ; attr = DELIMITER loc = 8 file = C:\Users\zxh\Desktop\lmem.c }
116 | Token: {name = } attr = DELIMITER loc = 9 file = C:\Users\zxh\Desktop\lmem.c }
117 | Token: {name = else attr = KEYWORD loc = 10 file = C:\Users\zxh\Desktop\lmem.c }
118 | Token: {name = { attr = DELIMITER loc = 10 file = C:\Users\zxh\Desktop\lmem.c }
119 | Token: {name = newsize attr = VARIABLE loc = 11 file = C:\Users\zxh\Desktop\lmem.c }
120 | Token: {name = = attr = DELIMITER loc = 11 file = C:\Users\zxh\Desktop\lmem.c }
121 | Token: {name = ( attr = DELIMITER loc = 11 file = C:\Users\zxh\Desktop\lmem.c }
122 | Token: {name = * attr = DELIMITER loc = 11 file = C:\Users\zxh\Desktop\lmem.c }
123 | Token: {name = size attr = VARIABLE loc = 11 file = C:\Users\zxh\Desktop\lmem.c }
124 | Token: {name = ) attr = DELIMITER loc = 11 file = C:\Users\zxh\Desktop\lmem.c }
125 | Token: {name = * attr = DELIMITER loc = 11 file = C:\Users\zxh\Desktop\lmem.c }
126 | Token: {name = 2 attr = INTEGER loc = 11 file = C:\Users\zxh\Desktop\lmem.c }
127 | Token: {name = ; attr = DELIMITER loc = 11 file = C:\Users\zxh\Desktop\lmem.c }
128 | Token: {name = if attr = KEYWORD loc = 12 file = C:\Users\zxh\Desktop\lmem.c }
129 | Token: {name = ( attr = DELIMITER loc = 12 file = C:\Users\zxh\Desktop\lmem.c }
130 | Token: {name = newsize attr = VARIABLE loc = 12 file = C:\Users\zxh\Desktop\lmem.c }
131 | Token: {name = < attr = DELIMITER loc = 12 file = C:\Users\zxh\Desktop\lmem.c }
132 | Token: {name = MINSIZEARRAY attr = VARIABLE loc = 12 file = C:\Users\zxh\Desktop\lmem.c }
133 | Token: {name = ) attr = DELIMITER loc = 12 file = C:\Users\zxh\Desktop\lmem.c }
134 | Token: {name = newsize attr = VARIABLE loc = 13 file = C:\Users\zxh\Desktop\lmem.c }
135 | Token: {name = = attr = DELIMITER loc = 13 file = C:\Users\zxh\Desktop\lmem.c }
136 | Token: {name = MINSIZEARRAY attr = VARIABLE loc = 13 file = C:\Users\zxh\Desktop\lmem.c }
137 | Token: {name = ; attr = DELIMITER loc = 13 file = C:\Users\zxh\Desktop\lmem.c }
138 | Token: {name = } attr = DELIMITER loc = 14 file = C:\Users\zxh\Desktop\lmem.c }
139 | Token: {name = newblock attr = VARIABLE loc = 15 file = C:\Users\zxh\Desktop\lmem.c }
140 | Token: {name = = attr = DELIMITER loc = 15 file = C:\Users\zxh\Desktop\lmem.c }
141 | Token: {name = luaM_reallocv attr = VARIABLE loc = 15 file = C:\Users\zxh\Desktop\lmem.c }
142 | Token: {name = ( attr = DELIMITER loc = 15 file = C:\Users\zxh\Desktop\lmem.c }
143 | Token: {name = L attr = VARIABLE loc = 15 file = C:\Users\zxh\Desktop\lmem.c }
144 | Token: {name = , attr = DELIMITER loc = 15 file = C:\Users\zxh\Desktop\lmem.c }
145 | Token: {name = block attr = VARIABLE loc = 15 file = C:\Users\zxh\Desktop\lmem.c }
146 | Token: {name = , attr = DELIMITER loc = 15 file = C:\Users\zxh\Desktop\lmem.c }
147 | Token: {name = * attr = DELIMITER loc = 15 file = C:\Users\zxh\Desktop\lmem.c }
148 | Token: {name = size attr = VARIABLE loc = 15 file = C:\Users\zxh\Desktop\lmem.c }
149 | Token: {name = , attr = DELIMITER loc = 15 file = C:\Users\zxh\Desktop\lmem.c }
150 | Token: {name = newsize attr = VARIABLE loc = 15 file = C:\Users\zxh\Desktop\lmem.c }
151 | Token: {name = , attr = DELIMITER loc = 15 file = C:\Users\zxh\Desktop\lmem.c }
152 | Token: {name = size_elems attr = VARIABLE loc = 15 file = C:\Users\zxh\Desktop\lmem.c }
153 | Token: {name = ) attr = DELIMITER loc = 15 file = C:\Users\zxh\Desktop\lmem.c }
154 | Token: {name = ; attr = DELIMITER loc = 15 file = C:\Users\zxh\Desktop\lmem.c }
155 | Token: {name = * attr = DELIMITER loc = 16 file = C:\Users\zxh\Desktop\lmem.c }
156 | Token: {name = size attr = VARIABLE loc = 16 file = C:\Users\zxh\Desktop\lmem.c }
157 | Token: {name = = attr = DELIMITER loc = 16 file = C:\Users\zxh\Desktop\lmem.c }
158 | Token: {name = newsize attr = VARIABLE loc = 16 file = C:\Users\zxh\Desktop\lmem.c }
159 | Token: {name = ; attr = DELIMITER loc = 16 file = C:\Users\zxh\Desktop\lmem.c }
160 | Token: {name = return attr = KEYWORD loc = 17 file = C:\Users\zxh\Desktop\lmem.c }
161 | Token: {name = newblock attr = VARIABLE loc = 17 file = C:\Users\zxh\Desktop\lmem.c }
162 | Token: {name = ; attr = DELIMITER loc = 17 file = C:\Users\zxh\Desktop\lmem.c }
163 | Token: {name = } attr = DELIMITER loc = 18 file = C:\Users\zxh\Desktop\lmem.c }
164 | Token: {name = void attr = KEYWORD loc = 19 file = C:\Users\zxh\Desktop\lmem.c }
165 | Token: {name = * attr = DELIMITER loc = 19 file = C:\Users\zxh\Desktop\lmem.c }
166 | Token: {name = luaM_toobig attr = VARIABLE loc = 19 file = C:\Users\zxh\Desktop\lmem.c }
167 | Token: {name = ( attr = DELIMITER loc = 19 file = C:\Users\zxh\Desktop\lmem.c }
168 | Token: {name = lua_State attr = VARIABLE loc = 19 file = C:\Users\zxh\Desktop\lmem.c }
169 | Token: {name = * attr = DELIMITER loc = 19 file = C:\Users\zxh\Desktop\lmem.c }
170 | Token: {name = L attr = VARIABLE loc = 19 file = C:\Users\zxh\Desktop\lmem.c }
171 | Token: {name = ) attr = DELIMITER loc = 19 file = C:\Users\zxh\Desktop\lmem.c }
172 | Token: {name = { attr = DELIMITER loc = 19 file = C:\Users\zxh\Desktop\lmem.c }
173 | Token: {name = luaG_runerror attr = VARIABLE loc = 20 file = C:\Users\zxh\Desktop\lmem.c }
174 | Token: {name = ( attr = DELIMITER loc = 20 file = C:\Users\zxh\Desktop\lmem.c }
175 | Token: {name = L attr = VARIABLE loc = 20 file = C:\Users\zxh\Desktop\lmem.c }
176 | Token: {name = , attr = DELIMITER loc = 20 file = C:\Users\zxh\Desktop\lmem.c }
177 | Token: {name = "memory allocation error: block too big" attr = STRING loc = 20 file = C:\Users\zxh\Desktop\lmem.c }
178 | Token: {name = ) attr = DELIMITER loc = 20 file = C:\Users\zxh\Desktop\lmem.c }
179 | Token: {name = ; attr = DELIMITER loc = 20 file = C:\Users\zxh\Desktop\lmem.c }
180 | Token: {name = return attr = KEYWORD loc = 21 file = C:\Users\zxh\Desktop\lmem.c }
181 | Token: {name = NULL attr = VARIABLE loc = 21 file = C:\Users\zxh\Desktop\lmem.c }
182 | Token: {name = ; attr = DELIMITER loc = 21 file = C:\Users\zxh\Desktop\lmem.c }
183 | Token: {name = } attr = DELIMITER loc = 22 file = C:\Users\zxh\Desktop\lmem.c }
184 | Token: {name = void attr = KEYWORD loc = 23 file = C:\Users\zxh\Desktop\lmem.c }
185 | Token: {name = * attr = DELIMITER loc = 23 file = C:\Users\zxh\Desktop\lmem.c }
186 | Token: {name = luaM_realloc_ attr = VARIABLE loc = 23 file = C:\Users\zxh\Desktop\lmem.c }
187 | Token: {name = ( attr = DELIMITER loc = 23 file = C:\Users\zxh\Desktop\lmem.c }
188 | Token: {name = lua_State attr = VARIABLE loc = 23 file = C:\Users\zxh\Desktop\lmem.c }
189 | Token: {name = * attr = DELIMITER loc = 23 file = C:\Users\zxh\Desktop\lmem.c }
190 | Token: {name = L attr = VARIABLE loc = 23 file = C:\Users\zxh\Desktop\lmem.c }
191 | Token: {name = , attr = DELIMITER loc = 23 file = C:\Users\zxh\Desktop\lmem.c }
192 | Token: {name = void attr = KEYWORD loc = 23 file = C:\Users\zxh\Desktop\lmem.c }
193 | Token: {name = * attr = DELIMITER loc = 23 file = C:\Users\zxh\Desktop\lmem.c }
194 | Token: {name = block attr = VARIABLE loc = 23 file = C:\Users\zxh\Desktop\lmem.c }
195 | Token: {name = , attr = DELIMITER loc = 23 file = C:\Users\zxh\Desktop\lmem.c }
196 | Token: {name = size_t attr = VARIABLE loc = 23 file = C:\Users\zxh\Desktop\lmem.c }
197 | Token: {name = osize attr = VARIABLE loc = 23 file = C:\Users\zxh\Desktop\lmem.c }
198 | Token: {name = , attr = DELIMITER loc = 23 file = C:\Users\zxh\Desktop\lmem.c }
199 | Token: {name = size_t attr = VARIABLE loc = 23 file = C:\Users\zxh\Desktop\lmem.c }
200 | Token: {name = nsize attr = VARIABLE loc = 23 file = C:\Users\zxh\Desktop\lmem.c }
201 | Token: {name = ) attr = DELIMITER loc = 23 file = C:\Users\zxh\Desktop\lmem.c }
202 | Token: {name = { attr = DELIMITER loc = 23 file = C:\Users\zxh\Desktop\lmem.c }
203 | Token: {name = global_State attr = VARIABLE loc = 24 file = C:\Users\zxh\Desktop\lmem.c }
204 | Token: {name = * attr = DELIMITER loc = 24 file = C:\Users\zxh\Desktop\lmem.c }
205 | Token: {name = g attr = VARIABLE loc = 24 file = C:\Users\zxh\Desktop\lmem.c }
206 | Token: {name = = attr = DELIMITER loc = 24 file = C:\Users\zxh\Desktop\lmem.c }
207 | Token: {name = G attr = VARIABLE loc = 24 file = C:\Users\zxh\Desktop\lmem.c }
208 | Token: {name = ( attr = DELIMITER loc = 24 file = C:\Users\zxh\Desktop\lmem.c }
209 | Token: {name = L attr = VARIABLE loc = 24 file = C:\Users\zxh\Desktop\lmem.c }
210 | Token: {name = ) attr = DELIMITER loc = 24 file = C:\Users\zxh\Desktop\lmem.c }
211 | Token: {name = ; attr = DELIMITER loc = 24 file = C:\Users\zxh\Desktop\lmem.c }
212 | Token: {name = lua_assert attr = VARIABLE loc = 25 file = C:\Users\zxh\Desktop\lmem.c }
213 | Token: {name = ( attr = DELIMITER loc = 25 file = C:\Users\zxh\Desktop\lmem.c }
214 | Token: {name = ( attr = DELIMITER loc = 25 file = C:\Users\zxh\Desktop\lmem.c }
215 | Token: {name = osize attr = VARIABLE loc = 25 file = C:\Users\zxh\Desktop\lmem.c }
216 | Token: {name = == attr = DELIMITER loc = 25 file = C:\Users\zxh\Desktop\lmem.c }
217 | Token: {name = 0 attr = INTEGER loc = 25 file = C:\Users\zxh\Desktop\lmem.c }
218 | Token: {name = ) attr = DELIMITER loc = 25 file = C:\Users\zxh\Desktop\lmem.c }
219 | Token: {name = == attr = DELIMITER loc = 25 file = C:\Users\zxh\Desktop\lmem.c }
220 | Token: {name = ( attr = DELIMITER loc = 25 file = C:\Users\zxh\Desktop\lmem.c }
221 | Token: {name = block attr = VARIABLE loc = 25 file = C:\Users\zxh\Desktop\lmem.c }
222 | Token: {name = == attr = DELIMITER loc = 25 file = C:\Users\zxh\Desktop\lmem.c }
223 | Token: {name = NULL attr = VARIABLE loc = 25 file = C:\Users\zxh\Desktop\lmem.c }
224 | Token: {name = ) attr = DELIMITER loc = 25 file = C:\Users\zxh\Desktop\lmem.c }
225 | Token: {name = ) attr = DELIMITER loc = 25 file = C:\Users\zxh\Desktop\lmem.c }
226 | Token: {name = ; attr = DELIMITER loc = 25 file = C:\Users\zxh\Desktop\lmem.c }
227 | Token: {name = block attr = VARIABLE loc = 26 file = C:\Users\zxh\Desktop\lmem.c }
228 | Token: {name = = attr = DELIMITER loc = 26 file = C:\Users\zxh\Desktop\lmem.c }
229 | Token: {name = ( attr = DELIMITER loc = 26 file = C:\Users\zxh\Desktop\lmem.c }
230 | Token: {name = * attr = DELIMITER loc = 26 file = C:\Users\zxh\Desktop\lmem.c }
231 | Token: {name = g attr = VARIABLE loc = 26 file = C:\Users\zxh\Desktop\lmem.c }
232 | Token: {name = -> attr = DELIMITER loc = 26 file = C:\Users\zxh\Desktop\lmem.c }
233 | Token: {name = frealloc attr = VARIABLE loc = 26 file = C:\Users\zxh\Desktop\lmem.c }
234 | Token: {name = ) attr = DELIMITER loc = 26 file = C:\Users\zxh\Desktop\lmem.c }
235 | Token: {name = ( attr = DELIMITER loc = 26 file = C:\Users\zxh\Desktop\lmem.c }
236 | Token: {name = g attr = VARIABLE loc = 26 file = C:\Users\zxh\Desktop\lmem.c }
237 | Token: {name = -> attr = DELIMITER loc = 26 file = C:\Users\zxh\Desktop\lmem.c }
238 | Token: {name = ud attr = VARIABLE loc = 26 file = C:\Users\zxh\Desktop\lmem.c }
239 | Token: {name = , attr = DELIMITER loc = 26 file = C:\Users\zxh\Desktop\lmem.c }
240 | Token: {name = block attr = VARIABLE loc = 26 file = C:\Users\zxh\Desktop\lmem.c }
241 | Token: {name = , attr = DELIMITER loc = 26 file = C:\Users\zxh\Desktop\lmem.c }
242 | Token: {name = osize attr = VARIABLE loc = 26 file = C:\Users\zxh\Desktop\lmem.c }
243 | Token: {name = , attr = DELIMITER loc = 26 file = C:\Users\zxh\Desktop\lmem.c }
244 | Token: {name = nsize attr = VARIABLE loc = 26 file = C:\Users\zxh\Desktop\lmem.c }
245 | Token: {name = ) attr = DELIMITER loc = 26 file = C:\Users\zxh\Desktop\lmem.c }
246 | Token: {name = ; attr = DELIMITER loc = 26 file = C:\Users\zxh\Desktop\lmem.c }
247 | Token: {name = if attr = KEYWORD loc = 27 file = C:\Users\zxh\Desktop\lmem.c }
248 | Token: {name = ( attr = DELIMITER loc = 27 file = C:\Users\zxh\Desktop\lmem.c }
249 | Token: {name = block attr = VARIABLE loc = 27 file = C:\Users\zxh\Desktop\lmem.c }
250 | Token: {name = == attr = DELIMITER loc = 27 file = C:\Users\zxh\Desktop\lmem.c }
251 | Token: {name = NULL attr = VARIABLE loc = 27 file = C:\Users\zxh\Desktop\lmem.c }
252 | Token: {name = && attr = DELIMITER loc = 27 file = C:\Users\zxh\Desktop\lmem.c }
253 | Token: {name = nsize attr = VARIABLE loc = 27 file = C:\Users\zxh\Desktop\lmem.c }
254 | Token: {name = > attr = DELIMITER loc = 27 file = C:\Users\zxh\Desktop\lmem.c }
255 | Token: {name = 0 attr = INTEGER loc = 27 file = C:\Users\zxh\Desktop\lmem.c }
256 | Token: {name = ) attr = DELIMITER loc = 27 file = C:\Users\zxh\Desktop\lmem.c }
257 | Token: {name = luaD_throw attr = VARIABLE loc = 28 file = C:\Users\zxh\Desktop\lmem.c }
258 | Token: {name = ( attr = DELIMITER loc = 28 file = C:\Users\zxh\Desktop\lmem.c }
259 | Token: {name = L attr = VARIABLE loc = 28 file = C:\Users\zxh\Desktop\lmem.c }
260 | Token: {name = , attr = DELIMITER loc = 28 file = C:\Users\zxh\Desktop\lmem.c }
261 | Token: {name = LUA_ERRMEM attr = VARIABLE loc = 28 file = C:\Users\zxh\Desktop\lmem.c }
262 | Token: {name = ) attr = DELIMITER loc = 28 file = C:\Users\zxh\Desktop\lmem.c }
263 | Token: {name = ; attr = DELIMITER loc = 28 file = C:\Users\zxh\Desktop\lmem.c }
264 | Token: {name = lua_assert attr = VARIABLE loc = 29 file = C:\Users\zxh\Desktop\lmem.c }
265 | Token: {name = ( attr = DELIMITER loc = 29 file = C:\Users\zxh\Desktop\lmem.c }
266 | Token: {name = ( attr = DELIMITER loc = 29 file = C:\Users\zxh\Desktop\lmem.c }
267 | Token: {name = nsize attr = VARIABLE loc = 29 file = C:\Users\zxh\Desktop\lmem.c }
268 | Token: {name = == attr = DELIMITER loc = 29 file = C:\Users\zxh\Desktop\lmem.c }
269 | Token: {name = 0 attr = INTEGER loc = 29 file = C:\Users\zxh\Desktop\lmem.c }
270 | Token: {name = ) attr = DELIMITER loc = 29 file = C:\Users\zxh\Desktop\lmem.c }
271 | Token: {name = == attr = DELIMITER loc = 29 file = C:\Users\zxh\Desktop\lmem.c }
272 | Token: {name = ( attr = DELIMITER loc = 29 file = C:\Users\zxh\Desktop\lmem.c }
273 | Token: {name = block attr = VARIABLE loc = 29 file = C:\Users\zxh\Desktop\lmem.c }
274 | Token: {name = == attr = DELIMITER loc = 29 file = C:\Users\zxh\Desktop\lmem.c }
275 | Token: {name = NULL attr = VARIABLE loc = 29 file = C:\Users\zxh\Desktop\lmem.c }
276 | Token: {name = ) attr = DELIMITER loc = 29 file = C:\Users\zxh\Desktop\lmem.c }
277 | Token: {name = ) attr = DELIMITER loc = 29 file = C:\Users\zxh\Desktop\lmem.c }
278 | Token: {name = ; attr = DELIMITER loc = 29 file = C:\Users\zxh\Desktop\lmem.c }
279 | Token: {name = g attr = VARIABLE loc = 30 file = C:\Users\zxh\Desktop\lmem.c }
280 | Token: {name = -> attr = DELIMITER loc = 30 file = C:\Users\zxh\Desktop\lmem.c }
281 | Token: {name = totalbytes attr = VARIABLE loc = 30 file = C:\Users\zxh\Desktop\lmem.c }
282 | Token: {name = = attr = DELIMITER loc = 30 file = C:\Users\zxh\Desktop\lmem.c }
283 | Token: {name = ( attr = DELIMITER loc = 30 file = C:\Users\zxh\Desktop\lmem.c }
284 | Token: {name = g attr = VARIABLE loc = 30 file = C:\Users\zxh\Desktop\lmem.c }
285 | Token: {name = -> attr = DELIMITER loc = 30 file = C:\Users\zxh\Desktop\lmem.c }
286 | Token: {name = totalbytes attr = VARIABLE loc = 30 file = C:\Users\zxh\Desktop\lmem.c }
287 | Token: {name = - attr = DELIMITER loc = 30 file = C:\Users\zxh\Desktop\lmem.c }
288 | Token: {name = osize attr = VARIABLE loc = 30 file = C:\Users\zxh\Desktop\lmem.c }
289 | Token: {name = ) attr = DELIMITER loc = 30 file = C:\Users\zxh\Desktop\lmem.c }
290 | Token: {name = + attr = DELIMITER loc = 30 file = C:\Users\zxh\Desktop\lmem.c }
291 | Token: {name = nsize attr = VARIABLE loc = 30 file = C:\Users\zxh\Desktop\lmem.c }
292 | Token: {name = ; attr = DELIMITER loc = 30 file = C:\Users\zxh\Desktop\lmem.c }
293 | Token: {name = return attr = KEYWORD loc = 31 file = C:\Users\zxh\Desktop\lmem.c }
294 | Token: {name = block attr = VARIABLE loc = 31 file = C:\Users\zxh\Desktop\lmem.c }
295 | Token: {name = ; attr = DELIMITER loc = 31 file = C:\Users\zxh\Desktop\lmem.c }
296 | Token: {name = } attr = DELIMITER loc = 32 file = C:\Users\zxh\Desktop\lmem.c }
297 | 
298 | -------------------------------------------------------------------------------- /TinyCompiler.sln: -------------------------------------------------------------------------------- 1 |  2 | Microsoft Visual Studio Solution File, Format Version 12.00 3 | # Visual Studio Express 2013 for Windows Desktop 4 | VisualStudioVersion = 12.0.21005.1 5 | MinimumVisualStudioVersion = 10.0.40219.1 6 | Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "TinyCompiler", "TinyCompiler\TinyCompiler.vcxproj", "{2CD79679-AE64-4D54-82F6-46B2B7D7D338}" 7 | EndProject 8 | Global 9 | GlobalSection(SolutionConfigurationPlatforms) = preSolution 10 | Debug|Win32 = Debug|Win32 11 | Release|Win32 = Release|Win32 12 | EndGlobalSection 13 | GlobalSection(ProjectConfigurationPlatforms) = postSolution 14 | {2CD79679-AE64-4D54-82F6-46B2B7D7D338}.Debug|Win32.ActiveCfg = Debug|Win32 15 | {2CD79679-AE64-4D54-82F6-46B2B7D7D338}.Debug|Win32.Build.0 = Debug|Win32 16 | {2CD79679-AE64-4D54-82F6-46B2B7D7D338}.Release|Win32.ActiveCfg = Release|Win32 17 | {2CD79679-AE64-4D54-82F6-46B2B7D7D338}.Release|Win32.Build.0 = Release|Win32 18 | EndGlobalSection 19 | GlobalSection(SolutionProperties) = preSolution 20 | HideSolutionNode = FALSE 21 | EndGlobalSection 22 | EndGlobal 23 | -------------------------------------------------------------------------------- /TinyCompiler/KeyWord/KeyWord.cpp: -------------------------------------------------------------------------------- 1 | #include "KeyWord.h" 2 | 3 | namespace TinyCompiler{ 4 | std::shared_ptr KeyWordDictInstance::pInstance = nullptr; 5 | 6 | std::shared_ptr KeyWordDictInstance::getInstance(){ 7 | if (!pInstance){ 8 | pInstance = std::make_shared(); 9 | pInstance->insert({ "break" }); 10 | pInstance->insert({ "case" }); 11 | pInstance->insert({ "char" }); 12 | pInstance->insert({ "const" }); 13 | pInstance->insert({ "continue" }); 14 | pInstance->insert({ "default" }); 15 | pInstance->insert({ "do" }); 16 | pInstance->insert({ "double" }); 17 | pInstance->insert({ "else" }); 18 | pInstance->insert({ "enum" }); 19 | pInstance->insert({ "float" }); 20 | pInstance->insert({ "for" }); 21 | pInstance->insert({ "if" }); 22 | pInstance->insert({ "int" }); 23 | pInstance->insert({ "long" }); 24 | pInstance->insert({ "return" }); 25 | pInstance->insert({ "short" }); 26 | pInstance->insert({ "signed" }); 27 | pInstance->insert({ "static" }); 28 | pInstance->insert({ "struct" }); 29 | pInstance->insert({ "switch" }); 30 | pInstance->insert({ "typedef" }); 31 | pInstance->insert({ "union" }); 32 | pInstance->insert({ "unsigned" }); 33 | pInstance->insert({ "void" }); 34 | pInstance->insert({ "while" }); 35 | } 36 | return pInstance; 37 | } 38 | } -------------------------------------------------------------------------------- /TinyCompiler/KeyWord/KeyWord.h: -------------------------------------------------------------------------------- 1 | #ifndef _KEYWORD_H_ 2 | #define _KEYWORD_H_ 3 | 4 | #include 5 | #include 6 | #include 7 | #include 8 | 9 | namespace TinyCompiler{ 10 | 11 | //define the key word of the source language 12 | enum class KeyWord{ 13 | BREAK, 14 | CASE, 15 | CHAR, 16 | CONST, 17 | CONTINUE, 18 | DEFAULT, 19 | DO, 20 | DOUBLE, 21 | ELSE, 22 | ENUM, 23 | FLOAT, 24 | FOR, 25 | IF, 26 | INT, 27 | LONG, 28 | RETURN, 29 | SHORT, 30 | SIGNED, 31 | STATIC, 32 | STRUCT, 33 | SWITCH, 34 | TYPEDEF, 35 | UNION, 36 | UNSIGNED, 37 | VOID, 38 | WHILE 39 | }; 40 | 41 | typedef std::set KeyWordDict; 42 | class KeyWordDictInstance{ 43 | private: 44 | static std::shared_ptr pInstance; 45 | public: 46 | static std::shared_ptr getInstance(); 47 | }; 48 | } 49 | 50 | #endif -------------------------------------------------------------------------------- /TinyCompiler/Profiler/Profiler.cpp: -------------------------------------------------------------------------------- 1 | #include "Profiler.h" 2 | 3 | namespace TinyCompiler{ 4 | namespace Profiler{ 5 | ProfilerInstance::TimePoint ProfilerInstance::startTime; 6 | ProfilerInstance::TimePoint ProfilerInstance::finishTime; 7 | 8 | void ProfilerInstance::start(){ 9 | startTime = SteadyClock::now(); 10 | } 11 | void ProfilerInstance::finish(){ 12 | finishTime = SteadyClock::now(); 13 | } 14 | void ProfilerInstance::dumpDuringTime(std::ostream& os){ 15 | typedef std::chrono::duration DurationTime; 16 | DurationTime duringTime = 17 | std::chrono::duration_cast(finishTime - startTime); 18 | os << "total " << duringTime.count() * 1000 << " milliseconds" << std::endl; 19 | } 20 | } 21 | } -------------------------------------------------------------------------------- /TinyCompiler/Profiler/Profiler.h: -------------------------------------------------------------------------------- 1 | #ifndef _PROFILER_H_ 2 | #define _PROFILER_H_ 3 | 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | #include 10 | 11 | namespace TinyCompiler{ 12 | 13 | namespace Profiler{ 14 | class ProfilerInstance{ 15 | public: 16 | typedef std::chrono::steady_clock SteadyClock; 17 | typedef SteadyClock::time_point TimePoint; 18 | private: 19 | static TimePoint startTime; 20 | static TimePoint finishTime; 21 | public: 22 | static void start(); 23 | static void finish(); 24 | static void dumpDuringTime(std::ostream& os = std::cout); 25 | }; 26 | } 27 | } 28 | 29 | #endif -------------------------------------------------------------------------------- /TinyCompiler/Scanner/Scanner.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | #include "../KeyWord/KeyWord.h" 6 | #include "Scanner.h" 7 | 8 | namespace TinyCompiler{ 9 | void Scanner::setPhrase(const ScanPhrase phrase){ 10 | this->phrase_ = phrase; 11 | } 12 | 13 | Scanner::ScanPhrase Scanner::getPhrase() const{ 14 | return this->phrase_; 15 | } 16 | 17 | void Scanner::setFileName(const std::string& fileName){ 18 | this->fileName_ = fileName; 19 | } 20 | 21 | std::string Scanner::getFileName() const{ 22 | return this->fileName_; 23 | } 24 | 25 | void Scanner::clear(){ 26 | this->code_ = ""; 27 | citer_ = this->code_.cbegin(); 28 | this->fileName_ = ""; 29 | this->phrase_ = ScanPhrase::END; 30 | } 31 | 32 | bool Scanner::openFile(){ 33 | assert(!(this->fileName_.empty())); 34 | 35 | std::ifstream in(this->fileName_); 36 | if (in){ 37 | in.seekg(0, in.end); 38 | size_t length = in.tellg(); 39 | in.seekg(0, in.beg); 40 | //resize the code_, so it can hold all source 41 | this->code_.resize(length); 42 | in.read(&(this->code_[0]), this->code_.size()); 43 | citer_ = code_.cbegin(); 44 | in.close(); 45 | return true; 46 | } 47 | return false; 48 | } 49 | bool Scanner::isDoubleDelimter(const char ch){ 50 | if (ch == '>' || ch == '+' || ch == '-' || 51 | ch == '<' || ch == '=' || ch == '&' || 52 | ch == '|') 53 | return true; 54 | return false; 55 | } 56 | void Scanner::skipBlank(){ 57 | for (; citer_ != code_.cend() && *citer_ != EOF; ){ 58 | if (std::isblank(*citer_) || *citer_ == '\n'){ 59 | if (*citer_ == '\n'){ 60 | ++location_; 61 | } 62 | ++citer_; 63 | } 64 | else{ 65 | break; 66 | } 67 | } 68 | } 69 | 70 | void Scanner::handleBegin(std::string& tokenName, TokenAttr& tokenAttr){ 71 | if (std::isdigit(*(citer_))){//is a number 72 | this->phrase_ = ScanPhrase::IN_INTEGER; 73 | }else if(std::isalpha(*(citer_)) || *citer_ == '_'){//is a letter or an underline 74 | this->phrase_ = ScanPhrase::IN_VARIALBE; 75 | }else if (*(citer_) == '\"' || *(citer_) == '\''){//is a single/double quotation marks 76 | this->phrase_ = ScanPhrase::IN_STRING; 77 | }else{//in other scenes, wo first think it is a single delimiter 78 | this->phrase_ = ScanPhrase::IN_SINGLEDELIMITER; 79 | } 80 | tokenName += *(citer_); 81 | ++(citer_); 82 | } 83 | void Scanner::handleString(std::string& tokenName, TokenAttr& tokenAttr){ 84 | if (*(citer_) != '\"' && *(citer_) != '\''){ 85 | tokenName += *(citer_); 86 | ++(citer_); 87 | this->phrase_ = ScanPhrase::IN_STRING; 88 | } 89 | else{ 90 | tokenName += *(citer_); 91 | ++(citer_); 92 | tokenAttr = TokenAttr::STRING; 93 | this->phrase_ = ScanPhrase::END; 94 | } 95 | } 96 | void Scanner::handleReal(std::string& tokenName, TokenAttr& tokenAttr){ 97 | if (isdigit(*(citer_))){//also a number 98 | tokenName += *(citer_); 99 | ++(citer_); 100 | this->phrase_ = ScanPhrase::IN_REAL; 101 | }else{ 102 | tokenAttr = TokenAttr::REAL; 103 | this->phrase_ = ScanPhrase::END; 104 | } 105 | } 106 | void Scanner::handleInteger(std::string& tokenName, TokenAttr& tokenAttr){ 107 | if (isdigit(*(citer_))){//also a number 108 | tokenName += *(citer_); 109 | ++(citer_); 110 | this->phrase_ = ScanPhrase::IN_INTEGER; 111 | }else if (*(citer_) == '.'){//a dot appears 112 | tokenName += *(citer_); 113 | ++(citer_); 114 | this->phrase_ = ScanPhrase::IN_REAL; 115 | }else{ 116 | tokenAttr = TokenAttr::INTEGER; 117 | this->phrase_ = ScanPhrase::END; 118 | } 119 | } 120 | void Scanner::handleVariable(std::string& tokenName, TokenAttr& tokenAttr){ 121 | if (std::isalpha(*(citer_)) || *citer_ == '_'){//also a letter or an underline 122 | tokenName += *(citer_); 123 | ++(citer_); 124 | this->phrase_ = ScanPhrase::IN_VARIALBE; 125 | }else{ 126 | if (KeyWordDictInstance::getInstance()->count(tokenName) != 0){//is a key word 127 | this->phrase_ = ScanPhrase::IN_KEYWORD; 128 | }else{ 129 | tokenAttr = TokenAttr::VARIABLE; 130 | this->phrase_ = ScanPhrase::END; 131 | } 132 | } 133 | } 134 | void Scanner::handleKeyWord(std::string& tokenName, TokenAttr& tokenAttr){ 135 | tokenAttr = TokenAttr::KEYWORD; 136 | this->phrase_ = ScanPhrase::END; 137 | } 138 | void Scanner::handleSingleDelimiter(std::string& tokenName, TokenAttr& tokenAttr){ 139 | if (isDoubleDelimter(*citer_)){//is a double delimiter 140 | tokenName += *citer_; 141 | ++citer_; 142 | this->phrase_ = ScanPhrase::IN_DOUBLEDELIMITER; 143 | }else{ 144 | tokenAttr = TokenAttr::DELIMITER; 145 | this->phrase_ = ScanPhrase::END; 146 | } 147 | } 148 | void Scanner::handleDoubleDelimiter(std::string& tokenName, TokenAttr& tokenAttr){ 149 | tokenAttr = TokenAttr::DELIMITER; 150 | this->phrase_ = ScanPhrase::END; 151 | } 152 | Token Scanner::handleEnd(std::string& name, 153 | const TokenAttr tokenAttr, 154 | const size_t location){ 155 | return Token(name, tokenAttr, this->fileName_, location); 156 | } 157 | 158 | Token Scanner::getNextToken(){ 159 | this->phrase_ = ScanPhrase::BEGIN; 160 | std::string tokenName; 161 | TokenAttr tokenAttr = TokenAttr::UNKNOWN; 162 | 163 | skipBlank(); 164 | while (citer_ != code_.cend() && *(citer_) != EOF){ 165 | switch (this->phrase_){ 166 | case ScanPhrase::BEGIN: 167 | handleBegin(tokenName, tokenAttr); 168 | break; 169 | case ScanPhrase::IN_SINGLEDELIMITER: 170 | handleSingleDelimiter(tokenName, tokenAttr); 171 | break; 172 | case ScanPhrase::IN_DOUBLEDELIMITER: 173 | handleDoubleDelimiter(tokenName, tokenAttr); 174 | break; 175 | case ScanPhrase::IN_INTEGER: 176 | handleInteger(tokenName, tokenAttr); 177 | break; 178 | case ScanPhrase::IN_KEYWORD: 179 | handleKeyWord(tokenName, tokenAttr); 180 | break; 181 | case ScanPhrase::IN_REAL: 182 | handleReal(tokenName, tokenAttr); 183 | break; 184 | case ScanPhrase::IN_STRING: 185 | handleString(tokenName, tokenAttr); 186 | break; 187 | case ScanPhrase::IN_VARIALBE: 188 | handleVariable(tokenName, tokenAttr); 189 | break; 190 | 191 | case ScanPhrase::END: 192 | auto tok = handleEnd(tokenName, tokenAttr, location_); 193 | return tok; 194 | } 195 | } 196 | clear();//clear the state of the scanner 197 | return Token("", TokenAttr::UNKNOWN, "", -1); 198 | } 199 | 200 | std::vector Scanner::getTokens(){ 201 | std::vector toks; 202 | Token tok; 203 | while ((tok = getNextToken())){ 204 | toks.push_back(tok); 205 | } 206 | clear();//clear the state of the scanner 207 | return toks; 208 | } 209 | } -------------------------------------------------------------------------------- /TinyCompiler/Scanner/Scanner.h: -------------------------------------------------------------------------------- 1 | #ifndef _SCANNER_H_ 2 | #define _SCANNER_H_ 3 | 4 | #include 5 | #include 6 | #include 7 | 8 | #include "../Token/Token.h" 9 | 10 | namespace TinyCompiler{ 11 | 12 | class Scanner{ 13 | private: 14 | enum class ScanPhrase{ 15 | BEGIN, 16 | IN_KEYWORD, 17 | IN_STRING, 18 | IN_INTEGER, 19 | IN_REAL, 20 | IN_SINGLEDELIMITER, 21 | IN_DOUBLEDELIMITER, 22 | IN_VARIALBE, 23 | END 24 | }; 25 | private: 26 | ScanPhrase phrase_; 27 | std::string fileName_; 28 | std::string code_; 29 | //record the position where we now scan 30 | std::string::const_iterator citer_; 31 | //record the number of line where the token locates 32 | size_t location_; 33 | private: 34 | bool openFile(); 35 | void clear(); 36 | void skipBlank(); 37 | bool isDoubleDelimter(const char ch); 38 | 39 | void handleBegin(std::string& tokenName, TokenAttr& tokenAttr); 40 | void handleKeyWord(std::string& tokenName, TokenAttr& tokenAttr); 41 | void handleSingleDelimiter(std::string& tokenName, TokenAttr& tokenAttr); 42 | void handleDoubleDelimiter(std::string& tokenName, TokenAttr& tokenAttr); 43 | void handleInteger(std::string& tokenName, TokenAttr& tokenAttr); 44 | void handleReal(std::string& tokenName, TokenAttr& tokenAttr); 45 | void handleString(std::string& tokenName, TokenAttr& tokenAttr); 46 | void handleVariable(std::string& tokenName, TokenAttr& tokenAttr); 47 | Token handleEnd(std::string& name, 48 | const TokenAttr tokenAttr, 49 | const size_t location); 50 | protected: 51 | void setPhrase(const ScanPhrase phrase); 52 | ScanPhrase getPhrase() const; 53 | void setFileName(const std::string& fileName); 54 | std::string getFileName() const; 55 | public: 56 | explicit Scanner(const std::string& fileName) 57 | :fileName_(fileName), phrase_(ScanPhrase::BEGIN), location_(1){ 58 | auto ret = openFile(); 59 | assert(ret); 60 | } 61 | 62 | //return all the tokens once 63 | std::vector getTokens(); 64 | //return a token once a time 65 | Token getNextToken(); 66 | }; 67 | } 68 | 69 | #endif -------------------------------------------------------------------------------- /TinyCompiler/StatementNode/CheckStatementNode/CheckStatementNode.h: -------------------------------------------------------------------------------- 1 | #ifndef _CHECK_STATEMENT_H_ 2 | #define _CHECK_STATEMENT_H_ 3 | 4 | #include "../StatementNode.h" 5 | namespace TinyCompiler{ 6 | 7 | //check statement class 8 | class CheckStatementNode : public StatementNode{ 9 | public: 10 | virtual void printNode(){ std::cout << "CheckStatementNode" << std::endl; } 11 | StatementNodeKind CheckStatementNode::kind_ = StatementNodeKind::CHECKSTATEMENTNODE; 12 | }; 13 | } 14 | #endif -------------------------------------------------------------------------------- /TinyCompiler/StatementNode/CheckStatementNode/IFCheckStatementNode.h: -------------------------------------------------------------------------------- 1 | #ifndef _IF_CHECK_STATEMENT_H_ 2 | #define _IF_CHECK_STATEMENT_H_ 3 | 4 | #include "CheckStatementNode.h" 5 | 6 | namespace TinyCompiler{ 7 | 8 | //if check statement class 9 | class IFCheckStatementNode : public CheckStatementNode{ 10 | public: 11 | virtual void printNode(){ std::cout << "IFCheckStatementNode" << std::endl; } 12 | StatementNodeKind IFCheckStatementNode::kind_ = StatementNodeKind::IFCHECKSTATEMENTNODE; 13 | }; 14 | } 15 | #endif -------------------------------------------------------------------------------- /TinyCompiler/StatementNode/LoopStatementNode/FORLoopStatementNode.h: -------------------------------------------------------------------------------- 1 | #ifndef _FOR_LOOP_STATEMENT_H_ 2 | #define _FOR_LOOP_STATEMENT_H_ 3 | 4 | #include "LoopStatementNode.h" 5 | 6 | namespace TinyCompiler{ 7 | 8 | //for loop statement class 9 | class FORLoopStatementNode : public LoopStatementNode{ 10 | public: 11 | virtual void printNode(){ std::cout << "FORLoopStatementNode" << std::endl; } 12 | StatementNodeKind FORLoopStatementNode::kind_ = StatementNodeKind::FORLOOPSTATEMENTNODE; 13 | }; 14 | } 15 | #endif -------------------------------------------------------------------------------- /TinyCompiler/StatementNode/LoopStatementNode/LoopStatementNode.h: -------------------------------------------------------------------------------- 1 | #ifndef _LOOP_STATEMENT_H_ 2 | #define _LOOP_STATEMENT_H_ 3 | 4 | #include "../StatementNode.h" 5 | namespace TinyCompiler{ 6 | 7 | //loop statement class 8 | class LoopStatementNode : public StatementNode{ 9 | public: 10 | virtual void printNode(){ std::cout << "LoopStatementNode" << std::endl; } 11 | StatementNodeKind LoopStatementNode::kind_ = StatementNodeKind::LOOPSTATEMENTNODE; 12 | }; 13 | } 14 | #endif -------------------------------------------------------------------------------- /TinyCompiler/StatementNode/LoopStatementNode/WHILELoopStatementNode.h: -------------------------------------------------------------------------------- 1 | #ifndef _WHILE_LOOP_STATEMENT_H_ 2 | #define _WHILE_LOOP_STATEMENT_H_ 3 | 4 | #include "LoopStatementNode.h" 5 | 6 | namespace TinyCompiler{ 7 | 8 | //while loop statement class 9 | class WhileLoopStatementNode : public LoopStatementNode{ 10 | public: 11 | virtual void printNode(){ std::cout << "WhileLoopStatementNode" << std::endl; } 12 | StatementNodeKind WhileLoopStatementNode::kind_ = StatementNodeKind::WHILELOOPSTATEMENTNODE; 13 | }; 14 | } 15 | #endif -------------------------------------------------------------------------------- /TinyCompiler/StatementNode/SquenceStatementNode/ArithmeticSquenceStatementNode.h: -------------------------------------------------------------------------------- 1 | #ifndef _ARITHMETIC_SQUENCE_STATEMENT_NODE_H_ 2 | #define _ARITHMETIC_SQUENCE_STATEMENT_NODE_H_ 3 | 4 | #include "SquenceStatementNode.h" 5 | 6 | namespace TinyCompiler{ 7 | 8 | //the arithmetic statement class 9 | class ArithmeticSquenceStatementNode :public SquenceStatementNode{ 10 | public: 11 | virtual void printNodeKind(){ std::cout << "ArithmeticSquenceStatementNode" << std::endl; } 12 | StatementNodeKind ArithmeticSquenceStatementNode::kind_ = StatementNodeKind::ARITHMETICSQUENCESTATEMENTNODE; 13 | }; 14 | } 15 | 16 | #endif -------------------------------------------------------------------------------- /TinyCompiler/StatementNode/SquenceStatementNode/AssignSquenceStatementNode.h: -------------------------------------------------------------------------------- 1 | #ifndef _ASSIGN_SQUENCE_STATEMENT_NODE_H_ 2 | #define _ASSIGN_SQUENCE_STATEMENT_NODE_H_ 3 | 4 | #include "SquenceStatementNode.h" 5 | 6 | namespace TinyCompiler{ 7 | 8 | //the assign statement class 9 | class AssignSquenceStatementNode :public SquenceStatementNode{ 10 | public: 11 | virtual void printNodeKind(){ std::cout << "AssignSquenceStatementNode" << std::endl; } 12 | StatementNodeKind AssignSquenceStatementNode::kind_ = StatementNodeKind::ASSIGNSQUENCESTATEMENTNODE; 13 | }; 14 | } 15 | #endif -------------------------------------------------------------------------------- /TinyCompiler/StatementNode/SquenceStatementNode/DefineSquenceStatementNode.h: -------------------------------------------------------------------------------- 1 | #ifndef _DEFINE_SQUENCE_STATEMENT_NODE_H_ 2 | #define _DEFINE_SQUENCE_STATEMENT_NODE_H_ 3 | 4 | #include "SquenceStatementNode.h" 5 | 6 | namespace TinyCompiler{ 7 | 8 | //the define statement class 9 | class DefineSquenceStatementNode :public SquenceStatementNode{ 10 | public: 11 | virtual void printNodeKind(){ std::cout << "DefineSquenceStatementNode" << std::endl; } 12 | 13 | StatementNodeKind DefineSquenceStatementNode::kind_ = StatementNodeKind::DEFINESQUENCESTATEMENTNODE; 14 | }; 15 | } 16 | #endif -------------------------------------------------------------------------------- /TinyCompiler/StatementNode/SquenceStatementNode/SquenceStatementNode.h: -------------------------------------------------------------------------------- 1 | #ifndef _SQUENCE_STATEMENT_H_ 2 | #define _SQUENCE_STATEMENT_H_ 3 | 4 | #include "../StatementNode.h" 5 | namespace TinyCompiler{ 6 | 7 | //squence statement class 8 | class SquenceStatementNode : public StatementNode{ 9 | public: 10 | virtual void printNode(){ std::cout << "SquenceStatementNode" << std::endl; } 11 | StatementNodeKind SquenceStatementNode::kind_ = StatementNodeKind::SQUENCESTATEMENTNODE; 12 | }; 13 | } 14 | 15 | #endif -------------------------------------------------------------------------------- /TinyCompiler/StatementNode/StatementNode.h: -------------------------------------------------------------------------------- 1 | #ifndef _STATEMENT_NODE_H_ 2 | #define _STATEMENT_NODE_H_ 3 | 4 | #include 5 | #include 6 | #include 7 | #include 8 | 9 | #include "StatementNodeAttr.h" 10 | 11 | namespace TinyCompiler{ 12 | 13 | //the base class of the statement 14 | class StatementNode{ 15 | public: 16 | //std::string name; 17 | typedef std::shared_ptr SNPtr; 18 | std::vector nodes_; 19 | static StatementNodeKind kind_; 20 | public: 21 | virtual void printNodeKind(){ std::cout << "StatementNode" << std::endl; } 22 | virtual ~StatementNode(){}; 23 | }; 24 | 25 | StatementNodeKind StatementNode::kind_ = StatementNodeKind::STATEMENTNODE; 26 | } 27 | 28 | #endif -------------------------------------------------------------------------------- /TinyCompiler/StatementNode/StatementNodeAttr.h: -------------------------------------------------------------------------------- 1 | #ifndef _STATEMENT_NODE_ATTR_H_ 2 | #define _STATEMENT_NODE_ATTR_H_ 3 | 4 | namespace TinyCompiler{ 5 | 6 | //the kind of statement node 7 | enum class StatementNodeKind{ 8 | STATEMENTNODE, 9 | 10 | LOOPSTATEMENTNODE, 11 | FORLOOPSTATEMENTNODE, 12 | WHILELOOPSTATEMENTNODE, 13 | 14 | CHECKSTATEMENTNODE, 15 | IFCHECKSTATEMENTNODE, 16 | 17 | SQUENCESTATEMENTNODE, 18 | ARITHMETICSQUENCESTATEMENTNODE, 19 | ASSIGNSQUENCESTATEMENTNODE, 20 | DEFINESQUENCESTATEMENTNODE, 21 | 22 | UNKNOWN, 23 | }; 24 | } 25 | 26 | #endif -------------------------------------------------------------------------------- /TinyCompiler/StatementNode/StatementNodeInclude.h: -------------------------------------------------------------------------------- 1 | #ifndef _STATEMENT_NODE_INCLUDE_H_ 2 | #define _STATEMENT_NODE_INCLUDE_H_ 3 | 4 | #include "StatementNode.h" 5 | 6 | #include "CheckStatementNode\CheckStatementNode.h" 7 | #include "CheckStatementNode\IFCheckStatementNode.h" 8 | 9 | #include "LoopStatementNode\LoopStatementNode.h" 10 | #include "LoopStatementNode\FORLoopStatementNode.h" 11 | #include "LoopStatementNode\WHILELoopStatementNode.h" 12 | 13 | #include "SquenceStatementNode\SquenceStatementNode.h" 14 | #include "SquenceStatementNode\ArithmeticSquenceStatementNode.h" 15 | #include "SquenceStatementNode\AssignSquenceStatementNode.h" 16 | #include "SquenceStatementNode\DefineSquenceStatementNode.h" 17 | 18 | #endif -------------------------------------------------------------------------------- /TinyCompiler/TinyCompiler.vcxproj: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | 5 | Debug 6 | Win32 7 | 8 | 9 | Release 10 | Win32 11 | 12 | 13 | 14 | {2CD79679-AE64-4D54-82F6-46B2B7D7D338} 15 | Win32Proj 16 | TinyCompiler 17 | 18 | 19 | 20 | Application 21 | true 22 | v140 23 | Unicode 24 | 25 | 26 | Application 27 | false 28 | v140 29 | true 30 | Unicode 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | true 44 | 45 | 46 | false 47 | 48 | 49 | 50 | 51 | 52 | Level3 53 | Disabled 54 | WIN32;_DEBUG;_CONSOLE;_LIB;%(PreprocessorDefinitions) 55 | 56 | 57 | Console 58 | true 59 | 60 | 61 | 62 | 63 | Level3 64 | 65 | 66 | MaxSpeed 67 | true 68 | true 69 | WIN32;NDEBUG;_CONSOLE;_LIB;%(PreprocessorDefinitions) 70 | 71 | 72 | Console 73 | true 74 | true 75 | true 76 | 77 | 78 | 79 | 80 | 81 | 82 | 83 | 84 | 85 | 86 | 87 | 88 | 89 | 90 | 91 | 92 | 93 | 94 | 95 | 96 | 97 | 98 | 99 | 100 | 101 | 102 | 103 | 104 | 105 | 106 | 107 | -------------------------------------------------------------------------------- /TinyCompiler/TinyCompiler.vcxproj.filters: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | 5 | {4FC737F1-C7A5-4376-A066-2A32D752A2FF} 6 | cpp;c;cc;cxx;def;odl;idl;hpj;bat;asm;asmx 7 | 8 | 9 | {67DA6AB6-F800-4c08-8B7A-83BB121AAD01} 10 | rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms 11 | 12 | 13 | {44c99448-c524-4833-8f24-0573eaf80a96} 14 | 15 | 16 | {0243e1c4-6967-4e97-b601-eff3d32db68c} 17 | 18 | 19 | {100b9a5c-e7c2-476c-a69e-186c7d1e4bb0} 20 | 21 | 22 | {b9567185-3426-4e0e-bc35-eb4c37339d36} 23 | 24 | 25 | {a1e70a14-afc4-4b49-bc9b-d238f6b75109} 26 | 27 | 28 | {41b9a602-a73c-488b-8a4f-0a0c1641c84c} 29 | 30 | 31 | {361856db-e5bd-4d29-a3ad-80c2b3a17056} 32 | 33 | 34 | {36778e79-035d-4096-b1d7-2fecf53bc7c0} 35 | 36 | 37 | 38 | 39 | 源文件 40 | 41 | 42 | KeyWord 43 | 44 | 45 | Profiler 46 | 47 | 48 | Scanner 49 | 50 | 51 | Token 52 | 53 | 54 | Token 55 | 56 | 57 | 58 | 59 | KeyWord 60 | 61 | 62 | Profiler 63 | 64 | 65 | Scanner 66 | 67 | 68 | Token 69 | 70 | 71 | Token 72 | 73 | 74 | StatementNode 75 | 76 | 77 | StatementNode\SquenceStatementNode 78 | 79 | 80 | StatementNode\LoopStatementNode 81 | 82 | 83 | StatementNode\CheckStatementNode 84 | 85 | 86 | StatementNode\CheckStatementNode 87 | 88 | 89 | StatementNode\LoopStatementNode 90 | 91 | 92 | StatementNode\LoopStatementNode 93 | 94 | 95 | StatementNode 96 | 97 | 98 | StatementNode\SquenceStatementNode 99 | 100 | 101 | StatementNode\SquenceStatementNode 102 | 103 | 104 | StatementNode\SquenceStatementNode 105 | 106 | 107 | -------------------------------------------------------------------------------- /TinyCompiler/Token/Token.cpp: -------------------------------------------------------------------------------- 1 | #include "Token.h" 2 | 3 | namespace TinyCompiler{ 4 | 5 | void Token::setName(const std::string& name){ 6 | this->name_ = name; 7 | } 8 | std::string Token::getName() const{ 9 | return this->name_; 10 | } 11 | void Token::setTokenAttr(const TokenAttr tokenAttr){ 12 | this->tokenAttr_ = tokenAttr; 13 | } 14 | TokenAttr Token::getTokenAttr() const{ 15 | return this->tokenAttr_; 16 | } 17 | void Token::setFileName(const std::string& fileName){ 18 | this->fileName_ = fileName; 19 | } 20 | std::string Token::getFileName() const{ 21 | return this->fileName_; 22 | } 23 | void Token::setLocation(const size_t location){ 24 | this->location_ = location; 25 | } 26 | size_t Token::getLocation() const{ 27 | return this->location_; 28 | } 29 | 30 | void Token::dumpToken(std::ostream& os) const{ 31 | os << "Token: " << "{" 32 | << "name = " << this->name_ << ", " 33 | << "attr = " << (*(TokenAttrDictInstance::getInstance()))[this->tokenAttr_] << ", " 34 | << "loc = " << this->location_ << ", " 35 | << "file = " << this->fileName_ 36 | << "}" << std::endl; 37 | } 38 | } -------------------------------------------------------------------------------- /TinyCompiler/Token/Token.h: -------------------------------------------------------------------------------- 1 | #ifndef _TOKEN_H_ 2 | #define _TOKEN_H_ 3 | 4 | #include 5 | #include 6 | 7 | #include "TokenAttr.h" 8 | 9 | namespace TinyCompiler{ 10 | 11 | class Token{ 12 | private: 13 | std::string name_; 14 | TokenAttr tokenAttr_; 15 | std::string fileName_; 16 | size_t location_; 17 | 18 | public: 19 | Token() 20 | :name_(""), tokenAttr_(TokenAttr::UNKNOWN), fileName_(""), location_(0){} 21 | Token(std::string name, 22 | TokenAttr tokenAttr, 23 | std::string fileName, 24 | size_t location) 25 | :name_(name), tokenAttr_(tokenAttr), fileName_(fileName), location_(location){} 26 | 27 | operator bool(){ 28 | if (tokenAttr_ == TokenAttr::UNKNOWN) 29 | return false; 30 | return true; 31 | } 32 | 33 | void setName(const std::string& name); 34 | std::string getName() const; 35 | void setTokenAttr(const TokenAttr tokenAttr); 36 | TokenAttr getTokenAttr() const; 37 | void setFileName(const std::string& fileName); 38 | std::string getFileName() const; 39 | void setLocation(const size_t location); 40 | size_t getLocation() const; 41 | 42 | void dumpToken(std::ostream& os = std::cout) const; 43 | }; 44 | } 45 | 46 | #endif -------------------------------------------------------------------------------- /TinyCompiler/Token/TokenAttr.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | #include "TokenAttr.h" 4 | 5 | namespace TinyCompiler{ 6 | 7 | std::shared_ptr 8 | TokenAttrDictInstance::pInstance = nullptr; 9 | 10 | std::shared_ptr 11 | TokenAttrDictInstance::getInstance(){ 12 | if (!pInstance){ 13 | pInstance = std::make_shared(); 14 | pInstance->insert({ TokenAttr::KEYWORD, "KEYWORD" }); 15 | pInstance->insert({ TokenAttr::DELIMITER, "DELIMITER" }); 16 | pInstance->insert({ TokenAttr::INTEGER, "INTEGER" }); 17 | pInstance->insert({ TokenAttr::REAL, "REAL" }); 18 | pInstance->insert({ TokenAttr::STRING, "STRING" }); 19 | pInstance->insert({ TokenAttr::VARIABLE, "VARIABLE" }); 20 | pInstance->insert({ TokenAttr::UNKNOWN, "UNKNOWN" }); 21 | } 22 | 23 | return pInstance; 24 | } 25 | 26 | } -------------------------------------------------------------------------------- /TinyCompiler/Token/TokenAttr.h: -------------------------------------------------------------------------------- 1 | #ifndef _TOKEN_ATTR_H_ 2 | #define _TOKEN_ATTR_H_ 3 | 4 | #include 5 | #include 6 | #include 7 | #include 8 | 9 | namespace TinyCompiler{ 10 | 11 | enum class TokenAttr{ 12 | KEYWORD, 13 | STRING, 14 | INTEGER, 15 | REAL, 16 | DELIMITER, 17 | VARIABLE, 18 | 19 | UNKNOWN 20 | }; 21 | 22 | class TokenAttrDictInstance{ 23 | private: 24 | typedef std::map TokenAttrDict; 25 | 26 | static std::shared_ptr pInstance; 27 | public: 28 | static std::shared_ptr getInstance(); 29 | }; 30 | } 31 | 32 | #endif -------------------------------------------------------------------------------- /TinyCompiler/main.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | #include "Profiler\Profiler.h" 6 | #include "Scanner\Scanner.h" 7 | #include "StatementNode\StatementNodeInclude.h" 8 | 9 | using namespace std; 10 | using namespace std::chrono; 11 | 12 | int main(){ 13 | using namespace TinyCompiler; 14 | StatementNode sn; 15 | 16 | LoopStatementNode lsn; 17 | FORLoopStatementNode flsn; 18 | WhileLoopStatementNode wlsn; 19 | 20 | CheckStatementNode csn; 21 | IFCheckStatementNode icsn; 22 | 23 | SquenceStatementNode ssn; 24 | ArithmeticSquenceStatementNode arssn; 25 | AssignSquenceStatementNode asssn; 26 | DefineSquenceStatementNode dssn; 27 | 28 | std::vector svec; 29 | svec.push_back(&sn); 30 | svec.push_back(&lsn); 31 | svec.push_back(&flsn); 32 | svec.push_back(&wlsn); 33 | svec.push_back(&csn); 34 | svec.push_back(&icsn); 35 | svec.push_back(&ssn); 36 | svec.push_back(&arssn); 37 | svec.push_back(&asssn); 38 | svec.push_back(&dssn); 39 | 40 | for (const auto& node : svec){ 41 | node->printNodeKind(); 42 | } 43 | 44 | //typedef TinyCompiler::Profiler::ProfilerInstance Profiler; 45 | //TinyCompiler::Scanner s("C:\\Users\\zxh\\Desktop\\nginx.c"); 46 | //TinyCompiler::Token tok; 47 | 48 | //ofstream out("C:\\Users\\zxh\\Desktop\\output.c", 49 | // ostream::out | ofstream::trunc); 50 | 51 | 52 | //Profiler::start(); 53 | //while ((tok = s.getNextToken())){ 54 | // cout << tok.getName() << endl; 55 | // //tok.dumpToken(); 56 | //} 57 | //Profiler::finish(); 58 | //Profiler::dumpDuringTime(); 59 | //out.close(); 60 | system("pause"); 61 | return 0; 62 | } --------------------------------------------------------------------------------