-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
13 changed files
with
171 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
use super::*; | ||
use crate::ast_node; | ||
|
||
ast_node! { | ||
typed struct Assign<TyInfo, FnIdentifier, IdentIdentifier> { | ||
binding: IdentIdentifier, | ||
value: Box<Expression<TyInfo, FnIdentifier, IdentIdentifier>>, | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
pub use super::*; | ||
|
||
pub fn parse_assign(compiler: &mut Compiler, tokens: &mut Lexer<'_>) -> Result<Assign, ParseError> { | ||
let (binding, span_start) = match tokens.next_spanned().unwrap() { | ||
(Token::Ident(ident), span) => (ident, span), | ||
(token, _) => { | ||
return Err(ParseError::ExpectedToken { | ||
expected: Box::new(Token::Ident(String::new())), | ||
found: Box::new(token), | ||
reason: "assign must start with ident".to_string(), | ||
}); | ||
} | ||
}; | ||
|
||
let binding = compiler.symbols.get_or_intern(binding); | ||
|
||
match tokens.next_token().unwrap() { | ||
Token::Eq => (), | ||
token => { | ||
return Err(ParseError::ExpectedToken { | ||
expected: Box::new(Token::Eq), | ||
found: Box::new(token), | ||
reason: "equals sign following binding for assign".to_string(), | ||
}); | ||
} | ||
} | ||
|
||
let value = parse_expression(compiler, tokens, Precedence::Lowest)?; | ||
|
||
Ok(Assign { | ||
span: span_start.start..value.span().end, | ||
binding, | ||
value: Box::new(value), | ||
ty_info: None, | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
use crate::{compiler::Compiler, util::scope::Scope}; | ||
|
||
use super::*; | ||
|
||
impl parse_ast::Assign { | ||
pub fn ty_solve(self, compiler: &mut Compiler, scope: &mut Scope) -> Result<Assign, TyError> { | ||
// Work out what type the variable has to be | ||
let (binding, ty) = scope | ||
.resolve(self.binding) | ||
.ok_or(TyError::SymbolNotFound(self.binding))?; | ||
|
||
let value = self.value.ty_solve(compiler, scope)?; | ||
|
||
let value_ty = value.get_ty_info().ty; | ||
|
||
if value_ty != ty { | ||
return Err(TyError::Mismatch(ty, value_ty)); | ||
} | ||
|
||
Ok(Assign { | ||
binding, | ||
ty_info: TyInfo { | ||
ty: Ty::Unit, | ||
return_ty: value.get_ty_info().return_ty, | ||
}, | ||
value: Box::new(value), | ||
span: self.span, | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters