Skip to content

Commit

Permalink
Change '&mut' to '!'
Browse files Browse the repository at this point in the history
  • Loading branch information
jfecher committed Aug 11, 2024
1 parent 1f7660e commit 0d704f2
Show file tree
Hide file tree
Showing 9 changed files with 41 additions and 28 deletions.
2 changes: 1 addition & 1 deletion examples/codegen/mutability.an
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ print num
mutate num
print num

mutate (n: &mut I32) =
mutate (n: !I32) =
x = double @n
n := x

Expand Down
2 changes: 1 addition & 1 deletion examples/codegen/string_builder.an
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import StringBuilder

sb: &mut StringBuilder = mut empty ()
sb: !StringBuilder = mut empty ()
reserve sb 10
append sb "你好,"
append sb " World!"
Expand Down
2 changes: 2 additions & 0 deletions src/lexer/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -610,6 +610,8 @@ impl<'cache, 'contents> Iterator for Lexer<'cache, 'contents> {
('\\', _) => self.advance_with(Token::Backslash),
('&', _) => self.advance_with(Token::Ampersand),
('@', _) => self.advance_with(Token::At),
('!', _) => self.advance_with(Token::ExclamationMark),
('?', _) => self.advance_with(Token::QuestionMark),
(c, _) => self.advance_with(Token::Invalid(LexerError::UnknownChar(c))),
}
}
Expand Down
4 changes: 4 additions & 0 deletions src/lexer/token.rs
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,8 @@ pub enum Token {
Backslash, // \
Ampersand, // &
At, // @
ExclamationMark, // !
QuestionMark, // ?
}

impl Token {
Expand Down Expand Up @@ -334,6 +336,8 @@ impl Display for Token {
Token::Backslash => write!(f, "'\\'"),
Token::Ampersand => write!(f, "'&'"),
Token::At => write!(f, "'@'"),
Token::ExclamationMark => write!(f, "'!'"),
Token::QuestionMark => write!(f, "'?'"),
}
}
}
1 change: 0 additions & 1 deletion src/parser/ast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -223,7 +223,6 @@ pub enum Sharedness {

#[derive(Debug, Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub enum Mutability {
#[allow(unused)]
Polymorphic,
Immutable,
Mutable,
Expand Down
24 changes: 16 additions & 8 deletions src/parser/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -570,7 +570,7 @@ parser!(not_expr loc =
);

parser!(ref_expr loc =
token <- expect(Token::Ampersand);
token <- or(&[expect(Token::Ampersand), expect(Token::ExclamationMark)], "expression");
expr !<- term;
Ast::function_call(Ast::operator(token, loc), vec![expr], loc)
);
Expand Down Expand Up @@ -645,6 +645,7 @@ fn function_argument<'a, 'b>(input: Input<'a, 'b>) -> AstResult<'a, 'b> {
match input[0].0 {
Token::Not => not_expr(input),
Token::Ampersand => ref_expr(input),
Token::ExclamationMark => ref_expr(input),
Token::At => at_expr(input),
_ => member_access(input),
}
Expand All @@ -653,6 +654,7 @@ fn function_argument<'a, 'b>(input: Input<'a, 'b>) -> AstResult<'a, 'b> {
fn pattern_function_argument<'a, 'b>(input: Input<'a, 'b>) -> AstResult<'a, 'b> {
match input[0].0 {
Token::Ampersand => ref_expr(input),
Token::ExclamationMark => ref_expr(input),
Token::At => at_expr(input),
_ => pattern_argument(input),
}
Expand Down Expand Up @@ -861,21 +863,27 @@ parser!(unit_type loc -> 'b Type<'b> =
);

parser!(reference_type loc -> 'b Type<'b> =
_ <- expect(Token::Ampersand);
mutability <- reference_operator;
sharedness <- sharedness;
mutability <- maybe(expect(Token::Mut));
element <- maybe(reference_element_type);
{
let mutability = if mutability.is_some() { Mutability::Mutable } else { Mutability::Immutable };
make_reference_type(Type::Reference(sharedness, mutability, loc), element, loc)
make_reference_type(Type::Reference(sharedness, mutability, loc), element, loc)
);

parser!(reference_operator loc -> 'b Mutability =
token <- or(&[expect(Token::Ampersand), expect(Token::ExclamationMark)], "type");
match token {
Token::Ampersand => Mutability::Immutable,
Token::ExclamationMark => Mutability::Mutable,
Token::QuestionMark => Mutability::Polymorphic,
_ => unreachable!(),
}
);

// The basic reference type `&t` can be used without parenthesis in a type application
parser!(basic_reference_type loc -> 'b Type<'b> =
_ <- expect(Token::Ampersand);
mutability <- reference_operator;
element <- maybe(basic_type);
make_reference_type(Type::Reference(Sharedness::Polymorphic, Mutability::Immutable, loc), element, loc)
make_reference_type(Type::Reference(Sharedness::Polymorphic, mutability, loc), element, loc)
);

parser!(reference_element_type loc -> 'b Type<'b> =
Expand Down
12 changes: 6 additions & 6 deletions stdlib/HashMap.an
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,14 @@ trait Hash t with

empty () = HashMap 0 0 (null ())

clear (map: &mut HashMap k v) : Unit =
clear (map: !HashMap k v) : Unit =
if map.capacity != 0 then
repeat map.capacity fn i ->
entry = mut deref_ptr <| offset map.entries i
entry.&occupied := false
entry.&tombstone := false

resize (map: &mut HashMap k v) (new_capacity: Usz) : Unit =
resize (map: !HashMap k v) (new_capacity: Usz) : Unit =
if new_capacity > map.capacity then
new_memory = calloc new_capacity (size_of (MkType : Type (Entry k v)))

Expand All @@ -44,16 +44,16 @@ should_resize (map: HashMap k v) : Bool =
scale_factor = 2
(map.len + 1) * scale_factor > map.capacity

insert (map: &mut HashMap k v) (key: k) (value: v) : Unit =
insert (map: !HashMap k v) (key: k) (value: v) : Unit =
if should_resize @map then
resize map ((map.capacity + 1) * 2)

iter_until (map: &mut HashMap k v) (key: k) (value: v) (start: Usz) (end: Usz) : Bool =
iter_until (map: !HashMap k v) (key: k) (value: v) (start: Usz) (end: Usz) : Bool =
if start >= end then
false
else
entry_ptr = offset (map.entries) start
entry = transmute entry_ptr : &mut Entry k v
entry = transmute entry_ptr : !Entry k v
if entry.occupied then
iter_until map key value (start + 1) end
else
Expand Down Expand Up @@ -105,7 +105,7 @@ get (map: HashMap k v) (key: k) : Maybe v =
| None -> None


remove (map: &mut HashMap k v) (key: k) : Maybe v =
remove (map: !HashMap k v) (key: k) : Maybe v =
match get_entry (@map) key
| None -> None
| Some e2 ->
Expand Down
4 changes: 2 additions & 2 deletions stdlib/StringBuilder.an
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ type StringBuilder =
empty () = StringBuilder (null ()) 0 0

// reserve space for at least n additional characters
reserve (s: &mut StringBuilder) (n: Usz) : Unit =
reserve (s: !StringBuilder) (n: Usz) : Unit =
if s.length + n > s.cap then
new_size = s.cap + n
ptr = realloc s.data new_size
Expand All @@ -19,7 +19,7 @@ reserve (s: &mut StringBuilder) (n: Usz) : Unit =
s.&cap := new_size

// append a string
append (s: &mut StringBuilder) (new_str: String) : Unit =
append (s: !StringBuilder) (new_str: String) : Unit =
reserve s new_str.length
memcpy (cast (cast s.data + s.length)) new_str.c_string (cast (new_str.length+1))
s.&length := s.length + new_str.length
Expand Down
18 changes: 9 additions & 9 deletions stdlib/Vec.an
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,12 @@ len v = v.len
capacity v = v.cap

//Fill Vec with items from the iterable
fill (v: &mut Vec t) iterable : &mut Vec t =
fill (v: !Vec t) iterable : !Vec t =
iter iterable (push v _)
v

//reserve numElements in Vec v, elements will be uninitialized
reserve (v: &mut Vec t) (numElems: Usz) : Unit =
reserve (v: !Vec t) (numElems: Usz) : Unit =
if v.len + numElems > v.cap then
size = (v.cap + numElems) * size_of (MkType: Type t)
ptr = realloc (v.data) size
Expand All @@ -37,7 +37,7 @@ reserve (v: &mut Vec t) (numElems: Usz) : Unit =

//push an element onto the end of the vector.
//resizes if necessary
push (v: &mut Vec t) (elem: t) : Unit =
push (v: !Vec t) (elem: t) : Unit =
if v.len >= v.cap then
reserve v (if v.cap == 0usz then 1 else v.cap)

Expand All @@ -46,15 +46,15 @@ push (v: &mut Vec t) (elem: t) : Unit =

//pop the last element off if it exists
//this will never resize the vector.
pop (v: &mut Vec t) : Maybe t =
pop (v: !Vec t) : Maybe t =
if is_empty v then None
else
v.&len := v.len - 1
Some (v.data#v.len)

//remove the element at the given index and return it.
//will error if the index is out of bounds.
remove_index (v: &mut Vec t) (idx:Usz) : t =
remove_index (v: !Vec t) (idx:Usz) : t =
if idx == v.len - 1 then
v.&len := v.len - 1
else if idx >= 0 and idx < v.len - 1 then
Expand All @@ -71,7 +71,7 @@ remove_index (v: &mut Vec t) (idx:Usz) : t =
//the vector or none if the element was not found.
//Uses == to determine element equality.
//returns the index where the element was found.
remove_first (v: &mut Vec t) (elem: t) : Maybe Usz =
remove_first (v: !Vec t) (elem: t) : Maybe Usz =
loop (i = 0) ->
if i >= v.len then
None
Expand All @@ -83,7 +83,7 @@ remove_first (v: &mut Vec t) (elem: t) : Maybe Usz =
//Remove the given indices from the vector
//Expects the indices to be in sorted order.
//Will error if any index is out of bounds.
remove_indices (v: &mut Vec t) (idxs: Vec Usz) : Unit =
remove_indices (v: !Vec t) (idxs: Vec Usz) : Unit =
moved = mut 0
iter (indices idxs) fn i ->
cur = idxs.data#i
Expand All @@ -105,7 +105,7 @@ remove_indices (v: &mut Vec t) (idxs: Vec Usz) : Unit =
//remove all matching elements from the vector and
//return the number of elements removed.
//Uses = to determine element equality.
remove_all (v: &mut Vec t) (elem: t) : Usz =
remove_all (v: !Vec t) (elem: t) : Usz =
idxs = mut empty ()
iter (indices @v) fn i ->
if elem == v.data#i then
Expand All @@ -118,7 +118,7 @@ remove_all (v: &mut Vec t) (elem: t) : Usz =
//Remove an element by swapping it with the last element in O(1) time.
//Returns true if a swap was performed or false otherwise.
//Will not swap if the given index is the index of the last element.
swap_last (v: &mut Vec t) (idx:Usz) : Bool =
swap_last (v: !Vec t) (idx:Usz) : Bool =
if idx >= v.len or idx + 1 == v.len then false
else
v.&len := v.len - 1
Expand Down

0 comments on commit 0d704f2

Please sign in to comment.