feat(tvixbolt): add toggle for displaying pretty-printed AST
This uses the JSON serialisation of the AST introduced earlier to display a text box with the serialised AST to users. Useful for debugging. Change-Id: Ibc400eaf5ca87fa5072d5c044942505331c3bb40 Reviewed-on: https://cl.tvl.fyi/c/depot/+/7005 Autosubmit: tazjin <tazjin@tvl.su> Tested-by: BuildkiteCI Reviewed-by: Adam Joseph <adam@westernsemico.com> Reviewed-by: tazjin <tazjin@tvl.su>
This commit is contained in:
		
							parent
							
								
									0abc66ad91
								
							
						
					
					
						commit
						e6d9be32a2
					
				
					 1 changed files with 26 additions and 2 deletions
				
			
		| 
						 | 
					@ -15,12 +15,14 @@ use yew_router::{prelude::*, AnyRoute};
 | 
				
			||||||
enum Msg {
 | 
					enum Msg {
 | 
				
			||||||
    CodeChange(String),
 | 
					    CodeChange(String),
 | 
				
			||||||
    ToggleTrace(bool),
 | 
					    ToggleTrace(bool),
 | 
				
			||||||
 | 
					    ToggleDisplayAst(bool),
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
#[derive(Clone, Serialize, Deserialize)]
 | 
					#[derive(Clone, Serialize, Deserialize)]
 | 
				
			||||||
struct Model {
 | 
					struct Model {
 | 
				
			||||||
    code: String,
 | 
					    code: String,
 | 
				
			||||||
    trace: bool,
 | 
					    trace: bool,
 | 
				
			||||||
 | 
					    display_ast: bool,
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
fn tvixbolt_overview() -> Html {
 | 
					fn tvixbolt_overview() -> Html {
 | 
				
			||||||
| 
						 | 
					@ -99,6 +101,7 @@ impl Component for Model {
 | 
				
			||||||
            .unwrap_or_else(|_| Self {
 | 
					            .unwrap_or_else(|_| Self {
 | 
				
			||||||
                code: String::new(),
 | 
					                code: String::new(),
 | 
				
			||||||
                trace: false,
 | 
					                trace: false,
 | 
				
			||||||
 | 
					                display_ast: false,
 | 
				
			||||||
            })
 | 
					            })
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
| 
						 | 
					@ -108,6 +111,10 @@ impl Component for Model {
 | 
				
			||||||
                self.trace = trace;
 | 
					                self.trace = trace;
 | 
				
			||||||
            }
 | 
					            }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					            Msg::ToggleDisplayAst(display_ast) => {
 | 
				
			||||||
 | 
					                self.display_ast = display_ast;
 | 
				
			||||||
 | 
					            }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
            Msg::CodeChange(new_code) => {
 | 
					            Msg::CodeChange(new_code) => {
 | 
				
			||||||
                self.code = new_code;
 | 
					                self.code = new_code;
 | 
				
			||||||
            }
 | 
					            }
 | 
				
			||||||
| 
						 | 
					@ -152,6 +159,17 @@ impl Component for Model {
 | 
				
			||||||
                       })}
 | 
					                       })}
 | 
				
			||||||
                       />
 | 
					                       />
 | 
				
			||||||
                    </div>
 | 
					                    </div>
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					                    <div class="form-group">
 | 
				
			||||||
 | 
					                      <label for="display-ast">{"Display parsed AST:"}</label>
 | 
				
			||||||
 | 
					                      <input
 | 
				
			||||||
 | 
					                       id="display-ast" type="checkbox" checked={self.display_ast}
 | 
				
			||||||
 | 
					                       onchange={link.callback(|e: Event| {
 | 
				
			||||||
 | 
					                           let trace = e.target_unchecked_into::<HtmlInputElement>().checked();
 | 
				
			||||||
 | 
					                           Msg::ToggleDisplayAst(trace)
 | 
				
			||||||
 | 
					                       })}
 | 
				
			||||||
 | 
					                       />
 | 
				
			||||||
 | 
					                    </div>
 | 
				
			||||||
                  </fieldset>
 | 
					                  </fieldset>
 | 
				
			||||||
                </form>
 | 
					                </form>
 | 
				
			||||||
                <hr />
 | 
					                <hr />
 | 
				
			||||||
| 
						 | 
					@ -179,7 +197,7 @@ impl Model {
 | 
				
			||||||
        html! {
 | 
					        html! {
 | 
				
			||||||
            <>
 | 
					            <>
 | 
				
			||||||
              <h2>{"Result:"}</h2>
 | 
					              <h2>{"Result:"}</h2>
 | 
				
			||||||
              {eval(self.trace, &self.code).display()}
 | 
					            {eval(self.trace, self.display_ast, &self.code).display()}
 | 
				
			||||||
            </>
 | 
					            </>
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
| 
						 | 
					@ -194,6 +212,7 @@ struct Output {
 | 
				
			||||||
    output: String,
 | 
					    output: String,
 | 
				
			||||||
    bytecode: Vec<u8>,
 | 
					    bytecode: Vec<u8>,
 | 
				
			||||||
    trace: Vec<u8>,
 | 
					    trace: Vec<u8>,
 | 
				
			||||||
 | 
					    ast: String,
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
fn maybe_show(title: &str, s: &str) -> Html {
 | 
					fn maybe_show(title: &str, s: &str) -> Html {
 | 
				
			||||||
| 
						 | 
					@ -220,12 +239,13 @@ impl Output {
 | 
				
			||||||
            {maybe_show("Bytecode:", &String::from_utf8_lossy(&self.bytecode))}
 | 
					            {maybe_show("Bytecode:", &String::from_utf8_lossy(&self.bytecode))}
 | 
				
			||||||
            {maybe_show("Runtime errors:", &self.runtime_errors)}
 | 
					            {maybe_show("Runtime errors:", &self.runtime_errors)}
 | 
				
			||||||
            {maybe_show("Runtime trace:", &String::from_utf8_lossy(&self.trace))}
 | 
					            {maybe_show("Runtime trace:", &String::from_utf8_lossy(&self.trace))}
 | 
				
			||||||
 | 
					            {maybe_show("Parsed AST:", &self.ast)}
 | 
				
			||||||
            </>
 | 
					            </>
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
fn eval(trace: bool, code: &str) -> Output {
 | 
					fn eval(trace: bool, display_ast: bool, code: &str) -> Output {
 | 
				
			||||||
    let mut out = Output::default();
 | 
					    let mut out = Output::default();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    if code.is_empty() {
 | 
					    if code.is_empty() {
 | 
				
			||||||
| 
						 | 
					@ -249,6 +269,10 @@ fn eval(trace: bool, code: &str) -> Output {
 | 
				
			||||||
        .expr()
 | 
					        .expr()
 | 
				
			||||||
        .expect("expression should exist if no errors occured");
 | 
					        .expect("expression should exist if no errors occured");
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    if display_ast {
 | 
				
			||||||
 | 
					        out.ast = tvix_eval::pretty_print_expr(&root_expr);
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    let source = SourceCode::new();
 | 
					    let source = SourceCode::new();
 | 
				
			||||||
    let file = source.add_file("nixbolt".to_string(), code.into());
 | 
					    let file = source.add_file("nixbolt".to_string(), code.into());
 | 
				
			||||||
 | 
					
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue