Skip to content

Commit

Permalink
Refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
jcmdln committed Sep 4, 2024
1 parent 9437cfb commit b169ec1
Show file tree
Hide file tree
Showing 25 changed files with 495 additions and 481 deletions.
2 changes: 1 addition & 1 deletion .clang-format
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ BreakBeforeBinaryOperators: None
BreakBeforeBraces: Custom
BreakBeforeTernaryOperators: false
BreakStringLiterals: false
ColumnLimit: 120
ColumnLimit: 100
CompactNamespaces: false
ConstructorInitializerIndentWidth: 4
ContinuationIndentWidth: 4
Expand Down
6 changes: 2 additions & 4 deletions .clang-tidy
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,10 @@ Checks: |
readability-*
-bugprone-easily-swappable-parameters
-clang-analyzer-security.insecureAPI
-misc-include-cleaner
-misc-no-recursion
-readability-braces-around-statements
-readability-function-cognitive-complexity
-readability-identifier-length
-readability-magic-numbers
-readability-non-const-parameter
WarningsAsErrors: |
*
-misc-no-recursion
2 changes: 1 addition & 1 deletion .editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ end_of_line = "lf"
indent_style = "space"
insert_final_newline = true
trim_trailing_whitespace = true
max_line_length = 120
max_line_length = 100

[*.{c,h}]
indent_size = 4
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,6 @@ jobs:
- name: clang-format
run: ninja -C build/ clang-format

# TODO(jcmdln): Only in actions, clang-tidy warns about suppressed warnings
# TODO: jcmdln: Only in actions, clang-tidy warns about suppressed warnings
- name: clang-tidy
run: ninja -C build/ clang-tidy
4 changes: 2 additions & 2 deletions .vscode/extensions.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
"editorconfig.editorconfig",
"esbenp.prettier-vscode",
"llvm-vs-code-extensions.vscode-clangd",
"ms-vscode.cmake-tools",
"redhat.vscode-yaml",
"vadimcn.vscode-lldb",
"ms-vscode.cmake-tools"
"vadimcn.vscode-lldb"
]
}
53 changes: 25 additions & 28 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -1,51 +1,48 @@
{
"clangd.arguments": [
// Don't automatically add missing headers
"--header-insertion=never"
],

// Don't immediately pester the user on open of the project
"cmake.configureOnOpen": false,
// Don't focusing CMake logs on changes
"cmake.revealLog": "never",
// Don't clutter the statusbar
"cmake.useProjectStatusView": true,

// Make Prettier the default formatter
"editor.defaultFormatter": "esbenp.prettier-vscode",
"editor.insertSpaces": true,

// Ensure files have the correct language association
"files.associations": {
".clang-format": "yaml",
".clang-tidy": "yaml",
".clangd": "yaml",
"*.c": "c",
"*.h": "c"
"*.h": "c",
"*.ploy": "clojure"
},

// Exclude targets from being watched for changes
"files.watcherExclude": {
"**/builddir/**": true
"build/": true
},
"files.insertFinalNewline": true,
"files.trimFinalNewlines": true,
"files.trimTrailingWhitespace": true,

//
// Extensions
//

"clangd.arguments": ["--header-insertion=never"],

"cmake.configureOnOpen": false,
"cmake.revealLog": "never",

"prettier.useEditorConfig": true,

// Associate YAML schemas with targets
"yaml.schemas": {
"https://json.schemastore.org/github-workflow.json": [
"${workspaceFolder}/.github/workflows/*.yml"
]
},

// Per-language settings
//
// Languages
//

"[c]": {
"editor.defaultFormatter": "llvm-vs-code-extensions.vscode-clangd",
"editor.insertSpaces": false,
"editor.tabSize": 4
},
"[json]": {
"editor.defaultFormatter": "esbenp.prettier-vscode"
},
"[jsonc]": {
"editor.defaultFormatter": "esbenp.prettier-vscode"
}

"[json]": { "editor.defaultFormatter": "esbenp.prettier-vscode" },
"[jsonc]": { "editor.defaultFormatter": "esbenp.prettier-vscode" },
"[yaml]": { "editor.defaultFormatter": "esbenp.prettier-vscode" }
}
53 changes: 22 additions & 31 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ cmake_minimum_required(VERSION 3.13)
project(Ploy LANGUAGES C VERSION 0.0.0)

add_executable(ploy src/main.c src/core.c src/eval.c src/math.c src/read.c)
target_compile_features(ploy PRIVATE c_std_11)
target_compile_options(ploy PRIVATE -DPLOY_VERSION=\"${CMAKE_PROJECT_VERSION}\")
target_include_directories(ploy PUBLIC "${CMAKE_CURRENT_SOURCE_DIR}/include")

Expand All @@ -13,35 +14,23 @@ if(NOT CMAKE_BUILD_TYPE)
set(CMAKE_BUILD_TYPE "Release")
endif()

target_compile_features(ploy PRIVATE c_std_11)
target_compile_options(ploy PRIVATE
-fshort-wchar -fstrict-aliasing -funsigned-char
-Wall -Wextra -pedantic
-Werror=cast-qual
-Werror=conversion
-Werror=implicit-int
-Werror=strict-prototypes
-Werror=switch
-Werror=vla
-Werror=write-strings
)

option(PLOY_LTO "Build ploy with link-time optimization" OFF)
if(PLOY_LTO)
target_compile_options(ploy PRIVATE -flto)
target_link_options(ploy PRIVATE -flto)
endif()

option(PLOY_USE_ASAN "Build ploy with AddressSanitizer" OFF)
if(PLOY_USE_ASAN)
target_compile_options(ploy PRIVATE -fsanitize=address,undefined)
target_link_options(ploy PRIVATE -fsanitize=address,undefined)
endif()
if (CMAKE_C_COMPILER_ID STREQUAL "Clang" OR CMAKE_C_COMPILER_ID STREQUAL "GNU")
target_compile_options(ploy PRIVATE
-fshort-wchar -fstrict-aliasing -funsigned-char
-Wall -Wextra -pedantic
-Werror=cast-qual
-Werror=conversion
-Werror=implicit-int
-Werror=strict-prototypes
-Werror=switch
-Werror=vla
-Werror=write-strings
)

option(PLOY_USE_TSAN "Build ploy with ThreadSanitizer" OFF)
if(PLOY_USE_TSAN)
target_compile_options(ploy PRIVATE -fsanitize=thread,undefined)
target_link_options(ploy PRIVATE -fsanitize=thread,undefined)
if (CMAKE_BUILD_TYPE STREQUAL "Debug")
target_compile_options(ploy PRIVATE -fsanitize=address,undefined)
target_link_options(ploy PRIVATE -fsanitize=address,undefined)
endif()
endif()

#
Expand All @@ -50,7 +39,7 @@ endif()

find_package(PkgConfig)
if(PKG_CONFIG_FOUND)
pkg_check_modules(bdwgc REQUIRED IMPORTED_TARGET bdw-gc>=8)
pkg_check_modules(bdwgc REQUIRED IMPORTED_TARGET bdw-gc>=8)
pkg_check_modules(readline REQUIRED IMPORTED_TARGET readline>=8)
target_link_libraries(ploy PUBLIC PkgConfig::bdwgc PkgConfig::readline)
endif()
Expand All @@ -60,13 +49,15 @@ endif()
#

add_custom_target(clang-format
COMMAND clang-format --dry-run --verbose --Werror
COMMAND
clang-format --dry-run --verbose --Werror
"${CMAKE_CURRENT_SOURCE_DIR}/include/*.h"
"${CMAKE_CURRENT_SOURCE_DIR}/src/*.c"
)

add_custom_target(clang-tidy
COMMAND clang-tidy -p "${CMAKE_CURRENT_SOURCE_DIR}/build"
COMMAND
clang-tidy -p "${CMAKE_CURRENT_SOURCE_DIR}/build"
"${CMAKE_CURRENT_SOURCE_DIR}/include/*.h"
"${CMAKE_CURRENT_SOURCE_DIR}/src/*.c"
)
Expand Down
38 changes: 21 additions & 17 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,18 @@ Ploy is a (work-in-progress) lisp-like language for my own fun and learning.

Building Ploy requires the following:

- A [c11] compiler
- CMake
- [bdwgc]
- [GNU Readline][readline]
- A [c11](<https://en.wikipedia.org/wiki/C11_(C_standard_revision)>) compiler
- [CMake](https://github.com/Kitware/CMake)
- [bdwgc](https://github.com/ivmai/bdwgc)
- [GNU Readline](https://git.savannah.gnu.org/cgit/readline.git)

[bdwgc]: https://github.com/ivmai/bdwgc
[c11]: https://en.wikipedia.org/wiki/C11_(C_standard_revision)
[cmake]: https://mesonbuild.com/
[readline]: https://git.savannah.gnu.org/cgit/readline.git
# Example

```sh
#!/usr/bin/env ploy

(print "Hello, world!")
```

# Using

Expand All @@ -20,18 +23,18 @@ Building Ploy requires the following:

```sh
sudo dnf install -y cmake gc-devel ninja-build pkgconf readline-devel
cmake -B build
cmake -B build -DCMAKE_EXPORT_COMPILE_COMMANDS:BOOL=ON
ninja -C build
```

### Debug

`-DCMAKE_BUILD_TYPE:STRING="Debug"` requires ASan/UBSan. If you want to avoid
these dependencies use `-DCMAKE_BUILD_TYPE:STRING="RelWithDebInfo"` instead.

```sh
sudo dnf install -y clang-tools-extra libasan libubsan
cmake -B build \
-DCMAKE_BUILD_TYPE:STRING="Debug" \
-DCMAKE_EXPORT_COMPILE_COMMANDS:BOOL=ON \
-DPLOY_USE_ASAN:BOOL=ON
cmake -B build -DCMAKE_BUILD_TYPE:STRING="Debug" -DCMAKE_EXPORT_COMPILE_COMMANDS:BOOL=ON
ninja -C build
```

Expand Down Expand Up @@ -60,11 +63,12 @@ ninja -C build test

```sh
$ ./build/ploy -h
usage: ploy [-h] [-e EXPR | -f FILE]
usage: ploy [-h] [-v] [-e EXPR] [-f FILE]

-e Evaluate an expression
-f Evaluate contents of a FILE
-h Show help output
-h Show help output
-v Show version
-e EXPR Evaluate an expression
-f FILE Evaluate contents of a FILE
```

# Special Thanks
Expand Down
5 changes: 5 additions & 0 deletions example/demo.ploy
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,14 @@
(print "Demo: Control Flow")
))

(define demo-math (lambda()
(print "Demo: Math")
))

#
# Execution
#

(demo-basics)
(demo-control-flow)
(demo-math)
82 changes: 47 additions & 35 deletions include/ploy.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,53 +3,65 @@
#include <stdbool.h>
#include <stdint.h>

typedef struct Object {
enum { NIL = 0, BOOLEAN, ERROR, LAMBDA, LIST, NUMBER, STRING, SYMBOL } type;
typedef struct Ploy Ploy;
struct Ploy {
enum {
PloyNIL = 0,
PloyBOOLEAN,
PloyERROR,
PloyLAMBDA,
PloyLIST,
PloyNUMBER,
PloySTRING,
PloySYMBOL
} type;
union { // clang-format off
bool boolean;
char const *error;
struct { Ploy *args, *body; } *lambda;
struct { Ploy *element, *next; } *list;
int64_t number;
char const *error, *string, *symbol;
struct { struct Object *args, *body; } *lambda;
struct { struct Object *element, *next; } *list;
char const *string;
char const *symbol;
}; // clang-format on
} Object;
};

//
// Core
//

static Object *const Nil = &(Object){ .type = NIL };
static Object *const True = &(Object){ .type = NIL, .boolean = true };
static Object *const False = &(Object){ .type = NIL, .boolean = false };

Object *Append(Object *target, Object *object);
Object *Apply(Object *object);
Object *Car(Object *object);
Object *Cdr(Object *object);
Object *Cons(Object *car, Object *cdr);
Object *Define(Object *env, Object *args, Object *body);
Object *Error(char const *error);
Object *Eval(Object *object);
Object *For(Object *expr, Object *body);
Object *If(Object *expr, Object *body);
Object *Lambda(Object *args, Object *body);
Object *Let(Object *env, Object *args, Object *body);
Object *Number(int64_t number);
Object *Print(Object *object);
Object *Read(char const *input);
Object *Reverse(Object *object);
Object *String(char const *string);
static Ploy *const PloyNil = &(Ploy){ .type = PloyNIL };
static Ploy *const PloyTrue = &(Ploy){ .type = PloyBOOLEAN, .boolean = true };
static Ploy *const PloyFalse = &(Ploy){ .type = PloyBOOLEAN, .boolean = false };

Ploy *PloyAppend(Ploy *target, Ploy *object);
Ploy *PloyApply(Ploy *object);
Ploy *PloyCar(Ploy *object);
Ploy *PloyCdr(Ploy *object);
Ploy *PloyCons(Ploy *car, Ploy *cdr);
Ploy *PloyDefine(Ploy *env, Ploy *args, Ploy *body);
Ploy *PloyError(char const *error);
Ploy *PloyEval(Ploy *object);
Ploy *PloyFor(Ploy *expr, Ploy *body);
Ploy *PloyIf(Ploy *expr, Ploy *body);
Ploy *PloyLambda(Ploy *args, Ploy *body);
Ploy *PloyLet(Ploy *env, Ploy *args, Ploy *body);
Ploy *PloyNumber(int64_t number);
Ploy *PloyPrint(Ploy *object);
Ploy *PloyRead(char const *input);
Ploy *PloyReverse(Ploy *object);
Ploy *PloyString(char const *string);

//
// Math
//

Object *Add(Object *object);
Object *Subtract(Object *object);
Object *Multiply(Object *object);
Object *Divide(Object *object);
Ploy *PloyAdd(Ploy *object);
Ploy *PloySubtract(Ploy *object);
Ploy *PloyMultiply(Ploy *object);
Ploy *PloyDivide(Ploy *object);

// Object *Exponent(Object *object);
// Object *Log(Object *object);
// Object *Modulo(Object *object);
// Object *NthRoot(Object *object, int64_t nth);
// Ploy *PloyExponent(Ploy *object);
// Ploy *PloyLog(Ploy *object);
// Ploy *PloyModulo(Ploy *object);
// Ploy *PloyNthRoot(Ploy *object, int64_t nth);
Loading

0 comments on commit b169ec1

Please sign in to comment.