From 0b93cd1c9005a8373385c4aac3e4ba6d4f99d859 Mon Sep 17 00:00:00 2001 From: hl-valdemar Date: Mon, 13 Jul 2026 15:02:42 +0200 Subject: [PATCH] toy lexer --- build.hon | 11 + build/honey | Bin 0 -> 68712 bytes ffi/c/posix.hon | 3 + ffi/c/stdio.hon | 420 ++++++++++++++++++++++++++++++++++++ ffi/c/stdlib.hon | 4 + source/main.hon | 166 ++++++++++++++ std/arraylist/arraylist.hon | 67 ++++++ std/build/build.hon | 17 ++ std/io/io.hon | 122 +++++++++++ std/mem/mem.hon | 202 +++++++++++++++++ std/std.hon | 3 + 11 files changed, 1015 insertions(+) create mode 100644 build.hon create mode 100755 build/honey create mode 100644 ffi/c/posix.hon create mode 100644 ffi/c/stdio.hon create mode 100644 ffi/c/stdlib.hon create mode 100644 source/main.hon create mode 100644 std/arraylist/arraylist.hon create mode 100644 std/build/build.hon create mode 100644 std/io/io.hon create mode 100644 std/mem/mem.hon create mode 100644 std/std.hon diff --git a/build.hon b/build.hon new file mode 100644 index 0000000..12b51b2 --- /dev/null +++ b/build.hon @@ -0,0 +1,11 @@ +b :: import "@std/build" + +config :: b.BuildConfig{ + name = "honey", + source = "source", + libraries = &[], + lib_paths = &[], + includes = &[], + defines = &[], + links = &[], +} diff --git a/build/honey b/build/honey new file mode 100755 index 0000000000000000000000000000000000000000..d933678f224f68439279b04cb5ff0a215b903595 GIT binary patch literal 68712 zcmeHv3wT@Ao#&CRL*zIQJ1;vgqF^94koaw18M-JqEDoe?UWNn$7sXa$4YK7(aA zCadXA7&={4G6RziB_ZilGPF$#wBc)a;xy%(ly-M`mD!o$n|0#Qm3iz38@6K=LiYDR z=iI9+Sx!<&$oF;5&3B^vzyEXokKg&9|9M==|M2_wKAFszgW+0i?GNTWGwBK7QJE;47)I3J z7nKA)a^)R^?Idqzm8e5B`DI-NY=?D^8^ef1dpAX_N4fIeY!LDY1Gr@; zp3&(K_V~kik}L1ZMj`KBQJoQOm&eA~*B`l~=q<*jFz*m1w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L eH2eGSKk??RH#MGl_upQxeQDo+3HeI6`TrlJIp{$E literal 0 HcmV?d00001 diff --git a/ffi/c/posix.hon b/ffi/c/posix.hon new file mode 100644 index 0000000..7b4a190 --- /dev/null +++ b/ffi/c/posix.hon @@ -0,0 +1,3 @@ +read c_func(_ c_int, _ ?*mut anyopaque, _ c_ulong) c_long +write c_func(_ c_int, _ ?*anyopaque, _ c_ulong) c_long +__error c_func() *mut c_int diff --git a/ffi/c/stdio.hon b/ffi/c/stdio.hon new file mode 100644 index 0000000..3f64bc9 --- /dev/null +++ b/ffi/c/stdio.hon @@ -0,0 +1,420 @@ +# generated by brolang translate-c from stdio.h + +# unsupported in bindings: C union '__mbstate_t' has no native spelling +__darwin_pthread_handler_rec :: c_struct { + __routine ?*c_func(_ ?*mut anyopaque) void + __arg ?*mut anyopaque + __next ?*mut __darwin_pthread_handler_rec +} +_opaque_pthread_attr_t :: c_struct { + __sig c_long + __opaque [56]c_char +} +_opaque_pthread_cond_t :: c_struct { + __sig c_long + __opaque [40]c_char +} +_opaque_pthread_condattr_t :: c_struct { + __sig c_long + __opaque [8]c_char +} +_opaque_pthread_mutex_t :: c_struct { + __sig c_long + __opaque [56]c_char +} +_opaque_pthread_mutexattr_t :: c_struct { + __sig c_long + __opaque [8]c_char +} +_opaque_pthread_once_t :: c_struct { + __sig c_long + __opaque [8]c_char +} +_opaque_pthread_rwlock_t :: c_struct { + __sig c_long + __opaque [192]c_char +} +_opaque_pthread_rwlockattr_t :: c_struct { + __sig c_long + __opaque [16]c_char +} +_opaque_pthread_t :: c_struct { + __sig c_long + __cleanup_stack ?*mut __darwin_pthread_handler_rec + __opaque [8176]c_char +} +__sbuf :: c_struct { + _base ?*mut c_uchar + _size c_int +} +__sFILEX :: opaque +__sFILE :: c_struct { + _p ?*mut c_uchar + _r c_int + _w c_int + _flags c_short + _file c_short + _bf __sbuf + _lbfsize c_int + _cookie ?*mut anyopaque + _close ?*c_func(_ ?*mut anyopaque) c_int + _read ?*c_func(_ ?*mut anyopaque, _ ?*mut c_char, _ c_int) c_int + _seek ?*c_func(_ ?*mut anyopaque, _ c_longlong, _ c_int) c_longlong + _write ?*c_func(_ ?*mut anyopaque, _ ?*c_char, _ c_int) c_int + _ub __sbuf + _extra ?*mut __sFILEX + _ur c_int + _ubuf [3]c_uchar + _nbuf [1]c_uchar + _lb __sbuf + _blksize c_int + _offset c_longlong +} + +__int8_t :: alias c_schar +__uint8_t :: alias c_uchar +__int16_t :: alias c_short +__uint16_t :: alias c_ushort +__int32_t :: alias c_int +__uint32_t :: alias c_uint +__int64_t :: alias c_longlong +__uint64_t :: alias c_ulonglong +__darwin_intptr_t :: alias c_long +__darwin_natural_t :: alias c_uint +__darwin_ct_rune_t :: alias c_int +__darwin_mbstate_t :: alias __mbstate_t +__darwin_ptrdiff_t :: alias c_long +__darwin_size_t :: alias c_ulong +__darwin_va_list :: alias ?*mut c_char +__darwin_wchar_t :: alias c_int +__darwin_rune_t :: alias c_int +__darwin_wint_t :: alias c_int +__darwin_clock_t :: alias c_ulong +__darwin_socklen_t :: alias c_uint +__darwin_ssize_t :: alias c_long +__darwin_time_t :: alias c_long +__darwin_blkcnt_t :: alias c_longlong +__darwin_blksize_t :: alias c_int +__darwin_dev_t :: alias c_int +__darwin_fsblkcnt_t :: alias c_uint +__darwin_fsfilcnt_t :: alias c_uint +__darwin_gid_t :: alias c_uint +__darwin_id_t :: alias c_uint +__darwin_ino64_t :: alias c_ulonglong +__darwin_ino_t :: alias c_ulonglong +__darwin_mach_port_name_t :: alias c_uint +__darwin_mach_port_t :: alias c_uint +__darwin_mode_t :: alias c_ushort +__darwin_off_t :: alias c_longlong +__darwin_pid_t :: alias c_int +__darwin_sigset_t :: alias c_uint +__darwin_suseconds_t :: alias c_int +__darwin_uid_t :: alias c_uint +__darwin_useconds_t :: alias c_uint +__darwin_uuid_t :: alias [16]c_uchar +__darwin_uuid_string_t :: alias [37]c_char +__darwin_pthread_attr_t :: alias _opaque_pthread_attr_t +__darwin_pthread_cond_t :: alias _opaque_pthread_cond_t +__darwin_pthread_condattr_t :: alias _opaque_pthread_condattr_t +__darwin_pthread_key_t :: alias c_ulong +__darwin_pthread_mutex_t :: alias _opaque_pthread_mutex_t +__darwin_pthread_mutexattr_t :: alias _opaque_pthread_mutexattr_t +__darwin_pthread_once_t :: alias _opaque_pthread_once_t +__darwin_pthread_rwlock_t :: alias _opaque_pthread_rwlock_t +__darwin_pthread_rwlockattr_t :: alias _opaque_pthread_rwlockattr_t +__darwin_pthread_t :: alias ?*mut _opaque_pthread_t +__darwin_nl_item :: alias c_int +__darwin_wctrans_t :: alias c_int +__darwin_wctype_t :: alias c_uint +int8_t :: alias c_schar +int16_t :: alias c_short +int32_t :: alias c_int +int64_t :: alias c_longlong +u_int8_t :: alias c_uchar +u_int16_t :: alias c_ushort +u_int32_t :: alias c_uint +u_int64_t :: alias c_ulonglong +register_t :: alias c_longlong +intptr_t :: alias c_long +uintptr_t :: alias c_ulong +user_addr_t :: alias c_ulonglong +user_size_t :: alias c_ulonglong +user_ssize_t :: alias c_longlong +user_long_t :: alias c_longlong +user_ulong_t :: alias c_ulonglong +user_time_t :: alias c_longlong +user_off_t :: alias c_longlong +syscall_arg_t :: alias c_ulonglong +va_list :: alias ?*mut c_char +size_t :: alias c_ulong +fpos_t :: alias c_longlong +FILE :: alias __sFILE +off_t :: alias c_longlong +ssize_t :: alias c_long + +_DARWIN_FEATURE_64_BIT_INODE c_int :: 1 +_DARWIN_FEATURE_ONLY_64_BIT_INODE c_int :: 1 +_DARWIN_FEATURE_ONLY_VERS_1050 c_int :: 1 +_DARWIN_FEATURE_ONLY_UNIX_CONFORMANCE c_int :: 1 +_DARWIN_FEATURE_UNIX_CONFORMANCE c_int :: 3 +_FORTIFY_SOURCE c_int :: 2 +RENAME_SECLUDE c_int :: 1 +RENAME_SWAP c_int :: 2 +RENAME_EXCL c_int :: 4 +RENAME_RESERVED1 c_int :: 8 +RENAME_NOFOLLOW_ANY c_int :: 16 +RENAME_RESOLVE_BENEATH c_int :: 32 +SEEK_SET c_int :: 0 +SEEK_CUR c_int :: 1 +SEEK_END c_int :: 2 +SEEK_HOLE c_int :: 3 +SEEK_DATA c_int :: 4 +_IOFBF c_int :: 0 +_IOLBF c_int :: 1 +_IONBF c_int :: 2 +BUFSIZ c_int :: 1024 +FOPEN_MAX c_int :: 20 +FILENAME_MAX c_int :: 1024 +L_tmpnam c_int :: 1024 +TMP_MAX c_int :: 308915776 +L_ctermid c_int :: 1024 +_USE_FORTIFY_LEVEL c_int :: 2 + +renameat c_func(_ c_int, _ ?*c_char, _ c_int, _ ?*c_char) c_int +renamex_np c_func(_ ?*c_char, _ ?*c_char, _ c_uint) c_int +renameatx_np c_func(_ c_int, _ ?*c_char, _ c_int, _ ?*c_char, _ c_uint) c_int +printf c_func(_ ?*c_char, ...) c_int +clearerr c_func(_ ?*mut __sFILE) void +fclose c_func(_ ?*mut __sFILE) c_int +feof c_func(_ ?*mut __sFILE) c_int +ferror c_func(_ ?*mut __sFILE) c_int +fflush c_func(_ ?*mut __sFILE) c_int +fgetc c_func(_ ?*mut __sFILE) c_int +fgetpos c_func(_ ?*mut __sFILE, _ ?*mut c_longlong) c_int +fgets c_func(_ ?*mut c_char, __size c_int, _ ?*mut __sFILE) ?*mut c_char +fopen c_func(__filename ?*c_char, __mode ?*c_char) ?*mut __sFILE +fprintf c_func(_ ?*mut __sFILE, _ ?*c_char, ...) c_int +fputc c_func(_ c_int, _ ?*mut __sFILE) c_int +fputs c_func(_ ?*c_char, _ ?*mut __sFILE) c_int +fread c_func(__ptr ?*mut anyopaque, __size c_ulong, __nitems c_ulong, __stream ?*mut __sFILE) c_ulong +freopen c_func(_ ?*c_char, _ ?*c_char, _ ?*mut __sFILE) ?*mut __sFILE +fscanf c_func(_ ?*mut __sFILE, _ ?*c_char, ...) c_int +fseek c_func(_ ?*mut __sFILE, _ c_long, _ c_int) c_int +fsetpos c_func(_ ?*mut __sFILE, _ ?*c_longlong) c_int +ftell c_func(_ ?*mut __sFILE) c_long +fwrite c_func(__ptr ?*anyopaque, __size c_ulong, __nitems c_ulong, __stream ?*mut __sFILE) c_ulong +getc c_func(_ ?*mut __sFILE) c_int +getchar c_func() c_int +gets c_func(_ ?*mut c_char) ?*mut c_char +perror c_func(_ ?*c_char) void +putc c_func(_ c_int, _ ?*mut __sFILE) c_int +putchar c_func(_ c_int) c_int +puts c_func(_ ?*c_char) c_int +remove c_func(_ ?*c_char) c_int +rename c_func(__old ?*c_char, __new ?*c_char) c_int +rewind c_func(_ ?*mut __sFILE) void +scanf c_func(_ ?*c_char, ...) c_int +setbuf c_func(_ ?*mut __sFILE, _ ?*mut c_char) void +setvbuf c_func(_ ?*mut __sFILE, _ ?*mut c_char, _ c_int, __size c_ulong) c_int +sprintf c_func(_ ?*mut c_char, _ ?*c_char, ...) c_int +sscanf c_func(_ ?*c_char, _ ?*c_char, ...) c_int +tmpfile c_func() ?*mut __sFILE +tmpnam c_func(_ ?*mut c_char) ?*mut c_char +ungetc c_func(_ c_int, _ ?*mut __sFILE) c_int +vfprintf c_func(_ ?*mut __sFILE, _ ?*c_char, _ ?*mut c_char) c_int +vprintf c_func(_ ?*c_char, _ ?*mut c_char) c_int +vsprintf c_func(_ ?*mut c_char, _ ?*c_char, _ ?*mut c_char) c_int +ctermid c_func(_ ?*mut c_char) ?*mut c_char +fdopen c_func(_ c_int, _ ?*c_char) ?*mut __sFILE +fileno c_func(_ ?*mut __sFILE) c_int +pclose c_func(_ ?*mut __sFILE) c_int +popen c_func(_ ?*c_char, _ ?*c_char) ?*mut __sFILE +__srget c_func(_ ?*mut __sFILE) c_int +__svfscanf c_func(_ ?*mut __sFILE, _ ?*c_char, _ ?*mut c_char) c_int +__swbuf c_func(_ c_int, _ ?*mut __sFILE) c_int +__sputc c_func(_c c_int, _p ?*mut __sFILE) c_int +flockfile c_func(_ ?*mut __sFILE) void +ftrylockfile c_func(_ ?*mut __sFILE) c_int +funlockfile c_func(_ ?*mut __sFILE) void +getc_unlocked c_func(_ ?*mut __sFILE) c_int +getchar_unlocked c_func() c_int +putc_unlocked c_func(_ c_int, _ ?*mut __sFILE) c_int +putchar_unlocked c_func(_ c_int) c_int +getw c_func(_ ?*mut __sFILE) c_int +putw c_func(_ c_int, _ ?*mut __sFILE) c_int +tempnam c_func(__dir ?*c_char, __prefix ?*c_char) ?*mut c_char +fseeko c_func(__stream ?*mut __sFILE, __offset c_longlong, __whence c_int) c_int +ftello c_func(__stream ?*mut __sFILE) c_longlong +snprintf c_func(__str ?*mut c_char, __size c_ulong, __format ?*c_char, ...) c_int +vfscanf c_func(__stream ?*mut __sFILE, __format ?*c_char, _ ?*mut c_char) c_int +vscanf c_func(__format ?*c_char, _ ?*mut c_char) c_int +vsnprintf c_func(__str ?*mut c_char, __size c_ulong, __format ?*c_char, _ ?*mut c_char) c_int +vsscanf c_func(__str ?*c_char, __format ?*c_char, _ ?*mut c_char) c_int +dprintf c_func(_ c_int, _ ?*c_char, ...) c_int +vdprintf c_func(_ c_int, _ ?*c_char, _ ?*mut c_char) c_int +getdelim c_func(__linep ?*mut ?*mut c_char, __linecapp ?*mut c_ulong, __delimiter c_int, __stream ?*mut __sFILE) c_long +getline c_func(__linep ?*mut ?*mut c_char, __linecapp ?*mut c_ulong, __stream ?*mut __sFILE) c_long +fmemopen c_func(__buf ?*mut anyopaque, __size c_ulong, __mode ?*c_char) ?*mut __sFILE +open_memstream c_func(__bufp ?*mut ?*mut c_char, __sizep ?*mut c_ulong) ?*mut __sFILE +asprintf c_func(_ ?*mut ?*mut c_char, _ ?*c_char, ...) c_int +ctermid_r c_func(_ ?*mut c_char) ?*mut c_char +fgetln c_func(_ ?*mut __sFILE, __len ?*mut c_ulong) ?*mut c_char +fmtcheck c_func(_ ?*c_char, _ ?*c_char) ?*c_char +fpurge c_func(_ ?*mut __sFILE) c_int +setbuffer c_func(_ ?*mut __sFILE, _ ?*mut c_char, __size c_int) void +setlinebuf c_func(_ ?*mut __sFILE) c_int +vasprintf c_func(_ ?*mut ?*mut c_char, _ ?*c_char, _ ?*mut c_char) c_int +funopen c_func(_ ?*anyopaque, _ ?*c_func(_ ?*mut anyopaque, _ ?*mut c_char, _ c_int) c_int, _ ?*c_func(_ ?*mut anyopaque, _ ?*c_char, _ c_int) c_int, _ ?*c_func(_ ?*mut anyopaque, _ c_longlong, _ c_int) c_longlong, _ ?*c_func(_ ?*mut anyopaque) c_int) ?*mut __sFILE +__snprintf_chk c_func(_ ?*mut c_char, __maxlen c_ulong, _ c_int, _ c_ulong, _ ?*c_char, ...) c_int +__vsnprintf_chk c_func(_ ?*mut c_char, __maxlen c_ulong, _ c_int, _ c_ulong, _ ?*c_char, _ ?*mut c_char) c_int +__sprintf_chk c_func(_ ?*mut c_char, _ c_int, _ c_ulong, _ ?*c_char, ...) c_int +__vsprintf_chk c_func(_ ?*mut c_char, _ c_int, _ c_ulong, _ ?*c_char, _ ?*mut c_char) c_int + +# unsupported in bindings: external variable '__stdinp' has no native spelling +# unsupported in bindings: external variable '__stdoutp' has no native spelling +# unsupported in bindings: external variable '__stderrp' has no native spelling +# unsupported in bindings: external variable 'sys_nerr' has no native spelling +# unsupported in bindings: external variable 'sys_errlist' has no native spelling +# unsupported in bindings: _STDIO_H_ — C macro has no replacement value +# unsupported in bindings: _LIBC_BOUNDS_H_ — C macro has no replacement value +# unsupported in bindings: _CDEFS_H_ — C macro has no replacement value +# unsupported in bindings: _LIBC_COUNT — C function-like macros are not supported +# unsupported in bindings: _LIBC_COUNT_OR_NULL — C function-like macros are not supported +# unsupported in bindings: _LIBC_SIZE — C function-like macros are not supported +# unsupported in bindings: _LIBC_SIZE_OR_NULL — C function-like macros are not supported +# unsupported in bindings: _LIBC_ENDED_BY — C function-like macros are not supported +# unsupported in bindings: _LIBC_SINGLE — C macro has no replacement value +# unsupported in bindings: _LIBC_UNSAFE_INDEXABLE — C macro has no replacement value +# unsupported in bindings: _LIBC_CSTR — C macro has no replacement value +# unsupported in bindings: _LIBC_NULL_TERMINATED — C macro has no replacement value +# unsupported in bindings: _LIBC_FLEX_COUNT — C function-like macros are not supported +# unsupported in bindings: _LIBC_SINGLE_BY_DEFAULT — C function-like macros are not supported +# unsupported in bindings: _LIBC_PTRCHECK_REPLACED — C function-like macros are not supported +# unsupported in bindings: _LIBC_FORGE_PTR — C function-like macros are not supported +# unsupported in bindings: MAC_OS_X_VERSION_10_0 — C macro is not a supported constant +# unsupported in bindings: MAC_OS_X_VERSION_10_1 — C macro is not a supported constant +# unsupported in bindings: MAC_OS_X_VERSION_10_2 — C macro is not a supported constant +# unsupported in bindings: MAC_OS_X_VERSION_10_3 — C macro is not a supported constant +# unsupported in bindings: MAC_OS_X_VERSION_10_4 — C macro is not a supported constant +# unsupported in bindings: MAC_OS_X_VERSION_10_5 — C macro is not a supported constant +# unsupported in bindings: MAC_OS_X_VERSION_10_6 — C macro is not a supported constant +# unsupported in bindings: MAC_OS_X_VERSION_10_7 — C macro is not a supported constant +# unsupported in bindings: MAC_OS_X_VERSION_10_8 — C macro is not a supported constant +# unsupported in bindings: MAC_OS_X_VERSION_10_9 — C macro is not a supported constant +# unsupported in bindings: MAC_OS_X_VERSION_10_10 — C macro is not a supported constant +# unsupported in bindings: MAC_OS_X_VERSION_10_10_2 — C macro is not a supported constant +# unsupported in bindings: MAC_OS_X_VERSION_10_10_3 — C macro is not a supported constant +# unsupported in bindings: MAC_OS_X_VERSION_10_11 — C macro is not a supported constant +# unsupported in bindings: MAC_OS_X_VERSION_10_11_2 — C macro is not a supported constant +# unsupported in bindings: MAC_OS_X_VERSION_10_11_3 — C macro is not a supported constant +# unsupported in bindings: MAC_OS_X_VERSION_10_11_4 — C macro is not a supported constant +# unsupported in bindings: MAC_OS_X_VERSION_10_12 — C macro is not a supported constant +# unsupported in bindings: MAC_OS_X_VERSION_10_12_1 — C macro is not a supported constant +# unsupported in bindings: MAC_OS_X_VERSION_10_12_2 — C macro is not a supported constant +# unsupported in bindings: MAC_OS_X_VERSION_10_12_4 — C macro is not a supported constant +# unsupported in bindings: MAC_OS_X_VERSION_10_13 — C macro is not a supported constant +# unsupported in bindings: MAC_OS_X_VERSION_10_13_1 — C macro is not a supported constant +# unsupported in bindings: MAC_OS_X_VERSION_10_13_2 — C macro is not a supported constant +# unsupported in bindings: MAC_OS_X_VERSION_10_13_4 — C macro is not a supported constant +# unsupported in bindings: MAC_OS_X_VERSION_10_14 — C macro is not a supported constant +# unsupported in bindings: MAC_OS_X_VERSION_10_14_1 — C macro is not a supported constant +# unsupported in bindings: MAC_OS_X_VERSION_10_14_4 — C macro is not a supported constant +# unsupported in bindings: MAC_OS_X_VERSION_10_14_5 — C macro is not a supported constant +# unsupported in bindings: MAC_OS_X_VERSION_10_14_6 — C macro is not a supported constant +# unsupported in bindings: MAC_OS_X_VERSION_10_15 — C macro is not a supported constant +# unsupported in bindings: MAC_OS_X_VERSION_10_15_1 — C macro is not a supported constant +# unsupported in bindings: MAC_OS_X_VERSION_10_15_4 — C macro is not a supported constant +# unsupported in bindings: MAC_OS_X_VERSION_10_16 — C macro is not a supported constant +# unsupported in bindings: MAC_OS_VERSION_11_0 — C macro is not a supported constant +# unsupported in bindings: MAC_OS_VERSION_11_1 — C macro is not a supported constant +# unsupported in bindings: MAC_OS_VERSION_11_3 — C macro is not a supported constant +# unsupported in bindings: MAC_OS_VERSION_11_4 — C macro is not a supported constant +# unsupported in bindings: MAC_OS_VERSION_11_5 — C macro is not a supported constant +# unsupported in bindings: MAC_OS_VERSION_11_6 — C macro is not a supported constant +# unsupported in bindings: MAC_OS_VERSION_12_0 — C macro is not a supported constant +# unsupported in bindings: MAC_OS_VERSION_12_1 — C macro is not a supported constant +# unsupported in bindings: MAC_OS_VERSION_12_2 — C macro is not a supported constant +# unsupported in bindings: MAC_OS_VERSION_12_3 — C macro is not a supported constant +# unsupported in bindings: MAC_OS_VERSION_12_4 — C macro is not a supported constant +# unsupported in bindings: MAC_OS_VERSION_12_5 — C macro is not a supported constant +# unsupported in bindings: MAC_OS_VERSION_12_6 — C macro is not a supported constant +# unsupported in bindings: MAC_OS_VERSION_12_7 — C macro is not a supported constant +# unsupported in bindings: MAC_OS_VERSION_13_0 — C macro is not a supported constant +# unsupported in bindings: MAC_OS_VERSION_13_1 — C macro is not a supported constant +# unsupported in bindings: MAC_OS_VERSION_13_2 — C macro is not a supported constant +# unsupported in bindings: MAC_OS_VERSION_13_3 — C macro is not a supported constant +# unsupported in bindings: MAC_OS_VERSION_13_4 — C macro is not a supported constant +# unsupported in bindings: MAC_OS_VERSION_13_5 — C macro is not a supported constant +# unsupported in bindings: MAC_OS_VERSION_13_6 — C macro is not a supported constant +# unsupported in bindings: MAC_OS_VERSION_13_7 — C macro is not a supported constant +# unsupported in bindings: MAC_OS_VERSION_14_0 — C macro is not a supported constant +# unsupported in bindings: MAC_OS_VERSION_14_1 — C macro is not a supported constant +# unsupported in bindings: MAC_OS_VERSION_14_2 — C macro is not a supported constant +# unsupported in bindings: MAC_OS_VERSION_14_3 — C macro is not a supported constant +# unsupported in bindings: MAC_OS_VERSION_14_4 — C macro is not a supported constant +# unsupported in bindings: MAC_OS_VERSION_14_5 — C macro is not a supported constant +# unsupported in bindings: MAC_OS_VERSION_14_6 — C macro is not a supported constant +# unsupported in bindings: MAC_OS_VERSION_14_7 — C macro is not a supported constant +# unsupported in bindings: MAC_OS_VERSION_15_0 — C macro is not a supported constant +# unsupported in bindings: MAC_OS_VERSION_15_1 — C macro is not a supported constant +# unsupported in bindings: MAC_OS_VERSION_15_2 — C macro is not a supported constant +# unsupported in bindings: MAC_OS_VERSION_15_3 — C macro is not a supported constant +# unsupported in bindings: MAC_OS_VERSION_15_4 — C macro is not a supported constant +# unsupported in bindings: MAC_OS_VERSION_15_5 — C macro is not a supported constant +# unsupported in bindings: MAC_OS_VERSION_15_6 — C macro is not a supported constant +# unsupported in bindings: MAC_OS_VERSION_16_0 — C macro is not a supported constant +# unsupported in bindings: MAC_OS_VERSION_26_0 — C macro is not a supported constant +# unsupported in bindings: MAC_OS_VERSION_26_1 — C macro is not a supported constant +# unsupported in bindings: MAC_OS_VERSION_26_2 — C macro is not a supported constant +# unsupported in bindings: MAC_OS_VERSION_26_3 — C macro is not a supported constant +# unsupported in bindings: MAC_OS_VERSION_26_4 — C macro is not a supported constant +# unsupported in bindings: _SYS__TYPES_H_ — C macro has no replacement value +# unsupported in bindings: _BSD_MACHINE__TYPES_H_ — C macro has no replacement value +# unsupported in bindings: _BSD_ARM__TYPES_H_ — C macro has no replacement value +# unsupported in bindings: _SYS__PTHREAD_TYPES_H_ — C macro has no replacement value +# unsupported in bindings: _VA_LIST_T — C macro has no replacement value +# unsupported in bindings: _BSD_MACHINE_TYPES_H_ — C macro has no replacement value +# unsupported in bindings: _ARM_MACHTYPES_H_ — C macro has no replacement value +# unsupported in bindings: _MACHTYPES_H_ — C macro has no replacement value +# unsupported in bindings: _INT8_T — C macro has no replacement value +# unsupported in bindings: _INT16_T — C macro has no replacement value +# unsupported in bindings: _INT32_T — C macro has no replacement value +# unsupported in bindings: _INT64_T — C macro has no replacement value +# unsupported in bindings: _U_INT8_T — C macro has no replacement value +# unsupported in bindings: _U_INT16_T — C macro has no replacement value +# unsupported in bindings: _U_INT32_T — C macro has no replacement value +# unsupported in bindings: _U_INT64_T — C macro has no replacement value +# unsupported in bindings: _INTPTR_T — C macro has no replacement value +# unsupported in bindings: _UINTPTR_T — C macro has no replacement value +# unsupported in bindings: USER_ADDR_NULL — C macro is not a supported constant +# unsupported in bindings: CAST_USER_ADDR_T — C function-like macros are not supported +# unsupported in bindings: _SIZE_T — C macro has no replacement value +# unsupported in bindings: NULL — C macro is not a supported constant +# unsupported in bindings: _SYS_STDIO_H_ — C macro has no replacement value +# unsupported in bindings: _FSTDIO — C macro has no replacement value +# unsupported in bindings: _SEEK_SET_H_ — C macro has no replacement value +# unsupported in bindings: EOF — C macro is not a supported constant +# unsupported in bindings: P_tmpdir — C macro is not a supported constant +# unsupported in bindings: stdin — C macro is not a supported constant +# unsupported in bindings: stdout — C macro is not a supported constant +# unsupported in bindings: stderr — C macro is not a supported constant +# unsupported in bindings: _LIBC_COUNT__L_CTERMID — C macro is not a supported constant +# unsupported in bindings: _CTERMID_H_ — C macro has no replacement value +# unsupported in bindings: getc_unlocked — C function-like macros are not supported +# unsupported in bindings: putc_unlocked — C function-like macros are not supported +# unsupported in bindings: getchar_unlocked — C function-like macros are not supported +# unsupported in bindings: putchar_unlocked — C function-like macros are not supported +# unsupported in bindings: _OFF_T — C macro has no replacement value +# unsupported in bindings: _SSIZE_T — C macro has no replacement value +# unsupported in bindings: fropen — C function-like macros are not supported +# unsupported in bindings: fwopen — C function-like macros are not supported +# unsupported in bindings: feof_unlocked — C function-like macros are not supported +# unsupported in bindings: ferror_unlocked — C function-like macros are not supported +# unsupported in bindings: clearerr_unlocked — C function-like macros are not supported +# unsupported in bindings: fileno_unlocked — C function-like macros are not supported +# unsupported in bindings: _SECURE__STDIO_H_ — C macro has no replacement value +# unsupported in bindings: _SECURE__COMMON_H_ — C macro has no replacement value +# unsupported in bindings: sprintf — C function-like macros are not supported +# unsupported in bindings: vsprintf — C function-like macros are not supported +# unsupported in bindings: snprintf — C function-like macros are not supported +# unsupported in bindings: vsnprintf — C function-like macros are not supported diff --git a/ffi/c/stdlib.hon b/ffi/c/stdlib.hon new file mode 100644 index 0000000..c2a0b3b --- /dev/null +++ b/ffi/c/stdlib.hon @@ -0,0 +1,4 @@ +malloc c_func(__size c_ulong) ?*mut anyopaque +realloc c_func(__ptr ?*mut anyopaque, __size c_ulong) ?*mut anyopaque +free c_func(_ ?*mut anyopaque) void +posix_memalign c_func(__memptr ?*mut ?*mut anyopaque, __alignment c_ulong, __size c_ulong) c_int diff --git a/source/main.hon b/source/main.hon new file mode 100644 index 0000000..c4889eb --- /dev/null +++ b/source/main.hon @@ -0,0 +1,166 @@ +import "@ffi/c" +import "@std" +import "@std/mem" +import "@std/arraylist" + +TokenKind :: enum { + ident + int + float + string + + equal + double_colon + + left_paren + right_paren + left_curly + right_curly + + newline + + invalid + eof +} + +Token :: struct { + kind TokenKind + start int +} + +_token_kind_str func(kind TokenKind) *c_char { + return match kind { + .ident: "ident" + .int: "int" + .float: "float" + .string: "string" + .equal: "equal" + .double_colon: "double_colon" + .left_paren: "left_paren" + .right_paren: "right_paren" + .left_curly: "left_curly" + .right_curly: "right_curly" + .newline: "newline" + .invalid: "invalid" + .eof: "eof" + } +} + +_is_whitespace func(char u8) bool { + return char == ' ' or char == '\t' or char == '\n' or char == '\r' +} + +_is_alpha func(char u8) bool { + return match char { + 'a'..'z', 'A'..'Z': true + else: false + } +} + +_is_digit func(char u8) bool { + return match char { + '0'..'9': true + else: false + } +} + +scan func(tokens @mut std.ArrayList(Token), input []u8) void ! mem.AllocError { + cursor usize = 0 + while cursor < input.len { + char :: input[cursor] + + # whitespace + if char == '\n' { + try arraylist.append(tokens, Token{ kind = .newline, start = cursor }) + cursor += 1 + } else if _is_whitespace(char) { + cursor += 1 + continue + } + + # comments + if char == '#' { + while cursor < input.len and input[cursor] != '\n' : cursor += 1 {} + } + + # identifiers and keywords + if _is_alpha(char) or char == '_' { + start :: cursor + cursor += 1 + while cursor < input.len and (_is_alpha(input[cursor]) or _is_digit(input[cursor]) or input[cursor] == '_') { + cursor += 1 + } + try arraylist.append(tokens, Token{ kind = .ident, start = start }) + continue + } + + # integers literals + if _is_digit(char) { + start :: cursor + cursor += 1 + while cursor < input.len and _is_digit(input[cursor]) { + cursor += 1 + } + try arraylist.append(tokens, Token{ kind = .int, start = start }) + continue + } + + # string literals + if char == '"' { + start :: cursor + cursor += 1 + + while cursor < input.len and input[cursor] != '"' and input[cursor] != '\n' : cursor += 1 { + # ignore escaped characters + if (input[cursor] == '\\' and cursor + 1 < input.len) cursor += 1 + } + + if cursor < input.len and input[cursor] == '"' { + cursor += 1 + try arraylist.append(tokens, Token{ kind = .string, start = start }) + } else { + try arraylist.append(tokens, Token{ kind = .invalid, start = start }) + } + + continue + } + + # mutable assignment + if char == '=' { + try arraylist.append(tokens, Token{ kind = .equal, start = cursor }) + cursor += 1 + continue + } + + # immutable assignment + cursor += 1 + if cursor < input.len and char == ':' and input[cursor] == ':' { + try arraylist.append(tokens, Token{ kind = .double_colon, start = cursor }) + cursor += 1 + continue + } + + # invalid character + try arraylist.append(tokens, Token{ kind = .invalid, start = cursor }) + } + try arraylist.append(tokens, Token{ kind = .eof, start = cursor }) +} + +program :: + `# these are immutable + `x :: 32 + `y :: 3.2 + +main func() void { + tokens std.ArrayList(Token) = arraylist.init(mem.c_allocator) + defer arraylist.deinit(&tokens) + + scan(&tokens, program) catch |_| { + _ = c.printf("failed to scan: out of memory\n") + return _ + } + + for tokens.items |token| { + _ = c.printf("%s\n", _token_kind_str(token.kind)) + } +} diff --git a/std/arraylist/arraylist.hon b/std/arraylist/arraylist.hon new file mode 100644 index 0000000..95cc061 --- /dev/null +++ b/std/arraylist/arraylist.hon @@ -0,0 +1,67 @@ +mem :: import "@std/mem" + +ArrayList func($T type) type { + return struct { + items []mut T + capacity usize + allocator mem.Allocator + } +} + +init func($T type, allocator mem.Allocator) ArrayList(T) { + return ArrayList(T) { + items = mem.empty(T), + capacity = 0, + allocator = allocator, + } +} + +deinit func($T type, list @mut ArrayList(T)) void { + allocation []mut T :: list.items.ptr[..list.capacity] + mem.free(list.allocator, allocation) + list.items = mem.empty(T) + list.capacity = 0 +} + +reserve func($T type, list @mut ArrayList(T), minimum_capacity usize) void ! mem.AllocError { + if minimum_capacity <= list.capacity { + return _ + } + + new_capacity usize = 8 + if list.capacity >= 8 { + half usize :: div_trunc(list.capacity, 2) + if list.capacity > max_value(usize) - half { + new_capacity = minimum_capacity + } else { + new_capacity = list.capacity + half + } + } + if new_capacity < minimum_capacity { + new_capacity = minimum_capacity + } + + length usize :: list.items.len + allocation []mut T :: list.items.ptr[..list.capacity] + grown []mut T :: mem.realloc(list.allocator, allocation, new_capacity) catch |_| { + return .out_of_memory + } + list.items = grown.ptr[..length] + list.capacity = new_capacity + return _ +} + +append func($T type, list @mut ArrayList(T), value T) void ! mem.AllocError { + length usize :: list.items.len + if length == max_value(usize) { + return .out_of_memory + } + try reserve(list, length + 1) + list.items = list.items.ptr[..length + 1] + list.items[length] = value + return _ +} + +clear func($T type, list @mut ArrayList(T)) void { + list.items = list.items.ptr[..0] +} diff --git a/std/build/build.hon b/std/build/build.hon new file mode 100644 index 0000000..fad3c9e --- /dev/null +++ b/std/build/build.hon @@ -0,0 +1,17 @@ +# Build configuration surface for `brolang build` (v0). +# +# A project's `build.bro` imports this module and declares a top-level constant +# named `config` of type `BuildConfig`. `brolang build [root]` type-checks +# build.bro, reads the config, and writes root/build/name. +# +# Declarative and literal-only: one executable per build. List fields take an +# address-of an array literal (`&["raylib"]`); empty lists are written `&[]`. +BuildConfig :: struct { + name []u8 # output executable name under root/build + source []u8 # program package directory, relative to build.bro + libraries [][]u8 # library names to link (-l) + lib_paths [][]u8 # library search directories (-L) + includes [][]u8 # C include directories (-I) + defines [][]u8 # C preprocessor defines (name or name=value) + links [][]u8 # extra linker inputs (object/source files, -framework pairs) +} diff --git a/std/io/io.hon b/std/io/io.hon new file mode 100644 index 0000000..bf51ec9 --- /dev/null +++ b/std/io/io.hon @@ -0,0 +1,122 @@ +c :: import "@ffi/c" + +ReadError :: enum { + read_failed +} + +WriteError :: enum { + write_failed + no_progress +} + +Io :: struct { + context ?*mut anyopaque + vtable @IoVTable +} + +IoVTable :: struct { + read @func(context ?*mut anyopaque, stream ReadStream, buffer []mut u8) usize ! ReadError + write @func(context ?*mut anyopaque, stream WriteStream, bytes []u8) usize ! WriteError +} + +ReadStream :: enum(c_int) { + stdin = 0 +} + +WriteStream :: enum(c_int) { + stdout = 1 + stderr = 2 +} + +Reader :: struct { + impl Io + stream ReadStream +} + +Writer :: struct { + impl Io + stream WriteStream +} + +read func(reader Reader, buffer []mut u8) usize ! ReadError { + if buffer.len == 0 { + return 0 + } + count usize :: try reader.impl.vtable.read(reader.impl.context, reader.stream, buffer) + if count > buffer.len { + return .read_failed + } + return count +} + +write func(writer Writer, bytes []u8) usize ! WriteError { + if bytes.len == 0 { + return 0 + } + count usize :: try writer.impl.vtable.write(writer.impl.context, writer.stream, bytes) + if count > bytes.len { + return .write_failed + } + return count +} + +write_all func(writer Writer, bytes []u8) void ! WriteError { + offset usize = 0 + while offset < bytes.len { + count usize :: write(writer, bytes[offset..]) catch |err| { + return err + } + if count == 0 { + return .no_progress + } + offset += count + } + return _ +} + +_system_read func(_ ?*mut anyopaque, stream ReadStream, buffer []mut u8) usize ! ReadError { + request usize = buffer.len + maximum usize :: usize(max_value(c_long)) + if request > maximum { + request = maximum + } + while true { + count c_long :: c.read(c_int(stream), buffer.ptr, c_ulong(request)) + if count >= 0 { + return usize(count) + } + if c.__error()^ != 4 { + return .read_failed + } + } +} + +_system_write func(_ ?*mut anyopaque, stream WriteStream, bytes []u8) usize ! WriteError { + fd c_int :: c_int(stream) + request usize = bytes.len + maximum usize :: usize(max_value(c_long)) + if request > maximum { + request = maximum + } + while true { + count c_long :: c.write(fd, bytes.ptr, c_ulong(request)) + if count >= 0 { + return usize(count) + } + if c.__error()^ != 4 { + return .write_failed + } + } +} + +_system_vtable IoVTable :: IoVTable { + read = _system_read, + write = _system_write, +} + +_system func() Io { + return Io { + context = none, + vtable = &_system_vtable, + } +} diff --git a/std/mem/mem.hon b/std/mem/mem.hon new file mode 100644 index 0000000..49450ed --- /dev/null +++ b/std/mem/mem.hon @@ -0,0 +1,202 @@ +c :: import "@ffi/c" + +AllocError :: enum { + out_of_memory +} + +Allocator :: struct { + context ?*mut anyopaque + vtable @AllocatorVTable +} + +AllocatorVTable :: struct { + alloc @func(context ?*mut anyopaque, size usize, alignment usize) ?*mut u8 + realloc @func(context ?*mut anyopaque, memory ?*mut u8, old_size usize, new_size usize, alignment usize) ?*mut u8 + free @func(context ?*mut anyopaque, memory ?*mut u8, size usize, alignment usize) void +} + +raw_alloc func(allocator Allocator, size usize, alignment usize) ?*mut u8 { + return allocator.vtable.alloc(allocator.context, size, alignment) +} + +raw_realloc func(allocator Allocator, memory ?*mut u8, old_size usize, new_size usize, alignment usize) ?*mut u8 { + return allocator.vtable.realloc(allocator.context, memory, old_size, new_size, alignment) +} + +raw_free func(allocator Allocator, memory ?*mut u8, size usize, alignment usize) void { + allocator.vtable.free(allocator.context, memory, size, alignment) +} + +eql func($T type, left, right []T) bool { + if left.len != right.len { + return false + } + + i usize = 0 + while i < left.len : i += 1 { + if left[i] != right[i] { + return false + } + } + return true +} + +_empty_storage [1]mut u64 = [0] + +_empty_slice func($T type, count usize) []mut T { + pointer *mut T :: ptr_cast(T, (&_empty_storage).ptr) + return pointer[..count] +} + +empty func($T type) []mut T { + return _empty_slice(T, 0) +} + +alloc func($T type, allocator Allocator, count usize) []mut T ! AllocError { + if count == 0 { + return _empty_slice(T, 0) + } + + element_size usize :: size_of(T) + if element_size == 0 { + return _empty_slice(T, count) + } + if count > div_trunc(max_value(usize), element_size) { + return .out_of_memory + } + + memory ?*mut u8 = raw_alloc(allocator, count * element_size, align_of(T)) + if memory |bytes| { + pointer *mut T :: ptr_cast(T, bytes) + return pointer[..count] + } + return .out_of_memory +} + +realloc func($T type, allocator Allocator, memory []mut T, new_count usize) []mut T ! AllocError { + if new_count == memory.len { + return memory + } + if new_count == 0 { + free(allocator, memory) + return _empty_slice(T, 0) + } + + element_size usize :: size_of(T) + if element_size == 0 { + return _empty_slice(T, new_count) + } + if new_count > div_trunc(max_value(usize), element_size) { + return .out_of_memory + } + + old_memory ?*mut u8 = none + old_size usize = 0 + if memory.len != 0 { + old_memory = ptr_cast(u8, memory.ptr) + old_size = memory.len * element_size + } + resized ?*mut u8 = raw_realloc( + allocator, + old_memory, + old_size, + new_count * element_size, + align_of(T), + ) + if resized |bytes| { + pointer *mut T :: ptr_cast(T, bytes) + return pointer[..new_count] + } + return .out_of_memory +} + +free func($T type, allocator Allocator, memory []mut T) void { + if memory.len != 0 and size_of(T) != 0 { + raw_free(allocator, ptr_cast(u8, memory.ptr), memory.len * size_of(T), align_of(T)) + } +} + +_malloc_alignment usize :: 16 # ponytail: aarch64-macos libc malloc alignment assumption. + +_power_of_two func(value usize) bool { + if value == 0 { + return false + } + + current usize = value + while current > 1 { + half usize = div_trunc(current, 2) + if half * 2 != current { + return false + } + current = half + } + + return true +} + +_c_alloc func(_ ?*mut anyopaque, size usize, alignment usize) ?*mut u8 { + if _power_of_two(alignment) == false { + return none + } + + if alignment <= _malloc_alignment { + return ptr_cast(u8, c.malloc(c_ulong(size))) + } + + memory [1]mut ?*mut anyopaque = [none] + status c_int = c.posix_memalign((&memory).ptr, c_ulong(alignment), c_ulong(size)) + if status != 0 { + return none + } + + return ptr_cast(u8, memory[0]) +} + +_c_realloc func(_ ?*mut anyopaque, memory ?*mut u8, old_size usize, new_size usize, alignment usize) ?*mut u8 { + if _power_of_two(alignment) == false { + return none + } + + if new_size == 0 { + c.free(memory) + return none + } + + if memory |old_memory| { + if alignment <= _malloc_alignment { + return ptr_cast(u8, c.realloc(old_memory, c_ulong(new_size))) + } + + new_memory ?*mut u8 = _c_alloc(none, new_size, alignment) + if new_memory |new_bytes| { + copy_size usize = old_size + if new_size < copy_size { + copy_size = new_size + } + i usize = 0 + while i < copy_size : i += 1 { + new_bytes[i] = old_memory[i] + } + c.free(old_memory) + } + return new_memory + } + + return _c_alloc(none, new_size, alignment) +} + +_c_free func(_ ?*mut anyopaque, memory ?*mut u8, _ usize, _ usize) void { + c.free(memory) +} + +_c_vtable AllocatorVTable :: AllocatorVTable { + alloc = _c_alloc, + realloc = _c_realloc, + free = _c_free, +} + +c_allocator Allocator :: Allocator { + context = none, + vtable = &_c_vtable, +} diff --git a/std/std.hon b/std/std.hon new file mode 100644 index 0000000..2e49663 --- /dev/null +++ b/std/std.hon @@ -0,0 +1,3 @@ +import "arraylist" + +ArrayList :: alias arraylist.ArrayList