Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # Running the example program
- Compilation, e.g.: clang++-7 -std=c++17 main.cpp
- Running: ./a.out "7 l / 100 km" (mind the double quotes and spaces between tokens).
- # Unit Conversion Scripting Language (UCSL)
- A language for unit conversions. UCSL is able to find a chain of implicit conversions needed to end up with desired result unit.
- # Hello world
- print 1 Hello / 1 World!
- # Statements
- ## constant
- Defines a constant that is used for conversion. E.g.
- ```
- constant 1000 m <-> 1 km
- constant 0.01 m <-> 1 cm
- ```
- To properly categorize units, make sure that the units from one category are always 'chained'. E.g. here the units are not chained and two categories are created, instead of just one.
- ```
- constant 1 cm <-> 10 mm
- constant 1 km <-> 1000 m
- constant 1 m <-> 100 cm
- ```
- There is no connection between defining cm <-> mm and km <-> m. To chain the units properly, rearrange constants in such way:
- ```
- constant 1 cm <-> 10 mm
- constant 1 m <-> 100 cm
- constant 1 km <-> 1000 m
- ```
- Chaining units from one category can be interrupted. E.g. this script is valid:
- ```
- constant 1 cm <-> 10 mm
- constant 1000 g <-> 1 kg
- constant 1 m <-> 100 cm
- print 1 g / 42.42 mm to X g / 20 m
- ```
- ## value
- Defines a named value with a result of value expression. Later on, the name can be referred.
- ```
- value value_name 42 km / 1 h
- value another_value value_name
- ```
- ## print
- Prints result of a value expression to stdout.
- ```
- print 1 m / 2 s
- ```
- Prints: 1 m / 2 s
- ## to operator
- Converts a value in one unit to a value in another unit. Conversion has to be known based on constants.
- ```
- constant 1.605 km <-> 1 mi
- constant 3.785 l <-> 1 gal
- value l_per_100_km 7 l / 100 km
- value conversion_result l_per_100_km to X mi / 1 gal
- print conversion_result
- ```
- This script prints `33.605 mi / 1 gal` to stdout.
- The X indicates that the mi value can change. That's why the result is some amount of miles per one gallon. X can appear in the denominator as well. X must apprear exactly once in the desired unit, otherwise the behavior is undefined.
- to operator automatically detects conversion chain. Here, it detects that it needs to convert m to km and then km to mi to meet desired unit.
- ```
- constant 1.605 km <-> 1 mi
- constant 1000 m <-> 1 km
- constant 3.785 l <-> 1 gal
- value l_per_m 0.00007 l / 1 m
- value conversion_result l_per_100_km to X mi / 1 gal
- print conversion_result
- ```
Add Comment
Please, Sign In to add comment