compiler.c 27 KB

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