Parser.cpp 8.3 KB

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