/** ****************************************************************************** * @file : compiler.c * @author : simon * @brief : None * @attention : None * @date : 2023/8/17 ****************************************************************************** */ #include "common.h" #include "compiler.h" #include "scanner.h" void compile(const char *source) { initScanner(source); int line = -1; /// \brief 如 print 1 + 2; 则会输出 /// 1 31 'print' /// | 21 '1' /// | 7 '+' /// | 21 '2' /// | 8 ';' /// 2 39 '' // for (;;) { Token token = scanToken(); if (token.line != line) { printf("%4d ", token.line); line = token.line; } else { printf(" | "); } /// %.*s: *用来指定宽度,对应一个整数。 /// .(点)与后面的数合起来 是指定必须输出这个宽度, /// 如果所输出的字符串长度大于这个数,则按此宽度输出,如果小于,则输出实际长度 printf("%2d '%.*s'\n", token.type, token.length, token.start); if (token.type == TOKEN_EOF) break; } }