red / REP

Red Enhancement Process
BSD 3-Clause "New" or "Revised" License
11 stars 4 forks source link

WISH: `lit word!` Parse rule to literally match data by reference #142

Open hiiamboris opened 1 year ago

hiiamboris commented 1 year ago

Rationale - untangle cases where excess of smarts requires ugly workarounds:

;) trying to match integer found in first block with integer in another block:
parse [3] [set n integer!]
parse [3] [set n2 integer! if (n = n2)]    ;) parse-only variant
parse [3] compose [quote (n)]              ;) parse hacked with composition

;) ditto for words
parse [abc] [set word word!]
parse [abc] compose [quote (word)]

;) trying to figure out if all words in the block are the same:
parse [a 1 2 a 3]   [to set w word! (w: compose [quote (w)]) any [w | not word! skip]]   ;) == true
parse [a 1 2 a 3 b] [to set w word! (w: compose [quote (w)]) any [w | not word! skip]]   ;) == false

How it could have been written:

parse [3] [set n integer!]
parse [3] [lit n]

parse [abc] [set word word!]
parse [abc] [lit word]

parse [a 1 2 a 3] [to set w word! any [lit w | not word! skip]]

Proposed semantics:

greggirwin commented 1 year ago

Nice idea.

Naming is tricky here. I've suggested lit as a new name for quote in the past, because quote is simply a carryover name from Lisp, and lit (while not a full word), matches how we think of "literal" in Red. What you're talking about is not a literal value, but a reference that also treats the value literally. That matches parse's implicit composition, but might be confusing.

If get-words didn't have such a different meaning in parse. We might use :quote as a keyword.