# redsc **Repository Path**: tpbb/redsc ## Basic Information - **Project Name**: redsc - **Description**: A Red/System Compiler based on LLVM - **Primary Language**: C++ - **License**: MulanPSL-2.0 - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 0 - **Forks**: 0 - **Created**: 2022-05-20 - **Last Updated**: 2022-05-23 ## Categories & Tags **Categories**: Uncategorized **Tags**: Llvm, cpp20, Cpp, red ## README # redsc ### 介绍 使用C++20, LLVM来实现一个[Red/System](https://static.red-lang.org/red-system-specs.html)编程语言的编译器 ### 软件架构 采用典型的编译器实现方式,注重代码的可读性,适合编译器初学者学习。 _Source Code_ -> |Scanner| -> _Tokens_ -> |Parser| -> _AST_ -> |Semantic Analysis| -> _LLVM IR_ 由于使用LLVM做后端,我们只需要实现前端的词法分析,语法分析和语义分析就行了。 在熟悉了编译器前端之后,如果想进一步学习后端,可以自己实现一个后端替代LLVM。 ### 使用说明 每个commit都配有代码说明,同学们可以通过浏览commit来学习代码。 ### 环境搭建 #### Windows 1. 安装 [Build Tools for Visual Studio 2022](https://aka.ms/vs/17/release/vs_BuildTools.exe). 记得勾选 "Desktop development with C++" 2. 安装 [xmake](https://xmake.io/#/getting_started) 3. 编译LLVM或者下载我[预编译好的库文件](https://pan.baidu.com/s/1O1nxswI1YGvwzuuAGC_ZPw?pwd=ecum) 4. 根据自己的情况,修改 `xmake.lua` (line 127 - 131) 中LLVM头文件和库文件的路径。我提供的库文件是debug版本的,修改行127和行128即可。 ``` 126 if is_mode("debug") then 127 add_linkdirs("../../../llvm+clang+lld-14.0.0-x86_64-windows-msvc-debug/lib") 128 add_includedirs("../../../llvm+clang+lld-14.0.0-x86_64-windows-msvc-debug/include") 129 elseif is_mode("release") then 130 add_linkdirs("../../../llvm+clang+lld-14.0.0-x86_64-windows-msvc-release/lib") 131 add_includedirs("../../../llvm+clang+lld-14.0.0-x86_64-windows-msvc-release/include") 132 end ``` 5. 打开CMD,进入redsc目录,开始编译: ``` xmake f -m debug xmake ``` 生成的 `redsc.exe` 在 `build\windows\x64\debug` 文件夹里. #### Ubuntu Ubuntu上搭建环境比在Windows上简单。 1. 运行redsc目录中的deps_ubuntu_20.04.sh脚本,安装LLVM14和GCC11 ``` chmod +x deps_ubuntu_20.04.sh sudo ./deps_ubuntu_20.04.sh ``` 2. 安装 [xmake](https://xmake.io/#/getting_started) ``` bash <(wget https://xmake.io/shget.text -O -) source ~/.xmake/profile ``` 3. 打开shell,进入redsc目录,开始编译: ``` xmake -v ```