From dd2f7d732c0d2d7f569241eb8edbb2ddef8a59ea Mon Sep 17 00:00:00 2001 From: Avi levy Date: Fri, 9 Dec 2022 21:01:21 +0200 Subject: [PATCH] Design updates for #2015 numeric type literal syntax (#2410) Edit numeric literal design for the literal type proposal and add reference where #2015 was mentioned. Closes #2159 Co-authored-by: Richard Smith Co-authored-by: josh11b --- docs/design/README.md | 4 +- docs/design/expressions/README.md | 11 +++++ docs/design/expressions/literals.md | 72 +++++++++++++++++++++++++++++ 3 files changed, 85 insertions(+), 2 deletions(-) create mode 100644 docs/design/expressions/literals.md diff --git a/docs/design/README.md b/docs/design/README.md index 9905bb0f7efe..3a8d47c284a2 100644 --- a/docs/design/README.md +++ b/docs/design/README.md @@ -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 - ## 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 diff --git a/docs/design/expressions/README.md b/docs/design/expressions/README.md index cae8c4eb058e..741dc159d93e 100644 --- a/docs/design/expressions/README.md +++ b/docs/design/expressions/README.md @@ -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 diff --git a/docs/design/expressions/literals.md b/docs/design/expressions/literals.md new file mode 100644 index 000000000000..76c4fc291ae7 --- /dev/null +++ b/docs/design/expressions/literals.md @@ -0,0 +1,72 @@ +# Literal expressions + + + + + +## Table of contents + +- [Overview](#overview) +- [Numeric type literals](#numeric-type-literals) + - [Usage](#usage) + - [Alternatives considered](#alternatives-considered) +- [References](#references) + + + +## 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)