Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WISH: Comma as an operator #149

Open
hiiamboris opened this issue Aug 12, 2023 · 1 comment
Open

WISH: Comma as an operator #149

hiiamboris opened this issue Aug 12, 2023 · 1 comment

Comments

@hiiamboris
Copy link

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:

  • accept two numbers (x1 + x2, y) and produce a point2D!
  • accept pair! or point2D! and a number ( (x1 + x2, y), z) <=> (x1 + x2, y, z) and produce a point3D!
  • accept triple! or point3D! and a number and produce a point4D! (4D is reserved for the future for now as we don't have enough space in the cell for it in our 32-bit runtime)

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:

  • comma doesn't look like something that evaluates, so it may make the code confusing to the reader
  • point! syntax already collides with paren! syntax, and (x,y,z) expression vs (1,2,3) literal value only adds to the confusion

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

@hiiamboris
Copy link
Author

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)
]

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant