public inbox for ~johnnyrichard/olang-devel@lists.sr.ht
 help / color / mirror / code / Atom feed
From: Johnny Richard <johnny@johnnyrichard.com>
To: Carlos Maniero <carlos@maniero.me>
Cc: ~johnnyrichard/olang-devel@lists.sr.ht
Subject: Re: [RFC SPEC] Primitive data types and arrays
Date: Fri, 12 Apr 2024 09:32:51 +0200	[thread overview]
Message-ID: <luc74cmsdjrtcmpnthz2oaqpwkrpxfilgzn3mdmplcr237o3ix@ihmktgnrpy4x> (raw)
In-Reply-To: <D0EF88PEF0OL.2XEJ3V58Q6X3Q@maniero.me>

On Mon, Apr 08, 2024 at 12:29:11AM -0300, Carlos Maniero wrote:
> This thread tries to specify a basic datatypes of olang.
> 
> Primitive data types:
> =====================
> 
> Primitive types are types that can be held on a general purpose register.
> 
> s8   : 8-bit  signed   integer type.
> s16  : 16-bit signed   integer type.
> s32  : 32-bit signed   integer type.
> s64  : 64-bit signed   integer type.
> u8   : 8-bit  unsigned integer type.
> u16  : 16-bit unsigned integer type.
> u32  : 32-bit unsigned integer type.
> u64  : 64-bit unsigned integer type.
> f32  : 32-bit floating point   type.
> f64  : 64-bit floating point   type.
> 
> Translation to C:
> -----------------
> 
> s8   : int8_t
> s16  : int16_t
> s32  : int32_t
> s64  : int64_t
> u8   : uint8_t
> u16  : uint16_t
> u32  : uint32_t
> u64  : uint64_t
> f32  : int32_t
> f64  : int64_t

I loved it.  Out of curiosity, we are going to have _boolean_ and _char_
I believe.  Shouldn't they also be included on these primitive spec?

> C also permits the use of type qualifiers, such as signed int or short int.
> However, this specification recommends omitting the qualifier for simplicity.
> In my opinion, this approach is more intuitive. While the meaning of long int
> can be ambiguous, there’s no ambiguity with int32_t.

I agree.

> Arrays:
> =======
> 
> An array is a fixed-size collection of similar data items stored in contiguous
> memory locations. It can be used to store the collection of primitive data
> types such as int, char, float, etc., and also derived and user-defined data
> types, structures, etc.
> 
> Example:
> --------
> 
> const x: u32[] = [1]
> const y: u32[2] = [1, 2]
> 
> Grammar:
> --------
> 
> <array-type> ::= <type> <ows> '[' <ows> <number>* <ows> ']'
> <array-assign> ::= '['
>                      <ows>
>                      (<expression>
>                        (<ows> ',' <ows> <expression>)*
>                      )?
>                      <ows>
>                    ']'
> 
> Open question:
> --------------
> 
> I have no idea how to initialize an array with a value. In C I know that this
> is allowed:
> 
> int arr[20] = {0};
> 
> But I think this is ambiguous since if I remove the number 20 from the
> statement above it will give me an one-sized array.

Yeah, I see... With the syntax you proposed I suggest the following syntax
for initialize all elements zeroed:

    const arr: u8[2] = [...0]

It should only work for arrays with size explicitly declared.

> Translation to C:
> -----------------
> 
> A olang array is just like a C array, no need to translation. Although it
> differs from C by using square brakets other then curly brakets. That way we
> could easily differ arrays from structs.

Sure.  I like it. I think we can split this RFC up into different RFCs.
One for discuss primitive types and another one for discussing arrays in
general.  What do you think?

There is other topics we also can discuss about arrays, for example:

Array access by index
---------------------

The brackets in C has weird behaviour on arrays when accessed by index
as listed bellow:

    int xs[] = { 1, 2 };

    xs[0] = 1;
    0[xs] = 1;

    xs[1] = 2;
    1[xs] = 2;

The weird array access with the number as the first element works
because c does pointer arithmetics in the end.

    xs[i] == *(xs + 1)
    [i]xs == *(i + xs)

I like the C simplicity of translating it to pointers arithmetics, but I
think the access using number first is too weird.  We should avoid it I
believe.

  reply	other threads:[~2024-04-12  6:34 UTC|newest]

Thread overview: 11+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-04-08  3:29 Carlos Maniero
2024-04-12  7:32 ` Johnny Richard [this message]
2024-04-13  2:51   ` Carlos Maniero
2024-04-13 23:31     ` Johnny Richard
2024-04-16  3:40       ` Carlos Maniero
2024-04-16 18:34         ` Johnny Richard
2024-04-17  1:30           ` ricardo_kagawa
2024-04-18 21:53             ` Carlos Maniero
2024-04-24 16:23               ` ricardo_kagawa
2024-04-20 11:45             ` Johnny Richard
2024-04-24 18:45               ` ricardo_kagawa

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=luc74cmsdjrtcmpnthz2oaqpwkrpxfilgzn3mdmplcr237o3ix@ihmktgnrpy4x \
    --to=johnny@johnnyrichard.com \
    --cc=carlos@maniero.me \
    --cc=~johnnyrichard/olang-devel@lists.sr.ht \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://git.johnnyrichard.com/olang.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox