Skip to content

Commit

Permalink
Safe(r) math; Prep for evaluator
Browse files Browse the repository at this point in the history
  • Loading branch information
jcmdln committed Sep 23, 2023
1 parent 2fe1dd3 commit 5329634
Show file tree
Hide file tree
Showing 20 changed files with 257 additions and 83 deletions.
74 changes: 49 additions & 25 deletions .clang-tidy
Original file line number Diff line number Diff line change
Expand Up @@ -4,28 +4,52 @@
---
FormatStyle: file
HeaderFilterRegex: ""
Checks: >
-*,
bugprone-*,
-bugprone-easily-swappable-parameters,
cert-*-c,
clang-analyzer-*,
-clang-analyzer-security.insecureAPI.DeprecatedOrUnsafeBufferHandling,
misc-*,
-misc-no-recursion,
performance-*,
portability-*,
readability-*,
-readability-braces-around-statements,
-readability-function-cognitive-complexity,
-readability-identifier-length,
-readability-magic-numbers,
WarningsAsErrors: >
-*,
bugprone-*,
cert-*-c,
clang-analyzer-*,
misc-*,
performance-*,
portability-*,
readability-*,
InheritParentConfig: false

Checks: |
-*
bugprone-*
-bugprone-easily-swappable-parameters
cert-*-c
clang-analyzer-core
-clang-analyzer-core.uninitialized.NewArraySize
clang-analyzer-deadcode
clang-analyzer-nullability
clang-analyzer-security
-clang-analyzer-security.insecureAPI.decodeValueOfObjCType
-clang-analyzer-security.insecureAPI.DeprecatedOrUnsafeBufferHandling
clang-analyzer-unix
clang-analyzer-valist
misc-*
-misc-no-recursion
performance-*
portability-*
readability-*
-readability-braces-around-statements
-readability-function-cognitive-complexity
-readability-identifier-length
-readability-magic-numbers
WarningsAsErrors: |
-*
bugprone-*
-bugprone-easily-swappable-parameters
cert-*-c
clang-analyzer-core
-clang-analyzer-core.uninitialized.NewArraySize
clang-analyzer-deadcode
clang-analyzer-nullability
clang-analyzer-security
-clang-analyzer-security.insecureAPI.decodeValueOfObjCType
-clang-analyzer-security.insecureAPI.DeprecatedOrUnsafeBufferHandling
clang-analyzer-unix
clang-analyzer-valist
misc-*
-misc-no-recursion
performance-*
portability-*
readability-*
-readability-braces-around-statements
-readability-function-cognitive-complexity
-readability-identifier-length
-readability-magic-numbers
24 changes: 11 additions & 13 deletions .github/workflows/lint.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ on:
paths:
- .clang-format
- .clang-tidy
- .clangd
- .github/workflows/lint.yml
- .prettierrc.yml
- include/**/*.h
Expand All @@ -19,6 +20,7 @@ on:
paths:
- .clang-format
- .clang-tidy
- .clangd
- .github/workflows/lint.yml
- .prettierrc.yml
- include/**/*.h
Expand All @@ -28,7 +30,7 @@ on:
workflow_dispatch:

jobs:
test:
lint:
strategy:
fail-fast: false
matrix:
Expand All @@ -37,7 +39,9 @@ jobs:
- name: clang-tidy
os:
- name: ubuntu-latest
deps: libasan5 libgc-dev libreadline-dev libubsan1 meson pkgconf
deps: >
clang clang-format clang-tidy libasan5 libubsan1 lld meson
libgc-dev libreadline-dev
name: ${{ matrix.linter.name }} (${{ matrix.os.name }})
runs-on: ${{ matrix.os.name }}
Expand All @@ -55,23 +59,17 @@ jobs:
- name: Install system dependencies
run: |
sudo apt-get update
sudo apt-get install -y ${{ matrix.os.deps }}
sudo apt-get install -y --no-install-recommends --no-install-suggests ${{ matrix.os.deps }}
- name: Run Meson
- name: Test Ploy (Debug)
env:
CC: clang
CC_LD: lld
NINJA: ninja
run: |
meson --version
meson builddir -Dbuildtype=debugoptimized -Dwerror=true -Db_sanitize=address,undefined
- name: Build Ploy (Debug)
run: |
ninja --version
ninja -C builddir
meson setup builddir -Db_sanitize=address,undefined -Dbuildtype=debugoptimized -Dwerror=true
ninja -C builddir test
- name: Run ${{ matrix.linter.name }}
run: |
ninja --version
${{ matrix.linter.name }} --version
ninja -C builddir ${{ matrix.linter.name }}
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@
!include/**/
!include/**/*.h
!include/**/meson.build
!ploy/
!ploy/**/
!ploy/**/*.ploy
!src/
!src/**/
!src/**/*.c
Expand Down
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ By default, Meson will perform a simple release build. See `default_options` in

```sh
sudo dnf install -y gc-devel meson pkgconf readline-devel
meson builddir
meson setup builddir
ninja -C builddir
```

Expand All @@ -35,7 +35,7 @@ the specified options with Meson:

```sh
sudo dnf install -y clang-tools-extra gc-devel libasan libubsan meson pkgconf readline-devel
meson builddir -Dbuildtype=debugoptimized -Dwerror=true -Db_sanitize=address,undefined
meson setup builddir -Db_sanitize=address,undefined -Dbuildtype=debugoptimized -Dwerror=true
ninja -C builddir
```

Expand Down
13 changes: 13 additions & 0 deletions include/ploy/evaluator.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
// SPDX-License-Identifier: ISC
//
// Copyright (c) 2023 Johnathan C Maudlin <[email protected]>

#ifndef PLOY_EVALUATOR_H
#define PLOY_EVALUATOR_H
#pragma once

#include "type.h"

Object const *evaluator(Object const *object);

#endif // PLOY_EVALUATOR_H
14 changes: 10 additions & 4 deletions include/ploy/math.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,18 @@

#include "type.h"

//
// Arithmetic
//

Object const *Add(Object const *object);
Object const *Divide(Object const *object);
Object const *Multiply(Object const *object);
Object const *Subtract(Object const *object);
Object const *Multiply(Object const *object);
Object const *Divide(Object const *object);

// Object const *Exponent(Object const *object);
// Object const *Modulo(Object const *object);
Object const *Exponent(Object const *object);
Object const *Log(Object const *object);
Object const *Modulo(Object const *object);
Object const *NthRoot(Object const *object);

#endif // PLOY_MATH_H
13 changes: 13 additions & 0 deletions include/ploy/printer.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
// SPDX-License-Identifier: ISC
//
// Copyright (c) 2023 Johnathan C Maudlin <[email protected]>

#ifndef PLOY_PRINTER_H
#define PLOY_PRINTER_H
#pragma once

#include "type.h"

void printer(Object const *object);

#endif // PLOY_PRINTER_H
13 changes: 13 additions & 0 deletions include/ploy/reader.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
// SPDX-License-Identifier: ISC
//
// Copyright (c) 2023 Johnathan C Maudlin <[email protected]>

#ifndef PLOY_READER_H
#define PLOY_READER_H
#pragma once

#include "type.h"

Object const *reader(char const *input);

#endif // PLOY_READER_H
14 changes: 11 additions & 3 deletions meson.build
Original file line number Diff line number Diff line change
Expand Up @@ -87,12 +87,17 @@ readline = dependency('readline', required: true)
ploy_include_dirs = include_directories('include')
ploy_include = files(
'include/ploy/core.h',
'include/ploy/evaluator.h',
'include/ploy/math.h',
'include/ploy/printer.h',
'include/ploy/reader.h',
'include/ploy/type.h',
)

ploy_src_main = files('src/main.c')
ploy_src = files(
'src/core.c',
'src/evaluator.c',
'src/math.c',
'src/printer.c',
'src/reader.c',
Expand All @@ -117,7 +122,10 @@ subdir('test')
# Lints
#

ploy_files = [] + ploy_include + ploy_src + ploy_src_main + ploy_test
run_target('clang-format', command: [
'clang-format', '--dry-run', '--verbose', '--Werror',
ploy_include, ploy_src, ploy_src_main, ploy_test])

run_target('clang-format', command: ['clang-format', '--dry-run', '--Werror', ploy_files])
run_target('clang-tidy', command: ['clang-tidy', '-p', '.', ploy_files])
run_target('clang-tidy', command: [
'clang-tidy', '--quiet', '-p', 'builddir/',
ploy_include, ploy_src, ploy_src_main, ploy_test])
12 changes: 12 additions & 0 deletions ploy/demo.ploy
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
; This is a comment
# This is also a comment

(define greeting "Hello, world!")

(define greet (lambda (message)
(print message)
))

(greet greeting)

(print (format "The answer is {}." (+ 40 2)))
10 changes: 6 additions & 4 deletions src/core.c
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,16 @@
#include <gc/gc.h>

#include <ploy/core.h>

void printer(Object const *object);
Object const *reader(char const *input);
#include <ploy/evaluator.h>
#include <ploy/printer.h>
#include <ploy/reader.h>

Object const *
Append(Object const *target, Object const *const object)
{
if (!object) return Error("Append: object is NULL");
if (!target || target->type == TYPE_NIL) return Cons(object, &NIL);

if (target->type != TYPE_LIST) target = Cons(target, &NIL);

Object const *head = target;
Expand All @@ -27,6 +28,7 @@ Append(Object const *target, Object const *const object)
}

if (cdr->type != TYPE_NIL) return Error("Append: cdr->type not of TYPE_NIL");

head->list->cdr = Cons(object, &NIL);
return target;
}
Expand Down Expand Up @@ -68,7 +70,7 @@ Eval(Object const *const object)
{
if (!object) return Error("Eval: object is NULL");

return object;
return evaluator(object);
}

Object const *
Expand Down
15 changes: 15 additions & 0 deletions src/evaluator.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
// SPDX-License-Identifier: ISC
//
// Copyright (c) 2023 Johnathan C Maudlin <[email protected]>

#include <stdlib.h>

#include <gc/gc.h>

#include <ploy/core.h>

Object const *
evaluator(Object const *object)
{
return object;
}
1 change: 1 addition & 0 deletions src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
#include <readline/readline.h>

#include <ploy/core.h>
#include <ploy/math.h>

int repf(char const *path);
int repl(void);
Expand Down
Loading

0 comments on commit 5329634

Please sign in to comment.