From df772ec737403287a17228304e16fdc545af72cb Mon Sep 17 00:00:00 2001 From: Araozu Date: Wed, 8 Feb 2023 19:53:02 -0500 Subject: [PATCH] Report sytax errors --- src/repl/mod.rs | 23 ++++++++++++++++++----- src/token.rs | 1 - 2 files changed, 18 insertions(+), 6 deletions(-) diff --git a/src/repl/mod.rs b/src/repl/mod.rs index 87cf7a5..fa79cfb 100755 --- a/src/repl/mod.rs +++ b/src/repl/mod.rs @@ -1,6 +1,7 @@ use std::io::{self, Write}; use crate::symbol_table::SymbolTable; +use crate::token::Token; use super::lexic; use super::syntax; @@ -12,11 +13,7 @@ fn compile(input: &String) { match _tokens { Ok(tokens) => { - let mut ast = syntax::construct_ast(&tokens).unwrap(); - let mut table = SymbolTable::new(); - semantic::check_ast(&mut ast, &mut table); - let js_code = codegen::codegen(&ast); - println!("{}", js_code) + build_ast(tokens); }, Err(error) => { eprintln!("Error scanning.\n{} at pos {}", error.reason, error.position) @@ -25,6 +22,22 @@ fn compile(input: &String) { } +fn build_ast(tokens: Vec) { + let ast = syntax::construct_ast(&tokens); + + match ast { + Ok(mut ast) => { + let mut table = SymbolTable::new(); + semantic::check_ast(&mut ast, &mut table); + let js_code = codegen::codegen(&ast); + println!("{}", js_code) + } + Err(reason) => { + eprintln!("Syntax error.\n{}", reason) + } + } +} + pub fn run() -> io::Result<()> { let stdin = io::stdin(); let mut buffer = String::new(); diff --git a/src/token.rs b/src/token.rs index c0a7468..b7772ba 100755 --- a/src/token.rs +++ b/src/token.rs @@ -1,7 +1,6 @@ #[derive(PartialEq, Debug, Clone)] pub enum TokenType { Identifier, - Comment, Number, String, Operator,