C1R Implementation: Difference between revisions

From Rosetta Code
Content added Content deleted
(Initial version)
 
imported>Katsumi
No edit summary
 
(3 intermediate revisions by 2 users not shown)
Line 2: Line 2:
c1r is the implementation for the [[C1R]] language on Unix machines; porting to Windows and other platforms should be fairly easy.
c1r is the implementation for the [[C1R]] language on Unix machines; porting to Windows and other platforms should be fairly easy.


In fact c1r is a simple shell script that efficiently uses the already available [[C]] compiler named cc:
In fact c1r is a simple shell script that efficiently uses the already available [[C]] compiler named cc.
The "Quine" task required special treatment.
<lang c>#! /bin/bash

<syntaxhighlight lang="bash">#! /bin/bash


# C1R compiler
# C1R compiler
Line 15: Line 17:
# unescape HTML codes: replace "&lt;" by "<" etc
# unescape HTML codes: replace "&lt;" by "<" etc
function unescapeHTML() {
function unescapeHTML() {
sed -e 's/&lt\;/</g;s/&gt\;/>/g;s/&nbsp\;/ /g;s/&quot\;/"/g;s/&#40\;/(/g;s/&#41\;/)/g;s/&#91\;/[/g;s/&#93\;/]/g;s/&#123\;/{/g;s/&#125\;/}/g'
sed -e 's/&lt\;/</g;s/&gt\;/>/g;s/&nbsp\;/ /g;s/&#160\;/ /g;s/&#xA0\;/ /g;s/&quot\;/"/g;s/&#40\;/(/g;s/&#41\;/)/g;s/&#91\;/[/g;s/&#93\;/]/g;s/&#123\;/{/g;s/&#125\;/}/g'
}
}


Line 34: Line 36:
if [ $WORDCOUNT -eq 1 ]
if [ $WORDCOUNT -eq 1 ]
then
then
# Note: the self-printing Quine program requires special treatment
PAGEURL=$ROSETTAURL/`cat $FILENAME`
if [ `cat $FILENAME` = "Quine" ]
curl $PAGEURL 2>/dev/null | grep -m 1 "<pre class=\"c highlighted_source\">" | removeHTMLtags | unescapeHTML >${FILENAME1}
then
cat << EOF > $FILENAME1
#include <stdio.h>
int main(char args[]) {printf("Quine\\n");}
EOF
else
PAGEURL=$ROSETTAURL/`cat $FILENAME`
curl $PAGEURL 2>/dev/null | grep -m 1 "<pre class=\"c highlighted_source\">" | removeHTMLtags | unescapeHTML >${FILENAME1}
fi
fi
fi


cc $FILENAME1
cc $FILENAME1
</syntaxhighlight>
</lang>


A typical test session would look like:
A typical test session would look like:
<lang bash>
<syntaxhighlight lang="bash">
$ echo Hello_world/Text >hw.c1r
$ echo Hello_world/Text >hw.c1r
$ ./c1r hw.c1r
$ ./c1r hw.c1r
$ ./a.out
$ ./a.out
Goodbye, World!
Goodbye, World!
</syntaxhighlight>
</lang>

Latest revision as of 14:04, 25 August 2023

C1R Implementation is an implementation of C1R. Other implementations of C1R.

c1r is the implementation for the C1R language on Unix machines; porting to Windows and other platforms should be fairly easy.

In fact c1r is a simple shell script that efficiently uses the already available C compiler named cc. The "Quine" task required special treatment.

#! /bin/bash

# C1R compiler

# remove HTML tags, but replace "<br />" by newlines
function removeHTMLtags() {
 sed -e 's~<br */>~\
~g' | sed -e :a -e 's/<[^>]*>//g;/</N;//ba' 
}

# unescape HTML codes: replace "&lt;" by "<" etc
function unescapeHTML() {
 sed -e 's/&lt\;/</g;s/&gt\;/>/g;s/&nbsp\;/ /g;s/&#160\;/ /g;s/&#xA0\;/ /g;s/&quot\;/"/g;s/&#40\;/(/g;s/&#41\;/)/g;s/&#91\;/[/g;s/&#93\;/]/g;s/&#123\;/{/g;s/&#125\;/}/g'
}

FILENAME=$1

if [ -z ${FILENAME} ] 
then
  echo "Usage: $0 <fileName>"
  exit 1
fi

FILENAME1=${FILENAME}.c
ROSETTAURL=rosettacode.org/wiki
WORDCOUNT=`cat $FILENAME|wc -l`

cp ${FILENAME} ${FILENAME1}

if [ $WORDCOUNT -eq 1 ]
then
  # Note: the self-printing Quine program requires special treatment
  if [ `cat $FILENAME` = "Quine" ]
  then 
    cat << EOF > $FILENAME1
#include <stdio.h>
int main(char args[]) {printf("Quine\\n");}
EOF
  else 
    PAGEURL=$ROSETTAURL/`cat $FILENAME`
    curl $PAGEURL 2>/dev/null | grep -m 1 "<pre class=\"c highlighted_source\">" | removeHTMLtags | unescapeHTML >${FILENAME1}
  fi
fi

cc $FILENAME1

A typical test session would look like:

$ echo Hello_world/Text >hw.c1r
$ ./c1r hw.c1r
$ ./a.out
Goodbye, World!