├── .clang-format ├── .gitignore ├── .gitmodules ├── CMakeLists.txt ├── LICENSE ├── README.md ├── checker ├── check.cc └── check.h ├── commands ├── build.cc ├── build.h ├── check.cc ├── check.h ├── color.h ├── command.cc ├── command.h ├── help.cc ├── help.h ├── ir.cc ├── ir.h ├── parse.cc ├── parse.h ├── run.cc └── run.h ├── core ├── common.h ├── error.cc ├── error.h └── location.h ├── emitter ├── emit.cc ├── emit.h ├── expression.cc ├── expression.h ├── optimize.cc └── optimize.h ├── main.cc └── parser ├── ast.cc ├── ast.h ├── grammar.y ├── parse.cc ├── parse.h └── scanner.l /.clang-format: -------------------------------------------------------------------------------- 1 | --- 2 | Language: Cpp 3 | BasedOnStyle: Google 4 | AllowShortBlocksOnASingleLine: false 5 | AllowShortCaseLabelsOnASingleLine: false 6 | AllowShortFunctionsOnASingleLine: None 7 | AllowShortIfStatementsOnASingleLine: false 8 | AllowShortLoopsOnASingleLine: false 9 | Standard: Cpp11 10 | Cpp11BracedListStyle: true 11 | BreakBeforeTernaryOperators: false 12 | PointerAlignment: Left 13 | FixNamespaceComments: false 14 | IncludeCategories: 15 | - Regex: '^"' 16 | Priority: 3 17 | - Regex: '^<' 18 | Priority: 2 19 | ... 20 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | build/ 2 | .vscode 3 | .DS_Store 4 | 5 | parser/grammar.h 6 | parser/grammar.cc 7 | parser/scanner.h 8 | parser/scanner.cc 9 | -------------------------------------------------------------------------------- /.gitmodules: -------------------------------------------------------------------------------- 1 | [submodule "ext/llvm"] 2 | path = ext/llvm 3 | url = https://github.com/llvm/llvm-project.git 4 | branch = release/11.x 5 | [submodule "ext/utf8"] 6 | path = ext/utf8 7 | url = https://github.com/nemtrif/utfcpp.git 8 | -------------------------------------------------------------------------------- /CMakeLists.txt: -------------------------------------------------------------------------------- 1 | cmake_minimum_required(VERSION 3.3) 2 | 3 | project(compiler VERSION 1.1.0) 4 | 5 | set(CMAKE_CXX_STANDARD 17) 6 | set(CMAKE_CXX_STANDARD_REQUIRED True) 7 | set(CMAKE_CXX_VISIBILITY_PRESET hidden) 8 | 9 | find_package(BISON 3.3) 10 | IF(BISON_FOUND) 11 | BISON_TARGET(grammar parser/grammar.y 12 | ${CMAKE_CURRENT_SOURCE_DIR}/parser/grammar.cc 13 | DEFINES_FILE ${CMAKE_CURRENT_SOURCE_DIR}/parser/grammar.h) 14 | ENDIF(BISON_FOUND) 15 | 16 | find_package(FLEX) 17 | IF(FLEX_FOUND) 18 | FLEX_TARGET(scanner parser/scanner.l 19 | ${CMAKE_CURRENT_SOURCE_DIR}/parser/scanner.cc 20 | DEFINES_FILE ${CMAKE_CURRENT_SOURCE_DIR}/parser/scanner.h) 21 | ENDIF(FLEX_FOUND) 22 | 23 | # Compiler binary 24 | add_executable(compiler 25 | main.cc 26 | checker/check.cc 27 | commands/build.cc 28 | commands/check.cc 29 | commands/command.cc 30 | commands/help.cc 31 | commands/ir.cc 32 | commands/parse.cc 33 | commands/run.cc 34 | core/error.cc 35 | emitter/emit.cc 36 | emitter/expression.cc 37 | emitter/optimize.cc 38 | parser/ast.cc 39 | parser/grammar.cc 40 | parser/parse.cc 41 | parser/scanner.cc) 42 | target_compile_options(compiler PUBLIC -Wall -Werror -Wno-register) 43 | 44 | # UTF-8 45 | target_include_directories(compiler PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/ext/utf8/source) 46 | 47 | # LLVM 48 | add_subdirectory(ext/llvm/llvm) 49 | target_include_directories(compiler PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/ext/llvm/llvm/include) 50 | target_include_directories(compiler PRIVATE ${CMAKE_CURRENT_BINARY_DIR}/ext/llvm/llvm/include) 51 | target_link_libraries(compiler 52 | LLVMCore 53 | LLVMipo 54 | LLVMExecutionEngine 55 | LLVMInterpreter 56 | LLVMMCJIT 57 | LLVMAArch64CodeGen 58 | LLVMAMDGPUCodeGen 59 | LLVMARMCodeGen 60 | LLVMAVRCodeGen 61 | LLVMBPFCodeGen 62 | LLVMHexagonCodeGen 63 | LLVMLanaiCodeGen 64 | LLVMMipsCodeGen 65 | LLVMMSP430CodeGen 66 | LLVMNVPTXCodeGen 67 | LLVMPowerPCCodeGen 68 | LLVMRISCVCodeGen 69 | LLVMSparcCodeGen 70 | LLVMSystemZCodeGen 71 | LLVMWebAssemblyCodeGen 72 | LLVMX86CodeGen 73 | LLVMXCoreCodeGen) 74 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Apache License 2 | Version 2.0, January 2004 3 | http://www.apache.org/licenses/ 4 | 5 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 6 | 7 | 1. Definitions. 8 | 9 | "License" shall mean the terms and conditions for use, reproduction, 10 | and distribution as defined by Sections 1 through 9 of this document. 11 | 12 | "Licensor" shall mean the copyright owner or entity authorized by 13 | the copyright owner that is granting the License. 14 | 15 | "Legal Entity" shall mean the union of the acting entity and all 16 | other entities that control, are controlled by, or are under common 17 | control with that entity. For the purposes of this definition, 18 | "control" means (i) the power, direct or indirect, to cause the 19 | direction or management of such entity, whether by contract or 20 | otherwise, or (ii) ownership of fifty percent (50%) or more of the 21 | outstanding shares, or (iii) beneficial ownership of such entity. 22 | 23 | "You" (or "Your") shall mean an individual or Legal Entity 24 | exercising permissions granted by this License. 25 | 26 | "Source" form shall mean the preferred form for making modifications, 27 | including but not limited to software source code, documentation 28 | source, and configuration files. 29 | 30 | "Object" form shall mean any form resulting from mechanical 31 | transformation or translation of a Source form, including but 32 | not limited to compiled object code, generated documentation, 33 | and conversions to other media types. 34 | 35 | "Work" shall mean the work of authorship, whether in Source or 36 | Object form, made available under the License, as indicated by a 37 | copyright notice that is included in or attached to the work 38 | (an example is provided in the Appendix below). 39 | 40 | "Derivative Works" shall mean any work, whether in Source or Object 41 | form, that is based on (or derived from) the Work and for which the 42 | editorial revisions, annotations, elaborations, or other modifications 43 | represent, as a whole, an original work of authorship. For the purposes 44 | of this License, Derivative Works shall not include works that remain 45 | separable from, or merely link (or bind by name) to the interfaces of, 46 | the Work and Derivative Works thereof. 47 | 48 | "Contribution" shall mean any work of authorship, including 49 | the original version of the Work and any modifications or additions 50 | to that Work or Derivative Works thereof, that is intentionally 51 | submitted to Licensor for inclusion in the Work by the copyright owner 52 | or by an individual or Legal Entity authorized to submit on behalf of 53 | the copyright owner. For the purposes of this definition, "submitted" 54 | means any form of electronic, verbal, or written communication sent 55 | to the Licensor or its representatives, including but not limited to 56 | communication on electronic mailing lists, source code control systems, 57 | and issue tracking systems that are managed by, or on behalf of, the 58 | Licensor for the purpose of discussing and improving the Work, but 59 | excluding communication that is conspicuously marked or otherwise 60 | designated in writing by the copyright owner as "Not a Contribution." 61 | 62 | "Contributor" shall mean Licensor and any individual or Legal Entity 63 | on behalf of whom a Contribution has been received by Licensor and 64 | subsequently incorporated within the Work. 65 | 66 | 2. Grant of Copyright License. Subject to the terms and conditions of 67 | this License, each Contributor hereby grants to You a perpetual, 68 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 69 | copyright license to reproduce, prepare Derivative Works of, 70 | publicly display, publicly perform, sublicense, and distribute the 71 | Work and such Derivative Works in Source or Object form. 72 | 73 | 3. Grant of Patent License. Subject to the terms and conditions of 74 | this License, each Contributor hereby grants to You a perpetual, 75 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 76 | (except as stated in this section) patent license to make, have made, 77 | use, offer to sell, sell, import, and otherwise transfer the Work, 78 | where such license applies only to those patent claims licensable 79 | by such Contributor that are necessarily infringed by their 80 | Contribution(s) alone or by combination of their Contribution(s) 81 | with the Work to which such Contribution(s) was submitted. If You 82 | institute patent litigation against any entity (including a 83 | cross-claim or counterclaim in a lawsuit) alleging that the Work 84 | or a Contribution incorporated within the Work constitutes direct 85 | or contributory patent infringement, then any patent licenses 86 | granted to You under this License for that Work shall terminate 87 | as of the date such litigation is filed. 88 | 89 | 4. Redistribution. You may reproduce and distribute copies of the 90 | Work or Derivative Works thereof in any medium, with or without 91 | modifications, and in Source or Object form, provided that You 92 | meet the following conditions: 93 | 94 | (a) You must give any other recipients of the Work or 95 | Derivative Works a copy of this License; and 96 | 97 | (b) You must cause any modified files to carry prominent notices 98 | stating that You changed the files; and 99 | 100 | (c) You must retain, in the Source form of any Derivative Works 101 | that You distribute, all copyright, patent, trademark, and 102 | attribution notices from the Source form of the Work, 103 | excluding those notices that do not pertain to any part of 104 | the Derivative Works; and 105 | 106 | (d) If the Work includes a "NOTICE" text file as part of its 107 | distribution, then any Derivative Works that You distribute must 108 | include a readable copy of the attribution notices contained 109 | within such NOTICE file, excluding those notices that do not 110 | pertain to any part of the Derivative Works, in at least one 111 | of the following places: within a NOTICE text file distributed 112 | as part of the Derivative Works; within the Source form or 113 | documentation, if provided along with the Derivative Works; or, 114 | within a display generated by the Derivative Works, if and 115 | wherever such third-party notices normally appear. The contents 116 | of the NOTICE file are for informational purposes only and 117 | do not modify the License. You may add Your own attribution 118 | notices within Derivative Works that You distribute, alongside 119 | or as an addendum to the NOTICE text from the Work, provided 120 | that such additional attribution notices cannot be construed 121 | as modifying the License. 122 | 123 | You may add Your own copyright statement to Your modifications and 124 | may provide additional or different license terms and conditions 125 | for use, reproduction, or distribution of Your modifications, or 126 | for any such Derivative Works as a whole, provided Your use, 127 | reproduction, and distribution of the Work otherwise complies with 128 | the conditions stated in this License. 129 | 130 | 5. Submission of Contributions. Unless You explicitly state otherwise, 131 | any Contribution intentionally submitted for inclusion in the Work 132 | by You to the Licensor shall be under the terms and conditions of 133 | this License, without any additional terms or conditions. 134 | Notwithstanding the above, nothing herein shall supersede or modify 135 | the terms of any separate license agreement you may have executed 136 | with Licensor regarding such Contributions. 137 | 138 | 6. Trademarks. This License does not grant permission to use the trade 139 | names, trademarks, service marks, or product names of the Licensor, 140 | except as required for reasonable and customary use in describing the 141 | origin of the Work and reproducing the content of the NOTICE file. 142 | 143 | 7. Disclaimer of Warranty. Unless required by applicable law or 144 | agreed to in writing, Licensor provides the Work (and each 145 | Contributor provides its Contributions) on an "AS IS" BASIS, 146 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 147 | implied, including, without limitation, any warranties or conditions 148 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A 149 | PARTICULAR PURPOSE. You are solely responsible for determining the 150 | appropriateness of using or redistributing the Work and assume any 151 | risks associated with Your exercise of permissions under this License. 152 | 153 | 8. Limitation of Liability. In no event and under no legal theory, 154 | whether in tort (including negligence), contract, or otherwise, 155 | unless required by applicable law (such as deliberate and grossly 156 | negligent acts) or agreed to in writing, shall any Contributor be 157 | liable to You for damages, including any direct, indirect, special, 158 | incidental, or consequential damages of any character arising as a 159 | result of this License or out of the use or inability to use the 160 | Work (including but not limited to damages for loss of goodwill, 161 | work stoppage, computer failure or malfunction, or any and all 162 | other commercial damages or losses), even if such Contributor 163 | has been advised of the possibility of such damages. 164 | 165 | 9. Accepting Warranty or Additional Liability. While redistributing 166 | the Work or Derivative Works thereof, You may choose to offer, 167 | and charge a fee for, acceptance of support, warranty, indemnity, 168 | or other liability obligations and/or rights consistent with this 169 | License. However, in accepting such obligations, You may act only 170 | on Your own behalf and on Your sole responsibility, not on behalf 171 | of any other Contributor, and only if You agree to indemnify, 172 | defend, and hold each Contributor harmless for any liability 173 | incurred by, or claims asserted against, such Contributor by reason 174 | of your accepting any such warranty or additional liability. 175 | 176 | END OF TERMS AND CONDITIONS 177 | 178 | APPENDIX: How to apply the Apache License to your work. 179 | 180 | To apply the Apache License to your work, attach the following 181 | boilerplate notice, with the fields enclosed by brackets "[]" 182 | replaced with your own identifying information. (Don't include 183 | the brackets!) The text should be enclosed in the appropriate 184 | comment syntax for the file format. We also recommend that a 185 | file or class name and description of purpose be included on the 186 | same "printed page" as the copyright notice for easier 187 | identification within third-party archives. 188 | 189 | Copyright [yyyy] [name of copyright owner] 190 | 191 | Licensed under the Apache License, Version 2.0 (the "License"); 192 | you may not use this file except in compliance with the License. 193 | You may obtain a copy of the License at 194 | 195 | http://www.apache.org/licenses/LICENSE-2.0 196 | 197 | Unless required by applicable law or agreed to in writing, software 198 | distributed under the License is distributed on an "AS IS" BASIS, 199 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 200 | See the License for the specific language governing permissions and 201 | limitations under the License. 202 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Generic Compiler Scaffolding 2 | 3 | This repository is the scaffolding for an end-to-end compiler. It is a useful 4 | starting point when developing a new language — taking care of the workflow 5 | and wiring typical in most of my personal programming language projects. 6 | 7 | To build, check out the repository and execute the following commands: 8 | 9 | $ git submodule init 10 | $ git submodule update 11 | $ cmake -H. -Bbuild 12 | $ cmake --build build --target compiler 13 | $ ./build/compiler 14 | 15 | We include [LLVM](https://llvm.org) as a submodule. 16 | 17 | ## Features and Dependencies 18 | 19 | The compiler is split into four primary directories representing the logical 20 | parts of the compiler workflow: 21 | 22 | 1. `parser/` - A parser based on [Bison](https://www.gnu.org/software/bison/) and [Flex](https://www.gnu.org/software/flex/). 23 | 2. `checker/` - Placeholder module to check the program for semantic correctness. 24 | 3. `emitter/` - Backend code generation to [LLVM IR](https://llvm.org/docs/LangRef.html). 25 | 4. `commands/` - A lightweight framework for supporting different compiler commands. Out of the box, the compiler supports the following commands: 26 | - `compiler run` - Execute a program using just-in-time compilation 27 | - `compiler build` - Generates a binary (optionally cross-compiling for different architectures) 28 | - `compiler check` - Checks a program for semantic correctness 29 | - `compiler parse` - Checks a program for syntactic correctness 30 | - `compiler ir` - Emits the LLVM IR code for a program 31 | 32 | ## Starting Point 33 | 34 | The project builds a compiler for a minimal languge that prints the results of 35 | simple integer expressions, e.g., 36 | 37 | 1 * 3 + (12 % 5) 38 | (2 << 3) / 2 39 | 40 | prints 41 | 42 | 5 43 | 8 44 | 45 | ## Author 46 | 47 | This scaffolding is used exclusively for personal projects by Bret Taylor 48 | ([btaylor@gmail.com](btaylor@gmail.com)). 49 | -------------------------------------------------------------------------------- /checker/check.cc: -------------------------------------------------------------------------------- 1 | // Copyright 2020 Bret Taylor 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // http://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | 15 | #include "check.h" 16 | 17 | namespace compiler::checker { 18 | 19 | bool check(shared_ptr error, shared_ptr module) { 20 | // In our toy language, it is impossible to have semantic errors 21 | return true; 22 | } 23 | 24 | } 25 | -------------------------------------------------------------------------------- /checker/check.h: -------------------------------------------------------------------------------- 1 | // Copyright 2020 Bret Taylor 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // http://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | 15 | #pragma once 16 | 17 | #include "../core/error.h" 18 | #include "../parser/ast.h" 19 | 20 | namespace compiler::checker { 21 | 22 | bool check(shared_ptr error, shared_ptr module); 23 | 24 | } 25 | -------------------------------------------------------------------------------- /commands/build.cc: -------------------------------------------------------------------------------- 1 | // Copyright 2020 Bret Taylor 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // http://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | 15 | #include "build.h" 16 | 17 | #include 18 | #include 19 | #include 20 | #include 21 | #include 22 | #include 23 | #include 24 | #include 25 | #include 26 | 27 | #include "../checker/check.h" 28 | #include "../emitter/emit.h" 29 | #include "../emitter/optimize.h" 30 | #include "../parser/parse.h" 31 | 32 | namespace compiler::commands { 33 | 34 | Build::Build() 35 | : Command("build", "Build an executable binary for a program", 36 | {Option("strict", "Treat warnings as fatal errors"), 37 | Option("unoptimized", "Do not optimize the program"), 38 | Option("output", "Output binary name", Option::OPTION), 39 | Option("target", "Target architecture", Option::OPTION), 40 | Option("linker", "Linker command", Option::OPTION, "cc"), 41 | Option("object", "Generate an unlinked object file")}, 42 | "path") { 43 | } 44 | 45 | bool Build::execute(const filesystem::path& executable, 46 | map& flags, map& options, 47 | vector& arguments) { 48 | if (arguments.size() < 1) { 49 | print_help(executable); 50 | return false; 51 | } 52 | 53 | // Initialize LLVM 54 | llvm::InitializeAllTargetInfos(); 55 | llvm::InitializeAllTargets(); 56 | llvm::InitializeAllTargetMCs(); 57 | llvm::InitializeAllAsmPrinters(); 58 | 59 | // Parse the program 60 | auto error = make_shared(); 61 | auto fail_level = flags["strict"] ? Error::WARNING : Error::ERROR; 62 | auto module = parser::parse(error, arguments[0]); 63 | if (!module) { 64 | return false; 65 | } 66 | 67 | // Check for correctness 68 | auto symbols = checker::check(error, module); 69 | if (!symbols || error->count(fail_level) > 0) { 70 | return false; 71 | } 72 | 73 | // Emit LLVM IR code 74 | llvm::LLVMContext llvm_context; 75 | string target = options["target"]; 76 | if (target.empty()) { 77 | target = llvm::sys::getDefaultTargetTriple(); 78 | } 79 | string llvm_error; 80 | auto llvm_target = 81 | llvm::TargetRegistry::lookupTarget(target.c_str(), llvm_error); 82 | if (!llvm_target) { 83 | error->report(Error::ERROR, llvm_error); 84 | return false; 85 | } 86 | auto llvm_machine = llvm_target->createTargetMachine( 87 | target.c_str(), "generic", "", llvm::TargetOptions(), 88 | llvm::Reloc::Model::PIC_); 89 | auto llvm_module = new llvm::Module(arguments[0], llvm_context); 90 | llvm_module->setDataLayout(llvm_machine->createDataLayout()); 91 | auto llvm_function = emitter::emit(module, llvm_module); 92 | if (!llvm_function) { 93 | return false; 94 | } 95 | if (!flags["unoptimized"]) { 96 | emitter::optimize(llvm_module); 97 | } 98 | 99 | // Write the object file 100 | char object_path[PATH_MAX]; 101 | auto pattern = filesystem::temp_directory_path() / "XXXXXXXXXX.o"; 102 | strcpy(object_path, pattern.c_str()); 103 | auto object_fd = mkstemps(object_path, 2); 104 | if (object_fd == -1) { 105 | error->report(Error::ERROR, 106 | "Could not create temporary file: " + string(object_path)); 107 | return false; 108 | } 109 | close(object_fd); 110 | std::error_code file_error; 111 | llvm::raw_fd_ostream out(object_path, file_error, llvm::sys::fs::F_None); 112 | if (file_error) { 113 | error->report(Error::ERROR, 114 | "Could not write file: " + file_error.message()); 115 | return false; 116 | } 117 | llvm::legacy::PassManager pass; 118 | if (llvm_machine->addPassesToEmitFile(pass, out, nullptr, 119 | llvm::CGFT_ObjectFile)) { 120 | error->report( 121 | Error::ERROR, 122 | "LLVM cannot emit object files for the target architecture: " + target); 123 | return false; 124 | } 125 | pass.run(*llvm_module); 126 | out.flush(); 127 | 128 | // Determine our output file name 129 | string output_name = options["output"]; 130 | if (output_name.empty()) { 131 | output_name = filesystem::path(arguments[0]).stem(); 132 | if (flags["object"]) { 133 | output_name += ".o"; 134 | } 135 | } 136 | 137 | // Finish with the object file if requested 138 | if (flags["object"]) { 139 | if (rename(object_path, output_name.c_str()) == -1) { 140 | error->report(Error::ERROR, 141 | "Could not move " + string(object_path) + " to " + name); 142 | return false; 143 | } 144 | unlink(object_path); 145 | return true; 146 | } 147 | 148 | // Link the object file using the cc command to include the C standard library 149 | string command = options["linker"] + " " + object_path + " -o " + output_name; 150 | if (system(command.c_str()) == -1) { 151 | error->report(Error::ERROR, "Could not execute linker: " + command); 152 | return false; 153 | } 154 | unlink(object_path); 155 | return true; 156 | } 157 | 158 | } 159 | -------------------------------------------------------------------------------- /commands/build.h: -------------------------------------------------------------------------------- 1 | // Copyright 2020 Bret Taylor 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // http://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | 15 | #pragma once 16 | 17 | #include "command.h" 18 | 19 | namespace compiler::commands { 20 | 21 | class Build : public Command { 22 | public: 23 | Build(); 24 | 25 | protected: 26 | bool execute(const filesystem::path& executable, map& flags, 27 | map& options, 28 | vector& arguments) override; 29 | }; 30 | 31 | } 32 | -------------------------------------------------------------------------------- /commands/check.cc: -------------------------------------------------------------------------------- 1 | // Copyright 2020 Bret Taylor 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // http://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | 15 | #include "check.h" 16 | 17 | #include 18 | 19 | #include "../checker/check.h" 20 | #include "../parser/parse.h" 21 | 22 | namespace compiler::commands { 23 | 24 | Check::Check() 25 | : Command("check", "Check the correctness of a module", 26 | {Option("strict", "Treat warnings as fatal errors")}, "path…") { 27 | } 28 | 29 | bool Check::execute(const filesystem::path& executable, 30 | map& flags, map& options, 31 | vector& arguments) { 32 | if (arguments.size() < 1) { 33 | print_help(executable); 34 | return false; 35 | } 36 | auto fail_level = flags["strict"] ? Error::WARNING : Error::ERROR; 37 | bool success = true; 38 | for (auto& path : arguments) { 39 | auto error = make_shared(); 40 | auto module = parser::parse(error, path); 41 | if (!module) { 42 | success = false; 43 | continue; 44 | } 45 | auto symbols = checker::check(error, module); 46 | if (!symbols || error->count(fail_level) > 0) { 47 | success = false; 48 | } 49 | } 50 | return success; 51 | } 52 | 53 | } 54 | -------------------------------------------------------------------------------- /commands/check.h: -------------------------------------------------------------------------------- 1 | // Copyright 2020 Bret Taylor 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // http://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | 15 | #pragma once 16 | 17 | #include "command.h" 18 | 19 | namespace compiler::commands { 20 | 21 | class Check : public Command { 22 | public: 23 | Check(); 24 | 25 | protected: 26 | bool execute(const filesystem::path& executable, map& flags, 27 | map& options, 28 | vector& arguments) override; 29 | }; 30 | 31 | } 32 | -------------------------------------------------------------------------------- /commands/color.h: -------------------------------------------------------------------------------- 1 | // Copyright 2020 Bret Taylor 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // http://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | 15 | #include "../core/common.h" 16 | 17 | namespace compiler::commands { 18 | 19 | // Our named colors, which make our command line interface more useful in 20 | // terminals that support color. 21 | class Color { 22 | public: 23 | Color(bool tty) : tty(tty) { 24 | } 25 | 26 | inline string error(const string& value) const { 27 | if (tty) { 28 | return "\033[31;1m" + value + "\033[0m"; 29 | } else { 30 | return value; 31 | } 32 | } 33 | 34 | inline string success(const string& value) const { 35 | if (tty) { 36 | return "\033[32;1m" + value + "\033[0m"; 37 | } else { 38 | return value; 39 | } 40 | } 41 | 42 | inline string command(const string& value) const { 43 | if (tty) { 44 | return "\033[1m" + value + "\033[0m"; 45 | } else { 46 | return value; 47 | } 48 | } 49 | 50 | inline string option(const string& value) const { 51 | if (tty) { 52 | return "\033[2m[" + value + "]\033[0m"; 53 | } else { 54 | return "[" + value + "]"; 55 | } 56 | } 57 | 58 | inline string arguments(const string& value) const { 59 | if (tty) { 60 | return "\033[2m" + value + "\033[0m"; 61 | } else { 62 | return value; 63 | } 64 | } 65 | 66 | bool tty; 67 | }; 68 | 69 | } 70 | -------------------------------------------------------------------------------- /commands/command.cc: -------------------------------------------------------------------------------- 1 | // Copyright 2020 Bret Taylor 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // http://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | 15 | #include "command.h" 16 | 17 | #include 18 | 19 | #include "color.h" 20 | 21 | namespace compiler::commands { 22 | 23 | bool Command::run(const filesystem::path& executable, 24 | const vector& arguments) { 25 | // Initialize with the default value for all options 26 | map flag_arguments; 27 | map option_arguments; 28 | map option_map; 29 | for (auto& option : options) { 30 | option_map.insert(std::make_pair(option.name, option)); 31 | switch (option.type) { 32 | case Option::FLAG: 33 | flag_arguments[option.name] = false; 34 | break; 35 | case Option::OPTION: 36 | option_arguments[option.name] = option.default_value; 37 | break; 38 | } 39 | } 40 | 41 | // Parse the command line flags 42 | Color color(isatty(STDERR_FILENO)); 43 | vector tail; 44 | for (size_t i = 0; i < arguments.size(); i++) { 45 | auto& argument(arguments[i]); 46 | if (argument.size() > 0 && argument[0] == '-') { 47 | size_t i = 1; 48 | while (i < argument.size() && argument[i] == '-') { 49 | i++; 50 | } 51 | string option_name; 52 | string option_value; 53 | string option_string(argument.substr(i)); 54 | auto loc = option_string.find('='); 55 | if (loc != string::npos) { 56 | option_name = option_string.substr(0, loc); 57 | option_value = option_string.substr(loc + 1); 58 | } else { 59 | option_name = option_string; 60 | } 61 | auto option_loc = option_map.find(option_name); 62 | if (option_loc == option_map.end()) { 63 | std::cerr << "Unrecognized option: " << color.error(argument) 64 | << std::endl; 65 | print_help(executable); 66 | return false; 67 | } 68 | auto option = option_loc->second; 69 | if (option.type == Option::OPTION) { 70 | if (option_value.empty()) { 71 | std::cerr << "Option " << color.error(option.name) 72 | << " requires a value" << std::endl; 73 | print_help(executable); 74 | return false; 75 | } 76 | option_arguments[option.name] = option_value; 77 | } else if (loc != std::string::npos) { 78 | if (option_value == "true") { 79 | flag_arguments[option.name] = true; 80 | } else if (option_value == "false") { 81 | flag_arguments[option.name] = false; 82 | } else { 83 | std::cerr << "Option " << color.error(option.name) 84 | << " is a boolean flag" << std::endl; 85 | print_help(executable); 86 | return false; 87 | } 88 | } else { 89 | flag_arguments[option.name] = true; 90 | } 91 | } else { 92 | for (size_t j = i; j < arguments.size(); j++) { 93 | tail.push_back(arguments[j]); 94 | } 95 | break; 96 | } 97 | } 98 | 99 | return execute(executable, flag_arguments, option_arguments, tail); 100 | } 101 | 102 | void Command::print_help(const filesystem::path& executable) { 103 | print_help(executable, std::cerr, isatty(STDERR_FILENO)); 104 | } 105 | 106 | void Command::print_help(const filesystem::path& executable, std::ostream& out, 107 | bool tty) { 108 | Color color(tty); 109 | out << "Usage: " << executable.string() << " " << color.command(name); 110 | if (options.size() > 0) { 111 | out << " " << color.option("options"); 112 | } 113 | if (!argument_placeholder.empty()) { 114 | out << " " << color.arguments(argument_placeholder); 115 | } 116 | out << std::endl << std::endl; 117 | if (options.size() > 0) { 118 | out << "Options for " << color.command(name) << ": " << std::endl; 119 | size_t tab_width = 0; 120 | for (auto& option : options) { 121 | size_t width = option.name.size() + 4; 122 | if (option.type != Option::FLAG) { 123 | width += 2; 124 | } 125 | tab_width = std::max(tab_width, width); 126 | } 127 | for (auto& option : options) { 128 | size_t width = option.name.size(); 129 | out << " -" << option.name; 130 | if (option.type != Option::FLAG) { 131 | out << "=…"; 132 | width += 2; 133 | } 134 | for (size_t i = width; i < tab_width; i++) { 135 | out << " "; 136 | } 137 | out << option.description << std::endl; 138 | } 139 | out << std::endl; 140 | } 141 | out << "Try `" << executable.string() << " " << color.command("help") 142 | << "` to show documentation for all commands." << std::endl; 143 | } 144 | 145 | } -------------------------------------------------------------------------------- /commands/command.h: -------------------------------------------------------------------------------- 1 | // Copyright 2020 Bret Taylor 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // http://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | 15 | #pragma once 16 | 17 | #include 18 | 19 | #include "../core/common.h" 20 | 21 | namespace compiler::commands { 22 | 23 | // A command-line option for a compiler commands. 24 | class Option { 25 | public: 26 | // A FLAG takes no arguments and represents a Boolean flag. An OPTION 27 | // requires an argument. 28 | enum Type { 29 | FLAG, 30 | OPTION, 31 | }; 32 | 33 | Option(const string& name, const string& description, Type type = FLAG, 34 | string default_value = "") 35 | : name(name), 36 | description(description), 37 | type(type), 38 | default_value(default_value) { 39 | } 40 | 41 | string name; 42 | string description; 43 | Type type; 44 | string default_value; 45 | }; 46 | 47 | // One of our compiler commands (e.g., "run" or "build"). 48 | class Command { 49 | public: 50 | Command(const string& name, const string& description, 51 | const vector