Browse Source

Change postfix apply to |>

David Peter 1 year ago
parent
commit
bfa32eac73

+ 3 - 3
book/src/example-numbat_syntax.md

@@ -53,10 +53,10 @@ mod(17, 4)        # Modulo
 3 in to cm        # Unit conversion with the 'to' keyword
 
 cos(pi/3 + pi)    # Call mathematical functions
-pi/3 + pi // cos  # Same, 'arg // f' is equivalent to 'f(arg)'
-                  # The '//' operator has the lowest precedence
+pi/3 + pi |> cos  # Same, 'arg |> f' is equivalent to 'f(arg)'
+                  # The '|>' operator has the lowest precedence
                   # which makes it very useful for interactive
-                  # terminals (press up-arrow, and add '// f')
+                  # terminals (press up-arrow, and add '|> f')
 
 # 4. Constants
 

+ 2 - 2
book/src/example-population_growth.md

@@ -1,7 +1,7 @@
 <!-- This file is autogenerated! Do not modify it -->
 
 # Population growth
-<a href="https://numbat.dev/?q=%23+Exponential+model+for+population+growth%0A%0Alet+initial_population+%3D+50_000+people%0Alet+growth_rate+%3D+2%25+per+year%0A%0Afn+predict_population%28t%3A+Time%29+%3D%0A++++initial_population+%C3%97+e%5E%28growth_rate%C2%B7t%29+%2F%2F+round%0A%0Aprint%28%22Population+in++20+years%3A+%7Bpredict_population%2820+years%29%7D%22%29%0Aprint%28%22Population+in+100+years%3A+%7Bpredict_population%281+century%29%7D%22%29%0A"><i class="fa fa-play"></i> Run this example</a>
+<a href="https://numbat.dev/?q=%23+Exponential+model+for+population+growth%0A%0Alet+initial_population+%3D+50_000+people%0Alet+growth_rate+%3D+2%25+per+year%0A%0Afn+predict_population%28t%3A+Time%29+%3D%0A++++initial_population+%C3%97+e%5E%28growth_rate%C2%B7t%29+%7C%3E+round%0A%0Aprint%28%22Population+in++20+years%3A+%7Bpredict_population%2820+years%29%7D%22%29%0Aprint%28%22Population+in+100+years%3A+%7Bpredict_population%281+century%29%7D%22%29%0A"><i class="fa fa-play"></i> Run this example</a>
 
 ``` numbat
 # Exponential model for population growth
@@ -10,7 +10,7 @@ let initial_population = 50_000 people
 let growth_rate = 2% per year
 
 fn predict_population(t: Time) =
-    initial_population × e^(growth_rate·t) // round
+    initial_population × e^(growth_rate·t) |> round
 
 print("Population in  20 years: {predict_population(20 years)}")
 print("Population in 100 years: {predict_population(1 century)}")

+ 24 - 24
book/src/example-xkcd_2585.md

@@ -1,7 +1,7 @@
 <!-- This file is autogenerated! Do not modify it -->
 
 # XKCD 2585
-<a href="https://numbat.dev/?q=%23+Rounding%0A%23%0A%23+https%3A%2F%2Fxkcd.com%2F2585%2F%0A%0A17+mph%0A%0Aans+-%3E+meters%2Fsec++++%2F%2F+round%0Aans+-%3E+knots+++++++++%2F%2F+round%0Aans+-%3E+fathoms%2Fsec+++%2F%2F+round%0Aans+-%3E+furlongs%2Fmin++%2F%2F+round%0Aans+-%3E+fathoms%2Fsec+++%2F%2F+round%0Aans+-%3E+kph+++++++++++%2F%2F+round%0Aans+-%3E+knots+++++++++%2F%2F+round%0Aans+-%3E+kph+++++++++++%2F%2F+round%0Aans+-%3E+furlongs%2Fhour+%2F%2F+round%0Aans+-%3E+mph+++++++++++%2F%2F+round%0Aans+-%3E+m%2Fs+++++++++++%2F%2F+round%0Aans+-%3E+furlongs%2Fmin++%2F%2F+round%0Aans+-%3E+yards%2Fsec+++++%2F%2F+round%0Aans+-%3E+fathoms%2Fsec+++%2F%2F+round%0Aans+-%3E+m%2Fs+++++++++++%2F%2F+round%0Aans+-%3E+mph+++++++++++%2F%2F+round%0Aans+-%3E+furlongs%2Fmin++%2F%2F+round%0Aans+-%3E+knots+++++++++%2F%2F+round%0Aans+-%3E+yards%2Fsec+++++%2F%2F+round%0Aans+-%3E+fathoms%2Fsec+++%2F%2F+round%0Aans+-%3E+knots+++++++++%2F%2F+round%0Aans+-%3E+furlongs%2Fmin++%2F%2F+round%0Aans+-%3E+mph+++++++++++%2F%2F+round%0A%0Aprint%28%22I+can+ride+my+bike+at+%7Bans%7D.%22%29%0Aprint%28%22If+you+round.%22%29%0A"><i class="fa fa-play"></i> Run this example</a>
+<a href="https://numbat.dev/?q=%23+Rounding%0A%23%0A%23+https%3A%2F%2Fxkcd.com%2F2585%2F%0A%0A17+mph%0A%0Aans+-%3E+meters%2Fsec++++%7C%3E+round%0Aans+-%3E+knots+++++++++%7C%3E+round%0Aans+-%3E+fathoms%2Fsec+++%7C%3E+round%0Aans+-%3E+furlongs%2Fmin++%7C%3E+round%0Aans+-%3E+fathoms%2Fsec+++%7C%3E+round%0Aans+-%3E+kph+++++++++++%7C%3E+round%0Aans+-%3E+knots+++++++++%7C%3E+round%0Aans+-%3E+kph+++++++++++%7C%3E+round%0Aans+-%3E+furlongs%2Fhour+%7C%3E+round%0Aans+-%3E+mph+++++++++++%7C%3E+round%0Aans+-%3E+m%2Fs+++++++++++%7C%3E+round%0Aans+-%3E+furlongs%2Fmin++%7C%3E+round%0Aans+-%3E+yards%2Fsec+++++%7C%3E+round%0Aans+-%3E+fathoms%2Fsec+++%7C%3E+round%0Aans+-%3E+m%2Fs+++++++++++%7C%3E+round%0Aans+-%3E+mph+++++++++++%7C%3E+round%0Aans+-%3E+furlongs%2Fmin++%7C%3E+round%0Aans+-%3E+knots+++++++++%7C%3E+round%0Aans+-%3E+yards%2Fsec+++++%7C%3E+round%0Aans+-%3E+fathoms%2Fsec+++%7C%3E+round%0Aans+-%3E+knots+++++++++%7C%3E+round%0Aans+-%3E+furlongs%2Fmin++%7C%3E+round%0Aans+-%3E+mph+++++++++++%7C%3E+round%0A%0Aprint%28%22I+can+ride+my+bike+at+%7Bans%7D.%22%29%0Aprint%28%22If+you+round.%22%29%0A"><i class="fa fa-play"></i> Run this example</a>
 
 ``` numbat
 # Rounding
@@ -10,29 +10,29 @@
 
 17 mph
 
-ans -> meters/sec    // round
-ans -> knots         // round
-ans -> fathoms/sec   // round
-ans -> furlongs/min  // round
-ans -> fathoms/sec   // round
-ans -> kph           // round
-ans -> knots         // round
-ans -> kph           // round
-ans -> furlongs/hour // round
-ans -> mph           // round
-ans -> m/s           // round
-ans -> furlongs/min  // round
-ans -> yards/sec     // round
-ans -> fathoms/sec   // round
-ans -> m/s           // round
-ans -> mph           // round
-ans -> furlongs/min  // round
-ans -> knots         // round
-ans -> yards/sec     // round
-ans -> fathoms/sec   // round
-ans -> knots         // round
-ans -> furlongs/min  // round
-ans -> mph           // round
+ans -> meters/sec    |> round
+ans -> knots         |> round
+ans -> fathoms/sec   |> round
+ans -> furlongs/min  |> round
+ans -> fathoms/sec   |> round
+ans -> kph           |> round
+ans -> knots         |> round
+ans -> kph           |> round
+ans -> furlongs/hour |> round
+ans -> mph           |> round
+ans -> m/s           |> round
+ans -> furlongs/min  |> round
+ans -> yards/sec     |> round
+ans -> fathoms/sec   |> round
+ans -> m/s           |> round
+ans -> mph           |> round
+ans -> furlongs/min  |> round
+ans -> knots         |> round
+ans -> yards/sec     |> round
+ans -> fathoms/sec   |> round
+ans -> knots         |> round
+ans -> furlongs/min  |> round
+ans -> mph           |> round
 
 print("I can ride my bike at {ans}.")
 print("If you round.")

+ 3 - 3
book/src/example-xkcd_2812.md

@@ -1,7 +1,7 @@
 <!-- This file is autogenerated! Do not modify it -->
 
 # XKCD 2812
-<a href="https://numbat.dev/?q=%23+Solar+panel+placement%0A%23%0A%23+Solar+energy+tip%3A+To+maximize+sun+exposure%2C+always%0A%23+orient+your+panels+downward+and+install+them+on+the%0A%23+surface+of+the+sun.%0A%23%0A%23+https%3A%2F%2Fxkcd.com%2F2812%2F%0A%23%0A%23+%5B1%5D+https%3A%2F%2Fen.wikipedia.org%2Fwiki%2FSolar_luminosity%0A%23+%5B2%5D+https%3A%2F%2Fen.wikipedia.org%2Fwiki%2FSun%0A%0Aunit+%24%3A+Money%0A%0Alet+net_metering_rate+%3D+%24+0.20+%2F+kWh%0Alet+panel_area+%3D+1+m%C2%B2%0Alet+panel_efficiency+%3D+20+%25%0A%0Afn+savings%28i%3A+Irradiance%29+-%3E+Money+%2F+Time+%3D%0A++++net_metering_rate+%C3%97+i+%C3%97+panel_area+%C3%97+panel_efficiency+-%3E+%24%2Fyear%0A%0Aprint%28%22Option+A%3A+On+the+roof%2C+south+facing%22%29%0A%0Alet+savings_a+%3D+savings%284+kWh%2Fm%C2%B2%2Fday%29%0A%0Aprint%28savings_a+%2F%2F+round%29%0A%0Aprint%28%29%0Aprint%28%22Option+B%3A+On+the+sun%2C+downward+facing%22%29%0A%0Adimension+Luminosity+%3D+Power%0A%0Alet+sun_luminosity%3A+Luminosity+%3D+3.828e26+W++%23+%5B1%5D%0Alet+sun_area%3A+Area+%3D+6.09e12+km%5E2++++++++++++%23+%5B2%5D%0A%0Alet+savings_b+%3D+savings%28sun_luminosity+%2F+sun_area%29%0A%0Aprint%28savings_b+%2F%2F+round%29%0A"><i class="fa fa-play"></i> Run this example</a>
+<a href="https://numbat.dev/?q=%23+Solar+panel+placement%0A%23%0A%23+Solar+energy+tip%3A+To+maximize+sun+exposure%2C+always%0A%23+orient+your+panels+downward+and+install+them+on+the%0A%23+surface+of+the+sun.%0A%23%0A%23+https%3A%2F%2Fxkcd.com%2F2812%2F%0A%23%0A%23+%5B1%5D+https%3A%2F%2Fen.wikipedia.org%2Fwiki%2FSolar_luminosity%0A%23+%5B2%5D+https%3A%2F%2Fen.wikipedia.org%2Fwiki%2FSun%0A%0Aunit+%24%3A+Money%0A%0Alet+net_metering_rate+%3D+%24+0.20+%2F+kWh%0Alet+panel_area+%3D+1+m%C2%B2%0Alet+panel_efficiency+%3D+20+%25%0A%0Afn+savings%28i%3A+Irradiance%29+-%3E+Money+%2F+Time+%3D%0A++++net_metering_rate+%C3%97+i+%C3%97+panel_area+%C3%97+panel_efficiency+-%3E+%24%2Fyear%0A%0Aprint%28%22Option+A%3A+On+the+roof%2C+south+facing%22%29%0A%0Alet+savings_a+%3D+savings%284+kWh%2Fm%C2%B2%2Fday%29%0A%0Aprint%28savings_a+%7C%3E+round%29%0A%0Aprint%28%29%0Aprint%28%22Option+B%3A+On+the+sun%2C+downward+facing%22%29%0A%0Adimension+Luminosity+%3D+Power%0A%0Alet+sun_luminosity%3A+Luminosity+%3D+3.828e26+W++%23+%5B1%5D%0Alet+sun_area%3A+Area+%3D+6.09e12+km%5E2++++++++++++%23+%5B2%5D%0A%0Alet+savings_b+%3D+savings%28sun_luminosity+%2F+sun_area%29%0A%0Aprint%28savings_b+%7C%3E+round%29%0A"><i class="fa fa-play"></i> Run this example</a>
 
 ``` numbat
 # Solar panel placement
@@ -28,7 +28,7 @@ print("Option A: On the roof, south facing")
 
 let savings_a = savings(4 kWh/m²/day)
 
-print(savings_a // round)
+print(savings_a |> round)
 
 print()
 print("Option B: On the sun, downward facing")
@@ -40,5 +40,5 @@ let sun_area: Area = 6.09e12 km^2            # [2]
 
 let savings_b = savings(sun_luminosity / sun_area)
 
-print(savings_b // round)
+print(savings_b |> round)
 ```

+ 2 - 2
book/src/number-notation.md

@@ -24,8 +24,8 @@ Numbers in Numbat can be written in the following forms:
 ## Convert numbers to other bases
 
 You can use the `bin`, `oct`, `dec` and `hex` functions to convert numbers to binary, octal, decimal and hexadecimal bases,
-respectively. You can call those using `hex(2^16 - 1)`, or `2^16 - 1 // hex`, but they are also available as targets of the
-conversion operator `->`/`to`, so you can write expressions like:
+respectively. You can call those using `hex(2^16 - 1)`, but they are also available as targets of the conversion operator `->`/`to`,
+so you can write expressions like:
 
 Examples:
 ```nbt

+ 1 - 1
book/src/operations.md

@@ -20,7 +20,7 @@ Numbat operators and other language constructs, ordered by precedence form *high
 | logical 'or'              | <code>x &#124;&#124; y</code>        |
 | unit conversion           | `x -> y`, `x → y`, `x ➞ y`, `x to y` |
 | conditionals              | `if x then y else z`                 |
-| reverse function call     | `x // f`                             |
+| reverse function call     | `x |> f`                             |
 
 Note that *implicit* multiplication has a higher precedence than division, i.e. `50 cm / 2 m` will be parsed as `50 cm / (2 m)`.
 

+ 1 - 1
book/src/procedures.md

@@ -29,7 +29,7 @@ print("Speed of the bicycle: {speed} ({speed -> mph})")
 Format specifiers are also supported in interpolations. For instance:
 
 ```nbt
-print("{pi:0.2f}") // Prints "3.14"
+print("{pi:0.2f}")  # Prints "3.14"
 ```
 
 For more information on supported format specifiers, please see

+ 3 - 3
examples/numbat_syntax.nbt

@@ -48,10 +48,10 @@ mod(17, 4)        # Modulo
 3 in to cm        # Unit conversion with the 'to' keyword
 
 cos(pi/3 + pi)    # Call mathematical functions
-pi/3 + pi // cos  # Same, 'arg // f' is equivalent to 'f(arg)'
-                  # The '//' operator has the lowest precedence
+pi/3 + pi |> cos  # Same, 'arg |> f' is equivalent to 'f(arg)'
+                  # The '|>' operator has the lowest precedence
                   # which makes it very useful for interactive
-                  # terminals (press up-arrow, and add '// f')
+                  # terminals (press up-arrow, and add '|> f')
 
 # 4. Constants
 

+ 1 - 1
examples/parse_error/expected_identifier_in_postfix_apply.nbt

@@ -1 +1 @@
-100 // 20
+100 |> 20

+ 1 - 1
examples/population_growth.nbt

@@ -4,7 +4,7 @@ let initial_population = 50_000 people
 let growth_rate = 2% per year
 
 fn predict_population(t: Time) =
-    initial_population × e^(growth_rate·t) // round
+    initial_population × e^(growth_rate·t) |> round
 
 print("Population in  20 years: {predict_population(20 years)}")
 print("Population in 100 years: {predict_population(1 century)}")

+ 23 - 23
examples/xkcd_2585.nbt

@@ -4,29 +4,29 @@
 
 17 mph
 
-ans -> meters/sec    // round
-ans -> knots         // round
-ans -> fathoms/sec   // round
-ans -> furlongs/min  // round
-ans -> fathoms/sec   // round
-ans -> kph           // round
-ans -> knots         // round
-ans -> kph           // round
-ans -> furlongs/hour // round
-ans -> mph           // round
-ans -> m/s           // round
-ans -> furlongs/min  // round
-ans -> yards/sec     // round
-ans -> fathoms/sec   // round
-ans -> m/s           // round
-ans -> mph           // round
-ans -> furlongs/min  // round
-ans -> knots         // round
-ans -> yards/sec     // round
-ans -> fathoms/sec   // round
-ans -> knots         // round
-ans -> furlongs/min  // round
-ans -> mph           // round
+ans -> meters/sec    |> round
+ans -> knots         |> round
+ans -> fathoms/sec   |> round
+ans -> furlongs/min  |> round
+ans -> fathoms/sec   |> round
+ans -> kph           |> round
+ans -> knots         |> round
+ans -> kph           |> round
+ans -> furlongs/hour |> round
+ans -> mph           |> round
+ans -> m/s           |> round
+ans -> furlongs/min  |> round
+ans -> yards/sec     |> round
+ans -> fathoms/sec   |> round
+ans -> m/s           |> round
+ans -> mph           |> round
+ans -> furlongs/min  |> round
+ans -> knots         |> round
+ans -> yards/sec     |> round
+ans -> fathoms/sec   |> round
+ans -> knots         |> round
+ans -> furlongs/min  |> round
+ans -> mph           |> round
 
 print("I can ride my bike at {ans}.")
 print("If you round.")

+ 2 - 2
examples/xkcd_2812.nbt

@@ -22,7 +22,7 @@ print("Option A: On the roof, south facing")
 
 let savings_a = savings(4 kWh/m²/day)
 
-print(savings_a // round)
+print(savings_a |> round)
 assert_eq(savings_a, 58 $/year, 1 $/year)
 
 print()
@@ -35,5 +35,5 @@ let sun_area: Area = 6.09e12 km^2            # [2]
 
 let savings_b = savings(sun_luminosity / sun_area)
 
-print(savings_b // round)
+print(savings_b |> round)
 assert_eq(savings_b, 22 million $/year, 1 million $/year)

+ 1 - 1
numbat-cli/src/completer.rs

@@ -116,7 +116,7 @@ impl Completer for NumbatCompleter {
 
         // don't add an opening paren if we're completing after a reverse function call
         // or when completing conversion functions
-        let add_paren = !["//", "->", "→", "➞", "to"]
+        let add_paren = !["|>", "->", "→", "➞", "to"]
             .iter()
             .any(|&s| line[..pos].contains(s));
 

+ 7 - 7
numbat/src/parser.rs

@@ -24,7 +24,7 @@
 //! dim_primary     ::=   identifier | "1" | "(" dimension_expr ")"
 //!
 //! expression      ::=   postfix_apply
-//! postfix_apply   ::=   condition ( "//" identifier ) *
+//! postfix_apply   ::=   condition ( "|>" identifier ) *
 //! condition       ::=   ( "if" conversion "then" condition "else" condition ) | conversion
 //! conversion      ::=   logical_or ( ( "→" | "->" | "to" ) logical_or ) *
 //! logical_or      ::=   logical_and ( "||" logical_and ) *
@@ -108,7 +108,7 @@ pub enum ParseErrorKind {
     #[error("Expected identifier")]
     ExpectedIdentifier,
 
-    #[error("Expected identifier or function call after postfix apply (`//`)")]
+    #[error("Expected identifier or function call after postfix apply (`|>`)")]
     ExpectedIdentifierOrCallAfterPostfixApply,
 
     #[error("Expected dimension identifier, '1', or opening parenthesis")]
@@ -2743,7 +2743,7 @@ mod tests {
     #[test]
     fn postfix_apply() {
         parse_as_expression(
-            &["1 + 1 // foo"],
+            &["1 + 1 |> foo"],
             Expression::FunctionCall(
                 Span::dummy(),
                 Span::dummy(),
@@ -2752,7 +2752,7 @@ mod tests {
             ),
         );
         parse_as_expression(
-            &["1 + 1 // kefir(2)"],
+            &["1 + 1 |> kefir(2)"],
             Expression::FunctionCall(
                 Span::dummy(),
                 Span::dummy(),
@@ -2761,12 +2761,12 @@ mod tests {
             ),
         );
 
-        should_fail_with(&["1 // print()"], ParseErrorKind::InlineProcedureUsage);
+        should_fail_with(&["1 |> print()"], ParseErrorKind::InlineProcedureUsage);
 
-        should_fail_with(&["1 // +"], ParseErrorKind::ExpectedPrimary);
+        should_fail_with(&["1 |> +"], ParseErrorKind::ExpectedPrimary);
 
         should_fail_with(
-            &["1 // 2", "1 // 1 +"],
+            &["1 |> 2", "1 |> 1 +"],
             ParseErrorKind::ExpectedIdentifierOrCallAfterPostfixApply,
         );
     }

+ 1 - 1
numbat/src/tokenizer.rs

@@ -530,10 +530,10 @@ impl Tokenizer {
             '\n' => TokenKind::Newline,
             '&' if self.match_char('&') => TokenKind::LogicalAnd,
             '|' if self.match_char('|') => TokenKind::LogicalOr,
+            '|' if self.match_char('>') => TokenKind::PostfixApply,
             '*' if self.match_char('*') => TokenKind::Power,
             '+' => TokenKind::Plus,
             '*' | '·' | '×' => TokenKind::Multiply,
-            '/' if self.match_char('/') => TokenKind::PostfixApply,
             '/' => TokenKind::Divide,
             '÷' => TokenKind::Divide,
             '^' => TokenKind::Power,