Parser.cpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412
  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. /// putchard - putchar that takes a double and returns 0.
  15. extern "C" double putchard(double X) {
  16. fputc((char)X, stderr);
  17. return 0;
  18. }
  19. /// printd - printf that takes a double prints it as "%f\n", returning 0.
  20. extern "C" double printd(double X) {
  21. fprintf(stderr, "%f\n", X);
  22. return 0;
  23. }
  24. //===----------------------------------------------------------------------===//
  25. // Parser
  26. //===----------------------------------------------------------------------===//
  27. /// CurTok/getNextToken - Provide a simple token buffer. CurTok is the current
  28. /// token the parser is looking at. getNextToken reads another token from the
  29. /// lexer and updates CurTok with its results.
  30. int getNextToken() { return CurTok = gettok(); }
  31. /// BinopPrecedence - This holds the precedence for each binary operator that is
  32. /// defined.
  33. /// GetTokPrecedence - Get the precedence of the pending binary operator token.
  34. static int GetTokPrecedence() {
  35. if (!isascii(CurTok))
  36. return -1;
  37. // Make sure it's a declared binop.
  38. int TokPrec = BinopPrecedence[CurTok];
  39. if (TokPrec <= 0)
  40. return -1;
  41. return TokPrec;
  42. }
  43. /// Error* - These are little helper functions for error handling.
  44. std::unique_ptr<ExprAST> Error(const char *Str) {
  45. fprintf(stderr, "Error: %s\n", Str);
  46. return nullptr;
  47. }
  48. std::unique_ptr<PrototypeAST> ErrorP(const char *Str) {
  49. Error(Str);
  50. return nullptr;
  51. }
  52. static std::unique_ptr<ExprAST> ParseExpression();
  53. /// numberexpr ::= number
  54. static std::unique_ptr<ExprAST> ParseNumberExpr() {
  55. auto Result = llvm::make_unique<NumberExprAST>(LexerObjects::NumVal);
  56. getNextToken(); // consume the number
  57. return std::move(Result);
  58. }
  59. /// parenexpr ::= '(' expression ')'
  60. static std::unique_ptr<ExprAST> ParseParenExpr() {
  61. getNextToken(); // eat (.
  62. auto V = ParseExpression();
  63. if (!V)
  64. return nullptr;
  65. if (CurTok != ')')
  66. return Error("expected ')'");
  67. getNextToken(); // eat ).
  68. return V;
  69. }
  70. /// identifierexpr
  71. /// ::= identifier
  72. /// ::= identifier '(' expression* ')'
  73. static std::unique_ptr<ExprAST> ParseIdentifierExpr() {
  74. std::string IdName = LexerObjects::IdentifierStr;
  75. getNextToken(); // eat identifier.
  76. if (CurTok != '(') // Simple variable ref.
  77. return llvm::make_unique<VariableExprAST>(IdName);
  78. // Call.
  79. getNextToken(); // eat (
  80. std::vector<std::unique_ptr<ExprAST>> Args;
  81. if (CurTok != ')') {
  82. while (1) {
  83. if (auto Arg = ParseExpression())
  84. Args.push_back(std::move(Arg));
  85. else
  86. return nullptr;
  87. if (CurTok == ')')
  88. break;
  89. if (CurTok != ',')
  90. return Error("Expected ')' or ',' in argument list");
  91. getNextToken();
  92. }
  93. }
  94. // Eat the ')'.
  95. getNextToken();
  96. return llvm::make_unique<CallExprAST>(IdName, std::move(Args));
  97. }
  98. /// ifexpr ::= 'if' expression 'then' expression 'else' expression
  99. static std::unique_ptr<ExprAST> ParseIfExpr() {
  100. getNextToken(); // eat the if.
  101. // condition.
  102. auto Cond = ParseExpression();
  103. if (!Cond)
  104. return nullptr;
  105. if (CurTok != tok_then)
  106. return Error("expected then");
  107. getNextToken(); // eat the then
  108. auto Then = ParseExpression();
  109. if (!Then)
  110. return nullptr;
  111. if (CurTok != tok_else)
  112. return Error("expected else");
  113. getNextToken();
  114. auto Else = ParseExpression();
  115. if (!Else)
  116. return nullptr;
  117. return llvm::make_unique<IfExprAST>(std::move(Cond), std::move(Then),
  118. std::move(Else));
  119. }
  120. /// forexpr ::= 'for' identifier '=' expr ',' expr (',' expr)? 'in' expression
  121. static std::unique_ptr<ExprAST> ParseForExpr() {
  122. getNextToken(); // eat the for.
  123. if (CurTok != tok_identifier)
  124. return Error("expected identifier after for");
  125. std::string IdName = LexerObjects::IdentifierStr;
  126. getNextToken(); // eat identifier.
  127. if (CurTok != '=')
  128. return Error("expected '=' after for");
  129. getNextToken(); // eat '='.
  130. auto Start = ParseExpression();
  131. if (!Start)
  132. return nullptr;
  133. if (CurTok != ',')
  134. return Error("expected ',' after for start value");
  135. getNextToken();
  136. auto End = ParseExpression();
  137. if (!End)
  138. return nullptr;
  139. // The step value is optional.
  140. std::unique_ptr<ExprAST> Step;
  141. if (CurTok == ',') {
  142. getNextToken();
  143. Step = ParseExpression();
  144. if (!Step)
  145. return nullptr;
  146. }
  147. if (CurTok != tok_in)
  148. return Error("expected 'in' after for");
  149. getNextToken(); // eat 'in'.
  150. auto Body = ParseExpression();
  151. if (!Body)
  152. return nullptr;
  153. return llvm::make_unique<ForExprAST>(IdName, std::move(Start),
  154. std::move(End), std::move(Step),
  155. std::move(Body));
  156. }
  157. /// primary
  158. /// ::= identifierexpr
  159. /// ::= numberexpr
  160. /// ::= parenexpr
  161. static std::unique_ptr<ExprAST> ParsePrimary() {
  162. switch (CurTok) {
  163. default:
  164. return Error("unknown token when expecting an expression");
  165. case tok_identifier:
  166. return ParseIdentifierExpr();
  167. case tok_number:
  168. return ParseNumberExpr();
  169. case '(':
  170. return ParseParenExpr();
  171. case tok_if:
  172. return ParseIfExpr();
  173. case tok_for:
  174. return ParseForExpr();
  175. }
  176. }
  177. /// binoprhs
  178. /// ::= ('+' primary)*
  179. static std::unique_ptr<ExprAST> ParseBinOpRHS(int ExprPrec,
  180. std::unique_ptr<ExprAST> LHS) {
  181. // If this is a binop, find its precedence.
  182. while (1) {
  183. int TokPrec = GetTokPrecedence();
  184. // If this is a binop that binds at least as tightly as the current binop,
  185. // consume it, otherwise we are done.
  186. if (TokPrec < ExprPrec)
  187. return LHS;
  188. // Okay, we know this is a binop.
  189. int BinOp = CurTok;
  190. getNextToken(); // eat binop
  191. // Parse the primary expression after the binary operator.
  192. auto RHS = ParsePrimary();
  193. if (!RHS)
  194. return nullptr;
  195. // If BinOp binds less tightly with RHS than the operator after RHS, let
  196. // the pending operator take RHS as its LHS.
  197. int NextPrec = GetTokPrecedence();
  198. if (TokPrec < NextPrec) {
  199. RHS = ParseBinOpRHS(TokPrec + 1, std::move(RHS));
  200. if (!RHS)
  201. return nullptr;
  202. }
  203. // Merge LHS/RHS.
  204. LHS =
  205. llvm::make_unique<BinaryExprAST>(BinOp, std::move(LHS), std::move(RHS));
  206. }
  207. }
  208. /// expression
  209. /// ::= primary binoprhs
  210. ///
  211. static std::unique_ptr<ExprAST> ParseExpression() {
  212. auto LHS = ParsePrimary();
  213. if (!LHS)
  214. return nullptr;
  215. return ParseBinOpRHS(0, std::move(LHS));
  216. }
  217. /// prototype
  218. /// ::= id '(' id* ')'
  219. static std::unique_ptr<PrototypeAST> ParsePrototype() {
  220. if (CurTok != tok_identifier)
  221. return ErrorP("Expected function name in prototype");
  222. std::string FnName = LexerObjects::IdentifierStr;
  223. getNextToken();
  224. if (CurTok != '(')
  225. return ErrorP("Expected '(' in prototype");
  226. std::vector<std::string> ArgNames;
  227. while (getNextToken() == tok_identifier)
  228. ArgNames.push_back(LexerObjects::IdentifierStr);
  229. if (CurTok != ')')
  230. return ErrorP("Expected ')' in prototype");
  231. // success.
  232. getNextToken(); // eat ')'.
  233. return llvm::make_unique<PrototypeAST>(FnName, std::move(ArgNames));
  234. }
  235. /// definition ::= 'def' prototype expression
  236. static std::unique_ptr<FunctionAST> ParseDefinition() {
  237. getNextToken(); // eat def.
  238. auto Proto = ParsePrototype();
  239. if (!Proto)
  240. return nullptr;
  241. if (auto E = ParseExpression())
  242. return llvm::make_unique<FunctionAST>(std::move(Proto), std::move(E));
  243. return nullptr;
  244. }
  245. /// toplevelexpr ::= expression
  246. static std::unique_ptr<FunctionAST> ParseTopLevelExpr() {
  247. if (auto E = ParseExpression()) {
  248. // Make an anonymous proto.
  249. auto Proto = llvm::make_unique<PrototypeAST>("__anon_expr",
  250. std::vector<std::string>());
  251. return llvm::make_unique<FunctionAST>(std::move(Proto), std::move(E));
  252. }
  253. return nullptr;
  254. }
  255. /// external ::= 'extern' prototype
  256. static std::unique_ptr<PrototypeAST> ParseExtern() {
  257. getNextToken(); // eat extern.
  258. return ParsePrototype();
  259. }
  260. //===----------------------------------------------------------------------===//
  261. // Top-Level parsing and JIT Driver
  262. //===----------------------------------------------------------------------===//
  263. static void HandleDefinition() {
  264. if (auto FnAST = ParseDefinition()) {
  265. if (auto *FnIR = FnAST->codegen()) {
  266. fprintf(stderr, "Read function definition:");
  267. FnIR->dump();
  268. JITObjects::TheJIT->addModule(std::move(AstObjects::TheModule));
  269. InitializeModuleAndPassManager();
  270. }
  271. } else {
  272. // Skip token for error recovery.
  273. getNextToken();
  274. }
  275. }
  276. static void HandleExtern() {
  277. if (auto ProtoAST = ParseExtern()) {
  278. if (auto *FnIR = ProtoAST->codegen()) {
  279. fprintf(stderr, "Read extern: ");
  280. FnIR->dump();
  281. JITObjects::FunctionProtos[ProtoAST->getName()] = std::move(ProtoAST);
  282. }
  283. } else {
  284. // Skip token for error recovery.
  285. getNextToken();
  286. }
  287. }
  288. static void HandleTopLevelExpression() {
  289. // Evaluate a top-level expression into an anonymous function.
  290. if (auto FnAST = ParseTopLevelExpr()) {
  291. if (FnAST->codegen()) {
  292. // JIT the module containing the anonymous expression, keeping a handle so
  293. // we can free it later.
  294. auto H = JITObjects::TheJIT->addModule(std::move(AstObjects::TheModule));
  295. InitializeModuleAndPassManager();
  296. // Search the JIT for the __anon_expr symbol.
  297. auto ExprSymbol = JITObjects::TheJIT->findSymbol("__anon_expr");
  298. assert(ExprSymbol && "Function not found");
  299. // Get the symbol's address and cast it to the right type (takes no
  300. // arguments, returns a double) so we can call it as a native function.
  301. double (*FP)() = (double (*)())(intptr_t)ExprSymbol.getAddress();
  302. fprintf(stderr, "Evaluated to %f\n", FP());
  303. // Delete the anonymous expression module from the JIT.
  304. JITObjects::TheJIT->removeModule(H);
  305. }
  306. } else {
  307. // Skip token for error recovery.
  308. getNextToken();
  309. }
  310. }
  311. /// top ::= definition | external | expression | ';'
  312. void MainLoop() {
  313. // Install standard binary operators.
  314. // 1 is lowest precedence.
  315. BinopPrecedence['<'] = 10;
  316. BinopPrecedence['+'] = 20;
  317. BinopPrecedence['-'] = 20;
  318. BinopPrecedence['*'] = 40; // highest.
  319. while (1) {
  320. fprintf(stderr, "ready> ");
  321. switch (CurTok) {
  322. case tok_eof:
  323. return;
  324. case ';': // ignore top-level semicolons.
  325. getNextToken();
  326. break;
  327. case tok_def:
  328. HandleDefinition();
  329. break;
  330. case tok_extern:
  331. HandleExtern();
  332. break;
  333. default:
  334. HandleTopLevelExpression();
  335. break;
  336. }
  337. }
  338. }
  339. }