Skip to content

Commit

Permalink
🚧 WIP
Browse files Browse the repository at this point in the history
semver: chore
  • Loading branch information
Somfic committed Nov 2, 2024
1 parent c46b92c commit e8a8359
Show file tree
Hide file tree
Showing 6 changed files with 114 additions and 40 deletions.
5 changes: 3 additions & 2 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,9 @@ pub mod parser;
pub mod passer;

const INPUT: &str = "
fn test() {
let a = 12 + {true + false;};
fn main() {
let string = \"Hello, world!\";
println(argument);
}
";

Expand Down
102 changes: 102 additions & 0 deletions src/parser/ast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,108 @@ pub enum StatementValue<'de> {
},
}

impl Display for StatementValue<'_> {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
StatementValue::Block(vec) => write!(f, "a block of {} statements", vec.len()),
StatementValue::Expression(expression) => write!(f, "{}", expression),
StatementValue::Assignment { name, value } => {
write!(f, "`{}` assignment with {}", name, value)
}
StatementValue::Struct { name, fields } => write!(f, "`{}` struct", name),
StatementValue::Enum { name, variants } => write!(f, "`{}` enum", name),
StatementValue::Function { header, body } => write!(f, "`{}` function", header.name),
StatementValue::Trait { name, functions } => write!(f, "`{}` trait", name),
StatementValue::Return(expression) => write!(f, "returning {}", expression),
StatementValue::Conditional {
condition,
truthy,
falsy,
} => write!(f, "conditional statement"),
}
}
}

impl Display for Statement<'_> {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{}", self.value)
}
}

impl Display for ExpressionValue<'_> {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
ExpressionValue::Primitive(primitive) => write!(f, "{}", primitive),
ExpressionValue::Binary {
operator,
left,
right,
} => write!(f, "{} expression", operator),
ExpressionValue::Unary { operator, operand } => write!(f, "{} expression", operator),
ExpressionValue::Group(expression) => write!(f, "grouped expression"),
ExpressionValue::Block {
statements,
return_value,
} => write!(f, "block expression"),
ExpressionValue::Conditional {
condition,
truthy,
falsy,
} => write!(f, "conditional expression"),
ExpressionValue::Call { callee, arguments } => write!(f, "calling {}", callee.value),
}
}
}

impl Display for Primitive<'_> {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
Primitive::Integer(value) => write!(f, "{}", value),
Primitive::Decimal(value) => write!(f, "{}", value),
Primitive::String(value) => write!(f, "{}", value),
Primitive::Identifier(value) => write!(f, "{}", value),
Primitive::Character(value) => write!(f, "{}", value),
Primitive::Boolean(value) => write!(f, "{}", value),
Primitive::Unit => write!(f, "nothing"),
}
}
}

impl Display for Expression<'_> {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{}", self.value)
}
}

impl Display for BinaryOperator {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
BinaryOperator::Add => write!(f, "addition"),
BinaryOperator::Subtract => write!(f, "subtraction"),
BinaryOperator::Multiply => write!(f, "multiplication"),
BinaryOperator::Divide => write!(f, "division"),
BinaryOperator::Modulo => write!(f, "modulo"),
BinaryOperator::Equality => write!(f, "equality"),
BinaryOperator::Inequality => write!(f, "inequality"),
BinaryOperator::LessThan => write!(f, "less than"),
BinaryOperator::LessThanOrEqual => write!(f, "less than or equal"),
BinaryOperator::GreaterThan => write!(f, "greater than"),
BinaryOperator::GreaterThanOrEqual => write!(f, "greater than or equal"),
BinaryOperator::And => write!(f, "and"),
BinaryOperator::Or => write!(f, "or"),
}
}
}

impl Display for UnaryOperator {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
UnaryOperator::Negate => write!(f, "negation"),
UnaryOperator::Negative => write!(f, "negative"),
}
}
}

#[derive(Debug, Clone)]
pub struct Expression<'de> {
pub value: ExpressionValue<'de>,
Expand Down
6 changes: 3 additions & 3 deletions src/parser/expression/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -93,12 +93,12 @@ pub fn call<'de>(
arguments.push(argument);
}

parser
let close = parser
.lexer
.expect(TokenKind::ParenClose, "expected a closing parenthesis")?;

Ok(Expression::at(
lhs.span,
Ok(Expression::at_multiple(
vec![lhs.span, close.span],
ExpressionValue::Call {
callee: Box::new(lhs.clone()),
arguments,
Expand Down
6 changes: 3 additions & 3 deletions src/parser/statement.rs
Original file line number Diff line number Diff line change
Expand Up @@ -244,14 +244,14 @@ pub fn trait_<'de>(parser: &mut Parser<'de>) -> Result<Statement<'de>> {
}

pub fn return_<'de>(parser: &mut Parser<'de>) -> Result<Statement<'de>> {
let token = parser
parser
.lexer
.expect(TokenKind::Return, "expected a return keyword")?;

let expression = expression::parse(parser, BindingPower::None)?;

Ok(Statement::at_multiple(
vec![token.span, expression.span],
Ok(Statement::at(
expression.span,
StatementValue::Return(expression),
))
}
Expand Down
2 changes: 1 addition & 1 deletion src/passer/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ pub trait Passer {
fn pass(ast: &Symbol<'_>) -> Result<PasserResult>;
}

#[derive(Default)]
#[derive(Default, Debug)]
pub struct PasserResult {
pub non_critical: Vec<Report>,
pub critical: Vec<Report>,
Expand Down
33 changes: 2 additions & 31 deletions src/passer/typing/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,36 +14,7 @@ pub struct TypingPasser;
impl Passer for TypingPasser {
fn pass(ast: &Symbol<'_>) -> Result<PasserResult> {
fn check_expression(expression: &Expression<'_>) -> Result<PasserResult> {
let mut critical = vec![];

let types = expression.possible_types();

let distinct_types = types.clone().into_iter().fold(vec![], |mut acc, ty| {
if !acc.iter().any(|t: &Type<'_>| t.value == ty.value) {
acc.push(ty);
}
acc
});

if distinct_types.is_empty() || distinct_types.len() == 1 {
return Ok(PasserResult::default());
}

let labels = types
.iter()
.map(|ty| LabeledSpan::at(ty.span, format!("{}", ty)))
.collect::<Vec<_>>();

// critical.push(miette::miette! {
// labels = labels,
// help = "expression has multiple possible types",
// "{:?} has multiple possible types", expression.value
// });

Ok(PasserResult {
critical,
non_critical: vec![],
})
Ok(PasserResult::default())
}

fn check_statement(statement: &Statement<'_>) -> Result<PasserResult> {
Expand All @@ -70,7 +41,7 @@ impl Passer for TypingPasser {
critical.push(miette::miette! {
labels = labels,
help = "statement has multiple possible types",
"{:?} has multiple possible types", statement.value
"{} has multiple possible types", statement.value
});

Ok(PasserResult {
Expand Down

0 comments on commit e8a8359

Please sign in to comment.