compiler.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967
  1. /**
  2. ******************************************************************************
  3. * @file : compiler.c
  4. * @author : simon
  5. * @brief : None
  6. * @attention : None
  7. * @date : 2023/8/17
  8. ******************************************************************************
  9. */
  10. #include "compiler.h"
  11. #include "common.h"
  12. #include "scanner.h"
  13. #ifdef DEBUG_PRINT_CODE
  14. #include "debug.h"
  15. #endif
  16. #include "memory.h"
  17. typedef struct {
  18. Token current;
  19. Token previous;
  20. bool hadError;
  21. bool panicMode;
  22. } Parser;
  23. typedef enum {
  24. PREC_NONE,
  25. PREC_ASSIGNMENT,// =
  26. PREC_OR, // or
  27. PREC_AND, // and
  28. PREC_EQUALITY, // == !=
  29. PREC_COMPARISON,// < > <= >=
  30. PREC_TERM, // + -
  31. PREC_FACTOR, // * /
  32. PREC_UNARY, // ! -
  33. PREC_CALL, // . ()
  34. PREC_PRIMARY
  35. } Precedence;
  36. typedef void (*ParseFn)(bool canAssign);
  37. typedef struct {
  38. ParseFn prefix;
  39. ParseFn infix;
  40. Precedence precedence;
  41. } ParseRule;
  42. typedef struct {
  43. Token name;
  44. int depth;
  45. bool isCaptured;
  46. } Local;
  47. typedef struct {
  48. uint8_t index;
  49. bool isLocal;
  50. } Upvalue;
  51. typedef enum {
  52. TYPE_METHOD,
  53. TYPE_FUNCTION,
  54. TYPE_INITIALIZER,
  55. TYPE_SCRIPT,
  56. } FunctionType;
  57. typedef struct Compiler {
  58. struct Compiler *enclosing;
  59. ObjFunction *function;
  60. FunctionType type;
  61. Local locals[UINT8_COUNT];
  62. int localCount;/// how many locals are in scope
  63. Upvalue upvalues[UINT8_COUNT];
  64. int scopeDepth;
  65. } Compiler;
  66. typedef struct ClassCompiler {
  67. struct ClassCompiler *enclosing;
  68. } ClassCompiler;
  69. Parser parser;
  70. Compiler *current = NULL;
  71. ClassCompiler *currentClass = NULL;
  72. static void parsePrecedence(Precedence);
  73. static uint8_t parseVariable(const char *);
  74. static void declareVariable();
  75. static void defineVariable(uint8_t);
  76. static uint8_t identifierConstant(Token *name);
  77. static bool identifiersEqual(Token *a, Token *b);
  78. static ParseRule *getRule(TokenType);
  79. static void statement();
  80. static void declaration();
  81. static void markInitialized();
  82. static void namedVariable(Token name, bool canAssign);
  83. static void initCompiler(Compiler *compiler, FunctionType type);
  84. static void beginScope();
  85. static void endScope();
  86. static void block();
  87. static ObjFunction *endCompiler();
  88. static Chunk *currentChunk() {
  89. return &current->function->chunk;
  90. }
  91. static void errorAt(Token *token, const char *message) {
  92. if (parser.panicMode) return;
  93. parser.panicMode = true;
  94. fprintf(stderr, "[line %d] Error", token->line);
  95. if (token->type == TOKEN_EOF) {
  96. fprintf(stderr, " at end");
  97. } else if (token->type == TOKEN_ERROR) {
  98. ///! Nothing.
  99. } else {
  100. fprintf(stderr, " at '%.*s'", token->length, token->start);
  101. }
  102. fprintf(stderr, ": %s\n", message);
  103. parser.hadError = true;
  104. }
  105. static void error(const char *message) {
  106. errorAt(&parser.previous, message);
  107. }
  108. static void errorAtCurrent(const char *message) {
  109. errorAt(&parser.current, message);
  110. }
  111. static void advance() {
  112. parser.previous = parser.current;
  113. for (;;) {
  114. parser.current = scanToken();
  115. if (parser.current.type != TOKEN_ERROR) break;
  116. errorAtCurrent(parser.current.start);
  117. }
  118. }
  119. void consume(TokenType type, const char *message) {
  120. if (parser.current.type == type) {
  121. advance();
  122. return;
  123. }
  124. errorAtCurrent(message);
  125. }
  126. static bool check(TokenType type) {
  127. return parser.current.type == type;
  128. }
  129. static bool match(TokenType type) {
  130. if (!check(type)) return false;
  131. advance();
  132. return true;
  133. }
  134. static void emitByte(uint8_t byte) {
  135. writeChunk(currentChunk(), byte, parser.previous.line);
  136. }
  137. static void emitBytes(uint8_t byte1, uint8_t byte2) {
  138. emitByte(byte1);
  139. emitByte(byte2);
  140. }
  141. static void emitLoop(int loopStart) {
  142. emitByte(OP_LOOP);
  143. int offset = currentChunk()->count - loopStart + 2;
  144. if (offset > UINT16_MAX) error("Loop body too large.");
  145. emitByte((offset >> 8) & 0xFF);
  146. emitByte(offset & 0xFF);
  147. }
  148. static int emitJump(uint8_t instruction) {
  149. emitByte(instruction);
  150. emitByte(0xff);// 占位字节,后面填入需要跳转的 offset
  151. emitByte(0xff);//
  152. return currentChunk()->count - 2;
  153. }
  154. static void emitReturn() {
  155. if (current->type == TYPE_INITIALIZER) {
  156. emitBytes(OP_GET_LOCAL, 0);
  157. } else {
  158. emitByte(OP_NIL);
  159. }
  160. emitByte(OP_RETURN);
  161. }
  162. static uint8_t makeConstant(Value value) {
  163. int constant = addConstant(currentChunk(), value);
  164. if (constant > UINT8_MAX) {
  165. error("Too many constants in one chunk.");
  166. return 0;
  167. }
  168. return (uint8_t) constant;
  169. }
  170. static void expression() {
  171. parsePrecedence(PREC_ASSIGNMENT);
  172. }
  173. static void function(FunctionType type) {
  174. Compiler compiler;
  175. initCompiler(&compiler, type);
  176. beginScope();
  177. consume(TOKEN_LEFT_PAREN, "Expect '(' after function name.");
  178. ///! Parameters
  179. if (!check(TOKEN_RIGHT_PAREN)) {
  180. do {
  181. current->function->arity++;
  182. if (current->function->arity > 255) {
  183. errorAtCurrent("Can't have more than 255 parameters.");
  184. }
  185. uint8_t constant = parseVariable("Expect parameter name.");
  186. defineVariable(constant);
  187. } while (match(TOKEN_COMMA));
  188. }
  189. consume(TOKEN_RIGHT_PAREN, "Expect ')' after parameters.");
  190. consume(TOKEN_LEFT_BRACE, "Expect '{' before function body.");
  191. ///! Body
  192. block();
  193. ObjFunction *fun = endCompiler();
  194. emitBytes(OP_CLOSURE, makeConstant(OBJ_VAL(fun)));
  195. for (int i = 0; i < fun->upvalueCount; i++) {
  196. emitByte(compiler.upvalues[i].isLocal ? 1 : 0);
  197. emitByte(compiler.upvalues[i].index);
  198. }
  199. //! NOTE:
  200. //! this beginScope() doesn't have a corresponding endScope() call.
  201. //! Because we end Compiler completely when we reach the end of the
  202. //! function body, there's no need to close the lingering outermost
  203. //! scope.
  204. }
  205. static void method() {
  206. //! To define a new method, the VM needs three things:
  207. //!
  208. //! 1. The name of the method.
  209. //!
  210. //! 2. The closure for the method body.
  211. //!
  212. //! 3. The class to bind the method to.
  213. consume(TOKEN_IDENTIFIER, "Expect method name.");
  214. uint8_t constant = identifierConstant(&parser.previous);
  215. // Method Body ObjClosure <OP_CLOSURE>
  216. FunctionType type = TYPE_METHOD;
  217. if (parser.previous.length == 4 && memcmp(parser.previous.start, "init", 4) == 0) {
  218. type = TYPE_INITIALIZER;
  219. }
  220. function(type);
  221. emitBytes(OP_METHOD, constant);
  222. }
  223. static void classDeclaration() {
  224. consume(TOKEN_IDENTIFIER, "Expect class name.");
  225. Token className = parser.previous;
  226. uint8_t nameConstant = identifierConstant(&parser.previous);
  227. declareVariable();
  228. emitBytes(OP_CLASS, nameConstant);
  229. defineVariable(nameConstant);
  230. ClassCompiler classCompiler;
  231. classCompiler.enclosing = currentClass;
  232. currentClass = &classCompiler;
  233. namedVariable(className, false);
  234. consume(TOKEN_LEFT_BRACE, "Expect '{' before class body.");
  235. while (!check(TOKEN_RIGHT_BRACE) && !check(TOKEN_EOF)) {
  236. method();
  237. }
  238. consume(TOKEN_RIGHT_BRACE, "Expect '}' after class body.");
  239. emitByte(OP_POP);
  240. currentClass = currentClass->enclosing;
  241. }
  242. static void funDeclaration() {
  243. uint8_t global = parseVariable("Expect function name.");
  244. markInitialized();
  245. function(TYPE_FUNCTION);
  246. defineVariable(global);
  247. }
  248. /// for example: var a; var b = 10;
  249. static void varDeclaration() {
  250. uint8_t global = parseVariable("Expect variable name.");
  251. // 变量初始化
  252. if (match(TOKEN_EQUAL)) {
  253. expression();
  254. } else {
  255. emitByte(OP_NIL);
  256. }
  257. consume(TOKEN_SEMICOLON, "Expect ';' after variable declaration.");
  258. defineVariable(global);
  259. }
  260. static void expressionStatement() {
  261. expression();
  262. consume(TOKEN_SEMICOLON, "Expect ';' after expression.");
  263. emitByte(OP_POP);
  264. }
  265. static void patchJump(int offset) {
  266. // -2 to adjust for the bytecode for the jump offset itself.
  267. int jump = currentChunk()->count - offset - 2;
  268. if (jump > UINT16_MAX) error("Too much code to jump over.");
  269. currentChunk()->code[offset] = (jump >> 8) & 0xFF;
  270. currentChunk()->code[offset + 1] = jump & 0xFF;
  271. }
  272. static void forStatement() {
  273. /**
  274. * *********************************************************
  275. * initializer clause
  276. * L4: condition expression
  277. * OP_JUMP_IF_FALSE L1
  278. * OP_POP
  279. * OP_JUMP L3
  280. * L2: increment expression
  281. * OP_POP
  282. * OP_LOOP L4
  283. * L3: body statement
  284. * OP_LOOP L2
  285. * L1: OP_POP
  286. * continues...
  287. * *********************************************************
  288. */
  289. beginScope();
  290. // for(I;II;III)
  291. consume(TOKEN_LEFT_PAREN, "Expect '(' after 'for'.");
  292. ///! I.<<Initializer Part>>
  293. if (match(TOKEN_SEMICOLON)) {
  294. // No initializer.
  295. } else if (match(TOKEN_VAR)) {
  296. varDeclaration();
  297. } else {
  298. expressionStatement();// ; op_pop
  299. }
  300. ///! II.<<Condition Part>>
  301. int loopStart = currentChunk()->count;
  302. int exitJump = -1;
  303. if (!match(TOKEN_SEMICOLON)) {
  304. expression();
  305. consume(TOKEN_SEMICOLON, "Expect ';' after loop conditon. ");
  306. // Jump out of the loop if the condition is false.
  307. exitJump = emitJump(OP_JUMP_IF_FALSE);
  308. emitByte(OP_POP);
  309. }
  310. ///! III.<<Increment Clause>>
  311. if (!match(TOKEN_RIGHT_PAREN)) {
  312. int bodyJump = emitJump(OP_JUMP);
  313. int incrementStart = currentChunk()->count;
  314. expression();
  315. emitByte(OP_POP);
  316. consume(TOKEN_RIGHT_PAREN, "Expect ')' after for clauses.");
  317. emitLoop(loopStart);
  318. loopStart = incrementStart;
  319. patchJump(bodyJump);
  320. }
  321. ///! <<Body Part>>
  322. statement();
  323. emitLoop(loopStart);
  324. if (exitJump != -1) {
  325. patchJump(exitJump);
  326. emitByte(OP_POP);// Condition.
  327. }
  328. endScope();
  329. }
  330. static void returnStatement() {
  331. if (current->type == TYPE_SCRIPT) {
  332. error("Can't return from top-level code.");
  333. }
  334. if (match(TOKEN_SEMICOLON)) {
  335. emitReturn();
  336. } else {
  337. if (current->type == TYPE_INITIALIZER) {
  338. error("Can't return a value from an initializer.");
  339. }
  340. expression();
  341. consume(TOKEN_SEMICOLON, "Expect ';' after return value.");
  342. emitByte(OP_RETURN);
  343. }
  344. }
  345. static void ifStatement() {
  346. consume(TOKEN_LEFT_PAREN, "Expect '(' after 'if'.");
  347. expression();
  348. consume(TOKEN_RIGHT_PAREN, "Expect ')' after condition.");
  349. int thenJump = emitJump(OP_JUMP_IF_FALSE);
  350. emitByte(OP_POP);
  351. statement();// then branch statement
  352. int elseJump = emitJump(OP_JUMP);
  353. patchJump(thenJump);
  354. emitByte(OP_POP);
  355. if (match(TOKEN_ELSE)) statement();// else branch statement
  356. patchJump(elseJump);
  357. }
  358. static void whileStatement() {
  359. int loopStart = currentChunk()->count;
  360. consume(TOKEN_LEFT_PAREN, "Expect '(' after 'while'.");
  361. expression();// while condition.
  362. consume(TOKEN_RIGHT_PAREN, "Expect ')' after condition.");
  363. int exitJump = emitJump(OP_JUMP_IF_FALSE);
  364. emitByte(OP_POP);
  365. statement();// while body.
  366. emitLoop(loopStart);
  367. patchJump(exitJump);
  368. emitByte(OP_POP);
  369. }
  370. static void printStatement() {
  371. expression();
  372. consume(TOKEN_SEMICOLON, "Expect ';' after value.");
  373. emitByte(OP_PRINT);
  374. }
  375. static void synchronize() {
  376. parser.panicMode = false;
  377. while (parser.current.type != TOKEN_EOF) {
  378. if (parser.previous.type == TOKEN_SEMICOLON) return;
  379. switch (parser.current.type) {
  380. case TOKEN_CLASS:
  381. case TOKEN_FUN:
  382. case TOKEN_VAR:
  383. case TOKEN_FOR:
  384. case TOKEN_IF:
  385. case TOKEN_WHILE:
  386. case TOKEN_PRINT:
  387. case TOKEN_RETURN:
  388. return;
  389. default:;// Do nothing.
  390. }
  391. advance();
  392. }
  393. }
  394. ///declaration → classDecl
  395. /// | funDecl
  396. /// | varDecl
  397. /// | statement ;
  398. static void declaration() {
  399. if (match(TOKEN_CLASS)) {
  400. classDeclaration();
  401. } else if (match(TOKEN_FUN)) {
  402. funDeclaration();
  403. } else if (match(TOKEN_VAR)) {
  404. varDeclaration();
  405. } else {
  406. statement();
  407. }
  408. if (parser.panicMode) synchronize();
  409. }
  410. static void block() {
  411. while (!check(TOKEN_RIGHT_BRACE) && !check(TOKEN_EOF)) {
  412. declaration();
  413. }
  414. consume(TOKEN_RIGHT_BRACE, "Expect '}' after block.");
  415. }
  416. ///statement → exprStmt
  417. /// | forStmt
  418. /// | ifStmt
  419. /// | printStmt
  420. /// | returnStmt
  421. /// | whileStmt
  422. /// | block ;
  423. static void statement() {
  424. if (match(TOKEN_PRINT)) {
  425. printStatement();
  426. } else if (match(TOKEN_FOR)) {
  427. forStatement();
  428. } else if (match(TOKEN_IF)) {
  429. ifStatement();
  430. } else if (match(TOKEN_RETURN)) {
  431. returnStatement();
  432. } else if (match(TOKEN_WHILE)) {
  433. whileStatement();
  434. } else if (match(TOKEN_LEFT_BRACE)) {
  435. ///! block → "{" declaration* "}" ;
  436. beginScope();
  437. block();
  438. endScope();
  439. } else {
  440. expressionStatement();
  441. }
  442. }
  443. static void beginScope() {
  444. current->scopeDepth++;
  445. }
  446. static void endScope() {
  447. current->scopeDepth--;
  448. while (current->localCount > 0
  449. && current->locals[current->localCount - 1].depth > current->scopeDepth) {
  450. if (current->locals[current->localCount - 1].isCaptured) {
  451. emitByte(OP_CLOSE_UPVALUE);
  452. } else {
  453. emitByte(OP_POP);
  454. }
  455. current->localCount--;
  456. }
  457. }
  458. static void emitConstant(Value value) {
  459. emitBytes(OP_CONSTANT, makeConstant(value));
  460. }
  461. static void initCompiler(Compiler *compiler, FunctionType type) {
  462. compiler->enclosing = current;// 上一极的 compiler
  463. compiler->function = NULL;
  464. compiler->type = type;
  465. compiler->localCount = 0;
  466. compiler->scopeDepth = 0;
  467. compiler->function = newFunction();
  468. current = compiler;
  469. if (type != TYPE_SCRIPT) {
  470. ///! Function name
  471. current->function->name = copyString(parser.previous.start, parser.previous.length);
  472. }
  473. Local *local = &current->locals[current->localCount++];
  474. local->depth = 0;
  475. local->isCaptured = false;
  476. if (type != TYPE_FUNCTION) {
  477. local->name.start = "this";
  478. local->name.length = 4;
  479. } else {
  480. local->name.start = "";
  481. local->name.length = 0;
  482. }
  483. }
  484. static ObjFunction *endCompiler() {
  485. emitReturn();
  486. ObjFunction *function = current->function;
  487. #ifdef DEBUG_PRINT_CODE
  488. if (!parser.hadError) {
  489. disassembleChunk(currentChunk(), function->name != NULL ? function->name->chars : "<script>");
  490. }
  491. #endif
  492. current = current->enclosing;// 当前的 compiler 变成上级 compiler
  493. return function;
  494. }
  495. static void grouping(__attribute__((unused)) bool canAssign) {
  496. expression();
  497. consume(TOKEN_RIGHT_PAREN, "Expect ')' after expression.");
  498. }
  499. /// \brief parse number token
  500. /// Number literals: 123
  501. static void number(__attribute__((unused)) bool canAssign) {
  502. double value = strtod(parser.previous.start, NULL);
  503. emitConstant(NUMBER_VAL(value));
  504. }
  505. static void string(__attribute__((unused)) bool canAssign) {
  506. emitConstant(OBJ_VAL(copyString(parser.previous.start + 1,
  507. parser.previous.length - 2)));
  508. }
  509. static void and_(__attribute__((unused)) bool canAssign) {
  510. /// left operand expression
  511. /// OP_JUMP_IF_FALSE b
  512. /// OP_POP
  513. /// right operand expression
  514. /// b: continues...
  515. int endJump = emitJump(OP_JUMP_IF_FALSE);
  516. emitByte(OP_POP);
  517. parsePrecedence(PREC_AND);// right operand expression
  518. patchJump(endJump);
  519. }
  520. static void or_(__attribute__((unused)) bool canAssign) {
  521. /// left operand expression
  522. /// OP_JUMP_IF_FALSE b1
  523. /// OP_JUMP b2
  524. /// b1: OP_POP
  525. /// right operand expression
  526. /// b2: continue
  527. int elseJump = emitJump(OP_JUMP_IF_FALSE);
  528. int endJump = emitJump(OP_JUMP);
  529. patchJump(elseJump);
  530. emitByte(OP_POP);
  531. parsePrecedence(PREC_OR);// right operand expression
  532. patchJump(endJump);
  533. }
  534. static int resolveLocal(Compiler *compile, Token *name) {
  535. for (int i = compile->localCount - 1; i >= 0; i--) {
  536. Local *local = &compile->locals[i];
  537. if (identifiersEqual(name, &local->name)) {
  538. if (local->depth == -1) {
  539. error("Can't read local variable in ints own initializer.");
  540. }
  541. return i;
  542. }
  543. }
  544. return -1;
  545. }
  546. static int addUpvalue(Compiler *compiler, uint8_t index, bool isLocal) {
  547. int upvalueCount = compiler->function->upvalueCount;
  548. //! before we add a new upvalue,
  549. //! we first check to see if the function already has an upvalue
  550. //! that closes over that variable.
  551. for (int i = 0; i < upvalueCount; i++) {
  552. Upvalue *upvalue = &compiler->upvalues[i];
  553. if (upvalue->index == index && upvalue->isLocal == isLocal) {
  554. return i;
  555. }
  556. }
  557. if (upvalueCount == UINT8_COUNT) {
  558. error("Too many closure variable in function.");
  559. return 0;
  560. }
  561. compiler->upvalues[upvalueCount].isLocal = isLocal;
  562. compiler->upvalues[upvalueCount].index = index;
  563. return compiler->function->upvalueCount++;
  564. }
  565. static int resolveUpvalue(Compiler *compiler, Token *name) {
  566. if (compiler->enclosing == NULL) return -1;
  567. int local = resolveLocal(compiler->enclosing, name);
  568. if (local != -1) {
  569. compiler->enclosing->locals[local].isCaptured = true;
  570. return addUpvalue(compiler, (uint8_t) local, true);
  571. }
  572. // 递归
  573. int upvalue = resolveUpvalue(compiler->enclosing, name);
  574. if (upvalue != -1) {
  575. return addUpvalue(compiler, (uint8_t) upvalue, false);
  576. }
  577. return -1;
  578. }
  579. static void namedVariable(Token name, bool canAssign) {
  580. uint8_t getOp, setOp;
  581. int arg = resolveLocal(current, &name);
  582. if (arg != -1) {
  583. getOp = OP_GET_LOCAL;
  584. setOp = OP_SET_LOCAL;
  585. } else if ((arg = resolveUpvalue(current, &name)) != -1) {
  586. getOp = OP_GET_UPVALUE;
  587. setOp = OP_SET_UPVALUE;
  588. } else {
  589. arg = identifierConstant(&name);
  590. getOp = OP_GET_GLOBAL;
  591. setOp = OP_SET_GLOBAL;
  592. }
  593. if (canAssign && match(TOKEN_EQUAL)) {
  594. // 如 menu.brunch(sunday).beverage = "mimosa";
  595. expression();
  596. emitBytes(setOp, (uint8_t) arg);
  597. } else {
  598. emitBytes(getOp, (uint8_t) arg);
  599. }
  600. }
  601. static void variable(bool canAssign) {
  602. namedVariable(parser.previous, canAssign);
  603. }
  604. static void this_(__attribute__((unused)) bool canAssign) {
  605. if (currentClass == NULL) {
  606. error("Can't use 'this' outside of a class.");
  607. return;
  608. }
  609. variable(false);
  610. }
  611. /// Unary negation: -123
  612. static void unary(__attribute__((unused)) bool canAssign) {
  613. TokenType operatorType = parser.previous.type;
  614. // Compile the operand.
  615. parsePrecedence(PREC_UNARY);
  616. // Emit the operator instruction.
  617. switch (operatorType) {
  618. case TOKEN_BANG:
  619. emitByte(OP_NOT);
  620. break;
  621. case TOKEN_MINUS:
  622. emitByte(OP_NEGATE);
  623. break;
  624. default: return;// Unreachable.
  625. }
  626. }
  627. /// \brief infix parser
  628. static void binary(__attribute__((unused)) bool canAssign) {
  629. TokenType operatorType = parser.previous.type;
  630. ParseRule *rule = getRule(operatorType);
  631. parsePrecedence((Precedence) rule->precedence + 1);
  632. switch (operatorType) {
  633. case TOKEN_BANG_EQUAL:
  634. emitBytes(OP_EQUAL, OP_NOT);
  635. break;
  636. case TOKEN_EQUAL_EQUAL:
  637. emitByte(OP_EQUAL);
  638. break;
  639. case TOKEN_GREATER:
  640. emitByte(OP_GREATER);
  641. break;
  642. case TOKEN_GREATER_EQUAL:
  643. emitBytes(OP_LESS, OP_NOT);
  644. break;
  645. case TOKEN_LESS:
  646. emitByte(OP_LESS);
  647. break;
  648. case TOKEN_LESS_EQUAL:
  649. emitBytes(OP_GREATER, OP_NOT);
  650. break;
  651. case TOKEN_PLUS:
  652. emitByte(OP_ADD);
  653. break;
  654. case TOKEN_MINUS:
  655. emitByte(OP_SUBTRACT);
  656. break;
  657. case TOKEN_STAR:
  658. emitByte(OP_MULTIPLY);
  659. break;
  660. case TOKEN_SLASH:
  661. emitByte(OP_DIVIDE);
  662. break;
  663. default: return;// Unreachable.
  664. }
  665. }
  666. static uint8_t argumentList() {
  667. uint8_t argCount = 0;
  668. if (!check(TOKEN_RIGHT_PAREN)) {
  669. do {
  670. expression();
  671. if (argCount == 255) error("Can't have more than 255 arguments.");
  672. argCount++;
  673. } while (match(TOKEN_COMMA));
  674. }
  675. consume(TOKEN_RIGHT_PAREN, "Expect ')' after arguments.");
  676. return argCount;
  677. }
  678. /// Function call expression is kind of an infix ( operator.
  679. /// You have a high-precedence expression on the left for the thing
  680. /// being called - usually just a single identifier.
  681. /// Then the ( in the middle, followed by the argument expressions
  682. /// separated by commas, and a final ) to wrap it up at the end.
  683. static void call(__attribute__((unused)) bool canAssign) {
  684. uint8_t argCount = argumentList();
  685. emitBytes(OP_CALL, argCount);
  686. }
  687. static void literal(__attribute__((unused)) bool canAssign) {
  688. switch (parser.previous.type) {
  689. case TOKEN_FALSE:
  690. emitByte(OP_FALSE);
  691. break;
  692. case TOKEN_NIL:
  693. emitByte(OP_NIL);
  694. break;
  695. case TOKEN_TRUE:
  696. emitByte(OP_TRUE);
  697. break;
  698. default: return;// Unreachable
  699. }
  700. }
  701. static void dot(bool canAssign) {
  702. // eclair.filling = "pastry creme";
  703. consume(TOKEN_IDENTIFIER, "Expect property name after '.'.");
  704. uint8_t name = identifierConstant(&parser.previous);
  705. if (canAssign && match(TOKEN_EQUAL)) {
  706. expression();
  707. emitBytes(OP_SET_PROPERTY, name);
  708. } else if (match(TOKEN_LEFT_PAREN)) {
  709. // Instance.method()
  710. uint8_t argCount = argumentList();
  711. emitBytes(OP_INVOKE, name);
  712. emitByte(argCount);
  713. } else {
  714. emitBytes(OP_GET_PROPERTY, name);
  715. }
  716. }
  717. ParseRule rules[] = {
  718. [TOKEN_LEFT_PAREN] = {grouping, call, PREC_CALL},
  719. [TOKEN_RIGHT_PAREN] = {NULL, NULL, PREC_NONE},
  720. [TOKEN_LEFT_BRACE] = {NULL, NULL, PREC_NONE},
  721. [TOKEN_RIGHT_BRACE] = {NULL, NULL, PREC_NONE},
  722. [TOKEN_COMMA] = {NULL, NULL, PREC_NONE},
  723. [TOKEN_DOT] = {NULL, dot, PREC_CALL},
  724. [TOKEN_MINUS] = {unary, binary, PREC_TERM},
  725. [TOKEN_PLUS] = {NULL, binary, PREC_TERM},
  726. [TOKEN_SEMICOLON] = {NULL, NULL, PREC_NONE},
  727. [TOKEN_SLASH] = {NULL, binary, PREC_FACTOR},
  728. [TOKEN_STAR] = {NULL, binary, PREC_FACTOR},
  729. [TOKEN_BANG] = {unary, NULL, PREC_NONE},
  730. [TOKEN_BANG_EQUAL] = {NULL, binary, PREC_EQUALITY},
  731. [TOKEN_EQUAL] = {NULL, NULL, PREC_NONE},
  732. [TOKEN_EQUAL_EQUAL] = {NULL, binary, PREC_EQUALITY},
  733. [TOKEN_GREATER] = {NULL, binary, PREC_COMPARISON},
  734. [TOKEN_GREATER_EQUAL] = {NULL, binary, PREC_COMPARISON},
  735. [TOKEN_LESS] = {NULL, binary, PREC_COMPARISON},
  736. [TOKEN_LESS_EQUAL] = {NULL, binary, PREC_COMPARISON},
  737. [TOKEN_IDENTIFIER] = {variable, NULL, PREC_NONE},
  738. [TOKEN_STRING] = {string, NULL, PREC_NONE},
  739. [TOKEN_NUMBER] = {number, NULL, PREC_NONE},
  740. [TOKEN_AND] = {NULL, and_, PREC_AND},
  741. [TOKEN_CLASS] = {NULL, NULL, PREC_NONE},
  742. [TOKEN_ELSE] = {NULL, NULL, PREC_NONE},
  743. [TOKEN_FALSE] = {literal, NULL, PREC_NONE},
  744. [TOKEN_FOR] = {NULL, NULL, PREC_NONE},
  745. [TOKEN_FUN] = {NULL, NULL, PREC_NONE},
  746. [TOKEN_IF] = {NULL, NULL, PREC_NONE},
  747. [TOKEN_NIL] = {literal, NULL, PREC_NONE},
  748. [TOKEN_OR] = {NULL, or_, PREC_OR},
  749. [TOKEN_PRINT] = {NULL, NULL, PREC_NONE},
  750. [TOKEN_RETURN] = {NULL, NULL, PREC_NONE},
  751. [TOKEN_SUPER] = {NULL, NULL, PREC_NONE},
  752. [TOKEN_THIS] = {this_, NULL, PREC_NONE},
  753. [TOKEN_TRUE] = {literal, NULL, PREC_NONE},
  754. [TOKEN_VAR] = {NULL, NULL, PREC_NONE},
  755. [TOKEN_WHILE] = {NULL, NULL, PREC_NONE},
  756. [TOKEN_ERROR] = {NULL, NULL, PREC_NONE},
  757. [TOKEN_EOF] = {NULL, NULL, PREC_NONE},
  758. };
  759. /// \brief 优先级处理
  760. /// \param precedence
  761. static void parsePrecedence(Precedence precedence) {
  762. advance();
  763. ParseFn prefixRule = getRule(parser.previous.type)->prefix;
  764. if (prefixRule == NULL) {
  765. error("Expect expression.");
  766. return;
  767. }
  768. bool canAssign = precedence <= PREC_ASSIGNMENT;
  769. prefixRule(canAssign);///! 执行具体函数
  770. while (precedence <= getRule(parser.current.type)->precedence) {
  771. advance();
  772. ParseFn infixRule = getRule(parser.previous.type)->infix;
  773. infixRule(canAssign);///! 执行具体函数
  774. }
  775. if (canAssign && match(TOKEN_EQUAL)) {
  776. error("Invalid assignment target.");
  777. }
  778. }
  779. static uint8_t identifierConstant(Token *name) {
  780. return makeConstant(OBJ_VAL(copyString(name->start, name->length)));
  781. }
  782. static bool identifiersEqual(Token *a, Token *b) {
  783. if (a->length != b->length) return false;
  784. return memcmp(a->start, b->start, a->length) == 0;
  785. }
  786. static void addLocal(Token name) {
  787. if (current->localCount == UINT8_COUNT) {
  788. error("Too many local variables in function.");
  789. return;
  790. }
  791. Local *local = &current->locals[current->localCount++];
  792. local->name = name;
  793. local->depth = -1;// 未初始化
  794. local->isCaptured = false;
  795. }
  796. static void declareVariable() {
  797. if (current->scopeDepth == 0) return;
  798. Token *name = &parser.previous;
  799. // we detect the error like:
  800. // {
  801. // var a = "first";
  802. // var a = "second";
  803. // }
  804. for (int i = current->localCount - 1; i >= 0; i--) {
  805. Local *local = &current->locals[i];
  806. if (local->depth != -1 && local->depth < current->scopeDepth) {
  807. break;
  808. }
  809. if (identifiersEqual(name, &local->name)) {
  810. error("Already a variable with the name in this scope.");
  811. }
  812. }
  813. addLocal(*name);
  814. }
  815. static uint8_t parseVariable(const char *errorMessage) {
  816. consume(TOKEN_IDENTIFIER, errorMessage);
  817. declareVariable();// 处理本地变量
  818. if (current->scopeDepth > 0) return 0;
  819. return identifierConstant(&parser.previous);
  820. }
  821. static void markInitialized() {
  822. if (current->scopeDepth == 0) return;
  823. current->locals[current->localCount - 1].depth = current->scopeDepth;
  824. }
  825. static void defineVariable(uint8_t global) {
  826. if (current->scopeDepth > 0) {
  827. // markInitialized 未初始化时值 为 -1
  828. markInitialized();
  829. // 本地变量,直接退出
  830. return;
  831. }
  832. emitBytes(OP_DEFINE_GLOBAL, global);
  833. }
  834. static ParseRule *getRule(TokenType type) {
  835. return &rules[type];
  836. }
  837. /**
  838. ******************************************************************************
  839. * statement → exprStmt
  840. | forStmt
  841. | ifStmt
  842. | printStmt
  843. | returnStmt
  844. | whileStmt
  845. | block ;
  846. block → "{" declaration* "}" ;
  847. declaration → classDecl
  848. | funDecl
  849. | varDecl
  850. | statement ;
  851. ******************************************************************************
  852. */
  853. ObjFunction *compile(const char *source) {
  854. initScanner(source);
  855. Compiler compiler;
  856. initCompiler(&compiler, TYPE_SCRIPT);
  857. parser.hadError = false;
  858. parser.panicMode = false;
  859. advance();
  860. while (!match(TOKEN_EOF)) {
  861. declaration();
  862. }
  863. ObjFunction *function = endCompiler();
  864. return parser.hadError ? NULL : function;
  865. }
  866. void markCompilerRoots() {
  867. Compiler *compiler = current;
  868. while (compiler != NULL) {
  869. markObject((Obj *) compiler->function);
  870. compiler = compiler->enclosing;
  871. }
  872. }