What are some syntaxes programming¹ languages have used for units?
I have a human-editable format where a human writes numbers, but might want to specify "3" vs "3 hz" vs "3 samples". I want units to be string names, however
- Allowing the number to be separated from the unit by a space makes my parser hard
- Forcing them together, like 3hz, creates other problems, like hex support
¹ Would also be happy for syntaxes "not normatively programming" languages have used, like spreadsheet programs
@mcc Would a DSL be helpful here? It seems like the space between a number and a unit would fit, but i know too little about your other requirements.
@admitsWrongIfProven well I'm writing a DSL lol
@mcc My knowledge is very, very dated (like 10 years ago), but scala worked pretty nicely back then. Ofc the fan club can be a bit unpleasant.
@mcc Ah, just now getting to your previous toots... masto is a bit problematic here, scroll on and loose the original toot, or answer and possibly miss context. Sorry for the chaotic answer ^^
@admitsWrongIfProven if you want the full context, which there's a lot of but no reason you'd have seen it
https://mastodon.social/@mcc/113556420380442813 and preceding posts and preceding linked posts… you don't have to read this, there's a lot :)
An example of a program in the current language is
(P t--3 0 p8x p13x p0x p8x p13x)
(R t--2 0 t--3 p0 x p8 x p13 x)
R P -2 R P +4 R P -2
So as you can see there is a high priority on terseness. But in some unusual circumstances, I might want to get a little more verbose.
@mcc Hmm, sounds like your actual requirement demands more than one letter. Ofc you could use pPqQ... to give p multiple unit types, but i'd say that won't be readable. Is there anything bad about "<p/t><unittype1/2/3>" as a two letter abbreviation? Compact is good, but if you'd loose necessary information not possible, right?
@mcc Ah, i think i'm starting to get it...
p -> has a default meaning
p_<x> -> the <x> is an unusual unit
Squashing those together without "_" has problems, so the "_" makes sure you don't have problems with spaces as seperators but parse the whole expression at once.
Hmm, i guess you already solved it ^^
I might not have contributed, but it was interesting :-)
@admitsWrongIfProven yeah, i already had a pretty clear idea of how *I* was going to proceed when I asked the question :)
but what I was curious about was how *other* languages had solved this, to give me ideas on "probably I put a symbol to indicate a unit— has anyone done this before, what symbol?"
@admitsWrongIfProven so my thinking is that
- normally each letter has a meaning by itself, so multi-letter symbols are unusable
- but if i put a symbol in between, such as !, >, :, /, or _, then the parser can be aware that everything after the "unit next" symbol and before the space is a unit, and therefore should be treated as multi letter