From 56a6e0b112c8ee3f2bea1cdff75a676e431705c9 Mon Sep 17 00:00:00 2001 From: Christoffer Lerno Date: Thu, 21 Jul 2022 22:14:44 +0200 Subject: [PATCH] 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 --- lib/std/list.c3 | 27 ++-- lib/std/priorityqueue.c3 | 130 ++++++++++-------- src/compiler/sema_expr.c | 4 +- src/version.h | 2 +- test/test_suite/functions/test_regression.c3t | 4 +- .../functions/test_regression_mingw.c3t | 6 +- .../methods/extension_method_generic.c3 | 2 +- .../test_suite2/functions/test_regression.c3t | 4 +- .../functions/test_regression_mingw.c3t | 6 +- .../methods/extension_method_generic.c3 | 2 +- 10 files changed, 105 insertions(+), 82 deletions(-) diff --git a/lib/std/list.c3 b/lib/std/list.c3 index c9a52e0e8..a2a77d0e6 100644 --- a/lib/std/list.c3 +++ b/lib/std/list.c3 @@ -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) diff --git a/lib/std/priorityqueue.c3 b/lib/std/priorityqueue.c3 index f6bd7016c..c630c1e68 100644 --- a/lib/std/priorityqueue.c3 +++ b/lib/std/priorityqueue.c3 @@ -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; -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(); } diff --git a/src/compiler/sema_expr.c b/src/compiler/sema_expr.c index 928240b8d..d4eb054fc 100644 --- a/src/compiler/sema_expr.c +++ b/src/compiler/sema_expr.c @@ -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; } diff --git a/src/version.h b/src/version.h index 67d728340..1ba65d899 100644 --- a/src/version.h +++ b/src/version.h @@ -1 +1 @@ -#define COMPILER_VERSION "0.2.19" \ No newline at end of file +#define COMPILER_VERSION "0.2.20" \ No newline at end of file diff --git a/test/test_suite/functions/test_regression.c3t b/test/test_suite/functions/test_regression.c3t index a89d1cb1a..c03597270 100644 --- a/test/test_suite/functions/test_regression.c3t +++ b/test/test_suite/functions/test_regression.c3t @@ -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 diff --git a/test/test_suite/functions/test_regression_mingw.c3t b/test/test_suite/functions/test_regression_mingw.c3t index 7ecf306d8..b171b7d3b 100644 --- a/test/test_suite/functions/test_regression_mingw.c3t +++ b/test/test_suite/functions/test_regression_mingw.c3t @@ -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*) diff --git a/test/test_suite/methods/extension_method_generic.c3 b/test/test_suite/methods/extension_method_generic.c3 index 8b06e02f2..29c452f8d 100644 --- a/test/test_suite/methods/extension_method_generic.c3 +++ b/test/test_suite/methods/extension_method_generic.c3 @@ -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); } diff --git a/test/test_suite2/functions/test_regression.c3t b/test/test_suite2/functions/test_regression.c3t index 1aea53bea..5470a8b63 100644 --- a/test/test_suite2/functions/test_regression.c3t +++ b/test/test_suite2/functions/test_regression.c3t @@ -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 diff --git a/test/test_suite2/functions/test_regression_mingw.c3t b/test/test_suite2/functions/test_regression_mingw.c3t index 1141a44ad..19e5a5fe5 100644 --- a/test/test_suite2/functions/test_regression_mingw.c3t +++ b/test/test_suite2/functions/test_regression_mingw.c3t @@ -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) diff --git a/test/test_suite2/methods/extension_method_generic.c3 b/test/test_suite2/methods/extension_method_generic.c3 index 8b06e02f2..29c452f8d 100644 --- a/test/test_suite2/methods/extension_method_generic.c3 +++ b/test/test_suite2/methods/extension_method_generic.c3 @@ -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); }