blob: 789caabce0ff757afa98db6b15b2ed31ec861e04 [file] [log] [blame]
Lang Hames09bf4c12015-08-18 18:11:06 +00001#include "llvm/ADT/STLExtras.h"
Will Dietz981af002013-10-12 00:55:57 +00002#include <cctype>
Erick Tryzelaar21e83ea2009-09-22 21:15:19 +00003#include <cstdio>
Erick Tryzelaar21e83ea2009-09-22 21:15:19 +00004#include <map>
Chandler Carruth605e30e2012-12-04 10:16:57 +00005#include <string>
Erick Tryzelaar21e83ea2009-09-22 21:15:19 +00006#include <vector>
7
8//===----------------------------------------------------------------------===//
9// Lexer
10//===----------------------------------------------------------------------===//
11
12// The lexer returns tokens [0-255] if it is an unknown character, otherwise one
13// of these for known things.
14enum Token {
15 tok_eof = -1,
16
17 // commands
18 tok_def = -2, tok_extern = -3,
19
20 // primary
21 tok_identifier = -4, tok_number = -5
22};
23
24static std::string IdentifierStr; // Filled in if tok_identifier
25static double NumVal; // Filled in if tok_number
26
27/// gettok - Return the next token from standard input.
28static int gettok() {
29 static int LastChar = ' ';
30
31 // Skip any whitespace.
32 while (isspace(LastChar))
33 LastChar = getchar();
34
35 if (isalpha(LastChar)) { // identifier: [a-zA-Z][a-zA-Z0-9]*
36 IdentifierStr = LastChar;
37 while (isalnum((LastChar = getchar())))
38 IdentifierStr += LastChar;
39
40 if (IdentifierStr == "def") return tok_def;
41 if (IdentifierStr == "extern") return tok_extern;
42 return tok_identifier;
43 }
44
45 if (isdigit(LastChar) || LastChar == '.') { // Number: [0-9.]+
46 std::string NumStr;
47 do {
48 NumStr += LastChar;
49 LastChar = getchar();
50 } while (isdigit(LastChar) || LastChar == '.');
51
52 NumVal = strtod(NumStr.c_str(), 0);
53 return tok_number;
54 }
55
56 if (LastChar == '#') {
57 // Comment until end of line.
58 do LastChar = getchar();
59 while (LastChar != EOF && LastChar != '\n' && LastChar != '\r');
60
61 if (LastChar != EOF)
62 return gettok();
63 }
64
65 // Check for end of file. Don't eat the EOF.
66 if (LastChar == EOF)
67 return tok_eof;
68
69 // Otherwise, just return the character as its ascii value.
70 int ThisChar = LastChar;
71 LastChar = getchar();
72 return ThisChar;
73}
74
75//===----------------------------------------------------------------------===//
76// Abstract Syntax Tree (aka Parse Tree)
77//===----------------------------------------------------------------------===//
Juergen Ributzka05c5a932013-11-19 03:08:35 +000078namespace {
Erick Tryzelaar21e83ea2009-09-22 21:15:19 +000079/// ExprAST - Base class for all expression nodes.
80class ExprAST {
81public:
Juergen Ributzka05c5a932013-11-19 03:08:35 +000082 virtual ~ExprAST() {}
Erick Tryzelaar21e83ea2009-09-22 21:15:19 +000083};
84
85/// NumberExprAST - Expression class for numeric literals like "1.0".
86class NumberExprAST : public ExprAST {
Erick Tryzelaar21e83ea2009-09-22 21:15:19 +000087public:
Lang Hames09bf4c12015-08-18 18:11:06 +000088 NumberExprAST(double Val) {}
Erick Tryzelaar21e83ea2009-09-22 21:15:19 +000089};
90
91/// VariableExprAST - Expression class for referencing a variable, like "a".
92class VariableExprAST : public ExprAST {
93 std::string Name;
94public:
Lang Hames09bf4c12015-08-18 18:11:06 +000095 VariableExprAST(const std::string &Name) : Name(Name) {}
Erick Tryzelaar21e83ea2009-09-22 21:15:19 +000096};
97
98/// BinaryExprAST - Expression class for a binary operator.
99class BinaryExprAST : public ExprAST {
Erick Tryzelaar21e83ea2009-09-22 21:15:19 +0000100public:
Lang Hames09bf4c12015-08-18 18:11:06 +0000101 BinaryExprAST(char Op, std::unique_ptr<ExprAST> LHS,
102 std::unique_ptr<ExprAST> RHS) {}
Erick Tryzelaar21e83ea2009-09-22 21:15:19 +0000103};
104
105/// CallExprAST - Expression class for function calls.
106class CallExprAST : public ExprAST {
107 std::string Callee;
Lang Hames09bf4c12015-08-18 18:11:06 +0000108 std::vector<std::unique_ptr<ExprAST>> Args;
Erick Tryzelaar21e83ea2009-09-22 21:15:19 +0000109public:
Lang Hames09bf4c12015-08-18 18:11:06 +0000110 CallExprAST(const std::string &Callee,
111 std::vector<std::unique_ptr<ExprAST>> Args)
112 : Callee(Callee), Args(std::move(Args)) {}
Erick Tryzelaar21e83ea2009-09-22 21:15:19 +0000113};
114
115/// PrototypeAST - This class represents the "prototype" for a function,
116/// which captures its name, and its argument names (thus implicitly the number
117/// of arguments the function takes).
118class PrototypeAST {
119 std::string Name;
120 std::vector<std::string> Args;
121public:
Lang Hames09bf4c12015-08-18 18:11:06 +0000122 PrototypeAST(const std::string &Name, std::vector<std::string> Args)
123 : Name(Name), Args(std::move(Args)) {}
Erick Tryzelaar21e83ea2009-09-22 21:15:19 +0000124
125};
126
127/// FunctionAST - This class represents a function definition itself.
128class FunctionAST {
Erick Tryzelaar21e83ea2009-09-22 21:15:19 +0000129public:
Lang Hames09bf4c12015-08-18 18:11:06 +0000130 FunctionAST(std::unique_ptr<PrototypeAST> Proto,
131 std::unique_ptr<ExprAST> Body) {}
Erick Tryzelaar21e83ea2009-09-22 21:15:19 +0000132};
Juergen Ributzka05c5a932013-11-19 03:08:35 +0000133} // end anonymous namespace
Erick Tryzelaar21e83ea2009-09-22 21:15:19 +0000134
135//===----------------------------------------------------------------------===//
136// Parser
137//===----------------------------------------------------------------------===//
138
139/// CurTok/getNextToken - Provide a simple token buffer. CurTok is the current
140/// token the parser is looking at. getNextToken reads another token from the
141/// lexer and updates CurTok with its results.
142static int CurTok;
143static int getNextToken() {
144 return CurTok = gettok();
145}
146
147/// BinopPrecedence - This holds the precedence for each binary operator that is
148/// defined.
149static std::map<char, int> BinopPrecedence;
150
151/// GetTokPrecedence - Get the precedence of the pending binary operator token.
152static int GetTokPrecedence() {
153 if (!isascii(CurTok))
154 return -1;
155
156 // Make sure it's a declared binop.
157 int TokPrec = BinopPrecedence[CurTok];
158 if (TokPrec <= 0) return -1;
159 return TokPrec;
160}
161
162/// Error* - These are little helper functions for error handling.
Lang Hames09bf4c12015-08-18 18:11:06 +0000163std::unique_ptr<ExprAST> Error(const char *Str) {
164 fprintf(stderr, "Error: %s\n", Str);
165 return nullptr;
166}
167std::unique_ptr<PrototypeAST> ErrorP(const char *Str) {
168 Error(Str);
169 return nullptr;
170}
Erick Tryzelaar21e83ea2009-09-22 21:15:19 +0000171
Lang Hames09bf4c12015-08-18 18:11:06 +0000172static std::unique_ptr<ExprAST> ParseExpression();
Erick Tryzelaar21e83ea2009-09-22 21:15:19 +0000173
174/// identifierexpr
175/// ::= identifier
176/// ::= identifier '(' expression* ')'
Lang Hames09bf4c12015-08-18 18:11:06 +0000177static std::unique_ptr<ExprAST> ParseIdentifierExpr() {
Erick Tryzelaar21e83ea2009-09-22 21:15:19 +0000178 std::string IdName = IdentifierStr;
179
180 getNextToken(); // eat identifier.
181
182 if (CurTok != '(') // Simple variable ref.
Lang Hames09bf4c12015-08-18 18:11:06 +0000183 return llvm::make_unique<VariableExprAST>(IdName);
Erick Tryzelaar21e83ea2009-09-22 21:15:19 +0000184
185 // Call.
186 getNextToken(); // eat (
Lang Hames09bf4c12015-08-18 18:11:06 +0000187 std::vector<std::unique_ptr<ExprAST>> Args;
Erick Tryzelaar21e83ea2009-09-22 21:15:19 +0000188 if (CurTok != ')') {
189 while (1) {
Lang Hames09bf4c12015-08-18 18:11:06 +0000190 if (auto Arg = ParseExpression())
191 Args.push_back(std::move(Arg));
192 else
193 return nullptr;
Erick Tryzelaar21e83ea2009-09-22 21:15:19 +0000194
195 if (CurTok == ')') break;
196
197 if (CurTok != ',')
198 return Error("Expected ')' or ',' in argument list");
199 getNextToken();
200 }
201 }
202
203 // Eat the ')'.
204 getNextToken();
205
Lang Hames09bf4c12015-08-18 18:11:06 +0000206 return llvm::make_unique<CallExprAST>(IdName, std::move(Args));
Erick Tryzelaar21e83ea2009-09-22 21:15:19 +0000207}
208
209/// numberexpr ::= number
Lang Hames09bf4c12015-08-18 18:11:06 +0000210static std::unique_ptr<ExprAST> ParseNumberExpr() {
211 auto Result = llvm::make_unique<NumberExprAST>(NumVal);
Erick Tryzelaar21e83ea2009-09-22 21:15:19 +0000212 getNextToken(); // consume the number
Lang Hames09bf4c12015-08-18 18:11:06 +0000213 return std::move(Result);
Erick Tryzelaar21e83ea2009-09-22 21:15:19 +0000214}
215
216/// parenexpr ::= '(' expression ')'
Lang Hames09bf4c12015-08-18 18:11:06 +0000217static std::unique_ptr<ExprAST> ParseParenExpr() {
Erick Tryzelaar21e83ea2009-09-22 21:15:19 +0000218 getNextToken(); // eat (.
Lang Hames09bf4c12015-08-18 18:11:06 +0000219 auto V = ParseExpression();
220 if (!V)
221 return nullptr;
Erick Tryzelaar21e83ea2009-09-22 21:15:19 +0000222
223 if (CurTok != ')')
224 return Error("expected ')'");
225 getNextToken(); // eat ).
226 return V;
227}
228
229/// primary
230/// ::= identifierexpr
231/// ::= numberexpr
232/// ::= parenexpr
Lang Hames09bf4c12015-08-18 18:11:06 +0000233static std::unique_ptr<ExprAST> ParsePrimary() {
Erick Tryzelaar21e83ea2009-09-22 21:15:19 +0000234 switch (CurTok) {
235 default: return Error("unknown token when expecting an expression");
236 case tok_identifier: return ParseIdentifierExpr();
237 case tok_number: return ParseNumberExpr();
238 case '(': return ParseParenExpr();
239 }
240}
241
242/// binoprhs
243/// ::= ('+' primary)*
Lang Hames09bf4c12015-08-18 18:11:06 +0000244static std::unique_ptr<ExprAST> ParseBinOpRHS(int ExprPrec,
245 std::unique_ptr<ExprAST> LHS) {
Erick Tryzelaar21e83ea2009-09-22 21:15:19 +0000246 // If this is a binop, find its precedence.
247 while (1) {
248 int TokPrec = GetTokPrecedence();
249
250 // If this is a binop that binds at least as tightly as the current binop,
251 // consume it, otherwise we are done.
252 if (TokPrec < ExprPrec)
253 return LHS;
254
255 // Okay, we know this is a binop.
256 int BinOp = CurTok;
257 getNextToken(); // eat binop
258
259 // Parse the primary expression after the binary operator.
Lang Hames09bf4c12015-08-18 18:11:06 +0000260 auto RHS = ParsePrimary();
261 if (!RHS) return nullptr;
Erick Tryzelaar21e83ea2009-09-22 21:15:19 +0000262
263 // If BinOp binds less tightly with RHS than the operator after RHS, let
264 // the pending operator take RHS as its LHS.
265 int NextPrec = GetTokPrecedence();
266 if (TokPrec < NextPrec) {
Lang Hames09bf4c12015-08-18 18:11:06 +0000267 RHS = ParseBinOpRHS(TokPrec+1, std::move(RHS));
268 if (!RHS) return nullptr;
Erick Tryzelaar21e83ea2009-09-22 21:15:19 +0000269 }
270
271 // Merge LHS/RHS.
Lang Hames09bf4c12015-08-18 18:11:06 +0000272 LHS = llvm::make_unique<BinaryExprAST>(BinOp, std::move(LHS),
273 std::move(RHS));
Erick Tryzelaar21e83ea2009-09-22 21:15:19 +0000274 }
275}
276
277/// expression
278/// ::= primary binoprhs
279///
Lang Hames09bf4c12015-08-18 18:11:06 +0000280static std::unique_ptr<ExprAST> ParseExpression() {
281 auto LHS = ParsePrimary();
282 if (!LHS) return nullptr;
Erick Tryzelaar21e83ea2009-09-22 21:15:19 +0000283
Lang Hames09bf4c12015-08-18 18:11:06 +0000284 return ParseBinOpRHS(0, std::move(LHS));
Erick Tryzelaar21e83ea2009-09-22 21:15:19 +0000285}
286
287/// prototype
288/// ::= id '(' id* ')'
Lang Hames09bf4c12015-08-18 18:11:06 +0000289static std::unique_ptr<PrototypeAST> ParsePrototype() {
Erick Tryzelaar21e83ea2009-09-22 21:15:19 +0000290 if (CurTok != tok_identifier)
291 return ErrorP("Expected function name in prototype");
292
293 std::string FnName = IdentifierStr;
294 getNextToken();
295
296 if (CurTok != '(')
297 return ErrorP("Expected '(' in prototype");
298
299 std::vector<std::string> ArgNames;
300 while (getNextToken() == tok_identifier)
301 ArgNames.push_back(IdentifierStr);
302 if (CurTok != ')')
303 return ErrorP("Expected ')' in prototype");
304
305 // success.
306 getNextToken(); // eat ')'.
307
Lang Hames09bf4c12015-08-18 18:11:06 +0000308 return llvm::make_unique<PrototypeAST>(std::move(FnName),
309 std::move(ArgNames));
Erick Tryzelaar21e83ea2009-09-22 21:15:19 +0000310}
311
312/// definition ::= 'def' prototype expression
Lang Hames09bf4c12015-08-18 18:11:06 +0000313static std::unique_ptr<FunctionAST> ParseDefinition() {
Erick Tryzelaar21e83ea2009-09-22 21:15:19 +0000314 getNextToken(); // eat def.
Lang Hames09bf4c12015-08-18 18:11:06 +0000315 auto Proto = ParsePrototype();
316 if (!Proto) return nullptr;
Erick Tryzelaar21e83ea2009-09-22 21:15:19 +0000317
Lang Hames09bf4c12015-08-18 18:11:06 +0000318 if (auto E = ParseExpression())
319 return llvm::make_unique<FunctionAST>(std::move(Proto), std::move(E));
320 return nullptr;
Erick Tryzelaar21e83ea2009-09-22 21:15:19 +0000321}
322
323/// toplevelexpr ::= expression
Lang Hames09bf4c12015-08-18 18:11:06 +0000324static std::unique_ptr<FunctionAST> ParseTopLevelExpr() {
325 if (auto E = ParseExpression()) {
Erick Tryzelaar21e83ea2009-09-22 21:15:19 +0000326 // Make an anonymous proto.
Lang Hames09bf4c12015-08-18 18:11:06 +0000327 auto Proto = llvm::make_unique<PrototypeAST>("",
328 std::vector<std::string>());
329 return llvm::make_unique<FunctionAST>(std::move(Proto), std::move(E));
Erick Tryzelaar21e83ea2009-09-22 21:15:19 +0000330 }
Lang Hames09bf4c12015-08-18 18:11:06 +0000331 return nullptr;
Erick Tryzelaar21e83ea2009-09-22 21:15:19 +0000332}
333
334/// external ::= 'extern' prototype
Lang Hames09bf4c12015-08-18 18:11:06 +0000335static std::unique_ptr<PrototypeAST> ParseExtern() {
Erick Tryzelaar21e83ea2009-09-22 21:15:19 +0000336 getNextToken(); // eat extern.
337 return ParsePrototype();
338}
339
340//===----------------------------------------------------------------------===//
341// Top-Level parsing
342//===----------------------------------------------------------------------===//
343
344static void HandleDefinition() {
345 if (ParseDefinition()) {
346 fprintf(stderr, "Parsed a function definition.\n");
347 } else {
348 // Skip token for error recovery.
349 getNextToken();
350 }
351}
352
353static void HandleExtern() {
354 if (ParseExtern()) {
355 fprintf(stderr, "Parsed an extern\n");
356 } else {
357 // Skip token for error recovery.
358 getNextToken();
359 }
360}
361
362static void HandleTopLevelExpression() {
363 // Evaluate a top-level expression into an anonymous function.
364 if (ParseTopLevelExpr()) {
365 fprintf(stderr, "Parsed a top-level expr\n");
366 } else {
367 // Skip token for error recovery.
368 getNextToken();
369 }
370}
371
372/// top ::= definition | external | expression | ';'
373static void MainLoop() {
374 while (1) {
375 fprintf(stderr, "ready> ");
376 switch (CurTok) {
377 case tok_eof: return;
378 case ';': getNextToken(); break; // ignore top-level semicolons.
379 case tok_def: HandleDefinition(); break;
380 case tok_extern: HandleExtern(); break;
381 default: HandleTopLevelExpression(); break;
382 }
383 }
384}
385
386//===----------------------------------------------------------------------===//
387// Main driver code.
388//===----------------------------------------------------------------------===//
389
390int main() {
391 // Install standard binary operators.
392 // 1 is lowest precedence.
393 BinopPrecedence['<'] = 10;
394 BinopPrecedence['+'] = 20;
395 BinopPrecedence['-'] = 20;
396 BinopPrecedence['*'] = 40; // highest.
397
398 // Prime the first token.
399 fprintf(stderr, "ready> ");
400 getNextToken();
401
402 // Run the main "interpreter loop" now.
403 MainLoop();
404
405 return 0;
406}