Akuli / jou

Yet another programming language
MIT License
11 stars 4 forks source link

What is the purpose of the 1234S syntax? #350

Open Akuli opened 1 year ago

Akuli commented 1 year ago

I'm not sure why we need the recently added 1234S syntax. Why not just do 1234 as float?

We still need the L thing to specify longs: 123456789123456789 as long wouldn't work, because 123456789123456789 by itself would be an int and the value does not fit into an int.

Similarly, I think we don't really need the 12.34f syntax. You can always do 12.34 as float.

littlewhitecloud commented 1 year ago

I've been thinking about this for a long time, I think we should let compiler to set what type is it. For example, just like 123456798123456798 doesn't fit into an int, so the compiler should set the type to long automaticly. But you can still use 12.34f or 12.34 as float to force type conversion or set the type.

Akuli commented 1 year ago

I like the idea, and it's pretty close to what Zig does, for example. But there's at least this situation where it would not work:

foo = 0
foo = some_function_returning_int()

The first line foo = 0 sets it to be as small as possible to fit a zero, so probably 8-bit signed (currently unnamed) or short. But then the second line fails, because it's trying to assign int into a short.

Akuli commented 1 year ago

Also, I think I really want to get this detail right, i.e. pick the least bad option possible. I'll keep this open for a few days and see what options we can come up with.

In general, Jou should be simple and minimal. That's why my current favorite is to only have L suffix: you can use as for everything else.

littlewhitecloud commented 1 year ago

The first line foo = 0 sets it to be as small as possible to fit a zero, so probably 8-bit signed (currently unnamed) or short. But then the second line fails, because it's trying to assign int into a short. I mean, we should make Type promotion like short to int but not int to short

Akuli commented 1 year ago

So what is the type of 0? Is it a short or an int? If it is a short, does foo = 0 still somehow make an int variable? If it is int, then do you just do foo = 0 as short when you need a short?

littlewhitecloud commented 1 year ago

It will be a int, because the number doesn't fit into short(16bit), so if we don't want overflow, we have to set the type to int(32bit). Maybe short use less memory(lol)... It looks like python's variable (type can be change)

Akuli commented 1 year ago

It looks like python's variable (type can be change)

This is something I want to avoid. When there is foo = bar in the code, and a variable foo doesn't exist yet, I want to use only that line to determine the type of the newly created foo variable. I don't want to look at other lines below to find the type of a variable. I also don't want the type of a variable to change in the middle.

Again, this is my design goal of keeping Jou simple and minimal. In the past I have made other languages where the type of a variable is determined from all places where it's used (I think my oomph language does this, for example).

Akuli commented 1 year ago

Another thought: Changing the type of a variable in the middle becomes complicated when the same code executes many times.

foo = 0  # foo is int
while True:
    # foo is int
    printf("%d\n", foo)
    ...
    foo = 123456789123456789L
    # foo is long

If the loop runs more than once, we somehow have to convert 123456789123456789L to int because the type of the variable was changed. The simplest solution is to never change the type of an existing variable.

Akuli commented 1 year ago

For example, just like 123456798123456798 doesn't fit into an int, so the compiler should set the type to long automaticly.

After thinking about this a bit more I think it is a good idea, if the type always becomes int or long. You can still use as when needed.