Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft: Add Else Operator #198

Draft
wants to merge 10 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions examples/parsing/else.an
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
x = Some 10

y = x else 0
3 changes: 3 additions & 0 deletions src/cache/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,8 @@ pub struct ModuleCache<'a> {
pub error_count: usize,

pub file_cache: FileCache,

pub maybe_type: Option<TypeInfoId>,
}

pub type FileCache = HashMap<PathBuf, String>;
Expand Down Expand Up @@ -417,6 +419,7 @@ impl<'a> ModuleCache<'a> {
global_dependency_graph: DependencyGraph::default(),
diagnostics: Vec::new(),
error_count: 0,
maybe_type: None,
file_cache,
}
}
Expand Down
2 changes: 2 additions & 0 deletions src/nameresolution/builtin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,8 @@ pub fn import_prelude(resolver: &mut NameResolver, cache: &mut ModuleCache<'_>)
if let Some(id) = declare_module(&prelude_dir, cache, Location::builtin()) {
let exports = define_module(id, cache, Location::builtin()).unwrap();
resolver.current_scope().import(exports, cache, Location::builtin(), &HashSet::new());
let maybe_id = resolver.current_scope().types.get("Maybe").unwrap().clone();
cache.maybe_type = Some(maybe_id);
}
}

Expand Down
9 changes: 9 additions & 0 deletions src/parser/desugar.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ pub fn desugar_operators<'a>(operator: Token, lhs: Ast<'a>, rhs: Ast<'a>, locati
Some(Token::ApplyRight) => prepend_argument_to_function(rhs, lhs, location),
Some(Token::And) => Ast::if_expr(lhs, rhs, Some(Ast::bool_literal(false, location)), location),
Some(Token::Or) => Ast::if_expr(lhs, Ast::bool_literal(true, location), Some(rhs), location),
Some(Token::Else) => create_else_match(lhs, rhs, location),
Some(operator_token) => {
let operator = Ast::operator(operator_token, location);
Ast::function_call(operator, vec![lhs, rhs], location)
Expand All @@ -72,6 +73,14 @@ pub fn desugar_operators<'a>(operator: Token, lhs: Ast<'a>, rhs: Ast<'a>, locati
desugar_explicit_currying(operator_symbol, vec![lhs, rhs], call_operator_function, location)
}

fn create_else_match<'a>(lhs: Ast<'a>, rhs: Ast<'a>, location: Location<'a>) -> Ast<'a> {
let x = Ast::variable(vec![], "x".to_owned(), location);
let some = Ast::type_constructor(vec!["prelude".to_owned()], "Some".to_owned(), location);
let some_x = Ast::function_call(some, vec![x.clone()], location);
let none = Ast::type_constructor(vec!["prelude".to_owned()], "None".to_owned(), location);
Ast::match_expr(lhs, vec![(some_x, x), (none, rhs)], location)
}

fn prepend_argument_to_function<'a>(f: Ast<'a>, arg: Ast<'a>, location: Location<'a>) -> Ast<'a> {
match f {
Ast::FunctionCall(mut call) => {
Expand Down
5 changes: 3 additions & 2 deletions src/parser/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -380,8 +380,9 @@ fn precedence(token: &Token) -> Option<(i8, bool)> {
Token::In => Some((8, false)),
Token::Append => Some((9, false)),
Token::Range => Some((10, false)),
Token::Add | Token::Subtract => Some((11, false)),
Token::Multiply | Token::Divide | Token::Modulus => Some((12, false)),
Token::Else => Some((11, false)),
Token::Add | Token::Subtract => Some((12, false)),
Token::Multiply | Token::Divide | Token::Modulus => Some((13, false)),
Token::Index => Some((14, false)),
Token::As => Some((15, false)),
_ => None,
Expand Down
Loading