1. A Puzzle in Eu -- what would you do?

Forget libraries for this puzzler. How would you solve this problem?

Say you are happily programming a program file called Mordor.ex based on
Tolkein's work...

<Eu-ish pseudocode, mentally fill in missing stuff>
...
for i=1 to length(rings) do
  oneRing=RuleThemAll(rings[i])
  oneRing=InTheDarknessBindThem(rings[i])
end for
...
--now for some nefarious Eu code mangling
...
line=GetLine(innocentCode)--strips <33 left/right
commentPart={}
if length(line) then
  x=match("--",line)
  if x then
    codePart=line[1..x-1]
    commentPart=line[x..$]
  else
	codePart=line
  end if
  codePart=NefariouslyMangle(codePart)
  if length(commentPart) then
    commentPart=CackleHideously(commentPart)
  end if
end if
--merrily carry on in blissful ignorance
..
<end Eu-ish>

OK, so this illustrates a general problem, versions of which I have come across
often.

What if the line contents above were this?:
line=InnocentRtn("--In Eu, a comment begins with 2 hyphens") --A real comment

The "x" would report the first "--" and miss the real comment (oops).

The puzzle:
How do *you* deal with this?  There must be a dozen ways, and I'd like to know
what you would do.  The better answers would deal with *any* such search issue,
not just double-hyphens.

Winners will be given virtual silver, blue, or gold stars, and the judges will
be characterized by non-existence.  Post your code, or just give reasoning, as
you wish.

--Quark

new topic     » topic index » view message » categorize

2. Re: A Puzzle in Eu -- what would you do?

DB James wrote:
> 
> 
> What if the line contents above were this?:
> line=InnocentRtn("--In Eu, a comment begins with 2 hyphens") --A real comment
> 
> The "x" would report the first "--" and miss the real comment (oops).
> 
> The puzzle:
> How do *you* deal with this?

In this case, just reverse the string first. Then, the first set of '--' is
the real comment. Snip it, reverse it back, and carry on.

x = "code --fake comment -- real comment"

becomes

x = "tnemmoc laer -- tnemmoc ekaf-- edoc"

then becomes

x = " tnemmoc ekaf-- edoc"

which finalizes to

x = "code --fake comment "

-=ck
"Programming in a state of EUPHORIA."
http://www.cklester.com/euphoria/

new topic     » goto parent     » topic index » view message » categorize

3. Re: A Puzzle in Eu -- what would you do?

cklester wrote:
> 
> DB James wrote:
> > 
> > 
> > What if the line contents above were this?:
> > line=InnocentRtn("--In Eu, a comment begins with 2 hyphens") --A real
> > comment
> > 
> > The "x" would report the first "--" and miss the real comment (oops).
> > 
> > The puzzle:
> > How do *you* deal with this?
> 
> In this case, just reverse the string first. Then, the first set of '--' is
> the real comment. Snip it, reverse it back, and carry on.
> 
> x = "code --fake comment -- real comment"
> 
> becomes
> 
> x = "tnemmoc laer -- tnemmoc ekaf-- edoc"
> 
> then becomes
> 
> x = " tnemmoc ekaf-- edoc"
> 
> which finalizes to
> 
> x = "code --fake comment "
> 
> -=ck
> "Programming in a state of EUPHORIA."
> <a
> href="http://www.cklester.com/euphoria/">http://www.cklester.com/euphoria/</a>
> 

Hmm, a good quick fix for the specific problem.  I have used that trick more
than once in the past.  You fell into a trap that I have fallen in many times:
the quick fix and move on, but the real problem remains to bedevil you again
sometime.

Remember the cautionary note in the original puzzle specs:   "The better answers
would deal with *any* such search issue, not just double-hyphens."  Want to try
again, ck?  Anyone else?

--Quark

new topic     » goto parent     » topic index » view message » categorize

4. Re: A Puzzle in Eu -- what would you do?

cklester wrote:
> DB James wrote:
> > What if the line contents above were this?:
> > line=InnocentRtn("--In Eu, a comment begins with 2 hyphens") --A real
> > comment
> > 
> > The "x" would report the first "--" and miss the real comment (oops).
> > 
> > The puzzle:
> > How do *you* deal with this?
> 
> In this case, just reverse the string first. Then, the first set of '--' is
> the real comment. Snip it, reverse it back, and carry on.
> 
> x = "code --fake comment -- real comment"
> 
> becomes
> 
> x = "tnemmoc laer -- tnemmoc ekaf-- edoc"
> 
> then becomes
> 
> x = " tnemmoc ekaf-- edoc"
> 
> which finalizes to
> 
> x = "code --fake comment "

This won't work if the real comment also contains --, like this
s = "-- fake comment" -- real comment with -- in it


--
The Internet combines the excitement of typing 
with the reliability of anonymous hearsay.

tommy online: http://users.telenet.be/tommycarlier
tommy.blog: http://tommycarlier.blogspot.com

new topic     » goto parent     » topic index » view message » categorize

5. Re: A Puzzle in Eu -- what would you do?

sequence ctr ctr = {1}

while 1 do
 ctr &= match("--",line)
 if ctr[&] = 0 then exit endif
end while

ctr now equals a list of all the locations of the "--" in the line.
so from there you can figure out where the real comment is.


Bernie

My files in archive:
w32engin.ew mixedlib.e eu_engin.e win32eru.exw

Can be downloaded here:
http://www.rapideuphoria.com/cgi-bin/asearch.exu?dos=on&win=on&lnx=on&gen=on&keywords=bernie+ryan

new topic     » goto parent     » topic index » view message » categorize

6. Re: A Puzzle in Eu -- what would you do?

Tommy Carlier wrote:
> cklester wrote:
> > DB James wrote:
> > > What if the line contents above were this?:
> > > line=InnocentRtn("--In Eu, a comment begins with 2 hyphens") --A real
> > > comment
> > > 
> > > The "x" would report the first "--" and miss the real comment (oops).
> > > 
> > > The puzzle:
> > > How do *you* deal with this?
> > 
> > In this case, just reverse the string first. Then, the first set of '--' is
> > the real comment. Snip it, reverse it back, and carry on.
> > 
> > x = "code --fake comment -- real comment"
> > 
> > becomes
> > 
> > x = "tnemmoc laer -- tnemmoc ekaf-- edoc"
> > 
> > then becomes
> > 
> > x = " tnemmoc ekaf-- edoc"
> > 
> > which finalizes to
> > 
> > x = "code --fake comment "
> 
> This won't work if the real comment also contains --, like this
> 
> s = "-- fake comment" -- real comment with -- in it
> 

Then it would be impossible to determine that without human intervention.
The PC could never resolve that ambiguity. You'd have to follow the rule:
no '--' in comments! :)

-=ck
"Programming in a state of EUPHORIA."
http://www.cklester.com/euphoria/

new topic     » goto parent     » topic index » view message » categorize

7. Re: A Puzzle in Eu -- what would you do?

DB James wrote:
> 
> 
> Remember the cautionary note in the original puzzle specs:   "The better
> answers would
> deal with *any* such search issue, not just double-hyphens."  Want to try
> again, ck?
>  Anyone else?

Yes, I remember that note, but I can't imagine what you mean. Maybe you could
post such a bedeviling example? :)

Besides, if it gets more complicated than that, I just use Kat's StrTok lib.

-=ck
"Programming in a state of EUPHORIA."
http://www.cklester.com/euphoria/

new topic     » goto parent     » topic index » view message » categorize

8. Re: A Puzzle in Eu -- what would you do?

DB James wrote:
> 
> 

> What if the line contents above were this?:
> line=InnocentRtn("--In Eu, a comment begins with 2 hyphens") --A real comment
> 
> The "x" would report the first "--" and miss the real comment (oops).
> 
> The puzzle:
> How do *you* deal with this?  There must be a dozen ways, and I'd like to know
> what
> you would do.  The better answers would deal with *any* such search issue, not
> just
> double-hyphens.

I once made a program that did somthing like this.
I simlply removed all strings, replaced them with negitive integers,
then removed comments as above. I then went back and replaced
the negative integers with the strings(ignoring any that might
have been cut out with the comments).
This worked fine for:
--A comment
blah("--blah")--A comment
blah("--blah")--A comment with -- in it
blah("--blah")--A comment with "quotes" in it
and also handled strange lines like these by ignoring \"
and removing anything from a " to the end of the line
if there wasn't an ending ".
blah("--\"blah\"")--A comment with -- and a " in it

new topic     » goto parent     » topic index » view message » categorize

9. Re: A Puzzle in Eu -- what would you do?

cklester wrote:
> 
> DB James wrote:
> > 
> > 
> > Remember the cautionary note in the original puzzle specs:   "The better
> > answers would
> > deal with *any* such search issue, not just double-hyphens."  Want to try
> > again, ck?
> >  Anyone else?
> 
> Yes, I remember that note, but I can't imagine what you mean. Maybe you could
> post such a bedeviling example? :)
> 
> Besides, if it gets more complicated than that, I just use Kat's StrTok lib.
> 
> -=ck
> "Programming in a state of EUPHORIA."
> <a
> href="http://www.cklester.com/euphoria/">http://www.cklester.com/euphoria/</a>
> 


ck,

I deliberately left the cautionary note a bit vague to let imagination play
around with it.

Image the search to be instead of the puzzle example, a finding of all "live"
commas in an Eu line of code of some particularly strange series of constants,
like:
constant eenie=Mangle(path&fName,"My mangling, dangling phrase", loc),
meenie=CommitAtrocity(haplessBystander, extremePrejudice, evilLawyer),
minnie=6,moe=repeat('*',40)

Heh, notice I don't define what "live" means.

And to paraphrase the first line in the specs [in Kingfish's voice from "Amos &
Andy"]: "NO-O libraries."

Anyway, try again.  Your answer will probably be better than mine.

--Quark

new topic     » goto parent     » topic index » view message » categorize

10. Re: A Puzzle in Eu -- what would you do?

CoJaBo wrote:
> 
> DB James wrote:
> > 
> > 
> > What if the line contents above were this?:
> > line=InnocentRtn("--In Eu, a comment begins with 2 hyphens") --A real
> > comment
> > 
> > The "x" would report the first "--" and miss the real comment (oops).
> > 
> > The puzzle:
> > How do *you* deal with this?  There must be a dozen ways, and I'd like to
> > know what
> > you would do.  The better answers would deal with *any* such search issue,
> > not just
> > double-hyphens.
> 
> I once made a program that did somthing like this.
> I simlply removed all strings, replaced them with negitive integers,
> then removed comments as above. I then went back and replaced
> the negative integers with the strings(ignoring any that might
> have been cut out with the comments).
> This worked fine for:
> --A comment
> blah("--blah")--A comment
> blah("--blah")--A comment with -- in it
> blah("--blah")--A comment with "quotes" in it
> and also handled strange lines like these by ignoring \"
> and removing anything from a " to the end of the line
> if there wasn't an ending ".
> blah("--\"blah\"")--A comment with -- and a " in it
> 

CaJaBo,

I like this direction of thinking a lot, though I'm not sure about going back
and replacing the negative integers with strings -- why?  But maybe there was a
good reason for it.  Let's see what others come up with.

--Quark

new topic     » goto parent     » topic index » view message » categorize

11. Re: A Puzzle in Eu -- what would you do?

Bernie Ryan wrote:
> 
> sequence ctr ctr = {1}
> 
> while 1 do
>  ctr &= match("--",line)
>  if ctr[&] = 0 then exit endif

that should be

  if ctr[$] = 0 then exit endif

-=ck
"Programming in a state of EUPHORIA."
http://www.cklester.com/euphoria/

new topic     » goto parent     » topic index » view message » categorize

12. Re: A Puzzle in Eu -- what would you do?

Bernie Ryan wrote:
> 
> 
> sequence ctr ctr = {1}
> 
> while 1 do
>  ctr &= match("--",line)
>  if ctr[&] = 0 then exit endif
> end while
> 
> ctr now equals a list of all the locations of the "--" in the line.
> so from there you can figure out where the real comment is.
> 
> 
> Bernie
> 
> My files in archive:
> w32engin.ew mixedlib.e eu_engin.e win32eru.exw
> 
> Can be downloaded here:
> <a
> href="http://www.rapideuphoria.com/cgi-bin/asearch.exu?dos=on&win=on&lnx=on&gen=on&keywords=bernie+ryan">http://www.rapideuphoria.com/cgi-bin/asearch.exu?dos=on&win=on&lnx=on&gen=on&keywords=bernie+ryan</a>
> 

Bernie,

Wow, zen programming.  But my brain has just melted a bit and I don't
understand.  Why start with {1}?  Why not jump forward with each new match? 
Like:
ctr=1
while ctr<=length(line)-length("--") do
  if match("--",line[ctr..$]
matched&=
  ctr=
And what is ctr[&]?

new topic     » goto parent     » topic index » view message » categorize

13. Re: A Puzzle in Eu -- what would you do?

sequence ctr ctr = {1}
 
 while 1 do
  ctr &= match("--",line[ctr[$]..$])
  if ctr[$] = 0 then exit endif
 end while
 
 ctr now equals a list of all the locations of the "--" in the line.
 so from there you can figure out where the real comment is.

Hit the keys too fast

Bernie

My files in archive:
w32engin.ew mixedlib.e eu_engin.e win32eru.exw

Can be downloaded here:
http://www.rapideuphoria.com/cgi-bin/asearch.exu?dos=on&win=on&lnx=on&gen=on&keywords=bernie+ryan

new topic     » goto parent     » topic index » view message » categorize

14. Re: A Puzzle in Eu -- what would you do?

Bernie Ryan wrote:
> 
> 
> sequence ctr ctr = {1}
> 
> while 1 do
>  ctr &= match("--",line)
>  if ctr[&] = 0 then exit endif
> end while
> 
> ctr now equals a list of all the locations of the "--" in the line.
> so from there you can figure out where the real comment is.
> 
> 
> Bernie
> 
> My files in archive:
> w32engin.ew mixedlib.e eu_engin.e win32eru.exw
> 
> Can be downloaded here:
> <a
> href="http://www.rapideuphoria.com/cgi-bin/asearch.exu?dos=on&win=on&lnx=on&gen=on&keywords=bernie+ryan">http://www.rapideuphoria.com/cgi-bin/asearch.exu?dos=on&win=on&lnx=on&gen=on&keywords=bernie+ryan</a>
> 

Bernie,

I tried responding to this and accidentally hit some magic key combination that
sent my post to neverland.  Trying again...

This is interesting, and simple, but why {1} and what is ctr[&]?  Why not jump
forward with each match?  Something like (hoping this would work):

while length(line) and match("--",line) do
  ctr&=match("--",line)
  line=line[ctr[$]+1..$]
end while

--Quark

new topic     » goto parent     » topic index » view message » categorize

15. Re: A Puzzle in Eu -- what would you do?

DB James wrote:
> 
> Image the search to be instead of the puzzle example, a finding of all "live"
> commas
> in an Eu line of code of some particularly strange series of constants, like:
> constant eenie=Mangle(path&fName,"My mangling, dangling phrase", loc),
> meenie=CommitAtrocity(haplessBystander,
> extremePrejudice, evilLawyer), minnie=6,moe=repeat('*',40)
> 
> Anyway, try again.  Your answer will probably be better than mine.

Yipes! I'll let you guys figure it out. I would use a library in this case. ;)

-=ck
"Programming in a state of EUPHORIA."
http://www.cklester.com/euphoria/

new topic     » goto parent     » topic index » view message » categorize

16. Re: A Puzzle in Eu -- what would you do?

Bernie Ryan wrote:
> 
> 
> sequence ctr ctr = {1}
>  
>  while 1 do
>   ctr &= match("--",line[ctr[$]..$])
>   if ctr[$] = 0 then exit endif
>  end while
>  
>  ctr now equals a list of all the locations of the "--" in the line.
>  so from there you can figure out where the real comment is.
> 
> Hit the keys too fast
> 
> Bernie
> 
> My files in archive:
> w32engin.ew mixedlib.e eu_engin.e win32eru.exw
> 
> Can be downloaded here:
> <a
> href="http://www.rapideuphoria.com/cgi-bin/asearch.exu?dos=on&win=on&lnx=on&gen=on&keywords=bernie+ryan">http://www.rapideuphoria.com/cgi-bin/asearch.exu?dos=on&win=on&lnx=on&gen=on&keywords=bernie+ryan</a>
> 

Bernie, your code went into an infinite loop for me. Try this slightly more
complicated version below:
sequence line, ctr
integer here

line = "there are no -- comments -- in this thing -- here"
ctr = {0}

while 1 do
  here = match("--", line[ctr[$]+1..$])
  if here = 0 then exit end if
  ctr &= here + ctr[$]
end while
? ctr


With your code it gets stuck in an infinite loop with ctr = {1,14,1,14,1,14...}

================================================================
"Actually, I'm sitting on my butt staring at a computer screen."
                                                  - Tom Tomorrow

j.

new topic     » goto parent     » topic index » view message » categorize

17. Re: A Puzzle in Eu -- what would you do?

Jason Gade wrote:
> 
> Bernie Ryan wrote:
> > 
> > 
> > sequence ctr ctr = {1}
> >  
> >  while 1 do
> >   ctr &= match("--",line[ctr[$]..$])
> >   if ctr[$] = 0 then exit endif
> >  end while
> >  
> >  ctr now equals a list of all the locations of the "--" in the line.
> >  so from there you can figure out where the real comment is.
> > 
> > Hit the keys too fast
> > 
> > Bernie
> > 
> > My files in archive:
> > w32engin.ew mixedlib.e eu_engin.e win32eru.exw
> > 
> > Can be downloaded here:
> > <a
> > href="http://www.rapideuphoria.com/cgi-bin/asearch.exu?dos=on&win=on&lnx=on&gen=on&keywords=bernie+ryan">http://www.rapideuphoria.com/cgi-bin/asearch.exu?dos=on&win=on&lnx=on&gen=on&keywords=bernie+ryan</a>
> > 
> 
> Bernie, your code went into an infinite loop for me. Try this slightly more
> complicated
> version below:
> }}}
<eucode>
> sequence line, ctr
> integer here
> 
> line = "there are no -- comments -- in this thing -- here"
> ctr = {0}
> 
> while 1 do
>   here = match("--", line[ctr[$]+1..$])
>   if here = 0 then exit end if
>   ctr &= here + ctr[$]
> end while
> ? ctr
> <font color="#330033"></eucode>
{{{
</font>
> 
> With your code it gets stuck in an infinite loop with ctr =
> {1,14,1,14,1,14...}
> 
> ================================================================
> "Actually, I'm sitting on my butt staring at a computer screen."
>                                                   - Tom Tomorrow
> 
> j.
> 

of course for comments that start with '----' you get a number for every match.
Maybe a for loop increasing the index by 2...

================================================================
"Actually, I'm sitting on my butt staring at a computer screen."
                                                  - Tom Tomorrow

j.

new topic     » goto parent     » topic index » view message » categorize

18. Re: A Puzzle in Eu -- what would you do?

I didn't feel like using match so I wrote this. This is one of the few times
I'll agree that GOTOs could be useful...


function strip_comments(sequence code)

    integer index,     -- index of letter in code
            in_string  -- "boolean"
            
    in_string = 0

    -- Replicate for loop
    index = 1

    while length(code) >= index do

        if code[index] = '\"' and code[index - 1] != '\\' then
            if in_string then
                in_string = not in_string
            else
                in_string = 1
            end if
        end if

        if not in_string
          -- What you search for goes here
          and code[index] = '-' and code[index + 1] = '-' then
            return code[1..index - 1]
        end if

        index += 1

    end while
    
    return code

end function

The Euphoria Standard Library project :
    http://esl.sourceforge.net/
The Euphoria Standard Library mailing list :
    https://lists.sourceforge.net/lists/listinfo/esl-discussion

new topic     » goto parent     » topic index » view message » categorize

19. Re: A Puzzle in Eu -- what would you do?

OK, I'll bite since I don't mind being ridiculed if it comes to that :)

I had to do this for a program already and here's what I did only I've 
modified it cosmetically to remove the swear word I had in the comments. :)

function munch(sequence delimiter, object string)

	integer delimiterpos
	sequence result
	result = {}

	delimiterpos = match(delimiter,string)
	while delimiterpos != 0 do

		result = append(result,string[1..delimiterpos - 1])
		string = string[delimiterpos + length(delimiter)..length(string)]
	        delimiterpos = match(delimiter,string)
		if equal(delimiterpos,0) then

			result = append(result,string) --last field
		end if
	end while

	return result
end function

So, I would take your line and do something like this with it:

sequence whatitis

whatitis = line
whatitis = munch(whatitis)
for i = 1 to length(whatitis) do
--do something really fancy to figure what the chunks you got back 
--really are relying on some kind of logic that's bound to fail :)
end for

So I hope I read the puzzle right.  You're not asking me to figure out
one is the real comment, just to make sure it gets picked up.  And the
routine is generic because the programmer supplies the delimiter.  The
only code I added for this puzzle it the part about adding the length of
the delimiter.  I used to assume a length of 1, but this way it's even 
more generic.

If that's the right word.

And if it isn't who cares? :)

So let the ridicule begin :) :D

new topic     » goto parent     » topic index » view message » categorize

20. Re: A Puzzle in Eu -- what would you do?

D. Newhall wrote:
> 
> I didn't feel like using match so I wrote this. This is one of the few times
> I'll agree
> that GOTOs could be useful...
> 
> 
> function strip_comments(sequence code)
> 
>     integer index,     -- index of letter in code
>             in_string  -- "boolean"
>             
>     in_string = 0
> 
>     -- Replicate for loop
>     index = 1
> 
>     while length(code) >= index do
> 
>         if code[index] = '\"' and code[index - 1] != '\\' then
>             if in_string then
>                 in_string = not in_string
>             else
>                 in_string = 1
>             end if
>         end if
> 
>         if not in_string
>           -- What you search for goes here
>           and code[index] = '-' and code[index + 1] = '-' then
>             return code[1..index - 1]
>         end if
> 
>         index += 1
> 
>     end while
>     
>     return code
> 
> end function

Oops! Ddid it backwards.

          and code[index] = '-' and code[index + 1] = '-' then
            return code[1..index - 1]

should instead be 

          and code[index] = '-' and code[index + 1] = '-' then
            return code[index + 2..$]


The Euphoria Standard Library project :
    http://esl.sourceforge.net/
The Euphoria Standard Library mailing list :
    https://lists.sourceforge.net/lists/listinfo/esl-discussion

new topic     » goto parent     » topic index » view message » categorize

21. Re: A Puzzle in Eu -- what would you do?

Boy I wish a guy could edit posts :)

Of course I screwed up the call to my own durn function.  It should read:

whatisit = munch("--",whatisit)

And I forgot to quote the original text which makes it bad too.  But "line" 
comes from the original puzzle and in my testing of my code, I picked it up
out of a file (so I wouldn't have to mess with quoting quotes).  Anyway, 
"line" contains this:

InnocentRtn("--In Eu, a comment begins with 2 hyphens") --A real comment

And in my testing, whatisit ends up with a length of 3 yielding (in a for loop)
the following:

InnocentRtn("
In Eu, a comment begins with 2 hyphens") 
A real comment

So I do believe the code does what's called for.

new topic     » goto parent     » topic index » view message » categorize

22. Re: A Puzzle in Eu -- what would you do?

DB James wrote:
> 
> Bernie Ryan wrote:
> > 
> > 
> > sequence ctr ctr = {1}
> > 
> > while 1 do
> >  ctr &= match("--",line)
> >  if ctr[&] = 0 then exit endif
> > end while
> > 
> > ctr now equals a list of all the locations of the "--" in the line.
> > so from there you can figure out where the real comment is.
> > 
> > 
> > Bernie
> > 
> > My files in archive:
> > w32engin.ew mixedlib.e eu_engin.e win32eru.exw
> > 
> > Can be downloaded here:
> > <a
> > href="http://www.rapideuphoria.com/cgi-bin/asearch.exu?dos=on&amp;win=on&amp;lnx=on&amp;gen=on&amp;keywords=bernie+ryan">http://www.rapideuphoria.com/cgi-bin/asearch.exu?dos=on&win=on&lnx=on&gen=on&keywords=bernie+ryan</a>
> > 
> 
> Bernie,
> 
> I tried responding to this and accidentally hit some magic key combination
> that sent
> my post to neverland.  Trying again...
> 
> This is interesting, and simple, but why {1} and what is ctr[&]?  Why not jump
> forward
> with each match?  Something like (hoping this would work):
> 
> while length(line) and match("--",line) do
>   ctr&=match("--",line)
>   line=line[ctr[$]+1..$]
> end while
> 
> --Quark
> 

Groan.  Me again.  Thanks for not pointing out what I conspiculously forgot to
do in my example.  Here is a complete test for what I was trying to get at:

integer x
sequence line,ctr

line="InnocentRtn(\"--In Eu, a comment begins with 2 hyphens\") --A real
comment"
ctr={}
x=match("--",line)
while x do
	ctr&=x
	x=match("--",line[x+1..$])
	if x then x+=ctr[$] end if  
end while

? ctr
while equal(get_key(),-1) do end while


This correctly gives {14,57} -- the two wanted numbers.

However, it isn't a great answer to the puzzle, though useful.

--Quark

new topic     » goto parent     » topic index » view message » categorize

23. Re: A Puzzle in Eu -- what would you do?

On Fri, 05 Aug 2005 12:47:16 -0700, DB James <guest at RapidEuphoria.com>
wrote:

>The puzzle:
>How do *you* deal with this?  There must be a dozen ways, and I'd like 
>to know what you would do.  The better answers would deal with *any* 
>such search issue, not just double-hyphens.

Since there are many places in Edita which must explicitly strip
comments, here is the general approach I use. It may not be as general
as you hoped, but it elegantly and efficiently covers all cases I know
of for comments and strings:

function getCodeAndComments(sequence line)
integer k, len, ch
	len=length(line)
	k=1
	while k<=len do
		ch=line[k]
		if ch='-' and k<len and line[k+1]='-' then
			exit
		elsif ch='\"' then
			while k<len do
				k+=1
				ch=line[k]
				if ch='\"' then exit end if
				if ch='\\' then k+=1 end if
			end while
		end if
		k+=1
	end while
	return {line[1..k-1],line[k..len]}
end function


Regards,
Pete

new topic     » goto parent     » topic index » view message » categorize

24. Re: A Puzzle in Eu -- what would you do?

On Sat, 06 Aug 2005 00:54:49 +0100, Pete Lomax
<petelomax at blueyonder.co.uk> wrote:

<snip>
oops, I left a bug in that, handling "\ (aka "\"\\"):
>}}}
<eucode>
>function getCodeAndComments(sequence line)
>integer k, len, ch
>	len=length(line)
>	k=1
>	while k<=len do
>		ch=line[k]
>		if ch='-' and k<len and line[k+1]='-' then
>			exit
>		elsif ch='\"' then
>			while k<len do
>				k+=1
>				ch=line[k]
>				if ch='\"' then exit end if
				if ch='\\' and k<len then k+=1 end if
>			end while
>		end if
>		k+=1
>	end while
>	return {line[1..k-1],line[k..len]}
>end function
></eucode>
{{{


Regards,
Pete

new topic     » goto parent     » topic index » view message » categorize

25. Re: A Puzzle in Eu -- what would you do?

On 5 Aug 2005, at 13:18, Tommy Carlier wrote:

> 
> 
> posted by: Tommy Carlier <tommy.carlier at telenet.be>
> 
> cklester wrote:
> > DB James wrote:
> > > What if the line contents above were this?:
> > > line=InnocentRtn("--In Eu, a comment begins with 2 hyphens") --A real
> > > comment
> > > 
> > > The "x" would report the first "--" and miss the real comment (oops).
> > > 
> > > The puzzle:
> > > How do *you* deal with this?
> > 
> > In this case, just reverse the string first. Then, the first set of '--' is
> > the real comment. Snip it, reverse it back, and carry on.
> > 
> > x = "code --fake comment -- real comment"
> > 
> > becomes
> > 
> > x = "tnemmoc laer -- tnemmoc ekaf-- edoc"
> > 
> > then becomes
> > 
> > x = " tnemmoc ekaf-- edoc"
> > 
> > which finalizes to
> > 
> > x = "code --fake comment "
> 
> This won't work if the real comment also contains --, like this
> }}}
<eucode>
> s = "-- fake comment" -- real comment with -- in it
> </eucode>
{{{


Or this one: 
x = "--one" -- "--one" is a sequence

That's the one that has me bedeviled, without writing a complete parser for 
DB. And in that case, i'd look at DavidC's Ox parser to solve it. The parses() 
in strtok v3 can do it, but that means using a lib, and code optomised for one 
set of syntactic rules, not like the contest,,, err, i mean "puzzle" rules
state.

Kat

new topic     » goto parent     » topic index » view message » categorize

26. Re: A Puzzle in Eu -- what would you do?

On 5 Aug 2005, at 16:52, DB James wrote:

> 
> 
> posted by: DB James <larch at adelphia.net>
> 
> DB James wrote:
> > 
> > Bernie Ryan wrote:
> > > 
> > > 
> > > sequence ctr ctr = {1}
> > > 
> > > while 1 do
> > >  ctr &= match("--",line)
> > >  if ctr[&] = 0 then exit endif
> > > end while
> > > 
> > > ctr now equals a list of all the locations of the "--" in the line.
> > > so from there you can figure out where the real comment is.
> > > 
> > > 
> > > Bernie
> > > 
> > > My files in archive:
> > > w32engin.ew mixedlib.e eu_engin.e win32eru.exw
> > > 
> > > Can be downloaded here:
> > > <a
> > >
> > > href="http://www.rapideuphoria.com/cgi-bin/asearch.exu?dos=on&amp;win=on&amp
> > >
> > > ;lnx=on&amp;gen=on&amp;keywords=bernie+ryan">http://www.rapideuphoria.com/cg
> > > i-bin/asearch.exu?dos=on&win=on&lnx=on&gen=on&keywords=bernie+ryan</a>
> > > 
> > 
> > Bernie,
> > 
> > I tried responding to this and accidentally hit some magic key combination
> > that sent my post to neverland.  Trying again...
> > 
> > This is interesting, and simple, but why {1} and what is ctr[&]?  Why not
> > jump
> > forward with each match?  Something like (hoping this would work):
> > 
> > while length(line) and match("--",line) do
> >   ctr&=match("--",line)
> >   line=line[ctr[$]+1..$]
> > end while
> > 
> > --Quark
> > 
> 
> Groan.  Me again.  Thanks for not pointing out what I conspiculously forgot to
> do in my example.  Here is a complete test for what I was trying to get at:
> 
> }}}
<eucode>
> integer x
> sequence line,ctr
> 
> line="InnocentRtn(\"--In Eu, a comment begins with 2 hyphens\") --A real
> comment" ctr={} x=match("--",line) while x do
>  ctr&=x
>  x=match("--",line[x+1..$])
>  if x then x+=ctr[$] end if  
> end while
> 
> ? ctr
> while equal(get_key(),-1) do end while
> 
> </eucode>
{{{

> 
> This correctly gives {14,57} -- the two wanted numbers.
> 
> However, it isn't a great answer to the puzzle, though useful.

Oh, you *wanted* the incorrect comment too?! Well then:

text = replace(text,"\r","\n")
text = replace(text,"\"","\n")
text = replace(text,"\\"","\n")

parsed = parses(text,{"--"})

for loop = 1 to length(parsed) do
   place = match("\n",parsed[loop])
   if place = 0 then place = length(parsed[loop]) end if
   comment &= {parsed[loop][1..place]}
end for


Kat

new topic     » goto parent     » topic index » view message » categorize

27. Re: A Puzzle in Eu -- what would you do?

DB James wrote:
> 
> 
> posted by: DB James <larch at adelphia.net>
> 
> 
> Forget libraries for this puzzler. How would you solve this problem?
> 
> Say you are happily programming a program file called Mordor.ex based
> on Tolkein's work...
> 
> <Eu-ish pseudocode, mentally fill in missing stuff> ... for i=1 to
> length(rings) do oneRing=RuleThemAll(rings[i]) 
> oneRing=InTheDarknessBindThem(rings[i]) end for ... --now for some
> nefarious Eu code mangling ... line=GetLine(innocentCode)--strips <33
> left/right commentPart={} if length(line) then x=match("--",line) if
> x then codePart=line[1..x-1] commentPart=line[x..$] else 
> codePart=line end if codePart=NefariouslyMangle(codePart) if
> length(commentPart) then commentPart=CackleHideously(commentPart) end
> if end if --merrily carry on in blissful ignorance .. <end Eu-ish>
> 
> OK, so this illustrates a general problem, versions of which I have
> come across often.
> 
> What if the line contents above were this?: line=InnocentRtn("--In
> Eu, a comment begins with 2 hyphens") --A real comment
> 
> The "x" would report the first "--" and miss the real comment (oops).
> 
> 
> The puzzle: How do *you* deal with this?  There must be a dozen ways,
> and I'd like to know what you would do.  The better answers would
> deal with *any* such search issue, not just double-hyphens.
> 
> Winners will be given virtual silver, blue, or gold stars, and the
> judges will be characterized by non-existence.  Post your code, or
> just give reasoning, as you wish.
> 
> --Quark

Well the following *almost* works -- it doesn't handle \" as the last 
test will show. I don't have time this weekend to work further on it; 
let me know if it misses any other tests.
function count_matches(object findme, sequence list)

     integer count, next, found_it

     if sequence(findme) then
         next = length(findme)
     else
         findme = {findme}
         next = 1
     end if

     count = 0
     found_it = match(findme, list)
     while found_it do
         count += 1
         list = list[found_it + next..$]
         found_it = match(findme, list)
     end while

     return count

end function

type even(integer i)

     return (i * 2) = (floor(i/2) * 2)

end type

type odd(integer i)

     return not even(i)

end type

function find_comments(sequence line)

     integer qt, x, y
     sequence new_line

     x = match("--", line)
     if x != 0 then
         y = find('\"', line)
         if y = 0 or y > x then
	        return {line[1..x-1], line[x..$]}  -- codePart, commentPart
         else
             qt = count_matches('\"', line[1..y])
             if odd(qt) then
	            y += find('\"', line[y+1..$])
	            if y = 0 then
		            return {line, {}}  -- codePart, commentPart
		        end if  -- bad form
		        new_line = find_comments(line[y+1..$])
		        new_line[1] = line[1..y] & new_line[1]
		        return new_line
		    else
			    return {line[1..x-1], line[x..$]} -- codePart, commentPart
	        end if	
         end if
     else
	    return {line, ""}
     end if

end function

constant HEADER = "Now testing string "
constant newline = '\n'

sequence label, test, test_string

test_string = "x = \"--one\" -- \"--one\" is a sequence"
label = HEADER & test_string & newline
test = find_comments(test_string)
puts(1, label & "code " & test[1] & newline & "comment " & test[2])

puts(1, newline & newline)

test_string = "InnocentRtn(\"--In Eu, a comment begins with 2 hyphens\") 
--A real comment"
label = HEADER & test_string & newline
test = find_comments(test_string)
puts(1, label & "code " & test[1] & newline & "comment " & test[2])

puts(1, newline & newline)

test_string = "x = \"\\\" -- a string with more quotes!\\\"\" -- comment"
label = HEADER & test_string & newline
test = find_comments(test_string)
puts(1, label & "code " & test[1] & newline & "comment " & test[2])


-- 
================================================================
"Actually, I'm sitting on my butt staring at a computer screen."
                                                   - Tom Tomorrow
j.

new topic     » goto parent     » topic index » view message » categorize

28. Re: A Puzzle in Eu -- what would you do?

My formatting and screen-wrapping is off a little...

================================================================
"Actually, I'm sitting on my butt staring at a computer screen."
                                                  - Tom Tomorrow

j.

new topic     » goto parent     » topic index » view message » categorize

29. Re: A Puzzle in Eu -- what would you do?

At 12:47 PM 8/5/05 -0700, you wrote:

>
>
>posted by: DB James <larch at adelphia.net>
>
>
>Forget libraries for this puzzler. How would you solve this problem?
>
>Say you are happily programming a program file called Mordor.ex based on 
>Tolkein's work...
>
><Eu-ish pseudocode, mentally fill in missing stuff>
>...
>for i=1 to length(rings) do
>   oneRing=RuleThemAll(rings[i])
>   oneRing=InTheDarknessBindThem(rings[i])
>end for
>...
>--now for some nefarious Eu code mangling
>...
>line=GetLine(innocentCode)--strips <33 left/right
>commentPart={}
>if length(line) then
>   x=match("--",line)
>   if x then
>     codePart=line[1..x-1]
>     commentPart=line[x..$]
>   else
>         codePart=line
>   end if
>   codePart=NefariouslyMangle(codePart)
>   if length(commentPart) then
>     commentPart=CackleHideously(commentPart)
>   end if
>end if
>--merrily carry on in blissful ignorance
>..
><end Eu-ish>
>
>OK, so this illustrates a general problem, versions of which I have come 
>across often.
>
>What if the line contents above were this?:
>line=InnocentRtn("--In Eu, a comment begins with 2 hyphens") --A real comment
>
>The "x" would report the first "--" and miss the real comment (oops).
>
>The puzzle:
>How do *you* deal with this?  There must be a dozen ways, and I'd like to 
>know what you would do.  The better answers would deal with *any* such 
>search issue, not just double-hyphens.
>
>Winners will be given virtual silver, blue, or gold stars, and the judges 
>will be characterized by non-existence.  Post your code, or just give 
>reasoning, as you wish.
>
>--Quark

-- STRIPCMT.E  Sat, Aug  6 2005 12:29:12 am

include misc.e FOR DEMO ONLY

with trace trace(1) function strip_comment(sequence line, sequence arg) sequence s integer inQuote, pos, ch s = line inQuote = 0

replace all escaped backslashes with spaces pos = match("

", s) while pos do s[pos..pos + 1] = " " pos = match("

", s) end while replace all escaped double quotes with spaces pos = match("
\"", s) while pos do s[pos..pos + 1] = " " pos = match("
\"", s) end while

replace remaining quoted strings with spaces for i = 1 to length(s) do ch = s[i] if ch = '"' then if inQuote then inQuote = 0 else inQuote = 1 end if elsif inQuote then s[i] = ' ' end if end for look for comment

pos = match("", s)

look for argument pos = match(arg, s) if pos then return line[1..pos - 1] else return line end if end function

sequence line

line = "result = x(\"\", \"

\", \"--
\"
\"\")
real comment "

pretty_print(1, line, {2}) puts(1, "\n\n")

pretty_print(1, strip_comment(line, ""), {2}) puts(1, "\n\n")

line = "line=InnocentRtn(\"In Eu, a comment begins with 2 hyphens\") A real comment" pretty_print(1, line, {2}) puts(1, "\n\n") pretty_print(1, strip_comment(line, ""), {2}) puts(1, "\n\n")

<\eucode>

I've written a few of these using more complex logic but it was hard. This was almost dashed off. Of course it could be improved but it's fast enough for stripping comments from EU code. I ran a large program through it which worked fine afterwards. I hope this is what you meant.

Bob }}}

new topic     » goto parent     » topic index » view message » categorize

30. Re: A Puzzle in Eu -- what would you do?

Bob Elia wrote:
> 
<SNIP>
> >--Quark
> 
> }}}
<eucode>
> -- STRIPCMT.E  Sat, Aug  6 2005 12:29:12 am
> 
> include misc.e  -- FOR DEMO ONLY
> 
> with trace
> trace(1)
> function strip_comment(sequence line, sequence arg)
>      sequence s  integer inQuote, pos, ch
>      s = line  inQuote = 0
>      -- replace all escaped backslashes with spaces
>      pos = match("\\\\", s)
>      while pos do
>          s[pos..pos + 1] = "  "
>          pos = match("\\\\", s)
>      end while
>      -- replace all escaped double quotes with spaces
>      pos = match("\\\"", s)
>      while pos do
>          s[pos..pos + 1] = "  "
>          pos = match("\\\"", s)
>      end while
>      -- replace remaining quoted strings with spaces
>      for i = 1 to length(s) do
>          ch = s[i]
>          if ch = '"' then
>              if inQuote then
>                  inQuote = 0
>              else
>                  inQuote = 1
>              end if
>          elsif inQuote then
>              s[i] = ' '
>          end if
>      end for
>      -- look for comment
>      --pos = match("--", s)
>      -- look for argument
>      pos = match(arg, s)
>      if pos then
>          return line[1..pos - 1]
>      else
>          return line
>      end if
> end function
> 
> sequence line
> 
> line = "result =  x(\"--\", \"\\\\--\", \"----\\\"\\\"\") -- real comment --"
> 
> pretty_print(1, line, {2})  puts(1, "\n\n")
> 
> pretty_print(1, strip_comment(line, "--"), {2})  puts(1, "\n\n")
> 
> line = "line=InnocentRtn(\"--In Eu, a comment begins with 2 hyphens\") --A 
> real comment"
> pretty_print(1, line, {2})  puts(1, "\n\n")
> pretty_print(1, strip_comment(line, "--"), {2})  puts(1, "\n\n")
> 
> 
> <\eucode>
> 
> I've written a few of these using more complex logic but it was hard.  This 
> was almost dashed off.  Of course it could be improved but it's fast enough 
> for stripping comments from EU code. I ran a large program through it which 
> worked fine afterwards.  I hope this is what you meant.
> 
>                  Bob

Hi Bob,

Thanks for this post.  Your instincts are in the right direction for sure. 
Alas, I have just posted the Puzzle Report and Awards which sort of ends the
puzzle, but that doesn't lessen the value of your post.  It may well give people
interesting ideas, or an itch to borrow your work.

Also, I am authorized to award you a silver star and two blue ones.  They are
virtual ones, so you will have to go out and buy these if you want people to be
able to actually see them.

--Quark

new topic     » goto parent     » topic index » view message » categorize

31. Re: A Puzzle in Eu -- what would you do?

At 11:27 PM 8/5/05 -0700, you wrote:

>
>
>posted by: DB James <larch at adelphia.net>
>
>Bob Elia wrote:
> >
><SNIP>
> > >--Quark
> >
> > }}}
<eucode>
> > -- STRIPCMT.E  Sat, Aug  6 2005 12:29:12 am
> >
> > include misc.e  -- FOR DEMO ONLY
> >
> > with trace
> > trace(1)
> > function strip_comment(sequence line, sequence arg)
> >      sequence s  integer inQuote, pos, ch
> >      s = line  inQuote = 0
> >      -- replace all escaped backslashes with spaces

<SNIP>

>Hi Bob,
>
>Thanks for this post.  Your instincts are in the right direction for 
>sure.  Alas, I have just posted the Puzzle Report and Awards which sort of 
>ends the puzzle, but that doesn't lessen the value of your post.  It may 
>well give people interesting ideas, or an itch to borrow your work.
>
>Also, I am authorized to award you a silver star and two blue ones.  They 
>are virtual ones, so you will have to go out and buy these if you want 
>people to be able to actually see them.
>
>--Quark

         I am most virtually honored.

                 Thanks,

                 Bob

new topic     » goto parent     » topic index » view message » categorize

Search



Quick Links

User menu

Not signed in.

Misc Menu