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 1c870a9 commit 1b00a59
Show file tree
Hide file tree
Showing 5 changed files with 177 additions and 108 deletions.
55 changes: 55 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ authors = ["Somfic"]
description = "The 'som' programming language"

[dependencies]
inkwell = "0.5.0"
inkwell = { version = "0.5.0", features = ["llvm13-0"] }
miette = { version = "7.2.0", features = ["fancy", "syntect-highlighter"] }
owo-colors = "4.1.0"
pretty_assertions = "1.4.1"
Expand Down
14 changes: 14 additions & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,3 +49,17 @@ code Purrer for Cat:
};

```


```
install cmake
install strawberryperl
winget install Ninja-build.Ninja
cargo install llvmenv
llvmenv init
llvmenv entries
llvmenv build-entry 10.0.0
```
107 changes: 107 additions & 0 deletions src/passer/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,110 @@ impl PasserResult {
self
}
}

pub fn walk<'de>(
symbol: &Symbol<'de>,
statement_fn: fn(&Statement<'de>) -> Result<PasserResult>,
expression_fn: fn(&Expression<'de>) -> Result<PasserResult>,
) -> Result<PasserResult> {
match symbol {
Symbol::Statement(statement) => walk_statement(statement, statement_fn, expression_fn),
Symbol::Expression(expression) => walk_expression(expression, statement_fn, expression_fn),
}
}

fn walk_statement<'de>(
statement: &Statement<'de>,
statement_fn: fn(&Statement<'de>) -> Result<PasserResult>,
expression_fn: fn(&Expression<'de>) -> Result<PasserResult>,
) -> Result<PasserResult> {
let mut result = statement_fn(statement)?;

match &statement.value {
StatementValue::Block(statements) => {
for statement in statements {
result = result.combine(walk_statement(statement, statement_fn, expression_fn)?);
}
}
StatementValue::Expression(expression) => {
result = result.combine(walk_expression(expression, statement_fn, expression_fn)?);
}
StatementValue::Assignment { name, value } => {
result = result.combine(walk_expression(value, statement_fn, expression_fn)?);
}
StatementValue::Struct { name, fields } => {}
StatementValue::Enum { name, variants } => {}
StatementValue::Function { header, body } => {
result = result.combine(walk_expression(body, statement_fn, expression_fn)?);
}
StatementValue::Trait { name, functions } => {}
StatementValue::Return(expression) => {
result = result.combine(walk_expression(expression, statement_fn, expression_fn)?);
}
StatementValue::Conditional {
condition,
truthy,
falsy,
} => {
result = result.combine(walk_expression(condition, statement_fn, expression_fn)?);
result = result.combine(walk_statement(truthy, statement_fn, expression_fn)?);
if let Some(falsy) = falsy {
result = result.combine(walk_statement(falsy, statement_fn, expression_fn)?);
}
}
}

Ok(result)
}

fn walk_expression<'de>(
expression: &Expression<'de>,
statement_fn: fn(&Statement<'de>) -> Result<PasserResult>,
expression_fn: fn(&Expression<'de>) -> Result<PasserResult>,
) -> Result<PasserResult> {
let mut result = expression_fn(expression)?;

match &expression.value {
ExpressionValue::Binary {
operator,
left,
right,
} => {
result = result.combine(walk_expression(left, statement_fn, expression_fn)?);
result = result.combine(walk_expression(right, statement_fn, expression_fn)?);
}
ExpressionValue::Unary { operator, operand } => {
result = result.combine(walk_expression(operand, statement_fn, expression_fn)?);
}
ExpressionValue::Group(expression) => {
result = result.combine(walk_expression(expression, statement_fn, expression_fn)?);
}
ExpressionValue::Block {
statements,
return_value,
} => {
for statement in statements {
result = result.combine(walk_statement(statement, statement_fn, expression_fn)?);
}
result = result.combine(walk_expression(return_value, statement_fn, expression_fn)?);
}
ExpressionValue::Conditional {
condition,
truthy,
falsy,
} => {
result = result.combine(walk_expression(condition, statement_fn, expression_fn)?);
result = result.combine(walk_expression(truthy, statement_fn, expression_fn)?);
result = result.combine(walk_expression(falsy, statement_fn, expression_fn)?);
}
ExpressionValue::Call { callee, arguments } => {
result = result.combine(walk_expression(callee, statement_fn, expression_fn)?);
for argument in arguments {
result = result.combine(walk_expression(argument, statement_fn, expression_fn)?);
}
}
ExpressionValue::Primitive(_) => {}
}

Ok(result)
}
107 changes: 0 additions & 107 deletions src/passer/typing/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,113 +54,6 @@ impl Passer for TypingPasser {
}
}

pub fn walk<'de>(
symbol: &Symbol<'de>,
statement_fn: fn(&Statement<'de>) -> Result<PasserResult>,
expression_fn: fn(&Expression<'de>) -> Result<PasserResult>,
) -> Result<PasserResult> {
match symbol {
Symbol::Statement(statement) => walk_statement(statement, statement_fn, expression_fn),
Symbol::Expression(expression) => walk_expression(expression, statement_fn, expression_fn),
}
}

pub fn walk_statement<'de>(
statement: &Statement<'de>,
statement_fn: fn(&Statement<'de>) -> Result<PasserResult>,
expression_fn: fn(&Expression<'de>) -> Result<PasserResult>,
) -> Result<PasserResult> {
let mut result = statement_fn(statement)?;

match &statement.value {
StatementValue::Block(statements) => {
for statement in statements {
result = result.combine(walk_statement(statement, statement_fn, expression_fn)?);
}
}
StatementValue::Expression(expression) => {
result = result.combine(walk_expression(expression, statement_fn, expression_fn)?);
}
StatementValue::Assignment { name, value } => {
result = result.combine(walk_expression(value, statement_fn, expression_fn)?);
}
StatementValue::Struct { name, fields } => {}
StatementValue::Enum { name, variants } => {}
StatementValue::Function { header, body } => {
result = result.combine(walk_expression(body, statement_fn, expression_fn)?);
}
StatementValue::Trait { name, functions } => {}
StatementValue::Return(expression) => {
result = result.combine(walk_expression(expression, statement_fn, expression_fn)?);
}
StatementValue::Conditional {
condition,
truthy,
falsy,
} => {
result = result.combine(walk_expression(condition, statement_fn, expression_fn)?);
result = result.combine(walk_statement(truthy, statement_fn, expression_fn)?);
if let Some(falsy) = falsy {
result = result.combine(walk_statement(falsy, statement_fn, expression_fn)?);
}
}
}

Ok(result)
}

pub fn walk_expression<'de>(
expression: &Expression<'de>,
statement_fn: fn(&Statement<'de>) -> Result<PasserResult>,
expression_fn: fn(&Expression<'de>) -> Result<PasserResult>,
) -> Result<PasserResult> {
let mut result = expression_fn(expression)?;

match &expression.value {
ExpressionValue::Binary {
operator,
left,
right,
} => {
result = result.combine(walk_expression(left, statement_fn, expression_fn)?);
result = result.combine(walk_expression(right, statement_fn, expression_fn)?);
}
ExpressionValue::Unary { operator, operand } => {
result = result.combine(walk_expression(operand, statement_fn, expression_fn)?);
}
ExpressionValue::Group(expression) => {
result = result.combine(walk_expression(expression, statement_fn, expression_fn)?);
}
ExpressionValue::Block {
statements,
return_value,
} => {
for statement in statements {
result = result.combine(walk_statement(statement, statement_fn, expression_fn)?);
}
result = result.combine(walk_expression(return_value, statement_fn, expression_fn)?);
}
ExpressionValue::Conditional {
condition,
truthy,
falsy,
} => {
result = result.combine(walk_expression(condition, statement_fn, expression_fn)?);
result = result.combine(walk_expression(truthy, statement_fn, expression_fn)?);
result = result.combine(walk_expression(falsy, statement_fn, expression_fn)?);
}
ExpressionValue::Call { callee, arguments } => {
result = result.combine(walk_expression(callee, statement_fn, expression_fn)?);
for argument in arguments {
result = result.combine(walk_expression(argument, statement_fn, expression_fn)?);
}
}
ExpressionValue::Primitive(_) => {}
}

Ok(result)
}

pub trait Typing<'de> {
fn possible_types(&self) -> Vec<Type<'de>>;
}
Expand Down

0 comments on commit 1b00a59

Please sign in to comment.