Comments (3)
Implementation plan:
- Write a special parser that works on identifiers. That could be a separate struct inside the existing parser module.
- Crucially, this part of the parser needs access to the existing set of units.
- This parser would take strings like
km
,kilometer
,mi
,cd
, or justkilo
(to supportkilo meter
) as input, and then- check if the full identifier corresponds to a known unit (e.g.
mi
for mile orcd
for candela). If that's the case, we simply emit anExpression::Identifier(…)
AST element. - if the identifier is not a known unit, we try to parse a SI or binary prefix. We first try the long versions (
kilo
,kibi
,milli
, etc.). If that fails, we try the short versions (k
,Ki
,m
…). The remaining part of the identifier is then treated as the unit identifier. If that unit exists in the dictionary, and if that unit is a 'normal' unit, we emit anExpression::BinaryOperator( BinaryOperator::Mul, Expression::Prefix(…), Expression::Identifier(remainder) )
- If it's not a normal unit (one which does not take an SI prefix), or if the unit name does not exist, we emit a simple
Expression::Identifier(…)
because that could still be a variable name.
- check if the full identifier corresponds to a known unit (e.g.
In addition, if the full identifier is a long name of a prefix, like kilo
, we could emit Expression::Prefix(Prefix::Decimal, 3)
.
Let's check if this handles known problematic cases.
- Let's say we have
mi
/mile
andmin
as known units. Each of them could be supported because the first check simply looks up the name in the dictionary. cd
could be parsed as candela (instead ofcenti day
) because that would be looked up first.ft
could be parsed asfoot
(instead offemto tonne
).
Potential problems:
- This turns our syntax into a context-dependent language
☹️ .ft
would either be a femto tonne. Or, if Imperial units are also defined.. it would be ft=foot. However, the situation is not worse than with global variables anywhere else. Just a bit more unexpected maybe. We could try to alleviate this by printing a warning/error: "ft
is ambiguous:foot
orfemto tonne
. Please use the long form. In order not to make this extremely annoying for everyone usingft=foot
orcd=candela
, we could somehow forbid usage offemto tonne
andcenti days
by explicitly only allowing positive prefixes fortonne
(kiloton, megaton, …) and disallowing them forday
completely? - Let's say someone defines a custom unit named
ns
. Then, nano seconds would not be available anymore as a unit.
from numbat.
Note: we can not delay this to compilation time because the type checker needs that information. it needs to know that kilometer
is of type Length
.
from numbat.
This has been solved in a different way. We introduced a new stage to the compiler ("Prefix Transformer").
from numbat.
Related Issues (20)
- Support for RFC 3339 in target time zone HOT 7
- Unexpected panic when using datetime formatter HOT 2
- Metric teaspoon and tablespoon missing
- log with arbitrary base
- Create data folder if it does not exist HOT 3
- logaritmic operations with units and decibel HOT 1
- 'human' doesn't work great for negative durations
- darcy as unit HOT 1
- arbitrary precision calculation HOT 5
- Functionality to easily copy output of last calculation to clipboard HOT 5
- Add semicolon as optional statement separator
- Add functionality to solve quadratic equation HOT 5
- LCM and GCD functions HOT 2
- Errors while developing aren't very human-readable HOT 3
- why is creating a function that has raising a power in it so complicated? HOT 2
- Allow for "forced let" HOT 1
- Random distribution sampling HOT 4
- Suggest correct casing if the entered unit doesn't exist HOT 3
- Add info for functions HOT 1
- Binary operations with callables
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from numbat.