Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use the same clang-format config with Mogan #8

Merged
merged 1 commit into from
Aug 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions .clang-format
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
SpaceBeforeAssignmentOperators: false
SpaceBeforeParens: Always
AlwaysBreakAfterReturnType: TopLevelDefinitions
AllowShortIfStatementsOnASingleLine: AllIfsAndElse
AlignConsecutiveAssignments: Consecutive
AlignConsecutiveDeclarations: Consecutive
SpaceAfterCStyleCast: true
AllowShortFunctionsOnASingleLine: All
BreakBeforeBraces: Custom
BraceWrapping:
BeforeElse: true
PointerAlignment: Left
90 changes: 51 additions & 39 deletions src/goldfish.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@ using std::vector;
using std::filesystem::exists;
using std::filesystem::path;

void display_help() {
void
display_help () {
cout << "Goldfish Scheme " << goldfish_version << " by LiiiLabs" << endl;
cout << "--version\t"
<< "display version" << endl;
Expand All @@ -42,75 +43,86 @@ void display_help() {
<< "Load the scheme code on path and print the evaluated result" << endl;
}

void display_version() {
void
display_version () {
cout << "Goldfish Scheme " << goldfish_version << " by LiiiLabs" << endl;
cout << "based on S7 Scheme " << S7_VERSION << " (" << S7_DATE << ")" << endl;
}

void display_for_invalid_options() {
void
display_for_invalid_options () {
cerr << "Invalid command line options!" << endl << endl;
display_help();
display_help ();
}

void goldfish_eval_file(s7_scheme *sc, string path, bool quiet) {
s7_pointer result = s7_load(sc, path.c_str());
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);
exit (-1);
}
if (!quiet) {
cout << path << " => " << s7_object_to_c_string(sc, result) << endl;
cout << path << " => " << s7_object_to_c_string (sc, result) << endl;
}
}

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;
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;
}

int main(int argc, char **argv) {
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_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)) {
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);
exit (-1);
}
if (!exists(gf_boot)) {
if (!exists (gf_boot)) {
cerr << "The boot.scm for Goldfish Scheme does not exist" << endl;
exit(-1);
exit (-1);
}

// 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());
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
glue_goldfish(sc);
glue_scheme_time(sc);
glue_goldfish (sc);
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] == '-') {
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();
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();
}
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;
}
69 changes: 37 additions & 32 deletions src/goldfish.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,49 +18,54 @@
#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 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));
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 Goldfish
static s7_pointer f_version(s7_scheme *sc, s7_pointer args) {
return s7_make_string(sc, goldfish_version.c_str());
static s7_pointer
f_version (s7_scheme* sc, s7_pointer args) {
return s7_make_string (sc, goldfish_version.c_str ());
}

inline void glue_goldfish(s7_scheme *sc) {
s7_pointer cur_env = s7_curlet(sc);
inline void
glue_goldfish (s7_scheme* sc) {
s7_pointer cur_env= s7_curlet (sc);

const char *s_version = "version";
const char *d_version = "(version) => string, return the "
"goldfish version";
s7_define(sc, cur_env, s7_make_symbol(sc, s_version),
s7_make_typed_function(sc, s_version, f_version, 0, 0, false,
d_version, NULL));
const char* s_version= "version";
const char* d_version= "(version) => string, return the "
"goldfish version";
s7_define (sc, cur_env, s7_make_symbol (sc, s_version),
s7_make_typed_function (sc, s_version, f_version, 0, 0, false,
d_version, NULL));
}

// Glues for (scheme time)
static s7_pointer f_current_second(s7_scheme *sc, s7_pointer args) {
auto now = std::chrono::system_clock::now();
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);
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);
}

inline void glue_scheme_time(s7_scheme *sc) {
s7_pointer cur_env = s7_curlet(sc);
inline 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));
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));
}
Loading