function input mutable by default

This commit is contained in:
gluax 2021-03-19 11:30:24 -04:00
parent b50919085b
commit 1d9befc0ac
14 changed files with 39 additions and 21 deletions

View File

@ -1,4 +1,4 @@
// Function input are immutable by default.
function main(a: bool) {
// Const function input are immutable.
function main(const a: bool) {
a = false;
}

View File

@ -7,7 +7,7 @@
// line 11: variable `x` is set to value `0`.
// line 18: program returns the value of `a`.
function bad_mutate(mut x: u32) {
function bad_mutate(x: u32) {
x = 0; // <- does not change `a`
}

View File

@ -1,5 +1,5 @@
// Adding the `mut` keyword makes a function variable mutable.
function main(mut a: bool) {
// Function input are mutable by default.
function main(a: bool) {
a = true;
console.assert(a == true);

View File

@ -1,5 +1,5 @@
// Swap two elements of an array.
function swap(mut a: [u32; 2], const i: u32, const j: u32) -> [u32; 2] {
function swap(a: [u32; 2], const i: u32, const j: u32) -> [u32; 2] {
const t = a[i];
a[i] = a[j];
a[j] = t;

View File

@ -7,7 +7,7 @@
// line 11: variable `x` is set to value `0`.
// line 18: program returns the value of `a`.
function bad_mutate(mut x: u32) {
function bad_mutate(x: u32) {
x = 0; // <- does not change `a`
}

View File

@ -1,4 +1,4 @@
// Function input are immutable by default.
function main(a: bool) {
// Const function input are immutable.
function main(const a: bool) {
a = false;
}

View File

@ -1,5 +1,5 @@
// Adding the `mut` keyword makes a function variable mutable.
function main(mut a: bool) {
// Function input are mutable by default.
function main(a: bool) {
a = true;
console.assert(a == true);

View File

@ -1,5 +1,5 @@
// Swap two elements of an array.
function swap(mut a: [u32; 2], const i: u32, const j: u32) -> [u32; 2] {
function swap(a: [u32; 2], const i: u32, const j: u32) -> [u32; 2] {
const t = a[i];
a[i] = a[j];
a[j] = t;

View File

@ -43,9 +43,19 @@ impl LeoError for DeprecatedError {
}
impl DeprecatedError {
pub fn let_mut_statement(span: &Span) -> Self {
pub fn mut_function_input(mut span: Span) -> Self {
let message =
"function func(mut a: u32) { ... } is deprecated. Passed variables are mutable by default.".to_string();
span.col_start -= 1;
span.col_stop -= 1;
Self::new_from_span(message, &span)
}
pub fn let_mut_statement(mut span: Span) -> Self {
let message = "let mut = ... is deprecated. `let` keyword implies mutabality by default.".to_string();
Self::new_from_span(message, span)
span.col_start -= 1;
span.col_stop -= 1;
Self::new_from_span(message, &span)
}
pub fn test_function(span: &Span) -> Self {

View File

@ -332,11 +332,18 @@ impl ParserContext {
}
return Ok(FunctionInput::SelfKeyword(SelfKeyword { span: name.span }));
}
if let Some(mutable) = &mutable {
return Err(SyntaxError::DeprecatedError(DeprecatedError::mut_function_input(
&mutable.span + &name.span,
)));
}
self.expect(Token::Colon)?;
let type_ = self.parse_type()?.0;
Ok(FunctionInput::Variable(FunctionInputVariable {
const_: const_.is_some(),
mutable: mutable.is_some(),
mutable: const_.is_none(),
type_,
span: name.span.clone(),
identifier: name,

View File

@ -287,9 +287,10 @@ impl ParserContext {
/// valid keywords.
///
pub fn parse_variable_name(&mut self, span: &SpannedToken) -> SyntaxResult<VariableName> {
if self.eat(Token::Mut).is_some() {
let mutable = self.eat(Token::Mut);
if let Some(mutable) = &mutable {
return Err(SyntaxError::DeprecatedError(DeprecatedError::let_mut_statement(
&span.span,
&mutable.span + &span.span,
)));
}

View File

@ -7,7 +7,7 @@
// line 11: variable `x` is set to value `0`.
// line 18: program returns the value of `a`.
function bad_mutate(mut x: u32) {
function bad_mutate(x: u32) {
x = 0; // <- does not change `a`
}

View File

@ -1,5 +1,5 @@
// Adding the `mut` keyword makes a function variable mutable.
function main(mut a: bool) {
// Function input are mutable by default.
function main(a: bool) {
a = true;
console.assert(a == true);

View File

@ -1,5 +1,5 @@
// Swap two elements of an array.
function swap(mut a: [u32; 2], const i: u32, const j: u32) -> [u32; 2] {
function swap(a: [u32; 2], const i: u32, const j: u32) -> [u32; 2] {
const t = a[i];
a[i] = a[j];
a[j] = t;