Skip to content

Commit

Permalink
Goldfish 17.10.0 (#3)
Browse files Browse the repository at this point in the history
  • Loading branch information
da-liii authored Aug 2, 2024
1 parent 441af34 commit 649e33b
Show file tree
Hide file tree
Showing 19 changed files with 162 additions and 73 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,5 @@
build/
bin/goldfish
goldfish/**/*-test.scm
compile_commands.json
.cache/
2 changes: 1 addition & 1 deletion AUTHORS
Original file line number Diff line number Diff line change
@@ -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 <email address>
Expand Down
2 changes: 2 additions & 0 deletions bin/format
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
#!/bin/bash
clang-format-16 -i src/*
2 changes: 1 addition & 1 deletion goldfish/scheme/base.scm
Original file line number Diff line number Diff line change
@@ -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.
Expand Down
2 changes: 1 addition & 1 deletion goldfish/scheme/file.scm
Original file line number Diff line number Diff line change
@@ -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.
Expand Down
2 changes: 1 addition & 1 deletion goldfish/srfi/srfi-1.scm
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
;;; hold me liable for its use. Please send bug reports to [email protected].
;;; -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)
Expand Down
2 changes: 1 addition & 1 deletion goldfish/srfi/srfi-13.scm
Original file line number Diff line number Diff line change
@@ -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.
Expand Down
2 changes: 1 addition & 1 deletion goldfish/srfi/srfi-16.scm
Original file line number Diff line number Diff line change
@@ -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.
Expand Down
2 changes: 1 addition & 1 deletion goldfish/srfi/srfi-78.scm
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
;
; Naming convention "check:<identifier>" 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)
Expand Down
2 changes: 1 addition & 1 deletion goldfish/srfi/srfi-8.scm
Original file line number Diff line number Diff line change
@@ -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.
Expand Down
151 changes: 92 additions & 59 deletions src/goldfish.cpp
Original file line number Diff line number Diff line change
@@ -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 <cstdio>
#include <cstdlib>
#include <cstring>
#include "goldfish.hpp"
#include "s7.h"

#include <chrono>
#include <filesystem>
#include <iostream>
#include <sstream>
#include <vector>

#ifndef _MSC_VER
#include <errno.h>
#include <unistd.h>
#endif

#include <s7.h>

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<double> (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<string> 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;
}
50 changes: 50 additions & 0 deletions src/goldfish.hpp
Original file line number Diff line number Diff line change
@@ -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 <chrono>
#include <s7.h>
#include <string>

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<double>(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));
}
1 change: 1 addition & 0 deletions tests/demo_error.scm
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
(car (list ))
1 change: 1 addition & 0 deletions tests/demo_no_error.scm
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
(+ 1 2)
2 changes: 1 addition & 1 deletion tests/scheme/case-lambda-test.scm
Original file line number Diff line number Diff line change
@@ -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.
Expand Down
2 changes: 1 addition & 1 deletion tests/srfi/srfi-16-test.scm
Original file line number Diff line number Diff line change
@@ -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.
Expand Down
2 changes: 1 addition & 1 deletion tests/srfi/srfi-39-test.scm
Original file line number Diff line number Diff line change
@@ -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.
Expand Down
4 changes: 2 additions & 2 deletions tests/srfi/srfi-78-test.scm
Original file line number Diff line number Diff line change
@@ -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.
Expand All @@ -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.
Expand Down
2 changes: 1 addition & 1 deletion tests/srfi/srfi-8-test.scm
Original file line number Diff line number Diff line change
@@ -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.
Expand Down

0 comments on commit 649e33b

Please sign in to comment.