Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

converting CWEB to noweb

5 views
Skip to first unread message

Allan Adler

unread,
Sep 25, 2006, 2:57:28 PM9/25/06
to

Since I'm more familiar with CWEB and since I already have gone to the
trouble to install it, something I still have to figure out how to do
with noweb, I'm more inclined to use CWEB until I run into something
that CWEB is absolutely unsuitable for. The process by which I discover
that CWEB is unsuitable for something is likely to be trying to use
CWEB and failing. That will leave me with a CWEB file that I will then
have a good reason to convert to noweb, which I will then be more
motivated to install.

The question is this: how painless is it to convert a CWEB file to noweb?
--
Ignorantly,
Allan Adler <a...@zurich.csail.mit.edu>
* Disclaimer: I am a guest and *not* a member of the MIT CSAIL. My actions and
* comments do not reflect in any way on MIT. Also, I am nowhere near Boston.

peter.k...@xs4all.nl

unread,
Nov 15, 2006, 11:32:26 PM11/15/06
to
Facing similar problems several years ago, I used a simple AWK
converter script, attached here. I vaguely remember successully
using it once, however, looking at it now, I don't quite recognise
it as one of my creations... Anyway, it needs some brushing up and
conversion to noweb itself...

Pjotr

Allan Adler <a...@nestle.csail.mit.edu> wrote:
>
> Since I'm more familiar with CWEB and since I already have gone to the
> trouble to install it, something I still have to figure out how to do
> with noweb, I'm more inclined to use CWEB until I run into something
> that CWEB is absolutely unsuitable for. The process by which I discover
> that CWEB is unsuitable for something is likely to be trying to use
> CWEB and failing. That will leave me with a CWEB file that I will then
> have a good reason to convert to noweb, which I will then be more
> motivated to install.
>
> The question is this: how painless is it to convert a CWEB file to noweb?

---------------------------------------------------------------------------
#!/usr/bin/gawk -f

func out_doc(str)
{ if (OP=="weave" || OP=="trans")
printf "%s",str
}

func out_code(str)
{ if (OP=="tangle" || OP=="trans")
printf "%s",str
}

func sec(start) {
for (i=start; i<=NF && In_Name; i++) {
if(name!="") name=name " "
name=name $i
if (match($i,"\\.")) {
gsub("\\.","",name)
out_doc(sprintf("@ " command "{%s}\n",name))
In_Name=0
}
}
if (i<=NF) {
for (; i<=NF; i++) {
out_doc(" ")
out_doc($i)
}
out_doc("\n")
}
}

func do_rest(start) {
res=""
for (i=start; i<=NF; i++) {
if (res!="") res=res " "
res=res $i
}
return res
}

BEGIN {
MODE="limbo"
if (OP=="") OP="trans"
First_Define=1
}
/^[:space:]*@\*\*/ {
MODE="doc"
name=""; In_Name=1; command="\\section"
sec(2)
next
}
/^[:space:]*@\*/ {
MODE="doc"
name=""; In_Name=1; command="\\subsection"
sec(2)
next
}
In_Name {
sec(1)
next
}
/^[:space:]*@[ \t]+/ {
MODE="doc"
}
/^[:space:]*@$/ {
MODE="doc"
}

(MODE=="doc") && /^[:space:]*@[sf]/ {
next
}

(MODE=="doc") && /^[:space:]*@d/ {
if (First_Define) {
out_doc("<<*>>=\n<<Definitions>>\n\n")
First_Define=0
}
out_doc("<<Definitions>>=\n")
MODE="code"
out_code("#define " do_rest(2))
out_code("\n")
MODE="doc"
next
}
(MODE=="doc") && /^[:space:]*@c/ {
out_doc("<<*>>=")
MODE="code"
out_code(do_rest(2))
out_code("\n")
next
}

(MODE=="doc" || MODE=="code") {
$0=gensub("\\|([^|]+)\\|","[[\\1]]","g",$0)
}

(MODE=="doc") && /^[:space:]*@</ {
match($0,/@<(.*)@>=(.*)/,a)
out_doc("<<" a[1] ">>=")
if (a[2]!="") out_doc("\n")
$0=a[2]
MODE="code"
}
(MODE=="doc") && /^[:space:]*@\(/ {
match($0,/@\((.*)@>=(.*)/,a)
out_doc("<<" a[1] ">>=")
if (a[2]!="") out_doc("\n")
$0=a[2]
MODE="code"
}
(MODE=="code") {
gsub(/@</,"<<")
gsub(/@>/,">>")
gsub(/@;/,"")
gsub(/@\//,"")
}
OP=="trans" { if (MODE=="doc" || MODE=="code")
print
}

OP=="tangle" { if (MODE=="code")
print
}

OP=="weave" { if (MODE=="doc")
print
}
{}

Allan Adler

unread,
Nov 17, 2006, 12:55:35 PM11/17/06
to
peter.k...@xs4all.nl writes:

> Facing similar problems several years ago, I used a simple AWK
> converter script, attached here.

Thanks!

> I vaguely remember successully
> using it once, however, looking at it now, I don't quite recognise
> it as one of my creations... Anyway, it needs some brushing up and
> conversion to noweb itself...

You mean that, if we call your script cweb2nw, then you want someone
to create a noweb file cweb2nw.nw, so that people will not only be able
to use the script but also know how and why it works?

Joe Riel

unread,
Nov 17, 2006, 1:29:09 PM11/17/06
to
Allan Adler <a...@nestle.csail.mit.edu> writes:

> peter.k...@xs4all.nl writes:
>
>> Facing similar problems several years ago, I used a simple AWK
>> converter script, attached here.
>
> Thanks!
>
>> I vaguely remember successully
>> using it once, however, looking at it now, I don't quite recognise
>> it as one of my creations... Anyway, it needs some brushing up and
>> conversion to noweb itself...
>
> You mean that, if we call your script cweb2nw, then you want someone
> to create a noweb file cweb2nw.nw, so that people will not only be able
> to use the script but also know how and why it works?

Maybe you should write the source for the cweb2nw in cweb,
and then run the script on itself 8-).

--
Joe Riel

0 new messages