Report sytax errors

master
Araozu 2023-02-08 19:53:02 -05:00
parent 236a9c296b
commit df772ec737
2 changed files with 18 additions and 6 deletions

View File

@ -1,6 +1,7 @@
use std::io::{self, Write}; use std::io::{self, Write};
use crate::symbol_table::SymbolTable; use crate::symbol_table::SymbolTable;
use crate::token::Token;
use super::lexic; use super::lexic;
use super::syntax; use super::syntax;
@ -12,11 +13,7 @@ fn compile(input: &String) {
match _tokens { match _tokens {
Ok(tokens) => { Ok(tokens) => {
let mut ast = syntax::construct_ast(&tokens).unwrap(); build_ast(tokens);
let mut table = SymbolTable::new();
semantic::check_ast(&mut ast, &mut table);
let js_code = codegen::codegen(&ast);
println!("{}", js_code)
}, },
Err(error) => { Err(error) => {
eprintln!("Error scanning.\n{} at pos {}", error.reason, error.position) eprintln!("Error scanning.\n{} at pos {}", error.reason, error.position)
@ -25,6 +22,22 @@ fn compile(input: &String) {
} }
fn build_ast(tokens: Vec<Token>) {
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<()> { pub fn run() -> io::Result<()> {
let stdin = io::stdin(); let stdin = io::stdin();
let mut buffer = String::new(); let mut buffer = String::new();

View File

@ -1,7 +1,6 @@
#[derive(PartialEq, Debug, Clone)] #[derive(PartialEq, Debug, Clone)]
pub enum TokenType { pub enum TokenType {
Identifier, Identifier,
Comment,
Number, Number,
String, String,
Operator, Operator,