START OF COMPILER

This commit is contained in:
2024-10-11 17:31:03 -04:00
parent bb3a0ad113
commit de79445ede
39 changed files with 710 additions and 94 deletions

120
src/parser/v2/body.rs Normal file
View File

@@ -0,0 +1,120 @@
use std::collections::HashSet;
use std::fmt::{Debug, Write};
use std::sync::LazyLock;
use crate::util::Padder;
use super::util::WHITESPACE_SET;
use super::CharCursor;
use super::Expr;
use super::ParserError;
static NAME_END: LazyLock<HashSet<char>> = LazyLock::new(|| {
let mut set = WHITESPACE_SET.clone();
set.extend(&['(']);
set
});
pub struct Body {
statements: Vec<Statement>,
}
pub enum Statement {
Let(String, Expr),
Return(Expr),
Expr(Expr),
}
impl Body {
pub fn parse(cursor: &mut CharCursor) -> Result<Self, ParserError> {
cursor.skip_whitespace();
let mut statements = Vec::new();
cursor.expect_char('{')?;
loop {
cursor.skip_whitespace();
let next = cursor.expect_peek()?;
if next == '}' {
cursor.next();
return Ok(Self { statements });
}
statements.push(Statement::parse(cursor)?);
}
}
}
impl Statement {
pub fn parse(cursor: &mut CharCursor) -> Result<Self, ParserError> {
cursor.skip_whitespace();
Ok(if cursor.advance_if_str("let", &WHITESPACE_SET) {
cursor.skip_whitespace();
let name = cursor.until(&NAME_END);
if name.is_empty() {
return Err(ParserError::at(
cursor.pos(),
"Expected variable name".to_string(),
));
}
cursor.skip_whitespace();
cursor.expect_char('=')?;
let expr = Expr::parse(cursor)?;
cursor.skip_whitespace();
cursor.expect_char(';')?;
Self::Let(name, expr)
} else if cursor.advance_if_str("return", &WHITESPACE_SET) {
let expr = Expr::parse(cursor)?;
cursor.skip_whitespace();
cursor.expect_char(';')?;
Self::Return(expr)
} else {
let expr = Expr::parse(cursor)?;
match cursor.expect_peek()? {
';' => {
cursor.next();
Self::Expr(expr)
}
'}' => Self::Return(expr),
_ => {
cursor.next();
return Err(ParserError::at(
cursor.prev_pos(),
"unexpected end of statement; expected a ';' or '}'".to_string(),
));
}
}
})
}
}
impl Debug for Statement {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
Statement::Let(n, e) => {
write!(f, "let {n} = {e:?};")?;
}
Statement::Return(e) => {
write!(f, "return {e:?};")?;
}
Statement::Expr(e) => {
write!(f, "{e:?};")?;
}
}
Ok(())
}
}
impl Debug for Body {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
if self.statements.first().is_some() {
write!(f, "{{\n ")?;
let mut padder = Padder::new(f);
for s in &self.statements {
// they don't expose wrap_buf :grief:
writeln!(padder, "{s:?}")?;
}
write!(f, "}}")?;
} else {
write!(f, "{{}}")?;
}
Ok(())
}
}

135
src/parser/v2/cursor.rs Normal file
View File

@@ -0,0 +1,135 @@
use std::{collections::HashSet, iter::Peekable, str::Chars};
use super::{error::ParserError, util::WHITESPACE_SET};
#[derive(Debug, Clone, Copy)]
pub struct FilePos {
pub line: usize,
pub col: usize,
}
#[derive(Debug, Clone, Copy)]
pub struct FileRegion {
pub start: FilePos,
pub end: FilePos,
}
pub struct CharCursor<'a> {
chars: Peekable<Chars<'a>>,
pos: FilePos,
prev_pos: FilePos,
}
impl CharCursor<'_> {
pub fn until(&mut self, set: &HashSet<char>) -> String {
let mut str = String::new();
loop {
let Some(next) = self.peek() else {
return str;
};
if set.contains(&next) {
return str;
}
str.push(next);
self.advance();
}
}
pub fn skip_whitespace(&mut self) {
while self.peek().is_some_and(|c| c.is_whitespace()) {
self.advance();
}
let mut copy = self.chars.clone();
if let Some('/') = copy.next() {
if let Some('/') = copy.next() {
self.advance();
self.advance();
while self.next() != Some('\n') {}
self.skip_whitespace();
}
}
}
pub fn next(&mut self) -> Option<char> {
let res = self.peek()?;
self.advance();
Some(res)
}
pub fn peek(&mut self) -> Option<char> {
self.chars.peek().copied()
}
pub fn advance(&mut self) {
self.prev_pos = self.pos;
if self.peek().is_some_and(|c| c == '\n') {
self.pos.col = 0;
self.pos.line += 1;
} else {
self.pos.col += 1;
}
self.chars.next();
}
pub fn advance_if(&mut self, c: char) -> bool {
if let Some(c2) = self.peek() {
if c2 == c {
self.advance();
return true;
}
}
false
}
pub fn advance_if_str(&mut self, exp: &str, end: &HashSet<char>) -> bool {
let mut new = self.chars.clone();
for e in exp.chars() {
let Some(c) = new.next() else {
return false;
};
if e != c {
return false;
}
}
if new.peek().is_some_and(|c| !end.contains(c)) {
return false;
}
for _ in 0..exp.len() {
self.advance();
}
true
}
pub fn expect_char(&mut self, c: char) -> Result<(), ParserError> {
let next = self.expect_next()?;
if next == c {
Ok(())
} else {
Err(ParserError::at(
self.prev_pos,
format!("unexpected char '{next}'; expected '{c}'"),
))
}
}
pub fn expect_next(&mut self) -> Result<char, ParserError> {
self.next().ok_or(ParserError::unexpected_end())
}
pub fn expect_peek(&mut self) -> Result<char, ParserError> {
self.peek().ok_or(ParserError::unexpected_end())
}
pub fn pos(&self) -> FilePos {
self.pos
}
pub fn prev_pos(&self) -> FilePos {
self.prev_pos
}
}
impl<'a> From<&'a str> for CharCursor<'a> {
fn from(value: &'a str) -> Self {
Self {
chars: value.chars().peekable(),
pos: FilePos::start(),
prev_pos: FilePos::start(),
}
}
}
impl FilePos {
pub fn start() -> Self {
Self { line: 0, col: 0 }
}
}

60
src/parser/v2/error.rs Normal file
View File

@@ -0,0 +1,60 @@
use super::{FilePos, FileRegion};
#[derive(Debug)]
pub struct ParserError {
pub msg: String,
pub regions: Vec<FileRegion>,
}
impl ParserError {
pub fn from_msg(msg: String) -> Self {
Self {
msg,
regions: Vec::new(),
}
}
pub fn at(pos: FilePos, msg: String) -> Self {
Self {
msg,
regions: vec![FileRegion {
start: pos,
end: pos,
}],
}
}
pub fn unexpected_end() -> Self {
Self::from_msg("Unexpected end of input".to_string())
}
}
const BEFORE: usize = 1;
const AFTER: usize = 1;
pub fn print_error(err: ParserError, file: &str) {
let after = if err.regions.is_empty() {""} else {":"};
println!("error: {}{}", err.msg, after);
for reg in err.regions {
print_region(file, reg);
}
}
pub fn print_region(file: &str, reg: FileRegion) {
let start = reg.start.line.saturating_sub(BEFORE);
let num_before = reg.start.line - start;
let mut lines = file.lines().skip(start);
let len = reg.end.col - reg.start.col + 1;
let width = format!("{}", reg.end.line + AFTER).len();
for i in 0..num_before + 1 {
println!("{:>width$} | {}", start + i, lines.next().unwrap());
}
println!(
"{} | {}",
" ".repeat(width),
" ".repeat(reg.start.col) + &"^".repeat(len)
);
for i in 0..AFTER {
if let Some(next) = lines.next() {
println!("{:>width$} | {}", reg.end.line + i + 1, next);
}
}
}

247
src/parser/v2/expr.rs Normal file
View File

@@ -0,0 +1,247 @@
use super::{util::WHITESPACE_SET, Body, CharCursor, ParserError};
use std::{collections::HashSet, fmt::Debug, sync::LazyLock};
static SYMBOLS: LazyLock<HashSet<char>> = LazyLock::new(|| {
let mut set = HashSet::new();
for o in Operator::ALL {
for c in o.str().chars() {
set.insert(c);
}
}
set
});
static IDENT_END: LazyLock<HashSet<char>> = LazyLock::new(|| {
let mut set = WHITESPACE_SET.clone();
let symbols = &SYMBOLS;
set.extend(symbols.iter().chain(&[';', '(', ')']));
set
});
#[derive(Debug)]
pub enum Val {
String(String),
Number(String),
Unit,
}
pub enum Expr {
Block(Body),
Val(Val),
Ident(String),
BinaryOp(Operator, Box<Expr>, Box<Expr>),
Call(Box<Expr>, Vec<Expr>),
}
#[derive(Debug, PartialEq, Eq)]
pub enum Operator {
Add,
Sub,
Mul,
Div,
LessThan,
GreaterThan,
Offset,
}
impl Expr {
pub fn parse(cursor: &mut CharCursor) -> Result<Self, ParserError> {
cursor.skip_whitespace();
let Some(next) = cursor.peek() else {
return Ok(Self::Val(Val::Unit));
};
let mut e1 = match next {
'(' => {
cursor.advance();
let expr = Self::parse(cursor)?;
cursor.skip_whitespace();
cursor.expect_char(')')?;
expr
}
'{' => {
Self::Block(Body::parse(cursor)?)
}
_ => {
if let Some(val) = Val::parse_nonunit(cursor)? {
Self::Val(val)
} else {
let name = cursor.until(&IDENT_END);
Self::Ident(name)
}
}
};
cursor.skip_whitespace();
let Some(mut next) = cursor.peek() else {
return Ok(e1);
};
while next == '(' {
cursor.advance();
let inner = Self::parse(cursor)?;
cursor.skip_whitespace();
cursor.expect_char(')')?;
e1 = Self::Call(Box::new(e1), vec![inner]);
let Some(next2) = cursor.peek() else {
return Ok(e1);
};
next = next2
}
if let Some(op) = Operator::parse(cursor) {
let e2 = Self::parse(cursor)?;
return Ok(if let Self::BinaryOp(op_next, e2, e3) = e2 {
if op.presedence() > op_next.presedence() {
Self::BinaryOp(op_next, Box::new(Self::BinaryOp(op, Box::new(e1), e2)), e3)
} else {
Self::BinaryOp(op, Box::new(e1), Box::new(Self::BinaryOp(op_next, e2, e3)))
}
} else {
Self::BinaryOp(op, Box::new(e1), Box::new(e2))
});
};
Ok(e1)
}
}
impl Val {
pub fn parse_nonunit(cursor: &mut CharCursor) -> Result<Option<Self>, ParserError> {
let Some(next) = cursor.peek() else {
return Ok(None);
};
Ok(Some(match next {
'"' => {
cursor.advance();
let mut str = String::new();
loop {
let mut next = cursor.expect_next()?;
if next == '"' {
break;
}
if next == '\\' {
next = match cursor.expect_next()? {
'"' => '"',
c => {
return Err(ParserError::at(
cursor.pos(),
format!("unexpected escape char '{c}'"),
))
}
}
}
str.push(next);
}
Self::String(str)
}
'0' | '1' | '2' | '3' | '4' | '5' | '6' | '7' | '8' | '9' => {
let mut str = String::new();
loop {
let Some(next) = cursor.peek() else {
break;
};
match next {
'0' | '1' | '2' | '3' | '4' | '5' | '6' | '7' | '8' | '9' => {
str.push(next);
}
_ => break,
}
cursor.advance();
}
Self::Number(str)
}
_ => {
return Ok(None);
}
}))
}
}
impl Operator {
const ALL: [Self; 7] = [
Self::Add,
Self::Sub,
Self::Mul,
Self::Div,
Self::Offset,
Self::GreaterThan,
Self::LessThan,
];
pub fn presedence(&self) -> u32 {
match self {
Operator::LessThan => 0,
Operator::GreaterThan => 0,
Operator::Add => 1,
Operator::Sub => 2,
Operator::Mul => 3,
Operator::Div => 4,
Operator::Offset => 5,
}
}
pub fn str(&self) -> &str {
match self {
Self::Add => "+",
Self::Sub => "-",
Self::Mul => "*",
Self::Div => "/",
Self::LessThan => "<",
Self::GreaterThan => ">",
Self::Offset => ".",
}
}
pub fn parse(cursor: &mut CharCursor) -> Option<Self> {
let res = match cursor.peek()? {
'+' => Operator::Add,
'-' => Operator::Sub,
'*' => Operator::Mul,
'/' => Operator::Div,
'.' => Operator::Offset,
_ => return None,
};
for _ in 0..res.str().len() {
cursor.advance();
}
Some(res)
}
pub fn pad(&self) -> bool {
match self {
Operator::Add => true,
Operator::Sub => true,
Operator::Mul => true,
Operator::Div => true,
Operator::LessThan => true,
Operator::GreaterThan => true,
Operator::Offset => false,
}
}
}
impl Debug for Expr {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
Expr::Block(b) => write!(f, "{:?}", b)?,
Expr::Ident(n) => f.write_str(n)?,
Expr::BinaryOp(op, e1, e2) => {
write!(f, "({:?}", *e1)?;
if op.pad() {
write!(f, " {} ", op.str())?;
} else {
write!(f, "{}", op.str())?;
}
write!(f, "{:?})", *e2)?;
}
Expr::Call(n, args) => {
n.fmt(f)?;
write!(f, "(")?;
if let Some(a) = args.first() {
a.fmt(f)?;
}
for arg in args.iter().skip(1) {
write!(f, ", ")?;
arg.fmt(f)?;
}
write!(f, ")")?;
}
Expr::Val(v) => {
write!(f, "{:?}", v)?;
}
}
Ok(())
}
}

32
src/parser/v2/mod.rs Normal file
View File

@@ -0,0 +1,32 @@
use std::io::{BufRead, BufReader};
mod body;
mod cursor;
mod error;
mod expr;
mod module;
mod util;
pub use body::*;
pub use cursor::*;
pub use error::*;
pub use expr::*;
pub use module::*;
pub fn parse_file(file: &str) {
match Module::parse(&mut CharCursor::from(file)) {
Err(err) => print_error(err, file),
Ok(module) => println!("{module:#?}"),
}
}
pub fn run_stdin() {
for line in BufReader::new(std::io::stdin()).lines() {
let str = &line.expect("failed to read line");
let mut cursor = CharCursor::from(&str[..]);
match Statement::parse(&mut cursor) {
Ok(expr) => println!("{:?}", expr),
Err(err) => print_error(err, str),
}
}
}

59
src/parser/v2/module.rs Normal file
View File

@@ -0,0 +1,59 @@
use std::{collections::HashSet, fmt::Debug, sync::LazyLock};
use super::{util::WHITESPACE_SET, Body, CharCursor, ParserError};
#[derive(Debug)]
pub struct Module {
functions: Vec<Function>,
}
pub struct Function {
pub name: String,
pub body: Body,
}
static NAME_END: LazyLock<HashSet<char>> = LazyLock::new(|| {
let mut set = WHITESPACE_SET.clone();
set.extend(&['(']);
set
});
impl Module {
pub fn parse(cursor: &mut CharCursor) -> Result<Self, ParserError> {
let mut functions = Vec::new();
loop {
let next = cursor.until(&WHITESPACE_SET);
if next.is_empty() {
return Ok(Self { functions });
}
if next == "fn" {
functions.push(Function::parse(cursor)?);
} else {
return Err(ParserError::at(cursor.pos(), "expected fn".to_string()));
}
}
}
}
impl Function {
pub fn parse(cursor: &mut CharCursor) -> Result<Self, ParserError> {
cursor.skip_whitespace();
let name = cursor.until(&NAME_END);
if name.is_empty() {
return Err(ParserError::at(cursor.pos(), "expected function name".to_string()));
}
cursor.expect_char('(')?;
cursor.expect_char(')')?;
let body = Body::parse(cursor)?;
Ok(Self { name, body })
}
}
impl Debug for Function {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.write_str("fn ")?;
f.write_str(&self.name)?;
f.write_str("() ")?;
self.body.fmt(f)?;
Ok(())
}
}

10
src/parser/v2/util.rs Normal file
View File

@@ -0,0 +1,10 @@
use std::{collections::HashSet, sync::LazyLock};
pub const WHITESPACE: [char; 25] = [
'\u{0009}', '\u{000A}', '\u{000B}', '\u{000C}', '\u{000D}', '\u{0020}', '\u{0085}', '\u{00A0}',
'\u{1680}', '\u{2000}', '\u{2001}', '\u{2002}', '\u{2003}', '\u{2004}', '\u{2005}', '\u{2006}',
'\u{2007}', '\u{2008}', '\u{2009}', '\u{200A}', '\u{2028}', '\u{2029}', '\u{202F}', '\u{205F}',
'\u{3000}',
];
pub static WHITESPACE_SET: LazyLock<HashSet<char>> = LazyLock::new(|| HashSet::from_iter(WHITESPACE));