⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 cweave.w

📁 模拟器提供了一个简单易用的平台
💻 W
📖 第 1 页 / 共 5 页
字号:
% This file is part of CWEB.% This program by Silvio Levy and Donald E. Knuth% is based on a program by Knuth.% It is distributed WITHOUT ANY WARRANTY, express or implied.% Version 3.61 --- July 2000% (essentially the same as version 3.6, which added%  recently introduced features of standard C++ to version 3.4)% Copyright (C) 1987,1990,1993,2000 Silvio Levy and Donald E. Knuth% Permission is granted to make and distribute verbatim copies of this% document provided that the copyright notice and this permission notice% are preserved on all copies.% Permission is granted to copy and distribute modified versions of this% document under the conditions for verbatim copying, provided that the% entire resulting derived work is given a different name and distributed% under the terms of a permission notice identical to this one.% Here is TeX material that gets inserted after \input cwebmac\def\hang{\hangindent 3em\indent\ignorespaces}\def\pb{$\.|\ldots\.|$} % C brackets (|...|)\def\v{\char'174} % vertical (|) in typewriter font\def\dleft{[\![} \def\dright{]\!]} % double brackets\mathchardef\RA="3221 % right arrow\mathchardef\BA="3224 % double arrow\def\({} % ) kludge for alphabetizing certain section names\def\TeXxstring{\\{\TEX/\_string}}\def\skipxTeX{\\{skip\_\TEX/}}\def\copyxTeX{\\{copy\_\TEX/}}\def\title{CWEAVE (Version 3.61)}\def\topofcontents{\null\vfill  \centerline{\titlefont The {\ttitlefont CWEAVE} processor}  \vskip 15pt  \centerline{(Version 3.61)}  \vfill}\def\botofcontents{\vfill\noindentCopyright \copyright\ 1987, 1990, 1993, 2000 Silvio Levy and Donald E. Knuth\bigskip\noindentPermission is granted to make and distribute verbatim copies of thisdocument provided that the copyright notice and this permission noticeare preserved on all copies.\smallskip\noindentPermission is granted to copy and distribute modified versions of thisdocument under the conditions for verbatim copying, provided that theentire resulting derived work is given a different name and distributedunder the terms of a permission notice identical to this one.}\pageno=\contentspagenumber \advance\pageno by 1\let\maybe=\iftrue@s not_eq normal @q unreserve a C++ keyword @>@** Introduction.This is the \.{CWEAVE} program by Silvio Levy and Donald E. Knuth,based on \.{WEAVE} by Knuth.We are thankful to Steve Avery,Nelson Beebe, Hans-Hermann Bode (to whom the original \CPLUSPLUS/ adaptationis due), Klaus Guntermann, Norman Ramsey, Tomas Rokicki, Joachim Schnitter,Joachim Schrod, Lee Wittenberg, Saroj Mahapatra, Cesar Augusto RoratoCrusius, and others who have contributed improvements.The ``banner line'' defined here should be changed whenever \.{CWEAVE}is modified.@d banner "This is CWEAVE (Version 3.61)\n"@c @<Include files@>@/@h@<Common code for \.{CWEAVE} and \.{CTANGLE}@>@/@<Typedef declarations@>@/@<Global variables@>@/@<Predeclaration of procedures@>@ We predeclare several standard system functions here instead of includingtheir system header files, because the names of the header files are not asstandard as the names of the functions. (For example, some \CEE/ environmentshave \.{<string.h>} where others have \.{<strings.h>}.)@<Predecl...@>=extern int strlen(); /* length of string */extern int strcmp(); /* compare strings lexicographically */extern char* strcpy(); /* copy one string to another */extern int strncmp(); /* compare up to $n$ string characters */extern char* strncpy(); /* copy up to $n$ string characters */@ \.{CWEAVE} has a fairly straightforward outline.  It operates inthree phases: First it inputs the source file and stores cross-referencedata, then it inputs the source once again and produces the \TEX/ outputfile, finally it sorts and outputs the index.Please read the documentation for \.{common}, the set of routines commonto \.{CTANGLE} and \.{CWEAVE}, before proceeding further.@cint main (ac, av)int ac; /* argument count */char **av; /* argument values */{  argc=ac; argv=av;  program=cweave;  make_xrefs=force_lines=1; /* controlled by command-line options */  common_init();  @<Set initial values@>;  if (show_banner) printf(banner); /* print a ``banner line'' */  @<Store all the reserved words@>;  phase_one(); /* read all the user's text and store the cross-references */  phase_two(); /* read all the text again and translate it to \TEX/ form */  phase_three(); /* output the cross-reference index */  return wrap_up(); /* and exit gracefully */}@ The following parameters were sufficient in the original \.{WEAVE} tohandle \TEX/, so they should be sufficient for most applications of \.{CWEAVE}.If you change |max_bytes|, |max_names|, |hash_size|, or |buf_size|you have to change them also in the file |"common.w"|.@d max_bytes 90000 /* the number of bytes in identifiers,  index entries, and section names */@d max_names 4000 /* number of identifiers, strings, section names;  must be less than 10240; used in |"common.w"| */@d max_sections 2000 /* greater than the total number of sections */@d hash_size 353 /* should be prime */@d buf_size 100 /* maximum length of input line, plus one */@d longest_name 10000 /* section names and strings shouldn't be longer than this */@d long_buf_size (buf_size+longest_name)@d line_length 80 /* lines of \TEX/ output have at most this many characters;  should be less than 256 */@d max_refs 20000 /* number of cross-references; must be less than 65536 */@d max_toks 20000 /* number of symbols in \CEE/ texts being parsed;  must be less than 65536 */@d max_texts 4000 /* number of phrases in \CEE/ texts being parsed;  must be less than 10240 */@d max_scraps 2000 /* number of tokens in \CEE/ texts being parsed */@d stack_size 400 /* number of simultaneous output levels */@ The next few sections contain stuff from the file |"common.w"| that mustbe included in both |"ctangle.w"| and |"cweave.w"|. It appears infile |"common.h"|, which needs to be updated when |"common.w"| changes.@i common.h@* Data structures exclusive to {\tt CWEAVE}.As explained in \.{common.w}, the field of a |name_info| structurethat contains the |rlink| of a section name is used for a completelydifferent purpose in the case of identifiers.  It is then called the|ilk| of the identifier, and it is used todistinguish between various types of identifiers, as follows:\yskip\hang |normal| and |func_template| identifiers are part of the\CEE/ program that will  appear in italic type (or in typewriter typeif all uppercase).\yskip\hang |custom| identifiers are part of the \CEE/ program thatwill be typeset in special ways.\yskip\hang |roman| identifiers are index entries that appear after\.{@@\^} in the \.{CWEB} file.\yskip\hang |wildcard| identifiers are index entries that appear after\.{@@:} in the \.{CWEB} file.\yskip\hang |typewriter| identifiers are index entries that appear after\.{@@.} in the \.{CWEB} file.\yskip\hang |alfop|, \dots, |template_like|identifiers are \CEE/ or \CPLUSPLUS/ reserved words whose |ilk|explains how they are to be treated when \CEE/ code is beingformatted.@d ilk dummy.Ilk@d normal 0 /* ordinary identifiers have |normal| ilk */@d roman 1 /* normal index entries have |roman| ilk */@d wildcard 2 /* user-formatted index entries have |wildcard| ilk */@d typewriter 3 /* `typewriter type' entries have |typewriter| ilk */@d abnormal(a) (a->ilk>typewriter) /* tells if a name is special */@d func_template 4 /* identifiers that can be followed by optional template */@d custom 5 /* identifiers with user-given control sequence */@d alfop 22 /* alphabetic operators like \&{and} or \&{not\_eq} */@d else_like 26 /* \&{else} */@d public_like 40 /* \&{public}, \&{private}, \&{protected} */@d operator_like 41 /* \&{operator} */@d new_like 42 /* \&{new} */@d catch_like 43 /* \&{catch} */@d for_like 45 /* \&{for}, \&{switch}, \&{while} */@d do_like 46 /* \&{do} */@d if_like 47 /* \&{if}, \&{ifdef}, \&{endif}, \&{pragma}, \dots */@d delete_like 48 /* \&{delete} */@d raw_ubin 49 /* `\.\&' or `\.*' when looking for \&{const} following */@d const_like 50 /* \&{const}, \&{volatile} */@d raw_int 51 /* \&{int}, \&{char}, \dots; also structure and class names  */@d int_like 52 /* same, when not followed by left parenthesis or \DC\ */@d case_like 53 /* \&{case}, \&{return}, \&{goto}, \&{break}, \&{continue} */@d sizeof_like 54 /* \&{sizeof} */@d struct_like 55 /* \&{struct}, \&{union}, \&{enum}, \&{class} */@d typedef_like 56 /* \&{typedef} */@d define_like 57 /* \&{define} */@d template_like 58 /* \&{template} */@ We keep track of the current section number in |section_count|, whichis the total number of sections that have started.  Sections which havebeen altered by a change file entry have their |changed_section| flagturned on during the first phase.@<Global...@>=boolean change_exists; /* has any section changed? */@ The other large memory area in \.{CWEAVE} keeps the cross-reference data.All uses of the name |p| are recorded in a linked list beginning at|p->xref|, which points into the |xmem| array. The elements of |xmem|are structures consisting of an integer, |num|, and a pointer |xlink|to another element of |xmem|.  If |x=p->xref| is a pointer into |xmem|,the value of |x->num| is either a section number where |p| is used,or |cite_flag| plus a section number where |p| is mentioned,or |def_flag| plus a section number where |p| is defined;and |x->xlink| points to the next such cross-reference for |p|,if any. This list of cross-references is in decreasing order bysection number. The next unused slot in |xmem| is |xref_ptr|.The linked list ends at |&xmem[0]|.The global variable |xref_switch| is set either to |def_flag| or to zero,depending on whether the next cross-reference to an identifier is to beunderlined or not in the index. This switch is set to |def_flag| when\.{@@!} or \.{@@d} is scanned, and it is cleared to zero whenthe next identifier or index entry cross-reference has been made.Similarly, the global variable |section_xref_switch| is either|def_flag| or |cite_flag| or zero, dependingon whether a section name is being defined, cited or used in \CEE/ text.@<Type...@>=typedef struct xref_info {  sixteen_bits num; /* section number plus zero or |def_flag| */  struct xref_info *xlink; /* pointer to the previous cross-reference */} xref_info;typedef xref_info *xref_pointer;@ @<Global...@>=xref_info xmem[max_refs]; /* contains cross-reference information */xref_pointer xmem_end = xmem+max_refs-1;xref_pointer xref_ptr; /* the largest occupied position in |xmem| */sixteen_bits xref_switch,section_xref_switch; /* either zero or |def_flag| */@ A section that is used for multi-file output (with the \.{@@(} feature)has a special first cross-reference whose |num| field is |file_flag|.@d file_flag (3*cite_flag)@d def_flag (2*cite_flag)@d cite_flag 10240 /* must be strictly larger than |max_sections| */@d xref equiv_or_xref@<Set init...@>=xref_ptr=xmem; name_dir->xref=(char*)xmem; xref_switch=0; section_xref_switch=0;xmem->num=0; /* sentinel value */@ A new cross-reference for an identifier is formed by calling |new_xref|,which discards duplicate entries and ignores non-underlined referencesto one-letter identifiers or \CEE/'s reserved words.If the user has sent the |no_xref| flag (the \.{-x} option of the command line),it is unnecessary to keep track of cross-references for identifiers.If one were careful, one could probably make more changes around section100 to avoid a lot of identifier looking up.@d append_xref(c) if (xref_ptr==xmem_end) overflow("cross-reference");  else (++xref_ptr)->num=c;@d no_xref (flags['x']==0)@d make_xrefs flags['x'] /* should cross references be output? */@d is_tiny(p) ((p+1)->byte_start==(p)->byte_start+1)@d unindexed(a) (a<res_wd_end && a->ilk>=custom)      /* tells if uses of a name are to be indexed */@cvoidnew_xref(p)name_pointer p;{  xref_pointer q; /* pointer to previous cross-reference */  sixteen_bits m, n; /* new and previous cross-reference value */  if (no_xref) return;  if ((unindexed(p) || is_tiny(p)) && xref_switch==0) return;  m=section_count+xref_switch; xref_switch=0; q=(xref_pointer)p->xref;  if (q != xmem) {    n=q->num;    if (n==m || n==m+def_flag) return;    else if (m==n+def_flag) {        q->num=m; return;    }  }  append_xref(m); xref_ptr->xlink=q; p->xref=(char*)xref_ptr;}@ The cross-reference lists for section names are slightly different.Suppose that a section name is defined in sections $m_1$, \dots,$m_k$, cited in sections $n_1$, \dots, $n_l$, and used in sections$p_1$, \dots, $p_j$.  Then its list will contain $m_1+|def_flag|$,\dots, $m_k+|def_flag|$, $n_1+|cite_flag|$, \dots,$n_l+|cite_flag|$, $p_1$, \dots, $p_j$, in this order.Although this method of storage takes quadratic time with respect tothe length of the list, under foreseeable uses of \.{CWEAVE} this inefficiencyis insignificant.@cvoidnew_section_xref(p)name_pointer p;{  xref_pointer q,r; /* pointers to previous cross-references */  q=(xref_pointer)p->xref; r=xmem;  if (q>xmem)        while (q->num>section_xref_switch) {r=q; q=q->xlink;}  if (r->num==section_count+section_xref_switch)        return; /* don't duplicate entries */  append_xref(section_count+section_xref_switch);  xref_ptr->xlink=q; section_xref_switch=0;  if (r==xmem) p->xref=(char*)xref_ptr;  else r->xlink=xref_ptr;}@ The cross-reference list for a section name may also begin with|file_flag|. Here's how that flag gets put~in.@cvoidset_file_flag(p)name_pointer p;{  xref_pointer q;  q=(xref_pointer)p->xref;  if (q->num==file_flag) return;  append_xref(file_flag);  xref_ptr->xlink = q;  p->xref = (char *)xref_ptr;}@ A third large area of memory is used for sixteen-bit `tokens', which appearin short lists similar to the strings of characters in |byte_mem|. Token listsare used to contain the result of \CEE/ code translated into \TEX/ form;further details about them will be explained later. A |text_pointer| variableis an index into |tok_start|.@<Typed...@>=typedef sixteen_bits token;typedef token *token_pointer;typedef token_pointer *text_pointer;@ The first position of |tok_mem|that is unoccupied by replacement text is called |tok_ptr|, and the firstunused location of |tok_start| is called |text_ptr|.Thus, we usually have |*text_ptr==tok_ptr|.@<Global...@>=token tok_mem[max_toks]; /* tokens */token_pointer tok_mem_end = tok_mem+max_toks-1; /* end of |tok_mem| */

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -