Skip to content

Commit

Permalink
[61_7] S7: flush scheme when starting with scheme headers
Browse files Browse the repository at this point in the history
  • Loading branch information
da-liii committed Jun 5, 2024
1 parent 8e50a6c commit c408886
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 11 deletions.
6 changes: 3 additions & 3 deletions TeXmacs/plugins/s7/doc/s7.en.tm
Original file line number Diff line number Diff line change
Expand Up @@ -264,15 +264,15 @@
<|unfolded-io>
(random 1.0)
<|unfolded-io>
0.5606284330786007
0.848072511242711
</unfolded-io>

<\unfolded-io>
\<gtr\>\
<|unfolded-io>
(random 3/4)
<|unfolded-io>
120413/340517
477221/1610063
</unfolded-io>

<\input>
Expand Down Expand Up @@ -344,7 +344,7 @@
<|unfolded-io>
(define test-tb (hash-table 'a 1 'b 2))
<|unfolded-io>
(hash-table 'b 2 'a 1)
(hash-table 'a 1 'b 2)
</unfolded-io>

<\unfolded-io>
Expand Down
68 changes: 60 additions & 8 deletions TeXmacs/plugins/s7/src/tm_s7.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

#include <iostream>
#include <sstream>
#include <unordered_set>
#include <vector>

using std::cout;
Expand All @@ -12,9 +13,9 @@ using std::string;

#include "s7.h"

#define DATA_BEGIN ((char) 2)
#define DATA_END ((char) 5)
#define DATA_ESCAPE ((char) 27)
#define DATA_BEGIN ((char) 2)
#define DATA_END ((char) 5)
#define DATA_ESCAPE ((char) 27)

void
data_begin () {
Expand All @@ -26,6 +27,13 @@ data_end () {
cout << DATA_END << flush;
}

void
flush_scheme (string msg) {
data_begin ();
cout << "scheme:" << msg;
data_end ();
}

void
flush_verbatim (string msg) {
data_begin ();
Expand All @@ -40,11 +48,42 @@ flush_prompt (string prompt) {
data_end ();
}

string
getBeforeSpace (const string& str) {
size_t pos= str.find (' ');
if (pos == string::npos) {
return str;
}
return str.substr (0, pos);
}

std::string::size_type
findBegin (const std::string& s) {
std::string::size_type pos= s.find_first_not_of (" \t\n\r\f\v");
return (pos == std::string::npos) ? s.length () : pos;
}

std::string::size_type
findEnd (const std::string& s) {
std::string::size_type pos= s.find_last_not_of (" \t\n\r\f\v");
return (pos == std::string::npos) ? 0 : pos + 1;
}

std::string
trim (const std::string& s) {
std::string::size_type left = findBegin (s);
std::string::size_type right= findEnd (s);
return s.substr (left, right - left);
}

int
main (int argc, char** argv) {
std::unordered_set<std::string> scheme_headers= {
"(document", "(math", "(equation*", "(align", "(with", "(graphics"};

std::stringstream welcome;
welcome << "S7 Scheme " << S7_VERSION << " (" << S7_DATE << ")\n";
flush_verbatim (welcome.str());
flush_verbatim (welcome.str ());
flush_prompt ("> ");

s7_scheme* sc;
Expand All @@ -59,15 +98,28 @@ main (int argc, char** argv) {
std::vector<string> lines;
lines.push_back (first_line);
std::stringstream ss;
string line= first_line;
string line= first_line;
while (line != "<EOF>") {
ss << line << "\n";
line= "";
std::getline (std::cin, line);
}
s7_pointer x= s7_eval_c_string (sc, ss.str().c_str ());

flush_verbatim (s7_object_to_c_string (sc, x));
s7_pointer x = s7_eval_c_string (sc, ss.str ().c_str ());
string result= s7_object_to_c_string (sc, x);
if (result.size () == 0) {
flush_verbatim ("");
}
else {
string trimmed= trim (result);
string head = getBeforeSpace (trimmed);
if (trimmed[trimmed.size () - 1] == ')' &&
scheme_headers.find (head) != scheme_headers.end ()) {
flush_scheme (trimmed);
}
else {
flush_verbatim (result);
}
}
}

return 0;
Expand Down
1 change: 1 addition & 0 deletions bin/format
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,5 @@ clang-format -i tests/**/*.cpp
clang-format -i tests/**/*.hpp
clang-format -i src/**/*.cpp
clang-format -i src/**/*.hpp
clang-format -i TeXmacs/plugins/**/*.cpp

0 comments on commit c408886

Please sign in to comment.