Report sytax errors
This commit is contained in:
parent
236a9c296b
commit
df772ec737
@ -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();
|
||||||
|
@ -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,
|
||||||
|
Loading…
Reference in New Issue
Block a user