mirror of
https://github.com/carbon-language/carbon-lang.git
synced 2026-09-24 22:02:23 +01:00
Edit numeric literal design for the literal type proposal and add reference where #2015 was mentioned. Closes #2159 Co-authored-by: Richard Smith <richard@metafoo.co.uk> Co-authored-by: josh11b <josh11b@users.noreply.github.com>
This commit is contained in:
co-authored by
Richard Smith
josh11b
parent
d96ede7144
commit
dd2f7d732c
@@ -9,8 +9,6 @@ SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
||||
> **STATUS:** Up-to-date on 09-Aug-2022, including proposals up through
|
||||
> [#1327](https://github.com/carbon-language/carbon-lang/pull/1327).
|
||||
|
||||
> FIXME: add #2015
|
||||
|
||||
<!-- toc -->
|
||||
|
||||
## Table of contents
|
||||
@@ -469,6 +467,7 @@ may be limited to integers of at most 128 bits due to LLVM limitations.
|
||||
|
||||
> References:
|
||||
>
|
||||
> - [Numeric type literal expressions](expressions/literals.md#numeric-type-literals)
|
||||
> - Question-for-leads issue
|
||||
> [#543: pick names for fixed-size integer types](https://github.com/carbon-language/carbon-lang/issues/543)
|
||||
> - Question-for-leads issue
|
||||
@@ -534,6 +533,7 @@ number.
|
||||
|
||||
> References:
|
||||
>
|
||||
> - [Numeric type literal expressions](expressions/literals.md#numeric-type-literals)
|
||||
> - Question-for-leads issue
|
||||
> [#543: pick names for fixed-size integer types](https://github.com/carbon-language/carbon-lang/issues/543)
|
||||
> - Question-for-leads issue
|
||||
|
||||
@@ -18,6 +18,7 @@ SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
||||
- [Operators](#operators)
|
||||
- [Conversions and casts](#conversions-and-casts)
|
||||
- [`if` expressions](#if-expressions)
|
||||
- [Numeric type literal expressions](#numeric-type-literal-expressions)
|
||||
- [Alternatives considered](#alternatives-considered)
|
||||
- [References](#references)
|
||||
|
||||
@@ -298,6 +299,16 @@ fn Run(args: Span(StringView)) {
|
||||
|
||||
`if` expressions are analogous to `?:` ternary expressions in C and C++.
|
||||
|
||||
## Numeric type literal expressions
|
||||
|
||||
Carbon's syntax provides a simple way to represent different types of integers
|
||||
and floating-point numbers. Each type is identified with a keyword-like syntax,
|
||||
prefixed with either `i`, `u`, or `f` followed by a multiple of 8, representing
|
||||
the size in bits of the data type.
|
||||
|
||||
These are referred to as
|
||||
[numeric type literals](literals.md#numeric-type-literals).
|
||||
|
||||
## Alternatives considered
|
||||
|
||||
Other expression documents will list more alternatives; this lists alternatives
|
||||
|
||||
@@ -0,0 +1,72 @@
|
||||
# Literal expressions
|
||||
|
||||
<!--
|
||||
Part of the Carbon Language project, under the Apache License v2.0 with LLVM
|
||||
Exceptions. See /LICENSE for license information.
|
||||
SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
||||
-->
|
||||
|
||||
<!-- toc -->
|
||||
|
||||
## Table of contents
|
||||
|
||||
- [Overview](#overview)
|
||||
- [Numeric type literals](#numeric-type-literals)
|
||||
- [Usage](#usage)
|
||||
- [Alternatives considered](#alternatives-considered)
|
||||
- [References](#references)
|
||||
|
||||
<!-- tocstop -->
|
||||
|
||||
## Overview
|
||||
|
||||
This document is intended to cover all literal expressions, excluding numeric,
|
||||
floats and strings, which are covered in the
|
||||
[Lexical Conventions](../lexical_conventions/README.md) section. For now, the
|
||||
document explains the numeric type literals.
|
||||
|
||||
## Numeric type literals
|
||||
|
||||
Carbon has a simple keyword-like syntax of `iN`, `uN`, and `fN` for two's
|
||||
complement integers, unsigned integers, and
|
||||
[IEEE-754](https://en.wikipedia.org/wiki/IEEE_754) floating-point numbers,
|
||||
respectively. Here, `N` can be a positive multiple of 8, including the common
|
||||
power-of-two sizes (for example, `N = 8, 16, 32`).
|
||||
|
||||
Examples of this syntax include:
|
||||
|
||||
- `i16` - A 16-bit two's complement signed integer type
|
||||
- `u32` - A 32-bit unsigned integer type
|
||||
- `f64` - A 64-bit IEEE-754 binary floating-point number type
|
||||
|
||||
### Usage
|
||||
|
||||
```carbon
|
||||
package sample api;
|
||||
|
||||
fn Sum(x: i32, y: i32) -> i32 {
|
||||
return x + y;
|
||||
}
|
||||
|
||||
fn Main() -> i32 {
|
||||
return Sum(4, 2);
|
||||
}
|
||||
```
|
||||
|
||||
In the above example, `Sum` has parameters `x` and `y`, each of which is typed
|
||||
as a 32-bit two's complement signed integer. `Main` then returns the output of
|
||||
`Sum` as a 32-bit two's complement signed integer.
|
||||
|
||||
### Alternatives considered
|
||||
|
||||
- [C++ LP64 convention](/proposals/p2015.md#c-lp64-convention)
|
||||
- [Type name with length suffix](/proposals/p2015.md#type-name-with-length-suffix)
|
||||
- [Uppercase suffixes](/proposals/p2015.md#uppercase-suffixes)
|
||||
- [Additional bit sizes](/proposals/p2015.md#additional-bit-sizes)
|
||||
|
||||
## References
|
||||
|
||||
- Issue
|
||||
[#543: pick names for fixed-size integer types](https://github.com/carbon-language/carbon-lang/issues/543)
|
||||
- Proposal
|
||||
[#2015: Numeric type literal syntax](https://github.com/carbon-language/carbon-lang/pull/2015)
|
||||
Reference in New Issue
Block a user