Add variable resolution; the error behavior is pretty bad here.
This commit is contained in:
64
src/passes.rs
Normal file
64
src/passes.rs
Normal file
@@ -0,0 +1,64 @@
|
||||
use codespan_reporting::files::SimpleFiles;
|
||||
use crate::asts::hil;
|
||||
use crate::errors::Error;
|
||||
use crate::syntax;
|
||||
use crate::syntax::Location;
|
||||
use crate::variable_map::VariableMap;
|
||||
use crate::warnings::Warning;
|
||||
use std::fs;
|
||||
|
||||
mod syntax_to_hil;
|
||||
|
||||
pub struct PassResult<T> {
|
||||
pub result: T,
|
||||
pub warnings: Vec<Warning>,
|
||||
pub errors: Vec<Error>,
|
||||
}
|
||||
|
||||
impl<T,E> From<E> for PassResult<Option<T>>
|
||||
where
|
||||
Error: From<E>
|
||||
{
|
||||
fn from(x: E) -> Self {
|
||||
PassResult {
|
||||
result: None,
|
||||
warnings: vec![],
|
||||
errors: vec![Error::from(x)],
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub fn run_front_end(
|
||||
file_database: &mut SimpleFiles<String, String>,
|
||||
initial_file_name: &str,
|
||||
) -> PassResult<Option<(hil::Program<Location>, VariableMap)>> {
|
||||
let initial_file_contents = match fs::read_to_string(initial_file_name) {
|
||||
Ok(x) => x,
|
||||
Err(e) => return PassResult::from(e),
|
||||
};
|
||||
|
||||
let initial_file = file_database.add(initial_file_name.to_string(), initial_file_contents);
|
||||
let db_version = match file_database.get(initial_file) {
|
||||
Ok(x) => x,
|
||||
Err(e) => return PassResult::from(e),
|
||||
};
|
||||
let db_version_source = db_version.source();
|
||||
let raw_syntax = match syntax::Program::parse(initial_file, db_version_source) {
|
||||
Ok(x) => x,
|
||||
Err(e) => return PassResult::from(e),
|
||||
};
|
||||
|
||||
let mut variable_map = VariableMap::empty();
|
||||
let conversion_result = hil::Program::convert(raw_syntax, &mut variable_map);
|
||||
let result = if conversion_result.errors.is_empty() {
|
||||
Some((conversion_result.result, variable_map))
|
||||
} else {
|
||||
None
|
||||
};
|
||||
|
||||
PassResult {
|
||||
result,
|
||||
warnings: conversion_result.warnings,
|
||||
errors: conversion_result.errors,
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user