aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--main.c8
-rw-r--r--moonshine.h32
2 files changed, 24 insertions, 16 deletions
diff --git a/main.c b/main.c
index 0442b92..3952bd7 100644
--- a/main.c
+++ b/main.c
@@ -5,10 +5,12 @@
int main() {
array(int) test = new (int);
- push(test, 2);
- for(int i = 45; i > 7; i--) push(test, i % 3);
+ int j = 1;
+ for(int i = 100; i >= 0; i--) push(test, i % (j++));
- print("Wowie: ", get(test, 30));
+ foreach(numba in test) {
+ print("Item: ", numba, " index ", numba_index);
+ }
return 0;
}
diff --git a/moonshine.h b/moonshine.h
index b156911..d842fae 100644
--- a/moonshine.h
+++ b/moonshine.h
@@ -798,7 +798,7 @@ void unsafe_push(var *array, const var ptr, const u4 ptr_size) {
const auto main = __array; \
const auto arr = main[ 0 ]; \
const u8 __get_index = __index; \
- if (__get_index >= count(main)) \
+ if (__get_index > count(main)) \
throw("Requested index in array (", __get_index, ") exceeds the size of the array! (", count(main), ")"); \
if (__get_index < 0) throw("Cannot access negative offset in array!"); \
arr[ __get_index ]; \
@@ -808,6 +808,13 @@ void unsafe_push(var *array, const var ptr, const u4 ptr_size) {
(u4) cast_index(cast_ptr(cast_ptr(cast_ptr(*array, byte, -addon_size), magic_type, 1), string, 1), u4, 0)
#define last(array) get(array, count(array) - 1)
+#define in ,
+#define foreach(...) foreach_xp(foreach_inner, (__VA_ARGS__))
+#define foreach_xp(a, b) a b
+#define foreach_inner(item, array) \
+ u4 cat(item, _index) = 0; \
+ for (auto item = get(array, cat(item, _index)++); cat(item, _index) <= count(array); item = get(array, cat(item, _index)++))
+
void puts_number(const int64_t number, const bool is_signed) {
uint64_t n;
if (is_signed && number < 0) {
@@ -983,34 +990,33 @@ __attribute__((noreturn)) void __moonshine_start(int argc, string *argv, string
exit(exit_code);
}
-__attribute__((force_align_arg_pointer)) __attribute__((naked))
-void _start(void) {
- __asm__ volatile (
+__attribute__((force_align_arg_pointer)) __attribute__((naked)) void _start(void) {
+ __asm__ volatile(
// rdi will get argc. The first quadword at rsp is argc.
- "mov (%rsp), %rdi \n" // rdi = argc
+ "mov (%rsp), %rdi \n" // rdi = argc
// rsi will get argv, which is at rsp + 8.
- "lea 8(%rsp), %rsi \n" // rsi = &argv[0]
+ "lea 8(%rsp), %rsi \n" // rsi = &argv[0]
// Compute envp pointer.
// We need to add 8*(argc + 1) to rsp.
// First, copy argc (which is in rdi) into rax.
- "mov %rdi, %rax \n" // rax = argc
- "lea (,%rax,8), %rcx \n" // rcx = argc * 8
- "add $8, %rcx \n" // rcx = 8*(argc + 1)
- "lea (%rsp, %rcx), %rdx \n" // rdx = rsp + 8*(argc+1) -> envp pointer
+ "mov %rdi, %rax \n" // rax = argc
+ "lea (,%rax,8), %rcx \n" // rcx = argc * 8
+ "add $8, %rcx \n" // rcx = 8*(argc + 1)
+ "lea (%rsp, %rcx), %rdx \n" // rdx = rsp + 8*(argc+1) -> envp pointer
// Now call __moonshine_start(argc, argv, envp).
"call __moonshine_start \n"
// If __moonshine_start returns, we exit.
// Exit syscall number 60; exit code 0.
- "mov $60, %rax \n" // syscall: exit
- "xor %rdi, %rdi \n" // rdi = 0 (exit code 0)
+ "mov $60, %rax \n" // syscall: exit
+ "xor %rdi, %rdi \n" // rdi = 0 (exit code 0)
"syscall \n"
// No return. Add an infinite loop as a safeguard.
- "hlt \n" // halt the CPU if syscall fails
+ "hlt \n" // halt the CPU if syscall fails
);
}