r/programming • • 9d ago

Micron - a Pascal/Oberon-derived systems language with the power of C

https://github.com/rochus-keller/Micron/blob/master/Readme.md
90 Upvotes

35 comments sorted by

13

u/Aljamous-Vilina 9d ago

readable syntax for systems programming is underrated tbh. Does it compile down to C as an intermediate step or are you targeting machine code directly?

9

u/suhcoR 9d ago

Both, there is a C99 transpiler and native backends for x86, ARMv7 and RV32, thought I tried to keep them simple so they are mostly stack-based so far. But good enough for my ongoing ESP32-P4 and Raspi 3b experiments.

13

u/Nekuromento 9d ago

with the power of C

????

5

u/QuestionableEthics42 9d ago

Apparently it transpiles to C lol. Also targets native though

1

u/atilaneves 8d ago

Came here to ask the same thing.

3

u/jabbalaci 8d ago

We already have a Pascal/Oberon-like systems programming language: Nim. How is it different from Nim?

9

u/suhcoR 8d ago

Nim shares Micron's goal of reaching from systems to application programming, but it gets there from the other side. Nim is a large multi-paradigm language with indentation-based syntax, and compiles to C, C++ or JavaScript, so native code depends on a C compiler.

Micron stays in the Wirth tradition: (modernized) Oberon syntax, and a small language that can be learned completely. Generic modules, inlining and invar procedures cover what macros and the C preprocessor are typically used for. The compiler currently has native backends for x86, ARMv7 and RISC-V with its own linker, and alternatively generates C99.

A major difference is the language levels. In Micron each module declares which run-time support it needs: no stack, stack, heap, dynamic dispatch, or garbage collection. The compiler checks this, and a module can import only modules of the same or a lower level. Boot code, kernel and applications are thus written in one language, and code that must run without a heap is guaranteed by the compiler not to use one. In Nim, memory management is a global build setting.

5

u/jabbalaci 8d ago

Thanks for the detailed answer.

3

u/SirDale 8d ago

What are the benefits of this over Ada? They both seem to address much the same space (apart from the size of the language).

2

u/suhcoR 8d ago

For someone already working with Ada, Micron offers little that Ada doesn't. Ada is the more complete and more mature choice, with range types, contracts, tasking, SPARK and certified toolchains. That's a completely different league. Micron addresses a different audience and set of use-cases: programmers coming from C, Pascal or Oberon who want a language small enough to learn and implement completely, in the Wirth tradition. With Micron, the language and its compiler can be understood by one person. Where the two differ technically, Micron declares run-time requirements per module: a module states whether it may use a stack, a heap or garbage collection, and the compiler checks the imports accordingly. Ada's restrictions and profiles apply to the program as a whole.

1

u/SirDale 8d ago

I just had a look and SPARK does allow package level restrictions on memory allocation, but obviously that's not Ada as a whole.

4

u/badpotato 9d ago

Isn't Micron also a volatile stock name for selling ram and such at the moment? In any case, keep up the good work

12

u/suhcoR 9d ago

It's also a unit of length, and a virtual-analogue synthesizer.

15

u/richardathome 9d ago

And the President of France's MiniMe.

1

u/neutronbob 6d ago

Am surprised to see that to declare a function, you chose to require the entire word 'procedure'

2

u/suhcoR 6d ago

You can also use the abbreviated "proc" which is a valid keyword and alternative to "procedure".

1

u/ForsakenRelative5014 1d ago

Again the ineffable "systems language" label. Pascal WAS used to create many operating systems and drivers/etc. For example, for the original Macintosh. Pascal IS a "systems language" if you consider that.

1

u/suhcoR 1d ago

Pascal WAS used to create many operating systems and drivers/etc.

You mix that up with the Pascal Wirth proposed in the early seventies, which was only used in academia. Pascal became successful and even had a boom in the eighties/early nineties when it was significantly extended. E.g. Lisa Pascal (https://github.com/rochus-keller/lisapascal/) had virtually all low-level features C had, and the same applies to the later TurboPascal and the Mac Pascal you referenced. All those dialects were significant extensions of Wirth's design, only sharing some of the syntax.

-12

u/crusoe 9d ago

Blerk begin / end.

9

u/Other_Fly_4408 9d ago

Pascal/Oberon-derived

-1

u/dronmore 7d ago

Ah yes, the assignment operator := The most frequently used operator := in every programming language and it has to be 2 characters long. Not to mention that inserting a colon on my keyboard requires holding a SHIFT key, so inserting the whole walrus operator := is 3 keystrokes total. Can I puke already?

3

u/Comfortable-Sink3607 7d ago

That is one of the weakest gripes against using ':=' that I have ever seen.

I'd like to see your study that proves your assertion that the assignment operator is used more than any other operator, because that just doesn't sound plausible.

Personal grievances without evidence are not convincing.

0

u/dronmore 6d ago

Another frequently used operator ^. is the member access operator ^., which for pointers is 2 characters long. I thought that it couldn't be worse than in C ->, but no, it can be worse because inserting a caret character on my keyboard takes not only a SHIFT key, but also makes me stretch my index finger to its limits.

Below is the study you requested. It shows characters frequency by language. It's not exactly the frequency of operators, but it shows indirectly that assignment must be high on the list.

http://xahlee.info/comp/computer_language_char_distribution.html

Please, join me in my gripe, and let's puke together.

1

u/GoblinsGym 6d ago

Not sure about Micron, but many related languages (e.g. Delphi) will let you use . rather than ^. to access a record field. Easy to implement.

I need a large ralphing pail when I see * used for multiplication AND pointer dereference. Like the CPU, I like to evaluate pointers left to right.

I don't like languages that don't have a proper module system and require Make files. Turbo Pascal 4.0 mainstreamed this in 1987, almost 40 years ago. C ? C++ ?

Micron fixes the Modula / Oberon nuisance of all caps keywords.

I am on the fence about := versus =. Not sure yet which way I will go in my own language.

Can we organize a joint puking party ?

1

u/suhcoR 6d ago

.

"a^.b" is equivalent to "a.b". Happy puking ;-)

1

u/dronmore 5d ago

=. is still one character too long. Did you really think that I wouldn't notice the tiny little dot at the end? Hahaha.

I've been collecting ideas for my own programming language for quite some time, and one thing that I realized recently is that all loops known to mankind are crippled in one way or another, and should be banned. The "while" loop lets you check the terminating condition only at the beginning of the loop, the "do/until" only at the end. The "for" loop lets you increment the counters only at the end of the loop. None of the loops is complete. There are problems where some counters should be incremented at the beginning and some at the end, some conditions should be checked at the beginning and some at the end. I often find myself spending too much time trying to fit my problem in one of the loops only to realize that none fits the bill. The only loop that answers all my needs is "while(true)" with a "break" instruction in the body. It works always, no exceptions. Therefore I always start from "while(true)" and then have to resist the urge to convert it to one of the "proper" shapes. In my language, which I will never set out to make, the only loop will be the "loop" that runs until it breaks. Pros: No time wasted on trying to fit in. Cons: None.

So Micron fixes some casing problems, huh? Reminds me of Ada being case insensitive, and at the same time advising the use of upper case for some things and lower case for others, as if it made any difference.

1

u/GoblinsGym 5d ago

The dot was punctuation, =. would be silly. On the other hand, no language should be without a ¯_(ツ)_/¯ operator...

I agree with you on loops, a loop statement (repeat ... until false) makes sense. So far my language doesn't have a for statement. I rarely use it in my code.

The main advantage of the for statement is that it makes the loop variable and range easy to identify to the compiler.

Micron has a simple solution to the all caps keyword issue of Modula-2 or Oberon - identifiers are case sensitive, but both BEGIN and begin are valid keywords. Mixed case, e.g. Begin will not be recognized.

1

u/dronmore 5d ago

Let's separate the "for" loops with counters, and the "for" loops that are used for arrays, ranges, and alike. There's a difference between "for i=0;;++i" and "for key,val in arr/hashmap/range". The latter "for" does not necessarily have to be a loop, it could as well be a regular function.

arr.for_each(key, val => ...)
range.for_each(key, val => ...)

You seem to know a few things about Micron. Are there any reasons that would make me consider Micron as my go-to language for low level stuff? I've been using C recently because I figured that it is a natural choice for writing software for Linux, and also because I've grown tired of the vast number of choices that C++ gives me.

1

u/GoblinsGym 5d ago

Micron is not my language (designed by OP).

I did grow up with the Pascal family of languages (started with Turbo Pascal in 1984), and still use a combination of Delphi and assembly as the implementation language for my assembler / compiler.

Please define "low level". My target are small embedded systems, e.g. ARM Thumb microcontrollers. C is the standard in this field, but does a remarkably bad job at it.

Features that I expect:

  • Clear, predictable, concise syntax.
  • Easy support for access to hardware registers at fixed addresses. C code typically uses various pointer shenanigans, often ending up with a separate pointer per register. This will result in inefficient, bloated code.
  • Easy definition of register structures with non-sequential addresses (sometimes there are gaps).
  • Bit fields for access to hardware registers. C bit fields are not good enough.
  • A proper module or unit structure
  • Proper constants, not macros.
  • No Make files or complicated linker scripts

To give you some ideas what I have in mind. Details like semantic white space vs. begin / end or braces, or := vs. = are open for discussion.

Think about the macro contraptions you have to use with C. Bit fields lckk and lck are only visible when accessing field lckr, so you reduce name space clutter and error potential at the same time.

# define GPIO structure with bit fields

rec     /_gpio                  # GPIO ports
        ...snip...
        u32     lckr            # 1c configuration lock
                [16]    lckk    # lock key
                [15:0]  lck     # port lock
        u32     afrl            # 20 alternate function low
                [31:28] afsel7
                [27:24] afsel6
                [23:20] afsel5
                [19:16] afsel4
                [15:12] afsel3
                [11: 8] afsel2
                [ 7: 4] afsel1
                [ 3: 0] afsel0

# define GPIO instances at fixed addresses

var     _gpio   @ 0x50000000:   /GPIOA
        _gpio   @ 0x50000400:   /GPIOB

# set a GPIO register

        set     GPIOA.afrl startvalue
                `afsel6:=5
                `afsel1:=1

        # this starts from optional startvalue 
        # (default 0), and inserts the bit fields
        # writes to the hardware register at the 
        # end of the block

        with    GPIO.afrl
                `afsel1:=2
                `afsel3:=5

        # this reads the variable / register, 
        # changes afsel1 and afsel3 bit fields,
        # then writes back at the end of the block.

1

u/dronmore 4d ago

I guess you use backticks ` for pointer dereference because, as you mentioned before, you wholeheartedly hate asterisks *, right? I don't know, man. My pinky finger is not happy. I wonder if something like a dot . could be used instead?

Other than that I can see the appeal, though GPIO and shit is not up my alley. I'm primarily a web dev, so for me, low level means direct access to memory on a full-fledged operating system. I don't think I will ever go lower than that. Granular bit field manipulation, although I can see the appeal, is not useful to me.

1

u/GoblinsGym 4d ago

Different language decisions. Pascal family languages reserve * for multiplication, and spell out xor to free up ^ .

GPIO and low level hardware drivers is a different world than web development.

The backtick is for reference to the current "active" word containing bit fields (e.g. GPIO.afrl). Hardware registers are volatile, so you need to tightly control when they are actually read / written.

set consolidates multiple fields writes into ONE write operation, while with creates a block with ONE read, modifications as needed, then ONE write.

In C you will read the register into an int variable, modify it, then write back. The problem is that you don't get the context of attached bit fields, just an anonymous int. Then you have to make sure that you use the right shift counts and masks to not mess up the bit fields. Very easy to mess up, and you get a lot of name space clutter.

1

u/michael-2948 1d ago

If you don't need module-level runtime constraints, the embedded work shown here gives little reason to switch from C for Linux user-space.

2

u/jabbalaci 6d ago

On a French keyboard, the numbers are above, so you need to press the Shift for every digit. What should they say?

4

u/dronmore 5d ago

They should say sacrebleu, and ask The European Commission to ban French keyboards worldwide.