Fix bug preventing implicit & on optionals. Updated priority queue to return optionals. Changed the list API to have snake case on methods. Bump to 0.2.20

This commit is contained in:
Christoffer Lerno
2022-07-21 22:14:44 +02:00
committed by Christoffer Lerno
parent 18f7f35e80
commit 56a6e0b112
10 changed files with 105 additions and 82 deletions

View File

@@ -10,7 +10,7 @@ struct List
Type *entries;
}
private fn void List.ensureCapacity(List *list) @inline
private fn void List.ensure_capacity(List *list) @inline
{
if (list.capacity == list.size)
{
@@ -26,7 +26,7 @@ fn void List.push(List *list, Type element) @inline
fn void List.append(List *list, Type element)
{
list.ensureCapacity();
list.ensure_capacity();
list.entries[list.size++] = element;
}
@@ -41,14 +41,14 @@ fn Type List.pop(List *list)
/**
* @require list.size > 0
*/
fn Type List.popFirst(List *list)
fn Type List.pop_first(List *list)
{
Type value = list.entries[0];
list.removeAt(0);
list.remove_at(0);
return value;
}
fn void List.removeAt(List *list, usize index)
fn void List.remove_at(List *list, usize index)
{
for (usize i = index + 1; i < list.size; i++)
{
@@ -57,14 +57,14 @@ fn void List.removeAt(List *list, usize index)
list.size--;
}
fn void List.pushFront(List *list, Type type) @inline
fn void List.push_front(List *list, Type type) @inline
{
list.insertAt(0, type);
list.insert_at(0, type);
}
fn void List.insertAt(List *list, usize index, Type type)
fn void List.insert_at(List *list, usize index, Type type)
{
list.ensureCapacity();
list.ensure_capacity();
for (usize i = list.size; i > index; i--)
{
list.entries[i] = list.entries[i - 1];
@@ -73,14 +73,14 @@ fn void List.insertAt(List *list, usize index, Type type)
list.entries[index] = type;
}
fn void List.removeLast(List *list)
fn void List.remove_last(List *list)
{
list.size--;
}
fn void List.removeFirst(List *list)
fn void List.remove_first(List *list)
{
list.removeAt(0);
list.remove_at(0);
}
fn Type* List.first(List *list)
@@ -93,7 +93,7 @@ fn Type* List.last(List *list)
return list.size ? &list.entries[list.size - 1] : null;
}
fn bool List.isEmpty(List *list)
fn bool List.is_empty(List *list)
{
return !list.size;
}
@@ -113,6 +113,7 @@ fn void List.free(List *list)
mem::free(list.entries);
list.capacity = 0;
list.size = 0;
list.entries = null;
}
fn void List.swap(List *list, usize i, usize j)

View File

@@ -25,81 +25,101 @@ import std::array::list;
// Helper macros to allow arbitrary non-primitive types to be comparable
macro bool less(Type x, Type y)
private macro bool less(Type x, Type y)
{
$if ($defined(Type.less)):
return x.less(y);
$else:
return x < y;
$endif;
$if ($defined(Type.less)):
return x.less(y);
$else:
return x < y;
$endif;
}
macro bool greater(Type x, Type y)
private macro bool greater(Type x, Type y)
{
$if ($defined(Type.greater)):
return x.greater(y);
$else:
return x > y;
$endif;
$if ($defined(Type.greater)):
return x.greater(y);
$else:
return x > y;
$endif;
}
define Heap = List<Type>;
struct PriorityQueue {
Heap heap;
bool max; // true if max-heap, false if min-heap
struct PriorityQueue
{
Heap heap;
bool max; // true if max-heap, false if min-heap
}
fn void PriorityQueue.push(PriorityQueue *pq, Type element) {
pq.heap.push(element);
usize i = pq.heap.len() - 1;
while (i > 0) {
usize parent = (i - 1) / 2;
if ((pq.max && greater(pq.heap.get(i), pq.heap.get(parent))) || (!pq.max && less(pq.heap.get(i), pq.heap.get(parent)))) {
pq.heap.swap(i, parent);
i = parent;
} else {
break;
}
}
fn void PriorityQueue.push(PriorityQueue* pq, Type element)
{
pq.heap.push(element);
usize i = pq.heap.len() - 1;
while (i > 0)
{
usize parent = (i - 1) / 2;
if ((pq.max && greater(pq.heap.get(i), pq.heap.get(parent))) || (!pq.max && less(pq.heap.get(i), pq.heap.get(parent))))
{
pq.heap.swap(i, parent);
i = parent;
continue;
}
break;
}
}
/**
* @require pq.heap.len() > 0
*/
fn Type PriorityQueue.pop(PriorityQueue *pq) {
usize i = 0;
usize newCount = pq.heap.len() - 1;
pq.heap.swap(0, newCount);
while ((2 * i + 1) < newCount){
usize j = 2 * i + 1;
if (((j + 1) < newCount) &&
((pq.max && greater(pq.heap.get(j + 1), pq.heap[j])) ||
(!pq.max && less(pq.heap.get(j + 1), pq.heap.get(j))))) {
j++;
}
if ((pq.max && less(pq.heap.get(i), pq.heap.get(j))) || (!pq.max && greater(pq.heap.get(i), pq.heap.get(j)))) {
pq.heap.swap(i, j);
i = j;
} else {
break;
}
}
* @require pq != null
*/
fn Type! PriorityQueue.pop(PriorityQueue* pq)
{
usize i = 0;
usize len = pq.heap.len() @inline;
if (!len) return IteratorResult.NO_MORE_ELEMENT!;
usize newCount = len - 1;
pq.heap.swap(0, newCount);
while ((2 * i + 1) < newCount)
{
usize j = 2 * i + 1;
if (((j + 1) < newCount) &&
((pq.max && greater(pq.heap.get(j + 1), pq.heap[j]))
|| (!pq.max && less(pq.heap.get(j + 1), pq.heap.get(j)))))
{
j++;
}
if ((pq.max && less(pq.heap.get(i), pq.heap.get(j))) || (!pq.max && greater(pq.heap.get(i), pq.heap.get(j))))
{
pq.heap.swap(i, j);
i = j;
continue;
}
break;
}
return pq.heap.pop();
return pq.heap.pop();
}
/**
* @require pq.heap.len() > 0
* @require pq != null
*/
fn Type PriorityQueue.peek(PriorityQueue *pq) {
return pq.heap.get(0);
fn Type! PriorityQueue.peek(PriorityQueue* pq)
{
if (!pq.len()) return IteratorResult.NO_MORE_ELEMENT!;
return pq.heap.get(0);
}
fn void PriorityQueue.free(PriorityQueue *pq) {
pq.heap.free();
/**
* @require pq != null
*/
fn void PriorityQueue.free(PriorityQueue* pq)
{
pq.heap.free();
}
fn usize PriorityQueue.len(PriorityQueue *pq) @operator(len) {
return pq.heap.len();
/**
* @require pq != null
*/
fn usize PriorityQueue.len(PriorityQueue* pq) @operator(len)
{
return pq.heap.len();
}

View File

@@ -135,7 +135,9 @@ void expr_insert_addr(Expr *original)
}
Expr *inner = expr_copy(original);
original->expr_kind = EXPR_UNARY;
original->type = type_get_ptr(inner->type);
Type *inner_type = inner->type;
bool failable = type_is_failable(inner->type);
original->type = type_get_opt_fail(type_get_ptr(type_no_fail(inner->type)), failable);
original->unary_expr.operator = UNARYOP_ADDR;
original->unary_expr.expr = inner;
}

View File

@@ -1 +1 @@
#define COMPILER_VERSION "0.2.19"
#define COMPILER_VERSION "0.2.20"

View File

@@ -138,7 +138,7 @@ fn void main()
array.append(200);
array.append(400);
array.push(600);
array.insertAt(2, 300);
array.insert_at(2, 300);
for (int i = 0; i < (int)(array.len()); i++)
{
printf("Element[%d]: %d\n", i, array.get(i));
@@ -500,7 +500,7 @@ loop.exit: ; preds = %loop.cond
call void @"std_array_list$$int_List_append"(%List* %array, i32 200)
call void @"std_array_list$$int_List_append"(%List* %array, i32 400)
call void @"std_array_list$$int_List_push"(%List* %array, i32 600) #3
call void @"std_array_list$$int_List_insertAt"(%List* %array, i64 2, i32 300)
call void @"std_array_list$$int_List_insert_at"(%List* %array, i64 2, i32 300)
store i32 0, i32* %i1, align 4
br label %loop.cond2

View File

@@ -140,7 +140,7 @@ fn void main()
array.append(200);
array.append(400);
array.push(600);
array.insertAt(2, 300);
array.insert_at(2, 300);
for (int i = 0; i < (int)(array.len()); i++)
{
printf("Element[%d]: %d\n", i, array.get(i));
@@ -538,7 +538,7 @@ loop.exit: ; preds = %loop.cond
call void @"std_array_list$$int_List_append"(%List* %array, i32 200)
call void @"std_array_list$$int_List_append"(%List* %array, i32 400)
call void @"std_array_list$$int_List_push"(%List* %array, i32 600) #3
call void @"std_array_list$$int_List_insertAt"(%List* %array, i64 2, i32 300)
call void @"std_array_list$$int_List_insert_at"(%List* %array, i64 2, i32 300)
store i32 0, i32* %i1, align 4
br label %loop.cond2
@@ -687,7 +687,7 @@ declare void @"std_array_list$$int_List_append"(%List*, i32)
declare void @"std_array_list$$int_List_push"(%List*, i32)
declare void @"std_array_list$$int_List_insertAt"(%List*, i64, i32)
declare void @"std_array_list$$int_List_insert_at"(%List*, i64, i32)
declare i64 @"std_array_list$$int_List_len"(%List*)

View File

@@ -19,7 +19,7 @@ fn int main(char[][] argv)
stk.push(i);
}
for (;!stk.isEmpty();) {
for (;!stk.is_empty();) {
int value = stk.pop();
printf("%i\n", value);
}

View File

@@ -138,7 +138,7 @@ fn void main()
array.append(200);
array.append(400);
array.push(600);
array.insertAt(2, 300);
array.insert_at(2, 300);
for (int i = 0; i < (int)(array.len()); i++)
{
printf("Element[%d]: %d\n", i, array.get(i));
@@ -487,7 +487,7 @@ loop.exit: ; preds = %loop.cond
call void @"std_array_list$$int_List_append"(ptr %array, i32 200)
call void @"std_array_list$$int_List_append"(ptr %array, i32 400)
call void @"std_array_list$$int_List_push"(ptr %array, i32 600) #3
call void @"std_array_list$$int_List_insertAt"(ptr %array, i64 2, i32 300)
call void @"std_array_list$$int_List_insert_at"(ptr %array, i64 2, i32 300)
store i32 0, ptr %i1, align 4
br label %loop.cond2

View File

@@ -140,7 +140,7 @@ fn void main()
array.append(200);
array.append(400);
array.push(600);
array.insertAt(2, 300);
array.insert_at(2, 300);
for (int i = 0; i < (int)(array.len()); i++)
{
printf("Element[%d]: %d\n", i, array.get(i));
@@ -524,7 +524,7 @@ loop.exit: ; preds = %loop.cond
call void @"std_array_list$$int_List_append"(ptr %array, i32 200)
call void @"std_array_list$$int_List_append"(ptr %array, i32 400)
call void @"std_array_list$$int_List_push"(ptr %array, i32 600) #3
call void @"std_array_list$$int_List_insertAt"(ptr %array, i64 2, i32 300)
call void @"std_array_list$$int_List_insert_at"(ptr %array, i64 2, i32 300)
store i32 0, ptr %i1, align 4
br label %loop.cond2
@@ -650,7 +650,7 @@ declare void @"std_array_list$$int_List_append"(ptr, i32)
declare void @"std_array_list$$int_List_push"(ptr, i32)
declare void @"std_array_list$$int_List_insertAt"(ptr, i64, i32)
declare void @"std_array_list$$int_List_insert_at"(ptr, i64, i32)
declare i64 @"std_array_list$$int_List_len"(ptr)

View File

@@ -19,7 +19,7 @@ fn int main(char[][] argv)
stk.push(i);
}
for (;!stk.isEmpty();) {
for (;!stk.is_empty();) {
int value = stk.pop();
printf("%i\n", value);
}