diff options
author | johannst <johannst@users.noreply.github.com> | 2022-09-24 22:38:40 +0000 |
---|---|---|
committer | johannst <johannst@users.noreply.github.com> | 2022-09-24 22:38:40 +0000 |
commit | 73e25a571f12d3deaa6f4493a5b4792a9dae39eb (patch) | |
tree | d34d13ad7917ad94433e1378dceff6fcccd2718a /src/llvm_kaleidoscope_rs/parser.rs.html | |
parent | 559c9ddd6079b2313c196866215133b926d07d34 (diff) | |
download | llvm-kaleidoscope-rs-73e25a571f12d3deaa6f4493a5b4792a9dae39eb.tar.gz llvm-kaleidoscope-rs-73e25a571f12d3deaa6f4493a5b4792a9dae39eb.zip |
deploy: 295081130ca1eed6e67dfc035e2df2c9ed49b174
Diffstat (limited to 'src/llvm_kaleidoscope_rs/parser.rs.html')
-rw-r--r-- | src/llvm_kaleidoscope_rs/parser.rs.html | 682 |
1 files changed, 537 insertions, 145 deletions
diff --git a/src/llvm_kaleidoscope_rs/parser.rs.html b/src/llvm_kaleidoscope_rs/parser.rs.html index 9093695..d3c9594 100644 --- a/src/llvm_kaleidoscope_rs/parser.rs.html +++ b/src/llvm_kaleidoscope_rs/parser.rs.html @@ -1,102 +1,108 @@ -<!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="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="../../light.css" id="themeStyle"><link rel="stylesheet" type="text/css" href="../../dark.css" disabled ><link rel="stylesheet" type="text/css" href="../../ayu.css" disabled ><script id="default-settings" ></script><script src="../../storage.js"></script><script src="../../crates.js"></script><noscript><link rel="stylesheet" href="../../noscript.css"></noscript><link rel="icon" type="image/svg+xml" href="../../favicon.svg"><link rel="alternate icon" type="image/png" href="../../favicon-16x16.png"><link rel="alternate icon" type="image/png" href="../../favicon-32x32.png"><style type="text/css">#crate-search{background-image:url("../../down-arrow.svg");}</style></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="sidebar"><div class="sidebar-menu" role="button">☰</div><a href='../../llvm_kaleidoscope_rs/index.html'><div class='logo-container rust-logo'><img src='../../rust-logo.png' alt='logo'></div></a></nav><div class="theme-picker"><button id="theme-picker" aria-label="Pick another theme!" aria-haspopup="menu" title="themes"><img width="18" height="18" alt="Pick another theme!" src="../../brush.svg"></button><div id="theme-choices" role="menu"></div></div><nav class="sub"><form class="search-form"><div class="search-container"><div><select id="crate-search"><option value="All crates">All crates</option></select><input class="search-input"name="search" disabled autocomplete="off" spellcheck="false" placeholder="Click or press ‘S’ to search, ‘?’ for more options…" type="search"></div><button type="button" id="help-button" title="help">?</button><a id="settings-menu" href="../../settings.html" title="settings"><img width="18" height="18" alt="Change settings" src="../../wheel.svg"></a></div></form></nav><section id="main" 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> -<span id="5"> 5</span> -<span id="6"> 6</span> -<span id="7"> 7</span> -<span id="8"> 8</span> -<span id="9"> 9</span> -<span id="10"> 10</span> -<span id="11"> 11</span> -<span id="12"> 12</span> -<span id="13"> 13</span> -<span id="14"> 14</span> -<span id="15"> 15</span> -<span id="16"> 16</span> -<span id="17"> 17</span> -<span id="18"> 18</span> -<span id="19"> 19</span> -<span id="20"> 20</span> -<span id="21"> 21</span> -<span id="22"> 22</span> -<span id="23"> 23</span> -<span id="24"> 24</span> -<span id="25"> 25</span> -<span id="26"> 26</span> -<span id="27"> 27</span> -<span id="28"> 28</span> -<span id="29"> 29</span> -<span id="30"> 30</span> -<span id="31"> 31</span> -<span id="32"> 32</span> -<span id="33"> 33</span> -<span id="34"> 34</span> -<span id="35"> 35</span> -<span id="36"> 36</span> -<span id="37"> 37</span> -<span id="38"> 38</span> -<span id="39"> 39</span> -<span id="40"> 40</span> -<span id="41"> 41</span> -<span id="42"> 42</span> -<span id="43"> 43</span> -<span id="44"> 44</span> -<span id="45"> 45</span> -<span id="46"> 46</span> -<span id="47"> 47</span> -<span id="48"> 48</span> -<span id="49"> 49</span> -<span id="50"> 50</span> -<span id="51"> 51</span> -<span id="52"> 52</span> -<span id="53"> 53</span> -<span id="54"> 54</span> -<span id="55"> 55</span> -<span id="56"> 56</span> -<span id="57"> 57</span> -<span id="58"> 58</span> -<span id="59"> 59</span> -<span id="60"> 60</span> -<span id="61"> 61</span> -<span id="62"> 62</span> -<span id="63"> 63</span> -<span id="64"> 64</span> -<span id="65"> 65</span> -<span id="66"> 66</span> -<span id="67"> 67</span> -<span id="68"> 68</span> -<span id="69"> 69</span> -<span id="70"> 70</span> -<span id="71"> 71</span> -<span id="72"> 72</span> -<span id="73"> 73</span> -<span id="74"> 74</span> -<span id="75"> 75</span> -<span id="76"> 76</span> -<span id="77"> 77</span> -<span id="78"> 78</span> -<span id="79"> 79</span> -<span id="80"> 80</span> -<span id="81"> 81</span> -<span id="82"> 82</span> -<span id="83"> 83</span> -<span id="84"> 84</span> -<span id="85"> 85</span> -<span id="86"> 86</span> -<span id="87"> 87</span> -<span id="88"> 88</span> -<span id="89"> 89</span> -<span id="90"> 90</span> -<span id="91"> 91</span> -<span id="92"> 92</span> -<span id="93"> 93</span> -<span id="94"> 94</span> -<span id="95"> 95</span> -<span id="96"> 96</span> -<span id="97"> 97</span> -<span id="98"> 98</span> -<span id="99"> 99</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" 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> +<span id="2">2</span> +<span id="3">3</span> +<span id="4">4</span> +<span id="5">5</span> +<span id="6">6</span> +<span id="7">7</span> +<span id="8">8</span> +<span id="9">9</span> +<span id="10">10</span> +<span id="11">11</span> +<span id="12">12</span> +<span id="13">13</span> +<span id="14">14</span> +<span id="15">15</span> +<span id="16">16</span> +<span id="17">17</span> +<span id="18">18</span> +<span id="19">19</span> +<span id="20">20</span> +<span id="21">21</span> +<span id="22">22</span> +<span id="23">23</span> +<span id="24">24</span> +<span id="25">25</span> +<span id="26">26</span> +<span id="27">27</span> +<span id="28">28</span> +<span id="29">29</span> +<span id="30">30</span> +<span id="31">31</span> +<span id="32">32</span> +<span id="33">33</span> +<span id="34">34</span> +<span id="35">35</span> +<span id="36">36</span> +<span id="37">37</span> +<span id="38">38</span> +<span id="39">39</span> +<span id="40">40</span> +<span id="41">41</span> +<span id="42">42</span> +<span id="43">43</span> +<span id="44">44</span> +<span id="45">45</span> +<span id="46">46</span> +<span id="47">47</span> +<span id="48">48</span> +<span id="49">49</span> +<span id="50">50</span> +<span id="51">51</span> +<span id="52">52</span> +<span id="53">53</span> +<span id="54">54</span> +<span id="55">55</span> +<span id="56">56</span> +<span id="57">57</span> +<span id="58">58</span> +<span id="59">59</span> +<span id="60">60</span> +<span id="61">61</span> +<span id="62">62</span> +<span id="63">63</span> +<span id="64">64</span> +<span id="65">65</span> +<span id="66">66</span> +<span id="67">67</span> +<span id="68">68</span> +<span id="69">69</span> +<span id="70">70</span> +<span id="71">71</span> +<span id="72">72</span> +<span id="73">73</span> +<span id="74">74</span> +<span id="75">75</span> +<span id="76">76</span> +<span id="77">77</span> +<span id="78">78</span> +<span id="79">79</span> +<span id="80">80</span> +<span id="81">81</span> +<span id="82">82</span> +<span id="83">83</span> +<span id="84">84</span> +<span id="85">85</span> +<span id="86">86</span> +<span id="87">87</span> +<span id="88">88</span> +<span id="89">89</span> +<span id="90">90</span> +<span id="91">91</span> +<span id="92">92</span> +<span id="93">93</span> +<span id="94">94</span> +<span id="95">95</span> +<span id="96">96</span> +<span id="97">97</span> +<span id="98">98</span> +<span id="99">99</span> <span id="100">100</span> <span id="101">101</span> <span id="102">102</span> @@ -458,8 +464,201 @@ <span id="458">458</span> <span id="459">459</span> <span id="460">460</span> -</pre><pre class="rust"> -<span class="kw">use</span> <span class="kw">crate</span><span class="ident">::lexer</span>::{<span class="ident">Lexer</span>, <span class="ident">Token</span>}; +<span id="461">461</span> +<span id="462">462</span> +<span id="463">463</span> +<span id="464">464</span> +<span id="465">465</span> +<span id="466">466</span> +<span id="467">467</span> +<span id="468">468</span> +<span id="469">469</span> +<span id="470">470</span> +<span id="471">471</span> +<span id="472">472</span> +<span id="473">473</span> +<span id="474">474</span> +<span id="475">475</span> +<span id="476">476</span> +<span id="477">477</span> +<span id="478">478</span> +<span id="479">479</span> +<span id="480">480</span> +<span id="481">481</span> +<span id="482">482</span> +<span id="483">483</span> +<span id="484">484</span> +<span id="485">485</span> +<span id="486">486</span> +<span id="487">487</span> +<span id="488">488</span> +<span id="489">489</span> +<span id="490">490</span> +<span id="491">491</span> +<span id="492">492</span> +<span id="493">493</span> +<span id="494">494</span> +<span id="495">495</span> +<span id="496">496</span> +<span id="497">497</span> +<span id="498">498</span> +<span id="499">499</span> +<span id="500">500</span> +<span id="501">501</span> +<span id="502">502</span> +<span id="503">503</span> +<span id="504">504</span> +<span id="505">505</span> +<span id="506">506</span> +<span id="507">507</span> +<span id="508">508</span> +<span id="509">509</span> +<span id="510">510</span> +<span id="511">511</span> +<span id="512">512</span> +<span id="513">513</span> +<span id="514">514</span> +<span id="515">515</span> +<span id="516">516</span> +<span id="517">517</span> +<span id="518">518</span> +<span id="519">519</span> +<span id="520">520</span> +<span id="521">521</span> +<span id="522">522</span> +<span id="523">523</span> +<span id="524">524</span> +<span id="525">525</span> +<span id="526">526</span> +<span id="527">527</span> +<span id="528">528</span> +<span id="529">529</span> +<span id="530">530</span> +<span id="531">531</span> +<span id="532">532</span> +<span id="533">533</span> +<span id="534">534</span> +<span id="535">535</span> +<span id="536">536</span> +<span id="537">537</span> +<span id="538">538</span> +<span id="539">539</span> +<span id="540">540</span> +<span id="541">541</span> +<span id="542">542</span> +<span id="543">543</span> +<span id="544">544</span> +<span id="545">545</span> +<span id="546">546</span> +<span id="547">547</span> +<span id="548">548</span> +<span id="549">549</span> +<span id="550">550</span> +<span id="551">551</span> +<span id="552">552</span> +<span id="553">553</span> +<span id="554">554</span> +<span id="555">555</span> +<span id="556">556</span> +<span id="557">557</span> +<span id="558">558</span> +<span id="559">559</span> +<span id="560">560</span> +<span id="561">561</span> +<span id="562">562</span> +<span id="563">563</span> +<span id="564">564</span> +<span id="565">565</span> +<span id="566">566</span> +<span id="567">567</span> +<span id="568">568</span> +<span id="569">569</span> +<span id="570">570</span> +<span id="571">571</span> +<span id="572">572</span> +<span id="573">573</span> +<span id="574">574</span> +<span id="575">575</span> +<span id="576">576</span> +<span id="577">577</span> +<span id="578">578</span> +<span id="579">579</span> +<span id="580">580</span> +<span id="581">581</span> +<span id="582">582</span> +<span id="583">583</span> +<span id="584">584</span> +<span id="585">585</span> +<span id="586">586</span> +<span id="587">587</span> +<span id="588">588</span> +<span id="589">589</span> +<span id="590">590</span> +<span id="591">591</span> +<span id="592">592</span> +<span id="593">593</span> +<span id="594">594</span> +<span id="595">595</span> +<span id="596">596</span> +<span id="597">597</span> +<span id="598">598</span> +<span id="599">599</span> +<span id="600">600</span> +<span id="601">601</span> +<span id="602">602</span> +<span id="603">603</span> +<span id="604">604</span> +<span id="605">605</span> +<span id="606">606</span> +<span id="607">607</span> +<span id="608">608</span> +<span id="609">609</span> +<span id="610">610</span> +<span id="611">611</span> +<span id="612">612</span> +<span id="613">613</span> +<span id="614">614</span> +<span id="615">615</span> +<span id="616">616</span> +<span id="617">617</span> +<span id="618">618</span> +<span id="619">619</span> +<span id="620">620</span> +<span id="621">621</span> +<span id="622">622</span> +<span id="623">623</span> +<span id="624">624</span> +<span id="625">625</span> +<span id="626">626</span> +<span id="627">627</span> +<span id="628">628</span> +<span id="629">629</span> +<span id="630">630</span> +<span id="631">631</span> +<span id="632">632</span> +<span id="633">633</span> +<span id="634">634</span> +<span id="635">635</span> +<span id="636">636</span> +<span id="637">637</span> +<span id="638">638</span> +<span id="639">639</span> +<span id="640">640</span> +<span id="641">641</span> +<span id="642">642</span> +<span id="643">643</span> +<span id="644">644</span> +<span id="645">645</span> +<span id="646">646</span> +<span id="647">647</span> +<span id="648">648</span> +<span id="649">649</span> +<span id="650">650</span> +<span id="651">651</span> +<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>}; <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> { @@ -474,6 +673,22 @@ <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">/// 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">/// 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">/// PrototypeAST - This class represents the "prototype" for a function,</span> @@ -502,7 +717,7 @@ <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">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="op">-</span><span class="op">></span> <span class="self">Self</span> { + <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>, @@ -517,14 +732,14 @@ <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="op">-</span><span class="op">></span> <span class="kw-2">&</span><span class="ident">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 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">&</span><span class="kw-2">mut</span> <span class="self">self</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>()); } @@ -535,28 +750,28 @@ <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">&</span><span class="kw-2">mut</span> <span class="self">self</span>) <span class="op">-</span><span class="op">></span> <span class="ident">ParseResult</span><span class="op"><</span><span class="ident">ExprAST</span><span class="op">></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="op">=</span><span class="op">></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="kw">_</span> <span class="op">=</span><span class="op">></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">&</span><span class="kw-2">mut</span> <span class="self">self</span>) <span class="op">-</span><span class="op">></span> <span class="ident">ParseResult</span><span class="op"><</span><span class="ident">ExprAST</span><span class="op">></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="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>.<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>) @@ -570,14 +785,14 @@ <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">&</span><span class="kw-2">mut</span> <span class="self">self</span>) <span class="op">-</span><span class="op">></span> <span class="ident">ParseResult</span><span class="op"><</span><span class="ident">ExprAST</span><span class="op">></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="op">=</span><span class="op">></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="kw">_</span> <span class="op">=</span><span class="op">></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>) { @@ -597,9 +812,7 @@ <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="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">// Eat ')' token.</span> - <span class="self">self</span>.<span class="ident">get_next_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="ident">Token::Char</span>(<span class="string">')'</span>) { <span class="kw">break</span>; } @@ -611,22 +824,119 @@ } } + <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="prelude-val">Ok</span>(<span class="ident">ExprAST::Call</span>(<span class="ident">id_name</span>, <span class="ident">args</span>)) } } + <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="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">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="comment">// Consume 'then' token.</span> + <span class="self">self</span>.<span class="ident">get_next_token</span>(); + + <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">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="comment">// Consume 'else' token.</span> + <span class="self">self</span>.<span class="ident">get_next_token</span>(); + + <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="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="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="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="self">self</span>.<span class="ident">get_next_token</span>(); + + <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="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="self">self</span>.<span class="ident">get_next_token</span>(); + + <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> <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="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="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="self">self</span>.<span class="ident">get_next_token</span>(); + + <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="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="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">&</span><span class="kw-2">mut</span> <span class="self">self</span>) <span class="op">-</span><span class="op">></span> <span class="ident">ParseResult</span><span class="op"><</span><span class="ident">ExprAST</span><span class="op">></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="op">=</span><span class="op">></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="op">=</span><span class="op">></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="op">=</span><span class="op">></span> <span class="self">self</span>.<span class="ident">parse_paren_expr</span>(), - <span class="kw">_</span> <span class="op">=</span><span class="op">></span> <span class="prelude-val">Err</span>(<span class="string">"unknown token when expecting an expression"</span>.<span class="ident">into</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>()), } } @@ -638,7 +948,7 @@ <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">&</span><span class="kw-2">mut</span> <span class="self">self</span>) <span class="op">-</span><span class="op">></span> <span class="ident">ParseResult</span><span class="op"><</span><span class="ident">ExprAST</span><span class="op">></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>) } @@ -647,7 +957,7 @@ <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">&</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="op">-</span><span class="op">></span> <span class="ident">ParseResult</span><span class="op"><</span><span class="ident">ExprAST</span><span class="op">></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>()); @@ -657,12 +967,12 @@ } <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="op">=</span><span class="op">></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">_</span> <span class="op">=</span><span class="op">></span> <span class="macro">unreachable!</span>(), + <span class="kw">_</span> => <span class="macro">unreachable!</span>(), }; <span class="comment">// lhs BINOP1 rhs BINOP2 remrhs</span> @@ -696,14 +1006,14 @@ <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">&</span><span class="kw-2">mut</span> <span class="self">self</span>) <span class="op">-</span><span class="op">></span> <span class="ident">ParseResult</span><span class="op"><</span><span class="ident">PrototypeAST</span><span class="op">></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="op">=</span><span class="op">></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="ident">other</span> <span class="op">=</span><span class="op">></span> { + <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>()); @@ -719,9 +1029,9 @@ <span class="self">self</span>.<span class="ident">get_next_token</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">arg</span>)) <span class="op">=</span><span class="op">></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="op">=</span><span class="op">></span> {} - <span class="ident">other</span> <span class="op">=</span><span class="op">></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">break</span>; } @@ -741,7 +1051,7 @@ <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">&</span><span class="kw-2">mut</span> <span class="self">self</span>) <span class="op">-</span><span class="op">></span> <span class="ident">ParseResult</span><span class="op"><</span><span class="ident">FunctionAST</span><span class="op">></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>(); @@ -755,7 +1065,7 @@ <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">&</span><span class="kw-2">mut</span> <span class="self">self</span>) <span class="op">-</span><span class="op">></span> <span class="ident">ParseResult</span><span class="op"><</span><span class="ident">PrototypeAST</span><span class="op">></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>(); @@ -766,7 +1076,7 @@ <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">&</span><span class="kw-2">mut</span> <span class="self">self</span>) <span class="op">-</span><span class="op">></span> <span class="ident">ParseResult</span><span class="op"><</span><span class="ident">FunctionAST</span><span class="op">></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>)) @@ -776,22 +1086,22 @@ <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="op">-</span><span class="op">></span> <span class="ident">isize</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="op">=</span><span class="op">></span> <span class="number">10</span>, - <span class="ident">Token::Char</span>(<span class="string">'+'</span>) <span class="op">=</span><span class="op">></span> <span class="number">20</span>, - <span class="ident">Token::Char</span>(<span class="string">'-'</span>) <span class="op">=</span><span class="op">></span> <span class="number">20</span>, - <span class="ident">Token::Char</span>(<span class="string">'*'</span>) <span class="op">=</span><span class="op">></span> <span class="number">40</span>, - <span class="kw">_</span> <span class="op">=</span><span class="op">></span> <span class="op">-</span><span class="number">1</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="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="kw">crate</span><span class="ident">::lexer::Lexer</span>; + <span class="kw">use</span> <span class="ident"><span class="kw">crate</span>::lexer::Lexer</span>; - <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="op">-</span><span class="op">></span> <span class="ident">Parser</span><span class="op"><</span><span class="ident">std::str::Chars</span><span class="op">></span> { + <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>); @@ -819,8 +1129,71 @@ } <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="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="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="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="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="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="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="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="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> + }) + ); + } + + <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="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="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> + }) + ); + } + + <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)"</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="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>))); @@ -830,6 +1203,26 @@ <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>)])) ); + + <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>())), + }) + ); + + <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>)), + }) + ); } <span class="attribute">#[<span class="ident">test</span>]</span> @@ -919,7 +1312,6 @@ <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>)); } } -</pre></div> -</section><section id="search" class="content hidden"></section><div id="rustdoc-vars" data-root-path="../../" data-current-crate="llvm_kaleidoscope_rs" data-search-index-js="../../search-index.js" data-search-js="../../search.js"></div> - <script src="../../main.js"></script><script src="../../source-script.js"></script><script src="../../source-files.js"></script> +</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 |