diff --git a/.gitignore b/.gitignore index 32e0e1f..9bdd54d 100644 --- a/.gitignore +++ b/.gitignore @@ -57,3 +57,5 @@ lex.yy.c bin/* .vscode/* +.idea/* +cmake-build-debug/* diff --git a/CMakeLists.txt b/CMakeLists.txt new file mode 100644 index 0000000..c83d8a0 --- /dev/null +++ b/CMakeLists.txt @@ -0,0 +1,14 @@ +cmake_minimum_required(VERSION 3.19) +project(summoner) + +set(CMAKE_C_STANDARD 99) + +add_subdirectory(compiler) +add_subdirectory(debug) +add_subdirectory(share) +add_subdirectory(builtin) +list(APPEND EXTRA_LIBS COMPILER DEBUG SHARE BUILTIN) + +add_executable(summoner main/main.c) + +target_link_libraries(summoner PUBLIC ${EXTRA_LIBS}) diff --git a/Makefile b/Makefile index c707170..d45d4e1 100644 --- a/Makefile +++ b/Makefile @@ -1,4 +1,4 @@ -output: main.o lex.o grammar.o create.o string.o compiler.o builtin.o fix_tree.o codegen.o opcode.o disassemble.o error.o debug.o wchar.o +all: main.o lex.o grammar.o create.o string.o compiler.o builtin.o fix_tree.o codegen.o opcode.o disassemble.o error.o debug.o wchar.o $(CC) -o bin/main $^ main.o : main/main.c $(CC) -c main/main.c @@ -17,11 +17,11 @@ builtin.o : builtin/builtin.c create.o : compiler/create.c $(CC) -c $< -o create.o grammar.o : compiler/grammar.y - yacc -d $< - $(CC) -c y.tab.c -o grammar.o + yacc -d -o compiler/y.tab.c $< + $(CC) -c compiler/y.tab.c -o grammar.o lex.o : compiler/lex.l grammar.o - lex $< - $(CC) -c lex.yy.c -o lex.o + lex -o compiler/lex.yy.c $< + $(CC) -c compiler/lex.yy.c -o lex.o string.o : compiler/string.c $(CC) -c $< -o string.o wchar.o: share/wchar.c @@ -32,7 +32,7 @@ debug.o: debug/debug.c $(CC) -c $< -o debug.o clean: - rm -f y.tab.* - rm -f lex.yy.c + rm -f compiler/y.tab.* + rm -f compiler/lex.yy.c rm -f *.o rm -f bin/main diff --git a/builtin/CMakeLists.txt b/builtin/CMakeLists.txt new file mode 100644 index 0000000..e3a6ffc --- /dev/null +++ b/builtin/CMakeLists.txt @@ -0,0 +1,2 @@ +add_library(BUILTIN builtin.c) +target_include_directories(BUILTIN INTERFACE ${CMAKE_CURRENT_SOURCE_DIR}) diff --git a/compiler/CMakeLists.txt b/compiler/CMakeLists.txt new file mode 100644 index 0000000..5202c8b --- /dev/null +++ b/compiler/CMakeLists.txt @@ -0,0 +1,10 @@ +add_custom_command( + OUTPUT ${CMAKE_CURRENT_SOURCE_DIR}/lex.yy.c + COMMAND lex -o ${CMAKE_CURRENT_SOURCE_DIR}/lex.yy.c ${CMAKE_CURRENT_SOURCE_DIR}/lex.l +) +add_custom_command( + OUTPUT ${CMAKE_CURRENT_SOURCE_DIR}/y.tab.c + COMMAND yacc -d -o ${CMAKE_CURRENT_SOURCE_DIR}/y.tab.c ${CMAKE_CURRENT_SOURCE_DIR}/grammar.y +) +add_library(COMPILER lex.yy.c y.tab.c codegen.c disassemble.c create.c error.c fix_tree.c opcode.c string.c compiler.c) +target_include_directories(COMPILER INTERFACE ${CMAKE_CURRENT_SOURCE_DIR}) diff --git a/compiler/grammar.y b/compiler/grammar.y index 18ca7b9..7861881 100644 --- a/compiler/grammar.y +++ b/compiler/grammar.y @@ -1,7 +1,7 @@ %{ #include #include -#include "compiler/summoner.h" +#include "summoner.h" int yylex(); int yyerror(const char *s); diff --git a/compiler/lex.l b/compiler/lex.l index 5c32104..05510ee 100644 --- a/compiler/lex.l +++ b/compiler/lex.l @@ -2,7 +2,7 @@ #include #include #include "y.tab.h" -#include "compiler/summoner.h" +#include "summoner.h" void lex_err(char *str) { fprintf(stderr, "lexical error:%s\n", str); diff --git a/debug/CMakeLists.txt b/debug/CMakeLists.txt new file mode 100644 index 0000000..ce93e45 --- /dev/null +++ b/debug/CMakeLists.txt @@ -0,0 +1,2 @@ +add_library(DEBUG debug.c) +target_include_directories(DEBUG INTERFACE ${CMAKE_CURRENT_SOURCE_DIR}) diff --git a/share/CMakeLists.txt b/share/CMakeLists.txt new file mode 100644 index 0000000..b83fe72 --- /dev/null +++ b/share/CMakeLists.txt @@ -0,0 +1,2 @@ +add_library(SHARE wchar.c) +target_include_directories(SHARE INTERFACE ${CMAKE_CURRENT_SOURCE_DIR})