Skip to content

Commit

Permalink
Use ANTLR4 for PreProcessor
Browse files Browse the repository at this point in the history
  • Loading branch information
LuisHsu committed Dec 23, 2024
1 parent f6a9613 commit cdfa4f3
Show file tree
Hide file tree
Showing 32 changed files with 647 additions and 3,990 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,5 @@ build/
install/
.devcontainer
.DS_Store
exclude
exclude
.antlr/
4 changes: 4 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,10 @@ elseif(WIN32)
endif(UNIX)

include_directories(${CMAKE_CURRENT_LIST_DIR}/src/include)


add_subdirectory(external)
link_directories(${PROJECT_BINARY_DIR}/external/antlr4_runtime/src/antlr4_runtime-install/lib)
add_subdirectory(src)

set(BUILD_TESTS ON CACHE BOOL "Build tests")
Expand Down
14 changes: 4 additions & 10 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,15 +1,7 @@
add_subdirectory(lib/PreProcessor)

add_library(cc
lib/Compiler.cpp
lib/SourceStream.cpp
lib/PreProcessor/PreProcessor.cpp
lib/PreProcessor/Scanner.cpp
lib/PreProcessor/Macro.cpp
lib/PreProcessor/Expression.cpp
lib/Token.cpp
lib/Lexer.cpp
lib/Parser.cpp
lib/AST/Expr.cpp
lib/AST/Dump.cpp
)

add_executable(wasmvm-cc
Expand All @@ -21,6 +13,8 @@ add_executable(wasmvm-cc
target_link_libraries(wasmvm-cc
WasmVM
cc
pp
antlr4-runtime
)
target_compile_definitions(wasmvm-cc PUBLIC
DEFAULT_LOOKUP_PATH="${DEFAULT_LOOKUP_PATH}"
Expand Down
31 changes: 16 additions & 15 deletions src/exec/wasmvm-cc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -134,18 +134,19 @@ int main(int argc, const char* argv[]){
// Preprocess
if(args["pp"]){
for(std::filesystem::path source_path : source_files){
PreProcessor pp(source_path);
if(args["output"]){
std::ofstream output_file(std::get<std::string>(args["output"].value()));
for(PreProcessor::PPToken tok = pp.get(); tok.has_value(); tok = pp.get()){
output_file << tok.value();
}
output_file.close();
}else{
for(PreProcessor::PPToken tok = pp.get(); tok.has_value(); tok = pp.get()){
std::cout << tok.value();
}
}
std::ifstream fin(source_path);
PreProcessor pp(source_path, fin);
// if(args["output"]){
// std::ofstream output_file(std::get<std::string>(args["output"].value()));
// for(PreProcessor::PPToken tok = pp.get(); tok.has_value(); tok = pp.get()){
// output_file << tok.value();
// }
// output_file.close();
// }else{
// for(PreProcessor::PPToken tok = pp.get(); tok.has_value(); tok = pp.get()){
// std::cout << tok.value();
// }
// }
}
return 0;
}
Expand Down Expand Up @@ -194,9 +195,9 @@ int main(int argc, const char* argv[]){
}
Linker linker(linker_config);

}catch(Exception::Error &e){
std::cerr << e.pos << ": " COLOR_Error ": " << e.what() << std::endl;
return -1;
// }catch(Exception::Error &e){
// std::cerr << e.pos << ": " COLOR_Error ": " << e.what() << std::endl;
// return -1;
}catch(Exception::Exception &e){
std::cerr << args.program.filename().string() << ": " COLOR_Error ": " << e.what() << std::endl;
return -1;
Expand Down
8 changes: 0 additions & 8 deletions src/include/AST/Decl.hpp

This file was deleted.

9 changes: 0 additions & 9 deletions src/include/AST/Dump.hpp

This file was deleted.

69 changes: 0 additions & 69 deletions src/include/AST/Expr.hpp

This file was deleted.

18 changes: 0 additions & 18 deletions src/include/AST/TransUnit.hpp

This file was deleted.

15 changes: 7 additions & 8 deletions src/include/Error.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,18 @@
#define WVMCC_Error_DEF

#include <exception.hpp>
#include <SourceStream.hpp>

namespace WasmVM {
namespace Exception {

struct Error : public Exception {
Error(SourcePos pos, std::string msg) : Exception(msg), pos(pos){}
SourcePos pos;
};
// struct Error : public Exception {
// Error(SourcePos pos, std::string msg) : Exception(msg), pos(pos){}
// SourcePos pos;
// };

struct SyntaxError : public Error {
SyntaxError(SourcePos pos, std::string msg) : Error(pos, "syntax error:" + msg){}
};
// struct SyntaxError : public Error {
// SyntaxError(SourcePos pos, std::string msg) : Error(pos, "syntax error:" + msg){}
// };

} // namespace WasmVM
} // namespace WasmVM
Expand Down
22 changes: 0 additions & 22 deletions src/include/Lexer.hpp

This file was deleted.

22 changes: 0 additions & 22 deletions src/include/Parser.hpp

This file was deleted.

Loading

0 comments on commit cdfa4f3

Please sign in to comment.