From 2a71aa843438244ae213b3782acdfb7cf75db4c3 Mon Sep 17 00:00:00 2001
From: HonestDeng <2958906959@qq.com>
Date: Tue, 30 Apr 2024 00:37:39 +0800
Subject: [PATCH 1/3] =?UTF-8?q?=E5=AE=8C=E6=88=90=E4=BA=86=E9=A1=B9?=
 =?UTF-8?q?=E7=9B=AE=E6=95=B4=E4=BD=93=E7=BB=93=E6=9E=84=EF=BC=8C=E6=8E=A5?=
 =?UTF-8?q?=E4=B8=8B=E6=9D=A5=E5=B0=9D=E8=AF=95=E5=AE=8C=E6=88=90CI?=
 =?UTF-8?q?=E7=9A=84=E6=90=AD=E5=BB=BA?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

---
 .gitignore                | 131 ++++++++++++++++++++++++++++++++++++++
 CMakeLists.txt            |  18 ++++++
 cmake/utils.cmake         |   3 +
 frontend/CMakeLists.txt   |  13 ++++
 frontend/gen_ir.cpp       |  55 ++++++++++++++++
 frontend/include/Lexer.h  |  20 ++++++
 frontend/include/Parser.h |  21 ++++++
 frontend/src/Lexer.cpp    |  20 ++++++
 frontend/src/Parser.cpp   |  22 +++++++
 sysY2022/only_main.sys    |   3 +
 10 files changed, 306 insertions(+)
 create mode 100644 .gitignore
 create mode 100644 CMakeLists.txt
 create mode 100644 cmake/utils.cmake
 create mode 100644 frontend/CMakeLists.txt
 create mode 100644 frontend/gen_ir.cpp
 create mode 100644 frontend/include/Lexer.h
 create mode 100644 frontend/include/Parser.h
 create mode 100644 frontend/src/Lexer.cpp
 create mode 100644 frontend/src/Parser.cpp
 create mode 100644 sysY2022/only_main.sys

diff --git a/.gitignore b/.gitignore
new file mode 100644
index 0000000..13f7fb1
--- /dev/null
+++ b/.gitignore
@@ -0,0 +1,131 @@
+# User ================================
+# 用户自定义的其他忽略文件
+
+# Clion ================================
+# Covers JetBrains IDEs: IntelliJ, RubyMine, PhpStorm, AppCode, PyCharm, CLion, Android Studio and WebStorm
+# Reference: https://intellij-support.jetbrains.com/hc/en-us/articles/206544839
+
+# User-specific stuff
+.idea/**/workspace.xml
+.idea/**/tasks.xml
+.idea/**/usage.statistics.xml
+.idea/**/dictionaries
+.idea/**/shelf
+
+# Generated files
+.idea/**/contentModel.xml
+
+# Sensitive or high-churn files
+.idea/**/dataSources/
+.idea/**/dataSources.ids
+.idea/**/dataSources.local.xml
+.idea/**/sqlDataSources.xml
+.idea/**/dynamic.xml
+.idea/**/uiDesigner.xml
+.idea/**/dbnavigator.xml
+
+# Gradle
+.idea/**/gradle.xml
+.idea/**/libraries
+
+# Gradle and Maven with auto-import
+# When using Gradle or Maven with auto-import, you should exclude module files,
+# since they will be recreated, and may cause churn.  Uncomment if using
+# auto-import.
+# .idea/modules.xml
+# .idea/*.iml
+# .idea/modules
+# *.iml
+# *.ipr
+
+# CMake
+cmake-build-*/
+
+# Mongo Explorer plugin
+.idea/**/mongoSettings.xml
+
+# File-based project format
+*.iws
+
+# IntelliJ
+out/
+
+# mpeltonen/sbt-idea plugin
+.idea_modules/
+
+# JIRA plugin
+atlassian-ide-plugin.xml
+
+# Cursive Clojure plugin
+.idea/replstate.xml
+
+# Crashlytics plugin (for Android Studio and IntelliJ)
+com_crashlytics_export_strings.xml
+crashlytics.properties
+crashlytics-build.properties
+fabric.properties
+
+# Editor-based Rest Client
+.idea/httpRequests
+
+# Android studio 3.1+ serialized cache file
+.idea/caches/build_file_checksums.ser
+
+# vscode ================================
+.vscode/*
+!.vscode/settings.json
+!.vscode/tasks.json
+!.vscode/launch.json
+!.vscode/extensions.json
+
+# C++ ================================
+ Prerequisites
+*.d
+
+# Compiled Object files
+*.slo
+*.lo
+*.o
+*.obj
+
+# Precompiled Headers
+*.gch
+*.pch
+
+# Compiled Dynamic libraries
+*.so
+*.dylib
+*.dll
+
+# Fortran module files
+*.mod
+*.smod
+
+# Compiled Static libraries
+*.lai
+*.la
+*.a
+*.lib
+
+# Executables
+*.exe
+*.out
+*.app
+
+
+# CMake ================================
+bin/
+build/
+CMakeLists.txt.user
+CMakeCache.txt
+CMakeFiles
+CMakeScripts
+Testing
+Makefile
+cmake_install.cmake
+install_manifest.txt
+compile_commands.json
+CTestTestfile.cmake
+
+.idea
+
diff --git a/CMakeLists.txt b/CMakeLists.txt
new file mode 100644
index 0000000..e800cb9
--- /dev/null
+++ b/CMakeLists.txt
@@ -0,0 +1,18 @@
+CMAKE_MINIMUM_REQUIRED(VERSION 2.8)
+
+if (NOT CMAKE_BUILD_TYPE)
+    set(CMAKE_BUILD_TYPE Release)
+endif()
+
+# 加载cmake中函数和变量
+include(cmake/utils.cmake)
+
+project(SCNUcc LANGUAGES CXX)
+
+add_subdirectory(frontend)
+#add_subdirectory(midend)
+#add_subdirectory(backend)
+
+#add_executable(compiler Compiler.cpp)
+
+#target_link_libraries(compiler frontend midend backend)
\ No newline at end of file
diff --git a/cmake/utils.cmake b/cmake/utils.cmake
new file mode 100644
index 0000000..f89a5f0
--- /dev/null
+++ b/cmake/utils.cmake
@@ -0,0 +1,3 @@
+set(CMAKE_CXX_STANDARD 17)
+set(CMAKE_CXX_STANDARD_REQUIRED ON)
+set(CMAKE_CXX_EXTENSIONS OFF)
\ No newline at end of file
diff --git a/frontend/CMakeLists.txt b/frontend/CMakeLists.txt
new file mode 100644
index 0000000..7c83cc6
--- /dev/null
+++ b/frontend/CMakeLists.txt
@@ -0,0 +1,13 @@
+# 将前端的代码编译成静态库
+add_library(frontend STATIC src/Lexer.cpp src/Parser.cpp gen_ir.cpp)
+# 同时生成一个gen_ir可执行文件,用于测试前端框架是否正确
+add_executable(gen_ir gen_ir.cpp)
+# 将gen_ir链接到前端的静态库
+target_link_libraries(gen_ir frontend)
+
+# 添加头文件路径
+target_include_directories(gen_ir PUBLIC include)
+target_include_directories(frontend PUBLIC include)
+
+# 创建cmake-build-debug/our_irs文件夹,用于存放程序生成的IR文件
+file(MAKE_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/our_irs)
\ No newline at end of file
diff --git a/frontend/gen_ir.cpp b/frontend/gen_ir.cpp
new file mode 100644
index 0000000..2379ace
--- /dev/null
+++ b/frontend/gen_ir.cpp
@@ -0,0 +1,55 @@
+//
+// Created by 邓实诚 on 2024/4/29.
+//
+
+#include <iostream>
+#include <fstream>
+#include <filesystem>
+#include "Parser.h"
+
+int main(int argc, char **argv) {
+  // usage: ./gen_ir <input_file> <output_dir>
+  if (argc != 3) {
+    std::cerr << "Convert sysY2022 file <input_file> to LLVM IR file in <output_dir> with same file name." << std::endl;
+    std::cerr << "Usage: ./gen_ir <input_file> <output_dir>" << std::endl;
+    std::cerr << "Example: ./gen_ir test.c ./our_irs/" << std::endl;
+    return 1;
+  }
+
+  // 读取文件
+  std::filesystem::path input_file = argv[1];
+  std::ifstream ifs;
+  ifs.open(input_file);
+  if (!ifs.is_open()) {
+    std::cerr << "Error: cannot open file " << input_file << std::endl;
+    return 1;
+  }
+  ifs.close();
+
+  // 解析文件
+  SCNUCC::Parser parser;
+  parser.parse();
+
+  // 输出解析结果
+  std::filesystem::path output_dir = argv[2];
+  if (!std::filesystem::exists(output_dir)) {
+    std::cerr << "Error: output directory " << output_dir << " does not exist" << std::endl;
+    return 1;
+  }
+  std::filesystem::path output_file = output_dir / input_file.filename();
+  output_file.replace_extension(".ll");
+  std::ofstream ofs;
+  ofs.open(output_file);
+  if (!ofs.is_open()) {
+    std::cerr << "Error: cannot open file " << output_file << std::endl;
+    return 1;
+  }
+  std::string ir = "define dso_local noundef i32 @main() #0 {\n"
+                   "  %1 = alloca i32, align 4\n"
+                   "  store i32 0, ptr %1, align 4\n"
+                   "  ret i32 0\n"
+                   "}";
+  ofs << ir;
+  ofs.close();
+  return 0;
+}
\ No newline at end of file
diff --git a/frontend/include/Lexer.h b/frontend/include/Lexer.h
new file mode 100644
index 0000000..b217002
--- /dev/null
+++ b/frontend/include/Lexer.h
@@ -0,0 +1,20 @@
+//
+// Created by 邓实诚 on 2024/4/29.
+//
+
+#ifndef SCNUCC_LEXER_H
+#define SCNUCC_LEXER_H
+
+namespace SCNUCC {
+class Lexer {
+public:
+  Lexer();
+
+  ~Lexer();
+
+  void scan();
+};
+}
+
+
+#endif //SCNUCC_LEXER_H
diff --git a/frontend/include/Parser.h b/frontend/include/Parser.h
new file mode 100644
index 0000000..7186316
--- /dev/null
+++ b/frontend/include/Parser.h
@@ -0,0 +1,21 @@
+//
+// Created by 邓实诚 on 2024/4/29.
+//
+
+#ifndef SCNUCC_PARSER_H
+#define SCNUCC_PARSER_H
+#include "Lexer.h"
+
+namespace SCNUCC {
+class Parser {
+private:
+  std::unique_ptr<Lexer> lexer;
+public:
+  Parser();
+
+  ~Parser();
+
+  void parse();
+};
+}
+#endif //SCNUCC_PARSER_H
diff --git a/frontend/src/Lexer.cpp b/frontend/src/Lexer.cpp
new file mode 100644
index 0000000..95c4f61
--- /dev/null
+++ b/frontend/src/Lexer.cpp
@@ -0,0 +1,20 @@
+//
+// Created by 邓实诚 on 2024/4/29.
+//
+#include "Lexer.h"
+#include <iostream>
+
+namespace SCNUCC {
+Lexer::Lexer() {
+
+  std::cout << "Lexer created" << std::endl;
+}
+
+Lexer::~Lexer() {
+  std::cout << "Lexer destroyed" << std::endl;
+}
+
+void Lexer::scan() {
+  std::cout << "Scanning..." << std::endl;
+}
+}
\ No newline at end of file
diff --git a/frontend/src/Parser.cpp b/frontend/src/Parser.cpp
new file mode 100644
index 0000000..dc2d08c
--- /dev/null
+++ b/frontend/src/Parser.cpp
@@ -0,0 +1,22 @@
+//
+// Created by 邓实诚 on 2024/4/29.
+//
+#include <iostream>
+#include "Lexer.h"
+#include "Parser.h"
+
+namespace SCNUCC {
+Parser::Parser() {
+  lexer = std::make_unique<Lexer>();
+  std::cout << "Parser created" << std::endl;
+}
+
+Parser::~Parser() {
+  std::cout << "Parser destroyed" << std::endl;
+}
+
+void Parser::parse() {
+  lexer->scan();
+  std::cout << "Parsing..." << std::endl;
+}
+}
\ No newline at end of file
diff --git a/sysY2022/only_main.sys b/sysY2022/only_main.sys
new file mode 100644
index 0000000..e9cdae1
--- /dev/null
+++ b/sysY2022/only_main.sys
@@ -0,0 +1,3 @@
+int main() {
+    return 0;
+}
\ No newline at end of file
-- 
GitLab


From 8acdca1df2c737a71459fe235205ce0a1299dfb2 Mon Sep 17 00:00:00 2001
From: HonestDeng <2958906959@qq.com>
Date: Tue, 30 Apr 2024 01:23:38 +0800
Subject: [PATCH 2/3] test Pull Request

---
 README.md | 1 +
 1 file changed, 1 insertion(+)

diff --git a/README.md b/README.md
index dab5e38..60b3f3a 100644
--- a/README.md
+++ b/README.md
@@ -2,6 +2,7 @@
 
 
 
+
 ## Getting started
 
 To make it easy for you to get started with GitLab, here's a list of recommended next steps.
-- 
GitLab


From fc6a7639e63eb271c2b6959f7c8dfb48c2f15c60 Mon Sep 17 00:00:00 2001
From: HonestDeng <2958906959@qq.com>
Date: Tue, 30 Apr 2024 02:05:21 +0800
Subject: [PATCH 3/3] =?UTF-8?q?=E4=BF=AE=E6=94=B9=E4=BA=86ci?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

---
 .gitlab-ci.yml | 5 +++++
 1 file changed, 5 insertions(+)

diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml
index 2661c89..51ae5cc 100644
--- a/.gitlab-ci.yml
+++ b/.gitlab-ci.yml
@@ -16,6 +16,11 @@
 # This specific template is located at:
 # https://gitlab.com/gitlab-org/gitlab/-/blob/master/lib/gitlab/ci/templates/Getting-Started.gitlab-ci.yml
 
+workflow:
+  rules:
+    - if: $CI_PIPELINE_SOURCE == 'merge_request_event'
+    - if: $CI_PIPELINE_SOURCE == 'push'
+
 stages:          # List of stages for jobs, and their order of execution
   - build
   - test
-- 
GitLab