#include <string.hfa>
#include <assert.h>

#define xstr(s) str(@s;@)
#define str(s) #s

void demo1() {
	sout | sepOff;
	sout | "Consider two strings @s1@ and @s1a@ that are in an aliasing relationship, and a third, @s2@, made by a simple copy from @s1@.";
	sout | "\\par\\noindent";
	sout | "\\begin{tabular}{llll}";
	sout | "\t\t\t\t& @s1@\t& @s1a@\t& @s2@\t\\\\";

	#define S1 string s1  = "abc"
	#define S1A string s1a = s1`shareEdits
	#define S2 string s2  = s1
	S1;
	S1A;
	S2;
	assert( s1 == "abc" );
	assert( s1a == "abc" );
	assert( s2 == "abc" );
	sout | xstr(S1) | "\t\\\\";
	sout | xstr(S1A) | "\t\\\\";
	sout | xstr(S2) | "\t& " | s1 | "\t& " | s1a | "\t& " | s2;
	sout | "\\end{tabular}";
	sout | "\\par\\noindent";

	sout | "Aliasing (@`shareEdits@) means that changes flow in both directions; with a simple copy, they do not.";
	sout | "\\par\\noindent";
	sout | "\\begin{tabular}{llll}";
	sout | "\t\t& @s1@\t& @s1a@\t& @s2@\t\\\\";
	sout | "\t\t& " | s1 | "\t& " | s1a | "\t& " | s2 | "\t\\\\";

	#define S1s1 s1 [1] = '+'
	S1s1;
	assert( s1 == "a+c" );
	sout | xstr(S1s1) | "\t& " | s1 | "\t& " | s1a | "\t& " | s2 | "\t\\\\";
      
	#define S1As1 s1a[1] = '-'
	S1As1;
	assert( s1a == "a-c" );
	sout | xstr(S1As1) | "\t& " | s1 | "\t& " | s1a | "\t& " | s2 | "\t\\\\";
      
	#define S2s1 s2 [1] = '|'
	S2s1;
	assert( s2 == "a|c" );
	sout | xstr(S2s1) | "\t& " | s1 | "\t& " | s1a | "\t& " | s2;
	sout | "\\end{tabular}";
	sout | "\\par\\noindent";
      
	sout | "Assignment of a value is just a modificiation."
		   "\nThe aliasing relationship is established at construction and is unaffected by assignment of a value.";
	sout | "\\par\\noindent";
	sout | "\\begin{tabular}{llll}";
	sout | "\t\t& @s1@\t& @s1a@\t& @s2@\t\\\\";
	sout | "\t\t& " | s1 | "\t& " | s1a | "\t& " | s2 | "\t\\\\";

	#define S1qrs s1  = "qrs"
	S1qrs;
	assert( s1 == "qrs" );
	sout | xstr(S1qrs) | "\t& " | s1 | "\t& " | s1a | "\t& " | s2 | "\t\\\\";
    
	#define S1Atuv s1a = "tuv"
	S1Atuv;
	assert( s1a == "tuv" );
	sout | xstr(S1Atuv) | "\t& " | s1 | "\t& " | s1a | "\t& " | s2 | "\t\\\\";
    
	#define S2wxy s2  = "wxy"
	S2wxy;
	assert( s2 == "wxy" );
	sout | xstr(S2wxy) | "\t& " | s1 | "\t& " | s1a | "\t& " | s2;
	sout | "\\end{tabular}";
	sout | "\\par\\noindent";

	sout | "Assignment from a string is just assignment of a value."
		   "\nWhether of not the RHS participates in aliasing is irrelevant.  Any aliasing of the LHS is unaffected.";
	sout | "\\par\\noindent";
	sout | "\\begin{tabular}{llll}";
	sout | "\t\t& @s1@\t& @s1a@\t& @s2@\t\\\\";
	sout | "\t\t& " | s1 | "\t& " | s1a | "\t& " | s2 | "\t\\\\";

	#define S1S2 s1  = s2
	S1S2;
	assert( s1 == "wxy" );
	assert( s1a == "wxy" );
	assert( s2 == "wxy" );
	sout | xstr(S1S2) | "\t& " | s1 | "\t& " | s1a | "\t& " | s2 | "\t\\\\";
    
	#define S1aaa s1  = "aaa"
	S1aaa;
	assert( s1 == "aaa" );
	assert( s1a == "aaa" );
	assert( s2 == "wxy" );
	sout | xstr(S1aaa) | "\t& " | s1 | "\t& " | s1a | "\t& " | s2 | "\t\\\\";
    
	#define S2S1 s2  = s1
	S2S1;
	assert( s1 == "aaa" );
	assert( s1a == "aaa" );
	assert( s2 == "aaa" );
	sout | xstr(S2S1) | "\t& " | s1 | "\t& " | s1a | "\t& " | s2 | "\t\\\\";
    
	#define S2bbb s2  = "bbb"
	S2bbb;
	assert( s1 == "aaa" );
	assert( s1a == "aaa" );
	assert( s2 == "bbb" );
	sout | xstr(S2bbb) | "\t& " | s1 | "\t& " | s1a | "\t& " | s2 | "\t\\\\";
      
    #define S2S1a s2  = s1a
	S2S1a;
	assert( s1 == "aaa" );
	assert( s1a == "aaa" );
	assert( s2 == "aaa" );
	sout | xstr(S2S1a) | "\t& " | s1 | "\t& " | s1a | "\t& " | s2 | "\t\\\\";

	#define S2ccc s2  = "ccc"
	S2ccc;
	assert( s1 == "aaa" );
	assert( s1a == "aaa" );
	assert( s2 == "ccc" );
	sout | xstr(S2ccc) | "\t& " | s1 | "\t& " | s1a | "\t& " | s2 | "\t\\\\";
      
	#define S1xxx s1  = "xxx"
	S1xxx;
	assert( s1 == "xxx" );
	assert( s1a == "xxx" );
	assert( s2 == "ccc" );
	sout | xstr(S1xxx) | "\t& " | s1 | "\t& " | s1a | "\t& " | s2 | "\t\\\\";
	sout | "\\end{tabular}";
	sout | "\\par";
}


void demo2() {
	sout | "Consider new strings @s1_mid@ being an alias for a run in the middle of @s1@, along with @s2@, made by a simple copy from the middle of @s1@.";
	sout | "\\par\\noindent";
	sout | "\\begin{tabular}{llll}";
	sout | "\t\t\t\t& @s1@\t& @s1_mid@\t& @s2@\t\\\\";

	#define D2_s1_abcd string s1     = "abcd"
	D2_s1_abcd;
	sout | xstr(D2_s1_abcd) | "\t\\\\";

	#define D2_s1mid_s1 string s1_mid = s1(1,3)`shareEdits
	D2_s1mid_s1;
	sout | xstr(D2_s1mid_s1) | "\t\\\\";

	#define D2_s2_s1 string s2     = s1(1,3)
	D2_s2_s1;      
	assert( s1 == "abcd" );
	assert( s1_mid == "bc" );
	assert( s2 == "bc" );
	sout | xstr(D2_s2_s1) | "\t& " | s1 | "\t& " | s1_mid | "\t& " | s2 | "\t\\\\";
	sout | "\\end{tabular}";
	sout | "\\par\\noindent";

    sout | "Again, @`shareEdits@ passes changes in both directions; copy does not.  Note the difference in index values, with the \\emph{b} position being 1 in the longer string and 0 in the shorter strings.  In the case of s1 aliasing with @s1_mid@, the very same character is being accessed by different postitions.";
	sout | "\\par\\noindent";
	sout | "\\begin{tabular}{llll}";
	sout | "\t\t\t\t& @s1@\t& @s1_mid@\t& @s2@\t\\\\";
	sout | "\t& " | s1 | "\t& " | s1_mid | "\t& " | s2 | "\t\\\\";

	#define D2_s1_plus s1    [1] = '+'
	D2_s1_plus;
	assert( s1 == "a+cd" );
	assert( s1_mid == "+c" );
	assert( s2 == "bc" );
	sout | xstr(D2_s1_plus) | "\t& " | s1 | "\t& " | s1_mid | "\t& " | s2 | "\t\\\\";

	#define D2_s1mid_minus s1_mid[0] = '-'
	D2_s1mid_minus;
	assert( s1 == "a-cd" );
	assert( s1_mid == "-c" );
	assert( s2 == "bc" );
	sout | xstr(D2_s1mid_minus) | "\t& " | s1 | "\t& " | s1_mid | "\t& " | s2 | "\t\\\\";
      
    #define D2_s2_pipe s2    [0] = '|'
	D2_s2_pipe;
	assert( s1 == "a-cd" );
	assert( s1_mid == "-c" );
	assert( s2 == "|c" );
	sout | xstr(D2_s2_pipe) | "\t& " | s1 | "\t& " | s1_mid | "\t& " | s2 | "\t\\\\";
	sout | "\\end{tabular}";
	sout | "\\par\\noindent";

    sout | "Once again, assignment of a value is a modificiation that flows through the aliasing relationship, without affecting its structure.";
	sout | "\\par\\noindent";
	sout | "\\begin{tabular}{llll}";
	sout | "\t\t\t\t& @s1@\t& @s1_mid@\t& @s2@\t\\\\";
	sout | "\t& " | s1 | "\t& " | s1_mid | "\t& " | s2 | "\t\\\\";
    
	#define D2_s1mid_ff s1_mid = "ff"
	D2_s1mid_ff;
	assert( s1 == "affd" );
	assert( s1_mid == "ff" );
	assert( s2 == "|c" );
	sout | xstr(D2_s1mid_ff) | "\t& " | s1 | "\t& " | s1_mid | "\t& " | s2 | "\t\\\\";
      
	#define D2_s2_gg s2     = "gg"
	D2_s2_gg;
	assert( s1 == "affd" );
	assert( s1_mid == "ff" );
	assert( s2 == "gg" );
	sout | xstr(D2_s2_gg) | "\t& " | s1 | "\t& " | s1_mid | "\t& " | s2 | "\t\\\\";
	sout | "\\end{tabular}";
	sout | "\\par\\noindent";
    
    sout | "In the \\emph{ff} step, which is a positive example of flow across an aliasing relationship, the result is straightforward to accept because the flow direction is from contained (small) to containing (large).  The following rules for edits through aliasing substrings will guide how to flow in the opposite direction.";
	sout | "\\par";


    sout | "Growth and shrinkage are natural extensions.  An empty substring is a real thing, at a well-defined location, whose meaning is extrapolated from the examples so far.  The intended metaphor is to operating a GUI text editor.  Having an aliasing substring is like using the mouse to select a few words.  Assigning onto an aliasign substring is like typing from having a few words selected:  depending how much you type, the file being edited can get shorter or longer.";
	sout | "\\par\\noindent";
	sout | "\\begin{tabular}{lll}";
	sout | "\t\t\t\t& @s1@\t& @s1_mid@\t\\\\";
	sout | "\t& " | s1 | "\t& " | s1_mid | "\t\\\\";

	assert( s1 == "affd" );
//	assert( s1_mid == "fc" );                                                     // ????????? bug?
	sout | xstr(D2_s2_gg) | "\t& " | s1 | "\t& " | s1_mid | "\t\\\\";

	#define D2_s1mid_hhhh s1_mid = "hhhh"
	D2_s1mid_hhhh;
	assert( s1 == "ahhhhd" );
	assert( s1_mid == "hhhh" );
	sout  | xstr(D2_s1mid_hhhh)  | "\t& " | s1 | "\t& " | s1_mid | "\t\\\\";
      
	#define D2_s1mid_i s1_mid = "i"
	D2_s1mid_i;
	assert( s1 == "aid" );
	assert( s1_mid == "i" );
	sout  | xstr(D2_s1mid_i)  | "\t& " | s1 | "\t& " | s1_mid | "\t\\\\";
    
	#define D2_s1mid_empty s1_mid = ""
	D2_s1mid_empty;
	assert( s1 == "ad" );
	// assert( s1_mid == "" );    ------ Should be so, but fails
	sout  | xstr(D2_s1mid_empty)  | "\t& " | s1 | "\t& " | s1_mid | "\t\\\\";

	#define D2_s1mid_jj s1_mid = "jj"
	D2_s1mid_jj;
	assert( s1 == "ajjd" );
	assert( s1_mid == "jj" );
	sout  | xstr(D2_s1mid_jj)  | "\t& " | s1 | "\t& " | s1_mid | "\t\\\\";
	sout | "\\end{tabular}";
	sout | "\\par\\noindent";
    
    sout | "Multiple portions can be aliased.  When there are several aliasing substrings at once, the text editor analogy becomes an online multi-user editor.  I should be able to edit a paragraph in one place (changing the document's length), without my edits affecting which letters are within a mouse-selection that you had made previously, somewhere else.";
	sout | "\\par\\noindent";
	sout | "\\begin{tabular}{lllll}";
	sout | "\t\t\t\t& @s1@\t& @s1_bgn@\t& @s1_mid@\t& @s1_end@\t\\\\";

	#define D2_s1bgn_s1	string s1_bgn = s1(0, 1)`shareEdits
	D2_s1bgn_s1;
	sout  | xstr(D2_s1bgn_s1)  | "\t\\\\";

	#define D2_s1end_s1 string s1_end = s1(3, 4)`shareEdits
	D2_s1end_s1;
	assert( s1 == "ajjd" );
	assert( s1_bgn == "a" );
	assert( s1_mid == "jj" );
	assert( s1_end == "d" );
	sout  | xstr(D2_s1end_s1)  | "\t& " | s1 | "\t& " | s1_bgn | "\t& " | s1_mid | "\t& " | s1_end | "\t\\\\";
      
	#define D1_s1bgn_zzz s1_bgn = "zzzz"
	D1_s1bgn_zzz;
	assert( s1 == "zzzzjjd" );
	assert( s1_bgn == "zzzz" );
	assert( s1_mid == "jj" );
	assert( s1_end == "d" );
	sout  | xstr(D1_s1bgn_zzz)  | "\t& " | s1 | "\t& " | s1_bgn | "\t& " | s1_mid | "\t& " | s1_end | "\t\\\\";
	sout | "\\end{tabular}";
	sout | "\\par\\noindent";

    sout | "When an edit happens on an aliasing substring that overlaps another, an effect is unavoidable.  Here, the passive party sees its selection shortened, to exclude the characters that were not part of the original selection.";
	sout | "\\par\\noindent";
	sout | "\\begin{tabular}{llllll}";
	sout | "\t\t\t\t& @s1@\t& @s1_bgn@\t& @s1_crs@\t& @s1_mid@\t& @s1_end@\t\\\\";
    
	#define D2_s1crs_s1 string s1_crs = s1(3, 5)`shareEdits
	D2_s1crs_s1;
	assert( s1 == "zzzzjjd" );
	assert( s1_bgn == "zzzz" );
	assert( s1_crs == "zj" );
	assert( s1_mid == "jj" );
	assert( s1_end == "d" );  
	sout  | xstr(D2_s1crs_s1)  | "\t& " | s1 | "\t& " | s1_bgn | "\t& " | s1_crs | "\t& " | s1_mid | "\t& " | s1_end | "\t\\\\";
    
	#define D2_s1crs_ppp s1_crs = "+++"
	D2_s1crs_ppp;
	assert( s1 == "zzz+++jd" );
	assert( s1_bgn == "zzz" );
	assert( s1_crs == "+++" );
	assert( s1_mid == "j" );
	assert( s1_end == "d" );
	sout  | xstr(D2_s1crs_ppp)  | "\t& " | s1 | "\t& " | s1_bgn | "\t& " | s1_crs | "\t& " | s1_mid | "\t& " | s1_end | "\t\\\\";
	sout | "\\end{tabular}";
	sout | "\\par\\noindent";
	sout | "TODO: finish typesetting the demo";

    // "This shortening behaviour means that a modification has to occur entirely inside a substring, to show up in that substring.  Sharing changes through the intersection of partially overlapping aliases is still possible, so long as the receiver's boundary is not inside the edit."

	string word = "Phi";
	string consonants = word(0,2)`shareEdits;
	string miniscules = word(1,3)`shareEdits;
	assert( word == "Phi" );
	assert( consonants == "Ph" );
	assert( miniscules == "hi" );
      
	consonants[1] = 's';
	assert( word == "Psi" );
	assert( consonants == "Ps" );
	assert( miniscules == "si" );

	// "The extreme form of this shortening happens when a bystander alias is a proper substring of an edit.  The bystander becomes an empty substring."
    
	string all = "They said hello again";
	string greet     = all(10,15)`shareEdits;
	string greet_bgn = all(10,11)`shareEdits;
	string greet_end = all(14,15)`shareEdits;
      
	assert( all == "They said hello again" );
	assert( greet == "hello" );
	assert( greet_bgn == "h" );
	assert( greet_end == "o" );
      
    
	greet = "sup";
	assert( all == "They said sup again" );
	assert( greet == "sup" );
	// assert( greet_bgn == "" );    ------ Should be so, but fails
	// assert( greet_end == "" );
      
    
  

  
    /* As in the earlier step where \emph{aj} becomes \emph{ajjd}, such empty substrings maintain their places in the total string, and can be used for filling it.  Because @greet_bgn@ was orginally at the start of the edit, in the outcome, the empty @greet_bgn@ sits just before the written value.  Similarly @greed_end@ goes after.  Though not shown, an overwritten substring at neither side goes arbitrarily to the before side. */
  

  
    
	greet_bgn = "what"; 
      
      
	assert( all == "They said whatsup again" );
      
	assert( greet == "sup" );
      
	assert( greet_bgn == "what" );
	// assert( greet_end == "" );    ------ Should be so, but fails
      
    
	greet_end = "..."; 
      
      
	assert( all == "They said whatsup... again" );
      
	assert( greet == "sup" );
      
	assert( greet_bgn == "what" );
      
	assert( greet_end == "..." );
      
    
  

  
    /* Though these empty substrings hold their places in the total string, an empty string only belongs to bigger strings when it occurs completely inside them.  There is no such state as including an empty substring at an edge.  For this reason, @word@ gains the characters added by assigning to @greet_bgn@ and @greet_end@, but the string @greet@ does not. */
  

}


int main(int argc, char ** argv) {

    demo1();
    demo2();
    printf("%% %s done running\n", argv[0]);
}
