Skip to content

Commit d049541

Browse files
authored
Write a compiler from Scrapscript to C (#120)
Emit straight C, no IR. Includes a GC and small runtime. Has immediate small integers (no bignum), small strings and heap strings, and immediate empty list.
1 parent 55f6318 commit d049541

File tree

8 files changed

+1324
-2
lines changed

8 files changed

+1324
-2
lines changed

.clang-format

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
BasedOnStyle: Google
2+
AlignEscapedNewlinesLeft: false
3+
DerivePointerAlignment: false
4+
PointerAlignment: Left
5+
IncludeBlocks: Regroup
6+
IncludeCategories:
7+
# C system headers
8+
- Regex: '^<.*\.h?>'
9+
Priority: 1
10+
# Scrapscript headers
11+
- Regex: '.*'
12+
Priority: 2

.clang-tidy

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
Checks: >
2+
-*,
3+
bugprone-argument-comment,
4+
google-readability-casting,
5+
google-readability-todo,
6+
modernize-use-nullptr,
7+
readability-braces-around-statements,
8+
readability-else-after-return,
9+
readability-identifier-naming,
10+
readability-inconsistent-declaration-parameter-name,
11+
readability-static-accessed-through-instance,
12+
FormatStyle: file
13+
CheckOptions:
14+
- { key: readability-identifier-naming.ClassCase, value: CamelCase }
15+
- { key: readability-identifier-naming.ClassMemberCase, value: lower_case }
16+
- { key: readability-identifier-naming.ClassMemberSuffix, value: '_' }
17+
- { key: readability-identifier-naming.GlobalConstantCase, value: CamelCase }
18+
- { key: readability-identifier-naming.GlobalConstantPrefix, value: 'k' }
19+
- { key: readability-identifier-naming.LocalVariableCase, value: lower_case }
20+
- { key: readability-identifier-naming.MethodCase, value: camelBack }
21+
- { key: readability-identifier-naming.NamespaceCase, value: lower_case }
22+
- { key: readability-identifier-naming.ParameterCase, value: lower_case }
23+
- { key: readability-braces-around-statements.ShortStatementLines, value: 1 }

.github/workflows/ci.yml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,10 @@ jobs:
4242
run: sudo apt update
4343
- name: Install Python
4444
run: sudo apt install --yes ${{matrix.PYTHON}}
45-
- name: Run tests
45+
- name: Run interpreter tests
4646
run: ${{matrix.PYTHON}} scrapscript.py test
47+
- name: Run compiler tests
48+
run: ${{matrix.PYTHON}} compiler_tests.py
4749
build_docker_image:
4850
runs-on: ubuntu-latest
4951
permissions:

README.md

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,30 @@ docker run -i -t ghcr.io/tekknolagi/scrapscript:trunk apply "1 + 2"
5353
docker run -i -t ghcr.io/tekknolagi/scrapscript:trunk repl
5454
```
5555

56+
### The experimental compiler:
57+
58+
#### Normal ELF
59+
60+
```bash
61+
./compiler.py some.scrap # produces output.c
62+
./compiler.py some.scrap --compile # produces a.out
63+
```
64+
65+
#### Cosmopolitan
66+
67+
```bash
68+
CC=~/Downloads/cosmos/bin/cosmocc ./compiler.py some.scrap --compile # produces a.out
69+
```
70+
71+
#### Wasm
72+
73+
```bash
74+
CC=/opt/wasi-sdk/bin/clang \
75+
CFLAGS=-D_WASI_EMULATED_MMAN \
76+
LDFLAGS=-lwasi-emulated-mman \
77+
./compiler.py some.scrap --compile # produces a.out
78+
```
79+
5680
## Running Tests
5781

5882
```bash

0 commit comments

Comments
 (0)