split compiler into backend and fronted
This commit is contained in:
@@ -0,0 +1,55 @@
|
||||
use crate::common::spans::Span;
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
pub enum DiagnosticLevel {
|
||||
Error,
|
||||
Warning,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct Diagnostic {
|
||||
pub level: DiagnosticLevel,
|
||||
pub message: String,
|
||||
pub span: Option<Span>,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct DiagnosticBundle {
|
||||
pub diagnostics: Vec<Diagnostic>,
|
||||
}
|
||||
|
||||
impl DiagnosticBundle {
|
||||
pub fn new() -> Self {
|
||||
Self {
|
||||
diagnostics: Vec::new(),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn push(&mut self, diagnostic: Diagnostic) {
|
||||
self.diagnostics.push(diagnostic);
|
||||
}
|
||||
|
||||
pub fn error(message: String, span: Option<Span>) -> Self {
|
||||
let mut bundle = Self::new();
|
||||
bundle.push(Diagnostic {
|
||||
level: DiagnosticLevel::Error,
|
||||
message,
|
||||
span,
|
||||
});
|
||||
bundle
|
||||
}
|
||||
|
||||
pub fn has_errors(&self) -> bool {
|
||||
self.diagnostics
|
||||
.iter()
|
||||
.any(|d| matches!(d.level, DiagnosticLevel::Error))
|
||||
}
|
||||
}
|
||||
|
||||
impl From<Diagnostic> for DiagnosticBundle {
|
||||
fn from(diagnostic: Diagnostic) -> Self {
|
||||
let mut bundle = Self::new();
|
||||
bundle.push(diagnostic);
|
||||
bundle
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,59 @@
|
||||
use std::path::PathBuf;
|
||||
use std::sync::Arc;
|
||||
|
||||
pub struct SourceFile {
|
||||
pub id: usize,
|
||||
pub path: PathBuf,
|
||||
pub source: Arc<str>,
|
||||
}
|
||||
|
||||
pub struct FileManager {
|
||||
files: Vec<SourceFile>,
|
||||
}
|
||||
|
||||
impl FileManager {
|
||||
pub fn new() -> Self {
|
||||
Self { files: Vec::new() }
|
||||
}
|
||||
|
||||
pub fn add(&mut self, path: PathBuf, source: String) -> usize {
|
||||
let id = self.files.len();
|
||||
self.files.push(SourceFile {
|
||||
id,
|
||||
path,
|
||||
source: Arc::from(source),
|
||||
});
|
||||
id
|
||||
}
|
||||
|
||||
pub fn get_file(&self, id: usize) -> Option<&SourceFile> {
|
||||
self.files.get(id)
|
||||
}
|
||||
|
||||
pub fn get_path(&self, id: usize) -> Option<PathBuf> {
|
||||
self.files.get(id).map(|f| f.path.clone())
|
||||
}
|
||||
|
||||
pub fn lookup_pos(&self, file_id: usize, pos: u32) -> (usize, usize) {
|
||||
let file = if let Some(f) = self.files.get(file_id) {
|
||||
f
|
||||
} else {
|
||||
return (0, 0);
|
||||
};
|
||||
|
||||
let mut line = 1;
|
||||
let mut col = 1;
|
||||
for (i, c) in file.source.char_indices() {
|
||||
if i as u32 == pos {
|
||||
break;
|
||||
}
|
||||
if c == '\n' {
|
||||
line += 1;
|
||||
col = 1;
|
||||
} else {
|
||||
col += 1;
|
||||
}
|
||||
}
|
||||
(line, col)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,5 @@
|
||||
pub mod diagnostics;
|
||||
pub mod spans;
|
||||
pub mod files;
|
||||
pub mod symbols;
|
||||
pub mod sourcemap;
|
||||
@@ -0,0 +1 @@
|
||||
// PC <-> Span mapping logic
|
||||
@@ -0,0 +1,16 @@
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
|
||||
pub struct Span {
|
||||
pub file_id: usize,
|
||||
pub start: u32,
|
||||
pub end: u32,
|
||||
}
|
||||
|
||||
impl Span {
|
||||
pub fn new(file_id: usize, start: u32, end: u32) -> Self {
|
||||
Self {
|
||||
file_id,
|
||||
start,
|
||||
end,
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
use serde::Serialize;
|
||||
|
||||
#[derive(Serialize, Debug, Clone)]
|
||||
pub struct Symbol {
|
||||
pub pc: u32,
|
||||
pub file: String,
|
||||
pub line: usize,
|
||||
pub col: usize,
|
||||
}
|
||||
Reference in New Issue
Block a user