Skip to content

Commit

Permalink
doc: improvements
Browse files Browse the repository at this point in the history
- improve number introduction
- add missing hexadecimal  double
  • Loading branch information
peter-jerry-ye committed Dec 10, 2024
1 parent 8ca28ab commit ae9f0d5
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 14 deletions.
6 changes: 5 additions & 1 deletion next/_ext/lexer.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,11 @@ class MoonBitLexer(RegexLexer):
(r"\$\|", token.String, "string.multiline"),
(r"0(b|B)[01]+", token.Number.Bin),
(r"0(o|O)[0-7]+", token.Number.Oct),
(r"0(x|X)[0-9a-fA-F]+", token.Number.Hex),
(r"0(x|X)[0-9a-fA-F][0-9a-fA-F_]*\.[0-9a-fA-F][0-9a-fA-F_]*(P|p)(\+|\-)?[0-9][0-9]*", token.Number.Float),
(r"0(x|X)[0-9a-fA-F][0-9a-fA-F_]*\.?(P|p)(\+|\-)?[0-9][0-9]*", token.Number.Float),
(r"0(x|X)[0-9a-fA-F][0-9a-fA-F_]*\.[0-9a-fA-F][0-9a-fA-F_]*", token.Number.Float),
(r"0(x|X)[0-9a-fA-F][0-9a-fA-F_]*\.", token.Number.Float),
(r"0(x|X)[0-9a-fA-F][0-9a-fA-F_]*", token.Number.Hex),
(r"\d(_|\d)*U?L", token.Number.Integer.Long),
(r"\d(_|\d)*U?", token.Number.Integer),
(r"\d+(.\d+)?", token.Number),
Expand Down
28 changes: 24 additions & 4 deletions next/language/fundamentals.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,9 @@ MoonBit also supports numeric literals, including decimal, binary, octal, and he

To improve readability, you may place underscores in the middle of numeric literals such as `1_000_000`. Note that underscores can be placed anywhere within a number, not just every three digits.

- There is nothing surprising about decimal numbers.
- Decimal numbers can have underscore between the numbers.

By default, an int literal is signed 32-bit number. For unsigned numbers, a postfix `U` is needed; for 64-bit numbers, a postfix `L` is needed.

```{literalinclude} /sources/language/src/builtin/top.mbt
:language: moonbit
Expand Down Expand Up @@ -70,9 +72,27 @@ To improve readability, you may place underscores in the middle of numeric liter
:end-before: end number 4
```

#### Overloaded int literal
- A floating-point number literal is 64-bit floating-point number. To define a float, type annotation is needed.

```{literalinclude} /sources/language/src/builtin/top.mbt
:language: moonbit
:dedent:
:start-after: start number 6
:end-before: end number 6
```

A 64-bit floating-point number can also be defined using hexadecimal format:

```{literalinclude} /sources/language/src/builtin/top.mbt
:language: moonbit
:dedent:
:start-after: start number 7
:end-before: end number 7
```

#### Overloaded literal

When the expected type is known, MoonBit can automatically overload integer literal, and there is no need to specify the type of number via letter postfix:
When the expected type is known, MoonBit can automatically overload literal, and there is no need to specify the type of number via letter postfix:

```{literalinclude} /sources/language/src/builtin/top.mbt
:language: moonbit
Expand Down Expand Up @@ -130,7 +150,7 @@ Multi-line strings do not support interpolation by default, but you can enable i

### Char

`Char` is an integer representing a Unicode code point.
`Char` represents a Unicode code point.

```{literalinclude} /sources/language/src/builtin/top.mbt
:language: moonbit
Expand Down
3 changes: 2 additions & 1 deletion next/language/introduction.md
Original file line number Diff line number Diff line change
Expand Up @@ -104,4 +104,5 @@ There's also a top-level structure called `test` block. A `test` block defines i
:end-before: end test 1
```

The following contents will use `test` block and `main` function to demonstrate the execution result.
The following contents will use `test` block and `main` function to demonstrate the execution result,
and we assume that all the `test` blocks pass unless stated otherwise.
23 changes: 15 additions & 8 deletions next/sources/language/src/builtin/top.mbt
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,10 @@ let boolean : Unit = {
let number : Unit = {
// start number 1
let a = 1234
let b = 1_000_000 + a
// UInt : 0_U
let unsigned_num = 4_294_967_295U
// Int64 : 0_L
let large_num = 9_223_372_036_854_775_807L
// UInt64 : 0_UL
let unsigned_large_num = 18_446_744_073_709_551_615UL
let b : Int = 1_000_000 + a
let unsigned_num : UInt = 4_294_967_295U
let large_num : Int64 = 9_223_372_036_854_775_807L
let unsigned_large_num : UInt64 = 18_446_744_073_709_551_615UL
// end number 1

// start number 2
Expand All @@ -33,9 +30,19 @@ let number : Unit = {

// start number 4
let hex = 0XA
let another_hex = 0xA
let another_hex = 0xA_B_C
// end number 4

// start number 6
let double = 3.14 // Double
let float : Float = 3.14
let float2 = (3.14 : Float)
// end number 6

// start number 7
let hex_double = 0x1.2P3 // (1.0 + 2 / 16) * 2^(+3) == 9
// end number 7

// start number 5
let int : Int = 42
let uint : UInt = 42
Expand Down

0 comments on commit ae9f0d5

Please sign in to comment.