red / REP

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

WISH: Comma as an operator #149

Open hiiamboris opened 1 year ago

hiiamboris commented 1 year ago

This came from point! design discussion: can we on top of literal (1, 2, 3) syntax also support expressions, like (x, y, z), as in the original geometry point notation that we copied?

Currently point can only be constructed as make point2D! reduce [x y] or as-point2D x y (similarly for 3D). This sometimes becomes quite verbose, turning reader's attention on the means rather than on the goal (syntactic noise problem).

We could make comma an operator that can by default:

This will allow one to write expressions like (x, y, z) which will be lexed as paren! with 5 word!s: (x , y , z), comma being a word! that lexer must unstick from other tokens (x,y not a single word but three). Parentheses are not required of course: point: x,y,z would work the same, but in more complex expressions, like s + (x,y,z) / 2 they still make sense and add readability.

Consider: compose [offset: (0,0,0) size: (x,y,z)]. (0,0,0) is lexed as a point3D! literal value, but (x,y,z) as a a paren! which gets evaluated by compose itself.

Another usage for comma would be dialects, maybe to separate items in lists e.g. [1 2, 3 4, 5 6] or words in sentences lorem ipsum, dolor, sit amet.

Points against it are:

So the main question here is whether the readability benefit will overweight the risks of abuse, or vice versa.

hiiamboris commented 1 year ago

Example from my code:

word-drawn: compose/deep/only [
    translate (word-offset, 0)              ;-- move to the 2D point
    #debug paragraph [push [
        translate (0, row-y1-1D)
        fill-pen off pen magenta line-width 1
        box 0x0 (word-width-1D', row-height)
    ]]
    scale (word-scale) 1.0
    clip 0x0 (word-span, total-1D/y)
    translate (offset1, 0)                  ;-- account for word's offset within geom/size/x
    (copy spaces-drawn)
]

vs

word-drawn: compose/deep/only [
    translate (as-point2D word-offset 0)    ;-- move to the 2D point
    #debug paragraph [push [
        translate (as-point2D 0 row-y1-1D)
        fill-pen off pen magenta line-width 1
        box 0x0 (as-point2D word-width-1D' row-height)
    ]]
    scale (word-scale) 1.0
    clip 0x0 (as-point2D word-span total-1D/y)
    translate (as-point2D offset1 0)        ;-- account for word's offset within geom/size/x
    (copy spaces-drawn)
]