Skip to content

Commit

Permalink
improved a lot of stuff
Browse files Browse the repository at this point in the history
- constant expressions (comptimes, locals, etc.) can be used as array sizes, and the comptime system has improved
- bool implements `&` and `|`
- fixed some issues with struct literals
- fixed some issues with distincts
- added more functions to `libc.capy` in `core` and added distinct types for file descriptors and `*FILE` pointers
- added a function in `ptr.capy` that can convert a raw pointer and length into a slice
- added better support for +Inf, -Inf, and NaN
- added hex literals
- `println` can now print types
- and a few more little things
  • Loading branch information
NotAFlyingGoose committed Jun 2, 2024
1 parent 94cd414 commit c8ed3e2
Show file tree
Hide file tree
Showing 33 changed files with 2,482 additions and 709 deletions.
6 changes: 4 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@
target
out
c-tests
examples/test.capy

hello.txt
examples/test*.capy

**/.DS_STORE

Cargo.lock
.editorconfig
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,8 @@ There are two requirements which determine if a variable is *const*.
1. It must be immutable.
2. It must either contain a literal value, a reference to another const variable, or a `comptime` block.

Beyond type annotations, the size of an array is also expected to be *const*, and this value can be calculated using `comptime`.

To see all the different types, you can look through [`core/meta.capy`](./core/meta.capy),
which contains reflection related code and documentation for all of Capy's types.

Expand Down
2 changes: 2 additions & 0 deletions core/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

This contains the basic functionality needed for any good program. It will automatically be downloaded from GitHub if it does not exist in the modules directory.

You can forcefully update your version of core with `--redownload-core`

You can import any module with the `mod` keyword

```capy
Expand Down
19 changes: 19 additions & 0 deletions core/fmt.capy
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,25 @@ _write_i64_to_buf :: (n: i64, signed: bool, buf: ^mut u8, buf_offset: usize, req
}

_write_f64_to_buf :: (n: f64, buf: ^mut u8, afterpoint: usize) -> usize {
if math.is_NaN(n) {
ptr.write(buf, 'N' as u8, 0);
ptr.write(buf, 'a' as u8, 1);
ptr.write(buf, 'N' as u8, 2);
return 3;
} else if n == math.positive_inf {
ptr.write(buf, '+' as u8, 0);
ptr.write(buf, 'I' as u8, 1);
ptr.write(buf, 'n' as u8, 2);
ptr.write(buf, 'f' as u8, 3);
return 4;
} else if n == math.negative_inf {
ptr.write(buf, '-' as u8, 0);
ptr.write(buf, 'I' as u8, 1);
ptr.write(buf, 'n' as u8, 2);
ptr.write(buf, 'f' as u8, 3);
return 4;
}

ipart := n as i64;

fpart := n - ipart as f64;
Expand Down
52 changes: 40 additions & 12 deletions core/libc.capy
Original file line number Diff line number Diff line change
Expand Up @@ -7,31 +7,59 @@ malloc :: (size: usize) -> ^mut any extern;
calloc :: (len: usize, size: usize) -> ^mut any extern;
// frees allocated memory
free :: (ptr: ^any) extern;
// reallocates already allocated memory
realloc :: (ptr: ^mut any, size: usize) -> ^mut any extern;
// copies len bytes from dst to src
memcpy :: (dst: ^any, src: ^any, len: usize) extern;
memcpy :: (dst: ^mut any, src: ^any, len: usize) extern;

// prints a string to the screen, adds a newline at the end
puts :: (text: str) extern;
// prints a char to the screen
putchar :: (ch: char) extern;

// opens a file for either reading "r", writing "w", appending "a",
// open a file to update both reading and writing "r+",
// create an empty file for reading and writing "w+",
// or open a file for reading and appending "a+"
fopen :: (filename: str, mode: str) -> usize extern;
// a file descriptor is an integer that represents an open file
File_Desc :: distinct i32;

stdin : File_Desc : 0;
stdout : File_Desc : 1;
stderr : File_Desc : 2;

// opens a file
open :: (pathname: str, flags: i32) -> File_Desc extern;
// moves the read/write pointer of a file descriptor
lseek :: (file: File_Desc, offset: isize, whence: i32) -> isize extern;
// closes a file descriptor fd
close :: (file: File_Desc) -> i32 extern;
// writes up to count bytes from the given buffer to
// the file referred to by the file descriptor fd.
write :: (file: File_Desc, buf: ^u8, count: usize) -> isize extern;
// attempts to read up to count bytes from the file into the buf.
// this returns the number of bytes read.
read :: (file: File_Desc, buf: ^mut u8, count: usize) -> isize extern;

// there are two ways to mess with files in libc:
// `*FILE` pointers and file descriptors.
// `FILE` is a struct in libc but it's platform specific,
// so here it's represented by a usize
File_Pointer :: distinct usize;

// opens a file for either reading "r", writing "w", appending "a".
// open a file to update both reading and writing "r+".
// create an empty file for reading and writing "w+".
// open a file for reading and appending "a+".
fopen :: (pathname: str, mode: str) -> File_Pointer extern;
// closes a file
fclose :: (fp: usize) -> i32 extern;
fclose :: (file: File_Pointer) -> i32 extern;

// writes a char to a file
fputc :: (ch: char, fp: usize) -> i32 extern;
// writes a string to a file, doesn't add a newline at the end
fputs :: (text: str, fp: usize) -> i32 extern;
fputc :: (ch: char, file: File_Pointer) -> i32 extern;
// writes a string to a file and doesn't add a newline at the end
fputs :: (text: str, file: File_Pointer) -> i32 extern;

// read a char from a file
fgetc :: (fp: usize) -> char extern;
fgetc :: (fp: File_Pointer) -> char extern;
// read len char's from a file and store them in buf
fgets :: (buf: ^char, len: i32, fp: usize) -> str extern;
fgets :: (buf: ^mut char, len: i32, fp: File_Pointer) -> str extern;

// tests the end-of-file indicator for the given file
feof :: (fp: usize) -> bool extern;
Expand Down
9 changes: 9 additions & 0 deletions core/math.capy
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@

positive_inf :: comptime 1.0 / 0.0;
negative_inf :: comptime -1.0 / 0.0;
// NaN != NaN, so use is_NaN instead
NaN :: comptime 0.0 / 0.0;

pow :: (base: i32, exp: i32) -> i32 {
result := 1;

Expand Down Expand Up @@ -65,3 +70,7 @@ min_usize :: (x: usize, y: usize) -> usize {
y
}
}

is_NaN :: (n: f64) -> bool {
n != n
}
Loading

0 comments on commit c8ed3e2

Please sign in to comment.