From 649e33b69b0b17d2682267f4dbbf10ca35e1f37e Mon Sep 17 00:00:00 2001 From: Darcy Shen Date: Fri, 2 Aug 2024 13:58:28 +0800 Subject: [PATCH] Goldfish 17.10.0 (#3) --- .gitignore | 2 + AUTHORS | 2 +- bin/format | 2 + goldfish/scheme/base.scm | 2 +- goldfish/scheme/file.scm | 2 +- goldfish/srfi/srfi-1.scm | 2 +- goldfish/srfi/srfi-13.scm | 2 +- goldfish/srfi/srfi-16.scm | 2 +- goldfish/srfi/srfi-78.scm | 2 +- goldfish/srfi/srfi-8.scm | 2 +- src/goldfish.cpp | 151 ++++++++++++++++++------------ src/goldfish.hpp | 50 ++++++++++ tests/demo_error.scm | 1 + tests/demo_no_error.scm | 1 + tests/scheme/case-lambda-test.scm | 2 +- tests/srfi/srfi-16-test.scm | 2 +- tests/srfi/srfi-39-test.scm | 2 +- tests/srfi/srfi-78-test.scm | 4 +- tests/srfi/srfi-8-test.scm | 2 +- 19 files changed, 162 insertions(+), 73 deletions(-) create mode 100755 bin/format create mode 100644 src/goldfish.hpp create mode 100644 tests/demo_error.scm create mode 100644 tests/demo_no_error.scm diff --git a/.gitignore b/.gitignore index f8d1a70..c2a6286 100644 --- a/.gitignore +++ b/.gitignore @@ -2,3 +2,5 @@ build/ bin/goldfish goldfish/**/*-test.scm +compile_commands.json +.cache/ diff --git a/AUTHORS b/AUTHORS index 8ee9e8b..ed8492c 100644 --- a/AUTHORS +++ b/AUTHORS @@ -1,5 +1,5 @@ # The original authors of the SRFI reference IMPL are kept in indivisual files -# This AUTHORS file is for S7 SRFI authors. +# This AUTHORS file is for Goldfish Scheme authors. # # Names should be added to this file like so: # Name or Organization diff --git a/bin/format b/bin/format new file mode 100755 index 0000000..ebde04c --- /dev/null +++ b/bin/format @@ -0,0 +1,2 @@ +#!/bin/bash +clang-format-16 -i src/* diff --git a/goldfish/scheme/base.scm b/goldfish/scheme/base.scm index 9f31d15..3b047b0 100644 --- a/goldfish/scheme/base.scm +++ b/goldfish/scheme/base.scm @@ -1,5 +1,5 @@ ; -; Copyright (C) 2024 The S7 SRFI Authors +; Copyright (C) 2024 The Goldfish Scheme Authors ; ; Licensed under the Apache License, Version 2.0 (the "License"); ; you may not use this file except in compliance with the License. diff --git a/goldfish/scheme/file.scm b/goldfish/scheme/file.scm index 7720746..9f337e9 100644 --- a/goldfish/scheme/file.scm +++ b/goldfish/scheme/file.scm @@ -1,5 +1,5 @@ ; -; Copyright (C) 2024 The S7 SRFI Authors +; Copyright (C) 2024 The Goldfish Scheme Authors ; ; Licensed under the Apache License, Version 2.0 (the "License"); ; you may not use this file except in compliance with the License. diff --git a/goldfish/srfi/srfi-1.scm b/goldfish/srfi/srfi-1.scm index 97a226e..42d3908 100644 --- a/goldfish/srfi/srfi-1.scm +++ b/goldfish/srfi/srfi-1.scm @@ -8,7 +8,7 @@ ;;; hold me liable for its use. Please send bug reports to shivers@ai.mit.edu. ;;; -Olin ;;; -;;; Copyright (c) 2024 The S7 SRFI Authors +;;; Copyright (c) 2024 The Goldfish Scheme Authors ;;; Follow the same License as the original one (define-library (srfi srfi-1) diff --git a/goldfish/srfi/srfi-13.scm b/goldfish/srfi/srfi-13.scm index 176404a..ade8078 100644 --- a/goldfish/srfi/srfi-13.scm +++ b/goldfish/srfi/srfi-13.scm @@ -1,5 +1,5 @@ ; -; Copyright (C) 2024 The S7 SRFI Authors +; Copyright (C) 2024 The Goldfish Scheme Authors ; ; Licensed under the Apache License, Version 2.0 (the "License"); ; you may not use this file except in compliance with the License. diff --git a/goldfish/srfi/srfi-16.scm b/goldfish/srfi/srfi-16.scm index 0af2038..6f159a5 100644 --- a/goldfish/srfi/srfi-16.scm +++ b/goldfish/srfi/srfi-16.scm @@ -1,5 +1,5 @@ ; -; Copyright (C) 2024 The S7 SRFI Authors +; Copyright (C) 2024 The Goldfish Scheme Authors ; ; Licensed under the Apache License, Version 2.0 (the "License"); ; you may not use this file except in compliance with the License. diff --git a/goldfish/srfi/srfi-78.scm b/goldfish/srfi/srfi-78.scm index 218f0bf..cf49816 100644 --- a/goldfish/srfi/srfi-78.scm +++ b/goldfish/srfi/srfi-78.scm @@ -34,7 +34,7 @@ ; ; Naming convention "check:" is used only internally. ; -; Copyright (c) 2024 The S7 SRFI Authors +; Copyright (c) 2024 The Goldfish Scheme Authors ; Follow the same License as the original one (define-library (srfi srfi-78) diff --git a/goldfish/srfi/srfi-8.scm b/goldfish/srfi/srfi-8.scm index 3138131..c8c952f 100644 --- a/goldfish/srfi/srfi-8.scm +++ b/goldfish/srfi/srfi-8.scm @@ -1,5 +1,5 @@ ; -; Copyright (C) 2024 The S7 SRFI Authors +; Copyright (C) 2024 The Goldfish Scheme Authors ; ; Licensed under the Apache License, Version 2.0 (the "License"); ; you may not use this file except in compliance with the License. diff --git a/src/goldfish.cpp b/src/goldfish.cpp index dc3e8a8..14f3bf9 100644 --- a/src/goldfish.cpp +++ b/src/goldfish.cpp @@ -1,82 +1,115 @@ -/* 0-clause BSD */ +// +// Copyright (C) 2024 The Goldfish Scheme Authors +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, WITHOUT +// WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the +// License for the specific language governing permissions and limitations +// under the License. +// -#include -#include -#include +#include "goldfish.hpp" +#include "s7.h" -#include #include #include #include +#include -#ifndef _MSC_VER -#include -#include -#endif - -#include - +using std::cerr; +using std::cout; +using std::endl; +using std::string; +using std::vector; using std::filesystem::exists; using std::filesystem::path; -static s7_pointer -g_current_second (s7_scheme* sc, s7_pointer args) { - auto now= std::chrono::system_clock::now (); - // TODO: use std::chrono::tai_clock::now() when using C++ 20 - auto now_duration= now.time_since_epoch (); - double ts = std::chrono::duration (now_duration).count (); - s7_double res = ts; - return s7_make_real (sc, res); +void display_help() { + cout << "Goldfish Scheme " << goldfish_version << " by LiiiLabs" << endl; + cout << "--version\t" + << "display version" << endl; + cout << "-e \t" + << "-e '(+ 1 2)'" << endl; + cout << "-l FILE\t" + << "Load the scheme code on path" << endl; + cout << "FILE\t" + << "Load the scheme code on path and print the evaluated result" << endl; } -static void -glue_scheme_time (s7_scheme* sc) { - s7_pointer cur_env= s7_curlet (sc); - s7_define (sc, cur_env, s7_make_symbol (sc, "current-second"), - s7_make_typed_function (sc, "current-second", g_current_second, 0, - 0, false, "current-second", NULL)); +void display_version() { + cout << "Goldfish Scheme " << goldfish_version << " by LiiiLabs" << endl; + cout << "based on S7 Scheme " << S7_VERSION << "(" << S7_DATE << ")" << endl; } -int -main (int argc, char** argv) { - const path gf_root= - std::filesystem::path (argv[0]).parent_path ().parent_path (); - const path gf_lib = gf_root / "goldfish"; - const path gf_boot= gf_lib / "scheme" / "boot.scm"; +void display_for_invalid_options() { + cerr << "Invalid command line options!" << endl << endl; + display_help(); +} - if (!exists (gf_lib)) { - std::cerr - << "The load path for Goldfish Scheme Standard Library does not exist" - << std::endl; +void goldfish_eval_file(s7_scheme *sc, string path, bool quiet) { + s7_pointer result = s7_load(sc, path.c_str()); + if (!result) { + cerr << "Failed to load " << path << endl; + exit(-1); } - if (!exists (gf_boot)) { - std::cerr << "The boot.scm for Goldfish Scheme does not exist" << std::endl; + if (!quiet) { + cout << path << " => " << s7_object_to_c_string(sc, result) << endl; } +} - s7_scheme* sc; - sc= s7_init (); - s7_load (sc, gf_boot.string ().c_str ()); - s7_add_to_load_path (sc, gf_lib.string ().c_str ()); +void goldfish_eval_code(s7_scheme *sc, string code) { + s7_pointer x = s7_eval_c_string(sc, code.c_str()); + cout << s7_object_to_c_string(sc, x) << endl; +} - glue_scheme_time (sc); +int main(int argc, char **argv) { + // Check if the standard library and boot.scm exists + const path gf_root = path(argv[0]).parent_path().parent_path(); + const path gf_lib = gf_root / "goldfish"; + const path gf_boot = gf_lib / "scheme" / "boot.scm"; + if (!exists(gf_lib)) { + cerr << "The load path for Goldfish Scheme Standard Library does not exist" + << endl; + exit(-1); + } + if (!exists(gf_boot)) { + cerr << "The boot.scm for Goldfish Scheme does not exist" << endl; + exit(-1); + } - if (argc >= 2) { - if (strcmp (argv[1], "-e") == 0) /* repl -e '(+ 1 2)' */ - { - s7_pointer x; - x= s7_eval_c_string (sc, argv[2]); - fprintf (stdout, "%s\n", s7_object_to_c_string (sc, x)); - return (0); - } - if (strcmp (argv[1], "--version") == 0) { - fprintf (stdout, "s7: %s, %s\n", S7_VERSION, S7_DATE); - return (0); - } - errno= 0; - if (!s7_load (sc, argv[1])) { - fprintf (stderr, "%s: %s\n", strerror (errno), argv[1]); - return (2); + // Init the underlying S7 Scheme and add the load_path + s7_scheme *sc; + sc = s7_init(); + s7_load(sc, gf_boot.string().c_str()); + s7_add_to_load_path(sc, gf_lib.string().c_str()); + + // Glues for the Standard Library + glue_scheme_time(sc); + + // Command options + vector args(argv + 1, argv + argc); + if (args.size() == 0) { + display_help(); + } else if (args.size() == 1 && args[0].size() > 0 && args[0][0] == '-') { + if (args[0] == "--version") { + display_version(); + } else { + display_for_invalid_options(); } + } else if (args.size() == 2 && args[0] == "-e") { + goldfish_eval_code(sc, args[1]); + } else if (args.size() == 2 && args[0] == "-l") { + goldfish_eval_file(sc, args[1], true); + } else if (args.size() == 1 && args[0].size() > 0 && args[0][0] != '-') { + goldfish_eval_file(sc, args[0], false); + } else { + display_for_invalid_options(); } return 0; } diff --git a/src/goldfish.hpp b/src/goldfish.hpp new file mode 100644 index 0000000..fa66db7 --- /dev/null +++ b/src/goldfish.hpp @@ -0,0 +1,50 @@ +// +// Copyright (C) 2024 The Goldfish Scheme Authors +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, WITHOUT +// WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the +// License for the specific language governing permissions and limitations +// under the License. +// + +#include +#include +#include + +const int patch_version = 0; // Goldfish Patch Version +const int minor_version = S7_MAJOR_VERSION; // S7 Major Version +const int major_version = 17; // C++ Standard version + +const std::string goldfish_version = std::to_string(major_version) + .append(".") + .append(std::to_string(minor_version)) + .append(".") + .append(std::to_string(patch_version)); + +// Glues for (scheme time) +static s7_pointer f_current_second(s7_scheme *sc, s7_pointer args) { + auto now = std::chrono::system_clock::now(); + // TODO: use std::chrono::tai_clock::now() when using C++ 20 + auto now_duration = now.time_since_epoch(); + double ts = std::chrono::duration(now_duration).count(); + s7_double res = ts; + return s7_make_real(sc, res); +} + +static void glue_scheme_time(s7_scheme *sc) { + s7_pointer cur_env = s7_curlet(sc); + + const char *s_current_second = "g_current-second"; + const char *d_current_second = "(g_current-second) => double, return the " + "current unix timestamp in double"; + s7_define(sc, cur_env, s7_make_symbol(sc, s_current_second), + s7_make_typed_function(sc, s_current_second, f_current_second, 0, 0, + false, d_current_second, NULL)); +} diff --git a/tests/demo_error.scm b/tests/demo_error.scm new file mode 100644 index 0000000..ce7a9fe --- /dev/null +++ b/tests/demo_error.scm @@ -0,0 +1 @@ +(car (list )) diff --git a/tests/demo_no_error.scm b/tests/demo_no_error.scm new file mode 100644 index 0000000..4d956a6 --- /dev/null +++ b/tests/demo_no_error.scm @@ -0,0 +1 @@ +(+ 1 2) diff --git a/tests/scheme/case-lambda-test.scm b/tests/scheme/case-lambda-test.scm index 543a4ef..060e103 100644 --- a/tests/scheme/case-lambda-test.scm +++ b/tests/scheme/case-lambda-test.scm @@ -1,5 +1,5 @@ ; -; Copyright (C) 2024 The S7 SRFI Authors +; Copyright (C) 2024 The Goldfish Scheme Authors ; ; Licensed under the Apache License, Version 2.0 (the "License"); ; you may not use this file except in compliance with the License. diff --git a/tests/srfi/srfi-16-test.scm b/tests/srfi/srfi-16-test.scm index 684b1e9..cd702d6 100644 --- a/tests/srfi/srfi-16-test.scm +++ b/tests/srfi/srfi-16-test.scm @@ -1,5 +1,5 @@ ; -; Copyright (C) 2024 The S7 SRFI Authors +; Copyright (C) 2024 The Goldfish Scheme Authors ; ; Licensed under the Apache License, Version 2.0 (the "License"); ; you may not use this file except in compliance with the License. diff --git a/tests/srfi/srfi-39-test.scm b/tests/srfi/srfi-39-test.scm index fec27a7..e00fb63 100644 --- a/tests/srfi/srfi-39-test.scm +++ b/tests/srfi/srfi-39-test.scm @@ -1,5 +1,5 @@ ; -; Copyright (C) 2024 The S7 SRFI Authors +; Copyright (C) 2024 The Goldfish Scheme Authors ; ; Licensed under the Apache License, Version 2.0 (the "License"); ; you may not use this file except in compliance with the License. diff --git a/tests/srfi/srfi-78-test.scm b/tests/srfi/srfi-78-test.scm index 2a3aa02..3779db0 100644 --- a/tests/srfi/srfi-78-test.scm +++ b/tests/srfi/srfi-78-test.scm @@ -1,5 +1,5 @@ ; -; Copyright (C) 2024 The S7 SRFI Authors +; Copyright (C) 2024 The Goldfish Scheme Authors ; ; Licensed under the Apache License, Version 2.0 (the "License"); ; you may not use this file except in compliance with the License. @@ -17,7 +17,7 @@ (import (srfi srfi-78)) ; -; Copyright (C) 2024 The S7 SRFI Authors +; Copyright (C) 2024 The Goldfish Scheme Authors ; ; Licensed under the Apache License, Version 2.0 (the "License"); ; you may not use this file except in compliance with the License. diff --git a/tests/srfi/srfi-8-test.scm b/tests/srfi/srfi-8-test.scm index 5a62fca..7a96291 100644 --- a/tests/srfi/srfi-8-test.scm +++ b/tests/srfi/srfi-8-test.scm @@ -1,5 +1,5 @@ ; -; Copyright (C) 2024 The S7 SRFI Authors +; Copyright (C) 2024 The Goldfish Scheme Authors ; ; Licensed under the Apache License, Version 2.0 (the "License"); ; you may not use this file except in compliance with the License.