Parser.cpp 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  1. // Standard includes
  2. #include <cstdio>
  3. #include <stdexcept>
  4. // Local includes
  5. #include "Parser.h"
  6. #include "Lexer.h"
  7. using namespace lexer;
  8. namespace parser{
  9. //===----------------------------------------------------------------------===//
  10. // Parser
  11. //===----------------------------------------------------------------------===//
  12. /// CurTok/getNextToken - Provide a simple token buffer. CurTok is the current
  13. /// token the parser is looking at. getNextToken reads another token from the
  14. /// lexer and updates CurTok with its results.
  15. int getNextToken() { return CurTok = gettok(); }
  16. /// BinopPrecedence - This holds the precedence for each binary operator that is
  17. /// defined.
  18. /// GetTokPrecedence - Get the precedence of the pending binary operator token.
  19. static int GetTokPrecedence() {
  20. if (!isascii(CurTok))
  21. return -1;
  22. // Make sure it's a declared binop.
  23. int TokPrec = BinopPrecedence[CurTok];
  24. if (TokPrec <= 0)
  25. return -1;
  26. return TokPrec;
  27. }
  28. /// Error* - These are little helper functions for error handling.
  29. std::unique_ptr<ExprAST> Error(const char *Str) {
  30. fprintf(stderr, "Error: %s\n", Str);
  31. return nullptr;
  32. }
  33. std::unique_ptr<PrototypeAST> ErrorP(const char *Str) {
  34. Error(Str);
  35. return nullptr;
  36. }
  37. static std::unique_ptr<ExprAST> ParseExpression();
  38. /// numberexpr ::= number
  39. static std::unique_ptr<ExprAST> ParseNumberExpr() {
  40. auto Result = llvm::make_unique<NumberExprAST>(LexerObjects::NumVal);
  41. getNextToken(); // consume the number
  42. return std::move(Result);
  43. }
  44. /// parenexpr ::= '(' expression ')'
  45. static std::unique_ptr<ExprAST> ParseParenExpr() {
  46. getNextToken(); // eat (.
  47. auto V = ParseExpression();
  48. if (!V)
  49. return nullptr;
  50. if (CurTok != ')')
  51. return Error("expected ')'");
  52. getNextToken(); // eat ).
  53. return V;
  54. }
  55. /// identifierexpr
  56. /// ::= identifier
  57. /// ::= identifier '(' expression* ')'
  58. static std::unique_ptr<ExprAST> ParseIdentifierExpr() {
  59. std::string IdName = LexerObjects::IdentifierStr;
  60. getNextToken(); // eat identifier.
  61. if (CurTok != '(') // Simple variable ref.
  62. return llvm::make_unique<VariableExprAST>(IdName);
  63. // Call.
  64. getNextToken(); // eat (
  65. std::vector<std::unique_ptr<ExprAST>> Args;
  66. if (CurTok != ')') {
  67. while (1) {
  68. if (auto Arg = ParseExpression())
  69. Args.push_back(std::move(Arg));
  70. else
  71. return nullptr;
  72. if (CurTok == ')')
  73. break;
  74. if (CurTok != ',')
  75. return Error("Expected ')' or ',' in argument list");
  76. getNextToken();
  77. }
  78. }
  79. // Eat the ')'.
  80. getNextToken();
  81. return llvm::make_unique<CallExprAST>(IdName, std::move(Args));
  82. }
  83. /// primary
  84. /// ::= identifierexpr
  85. /// ::= numberexpr
  86. /// ::= parenexpr
  87. static std::unique_ptr<ExprAST> ParsePrimary() {
  88. switch (CurTok) {
  89. default:
  90. return Error("unknown token when expecting an expression");
  91. case tok_identifier:
  92. return ParseIdentifierExpr();
  93. case tok_number:
  94. return ParseNumberExpr();
  95. case '(':
  96. return ParseParenExpr();
  97. }
  98. }
  99. /// binoprhs
  100. /// ::= ('+' primary)*
  101. static std::unique_ptr<ExprAST> ParseBinOpRHS(int ExprPrec,
  102. std::unique_ptr<ExprAST> LHS) {
  103. // If this is a binop, find its precedence.
  104. while (1) {
  105. int TokPrec = GetTokPrecedence();
  106. // If this is a binop that binds at least as tightly as the current binop,
  107. // consume it, otherwise we are done.
  108. if (TokPrec < ExprPrec)
  109. return LHS;
  110. // Okay, we know this is a binop.
  111. int BinOp = CurTok;
  112. getNextToken(); // eat binop
  113. // Parse the primary expression after the binary operator.
  114. auto RHS = ParsePrimary();
  115. if (!RHS)
  116. return nullptr;
  117. // If BinOp binds less tightly with RHS than the operator after RHS, let
  118. // the pending operator take RHS as its LHS.
  119. int NextPrec = GetTokPrecedence();
  120. if (TokPrec < NextPrec) {
  121. RHS = ParseBinOpRHS(TokPrec + 1, std::move(RHS));
  122. if (!RHS)
  123. return nullptr;
  124. }
  125. // Merge LHS/RHS.
  126. LHS =
  127. llvm::make_unique<BinaryExprAST>(BinOp, std::move(LHS), std::move(RHS));
  128. }
  129. }
  130. /// expression
  131. /// ::= primary binoprhs
  132. ///
  133. static std::unique_ptr<ExprAST> ParseExpression() {
  134. auto LHS = ParsePrimary();
  135. if (!LHS)
  136. return nullptr;
  137. return ParseBinOpRHS(0, std::move(LHS));
  138. }
  139. /// prototype
  140. /// ::= id '(' id* ')'
  141. static std::unique_ptr<PrototypeAST> ParsePrototype() {
  142. if (CurTok != tok_identifier)
  143. return ErrorP("Expected function name in prototype");
  144. std::string FnName = LexerObjects::IdentifierStr;
  145. getNextToken();
  146. if (CurTok != '(')
  147. return ErrorP("Expected '(' in prototype");
  148. std::vector<std::string> ArgNames;
  149. while (getNextToken() == tok_identifier)
  150. ArgNames.push_back(LexerObjects::IdentifierStr);
  151. if (CurTok != ')')
  152. return ErrorP("Expected ')' in prototype");
  153. // success.
  154. getNextToken(); // eat ')'.
  155. return llvm::make_unique<PrototypeAST>(FnName, std::move(ArgNames));
  156. }
  157. /// definition ::= 'def' prototype expression
  158. static std::unique_ptr<FunctionAST> ParseDefinition() {
  159. getNextToken(); // eat def.
  160. auto Proto = ParsePrototype();
  161. if (!Proto)
  162. return nullptr;
  163. if (auto E = ParseExpression())
  164. return llvm::make_unique<FunctionAST>(std::move(Proto), std::move(E));
  165. return nullptr;
  166. }
  167. /// toplevelexpr ::= expression
  168. static std::unique_ptr<FunctionAST> ParseTopLevelExpr() {
  169. if (auto E = ParseExpression()) {
  170. // Make an anonymous proto.
  171. auto Proto = llvm::make_unique<PrototypeAST>("__anon_expr", std::vector<std::string>());
  172. return llvm::make_unique<FunctionAST>(std::move(Proto), std::move(E));
  173. }
  174. return nullptr;
  175. }
  176. /// external ::= 'extern' prototype
  177. static std::unique_ptr<PrototypeAST> ParseExtern() {
  178. getNextToken(); // eat extern.
  179. return ParsePrototype();
  180. }
  181. //===----------------------------------------------------------------------===//
  182. // Top-Level parsing and JIT Driver
  183. //===----------------------------------------------------------------------===//
  184. static void HandleDefinition() {
  185. if (auto FnAST = ParseDefinition()) {
  186. if (auto *FnIR = FnAST->codegen()) {
  187. fprintf(stderr, "Read function definition:");
  188. FnIR->dump();
  189. }
  190. } else {
  191. // Skip token for error recovery.
  192. getNextToken();
  193. }
  194. }
  195. static void HandleExtern() {
  196. if (auto ProtoAST = ParseExtern()) {
  197. if (auto *FnIR = ProtoAST->codegen()) {
  198. fprintf(stderr, "Read extern: ");
  199. FnIR->dump();
  200. }
  201. } else {
  202. // Skip token for error recovery.
  203. getNextToken();
  204. }
  205. }
  206. static void HandleTopLevelExpression() {
  207. // Evaluate a top-level expression into an anonymous function.
  208. if (auto FnAST = ParseTopLevelExpr()) {
  209. if (auto *FnIR = FnAST->codegen()) {
  210. fprintf(stderr, "Read top-level expression:");
  211. FnIR->dump();
  212. }
  213. } else {
  214. // Skip token for error recovery.
  215. getNextToken();
  216. }
  217. }
  218. /// top ::= definition | external | expression | ';'
  219. void MainLoop() {
  220. // Install standard binary operators.
  221. // 1 is lowest precedence.
  222. BinopPrecedence['<'] = 10;
  223. BinopPrecedence['+'] = 20;
  224. BinopPrecedence['-'] = 20;
  225. BinopPrecedence['*'] = 40; // highest.
  226. while (1) {
  227. fprintf(stderr, "ready> ");
  228. switch (CurTok) {
  229. case tok_eof:
  230. return;
  231. case ';': // ignore top-level semicolons.
  232. getNextToken();
  233. break;
  234. case tok_def:
  235. HandleDefinition();
  236. break;
  237. case tok_extern:
  238. HandleExtern();
  239. break;
  240. default:
  241. HandleTopLevelExpression();
  242. break;
  243. }
  244. }
  245. }
  246. }