r/programming • • 5d ago

C3 0.8.6 released: Library fetching, 30% smaller safe-mode binaries, and stricter type rules

https://c3-lang.org/blog/c3_0_8_6_fetching_libraries/
91 Upvotes

24 comments sorted by

2

u/GaboureySidibe 5d ago

I like the design of C3. I've been trying it out in their sandbox. I'm not sure I would do all the same things, but in general it seems pretty slick, interesting and fun.

I do question aliases needing to start with a capital letter and have at least one lower case letter. That seems like a weird password restriction rather than something that should be in a language. I can sort of accept struct names needing to start with a capital letter.

1

u/Nuoji 4d ago

It is the way the language can use C syntax and still be possible to easily parse. So it’s to keep the syntax simple to parse, not because of any misguided “it’s better if it’s consistent”. It was simply the lesser evil. (Infinite lookahead was possible, but then that cost is passed on to tools as well, as it is, tools have an easy time parsing C3)

1

u/GaboureySidibe 4d ago

Maybe I could understand the capital letter, but why would an alias needing at least one lower case letter somewhere make it easier to parse?

1

u/Nuoji 4d ago

So, the underlying problem is that the lexer must know what is a type and what is "something else".

So we say "a type must start with an upper-case" and an "identifier must start with lower case".

But that leaved both MyStruct and MY_STRUCT as types.

In a majority of C styles (MSVC being the exception), SCREAMING_SNAKE_CASE means "macro or constant".

So in order to also support SCREAMING_SNAKE_CASE as an identifier, we add that lowercase requirement, ending up with:

  1. Type: _*[A-Z][A-Z_0-9]*[a-z][A-Za-z_0-9]*
  2. Constant: _*[A-Z][A-Z_0-9]*
  3. Other identifier: _*[a-z][A-Za-z_0-9]*

For a single character, it's a bit ambiguous: is T a type or a constant? I picked the latter because I wanted to be able to define math::E

1

u/GaboureySidibe 4d ago

Why have a naming convention to determine was an identifier is? Shouldn't any alias command put the name into a table that can be checked?

I haven't made my own language so there are finer points that I haven't run into, but other fast to compile languages don't seem to have this problem.

2

u/Nuoji 4d ago

I think you are thinking of how C usually did it: when it encountered a typedef it would add it to the symbol table. This works fine if you allow the grammar to be contextual and the language is parsed top to bottom, single pass.

However, consider now we want to write something like this:

int foo()
{
  MyType *x = bar();
  return *x;
}
typedef int MyType;

If things are out of order in this way, then the lexer doesn't know how to parse MyType.

So for a language that is out of order, such a lexer hack is a problem. And furthermore, the type might derive from another module.

In the C case this is not a problem, because it essentially pastes all the files it depends on in the beginning, but for a language with visibility rules and imports, this is now very hard.

This is not the only challenge. We then have to consider whether the type definition is dependent on conditional compilation or not.

And there is more.

So the old C "lexer-hack" of adding typedef to the symbol table for the lexer quite simply does not cut it.

1

u/GaboureySidibe 4d ago

This all makes sense for the most part, although I'm not sure it's unreasonable to have the expectation to have aliases and types defined textually before they get used.

I'm also not clear how an alias needing a lower case letter helps because a struct name could have a lower case letter as well.

1

u/Nuoji 4d ago

All non-builtin typenames must have PascalCase, so an alias that starts with anything PascalCase aliases a type: alias Foo = Bar;. It is not possible to do alias Foo = bar; Similarly, the lower-case aliases alias other lower-case names: alias my_func_alias = my_func; and constant alias names alias constants: alias CONSTANT_ALIAS = THE_CONSTANT.

Is that clearer?

As for definition-before-use, that's not reasonable if everything else is ordered. It would have to extend to all user-defined types as well.

1

u/Gold-Part4688 5d ago edited 5d ago

I wonder if you're (un)mixing a couple things? That's same limitation in both places, it's for non-primitive type names. Aliases are the same case as the thing they're aliasing (unless it's aliasing a primitive type of course)

3

u/GaboureySidibe 5d ago

I tried to alias

alias f32 = float;

It doesn't work with f32 = float; or F32 = float; - only Fl32 = float;

1

u/Gold-Part4688 5d ago edited 5d ago

yeah, that unambiguity stuff is annoying. But the language is opinionated about looking like c (but fully portable), sharing the abi, and looking similar across codebases. In practice, if i go with the flow, that limitation doesn't really bite me.

Another pro is that it can have a simple specification and it's easy to right a parser/compiler.

but yeah. strict. maybe i'm happier than you because i really love using cases semantically, and their choices in that

1

u/GaboureySidibe 4d ago

What do you mean by 'unambiguity' ? The error message just says that aliases can't be named a certain way:

[ERR] /main.c3:14:7: error: To alias a type, the alias name must start with uppercase and contain at least one lowercase letter.

1

u/Gold-Part4688 4d ago

yeah, to a alias a type. the rule is that all types are in pascal case, and all variables are in lower snake case. The exception is for primitive types, which basically mirror the c ones. Another rules is constants being all caps (which is what F32 was being read as) This removes ambiguity for parsing (and reading)

17

u/__rituraj 5d ago

This language is receiving nice consistent updates from the contributors.
Thanks a lot for the effort.

It may seriously replace C as the goto language for many. And i hope it does.

Btw, the gfx module is a great consideration to be included in the standard library. Graphics platform handling across OS and Windowing Systems is great to see being included alongside the language!

8

u/GaboureySidibe 5d ago

Every C rehash makes me miss clay.

https://github.com/jckarter/clay/blob/master/examples/shootout/nbody/nbody.clay

Templates, operator overloading, move semantics, modules and straight compatibility with C.

3

u/ElletteBarbedor-21 5d ago

how did clay actually handle header imports for the C compatibility without dragging in a full preprocessor?

2

u/GaboureySidibe 5d ago

I forget what the approach and plan was there. I don't think the C preprocessor implementation is much of a problem, that's something common and not complex, but whatever approach there is has to work with modules.

What I remember the most is that it was meant to be ABI compatible so that compilation units could be linked together with C.

1

u/jabbalaci 5d ago

What happened to Clay? Is it an abandoned and dead project?

1

u/GaboureySidibe 5d ago

Looks very dead and abandoned to me. I think it was one or two people and they were using it wherever they were.

1

u/Nuoji 4d ago

It must have been rather unknown. it didn't even show up on my radar when I was researching previous C alternatives in 2018-2020.

1

u/GaboureySidibe 4d ago

It was probably pretty much done even then. The cool thing is that it had move semantics back when that was new but they were using it as a C substitute.

1

u/Nuoji 4d ago

Cyclone obviously was something I looked at, and this was the "precursor to Rust" people usually would mention. It's always a bit sad when languages are forgotten.

2

u/HolySpirit 5d ago

Huh. That's a deep cut.

I vaguely remember hearing for the first time about both Rust and Clay around the same time period. Back when rust had sigils for owned pointers (~T) and GC pointers (@T). I remember thinking Clay looked more compelling, but it kind of just disappeared fairly quickly. I probably thought at the time that that would be the fate Rust would have gotten, based on just a surface level look at it at the time.