Skip to content

Commit

Permalink
feat(rivet+rivetc): implement basic comptime if (#56)
Browse files Browse the repository at this point in the history
The comptime `if` feature is added, and the previously used preprocessor is removed.

This comptime `if` is basic, and has access to flags defined with the `-D` option and 
symbols that already worked in the preprocessor.
  • Loading branch information
StunxFS authored Dec 25, 2023
1 parent 6f5d79b commit d3e6d64
Show file tree
Hide file tree
Showing 57 changed files with 1,302 additions and 1,253 deletions.
4 changes: 1 addition & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,6 @@ quality.
* **Not NULL values by default**: This is only possible with option types (`?T`) and `none`.
* **Easy error handling**: With result types, `func my_func() -> !T { ... }`,
`throw` and `catch`.
* **A basic preprocessor**: `if`, `else_if`, `else` and `endif` for optional code using
flags (`-D my_flag`).
* **Immutable values**: Variables and fields are immutable by default.
structs have internal immutability.
* **Polymorphism**: Traits, Embedded Structs and Tagged Enums are supported.
Expand Down Expand Up @@ -88,4 +86,4 @@ successfully.
Only linux is supported for now. Windows is not well supported, and macOS is not supported
yet. Any help to provide full support for both Windows and macOS is welcome.

Read [CONTRIBUTING](CONTRIBUTING.md) to get more information.
Read [CONTRIBUTING](CONTRIBUTING.md) to get more information.
16 changes: 7 additions & 9 deletions lib/c/src/errno.c.ri
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// Copyright (C) 2023-present Jose Mendoza - All rights reserved. Use of this
// source code is governed by an MIT license that can be found in the LICENSE
// Copyright (C) 2023-present Jose Mendoza - All rights reserved. Use of this
// source code is governed by an MIT license that can be found in the LICENSE
// file.

import c/libc;
Expand All @@ -16,13 +16,11 @@ pub struct ErrnoError < Throwable {

#[inline]
pub func errno() -> int32 {
return unsafe {
#if _LINUX_
libc.__errno_location()?.*
#else
libc._errno().*
#endif
};
comptime if _LINUX_ {
return unsafe { libc.__errno_location()?.* };
} else {
return unsafe { libc._errno().* };
}
}

#[inline]
Expand Down
36 changes: 18 additions & 18 deletions lib/c/src/libc/errno.ri
Original file line number Diff line number Diff line change
@@ -1,24 +1,24 @@
// Copyright (C) 2023-present Jose Mendoza - All rights reserved. Use of this
// source code is governed by an MIT license that can be found in the LICENSE
// Copyright (C) 2023-present Jose Mendoza - All rights reserved. Use of this
// source code is governed by an MIT license that can be found in the LICENSE
// file.

#if _LINUX_
extern (C) {
pub func __errno_location() -> ?&mut int32;
comptime if _LINUX_ {
extern (C) {
pub func __errno_location() -> ?&mut int32;
}

pub const EDOM := 33;
pub const EILSEQ := 84;
pub const ERANGE := 34;
}

pub const EDOM := 33;
pub const EILSEQ := 84;
pub const ERANGE := 34;
#endif
comptime if _WINDOWS_ {
extern (C) {
#[dllimport]
pub func _errno() -> &mut int32;
}

#if _WINDOWS_
extern (C) {
[dllimport]
pub func _errno() &mut int32;
pub const EDOM := 33;
pub const EILSEQ := 42;
pub const ERANGE := 34;
}

pub const EDOM := 33;
pub const EILSEQ := 42;
pub const ERANGE := 34;
#endif
22 changes: 11 additions & 11 deletions lib/c/src/libc/signal.ri
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// Copyright (C) 2023-present Jose Mendoza - All rights reserved. Use of this
// source code is governed by an MIT license that can be found in the LICENSE
// Copyright (C) 2023-present Jose Mendoza - All rights reserved. Use of this
// source code is governed by an MIT license that can be found in the LICENSE
// file.

// TODO(StunxFS): replace the argument type of `SignalHandler` from
Expand All @@ -9,7 +9,7 @@ import ../ctypes.{ pid_t, uid_t };

pub alias SignalHandler := func(int32, &mut siginfo_t, rawptr);

#if _LINUX_
comptime if _LINUX_ {
pub const SA_ONSTACK := 0x08000000;
pub const SA_SIGINFO := 0x00000004;
pub const SA_NOCLDWAIT := 0x00000002;
Expand Down Expand Up @@ -50,7 +50,7 @@ pub alias SignalHandler := func(int32, &mut siginfo_t, rawptr);
pub const SEGV_ACCERR := 0x2;

pub struct sigset_t {
pub __val: [#if _X86_ 32 #else 16 #endif]uint32;
pub __val: [comptime if _X86_ { 32 } else { 16 }]uint32;
}

pub struct sigval {
Expand All @@ -76,21 +76,21 @@ pub alias SignalHandler := func(int32, &mut siginfo_t, rawptr);
pub sa_flags: int32;
pub sa_restorer: func();
}
#else_if _WINDOWS_
} else if _WINDOWS_ {
pub const SIGABRT := 22;
pub const SIGFPE := 8;
pub const SIGILL := 4;
pub const SIGINT := 2;
pub const SIGSEGV := 11;
pub const SIGTERM := 15;
#else
#error cannot define values for signals in this operating system, please report the issue
#endif
} else {
// TODO: error cannot define values for signals in this operating system, please report the issue
}

extern (C) {
#if _LINUX_
pub func sigaction(signum: int32, act: ?&sigaction_t, old_act: ?&mut sigaction_t) -> int32;
#endif
comptime if _LINUX_ {
pub func sigaction(signum: int32, act: ?&sigaction_t, old_act: ?&mut sigaction_t) -> int32;
}
pub func signal(sig: int32, handler: SignalHandler) -> ?SignalHandler;
pub func raise(sig: int32) -> int32;
}
42 changes: 21 additions & 21 deletions lib/c/src/libc/stat.ri
Original file line number Diff line number Diff line change
@@ -1,31 +1,31 @@
// Copyright (C) 2023-present Jose Mendoza - All rights reserved. Use of this
// source code is governed by an MIT license that can be found in the LICENSE
// Copyright (C) 2023-present Jose Mendoza - All rights reserved. Use of this
// source code is governed by an MIT license that can be found in the LICENSE
// file.

import c/ctypes.*;

#if _AMD64_
// The `stat` definition used by the Linux kernel.
pub struct Stat {
pub dev: dev_t;
pub ino: ino_t;
pub nlink: uint;
comptime if _AMD64_ {
// The `stat` definition used by the Linux kernel.
pub struct Stat {
pub dev: dev_t;
pub ino: ino_t;
pub nlink: uint;

pub mode: uint32;
pub uid: uid_t;
pub gid: gid_t;
__pad0: uint32;
pub rdev: dev_t;
pub size: off_t;
pub blksize: int;
pub blocks: int64;
pub mode: uint32;
pub uid: uid_t;
pub gid: gid_t;
__pad0: uint32;
pub rdev: dev_t;
pub size: off_t;
pub blksize: int;
pub blocks: int64;

pub atim: timespec;
pub mtim: timespec;
pub ctim: timespec;
__unused: [3]int;
pub atim: timespec;
pub mtim: timespec;
pub ctim: timespec;
__unused: [3]int;
}
}
#endif

extern (C) {
pub func stat(__file: [&]uint8, __buf: &mut Stat) -> int32;
Expand Down
14 changes: 7 additions & 7 deletions lib/c/src/libc/stdio.ri
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// Copyright (C) 2023-present Jose Mendoza - All rights reserved. Use of this
// source code is governed by an MIT license that can be found in the LICENSE
// Copyright (C) 2023-present Jose Mendoza - All rights reserved. Use of this
// source code is governed by an MIT license that can be found in the LICENSE
// file.

import c/ctypes.*;
Expand Down Expand Up @@ -35,7 +35,7 @@ pub const O_CLOEXEC := 0x80000;

pub struct FILE;

#if _LINUX_
comptime if _LINUX_ {
extern (C) {
pub var stdin: &mut FILE;
pub var stdout: &mut FILE;
Expand All @@ -62,11 +62,11 @@ pub struct FILE;
pub const SEEK_END := 2;

pub const TMP_MAX := 308915776;
#endif
}

#if _WINDOWS_
comptime if _WINDOWS_ {
extern (C) {
func __acrt_iob_fn(index: uint32) &mut FILE;
func __acrt_iob_fn(index: uint32) -> &mut FILE;
}

pub alias fpos_t := int64;
Expand All @@ -89,7 +89,7 @@ pub struct FILE;
pub var stdin: &mut FILE := unsafe { __acrt_iob_fn(0) };
pub var stdout: &mut FILE := unsafe { __acrt_iob_fn(1) };
pub var stderr: &mut FILE := unsafe { __acrt_iob_fn(2) };
#endif
}

extern (C) {
pub func fopen(path: [&]uint8, mode: [&]uint8) -> ?&mut FILE;
Expand Down
14 changes: 7 additions & 7 deletions lib/c/src/libc/stdlib.ri
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
// Copyright (C) 2023-present Jose Mendoza - All rights reserved. Use of this
// source code is governed by an MIT license that can be found in the LICENSE
// Copyright (C) 2023-present Jose Mendoza - All rights reserved. Use of this
// source code is governed by an MIT license that can be found in the LICENSE
// file.

#if _LINUX_
comptime if _LINUX_ {
pub const RAND_MAX := 0x7fffffff;
#else_if _WINDOWS_
} else if _WINDOWS_ {
pub const RAND_MAX := 0x7fff;
#else
#error cannot define RAND_MAX in this operating system, please report the issue
#endif
} else {
// TODO: error cannot define RAND_MAX in this operating system, please report the issue
}

extern (C) {
pub func rand() -> int32;
Expand Down
10 changes: 5 additions & 5 deletions lib/c/src/libc/unistd.ri
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// Copyright (C) 2023-present Jose Mendoza - All rights reserved. Use of this
// source code is governed by an MIT license that can be found in the LICENSE
// Copyright (C) 2023-present Jose Mendoza - All rights reserved. Use of this
// source code is governed by an MIT license that can be found in the LICENSE
// file.

extern (C) {
Expand All @@ -21,7 +21,7 @@ extern (C) {

pub func readlink(path: [&]uint8, buf: [&]uint8, size: uint) -> int;

#if _LINUX_
pub func get_nprocs() -> int32;
#endif
comptime if _LINUX_ {
pub func get_nprocs() -> int32;
}
}
10 changes: 5 additions & 5 deletions lib/c/src/wyhash.ri
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// Copyright (C) 2023-present Jose Mendoza - All rights reserved. Use of this
// source code is governed by an MIT license that can be found in the LICENSE
// Copyright (C) 2023-present Jose Mendoza - All rights reserved. Use of this
// source code is governed by an MIT license that can be found in the LICENSE
// file.

#![compile_c_source("../thirdparty/wyhash/wyhash.c")]
Expand All @@ -20,7 +20,7 @@ extern (C) {
func wy2u01(r: uint64) -> float64;
func wy2gau(r: uint64) -> float64;

#if !WYHASH_32BIT_MUM
func wy2u0k(r: uint64, k: uint64) -> uint64;
#endif
comptime if !WYHASH_32BIT_MUM {
func wy2u0k(r: uint64, k: uint64) -> uint64;
}
}
Loading

0 comments on commit d3e6d64

Please sign in to comment.