From 9c02a86a517b560f2e4a676a3447eca7b151971e Mon Sep 17 00:00:00 2001 From: servostar Date: Sun, 4 Feb 2024 16:23:32 +0100 Subject: [PATCH] added canonical project structure --- .gitignore | 6 +++ CMakeLists.txt | 102 ++++++++++++++++++++++++++++++++++++++++++++++ Dockerfile | 10 +++++ README.md | 47 +++++++++++++++++++++ sdk/Dockerfile | 13 ++++++ src/lex/lexer.l | 11 +++++ src/main.c | 6 +++ src/yacc/parser.y | 15 +++++++ 8 files changed, 210 insertions(+) create mode 100644 .gitignore create mode 100644 CMakeLists.txt create mode 100644 Dockerfile create mode 100644 README.md create mode 100644 sdk/Dockerfile create mode 100644 src/lex/lexer.l create mode 100644 src/main.c create mode 100644 src/yacc/parser.y diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..c396473 --- /dev/null +++ b/.gitignore @@ -0,0 +1,6 @@ +bin +cmake-build-debug +.vscode +.idea +tmp +.git \ No newline at end of file diff --git a/CMakeLists.txt b/CMakeLists.txt new file mode 100644 index 0000000..0ba7d85 --- /dev/null +++ b/CMakeLists.txt @@ -0,0 +1,102 @@ +cmake_minimum_required(VERSION 3.15...3.25) + +# Canonical project structure +# Header must be included this way: #include +# +# ├─ res +# ├─ src +# │ ├─ lex +# │ │ └─ lexer.l +# │ ├─ yacc +# │ │ └─ parser.y +# │ ├─ module +# │ │ ├─ header.h +# │ │ └─ source.c +# │ └─ main.c +# └─ tests +# └─ test.c + +project(gemstone + VERSION 0.1.0 + DESCRIPTION "programming language compiler" + LANGUAGES C) + +# ------------------------------------------------ # +# Lex # +# ------------------------------------------------ # + +set(LEX_SOURCE_FILE ${PROJECT_SOURCE_DIR}/src/lex/lexer.l) +set(LEX_GENERATED_SOURCE_FILE ${PROJECT_SOURCE_DIR}/src/lex/lexer.ll.c) + +add_custom_command(OUTPUT ${LEX_GENERATED_SOURCE_FILE} + COMMAND lex + ARGS -o ${LEX_GENERATED_SOURCE_FILE} ${LEX_SOURCE_FILE} + COMMENT "generate C source file for lexer" + VERBATIM) + +# ------------------------------------------------ # +# Yacc # +# ------------------------------------------------ # + +set(YACC_SOURCE_FILE ${PROJECT_SOURCE_DIR}/src/yacc/parser.y) +set(YACC_GENERATED_SOURCE_FILE ${PROJECT_SOURCE_DIR}/src/yacc/parser.tab.c) + +add_custom_command(OUTPUT ${YACC_GENERATED_SOURCE_FILE} + COMMAND yacc + ARGS -d -o ${YACC_GENERATED_SOURCE_FILE} ${YACC_SOURCE_FILE} + COMMENT "generate C source file for parser" + VERBATIM) + +# ------------------------------------------------ # +# Source # +# ------------------------------------------------ # + +file(GLOB_RECURSE SOURCE_FILES src/*.c) + +# define default compile flags +set(FLAGS -Wall -Wconversion) + +# ------------------------------------------------ # +# Target RELEASE # +# ------------------------------------------------ # + +add_executable(release + ${SOURCE_FILES} + ${LEX_GENERATED_SOURCE_FILE} + ${YACC_GENERATED_SOURCE_FILE}) + +set_target_properties(release + PROPERTIES + OUTPUT_NAME "gsc" + RUNTIME_OUTPUT_DIRECTORY "bin/release") + +# compiler flags targeting a 64-bit GCC release environment +# flags: +# - m64: build for 64-bit +# - O3: optimization level 3 +# - fprefetch-loop-arrays: pre load arrays used in loops by using prefetch instruction +# - mrecip: make use RCPSS and RSQRTSS instructions +target_compile_options(release PUBLIC ${FLAGS} -m64 -O3 -fprefetch-loop-arrays -mrecip) + +# add src directory as include path +target_include_directories(release PUBLIC src) + +# ------------------------------------------------ # +# Target DEBUG # +# ------------------------------------------------ # + +add_executable(debug + ${SOURCE_FILES} + ${LEX_GENERATED_SOURCE_FILE} + ${YACC_GENERATED_SOURCE_FILE}) + +set_target_properties(debug + PROPERTIES + OUTPUT_NAME "gsc" + RUNTIME_OUTPUT_DIRECTORY "bin/debug") + +# compiler flags targeting a GCC debug environment +target_compile_options(debug PUBLIC ${FLAGS} -g) + +# add src directory as include path +target_include_directories(debug PUBLIC src) diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..d694bd8 --- /dev/null +++ b/Dockerfile @@ -0,0 +1,10 @@ +FROM servostar/gemstone:sdk-0.1.0-alma-9.3 +LABEL authors="servostar" +LABEL version="0.1.0" +LABEL description="docker image for setting up the build pipeline on SDK" +LABEL website="https://github.com/Servostar/gemstone" + +COPY --chown=lorang src /home/lorang/src +COPY --chown=lorang CMakeLists.txt /home/lorang/ + +RUN cmake . \ No newline at end of file diff --git a/README.md b/README.md new file mode 100644 index 0000000..e1bced7 --- /dev/null +++ b/README.md @@ -0,0 +1,47 @@ +# Gemstone + +Gemstone is a programming language compiler written in C with lex and yacc. + +## Build +The build pipeline is configured with CMake in the file CMakeLists.txt. +In order to avoid dependency and configuration issues the recommended way to build is by using the provided docker containers. +All tools required for building (`cmake`, `make`, `gcc`, `lex`, `yacc`) are installed inside the SDK container (see Dockerfile sdk/Dockerfile). +For creating the build pipeline build the Dockerfile in the root folder of this repository. This takes the current SDK and copies the source files into the home of the build user. +Then the make targets are generated. Running `make release` will build gemstone from source in release mode. +The generated binaries can be found either in `bin/release/gsc` or `bin/debug/gsc` depending on the chosen target. +The following graph visualizes the build pipeline: +``` + SDK (environment) + │ + │ configure build environment + │ cmake, make, gcc, yacc, lex + │ + ▼ + Devkit (pipeline) + │ + │ create build pipeline + │ create make targets + ▼ + Pipeline + + +yacc (generate files) GCC (compile) Extra Source Files (src/*.c) +│ │ │ +├─ parser.tab.h ─────────────►│◄────────────────────┘ +│ │ +└─ parser.tab.c ─────────────►│ + │ +lex (generate file) │ +│ │ +└─ lexer.ll.c ──────────────►│ + │ + ▼ + gsc +``` + +## Docker images +Currently, the SDK is based on Almalinux 9.3, an open source distro binary compatible to RHEL 9.3. + +The following images can be found in the offical repository at [Docker Bub](https://hub.docker.com/r/servostar/gemstone): +- SDK +- Devkit \ No newline at end of file diff --git a/sdk/Dockerfile b/sdk/Dockerfile new file mode 100644 index 0000000..296a627 --- /dev/null +++ b/sdk/Dockerfile @@ -0,0 +1,13 @@ +FROM almalinux:9.3 +LABEL authors="servostar" +LABEL version="0.1.0" +LABEL description="base image for building the gemstone programming language compiler" +LABEL website="https://github.com/Servostar/gemstone" + +# install dependencies +RUN yum install cmake gcc flex byacc -y + +# create user for build +RUN adduser lorang +WORKDIR /home/lorang +USER lorang \ No newline at end of file diff --git a/src/lex/lexer.l b/src/lex/lexer.l new file mode 100644 index 0000000..a17888b --- /dev/null +++ b/src/lex/lexer.l @@ -0,0 +1,11 @@ +%option noyywrap +%{ + #include + + int yyLineNumber = 1; + int yylex(); +%} + +%% +.; +%% \ No newline at end of file diff --git a/src/main.c b/src/main.c new file mode 100644 index 0000000..6b48e00 --- /dev/null +++ b/src/main.c @@ -0,0 +1,6 @@ +#include + +int main() { + yyparse(); + return 0; +} \ No newline at end of file diff --git a/src/yacc/parser.y b/src/yacc/parser.y new file mode 100644 index 0000000..fbe3d5a --- /dev/null +++ b/src/yacc/parser.y @@ -0,0 +1,15 @@ +%{ + extern int yylineno; + + int yyerror(char*); + + extern int yylex(); +%} + +%% +program: ; +%% + +int yyerror(char *s) { + return 0; +} \ No newline at end of file