saltwater: the part of the sea causing lots of rust
A C compiler written in Rust, with a focus on good error messages.
swcc
reads from standard in by default, so you can type in code directly.
It's not interactive though, you have to hit Ctrl+D to indicate end of file (Ctrl+Z on Windows).
Use swcc --help
for all options (or see below).
You need to have cc
on your PATH. You can either install mingw + gcc or MSVC.
Other than that, it should work exactly the same as on Linux.
brew install saltwater
printf
) is already supported.int a[n]
)```c $ cat tests/runner-tests/readme.c // output: j is 6 int printf(const char *, ...);
typedef struct s *sp;
int i = 1; int a[3] = {1, 2, 3}; float f = 2.5;
struct s { int outer; } my_struct;
int g(int);
int main(void) { sp mystructpointer = &mystruct; const int c = mystruct_pointer->outer = 4; // should return 6 int j = i + f*a[2] - c/g(1); printf("j is %d\n", j); return j; }
int g(int i) { if (i < 0 || i >= 3) { return 0; } return a[i]; } $ swcc tests/runner-tests/readme.c $️ ./a.out j is 6 ```
```c $ cat tests/runner-tests/cpp/if/defined.c // code: 2
int i = 2;
syntax error
int main() { return i; }
$ swcc -E tests/runner-tests/cpp/if/defined.c int i = 2 ; int main ( ) { return i ; } ```
c
$ echo 'int i = 1 + 2 ^ 3 % 5 / 2 & 1; int main(){}' | swcc --debug-ast
ast: int i = ((1) + (2)) ^ ((((3) % (5)) / (2)) & (1));
ast: int main(){
}
```c $ cat tests/runner-tests/hello_world.c
int main() { puts("Hello, world!"); } $ swcc --debug-ir tests/runner-tests/helloworld.c function u0:0() -> i32 systemv { gv0 = symbol colocated u1:3 sig0 = (i64) -> i32 system_v fn0 = u0:26 sig0
block0: v0 = global_value.i64 gv0 v1 = call fn0(v0) v2 = iconst.i32 0 return v2 } $ ./a.out Hello, world! ```
```txt $ swcc --help swcc 0.9.0 Joshua Nelson jyn514@gmail.com A C compiler written in Rust, with a focus on good error messages. Homepage: https://github.com/jyn514/rcc/
usage: swcc [FLAGS] [OPTIONS] [
FLAGS:
--debug-ast If set, print the parsed abstract syntax tree (AST) in addition to compiling.
The AST does no type checking or validation, it only parses.
--debug-hir If set, print the high intermediate representation (HIR) in addition to compiling.
This does type checking and validation and also desugars various expressions.
--debug-ir If set, print the intermediate representation (IR) of the program in addition to compiling.
--debug-lex If set, print all tokens found by the lexer in addition to compiling.
--jit If set, will use JIT compilation for C code and instantly run compiled code (No files produced).
NOTE: this option only works if swcc was compiled with the jit
feature.
-h, --help Prints help information
-c, --no-link If set, compile and assemble but do not link. Object file is machine-dependent.
-E, --preprocess-only If set, preprocess only, but do not do anything else.
Note that preprocessing discards whitespace and comments.
There is not currently a way to disable this behavior.
-V, --version Prints version information
OPTIONS:
--color
ARGS:
```sh cargo test
grep -R unimplemented src
, thoughtests/fuzz.sh
HFUZZRUNARGS="--tmoutsigvtalrm --exitupon_crash" tests/hfuzz.sh ```
See FAQ.md
See CONTRIBUTING.md. This also includes reporting bugs.