diff options
Diffstat (limited to 'src/llvm_kaleidoscope_rs/parser.rs.html')
-rw-r--r-- | src/llvm_kaleidoscope_rs/parser.rs.html | 915 |
1 files changed, 454 insertions, 461 deletions
diff --git a/src/llvm_kaleidoscope_rs/parser.rs.html b/src/llvm_kaleidoscope_rs/parser.rs.html index d3c9594..1751341 100644 --- a/src/llvm_kaleidoscope_rs/parser.rs.html +++ b/src/llvm_kaleidoscope_rs/parser.rs.html @@ -1,10 +1,4 @@ -<!DOCTYPE html><html lang="en"><head><meta charset="utf-8"><meta name="viewport" content="width=device-width, initial-scale=1.0"><meta name="generator" content="rustdoc"><meta name="description" content="Source of the Rust file `src/parser.rs`."><meta name="keywords" content="rust, rustlang, rust-lang"><title>parser.rs - source</title><link rel="preload" as="font" type="font/woff2" crossorigin href="../../SourceSerif4-Regular.ttf.woff2"><link rel="preload" as="font" type="font/woff2" crossorigin href="../../FiraSans-Regular.woff2"><link rel="preload" as="font" type="font/woff2" crossorigin href="../../FiraSans-Medium.woff2"><link rel="preload" as="font" type="font/woff2" crossorigin href="../../SourceCodePro-Regular.ttf.woff2"><link rel="preload" as="font" type="font/woff2" crossorigin href="../../SourceSerif4-Bold.ttf.woff2"><link rel="preload" as="font" type="font/woff2" crossorigin href="../../SourceCodePro-Semibold.ttf.woff2"><link rel="stylesheet" type="text/css" href="../../normalize.css"><link rel="stylesheet" type="text/css" href="../../rustdoc.css" id="mainThemeStyle"><link rel="stylesheet" type="text/css" href="../../ayu.css" disabled><link rel="stylesheet" type="text/css" href="../../dark.css" disabled><link rel="stylesheet" type="text/css" href="../../light.css" id="themeStyle"><script id="default-settings" ></script><script src="../../storage.js"></script><script defer src="../../source-script.js"></script><script defer src="../../source-files.js"></script><script defer src="../../main.js"></script><noscript><link rel="stylesheet" href="../../noscript.css"></noscript><link rel="alternate icon" type="image/png" href="../../favicon-16x16.png"><link rel="alternate icon" type="image/png" href="../../favicon-32x32.png"><link rel="icon" type="image/svg+xml" href="../../favicon.svg"></head><body class="rustdoc source"><!--[if lte IE 11]><div class="warning">This old browser is unsupported and will most likely display funky things.</div><![endif]--><nav class="mobile-topbar"><button class="sidebar-menu-toggle">☰</button><a class="sidebar-logo" href="../../llvm_kaleidoscope_rs/index.html"><div class="logo-container"><img class="rust-logo" src="../../rust-logo.svg" alt="logo"></div> - </a><h2 class="location"></h2> - </nav> - <nav class="sidebar"><a class="sidebar-logo" href="../../llvm_kaleidoscope_rs/index.html"><div class="logo-container"><img class="rust-logo" src="../../rust-logo.svg" alt="logo"></div> - </a></nav><main><div class="width-limiter"><div class="sub-container"><a class="sub-logo-container" href="../../llvm_kaleidoscope_rs/index.html"><img class="rust-logo" src="../../rust-logo.svg" alt="logo"></a><nav class="sub"><form class="search-form"><div class="search-container"><span></span><input class="search-input" name="search" autocomplete="off" spellcheck="false" placeholder="Click or press ‘S’ to search, ‘?’ for more options…" type="search"><button type="button" id="help-button" title="help">?</button><div id="settings-menu" tabindex="-1"> - <a href="../../settings.html" title="settings"><img width="22" height="22" alt="Change settings" src="../../wheel.svg"></a></div> - </div></form></nav></div><section id="main-content" class="content"><div class="example-wrap"><pre class="line-numbers"><span id="1">1</span> +<!DOCTYPE html><html lang="en"><head><meta charset="utf-8"><meta name="viewport" content="width=device-width, initial-scale=1.0"><meta name="generator" content="rustdoc"><meta name="description" content="Source of the Rust file `src/parser.rs`."><meta name="keywords" content="rust, rustlang, rust-lang"><title>parser.rs - source</title><link rel="preload" as="font" type="font/woff2" crossorigin href="../../SourceSerif4-Regular.ttf.woff2"><link rel="preload" as="font" type="font/woff2" crossorigin href="../../FiraSans-Regular.woff2"><link rel="preload" as="font" type="font/woff2" crossorigin href="../../FiraSans-Medium.woff2"><link rel="preload" as="font" type="font/woff2" crossorigin href="../../SourceCodePro-Regular.ttf.woff2"><link rel="preload" as="font" type="font/woff2" crossorigin href="../../SourceSerif4-Bold.ttf.woff2"><link rel="preload" as="font" type="font/woff2" crossorigin href="../../SourceCodePro-Semibold.ttf.woff2"><link rel="stylesheet" href="../../normalize.css"><link rel="stylesheet" href="../../rustdoc.css" id="mainThemeStyle"><link rel="stylesheet" href="../../ayu.css" disabled><link rel="stylesheet" href="../../dark.css" disabled><link rel="stylesheet" href="../../light.css" id="themeStyle"><script id="default-settings" ></script><script src="../../storage.js"></script><script defer src="../../source-script.js"></script><script defer src="../../source-files.js"></script><script defer src="../../main.js"></script><noscript><link rel="stylesheet" href="../../noscript.css"></noscript><link rel="alternate icon" type="image/png" href="../../favicon-16x16.png"><link rel="alternate icon" type="image/png" href="../../favicon-32x32.png"><link rel="icon" type="image/svg+xml" href="../../favicon.svg"></head><body class="rustdoc source"><!--[if lte IE 11]><div class="warning">This old browser is unsupported and will most likely display funky things.</div><![endif]--><nav class="mobile-topbar"><button class="sidebar-menu-toggle">☰</button><a class="sidebar-logo" href="../../llvm_kaleidoscope_rs/index.html"><div class="logo-container"><img class="rust-logo" src="../../rust-logo.svg" alt="logo"></div></a><h2 class="location"></h2></nav><nav class="sidebar"><a class="sidebar-logo" href="../../llvm_kaleidoscope_rs/index.html"><div class="logo-container"><img class="rust-logo" src="../../rust-logo.svg" alt="logo"></div></a></nav><main><div class="width-limiter"><div class="sub-container"><a class="sub-logo-container" href="../../llvm_kaleidoscope_rs/index.html"><img class="rust-logo" src="../../rust-logo.svg" alt="logo"></a><nav class="sub"><form class="search-form"><div class="search-container"><span></span><input class="search-input" name="search" autocomplete="off" spellcheck="false" placeholder="Click or press ‘S’ to search, ‘?’ for more options…" type="search"><div id="help-button" title="help" tabindex="-1"><button type="button">?</button></div><div id="settings-menu" tabindex="-1"><a href="../../settings.html" title="settings"><img width="22" height="22" alt="Change settings" src="../../wheel.svg"></a></div></div></form></nav></div><section id="main-content" class="content"><div class="example-wrap"><pre class="line-numbers"><span id="1">1</span> <span id="2">2</span> <span id="3">3</span> <span id="4">4</span> @@ -658,660 +652,659 @@ <span id="652">652</span> <span id="653">653</span> <span id="654">654</span> -</pre><pre class="rust"><code><span class="kw">use</span> <span class="ident"><span class="kw">crate</span>::lexer</span>::{<span class="ident">Lexer</span>, <span class="ident">Token</span>}; +</pre><pre class="rust"><code><span class="kw">use </span><span class="kw">crate</span>::lexer::{Lexer, Token}; -<span class="attribute">#[<span class="ident">derive</span>(<span class="ident">Debug</span>, <span class="ident">PartialEq</span>)]</span> -<span class="kw">pub</span> <span class="kw">enum</span> <span class="ident">ExprAST</span> { - <span class="doccomment">/// Number - Expression class for numeric literals like "1.0".</span> - <span class="ident">Number</span>(<span class="ident">f64</span>), +<span class="attribute">#[derive(Debug, PartialEq)] +</span><span class="kw">pub enum </span>ExprAST { + <span class="doccomment">/// Number - Expression class for numeric literals like "1.0". + </span>Number(f64), - <span class="doccomment">/// Variable - Expression class for referencing a variable, like "a".</span> - <span class="ident">Variable</span>(<span class="ident">String</span>), + <span class="doccomment">/// Variable - Expression class for referencing a variable, like "a". + </span>Variable(String), - <span class="doccomment">/// Binary - Expression class for a binary operator.</span> - <span class="ident">Binary</span>(<span class="ident">char</span>, <span class="ident">Box</span><span class="op"><</span><span class="ident">ExprAST</span><span class="op">></span>, <span class="ident">Box</span><span class="op"><</span><span class="ident">ExprAST</span><span class="op">></span>), + <span class="doccomment">/// Binary - Expression class for a binary operator. + </span>Binary(char, Box<ExprAST>, Box<ExprAST>), - <span class="doccomment">/// Call - Expression class for function calls.</span> - <span class="ident">Call</span>(<span class="ident">String</span>, <span class="ident">Vec</span><span class="op"><</span><span class="ident">ExprAST</span><span class="op">></span>), + <span class="doccomment">/// Call - Expression class for function calls. + </span>Call(String, Vec<ExprAST>), - <span class="doccomment">/// If - Expression class for if/then/else.</span> - <span class="ident">If</span> { - <span class="ident">cond</span>: <span class="ident">Box</span><span class="op"><</span><span class="ident">ExprAST</span><span class="op">></span>, - <span class="ident">then</span>: <span class="ident">Box</span><span class="op"><</span><span class="ident">ExprAST</span><span class="op">></span>, - <span class="ident">else_</span>: <span class="ident">Box</span><span class="op"><</span><span class="ident">ExprAST</span><span class="op">></span>, + <span class="doccomment">/// If - Expression class for if/then/else. + </span>If { + cond: Box<ExprAST>, + then: Box<ExprAST>, + else_: Box<ExprAST>, }, - <span class="doccomment">/// ForExprAST - Expression class for for/in.</span> - <span class="ident">For</span> { - <span class="ident">var</span>: <span class="ident">String</span>, - <span class="ident">start</span>: <span class="ident">Box</span><span class="op"><</span><span class="ident">ExprAST</span><span class="op">></span>, - <span class="ident">end</span>: <span class="ident">Box</span><span class="op"><</span><span class="ident">ExprAST</span><span class="op">></span>, - <span class="ident">step</span>: <span class="prelude-ty">Option</span><span class="op"><</span><span class="ident">Box</span><span class="op"><</span><span class="ident">ExprAST</span><span class="op">></span><span class="op">></span>, - <span class="ident">body</span>: <span class="ident">Box</span><span class="op"><</span><span class="ident">ExprAST</span><span class="op">></span>, + <span class="doccomment">/// ForExprAST - Expression class for for/in. + </span>For { + var: String, + start: Box<ExprAST>, + end: Box<ExprAST>, + step: <span class="prelude-ty">Option</span><Box<ExprAST>>, + body: Box<ExprAST>, }, } -<span class="doccomment">/// PrototypeAST - This class represents the "prototype" for a function,</span> -<span class="doccomment">/// which captures its name, and its argument names (thus implicitly the number</span> -<span class="doccomment">/// of arguments the function takes).</span> -<span class="attribute">#[<span class="ident">derive</span>(<span class="ident">Debug</span>, <span class="ident">PartialEq</span>, <span class="ident">Clone</span>)]</span> -<span class="kw">pub</span> <span class="kw">struct</span> <span class="ident">PrototypeAST</span>(<span class="kw">pub</span> <span class="ident">String</span>, <span class="kw">pub</span> <span class="ident">Vec</span><span class="op"><</span><span class="ident">String</span><span class="op">></span>); +<span class="doccomment">/// PrototypeAST - This class represents the "prototype" for a function, +/// which captures its name, and its argument names (thus implicitly the number +/// of arguments the function takes). +</span><span class="attribute">#[derive(Debug, PartialEq, Clone)] +</span><span class="kw">pub struct </span>PrototypeAST(<span class="kw">pub </span>String, <span class="kw">pub </span>Vec<String>); -<span class="doccomment">/// FunctionAST - This class represents a function definition itself.</span> -<span class="attribute">#[<span class="ident">derive</span>(<span class="ident">Debug</span>, <span class="ident">PartialEq</span>)]</span> -<span class="kw">pub</span> <span class="kw">struct</span> <span class="ident">FunctionAST</span>(<span class="kw">pub</span> <span class="ident">PrototypeAST</span>, <span class="kw">pub</span> <span class="ident">ExprAST</span>); +<span class="doccomment">/// FunctionAST - This class represents a function definition itself. +</span><span class="attribute">#[derive(Debug, PartialEq)] +</span><span class="kw">pub struct </span>FunctionAST(<span class="kw">pub </span>PrototypeAST, <span class="kw">pub </span>ExprAST); -<span class="doccomment">/// Parse result with String as Error type (to be compliant with tutorial).</span> -<span class="kw">type</span> <span class="ident">ParseResult</span><span class="op"><</span><span class="ident">T</span><span class="op">></span> <span class="op">=</span> <span class="prelude-ty">Result</span><span class="op"><</span><span class="ident">T</span>, <span class="ident">String</span><span class="op">></span>; +<span class="doccomment">/// Parse result with String as Error type (to be compliant with tutorial). +</span><span class="kw">type </span>ParseResult<T> = <span class="prelude-ty">Result</span><T, String>; -<span class="doccomment">/// Parser for the `kaleidoscope` language.</span> -<span class="kw">pub</span> <span class="kw">struct</span> <span class="ident">Parser</span><span class="op"><</span><span class="ident">I</span><span class="op">></span> -<span class="kw">where</span> - <span class="ident">I</span>: <span class="ident">Iterator</span><span class="op"><</span><span class="ident">Item</span> <span class="op">=</span> <span class="ident">char</span><span class="op">></span>, +<span class="doccomment">/// Parser for the `kaleidoscope` language. +</span><span class="kw">pub struct </span>Parser<I> +<span class="kw">where + </span>I: Iterator<Item = char>, { - <span class="ident">lexer</span>: <span class="ident">Lexer</span><span class="op"><</span><span class="ident">I</span><span class="op">></span>, - <span class="ident">cur_tok</span>: <span class="prelude-ty">Option</span><span class="op"><</span><span class="ident">Token</span><span class="op">></span>, + lexer: Lexer<I>, + cur_tok: <span class="prelude-ty">Option</span><Token>, } -<span class="kw">impl</span><span class="op"><</span><span class="ident">I</span><span class="op">></span> <span class="ident">Parser</span><span class="op"><</span><span class="ident">I</span><span class="op">></span> -<span class="kw">where</span> - <span class="ident">I</span>: <span class="ident">Iterator</span><span class="op"><</span><span class="ident">Item</span> <span class="op">=</span> <span class="ident">char</span><span class="op">></span>, +<span class="kw">impl</span><I> Parser<I> +<span class="kw">where + </span>I: Iterator<Item = char>, { - <span class="kw">pub</span> <span class="kw">fn</span> <span class="ident">new</span>(<span class="ident">lexer</span>: <span class="ident">Lexer</span><span class="op"><</span><span class="ident">I</span><span class="op">></span>) -> <span class="self">Self</span> { - <span class="ident">Parser</span> { - <span class="ident">lexer</span>, - <span class="ident">cur_tok</span>: <span class="prelude-val">None</span>, + <span class="kw">pub fn </span>new(lexer: Lexer<I>) -> <span class="self">Self </span>{ + Parser { + lexer, + cur_tok: <span class="prelude-val">None</span>, } } - <span class="comment">// -----------------------</span> - <span class="comment">// Simple Token Buffer</span> - <span class="comment">// -----------------------</span> + <span class="comment">// ----------------------- + // Simple Token Buffer + // ----------------------- - <span class="doccomment">/// Implement the global variable `int CurTok;` from the tutorial.</span> - <span class="doccomment">///</span> - <span class="doccomment">/// # Panics</span> - <span class="doccomment">/// Panics if the parser doesn't have a current token.</span> - <span class="kw">pub</span> <span class="kw">fn</span> <span class="ident">cur_tok</span>(<span class="kw-2">&</span><span class="self">self</span>) -> <span class="kw-2">&</span><span class="ident">Token</span> { - <span class="self">self</span>.<span class="ident">cur_tok</span>.<span class="ident">as_ref</span>().<span class="ident">expect</span>(<span class="string">"Parser: Expected cur_token!"</span>) + </span><span class="doccomment">/// Implement the global variable `int CurTok;` from the tutorial. + /// + /// # Panics + /// Panics if the parser doesn't have a current token. + </span><span class="kw">pub fn </span>cur_tok(<span class="kw-2">&</span><span class="self">self</span>) -> <span class="kw-2">&</span>Token { + <span class="self">self</span>.cur_tok.as_ref().expect(<span class="string">"Parser: Expected cur_token!"</span>) } - <span class="doccomment">/// Advance the `cur_tok` by getting the next token from the lexer.</span> - <span class="doccomment">///</span> - <span class="doccomment">/// Implement the fucntion `int getNextToken();` from the tutorial.</span> - <span class="kw">pub</span> <span class="kw">fn</span> <span class="ident">get_next_token</span>(<span class="kw-2">&mut</span> <span class="self">self</span>) { - <span class="self">self</span>.<span class="ident">cur_tok</span> <span class="op">=</span> <span class="prelude-val">Some</span>(<span class="self">self</span>.<span class="ident">lexer</span>.<span class="ident">gettok</span>()); + <span class="doccomment">/// Advance the `cur_tok` by getting the next token from the lexer. + /// + /// Implement the fucntion `int getNextToken();` from the tutorial. + </span><span class="kw">pub fn </span>get_next_token(<span class="kw-2">&mut </span><span class="self">self</span>) { + <span class="self">self</span>.cur_tok = <span class="prelude-val">Some</span>(<span class="self">self</span>.lexer.gettok()); } - <span class="comment">// ----------------------------</span> - <span class="comment">// Basic Expression Parsing</span> - <span class="comment">// ----------------------------</span> - - <span class="doccomment">/// numberexpr ::= number</span> - <span class="doccomment">///</span> - <span class="doccomment">/// Implement `std::unique_ptr<ExprAST> ParseNumberExpr();` from the tutorial.</span> - <span class="kw">fn</span> <span class="ident">parse_num_expr</span>(<span class="kw-2">&mut</span> <span class="self">self</span>) -> <span class="ident">ParseResult</span><span class="op"><</span><span class="ident">ExprAST</span><span class="op">></span> { - <span class="kw">match</span> <span class="kw-2">*</span><span class="self">self</span>.<span class="ident">cur_tok</span>() { - <span class="ident">Token::Number</span>(<span class="ident">num</span>) => { - <span class="comment">// Consume the number token.</span> - <span class="self">self</span>.<span class="ident">get_next_token</span>(); - <span class="prelude-val">Ok</span>(<span class="ident">ExprAST::Number</span>(<span class="ident">num</span>)) + <span class="comment">// ---------------------------- + // Basic Expression Parsing + // ---------------------------- + + </span><span class="doccomment">/// numberexpr ::= number + /// + /// Implement `std::unique_ptr<ExprAST> ParseNumberExpr();` from the tutorial. + </span><span class="kw">fn </span>parse_num_expr(<span class="kw-2">&mut </span><span class="self">self</span>) -> ParseResult<ExprAST> { + <span class="kw">match </span><span class="kw-2">*</span><span class="self">self</span>.cur_tok() { + Token::Number(num) => { + <span class="comment">// Consume the number token. + </span><span class="self">self</span>.get_next_token(); + <span class="prelude-val">Ok</span>(ExprAST::Number(num)) } - <span class="kw">_</span> => <span class="macro">unreachable!</span>(), + <span class="kw">_ </span>=> <span class="macro">unreachable!</span>(), } } - <span class="doccomment">/// parenexpr ::= '(' expression ')'</span> - <span class="doccomment">///</span> - <span class="doccomment">/// Implement `std::unique_ptr<ExprAST> ParseParenExpr();` from the tutorial.</span> - <span class="kw">fn</span> <span class="ident">parse_paren_expr</span>(<span class="kw-2">&mut</span> <span class="self">self</span>) -> <span class="ident">ParseResult</span><span class="op"><</span><span class="ident">ExprAST</span><span class="op">></span> { - <span class="comment">// Eat '(' token.</span> - <span class="macro">assert_eq!</span>(<span class="kw-2">*</span><span class="self">self</span>.<span class="ident">cur_tok</span>(), <span class="ident">Token::Char</span>(<span class="string">'('</span>)); - <span class="self">self</span>.<span class="ident">get_next_token</span>(); - - <span class="kw">let</span> <span class="ident">v</span> <span class="op">=</span> <span class="self">self</span>.<span class="ident">parse_expression</span>()<span class="question-mark">?</span>; - - <span class="kw">if</span> <span class="kw-2">*</span><span class="self">self</span>.<span class="ident">cur_tok</span>() <span class="op">==</span> <span class="ident">Token::Char</span>(<span class="string">')'</span>) { - <span class="comment">// Eat ')' token.</span> - <span class="self">self</span>.<span class="ident">get_next_token</span>(); - <span class="prelude-val">Ok</span>(<span class="ident">v</span>) - } <span class="kw">else</span> { - <span class="prelude-val">Err</span>(<span class="string">"expected ')'"</span>.<span class="ident">into</span>()) + <span class="doccomment">/// parenexpr ::= '(' expression ')' + /// + /// Implement `std::unique_ptr<ExprAST> ParseParenExpr();` from the tutorial. + </span><span class="kw">fn </span>parse_paren_expr(<span class="kw-2">&mut </span><span class="self">self</span>) -> ParseResult<ExprAST> { + <span class="comment">// Eat '(' token. + </span><span class="macro">assert_eq!</span>(<span class="kw-2">*</span><span class="self">self</span>.cur_tok(), Token::Char(<span class="string">'('</span>)); + <span class="self">self</span>.get_next_token(); + + <span class="kw">let </span>v = <span class="self">self</span>.parse_expression()<span class="question-mark">?</span>; + + <span class="kw">if </span><span class="kw-2">*</span><span class="self">self</span>.cur_tok() == Token::Char(<span class="string">')'</span>) { + <span class="comment">// Eat ')' token. + </span><span class="self">self</span>.get_next_token(); + <span class="prelude-val">Ok</span>(v) + } <span class="kw">else </span>{ + <span class="prelude-val">Err</span>(<span class="string">"expected ')'"</span>.into()) } } - <span class="doccomment">/// identifierexpr</span> - <span class="doccomment">/// ::= identifier</span> - <span class="doccomment">/// ::= identifier '(' expression* ')'</span> - <span class="doccomment">///</span> - <span class="doccomment">/// Implement `std::unique_ptr<ExprAST> ParseIdentifierExpr();` from the tutorial.</span> - <span class="kw">fn</span> <span class="ident">parse_identifier_expr</span>(<span class="kw-2">&mut</span> <span class="self">self</span>) -> <span class="ident">ParseResult</span><span class="op"><</span><span class="ident">ExprAST</span><span class="op">></span> { - <span class="kw">let</span> <span class="ident">id_name</span> <span class="op">=</span> <span class="kw">match</span> <span class="self">self</span>.<span class="ident">cur_tok</span>.<span class="ident">take</span>() { - <span class="prelude-val">Some</span>(<span class="ident">Token::Identifier</span>(<span class="ident">id</span>)) => { - <span class="comment">// Consume identifier.</span> - <span class="self">self</span>.<span class="ident">get_next_token</span>(); - <span class="ident">id</span> + <span class="doccomment">/// identifierexpr + /// ::= identifier + /// ::= identifier '(' expression* ')' + /// + /// Implement `std::unique_ptr<ExprAST> ParseIdentifierExpr();` from the tutorial. + </span><span class="kw">fn </span>parse_identifier_expr(<span class="kw-2">&mut </span><span class="self">self</span>) -> ParseResult<ExprAST> { + <span class="kw">let </span>id_name = <span class="kw">match </span><span class="self">self</span>.cur_tok.take() { + <span class="prelude-val">Some</span>(Token::Identifier(id)) => { + <span class="comment">// Consume identifier. + </span><span class="self">self</span>.get_next_token(); + id } - <span class="kw">_</span> => <span class="macro">unreachable!</span>(), + <span class="kw">_ </span>=> <span class="macro">unreachable!</span>(), }; - <span class="kw">if</span> <span class="kw-2">*</span><span class="self">self</span>.<span class="ident">cur_tok</span>() <span class="op">!</span><span class="op">=</span> <span class="ident">Token::Char</span>(<span class="string">'('</span>) { - <span class="comment">// Simple variable reference.</span> - <span class="prelude-val">Ok</span>(<span class="ident">ExprAST::Variable</span>(<span class="ident">id_name</span>)) - } <span class="kw">else</span> { - <span class="comment">// Call.</span> + <span class="kw">if </span><span class="kw-2">*</span><span class="self">self</span>.cur_tok() != Token::Char(<span class="string">'('</span>) { + <span class="comment">// Simple variable reference. + </span><span class="prelude-val">Ok</span>(ExprAST::Variable(id_name)) + } <span class="kw">else </span>{ + <span class="comment">// Call. - <span class="comment">// Eat '(' token.</span> - <span class="self">self</span>.<span class="ident">get_next_token</span>(); + // Eat '(' token. + </span><span class="self">self</span>.get_next_token(); - <span class="kw">let</span> <span class="kw-2">mut</span> <span class="ident">args</span>: <span class="ident">Vec</span><span class="op"><</span><span class="ident">ExprAST</span><span class="op">></span> <span class="op">=</span> <span class="ident">Vec::new</span>(); + <span class="kw">let </span><span class="kw-2">mut </span>args: Vec<ExprAST> = Vec::new(); - <span class="comment">// If there are arguments collect them.</span> - <span class="kw">if</span> <span class="kw-2">*</span><span class="self">self</span>.<span class="ident">cur_tok</span>() <span class="op">!</span><span class="op">=</span> <span class="ident">Token::Char</span>(<span class="string">')'</span>) { - <span class="kw">loop</span> { - <span class="kw">let</span> <span class="ident">arg</span> <span class="op">=</span> <span class="self">self</span>.<span class="ident">parse_expression</span>()<span class="question-mark">?</span>; - <span class="ident">args</span>.<span class="ident">push</span>(<span class="ident">arg</span>); + <span class="comment">// If there are arguments collect them. + </span><span class="kw">if </span><span class="kw-2">*</span><span class="self">self</span>.cur_tok() != Token::Char(<span class="string">')'</span>) { + <span class="kw">loop </span>{ + <span class="kw">let </span>arg = <span class="self">self</span>.parse_expression()<span class="question-mark">?</span>; + args.push(arg); - <span class="kw">if</span> <span class="kw-2">*</span><span class="self">self</span>.<span class="ident">cur_tok</span>() <span class="op">==</span> <span class="ident">Token::Char</span>(<span class="string">')'</span>) { + <span class="kw">if </span><span class="kw-2">*</span><span class="self">self</span>.cur_tok() == Token::Char(<span class="string">')'</span>) { <span class="kw">break</span>; } - <span class="kw">if</span> <span class="kw-2">*</span><span class="self">self</span>.<span class="ident">cur_tok</span>() <span class="op">!</span><span class="op">=</span> <span class="ident">Token::Char</span>(<span class="string">','</span>) { - <span class="kw">return</span> <span class="prelude-val">Err</span>(<span class="string">"Expected ')' or ',' in argument list"</span>.<span class="ident">into</span>()); + <span class="kw">if </span><span class="kw-2">*</span><span class="self">self</span>.cur_tok() != Token::Char(<span class="string">','</span>) { + <span class="kw">return </span><span class="prelude-val">Err</span>(<span class="string">"Expected ')' or ',' in argument list"</span>.into()); } - <span class="self">self</span>.<span class="ident">get_next_token</span>(); + <span class="self">self</span>.get_next_token(); } } - <span class="macro">assert_eq!</span>(<span class="kw-2">*</span><span class="self">self</span>.<span class="ident">cur_tok</span>(), <span class="ident">Token::Char</span>(<span class="string">')'</span>)); - <span class="comment">// Eat ')' token.</span> - <span class="self">self</span>.<span class="ident">get_next_token</span>(); + <span class="macro">assert_eq!</span>(<span class="kw-2">*</span><span class="self">self</span>.cur_tok(), Token::Char(<span class="string">')'</span>)); + <span class="comment">// Eat ')' token. + </span><span class="self">self</span>.get_next_token(); - <span class="prelude-val">Ok</span>(<span class="ident">ExprAST::Call</span>(<span class="ident">id_name</span>, <span class="ident">args</span>)) + <span class="prelude-val">Ok</span>(ExprAST::Call(id_name, args)) } } - <span class="doccomment">/// ifexpr ::= 'if' expression 'then' expression 'else' expression</span> - <span class="doccomment">///</span> - <span class="doccomment">/// Implement `std::unique_ptr<ExprAST> ParseIfExpr();` from the tutorial.</span> - <span class="kw">fn</span> <span class="ident">parse_if_expr</span>(<span class="kw-2">&mut</span> <span class="self">self</span>) -> <span class="ident">ParseResult</span><span class="op"><</span><span class="ident">ExprAST</span><span class="op">></span> { - <span class="comment">// Consume 'if' token.</span> - <span class="macro">assert_eq!</span>(<span class="kw-2">*</span><span class="self">self</span>.<span class="ident">cur_tok</span>(), <span class="ident">Token::If</span>); - <span class="self">self</span>.<span class="ident">get_next_token</span>(); + <span class="doccomment">/// ifexpr ::= 'if' expression 'then' expression 'else' expression + /// + /// Implement `std::unique_ptr<ExprAST> ParseIfExpr();` from the tutorial. + </span><span class="kw">fn </span>parse_if_expr(<span class="kw-2">&mut </span><span class="self">self</span>) -> ParseResult<ExprAST> { + <span class="comment">// Consume 'if' token. + </span><span class="macro">assert_eq!</span>(<span class="kw-2">*</span><span class="self">self</span>.cur_tok(), Token::If); + <span class="self">self</span>.get_next_token(); - <span class="kw">let</span> <span class="ident">cond</span> <span class="op">=</span> <span class="self">self</span>.<span class="ident">parse_expression</span>()<span class="question-mark">?</span>; + <span class="kw">let </span>cond = <span class="self">self</span>.parse_expression()<span class="question-mark">?</span>; - <span class="kw">if</span> <span class="kw-2">*</span><span class="macro">dbg!</span>(<span class="self">self</span>.<span class="ident">cur_tok</span>()) <span class="op">!</span><span class="op">=</span> <span class="ident">Token::Then</span> { - <span class="kw">return</span> <span class="prelude-val">Err</span>(<span class="string">"Expected 'then'"</span>.<span class="ident">into</span>()); + <span class="kw">if </span><span class="kw-2">*</span><span class="macro">dbg!</span>(<span class="self">self</span>.cur_tok()) != Token::Then { + <span class="kw">return </span><span class="prelude-val">Err</span>(<span class="string">"Expected 'then'"</span>.into()); } - <span class="comment">// Consume 'then' token.</span> - <span class="self">self</span>.<span class="ident">get_next_token</span>(); + <span class="comment">// Consume 'then' token. + </span><span class="self">self</span>.get_next_token(); - <span class="kw">let</span> <span class="ident">then</span> <span class="op">=</span> <span class="self">self</span>.<span class="ident">parse_expression</span>()<span class="question-mark">?</span>; + <span class="kw">let </span>then = <span class="self">self</span>.parse_expression()<span class="question-mark">?</span>; - <span class="kw">if</span> <span class="kw-2">*</span><span class="self">self</span>.<span class="ident">cur_tok</span>() <span class="op">!</span><span class="op">=</span> <span class="ident">Token::Else</span> { - <span class="kw">return</span> <span class="prelude-val">Err</span>(<span class="string">"Expected 'else'"</span>.<span class="ident">into</span>()); + <span class="kw">if </span><span class="kw-2">*</span><span class="self">self</span>.cur_tok() != Token::Else { + <span class="kw">return </span><span class="prelude-val">Err</span>(<span class="string">"Expected 'else'"</span>.into()); } - <span class="comment">// Consume 'else' token.</span> - <span class="self">self</span>.<span class="ident">get_next_token</span>(); + <span class="comment">// Consume 'else' token. + </span><span class="self">self</span>.get_next_token(); - <span class="kw">let</span> <span class="ident">else_</span> <span class="op">=</span> <span class="self">self</span>.<span class="ident">parse_expression</span>()<span class="question-mark">?</span>; + <span class="kw">let </span>else_ = <span class="self">self</span>.parse_expression()<span class="question-mark">?</span>; - <span class="prelude-val">Ok</span>(<span class="ident">ExprAST::If</span> { - <span class="ident">cond</span>: <span class="ident">Box::new</span>(<span class="ident">cond</span>), - <span class="ident">then</span>: <span class="ident">Box::new</span>(<span class="ident">then</span>), - <span class="ident">else_</span>: <span class="ident">Box::new</span>(<span class="ident">else_</span>), + <span class="prelude-val">Ok</span>(ExprAST::If { + cond: Box::new(cond), + then: Box::new(then), + else_: Box::new(else_), }) } - <span class="doccomment">/// forexpr ::= 'for' identifier '=' expr ',' expr (',' expr)? 'in' expression</span> - <span class="doccomment">///</span> - <span class="doccomment">/// Implement `std::unique_ptr<ExprAST> ParseForExpr();` from the tutorial.</span> - <span class="kw">fn</span> <span class="ident">parse_for_expr</span>(<span class="kw-2">&mut</span> <span class="self">self</span>) -> <span class="ident">ParseResult</span><span class="op"><</span><span class="ident">ExprAST</span><span class="op">></span> { - <span class="comment">// Consume the 'for' token.</span> - <span class="macro">assert_eq!</span>(<span class="kw-2">*</span><span class="self">self</span>.<span class="ident">cur_tok</span>(), <span class="ident">Token::For</span>); - <span class="self">self</span>.<span class="ident">get_next_token</span>(); - - <span class="kw">let</span> <span class="ident">var</span> <span class="op">=</span> <span class="kw">match</span> <span class="self">self</span> - .<span class="ident">parse_identifier_expr</span>() - .<span class="ident">map_err</span>(<span class="op">|</span><span class="kw">_</span><span class="op">|</span> <span class="ident">String::from</span>(<span class="string">"expected identifier after 'for'"</span>))<span class="question-mark">?</span> - { - <span class="ident">ExprAST::Variable</span>(<span class="ident">var</span>) => <span class="ident">var</span>, - <span class="kw">_</span> => <span class="macro">unreachable!</span>(), + <span class="doccomment">/// forexpr ::= 'for' identifier '=' expr ',' expr (',' expr)? 'in' expression + /// + /// Implement `std::unique_ptr<ExprAST> ParseForExpr();` from the tutorial. + </span><span class="kw">fn </span>parse_for_expr(<span class="kw-2">&mut </span><span class="self">self</span>) -> ParseResult<ExprAST> { + <span class="comment">// Consume the 'for' token. + </span><span class="macro">assert_eq!</span>(<span class="kw-2">*</span><span class="self">self</span>.cur_tok(), Token::For); + <span class="self">self</span>.get_next_token(); + + <span class="kw">let </span>var = <span class="kw">match </span><span class="self">self + </span>.parse_identifier_expr() + .map_err(|<span class="kw">_</span>| String::from(<span class="string">"expected identifier after 'for'"</span>))<span class="question-mark">? + </span>{ + ExprAST::Variable(var) => var, + <span class="kw">_ </span>=> <span class="macro">unreachable!</span>(), }; - <span class="comment">// Consume the '=' token.</span> - <span class="kw">if</span> <span class="kw-2">*</span><span class="self">self</span>.<span class="ident">cur_tok</span>() <span class="op">!</span><span class="op">=</span> <span class="ident">Token::Char</span>(<span class="string">'='</span>) { - <span class="kw">return</span> <span class="prelude-val">Err</span>(<span class="string">"expected '=' after for"</span>.<span class="ident">into</span>()); + <span class="comment">// Consume the '=' token. + </span><span class="kw">if </span><span class="kw-2">*</span><span class="self">self</span>.cur_tok() != Token::Char(<span class="string">'='</span>) { + <span class="kw">return </span><span class="prelude-val">Err</span>(<span class="string">"expected '=' after for"</span>.into()); } - <span class="self">self</span>.<span class="ident">get_next_token</span>(); + <span class="self">self</span>.get_next_token(); - <span class="kw">let</span> <span class="ident">start</span> <span class="op">=</span> <span class="self">self</span>.<span class="ident">parse_expression</span>()<span class="question-mark">?</span>; + <span class="kw">let </span>start = <span class="self">self</span>.parse_expression()<span class="question-mark">?</span>; - <span class="comment">// Consume the ',' token.</span> - <span class="kw">if</span> <span class="kw-2">*</span><span class="self">self</span>.<span class="ident">cur_tok</span>() <span class="op">!</span><span class="op">=</span> <span class="ident">Token::Char</span>(<span class="string">','</span>) { - <span class="kw">return</span> <span class="prelude-val">Err</span>(<span class="string">"expected ',' after for start value"</span>.<span class="ident">into</span>()); + <span class="comment">// Consume the ',' token. + </span><span class="kw">if </span><span class="kw-2">*</span><span class="self">self</span>.cur_tok() != Token::Char(<span class="string">','</span>) { + <span class="kw">return </span><span class="prelude-val">Err</span>(<span class="string">"expected ',' after for start value"</span>.into()); } - <span class="self">self</span>.<span class="ident">get_next_token</span>(); + <span class="self">self</span>.get_next_token(); - <span class="kw">let</span> <span class="ident">end</span> <span class="op">=</span> <span class="self">self</span>.<span class="ident">parse_expression</span>()<span class="question-mark">?</span>; + <span class="kw">let </span>end = <span class="self">self</span>.parse_expression()<span class="question-mark">?</span>; - <span class="kw">let</span> <span class="ident">step</span> <span class="op">=</span> <span class="kw">if</span> <span class="kw-2">*</span><span class="self">self</span>.<span class="ident">cur_tok</span>() <span class="op">==</span> <span class="ident">Token::Char</span>(<span class="string">','</span>) { - <span class="comment">// Consume the ',' token.</span> - <span class="self">self</span>.<span class="ident">get_next_token</span>(); + <span class="kw">let </span>step = <span class="kw">if </span><span class="kw-2">*</span><span class="self">self</span>.cur_tok() == Token::Char(<span class="string">','</span>) { + <span class="comment">// Consume the ',' token. + </span><span class="self">self</span>.get_next_token(); - <span class="prelude-val">Some</span>(<span class="self">self</span>.<span class="ident">parse_expression</span>()<span class="question-mark">?</span>) - } <span class="kw">else</span> { - <span class="prelude-val">None</span> - }; + <span class="prelude-val">Some</span>(<span class="self">self</span>.parse_expression()<span class="question-mark">?</span>) + } <span class="kw">else </span>{ + <span class="prelude-val">None + </span>}; - <span class="comment">// Consume the 'in' token.</span> - <span class="kw">if</span> <span class="kw-2">*</span><span class="self">self</span>.<span class="ident">cur_tok</span>() <span class="op">!</span><span class="op">=</span> <span class="ident">Token::In</span> { - <span class="kw">return</span> <span class="prelude-val">Err</span>(<span class="string">"expected 'in' after for"</span>.<span class="ident">into</span>()); + <span class="comment">// Consume the 'in' token. + </span><span class="kw">if </span><span class="kw-2">*</span><span class="self">self</span>.cur_tok() != Token::In { + <span class="kw">return </span><span class="prelude-val">Err</span>(<span class="string">"expected 'in' after for"</span>.into()); } - <span class="self">self</span>.<span class="ident">get_next_token</span>(); + <span class="self">self</span>.get_next_token(); - <span class="kw">let</span> <span class="ident">body</span> <span class="op">=</span> <span class="self">self</span>.<span class="ident">parse_expression</span>()<span class="question-mark">?</span>; + <span class="kw">let </span>body = <span class="self">self</span>.parse_expression()<span class="question-mark">?</span>; - <span class="prelude-val">Ok</span>(<span class="ident">ExprAST::For</span> { - <span class="ident">var</span>, - <span class="ident">start</span>: <span class="ident">Box::new</span>(<span class="ident">start</span>), - <span class="ident">end</span>: <span class="ident">Box::new</span>(<span class="ident">end</span>), - <span class="ident">step</span>: <span class="ident">step</span>.<span class="ident">map</span>(<span class="op">|</span><span class="ident">s</span><span class="op">|</span> <span class="ident">Box::new</span>(<span class="ident">s</span>)), - <span class="ident">body</span>: <span class="ident">Box::new</span>(<span class="ident">body</span>), + <span class="prelude-val">Ok</span>(ExprAST::For { + var, + start: Box::new(start), + end: Box::new(end), + step: step.map(|s| Box::new(s)), + body: Box::new(body), }) } - <span class="doccomment">/// primary</span> - <span class="doccomment">/// ::= identifierexpr</span> - <span class="doccomment">/// ::= numberexpr</span> - <span class="doccomment">/// ::= parenexpr</span> - <span class="doccomment">///</span> - <span class="doccomment">/// Implement `std::unique_ptr<ExprAST> ParsePrimary();` from the tutorial.</span> - <span class="kw">fn</span> <span class="ident">parse_primary</span>(<span class="kw-2">&mut</span> <span class="self">self</span>) -> <span class="ident">ParseResult</span><span class="op"><</span><span class="ident">ExprAST</span><span class="op">></span> { - <span class="kw">match</span> <span class="kw-2">*</span><span class="self">self</span>.<span class="ident">cur_tok</span>() { - <span class="ident">Token::Identifier</span>(<span class="kw">_</span>) => <span class="self">self</span>.<span class="ident">parse_identifier_expr</span>(), - <span class="ident">Token::Number</span>(<span class="kw">_</span>) => <span class="self">self</span>.<span class="ident">parse_num_expr</span>(), - <span class="ident">Token::Char</span>(<span class="string">'('</span>) => <span class="self">self</span>.<span class="ident">parse_paren_expr</span>(), - <span class="ident">Token::If</span> => <span class="self">self</span>.<span class="ident">parse_if_expr</span>(), - <span class="ident">Token::For</span> => <span class="self">self</span>.<span class="ident">parse_for_expr</span>(), - <span class="kw">_</span> => <span class="prelude-val">Err</span>(<span class="string">"unknown token when expecting an expression"</span>.<span class="ident">into</span>()), + <span class="doccomment">/// primary + /// ::= identifierexpr + /// ::= numberexpr + /// ::= parenexpr + /// + /// Implement `std::unique_ptr<ExprAST> ParsePrimary();` from the tutorial. + </span><span class="kw">fn </span>parse_primary(<span class="kw-2">&mut </span><span class="self">self</span>) -> ParseResult<ExprAST> { + <span class="kw">match </span><span class="kw-2">*</span><span class="self">self</span>.cur_tok() { + Token::Identifier(<span class="kw">_</span>) => <span class="self">self</span>.parse_identifier_expr(), + Token::Number(<span class="kw">_</span>) => <span class="self">self</span>.parse_num_expr(), + Token::Char(<span class="string">'('</span>) => <span class="self">self</span>.parse_paren_expr(), + Token::If => <span class="self">self</span>.parse_if_expr(), + Token::For => <span class="self">self</span>.parse_for_expr(), + <span class="kw">_ </span>=> <span class="prelude-val">Err</span>(<span class="string">"unknown token when expecting an expression"</span>.into()), } } - <span class="comment">// -----------------------------</span> - <span class="comment">// Binary Expression Parsing</span> - <span class="comment">// -----------------------------</span> - - <span class="doccomment">/// /// expression</span> - <span class="doccomment">/// ::= primary binoprhs</span> - <span class="doccomment">///</span> - <span class="doccomment">/// Implement `std::unique_ptr<ExprAST> ParseExpression();` from the tutorial.</span> - <span class="kw">fn</span> <span class="ident">parse_expression</span>(<span class="kw-2">&mut</span> <span class="self">self</span>) -> <span class="ident">ParseResult</span><span class="op"><</span><span class="ident">ExprAST</span><span class="op">></span> { - <span class="kw">let</span> <span class="ident">lhs</span> <span class="op">=</span> <span class="self">self</span>.<span class="ident">parse_primary</span>()<span class="question-mark">?</span>; - <span class="self">self</span>.<span class="ident">parse_bin_op_rhs</span>(<span class="number">0</span>, <span class="ident">lhs</span>) + <span class="comment">// ----------------------------- + // Binary Expression Parsing + // ----------------------------- + + </span><span class="doccomment">/// /// expression + /// ::= primary binoprhs + /// + /// Implement `std::unique_ptr<ExprAST> ParseExpression();` from the tutorial. + </span><span class="kw">fn </span>parse_expression(<span class="kw-2">&mut </span><span class="self">self</span>) -> ParseResult<ExprAST> { + <span class="kw">let </span>lhs = <span class="self">self</span>.parse_primary()<span class="question-mark">?</span>; + <span class="self">self</span>.parse_bin_op_rhs(<span class="number">0</span>, lhs) } - <span class="doccomment">/// binoprhs</span> - <span class="doccomment">/// ::= ('+' primary)*</span> - <span class="doccomment">///</span> - <span class="doccomment">/// Implement `std::unique_ptr<ExprAST> ParseBinOpRHS(int ExprPrec, std::unique_ptr<ExprAST> LHS);` from the tutorial.</span> - <span class="kw">fn</span> <span class="ident">parse_bin_op_rhs</span>(<span class="kw-2">&mut</span> <span class="self">self</span>, <span class="ident">expr_prec</span>: <span class="ident">isize</span>, <span class="kw-2">mut</span> <span class="ident">lhs</span>: <span class="ident">ExprAST</span>) -> <span class="ident">ParseResult</span><span class="op"><</span><span class="ident">ExprAST</span><span class="op">></span> { - <span class="kw">loop</span> { - <span class="kw">let</span> <span class="ident">tok_prec</span> <span class="op">=</span> <span class="ident">get_tok_precedence</span>(<span class="self">self</span>.<span class="ident">cur_tok</span>()); - - <span class="comment">// Not a binary operator or precedence is too small.</span> - <span class="kw">if</span> <span class="ident">tok_prec</span> <span class="op"><</span> <span class="ident">expr_prec</span> { - <span class="kw">return</span> <span class="prelude-val">Ok</span>(<span class="ident">lhs</span>); + <span class="doccomment">/// binoprhs + /// ::= ('+' primary)* + /// + /// Implement `std::unique_ptr<ExprAST> ParseBinOpRHS(int ExprPrec, std::unique_ptr<ExprAST> LHS);` from the tutorial. + </span><span class="kw">fn </span>parse_bin_op_rhs(<span class="kw-2">&mut </span><span class="self">self</span>, expr_prec: isize, <span class="kw-2">mut </span>lhs: ExprAST) -> ParseResult<ExprAST> { + <span class="kw">loop </span>{ + <span class="kw">let </span>tok_prec = get_tok_precedence(<span class="self">self</span>.cur_tok()); + + <span class="comment">// Not a binary operator or precedence is too small. + </span><span class="kw">if </span>tok_prec < expr_prec { + <span class="kw">return </span><span class="prelude-val">Ok</span>(lhs); } - <span class="kw">let</span> <span class="ident">binop</span> <span class="op">=</span> <span class="kw">match</span> <span class="self">self</span>.<span class="ident">cur_tok</span>.<span class="ident">take</span>() { - <span class="prelude-val">Some</span>(<span class="ident">Token::Char</span>(<span class="ident">c</span>)) => { - <span class="comment">// Eat binary operator.</span> - <span class="self">self</span>.<span class="ident">get_next_token</span>(); - <span class="ident">c</span> + <span class="kw">let </span>binop = <span class="kw">match </span><span class="self">self</span>.cur_tok.take() { + <span class="prelude-val">Some</span>(Token::Char(c)) => { + <span class="comment">// Eat binary operator. + </span><span class="self">self</span>.get_next_token(); + c } - <span class="kw">_</span> => <span class="macro">unreachable!</span>(), + <span class="kw">_ </span>=> <span class="macro">unreachable!</span>(), }; - <span class="comment">// lhs BINOP1 rhs BINOP2 remrhs</span> - <span class="comment">// ^^^^^^ ^^^^^^</span> - <span class="comment">// tok_prec next_prec</span> - <span class="comment">//</span> - <span class="comment">// In case BINOP1 has higher precedence, we are done here and can build a 'Binary' AST</span> - <span class="comment">// node between 'lhs' and 'rhs'.</span> - <span class="comment">//</span> - <span class="comment">// In case BINOP2 has higher precedence, we take 'rhs' as 'lhs' and recurse into the</span> - <span class="comment">// 'remrhs' expression first.</span> - - <span class="comment">// Parse primary expression after binary operator.</span> - <span class="kw">let</span> <span class="kw-2">mut</span> <span class="ident">rhs</span> <span class="op">=</span> <span class="self">self</span>.<span class="ident">parse_primary</span>()<span class="question-mark">?</span>; - - <span class="kw">let</span> <span class="ident">next_prec</span> <span class="op">=</span> <span class="ident">get_tok_precedence</span>(<span class="self">self</span>.<span class="ident">cur_tok</span>()); - <span class="kw">if</span> <span class="ident">tok_prec</span> <span class="op"><</span> <span class="ident">next_prec</span> { - <span class="comment">// BINOP2 has higher precedence thatn BINOP1, recurse into 'remhs'.</span> - <span class="ident">rhs</span> <span class="op">=</span> <span class="self">self</span>.<span class="ident">parse_bin_op_rhs</span>(<span class="ident">tok_prec</span> <span class="op">+</span> <span class="number">1</span>, <span class="ident">rhs</span>)<span class="question-mark">?</span> - } - - <span class="ident">lhs</span> <span class="op">=</span> <span class="ident">ExprAST::Binary</span>(<span class="ident">binop</span>, <span class="ident">Box::new</span>(<span class="ident">lhs</span>), <span class="ident">Box::new</span>(<span class="ident">rhs</span>)); + <span class="comment">// lhs BINOP1 rhs BINOP2 remrhs + // ^^^^^^ ^^^^^^ + // tok_prec next_prec + // + // In case BINOP1 has higher precedence, we are done here and can build a 'Binary' AST + // node between 'lhs' and 'rhs'. + // + // In case BINOP2 has higher precedence, we take 'rhs' as 'lhs' and recurse into the + // 'remrhs' expression first. + + // Parse primary expression after binary operator. + </span><span class="kw">let </span><span class="kw-2">mut </span>rhs = <span class="self">self</span>.parse_primary()<span class="question-mark">?</span>; + + <span class="kw">let </span>next_prec = get_tok_precedence(<span class="self">self</span>.cur_tok()); + <span class="kw">if </span>tok_prec < next_prec { + <span class="comment">// BINOP2 has higher precedence thatn BINOP1, recurse into 'remhs'. + </span>rhs = <span class="self">self</span>.parse_bin_op_rhs(tok_prec + <span class="number">1</span>, rhs)<span class="question-mark">? + </span>} + + lhs = ExprAST::Binary(binop, Box::new(lhs), Box::new(rhs)); } } - <span class="comment">// --------------------</span> - <span class="comment">// Parsing the Rest</span> - <span class="comment">// --------------------</span> - - <span class="doccomment">/// prototype</span> - <span class="doccomment">/// ::= id '(' id* ')'</span> - <span class="doccomment">///</span> - <span class="doccomment">/// Implement `std::unique_ptr<PrototypeAST> ParsePrototype();` from the tutorial.</span> - <span class="kw">fn</span> <span class="ident">parse_prototype</span>(<span class="kw-2">&mut</span> <span class="self">self</span>) -> <span class="ident">ParseResult</span><span class="op"><</span><span class="ident">PrototypeAST</span><span class="op">></span> { - <span class="kw">let</span> <span class="ident">id_name</span> <span class="op">=</span> <span class="kw">match</span> <span class="self">self</span>.<span class="ident">cur_tok</span>.<span class="ident">take</span>() { - <span class="prelude-val">Some</span>(<span class="ident">Token::Identifier</span>(<span class="ident">id</span>)) => { - <span class="comment">// Consume the identifier.</span> - <span class="self">self</span>.<span class="ident">get_next_token</span>(); - <span class="ident">id</span> + <span class="comment">// -------------------- + // Parsing the Rest + // -------------------- + + </span><span class="doccomment">/// prototype + /// ::= id '(' id* ')' + /// + /// Implement `std::unique_ptr<PrototypeAST> ParsePrototype();` from the tutorial. + </span><span class="kw">fn </span>parse_prototype(<span class="kw-2">&mut </span><span class="self">self</span>) -> ParseResult<PrototypeAST> { + <span class="kw">let </span>id_name = <span class="kw">match </span><span class="self">self</span>.cur_tok.take() { + <span class="prelude-val">Some</span>(Token::Identifier(id)) => { + <span class="comment">// Consume the identifier. + </span><span class="self">self</span>.get_next_token(); + id } - <span class="ident">other</span> => { - <span class="comment">// Plug back current token.</span> - <span class="self">self</span>.<span class="ident">cur_tok</span> <span class="op">=</span> <span class="ident">other</span>; - <span class="kw">return</span> <span class="prelude-val">Err</span>(<span class="string">"Expected function name in prototype"</span>.<span class="ident">into</span>()); + other => { + <span class="comment">// Plug back current token. + </span><span class="self">self</span>.cur_tok = other; + <span class="kw">return </span><span class="prelude-val">Err</span>(<span class="string">"Expected function name in prototype"</span>.into()); } }; - <span class="kw">if</span> <span class="kw-2">*</span><span class="self">self</span>.<span class="ident">cur_tok</span>() <span class="op">!</span><span class="op">=</span> <span class="ident">Token::Char</span>(<span class="string">'('</span>) { - <span class="kw">return</span> <span class="prelude-val">Err</span>(<span class="string">"Expected '(' in prototype"</span>.<span class="ident">into</span>()); + <span class="kw">if </span><span class="kw-2">*</span><span class="self">self</span>.cur_tok() != Token::Char(<span class="string">'('</span>) { + <span class="kw">return </span><span class="prelude-val">Err</span>(<span class="string">"Expected '(' in prototype"</span>.into()); } - <span class="kw">let</span> <span class="kw-2">mut</span> <span class="ident">args</span>: <span class="ident">Vec</span><span class="op"><</span><span class="ident">String</span><span class="op">></span> <span class="op">=</span> <span class="ident">Vec::new</span>(); - <span class="kw">loop</span> { - <span class="self">self</span>.<span class="ident">get_next_token</span>(); + <span class="kw">let </span><span class="kw-2">mut </span>args: Vec<String> = Vec::new(); + <span class="kw">loop </span>{ + <span class="self">self</span>.get_next_token(); - <span class="kw">match</span> <span class="self">self</span>.<span class="ident">cur_tok</span>.<span class="ident">take</span>() { - <span class="prelude-val">Some</span>(<span class="ident">Token::Identifier</span>(<span class="ident">arg</span>)) => <span class="ident">args</span>.<span class="ident">push</span>(<span class="ident">arg</span>), - <span class="prelude-val">Some</span>(<span class="ident">Token::Char</span>(<span class="string">','</span>)) => {} - <span class="ident">other</span> => { - <span class="self">self</span>.<span class="ident">cur_tok</span> <span class="op">=</span> <span class="ident">other</span>; + <span class="kw">match </span><span class="self">self</span>.cur_tok.take() { + <span class="prelude-val">Some</span>(Token::Identifier(arg)) => args.push(arg), + <span class="prelude-val">Some</span>(Token::Char(<span class="string">','</span>)) => {} + other => { + <span class="self">self</span>.cur_tok = other; <span class="kw">break</span>; } } } - <span class="kw">if</span> <span class="kw-2">*</span><span class="self">self</span>.<span class="ident">cur_tok</span>() <span class="op">!</span><span class="op">=</span> <span class="ident">Token::Char</span>(<span class="string">')'</span>) { - <span class="kw">return</span> <span class="prelude-val">Err</span>(<span class="string">"Expected ')' in prototype"</span>.<span class="ident">into</span>()); + <span class="kw">if </span><span class="kw-2">*</span><span class="self">self</span>.cur_tok() != Token::Char(<span class="string">')'</span>) { + <span class="kw">return </span><span class="prelude-val">Err</span>(<span class="string">"Expected ')' in prototype"</span>.into()); } - <span class="comment">// Consume ')'.</span> - <span class="self">self</span>.<span class="ident">get_next_token</span>(); + <span class="comment">// Consume ')'. + </span><span class="self">self</span>.get_next_token(); - <span class="prelude-val">Ok</span>(<span class="ident">PrototypeAST</span>(<span class="ident">id_name</span>, <span class="ident">args</span>)) + <span class="prelude-val">Ok</span>(PrototypeAST(id_name, args)) } - <span class="doccomment">/// definition ::= 'def' prototype expression</span> - <span class="doccomment">///</span> - <span class="doccomment">/// Implement `std::unique_ptr<FunctionAST> ParseDefinition();` from the tutorial.</span> - <span class="kw">pub</span> <span class="kw">fn</span> <span class="ident">parse_definition</span>(<span class="kw-2">&mut</span> <span class="self">self</span>) -> <span class="ident">ParseResult</span><span class="op"><</span><span class="ident">FunctionAST</span><span class="op">></span> { - <span class="comment">// Consume 'def' token.</span> - <span class="macro">assert_eq!</span>(<span class="kw-2">*</span><span class="self">self</span>.<span class="ident">cur_tok</span>(), <span class="ident">Token::Def</span>); - <span class="self">self</span>.<span class="ident">get_next_token</span>(); + <span class="doccomment">/// definition ::= 'def' prototype expression + /// + /// Implement `std::unique_ptr<FunctionAST> ParseDefinition();` from the tutorial. + </span><span class="kw">pub fn </span>parse_definition(<span class="kw-2">&mut </span><span class="self">self</span>) -> ParseResult<FunctionAST> { + <span class="comment">// Consume 'def' token. + </span><span class="macro">assert_eq!</span>(<span class="kw-2">*</span><span class="self">self</span>.cur_tok(), Token::Def); + <span class="self">self</span>.get_next_token(); - <span class="kw">let</span> <span class="ident">proto</span> <span class="op">=</span> <span class="self">self</span>.<span class="ident">parse_prototype</span>()<span class="question-mark">?</span>; - <span class="kw">let</span> <span class="ident">expr</span> <span class="op">=</span> <span class="self">self</span>.<span class="ident">parse_expression</span>()<span class="question-mark">?</span>; + <span class="kw">let </span>proto = <span class="self">self</span>.parse_prototype()<span class="question-mark">?</span>; + <span class="kw">let </span>expr = <span class="self">self</span>.parse_expression()<span class="question-mark">?</span>; - <span class="prelude-val">Ok</span>(<span class="ident">FunctionAST</span>(<span class="ident">proto</span>, <span class="ident">expr</span>)) + <span class="prelude-val">Ok</span>(FunctionAST(proto, expr)) } - <span class="doccomment">/// external ::= 'extern' prototype</span> - <span class="doccomment">///</span> - <span class="doccomment">/// Implement `std::unique_ptr<PrototypeAST> ParseExtern();` from the tutorial.</span> - <span class="kw">pub</span> <span class="kw">fn</span> <span class="ident">parse_extern</span>(<span class="kw-2">&mut</span> <span class="self">self</span>) -> <span class="ident">ParseResult</span><span class="op"><</span><span class="ident">PrototypeAST</span><span class="op">></span> { - <span class="comment">// Consume 'extern' token.</span> - <span class="macro">assert_eq!</span>(<span class="kw-2">*</span><span class="self">self</span>.<span class="ident">cur_tok</span>(), <span class="ident">Token::Extern</span>); - <span class="self">self</span>.<span class="ident">get_next_token</span>(); + <span class="doccomment">/// external ::= 'extern' prototype + /// + /// Implement `std::unique_ptr<PrototypeAST> ParseExtern();` from the tutorial. + </span><span class="kw">pub fn </span>parse_extern(<span class="kw-2">&mut </span><span class="self">self</span>) -> ParseResult<PrototypeAST> { + <span class="comment">// Consume 'extern' token. + </span><span class="macro">assert_eq!</span>(<span class="kw-2">*</span><span class="self">self</span>.cur_tok(), Token::Extern); + <span class="self">self</span>.get_next_token(); - <span class="self">self</span>.<span class="ident">parse_prototype</span>() + <span class="self">self</span>.parse_prototype() } - <span class="doccomment">/// toplevelexpr ::= expression</span> - <span class="doccomment">///</span> - <span class="doccomment">/// Implement `std::unique_ptr<FunctionAST> ParseTopLevelExpr();` from the tutorial.</span> - <span class="kw">pub</span> <span class="kw">fn</span> <span class="ident">parse_top_level_expr</span>(<span class="kw-2">&mut</span> <span class="self">self</span>) -> <span class="ident">ParseResult</span><span class="op"><</span><span class="ident">FunctionAST</span><span class="op">></span> { - <span class="kw">let</span> <span class="ident">e</span> <span class="op">=</span> <span class="self">self</span>.<span class="ident">parse_expression</span>()<span class="question-mark">?</span>; - <span class="kw">let</span> <span class="ident">proto</span> <span class="op">=</span> <span class="ident">PrototypeAST</span>(<span class="string">"__anon_expr"</span>.<span class="ident">into</span>(), <span class="ident">Vec::new</span>()); - <span class="prelude-val">Ok</span>(<span class="ident">FunctionAST</span>(<span class="ident">proto</span>, <span class="ident">e</span>)) + <span class="doccomment">/// toplevelexpr ::= expression + /// + /// Implement `std::unique_ptr<FunctionAST> ParseTopLevelExpr();` from the tutorial. + </span><span class="kw">pub fn </span>parse_top_level_expr(<span class="kw-2">&mut </span><span class="self">self</span>) -> ParseResult<FunctionAST> { + <span class="kw">let </span>e = <span class="self">self</span>.parse_expression()<span class="question-mark">?</span>; + <span class="kw">let </span>proto = PrototypeAST(<span class="string">"__anon_expr"</span>.into(), Vec::new()); + <span class="prelude-val">Ok</span>(FunctionAST(proto, e)) } } -<span class="doccomment">/// Get the binary operator precedence.</span> -<span class="doccomment">///</span> -<span class="doccomment">/// Implement `int GetTokPrecedence();` from the tutorial.</span> -<span class="kw">fn</span> <span class="ident">get_tok_precedence</span>(<span class="ident">tok</span>: <span class="kw-2">&</span><span class="ident">Token</span>) -> <span class="ident">isize</span> { - <span class="kw">match</span> <span class="ident">tok</span> { - <span class="ident">Token::Char</span>(<span class="string">'<'</span>) => <span class="number">10</span>, - <span class="ident">Token::Char</span>(<span class="string">'+'</span>) => <span class="number">20</span>, - <span class="ident">Token::Char</span>(<span class="string">'-'</span>) => <span class="number">20</span>, - <span class="ident">Token::Char</span>(<span class="string">'*'</span>) => <span class="number">40</span>, - <span class="kw">_</span> => <span class="op">-</span><span class="number">1</span>, +<span class="doccomment">/// Get the binary operator precedence. +/// +/// Implement `int GetTokPrecedence();` from the tutorial. +</span><span class="kw">fn </span>get_tok_precedence(tok: <span class="kw-2">&</span>Token) -> isize { + <span class="kw">match </span>tok { + Token::Char(<span class="string">'<'</span>) => <span class="number">10</span>, + Token::Char(<span class="string">'+'</span>) => <span class="number">20</span>, + Token::Char(<span class="string">'-'</span>) => <span class="number">20</span>, + Token::Char(<span class="string">'*'</span>) => <span class="number">40</span>, + <span class="kw">_ </span>=> -<span class="number">1</span>, } } -<span class="attribute">#[<span class="ident">cfg</span>(<span class="ident">test</span>)]</span> -<span class="kw">mod</span> <span class="ident">test</span> { - <span class="kw">use</span> <span class="kw">super</span>::{<span class="ident">ExprAST</span>, <span class="ident">FunctionAST</span>, <span class="ident">Parser</span>, <span class="ident">PrototypeAST</span>}; - <span class="kw">use</span> <span class="ident"><span class="kw">crate</span>::lexer::Lexer</span>; +<span class="attribute">#[cfg(test)] +</span><span class="kw">mod </span>test { + <span class="kw">use super</span>::{ExprAST, FunctionAST, Parser, PrototypeAST}; + <span class="kw">use </span><span class="kw">crate</span>::lexer::Lexer; - <span class="kw">fn</span> <span class="ident">parser</span>(<span class="ident">input</span>: <span class="kw-2">&</span><span class="ident">str</span>) -> <span class="ident">Parser</span><span class="op"><</span><span class="ident">std::str::Chars</span><span class="op">></span> { - <span class="kw">let</span> <span class="ident">l</span> <span class="op">=</span> <span class="ident">Lexer::new</span>(<span class="ident">input</span>.<span class="ident">chars</span>()); - <span class="kw">let</span> <span class="kw-2">mut</span> <span class="ident">p</span> <span class="op">=</span> <span class="ident">Parser::new</span>(<span class="ident">l</span>); + <span class="kw">fn </span>parser(input: <span class="kw-2">&</span>str) -> Parser<std::str::Chars> { + <span class="kw">let </span>l = Lexer::new(input.chars()); + <span class="kw">let </span><span class="kw-2">mut </span>p = Parser::new(l); - <span class="comment">// Drop initial coin, initialize cur_tok.</span> - <span class="ident">p</span>.<span class="ident">get_next_token</span>(); + <span class="comment">// Drop initial coin, initialize cur_tok. + </span>p.get_next_token(); - <span class="ident">p</span> + p } - <span class="attribute">#[<span class="ident">test</span>]</span> - <span class="kw">fn</span> <span class="ident">parse_number</span>() { - <span class="kw">let</span> <span class="kw-2">mut</span> <span class="ident">p</span> <span class="op">=</span> <span class="ident">parser</span>(<span class="string">"13.37"</span>); + <span class="attribute">#[test] + </span><span class="kw">fn </span>parse_number() { + <span class="kw">let </span><span class="kw-2">mut </span>p = parser(<span class="string">"13.37"</span>); - <span class="macro">assert_eq!</span>(<span class="ident">p</span>.<span class="ident">parse_num_expr</span>(), <span class="prelude-val">Ok</span>(<span class="ident">ExprAST::Number</span>(<span class="number">13.37f64</span>))); + <span class="macro">assert_eq!</span>(p.parse_num_expr(), <span class="prelude-val">Ok</span>(ExprAST::Number(<span class="number">13.37f64</span>))); } - <span class="attribute">#[<span class="ident">test</span>]</span> - <span class="kw">fn</span> <span class="ident">parse_variable</span>() { - <span class="kw">let</span> <span class="kw-2">mut</span> <span class="ident">p</span> <span class="op">=</span> <span class="ident">parser</span>(<span class="string">"foop"</span>); + <span class="attribute">#[test] + </span><span class="kw">fn </span>parse_variable() { + <span class="kw">let </span><span class="kw-2">mut </span>p = parser(<span class="string">"foop"</span>); <span class="macro">assert_eq!</span>( - <span class="ident">p</span>.<span class="ident">parse_identifier_expr</span>(), - <span class="prelude-val">Ok</span>(<span class="ident">ExprAST::Variable</span>(<span class="string">"foop"</span>.<span class="ident">into</span>())) + p.parse_identifier_expr(), + <span class="prelude-val">Ok</span>(ExprAST::Variable(<span class="string">"foop"</span>.into())) ); } - <span class="attribute">#[<span class="ident">test</span>]</span> - <span class="kw">fn</span> <span class="ident">parse_if</span>() { - <span class="kw">let</span> <span class="kw-2">mut</span> <span class="ident">p</span> <span class="op">=</span> <span class="ident">parser</span>(<span class="string">"if 1 then 2 else 3"</span>); + <span class="attribute">#[test] + </span><span class="kw">fn </span>parse_if() { + <span class="kw">let </span><span class="kw-2">mut </span>p = parser(<span class="string">"if 1 then 2 else 3"</span>); - <span class="kw">let</span> <span class="ident">cond</span> <span class="op">=</span> <span class="ident">Box::new</span>(<span class="ident">ExprAST::Number</span>(<span class="number">1f64</span>)); - <span class="kw">let</span> <span class="ident">then</span> <span class="op">=</span> <span class="ident">Box::new</span>(<span class="ident">ExprAST::Number</span>(<span class="number">2f64</span>)); - <span class="kw">let</span> <span class="ident">else_</span> <span class="op">=</span> <span class="ident">Box::new</span>(<span class="ident">ExprAST::Number</span>(<span class="number">3f64</span>)); + <span class="kw">let </span>cond = Box::new(ExprAST::Number(<span class="number">1f64</span>)); + <span class="kw">let </span>then = Box::new(ExprAST::Number(<span class="number">2f64</span>)); + <span class="kw">let </span>else_ = Box::new(ExprAST::Number(<span class="number">3f64</span>)); - <span class="macro">assert_eq!</span>(<span class="ident">p</span>.<span class="ident">parse_if_expr</span>(), <span class="prelude-val">Ok</span>(<span class="ident">ExprAST::If</span> { <span class="ident">cond</span>, <span class="ident">then</span>, <span class="ident">else_</span> })); + <span class="macro">assert_eq!</span>(p.parse_if_expr(), <span class="prelude-val">Ok</span>(ExprAST::If { cond, then, else_ })); - <span class="kw">let</span> <span class="kw-2">mut</span> <span class="ident">p</span> <span class="op">=</span> <span class="ident">parser</span>(<span class="string">"if foo() then bar(2) else baz(3)"</span>); + <span class="kw">let </span><span class="kw-2">mut </span>p = parser(<span class="string">"if foo() then bar(2) else baz(3)"</span>); - <span class="kw">let</span> <span class="ident">cond</span> <span class="op">=</span> <span class="ident">Box::new</span>(<span class="ident">ExprAST::Call</span>(<span class="string">"foo"</span>.<span class="ident">into</span>(), <span class="macro">vec!</span>[])); - <span class="kw">let</span> <span class="ident">then</span> <span class="op">=</span> <span class="ident">Box::new</span>(<span class="ident">ExprAST::Call</span>(<span class="string">"bar"</span>.<span class="ident">into</span>(), <span class="macro">vec!</span>[<span class="ident">ExprAST::Number</span>(<span class="number">2f64</span>)])); - <span class="kw">let</span> <span class="ident">else_</span> <span class="op">=</span> <span class="ident">Box::new</span>(<span class="ident">ExprAST::Call</span>(<span class="string">"baz"</span>.<span class="ident">into</span>(), <span class="macro">vec!</span>[<span class="ident">ExprAST::Number</span>(<span class="number">3f64</span>)])); + <span class="kw">let </span>cond = Box::new(ExprAST::Call(<span class="string">"foo"</span>.into(), <span class="macro">vec!</span>[])); + <span class="kw">let </span>then = Box::new(ExprAST::Call(<span class="string">"bar"</span>.into(), <span class="macro">vec!</span>[ExprAST::Number(<span class="number">2f64</span>)])); + <span class="kw">let </span>else_ = Box::new(ExprAST::Call(<span class="string">"baz"</span>.into(), <span class="macro">vec!</span>[ExprAST::Number(<span class="number">3f64</span>)])); - <span class="macro">assert_eq!</span>(<span class="ident">p</span>.<span class="ident">parse_if_expr</span>(), <span class="prelude-val">Ok</span>(<span class="ident">ExprAST::If</span> { <span class="ident">cond</span>, <span class="ident">then</span>, <span class="ident">else_</span> })); + <span class="macro">assert_eq!</span>(p.parse_if_expr(), <span class="prelude-val">Ok</span>(ExprAST::If { cond, then, else_ })); } - <span class="attribute">#[<span class="ident">test</span>]</span> - <span class="kw">fn</span> <span class="ident">parse_for</span>() { - <span class="kw">let</span> <span class="kw-2">mut</span> <span class="ident">p</span> <span class="op">=</span> <span class="ident">parser</span>(<span class="string">"for i = 1, 2, 3 in 4"</span>); + <span class="attribute">#[test] + </span><span class="kw">fn </span>parse_for() { + <span class="kw">let </span><span class="kw-2">mut </span>p = parser(<span class="string">"for i = 1, 2, 3 in 4"</span>); - <span class="kw">let</span> <span class="ident">var</span> <span class="op">=</span> <span class="ident">String::from</span>(<span class="string">"i"</span>); - <span class="kw">let</span> <span class="ident">start</span> <span class="op">=</span> <span class="ident">Box::new</span>(<span class="ident">ExprAST::Number</span>(<span class="number">1f64</span>)); - <span class="kw">let</span> <span class="ident">end</span> <span class="op">=</span> <span class="ident">Box::new</span>(<span class="ident">ExprAST::Number</span>(<span class="number">2f64</span>)); - <span class="kw">let</span> <span class="ident">step</span> <span class="op">=</span> <span class="prelude-val">Some</span>(<span class="ident">Box::new</span>(<span class="ident">ExprAST::Number</span>(<span class="number">3f64</span>))); - <span class="kw">let</span> <span class="ident">body</span> <span class="op">=</span> <span class="ident">Box::new</span>(<span class="ident">ExprAST::Number</span>(<span class="number">4f64</span>)); + <span class="kw">let </span>var = String::from(<span class="string">"i"</span>); + <span class="kw">let </span>start = Box::new(ExprAST::Number(<span class="number">1f64</span>)); + <span class="kw">let </span>end = Box::new(ExprAST::Number(<span class="number">2f64</span>)); + <span class="kw">let </span>step = <span class="prelude-val">Some</span>(Box::new(ExprAST::Number(<span class="number">3f64</span>))); + <span class="kw">let </span>body = Box::new(ExprAST::Number(<span class="number">4f64</span>)); <span class="macro">assert_eq!</span>( - <span class="ident">p</span>.<span class="ident">parse_for_expr</span>(), - <span class="prelude-val">Ok</span>(<span class="ident">ExprAST::For</span> { - <span class="ident">var</span>, - <span class="ident">start</span>, - <span class="ident">end</span>, - <span class="ident">step</span>, - <span class="ident">body</span> + p.parse_for_expr(), + <span class="prelude-val">Ok</span>(ExprAST::For { + var, + start, + end, + step, + body }) ); } - <span class="attribute">#[<span class="ident">test</span>]</span> - <span class="kw">fn</span> <span class="ident">parse_for_no_step</span>() { - <span class="kw">let</span> <span class="kw-2">mut</span> <span class="ident">p</span> <span class="op">=</span> <span class="ident">parser</span>(<span class="string">"for i = 1, 2 in 4"</span>); + <span class="attribute">#[test] + </span><span class="kw">fn </span>parse_for_no_step() { + <span class="kw">let </span><span class="kw-2">mut </span>p = parser(<span class="string">"for i = 1, 2 in 4"</span>); - <span class="kw">let</span> <span class="ident">var</span> <span class="op">=</span> <span class="ident">String::from</span>(<span class="string">"i"</span>); - <span class="kw">let</span> <span class="ident">start</span> <span class="op">=</span> <span class="ident">Box::new</span>(<span class="ident">ExprAST::Number</span>(<span class="number">1f64</span>)); - <span class="kw">let</span> <span class="ident">end</span> <span class="op">=</span> <span class="ident">Box::new</span>(<span class="ident">ExprAST::Number</span>(<span class="number">2f64</span>)); - <span class="kw">let</span> <span class="ident">step</span> <span class="op">=</span> <span class="prelude-val">None</span>; - <span class="kw">let</span> <span class="ident">body</span> <span class="op">=</span> <span class="ident">Box::new</span>(<span class="ident">ExprAST::Number</span>(<span class="number">4f64</span>)); + <span class="kw">let </span>var = String::from(<span class="string">"i"</span>); + <span class="kw">let </span>start = Box::new(ExprAST::Number(<span class="number">1f64</span>)); + <span class="kw">let </span>end = Box::new(ExprAST::Number(<span class="number">2f64</span>)); + <span class="kw">let </span>step = <span class="prelude-val">None</span>; + <span class="kw">let </span>body = Box::new(ExprAST::Number(<span class="number">4f64</span>)); <span class="macro">assert_eq!</span>( - <span class="ident">p</span>.<span class="ident">parse_for_expr</span>(), - <span class="prelude-val">Ok</span>(<span class="ident">ExprAST::For</span> { - <span class="ident">var</span>, - <span class="ident">start</span>, - <span class="ident">end</span>, - <span class="ident">step</span>, - <span class="ident">body</span> + p.parse_for_expr(), + <span class="prelude-val">Ok</span>(ExprAST::For { + var, + start, + end, + step, + body }) ); } - <span class="attribute">#[<span class="ident">test</span>]</span> - <span class="kw">fn</span> <span class="ident">parse_primary</span>() { - <span class="kw">let</span> <span class="kw-2">mut</span> <span class="ident">p</span> <span class="op">=</span> <span class="ident">parser</span>(<span class="string">"1337 foop \n bla(123) \n if a then b else c \n for x=1,2 in 3"</span>); + <span class="attribute">#[test] + </span><span class="kw">fn </span>parse_primary() { + <span class="kw">let </span><span class="kw-2">mut </span>p = parser(<span class="string">"1337 foop \n bla(123) \n if a then b else c \n for x=1,2 in 3"</span>); - <span class="macro">assert_eq!</span>(<span class="ident">p</span>.<span class="ident">parse_primary</span>(), <span class="prelude-val">Ok</span>(<span class="ident">ExprAST::Number</span>(<span class="number">1337f64</span>))); + <span class="macro">assert_eq!</span>(p.parse_primary(), <span class="prelude-val">Ok</span>(ExprAST::Number(<span class="number">1337f64</span>))); - <span class="macro">assert_eq!</span>(<span class="ident">p</span>.<span class="ident">parse_primary</span>(), <span class="prelude-val">Ok</span>(<span class="ident">ExprAST::Variable</span>(<span class="string">"foop"</span>.<span class="ident">into</span>()))); + <span class="macro">assert_eq!</span>(p.parse_primary(), <span class="prelude-val">Ok</span>(ExprAST::Variable(<span class="string">"foop"</span>.into()))); <span class="macro">assert_eq!</span>( - <span class="ident">p</span>.<span class="ident">parse_primary</span>(), - <span class="prelude-val">Ok</span>(<span class="ident">ExprAST::Call</span>(<span class="string">"bla"</span>.<span class="ident">into</span>(), <span class="macro">vec!</span>[<span class="ident">ExprAST::Number</span>(<span class="number">123f64</span>)])) + p.parse_primary(), + <span class="prelude-val">Ok</span>(ExprAST::Call(<span class="string">"bla"</span>.into(), <span class="macro">vec!</span>[ExprAST::Number(<span class="number">123f64</span>)])) ); <span class="macro">assert_eq!</span>( - <span class="ident">p</span>.<span class="ident">parse_primary</span>(), - <span class="prelude-val">Ok</span>(<span class="ident">ExprAST::If</span> { - <span class="ident">cond</span>: <span class="ident">Box::new</span>(<span class="ident">ExprAST::Variable</span>(<span class="string">"a"</span>.<span class="ident">into</span>())), - <span class="ident">then</span>: <span class="ident">Box::new</span>(<span class="ident">ExprAST::Variable</span>(<span class="string">"b"</span>.<span class="ident">into</span>())), - <span class="ident">else_</span>: <span class="ident">Box::new</span>(<span class="ident">ExprAST::Variable</span>(<span class="string">"c"</span>.<span class="ident">into</span>())), + p.parse_primary(), + <span class="prelude-val">Ok</span>(ExprAST::If { + cond: Box::new(ExprAST::Variable(<span class="string">"a"</span>.into())), + then: Box::new(ExprAST::Variable(<span class="string">"b"</span>.into())), + else_: Box::new(ExprAST::Variable(<span class="string">"c"</span>.into())), }) ); <span class="macro">assert_eq!</span>( - <span class="ident">p</span>.<span class="ident">parse_primary</span>(), - <span class="prelude-val">Ok</span>(<span class="ident">ExprAST::For</span> { - <span class="ident">var</span>: <span class="ident">String::from</span>(<span class="string">"x"</span>), - <span class="ident">start</span>: <span class="ident">Box::new</span>(<span class="ident">ExprAST::Number</span>(<span class="number">1f64</span>)), - <span class="ident">end</span>: <span class="ident">Box::new</span>(<span class="ident">ExprAST::Number</span>(<span class="number">2f64</span>)), - <span class="ident">step</span>: <span class="prelude-val">None</span>, - <span class="ident">body</span>: <span class="ident">Box::new</span>(<span class="ident">ExprAST::Number</span>(<span class="number">3f64</span>)), + p.parse_primary(), + <span class="prelude-val">Ok</span>(ExprAST::For { + var: String::from(<span class="string">"x"</span>), + start: Box::new(ExprAST::Number(<span class="number">1f64</span>)), + end: Box::new(ExprAST::Number(<span class="number">2f64</span>)), + step: <span class="prelude-val">None</span>, + body: Box::new(ExprAST::Number(<span class="number">3f64</span>)), }) ); } - <span class="attribute">#[<span class="ident">test</span>]</span> - <span class="kw">fn</span> <span class="ident">parse_binary_op</span>() { - <span class="comment">// Operator before RHS has higher precedence, expected AST</span> - <span class="comment">//</span> - <span class="comment">// -</span> - <span class="comment">// / \</span> - <span class="comment">// + c</span> - <span class="comment">// / \</span> - <span class="comment">// a b</span> - <span class="kw">let</span> <span class="kw-2">mut</span> <span class="ident">p</span> <span class="op">=</span> <span class="ident">parser</span>(<span class="string">"a + b - c"</span>); - - <span class="kw">let</span> <span class="ident">binexpr_ab</span> <span class="op">=</span> <span class="ident">ExprAST::Binary</span>( + <span class="attribute">#[test] + </span><span class="kw">fn </span>parse_binary_op() { + <span class="comment">// Operator before RHS has higher precedence, expected AST + // + // - + // / \ + // + c + // / \ + // a b + </span><span class="kw">let </span><span class="kw-2">mut </span>p = parser(<span class="string">"a + b - c"</span>); + + <span class="kw">let </span>binexpr_ab = ExprAST::Binary( <span class="string">'+'</span>, - <span class="ident">Box::new</span>(<span class="ident">ExprAST::Variable</span>(<span class="string">"a"</span>.<span class="ident">into</span>())), - <span class="ident">Box::new</span>(<span class="ident">ExprAST::Variable</span>(<span class="string">"b"</span>.<span class="ident">into</span>())), + Box::new(ExprAST::Variable(<span class="string">"a"</span>.into())), + Box::new(ExprAST::Variable(<span class="string">"b"</span>.into())), ); - <span class="kw">let</span> <span class="ident">binexpr_abc</span> <span class="op">=</span> <span class="ident">ExprAST::Binary</span>( + <span class="kw">let </span>binexpr_abc = ExprAST::Binary( <span class="string">'-'</span>, - <span class="ident">Box::new</span>(<span class="ident">binexpr_ab</span>), - <span class="ident">Box::new</span>(<span class="ident">ExprAST::Variable</span>(<span class="string">"c"</span>.<span class="ident">into</span>())), + Box::new(binexpr_ab), + Box::new(ExprAST::Variable(<span class="string">"c"</span>.into())), ); - <span class="macro">assert_eq!</span>(<span class="ident">p</span>.<span class="ident">parse_expression</span>(), <span class="prelude-val">Ok</span>(<span class="ident">binexpr_abc</span>)); + <span class="macro">assert_eq!</span>(p.parse_expression(), <span class="prelude-val">Ok</span>(binexpr_abc)); } - <span class="attribute">#[<span class="ident">test</span>]</span> - <span class="kw">fn</span> <span class="ident">parse_binary_op2</span>() { - <span class="comment">// Operator after RHS has higher precedence, expected AST</span> - <span class="comment">//</span> - <span class="comment">// +</span> - <span class="comment">// / \</span> - <span class="comment">// a *</span> - <span class="comment">// / \</span> - <span class="comment">// b c</span> - <span class="kw">let</span> <span class="kw-2">mut</span> <span class="ident">p</span> <span class="op">=</span> <span class="ident">parser</span>(<span class="string">"a + b * c"</span>); - - <span class="kw">let</span> <span class="ident">binexpr_bc</span> <span class="op">=</span> <span class="ident">ExprAST::Binary</span>( + <span class="attribute">#[test] + </span><span class="kw">fn </span>parse_binary_op2() { + <span class="comment">// Operator after RHS has higher precedence, expected AST + // + // + + // / \ + // a * + // / \ + // b c + </span><span class="kw">let </span><span class="kw-2">mut </span>p = parser(<span class="string">"a + b * c"</span>); + + <span class="kw">let </span>binexpr_bc = ExprAST::Binary( <span class="string">'*'</span>, - <span class="ident">Box::new</span>(<span class="ident">ExprAST::Variable</span>(<span class="string">"b"</span>.<span class="ident">into</span>())), - <span class="ident">Box::new</span>(<span class="ident">ExprAST::Variable</span>(<span class="string">"c"</span>.<span class="ident">into</span>())), + Box::new(ExprAST::Variable(<span class="string">"b"</span>.into())), + Box::new(ExprAST::Variable(<span class="string">"c"</span>.into())), ); - <span class="kw">let</span> <span class="ident">binexpr_abc</span> <span class="op">=</span> <span class="ident">ExprAST::Binary</span>( + <span class="kw">let </span>binexpr_abc = ExprAST::Binary( <span class="string">'+'</span>, - <span class="ident">Box::new</span>(<span class="ident">ExprAST::Variable</span>(<span class="string">"a"</span>.<span class="ident">into</span>())), - <span class="ident">Box::new</span>(<span class="ident">binexpr_bc</span>), + Box::new(ExprAST::Variable(<span class="string">"a"</span>.into())), + Box::new(binexpr_bc), ); - <span class="macro">assert_eq!</span>(<span class="ident">p</span>.<span class="ident">parse_expression</span>(), <span class="prelude-val">Ok</span>(<span class="ident">binexpr_abc</span>)); + <span class="macro">assert_eq!</span>(p.parse_expression(), <span class="prelude-val">Ok</span>(binexpr_abc)); } - <span class="attribute">#[<span class="ident">test</span>]</span> - <span class="kw">fn</span> <span class="ident">parse_prototype</span>() { - <span class="kw">let</span> <span class="kw-2">mut</span> <span class="ident">p</span> <span class="op">=</span> <span class="ident">parser</span>(<span class="string">"foo(a,b)"</span>); + <span class="attribute">#[test] + </span><span class="kw">fn </span>parse_prototype() { + <span class="kw">let </span><span class="kw-2">mut </span>p = parser(<span class="string">"foo(a,b)"</span>); - <span class="kw">let</span> <span class="ident">proto</span> <span class="op">=</span> <span class="ident">PrototypeAST</span>(<span class="string">"foo"</span>.<span class="ident">into</span>(), <span class="macro">vec!</span>[<span class="string">"a"</span>.<span class="ident">into</span>(), <span class="string">"b"</span>.<span class="ident">into</span>()]); + <span class="kw">let </span>proto = PrototypeAST(<span class="string">"foo"</span>.into(), <span class="macro">vec!</span>[<span class="string">"a"</span>.into(), <span class="string">"b"</span>.into()]); - <span class="macro">assert_eq!</span>(<span class="ident">p</span>.<span class="ident">parse_prototype</span>(), <span class="prelude-val">Ok</span>(<span class="ident">proto</span>)); + <span class="macro">assert_eq!</span>(p.parse_prototype(), <span class="prelude-val">Ok</span>(proto)); } - <span class="attribute">#[<span class="ident">test</span>]</span> - <span class="kw">fn</span> <span class="ident">parse_definition</span>() { - <span class="kw">let</span> <span class="kw-2">mut</span> <span class="ident">p</span> <span class="op">=</span> <span class="ident">parser</span>(<span class="string">"def bar( arg0 , arg1 ) arg0 + arg1"</span>); + <span class="attribute">#[test] + </span><span class="kw">fn </span>parse_definition() { + <span class="kw">let </span><span class="kw-2">mut </span>p = parser(<span class="string">"def bar( arg0 , arg1 ) arg0 + arg1"</span>); - <span class="kw">let</span> <span class="ident">proto</span> <span class="op">=</span> <span class="ident">PrototypeAST</span>(<span class="string">"bar"</span>.<span class="ident">into</span>(), <span class="macro">vec!</span>[<span class="string">"arg0"</span>.<span class="ident">into</span>(), <span class="string">"arg1"</span>.<span class="ident">into</span>()]); + <span class="kw">let </span>proto = PrototypeAST(<span class="string">"bar"</span>.into(), <span class="macro">vec!</span>[<span class="string">"arg0"</span>.into(), <span class="string">"arg1"</span>.into()]); - <span class="kw">let</span> <span class="ident">body</span> <span class="op">=</span> <span class="ident">ExprAST::Binary</span>( + <span class="kw">let </span>body = ExprAST::Binary( <span class="string">'+'</span>, - <span class="ident">Box::new</span>(<span class="ident">ExprAST::Variable</span>(<span class="string">"arg0"</span>.<span class="ident">into</span>())), - <span class="ident">Box::new</span>(<span class="ident">ExprAST::Variable</span>(<span class="string">"arg1"</span>.<span class="ident">into</span>())), + Box::new(ExprAST::Variable(<span class="string">"arg0"</span>.into())), + Box::new(ExprAST::Variable(<span class="string">"arg1"</span>.into())), ); - <span class="kw">let</span> <span class="ident">func</span> <span class="op">=</span> <span class="ident">FunctionAST</span>(<span class="ident">proto</span>, <span class="ident">body</span>); + <span class="kw">let </span>func = FunctionAST(proto, body); - <span class="macro">assert_eq!</span>(<span class="ident">p</span>.<span class="ident">parse_definition</span>(), <span class="prelude-val">Ok</span>(<span class="ident">func</span>)); + <span class="macro">assert_eq!</span>(p.parse_definition(), <span class="prelude-val">Ok</span>(func)); } - <span class="attribute">#[<span class="ident">test</span>]</span> - <span class="kw">fn</span> <span class="ident">parse_extern</span>() { - <span class="kw">let</span> <span class="kw-2">mut</span> <span class="ident">p</span> <span class="op">=</span> <span class="ident">parser</span>(<span class="string">"extern baz()"</span>); + <span class="attribute">#[test] + </span><span class="kw">fn </span>parse_extern() { + <span class="kw">let </span><span class="kw-2">mut </span>p = parser(<span class="string">"extern baz()"</span>); - <span class="kw">let</span> <span class="ident">proto</span> <span class="op">=</span> <span class="ident">PrototypeAST</span>(<span class="string">"baz"</span>.<span class="ident">into</span>(), <span class="macro">vec!</span>[]); + <span class="kw">let </span>proto = PrototypeAST(<span class="string">"baz"</span>.into(), <span class="macro">vec!</span>[]); - <span class="macro">assert_eq!</span>(<span class="ident">p</span>.<span class="ident">parse_extern</span>(), <span class="prelude-val">Ok</span>(<span class="ident">proto</span>)); + <span class="macro">assert_eq!</span>(p.parse_extern(), <span class="prelude-val">Ok</span>(proto)); } } </code></pre></div> -</section></div></main><div id="rustdoc-vars" data-root-path="../../" data-current-crate="llvm_kaleidoscope_rs" data-themes="ayu,dark,light" data-resource-suffix="" data-rustdoc-version="1.63.0 (4b91a6ea7 2022-08-08)" ></div> -</body></html>
\ No newline at end of file +</section></div></main><div id="rustdoc-vars" data-root-path="../../" data-current-crate="llvm_kaleidoscope_rs" data-themes="ayu,dark,light" data-resource-suffix="" data-rustdoc-version="1.65.0 (897e37553 2022-11-02)" ></div></body></html>
\ No newline at end of file |