[fine] Re-work compiler main, checking is everything
This commit is contained in:
parent
5c05077312
commit
d8db65af55
3 changed files with 67 additions and 30 deletions
|
|
@ -1,4 +1,32 @@
|
||||||
|
use std::fs;
|
||||||
|
|
||||||
|
use parser::parse;
|
||||||
|
use semantics::{check, Semantics};
|
||||||
|
|
||||||
pub mod compiler;
|
pub mod compiler;
|
||||||
pub mod parser;
|
pub mod parser;
|
||||||
pub mod semantics;
|
pub mod semantics;
|
||||||
pub mod tokens;
|
pub mod tokens;
|
||||||
|
pub mod vm;
|
||||||
|
|
||||||
|
pub fn process_file(file: &str) {
|
||||||
|
let source = match fs::read_to_string(file) {
|
||||||
|
Ok(c) => c,
|
||||||
|
Err(e) => {
|
||||||
|
eprintln!("Unable to read file {file}: {e}");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
// What am I doing here?
|
||||||
|
let (tree, lines) = parse(&source);
|
||||||
|
let semantics = Semantics::new(&tree, &lines);
|
||||||
|
check(&semantics);
|
||||||
|
|
||||||
|
// OK now there might be errors.
|
||||||
|
let mut errors = semantics.snapshot_errors();
|
||||||
|
errors.reverse();
|
||||||
|
for e in errors {
|
||||||
|
eprintln!("{file}: {}:{}: {}", e.start.0, e.start.1, e.message);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,37 +1,8 @@
|
||||||
use fine::parser::parse;
|
|
||||||
use fine::semantics::Semantics;
|
|
||||||
use std::env;
|
use std::env;
|
||||||
use std::fs;
|
|
||||||
|
|
||||||
pub fn process_file(file: &str) {
|
|
||||||
let source = match fs::read_to_string(file) {
|
|
||||||
Ok(c) => c,
|
|
||||||
Err(e) => {
|
|
||||||
eprintln!("Unable to read file {file}: {e}");
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
// What am I doing here?
|
|
||||||
let (tree, lines) = parse(&source);
|
|
||||||
let semantics = Semantics::new(&tree, &lines);
|
|
||||||
|
|
||||||
// This is... probably wrong, I don't know, what am I doing?
|
|
||||||
for t in tree.trees() {
|
|
||||||
let _ = semantics.type_of(t);
|
|
||||||
}
|
|
||||||
|
|
||||||
// OK now there might be errors.
|
|
||||||
let mut errors = semantics.snapshot_errors();
|
|
||||||
errors.reverse();
|
|
||||||
for e in errors {
|
|
||||||
eprintln!("{file}: {}:{}: {}", e.start.0, e.start.1, e.message);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn main() {
|
pub fn main() {
|
||||||
let args: Vec<String> = env::args().collect();
|
let args: Vec<String> = env::args().collect();
|
||||||
for arg in &args[1..] {
|
for arg in &args[1..] {
|
||||||
process_file(arg);
|
fine::process_file(arg);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -841,6 +841,44 @@ impl<'a> Semantics<'a> {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn check(s: &Semantics) {
|
||||||
|
for t in s.syntax_tree.trees() {
|
||||||
|
let tree = &s.syntax_tree[t];
|
||||||
|
match tree.kind {
|
||||||
|
TreeKind::Error => {} // already reported
|
||||||
|
TreeKind::File => {}
|
||||||
|
TreeKind::FunctionDecl => {
|
||||||
|
let _ = s.environment_of(t);
|
||||||
|
}
|
||||||
|
TreeKind::ParamList => {}
|
||||||
|
TreeKind::Parameter => {}
|
||||||
|
TreeKind::TypeExpression => {
|
||||||
|
let _ = s.type_of_type_expr(tree);
|
||||||
|
}
|
||||||
|
TreeKind::Block => {
|
||||||
|
let _ = s.type_of_block(tree);
|
||||||
|
}
|
||||||
|
TreeKind::LetStatement => {
|
||||||
|
let _ = s.environment_of(t);
|
||||||
|
}
|
||||||
|
TreeKind::ReturnStatement => {}
|
||||||
|
TreeKind::ExpressionStatement
|
||||||
|
| TreeKind::LiteralExpression
|
||||||
|
| TreeKind::GroupingExpression
|
||||||
|
| TreeKind::UnaryExpression
|
||||||
|
| TreeKind::ConditionalExpression
|
||||||
|
| TreeKind::CallExpression
|
||||||
|
| TreeKind::BinaryExpression => {
|
||||||
|
let _ = s.type_of(t);
|
||||||
|
}
|
||||||
|
TreeKind::ArgumentList => {}
|
||||||
|
TreeKind::Argument => {}
|
||||||
|
TreeKind::IfStatement => {}
|
||||||
|
TreeKind::Identifier => {}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod tests {
|
mod tests {
|
||||||
use super::*;
|
use super::*;
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue