Skip to content

Commit

Permalink
[test/shell-vs-shell] New comparison of languages
Browse files Browse the repository at this point in the history
So we can test the language designs!
  • Loading branch information
Andy C committed Jun 14, 2022
1 parent cc29b83 commit 732eb3a
Show file tree
Hide file tree
Showing 3 changed files with 298 additions and 0 deletions.
65 changes: 65 additions & 0 deletions doctools/oil_doc.py 100644 → 100755
Expand Up @@ -311,6 +311,52 @@ def PrintHighlighted(self, out):
out.Print(highlighted)


def SimpleHighlightCode(s):
"""
Simple highlighting for test/shell-vs-shell.sh
"""

f = cStringIO.StringIO()
out = html.Output(s, f)

tag_lexer = html.TagLexer(s)

pos = 0

it = html.ValidTokens(s)

while True:
try:
tok_id, end_pos = next(it)
except StopIteration:
break

if tok_id == html.StartTag:

tag_lexer.Reset(pos, end_pos)
if tag_lexer.TagName() == 'pre':
pre_start_pos = pos
pre_end_pos = end_pos

slash_pre_right, slash_pre_right = \
html.ReadUntilEndTag(it, tag_lexer, 'pre')

out.PrintUntil(pre_end_pos)

# Using ShPromptPlugin because it does the comment highlighting we want!
plugin = ShPromptPlugin(s, pre_start_pos, slash_pre_right)
plugin.PrintHighlighted(out)

out.SkipTo(slash_pre_right)

pos = end_pos

out.PrintTheRest()

return f.getvalue()



def HighlightCode(s, default_highlighter):
"""
Algorithm:
Expand Down Expand Up @@ -552,3 +598,22 @@ def PrintHighlighted(self, s, start_pos, end_pos, out):
s: an HTML string.
"""
pass



def main(argv):
action = argv[1]

if action == 'highlight':
# for test/shell-vs-shell.sh

html = sys.stdin.read()
out = SimpleHighlightCode(html)
print(out)

else:
raise RuntimeError('Invalid action %r' % action)


if __name__ == '__main__':
main(sys.argv)
222 changes: 222 additions & 0 deletions test/shell-vs-shell.sh
@@ -0,0 +1,222 @@
#!/usr/bin/env bash
#
# Compare alternative shell designs!
#
# Usage:
# oil_lang/shell-vs-shell.sh <function name>

set -o nounset
set -o pipefail
set -o errexit

readonly BASE_DIR=_tmp/shell-vs-shell
readonly REPO_ROOT=$(cd $(dirname $0)/.. ; pwd)

readonly TAB=$'\t'

html-head() {
PYTHONPATH=. doctools/html_head.py "$@"
}

cmark() {
# copied from build/doc.sh
PYTHONPATH=. doctools/cmark.py --toc-tag h2 --toc-tag h3 --toc-pretty-href "$@"
}

highlight-code() {
PYTHONPATH=. doctools/oil_doc.py highlight "$@"
}

desc() {
echo "$@" > description.txt
}

src() {
local lang=$1

local prog=src/$lang

# read from stdin
cat > $prog

case $lang in
(oil)
$REPO_ROOT/bin/oil $prog | tee output/$lang.txt
;;
(shpp)
~/git/languages/shell-plus-plus/build/shell/shpp $prog | tee output/$lang.txt
;;
(*)
die "Invalid language $lang"
;;
esac
}

CASE-hello() {

desc "print the string 'hello world'"

# TODO:
# - need to group these into a row somehow ...
# code-begin or something
# - setup script
# - show output in the HTML too
# - save that in a dir and then escape it

src oil <<EOF
# oil requires quotes
echo 'hello world'
echo "hello world"
EOF

# single quotes not supported?
src shpp <<EOF
# no single quotes
echo hello world
echo "hello world"
EOF
}

CASE-pipeline() {
desc 'Pipeline'

src oil <<EOF
seq 5 | sort -r | head -n 3
EOF

src shpp <<EOF
seq 5 | sort -r | head -n 3
EOF

}

test-one() {
local func_name=$1

echo "$TAB---"
echo "$TAB$func_name"
echo "$TAB---"

local dir=$BASE_DIR/$func_name

mkdir -p $dir/{src,output}
pushd $dir >/dev/null

$func_name

popd >/dev/null

}

test-all() {
rm -r -f -v $BASE_DIR/
mkdir -p $BASE_DIR/
compgen -A function | grep '^CASE-' | xargs -n 1 -- $0 test-one

tree $BASE_DIR
}

html-escape() {
sed 's/&/&amp;/g; s/</&lt;/g; s/>/&gt;/g' "$@"
}

html-footer() {
echo '
</body>
</html>
'
}

# TODO: Run through doctools plugin for syntax highlighting

make-table() {
echo '<h2>'
html-escape description.txt
echo '</h2>'

echo '<table>'
echo ' <thead>'
echo ' <tr>'
echo ' <td></td>'
for src_file in src/*; do
echo "<td>$(basename $src_file)</td>"
done
echo ' </tr>'
echo ' </thead>'

echo ' <tr>'
echo ' <td>source</td>'
for src_file in src/*; do
echo '<td><pre>'
cat $src_file | html-escape
echo '</pre></td>'
done
echo ' </tr>'

echo ' <tr>'
echo ' <td>output</td>'
for out_file in output/*; do
echo '<td><pre>'
cat $out_file | html-escape
echo '</pre></td>'
done
echo ' </tr>'

echo '</table>'
}


_html-all() {
html-head --title 'Shell vs. Shell' \
../web/base.css ../web/shell-vs-shell.css ../web/language.css

echo '<body class="width50">'

cmark <<EOF
# Shell vs. Shell
This is a friendly comparison of the syntax of different shells!
- Oil: <https://github.com/oilshell/oil>
- [A Tour of the Oil
Language](https://www.oilshell.org/release/latest/doc/oil-language-tour.html)
- Shell++: <https://github.com/alexst07/shell-plus-plus>
- [Shell++ Language Basics](https://alexst07.github.io/shell-plus-plus/lang-basics/)
&nbsp;
- More shells: <https://github.com/oilshell/oil/wiki/Alternative-Shells>
- Script that generates this file:
<https://github.com/oilshell/oil/blob/master/test/shell-vs-shell.sh>
&nbsp;
EOF

for dir in $BASE_DIR/CASE-*; do
pushd $dir >/dev/null

make-table

popd >/dev/null
done

html-footer
}

html-all() {
mkdir -p $BASE_DIR

local out=$BASE_DIR/index.html

_html-all | highlight-code > $out

echo "Wrote $out"
}

all() {
test-all
html-all
}

"$@"
11 changes: 11 additions & 0 deletions web/shell-vs-shell.css
@@ -0,0 +1,11 @@
body {
font-size: large;
}

table {
width: 100%
}

thead {
font-weight: bold;
}

0 comments on commit 732eb3a

Please sign in to comment.