From 1835b2be990f51f4111c847aa8ad3c8477191eba Mon Sep 17 00:00:00 2001 From: Vincent Ambo Date: Sun, 6 Dec 2020 15:49:44 +0100 Subject: feat(tazjin/rlox): Wire up parser to the REPL Change-Id: I940448c63ce105d53a0f281b6320ffb01378f207 Reviewed-on: https://cl.tvl.fyi/c/depot/+/2235 Reviewed-by: tazjin Tested-by: BuildkiteCI --- users/tazjin/rlox/src/interpreter.rs | 12 ++++++++++-- users/tazjin/rlox/src/parser.rs | 16 +++++++++++----- 2 files changed, 21 insertions(+), 7 deletions(-) (limited to 'users/tazjin/rlox') diff --git a/users/tazjin/rlox/src/interpreter.rs b/users/tazjin/rlox/src/interpreter.rs index 77f2492a46a2..8a4d5cfef0df 100644 --- a/users/tazjin/rlox/src/interpreter.rs +++ b/users/tazjin/rlox/src/interpreter.rs @@ -1,4 +1,5 @@ use crate::errors::{report, Error}; +use crate::parser; use crate::scanner::{self, Token}; // Run some Lox code and print it to stdout @@ -6,12 +7,19 @@ pub fn run(code: &str) { let chars: Vec = code.chars().collect(); match scanner::scan(&chars) { - Ok(tokens) => print_tokens(tokens), + Ok(tokens) => { + print_tokens(&tokens); + match parser::parse(tokens) { + Ok(expr) => println!("Expression:\n{:?}", expr), + Err(error) => report_errors(vec![error]), + } + } Err(errors) => report_errors(errors), } } -fn print_tokens<'a>(tokens: Vec>) { +fn print_tokens<'a>(tokens: &Vec>) { + println!("Tokens:"); for token in tokens { println!("{:?}", token); } diff --git a/users/tazjin/rlox/src/parser.rs b/users/tazjin/rlox/src/parser.rs index e7266c2352b4..cf96981e5b41 100644 --- a/users/tazjin/rlox/src/parser.rs +++ b/users/tazjin/rlox/src/parser.rs @@ -11,17 +11,17 @@ use crate::scanner::{Token, TokenKind}; // AST #[derive(Debug)] -struct Binary<'a> { +pub struct Binary<'a> { left: Box>, operator: Token<'a>, right: Box>, } #[derive(Debug)] -struct Grouping<'a>(Box>); +pub struct Grouping<'a>(Box>); #[derive(Debug)] -enum Literal { +pub enum Literal { Boolean(bool), Number(f64), String(String), @@ -29,13 +29,13 @@ enum Literal { } #[derive(Debug)] -struct Unary<'a> { +pub struct Unary<'a> { operator: Token<'a>, right: Box>, } #[derive(Debug)] -enum Expr<'a> { +pub enum Expr<'a> { Binary(Binary<'a>), Grouping(Grouping<'a>), Literal(Literal), @@ -200,3 +200,9 @@ impl<'a> Parser<'a> { return Ok(expr); } } + +pub fn parse<'a>(tokens: Vec>) -> ExprResult<'a> { + let mut parser = Parser { tokens, current: 0 }; + + parser.expression() +} -- cgit 1.4.1