Compare commits
10 Commits
0010783b29
...
a01858615d
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
a01858615d | ||
|
|
6c2af30345 | ||
|
|
cfd3e2ba51 | ||
|
|
8a60ebbfbf | ||
|
|
01cb40babc | ||
|
|
a0175024f3 | ||
|
|
270c34518a | ||
|
|
d82ebb7986 | ||
|
|
94248e38fb | ||
|
|
3590f26005 |
5
.github/workflows/c-cpp.yml
vendored
5
.github/workflows/c-cpp.yml
vendored
@@ -13,6 +13,11 @@ jobs:
|
|||||||
|
|
||||||
steps:
|
steps:
|
||||||
- uses: actions/checkout@v4
|
- uses: actions/checkout@v4
|
||||||
|
- name: Install NASM & gcc-multilib
|
||||||
|
run: |
|
||||||
|
sudo apt-get update
|
||||||
|
sudo apt-get install -y nasm gcc-multilib
|
||||||
|
|
||||||
- name: make
|
- name: make
|
||||||
run: make
|
run: make
|
||||||
- name: make test
|
- name: make test
|
||||||
|
|||||||
19
LICENSE
Normal file
19
LICENSE
Normal file
@@ -0,0 +1,19 @@
|
|||||||
|
|
||||||
|
No Copyright
|
||||||
|
|
||||||
|
The person who associated a work with this deed has dedicated the work to the public domain by waiving all of his or her rights to the work worldwide under copyright law, including all related and neighboring rights, to the extent allowed by law.
|
||||||
|
You can copy, modify, distribute and perform the work, even for commercial purposes, all without asking permission. See Other Information below.
|
||||||
|
|
||||||
|
Other Information
|
||||||
|
|
||||||
|
In no way are the patent or trademark rights of any person affected by CC0, nor are the rights that other persons may have in the work or in how the work is used, such as publicity or privacy rights.
|
||||||
|
Unless expressly stated otherwise, the person who associated a work with this deed makes no warranties about the work, and disclaims liability for all uses of the work, to the fullest extent permitted by applicable law.
|
||||||
|
When using or citing the work, you should not imply endorsement by the author or the affirmer.
|
||||||
|
|
||||||
|
Notice
|
||||||
|
|
||||||
|
The Commons Deed is not a legal instrument. It is simply a handy reference for understanding the CC0 Legal Code, a human-readable expression of some of its key terms. Think of it as the user-friendly interface to the CC0 Legal Code beneath. This Deed itself has no legal value, and its contents do not appear in CC0.
|
||||||
|
|
||||||
|
Creative Commons is not a law firm and does not provide legal services. Distributing, displaying, or linking to this Commons Deed does not create an attorney-client relationship.
|
||||||
|
|
||||||
|
Creative Commons has not verified the copyright status of any work to which CC0 has been applied. CC makes no warranties about any work or its copyright status in any jurisdiction, and disclaims all liability for all uses of any work.
|
||||||
4
Makefile
4
Makefile
@@ -4,8 +4,8 @@ CC = gcc
|
|||||||
CFLAGS = -std=c99 -Wall -O2
|
CFLAGS = -std=c99 -Wall -O2
|
||||||
NASM = nasm
|
NASM = nasm
|
||||||
NASMFLAGS = -f elf32
|
NASMFLAGS = -f elf32
|
||||||
LD = gcc
|
LD = ./commonl
|
||||||
LDFLAGS = -m32
|
LDFLAGS =
|
||||||
|
|
||||||
# Compiler
|
# Compiler
|
||||||
COMPILER = common
|
COMPILER = common
|
||||||
|
|||||||
16
README.md
16
README.md
@@ -302,6 +302,7 @@ gcc -m32 main.c common.o -o program
|
|||||||
```
|
```
|
||||||
.
|
.
|
||||||
├── common.c # Compiler source (2000 LOC)
|
├── common.c # Compiler source (2000 LOC)
|
||||||
|
├── commonl # Linker
|
||||||
├── Makefile # Build automation
|
├── Makefile # Build automation
|
||||||
├── run_tests.sh # Quick test script
|
├── run_tests.sh # Quick test script
|
||||||
│
|
│
|
||||||
@@ -328,19 +329,30 @@ gcc -m32 main.c common.o -o program
|
|||||||
└── linkedlist.cm
|
└── linkedlist.cm
|
||||||
```
|
```
|
||||||
|
|
||||||
## License
|
## License & Educational Purpose
|
||||||
|
|
||||||
Public domain / CC0. Use freely for any purpose.
|
### Public Domain / CC0
|
||||||
|
|
||||||
|
This project is dedicated to the public domain under the CC0 1.0 Universal license. This means you can copy, modify, distribute, and perform the work, even for commercial purposes, all without asking permission.
|
||||||
|
|
||||||
|
### Why Public Domain?
|
||||||
|
|
||||||
|
- **Ease of Use:** - By removing all licensing restrictions, students can freely integrate code snippets from common.c into their own projects without legal overhead or attribution requirements.
|
||||||
|
- **Educational Accessibility:** - The compiler is designed to be a "pure" learning resource. Its single-file implementation is intended to be read and modified as if it were a textbook example.
|
||||||
|
No Barriers: Just as the language requires "zero external dependencies," its legal status requires no compliance tracking, making it ideal for classroom settings and open-source forks.
|
||||||
|
- **Simplicity:** - A complex license would contradict the project's philosophy of "simplicity over features"
|
||||||
|
|
||||||
## Credits
|
## Credits
|
||||||
|
|
||||||
Inspired by:
|
Inspired by:
|
||||||
|
|
||||||
- **C** - Dennis Ritchie and Brian Kernighan
|
- **C** - Dennis Ritchie and Brian Kernighan
|
||||||
- **chibicc** - Rui Ueyama's educational C compiler
|
- **chibicc** - Rui Ueyama's educational C compiler
|
||||||
- **8cc** - Rui Ueyama's C compiler
|
- **8cc** - Rui Ueyama's C compiler
|
||||||
- **tcc** - Fabrice Bellard's Tiny C Compiler
|
- **tcc** - Fabrice Bellard's Tiny C Compiler
|
||||||
|
|
||||||
Built for programmers who value:
|
Built for programmers who value:
|
||||||
|
|
||||||
- Simplicity over features
|
- Simplicity over features
|
||||||
- Control over convenience
|
- Control over convenience
|
||||||
- Learning over abstraction
|
- Learning over abstraction
|
||||||
|
|||||||
28
common.c
28
common.c
@@ -153,8 +153,8 @@ static void next(void) {
|
|||||||
|
|
||||||
/* multi/single char operators */
|
/* multi/single char operators */
|
||||||
src_pos++;
|
src_pos++;
|
||||||
#define PEEK src[src_pos]
|
#define PEEK src[src_pos]
|
||||||
#define EAT src_pos++
|
#define EAT src_pos++
|
||||||
switch(c) {
|
switch(c) {
|
||||||
case '+': if(PEEK=='+'){ EAT; tok.kind=TK_INC; }
|
case '+': if(PEEK=='+'){ EAT; tok.kind=TK_INC; }
|
||||||
else if(PEEK=='='){ EAT; tok.kind=TK_ADDEQ; }
|
else if(PEEK=='='){ EAT; tok.kind=TK_ADDEQ; }
|
||||||
@@ -191,8 +191,8 @@ static void next(void) {
|
|||||||
case '?': tok.kind=TK_QUESTION;break;
|
case '?': tok.kind=TK_QUESTION;break;
|
||||||
default: die("Unknown char '%c'", c);
|
default: die("Unknown char '%c'", c);
|
||||||
}
|
}
|
||||||
#undef PEEK
|
#undef PEEK
|
||||||
#undef EAT
|
#undef EAT
|
||||||
}
|
}
|
||||||
|
|
||||||
static void expect(TKind k) {
|
static void expect(TKind k) {
|
||||||
@@ -359,7 +359,7 @@ static Node *unary(void) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
#define BINOP(name, next_fn, ...) \
|
#define BINOP(name, next_fn, ...) \
|
||||||
static Node *name(void) { \
|
static Node *name(void) { \
|
||||||
Node *l=next_fn(), *t; TKind ops[]={__VA_ARGS__, TK_EOF}; \
|
Node *l=next_fn(), *t; TKind ops[]={__VA_ARGS__, TK_EOF}; \
|
||||||
for(;;) { \
|
for(;;) { \
|
||||||
int found=0; \
|
int found=0; \
|
||||||
@@ -368,18 +368,18 @@ static Node *name(void) { \
|
|||||||
TKind op=tok.kind; next(); \
|
TKind op=tok.kind; next(); \
|
||||||
t=alloc_node(N_BOPN); t->op=op; t->ch[0]=l; t->ch[1]=next_fn(); l=t; \
|
t=alloc_node(N_BOPN); t->op=op; t->ch[0]=l; t->ch[1]=next_fn(); l=t; \
|
||||||
} return l; \
|
} return l; \
|
||||||
}
|
}
|
||||||
|
|
||||||
BINOP(mul_expr, unary, TK_STAR, TK_SLASH, TK_MOD)
|
BINOP(mul_expr, unary, TK_STAR, TK_SLASH, TK_MOD)
|
||||||
BINOP(add_expr, mul_expr, TK_PLUS, TK_MINUS)
|
BINOP(add_expr, mul_expr, TK_PLUS, TK_MINUS)
|
||||||
BINOP(shf_expr, add_expr, TK_SHL, TK_SHR)
|
BINOP(shf_expr, add_expr, TK_SHL, TK_SHR)
|
||||||
BINOP(cmp_expr, shf_expr, TK_LT, TK_LEQ, TK_GT, TK_GEQ)
|
BINOP(cmp_expr, shf_expr, TK_LT, TK_LEQ, TK_GT, TK_GEQ)
|
||||||
BINOP(eq_expr, cmp_expr, TK_EQ, TK_NEQ)
|
BINOP(eq_expr, cmp_expr, TK_EQ, TK_NEQ)
|
||||||
BINOP(band_expr, eq_expr, TK_AMP)
|
BINOP(band_expr, eq_expr, TK_AMP)
|
||||||
BINOP(bxor_expr, band_expr,TK_CARET)
|
BINOP(bxor_expr, band_expr,TK_CARET)
|
||||||
BINOP(bor_expr, bxor_expr,TK_PIPE)
|
BINOP(bor_expr, bxor_expr,TK_PIPE)
|
||||||
|
|
||||||
static Node *land_expr(void) {
|
static Node *land_expr(void) {
|
||||||
Node *l=bor_expr(), *t;
|
Node *l=bor_expr(), *t;
|
||||||
while (tok.kind==TK_AND) {
|
while (tok.kind==TK_AND) {
|
||||||
next(); t=alloc_node(N_BOPN); t->op=TK_AND;
|
next(); t=alloc_node(N_BOPN); t->op=TK_AND;
|
||||||
|
|||||||
11
commonl
Executable file
11
commonl
Executable file
@@ -0,0 +1,11 @@
|
|||||||
|
#!/usr/bin/env bash
|
||||||
|
# Public domain / CC0. Use freely for any purpose. RoyR 2026
|
||||||
|
|
||||||
|
GCC_NO_PIE=1
|
||||||
|
echo 'int main() {}' | gcc -E -no-pie - -o /dev/null || GCC_NO_PIE=0
|
||||||
|
|
||||||
|
if [ $GCC_NO_PIE = 1 ];then
|
||||||
|
gcc -m32 -no-pie "$@"
|
||||||
|
else
|
||||||
|
gcc -m32 "$@"
|
||||||
|
fi
|
||||||
@@ -72,7 +72,7 @@ static void run_test(Test *t) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/* Link */
|
/* Link */
|
||||||
if (run_command("gcc -m32 /tmp/test.o -o /tmp/test 2>/tmp/test.err") != 0) {
|
if (run_command("./commonl /tmp/test.o -o /tmp/test 2>/tmp/test.err") != 0) {
|
||||||
printf("FAIL (linker error)\n");
|
printf("FAIL (linker error)\n");
|
||||||
test_failed++;
|
test_failed++;
|
||||||
return;
|
return;
|
||||||
|
|||||||
Reference in New Issue
Block a user