7 //===----------------------------------------------------------------------===//
9 //===----------------------------------------------------------------------===//
11 // The lexer returns tokens [0-255] if it is an unknown character, otherwise one
12 // of these for known things.
17 tok_def = -2, tok_extern = -3,
20 tok_identifier = -4, tok_number = -5
23 static std::string IdentifierStr; // Filled in if tok_identifier
24 static double NumVal; // Filled in if tok_number
26 /// gettok - Return the next token from standard input.
28 static int LastChar = ' ';
30 // Skip any whitespace.
31 while (isspace(LastChar))
34 if (isalpha(LastChar)) { // identifier: [a-zA-Z][a-zA-Z0-9]*
35 IdentifierStr = LastChar;
36 while (isalnum((LastChar = getchar())))
37 IdentifierStr += LastChar;
39 if (IdentifierStr == "def") return tok_def;
40 if (IdentifierStr == "extern") return tok_extern;
41 return tok_identifier;
44 if (isdigit(LastChar) || LastChar == '.') { // Number: [0-9.]+
49 } while (isdigit(LastChar) || LastChar == '.');
51 NumVal = strtod(NumStr.c_str(), 0);
55 if (LastChar == '#') {
56 // Comment until end of line.
57 do LastChar = getchar();
58 while (LastChar != EOF && LastChar != '\n' && LastChar != '\r');
64 // Check for end of file. Don't eat the EOF.
68 // Otherwise, just return the character as its ascii value.
69 int ThisChar = LastChar;
74 //===----------------------------------------------------------------------===//
75 // Abstract Syntax Tree (aka Parse Tree)
76 //===----------------------------------------------------------------------===//
78 /// ExprAST - Base class for all expression nodes.
84 /// NumberExprAST - Expression class for numeric literals like "1.0".
85 class NumberExprAST : public ExprAST {
87 NumberExprAST(double val) {}
90 /// VariableExprAST - Expression class for referencing a variable, like "a".
91 class VariableExprAST : public ExprAST {
94 VariableExprAST(const std::string &name) : Name(name) {}
97 /// BinaryExprAST - Expression class for a binary operator.
98 class BinaryExprAST : public ExprAST {
100 BinaryExprAST(char op, ExprAST *lhs, ExprAST *rhs) {}
103 /// CallExprAST - Expression class for function calls.
104 class CallExprAST : public ExprAST {
106 std::vector<ExprAST*> Args;
108 CallExprAST(const std::string &callee, std::vector<ExprAST*> &args)
109 : Callee(callee), Args(args) {}
112 /// PrototypeAST - This class represents the "prototype" for a function,
113 /// which captures its name, and its argument names (thus implicitly the number
114 /// of arguments the function takes).
117 std::vector<std::string> Args;
119 PrototypeAST(const std::string &name, const std::vector<std::string> &args)
120 : Name(name), Args(args) {}
124 /// FunctionAST - This class represents a function definition itself.
127 FunctionAST(PrototypeAST *proto, ExprAST *body) {}
130 //===----------------------------------------------------------------------===//
132 //===----------------------------------------------------------------------===//
134 /// CurTok/getNextToken - Provide a simple token buffer. CurTok is the current
135 /// token the parser is looking at. getNextToken reads another token from the
136 /// lexer and updates CurTok with its results.
138 static int getNextToken() {
139 return CurTok = gettok();
142 /// BinopPrecedence - This holds the precedence for each binary operator that is
144 static std::map<char, int> BinopPrecedence;
146 /// GetTokPrecedence - Get the precedence of the pending binary operator token.
147 static int GetTokPrecedence() {
148 if (!isascii(CurTok))
151 // Make sure it's a declared binop.
152 int TokPrec = BinopPrecedence[CurTok];
153 if (TokPrec <= 0) return -1;
157 /// Error* - These are little helper functions for error handling.
158 ExprAST *Error(const char *Str) { fprintf(stderr, "Error: %s\n", Str);return 0;}
159 PrototypeAST *ErrorP(const char *Str) { Error(Str); return 0; }
160 FunctionAST *ErrorF(const char *Str) { Error(Str); return 0; }
162 static ExprAST *ParseExpression();
166 /// ::= identifier '(' expression* ')'
167 static ExprAST *ParseIdentifierExpr() {
168 std::string IdName = IdentifierStr;
170 getNextToken(); // eat identifier.
172 if (CurTok != '(') // Simple variable ref.
173 return new VariableExprAST(IdName);
176 getNextToken(); // eat (
177 std::vector<ExprAST*> Args;
180 ExprAST *Arg = ParseExpression();
184 if (CurTok == ')') break;
187 return Error("Expected ')' or ',' in argument list");
195 return new CallExprAST(IdName, Args);
198 /// numberexpr ::= number
199 static ExprAST *ParseNumberExpr() {
200 ExprAST *Result = new NumberExprAST(NumVal);
201 getNextToken(); // consume the number
205 /// parenexpr ::= '(' expression ')'
206 static ExprAST *ParseParenExpr() {
207 getNextToken(); // eat (.
208 ExprAST *V = ParseExpression();
212 return Error("expected ')'");
213 getNextToken(); // eat ).
218 /// ::= identifierexpr
221 static ExprAST *ParsePrimary() {
223 default: return Error("unknown token when expecting an expression");
224 case tok_identifier: return ParseIdentifierExpr();
225 case tok_number: return ParseNumberExpr();
226 case '(': return ParseParenExpr();
231 /// ::= ('+' primary)*
232 static ExprAST *ParseBinOpRHS(int ExprPrec, ExprAST *LHS) {
233 // If this is a binop, find its precedence.
235 int TokPrec = GetTokPrecedence();
237 // If this is a binop that binds at least as tightly as the current binop,
238 // consume it, otherwise we are done.
239 if (TokPrec < ExprPrec)
242 // Okay, we know this is a binop.
244 getNextToken(); // eat binop
246 // Parse the primary expression after the binary operator.
247 ExprAST *RHS = ParsePrimary();
250 // If BinOp binds less tightly with RHS than the operator after RHS, let
251 // the pending operator take RHS as its LHS.
252 int NextPrec = GetTokPrecedence();
253 if (TokPrec < NextPrec) {
254 RHS = ParseBinOpRHS(TokPrec+1, RHS);
255 if (RHS == 0) return 0;
259 LHS = new BinaryExprAST(BinOp, LHS, RHS);
264 /// ::= primary binoprhs
266 static ExprAST *ParseExpression() {
267 ExprAST *LHS = ParsePrimary();
270 return ParseBinOpRHS(0, LHS);
274 /// ::= id '(' id* ')'
275 static PrototypeAST *ParsePrototype() {
276 if (CurTok != tok_identifier)
277 return ErrorP("Expected function name in prototype");
279 std::string FnName = IdentifierStr;
283 return ErrorP("Expected '(' in prototype");
285 std::vector<std::string> ArgNames;
286 while (getNextToken() == tok_identifier)
287 ArgNames.push_back(IdentifierStr);
289 return ErrorP("Expected ')' in prototype");
292 getNextToken(); // eat ')'.
294 return new PrototypeAST(FnName, ArgNames);
297 /// definition ::= 'def' prototype expression
298 static FunctionAST *ParseDefinition() {
299 getNextToken(); // eat def.
300 PrototypeAST *Proto = ParsePrototype();
301 if (Proto == 0) return 0;
303 if (ExprAST *E = ParseExpression())
304 return new FunctionAST(Proto, E);
308 /// toplevelexpr ::= expression
309 static FunctionAST *ParseTopLevelExpr() {
310 if (ExprAST *E = ParseExpression()) {
311 // Make an anonymous proto.
312 PrototypeAST *Proto = new PrototypeAST("", std::vector<std::string>());
313 return new FunctionAST(Proto, E);
318 /// external ::= 'extern' prototype
319 static PrototypeAST *ParseExtern() {
320 getNextToken(); // eat extern.
321 return ParsePrototype();
324 //===----------------------------------------------------------------------===//
326 //===----------------------------------------------------------------------===//
328 static void HandleDefinition() {
329 if (ParseDefinition()) {
330 fprintf(stderr, "Parsed a function definition.\n");
332 // Skip token for error recovery.
337 static void HandleExtern() {
339 fprintf(stderr, "Parsed an extern\n");
341 // Skip token for error recovery.
346 static void HandleTopLevelExpression() {
347 // Evaluate a top-level expression into an anonymous function.
348 if (ParseTopLevelExpr()) {
349 fprintf(stderr, "Parsed a top-level expr\n");
351 // Skip token for error recovery.
356 /// top ::= definition | external | expression | ';'
357 static void MainLoop() {
359 fprintf(stderr, "ready> ");
361 case tok_eof: return;
362 case ';': getNextToken(); break; // ignore top-level semicolons.
363 case tok_def: HandleDefinition(); break;
364 case tok_extern: HandleExtern(); break;
365 default: HandleTopLevelExpression(); break;
370 //===----------------------------------------------------------------------===//
372 //===----------------------------------------------------------------------===//
375 // Install standard binary operators.
376 // 1 is lowest precedence.
377 BinopPrecedence['<'] = 10;
378 BinopPrecedence['+'] = 20;
379 BinopPrecedence['-'] = 20;
380 BinopPrecedence['*'] = 40; // highest.
382 // Prime the first token.
383 fprintf(stderr, "ready> ");
386 // Run the main "interpreter loop" now.