From 9387598b23de080fade7756d0df6e9a239b3cad6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=8D=8Ddiohabara=E5=8D=8D?= Date: Sat, 5 Aug 2023 18:42:56 -0500 Subject: [PATCH] refactor: move files into src directory --- .gitignore | 1 + Makefile | 56 +++++++++++++++++++----------------- ccc.h => src/ccc.h | 0 codegen.c => src/codegen.c | 0 main.c => src/main.c | 0 parse.c => src/parse.c | 12 ++++---- tokenize.c => src/tokenize.c | 0 type.c => src/type.c | 0 8 files changed, 36 insertions(+), 33 deletions(-) rename ccc.h => src/ccc.h (100%) rename codegen.c => src/codegen.c (100%) rename main.c => src/main.c (100%) rename parse.c => src/parse.c (99%) rename tokenize.c => src/tokenize.c (100%) rename type.c => src/type.c (100%) diff --git a/.gitignore b/.gitignore index 9fb6287..9afd041 100644 --- a/.gitignore +++ b/.gitignore @@ -17,6 +17,7 @@ tmp* # for debug .vscode/ +t tests ext test.s diff --git a/Makefile b/Makefile index e04da48..dc2e24d 100644 --- a/Makefile +++ b/Makefile @@ -1,23 +1,15 @@ CFLAGS=-std=c11 -g -static -SRCS=$(wildcard *.c) +SRCS=$(wildcard src/*.c) OBJS=$(SRCS:.c=.o) -$(OBJS): ccc.h - -help: - @echo "Usage: make [lint|test|debug|clean|process]" - @echo " lint: run c and shell checker" - @echo " test: run tests" - @echo " debug: run tests in debug mode" - @echo " clean: remove object files" - @echo " process: run the preprocessor on all source files for each stage" +$(OBJS): src/ccc.h ccc: $(OBJS) - $(CC) -o $@ $(OBJS) $(CFLAGS) + @$(CC) -o $@ $(OBJS) $(CFLAGS) lint: - clang-format -i ./*.c - clang-format -i ./*.h + clang-format -i src/*.c + clang-format -i src/*.h test: ccc ./ccc test/test.c > tmp.s @@ -26,9 +18,15 @@ test: ccc ./tmp clean: - rm -f ccc *.o *~ tmp* stage*/* + rm -f ccc src/*.o *~ tmp* stage*/* + +help: + @echo "Usage: make [lint|test|clean|self-host]" + @echo " lint: run c and shell checker" + @echo " test: run tests" + @echo " clean: remove object files" + @echo " self-host: check self-hosted compiler" -.PHONY: help lint test clean process # stage0: gcc # stage1: ccc on host machine generated by stage0 (gcc) @@ -43,11 +41,12 @@ ccc-stage1: $(OBJS) ccc-stage2: ccc-stage1 $(SRCS) @rm -rf stage2 @mkdir stage2 - $(CC) -D__ccc_self__ -E -P codegen.c -o stage2/codegen.c - $(CC) -D__ccc_self__ -E -P main.c -o stage2/main.c - $(CC) -D__ccc_self__ -E -P parse.c -o stage2/parse.c - $(CC) -D__ccc_self__ -E -P tokenize.c -o stage2/tokenize.c - $(CC) -D__ccc_self__ -E -P type.c -o stage2/type.c + @echo "`-D__ccc_self__` is to avoid `#include \"ccc.h\"`" + $(CC) -D__ccc_self__ -E -P src/codegen.c -o stage2/codegen.c + $(CC) -D__ccc_self__ -E -P src/main.c -o stage2/main.c + $(CC) -D__ccc_self__ -E -P src/parse.c -o stage2/parse.c + $(CC) -D__ccc_self__ -E -P src/tokenize.c -o stage2/tokenize.c + $(CC) -D__ccc_self__ -E -P src/type.c -o stage2/type.c stage1/ccc-stage1 stage2/codegen.c > stage2/codegen.s stage1/ccc-stage1 stage2/main.c > stage2/main.s stage1/ccc-stage1 stage2/parse.c > stage2/parse.s @@ -58,11 +57,12 @@ ccc-stage2: ccc-stage1 $(SRCS) ccc-stage3: ccc-stage2 $(SRCS) @rm -rf stage3 @mkdir stage3 - $(CC) -D__ccc_self__ -E -P type.c -o stage3/type.c - $(CC) -D__ccc_self__ -E -P codegen.c -o stage3/codegen.c - $(CC) -D__ccc_self__ -E -P main.c -o stage3/main.c - $(CC) -D__ccc_self__ -E -P parse.c -o stage3/parse.c - $(CC) -D__ccc_self__ -E -P tokenize.c -o stage3/tokenize.c + @echo "`-D__ccc_self__` is to avoid `#include \"ccc.h\"`" + $(CC) -D__ccc_self__ -E -P src/type.c -o stage3/type.c + $(CC) -D__ccc_self__ -E -P src/codegen.c -o stage3/codegen.c + $(CC) -D__ccc_self__ -E -P src/main.c -o stage3/main.c + $(CC) -D__ccc_self__ -E -P src/parse.c -o stage3/parse.c + $(CC) -D__ccc_self__ -E -P src/tokenize.c -o stage3/tokenize.c stage2/ccc-stage2 stage3/codegen.c > stage3/codegen.s stage2/ccc-stage2 stage3/main.c > stage3/main.s stage2/ccc-stage2 stage3/parse.c > stage3/parse.s @@ -70,7 +70,9 @@ ccc-stage3: ccc-stage2 $(SRCS) stage2/ccc-stage2 stage3/type.c > stage3/type.s $(CC) -o stage3/$@ -test-self-host: ccc-stage1 ccc-stage2 ccc-stage3 +self-host: ccc-stage1 ccc-stage2 ccc-stage3 strip stage2/ccc-stage2 strip stage3/ccc-stage3 - diff -s stage2/ccc-stage2 stage3/ccc-stage3 \ No newline at end of file + diff -s stage2/ccc-stage2 stage3/ccc-stage3 + +.PHONY: all help lint test clean self-host diff --git a/ccc.h b/src/ccc.h similarity index 100% rename from ccc.h rename to src/ccc.h diff --git a/codegen.c b/src/codegen.c similarity index 100% rename from codegen.c rename to src/codegen.c diff --git a/main.c b/src/main.c similarity index 100% rename from main.c rename to src/main.c diff --git a/parse.c b/src/parse.c similarity index 99% rename from parse.c rename to src/parse.c index eb176a1..c428081 100644 --- a/parse.c +++ b/src/parse.c @@ -231,12 +231,12 @@ void global_var() { ty->is_extern = is_extern; // // TODO: write tyepdef here - // if (ty->is_typedef) { - // expect(";"); - // ty->is_typedef = false; - // push_scope(name)->type_def = ty; - // return; - // } + if (ty->is_typedef) { + expect(";"); + ty->is_typedef = false; + push_scope(name)->type_def = ty; + return; + } Var *var = push_var(name, ty, false, tok); push_scope(name)->var = var; diff --git a/tokenize.c b/src/tokenize.c similarity index 100% rename from tokenize.c rename to src/tokenize.c diff --git a/type.c b/src/type.c similarity index 100% rename from type.c rename to src/type.c