Micron 发布 Oberon 风格系统编程语言
Micron 是一款以 Oberon+ 语法为基础、能力对标 C 的系统编程语言,名称源自 MicroOberon。项目同时提供编译器、解释器与 IDE,用语言级概念替代预处理器,并引入语言层级机制,使同一语言可覆盖启动代码、驱动、内核与普通应用,且无需封装即可直接调用 C。
报道正文
本文目录5 个章节

Welcome to the Micron Programming Language
This project implements a compiler, interpreter and IDE for the Micron programming language as specified here; see also the code examples below.
Micron is a systems programming language with a syntax similar to Oberon+ and the reach of C. The name "Micron" is an abbreviation of "MicroOberon".
Micron is intended as a practical alternative to C for systems programming. It combines the modularization, strictness and simplicity of Oberon with the flexibility of C: the same language serves for boot code, drivers, an operating system kernel and ordinary applications, and it calls C without wrappers. The preprocessor is replaced by language concepts that can largely substitute its features, supplemented by explicit support for generic programming, inlining and procedures that run at compile time. Thereby the focus is always on simplicity and the greatest effect with a practicable effort.
Micron supports language levels: a module declares the level it needs, from level 0 (inlined code for hardware initialization, no stack) over level 1 (stack frames, recursion, extern C procedures), level 3 (heap, pointers, exceptions) and level 4 (type extension and dynamic dispatch) up to level 5 (garbage collection). A module can only import modules of the same or a lower level, so a kernel written at level 1 cannot depend on the heap by accident. Without the attribute, level 3 is assumed.
Documentation
In addition to the specification, a free textbook is available which shows how each feature of Micron is used and why it is designed the way it is. Part III walks through the Micron System, a port of Wirth and Gutknecht's Project Oberon operating system, which is compiled, linked and booted on an emulated RISC-V machine with a single command.
- PDF version of the book,
- Source and examples of the book; all examples compile and run with
micc.
Example applications and systems
Since the start of the project in January 2024, numerous benchmarks, virtual machines and operating systems of significant size have been migrated to Micron. The following references comprise about 130 kSLOC of working Micron code.
The examples subdirectory contains:
- a Smalltalk-80 implementation,
- the original 1976 P4 Pascal compiler,
- the Scheme 9 virtual machine,
- the Project Oberon System running on the host,
- and the Micron System described in the textbook, the migrated Project Oberon System natively running on RISC-V.
The Are-we-fast-yet repository includes the same benchmark suite in four different implementations for comparison: procedural, hybrid, interface-based and object-oriented.
The migration of the Oberon System 3 to Micron currently runs on the emulated RISC-V machine; support for the Raspberry Pi Zero 2 is work in progress.
Try it
The following precompiled versions are available at this time:
Just download, unpack and run. The command line options are described in the micc manual.
Here is a precompiled version of the Micron System discussed in the book
which you can run on Linux x86_64 by just typing ./rv32vm --elf --ram 4 --disk disk.img system.bin (requires SDL2).
More information is in the Readme.md.
Code examples
Micron is case sensitive, but each reserved word and predeclared identifier exists in a fully lower-case and a fully upper-case form with identical meaning; the last example is written in the Oberon style, all others in the lower-case style used in the book. Statement separators are optional.
Low-level programming and C interoperability
module Bits [level = 1]
var
a: array 4 of int32
p: ^int32
u, u1: uint32
begin
println(band(0F0FH, 00FFH)) // 15
println(bor(1, 4)) // 5
println(bxor(5, 1)) // 4
println(shl(1, 5)) // 32
println(shr(0FFH, 4)) // 15, logical
println(asr(-16, 2)) // -4, arithmetic
u1 := 0FFFFFFFFH
println(cast(int32, u1)) // -1, same bits
u := usig(-1)
println(u) // 4294967295, same bits
a[0] := 10; a[1] := 20; a[2] := 30
p := @a[0]
p := ptroff(p, 1) // advance by one int32
println(p^) // 20
inc(p)
println(p^) // 30
end BitsThe file is in the examples subdirectory and compiles/runs with ./micc Bits.mic --run.
module Clib [level = 1]
procedure abs(x: int32): int32; extern C
procedure toupper(c: int32): int32; extern C
procedure write(fd: int32; const buf: ^[]char; n: uint32): int32
extern C "write"
var s: array 3 of char
begin
s := "ok"
if write(1, @s, 2) # 2 then return end
println(abs(-5))
println(toupper(sigl(ord('a')))) // 65
end ClibAbstraction without objects
module Shapes [level = 2]
type
Shape = interface // a contract, satisfied structurally
proc area(): integer
proc print()
end
Rect = record w, h: integer end
Circle = record radius: integer end
procedure Rect.area(): integer // type-bound procedure, bound statically
begin
return self.w * self.h
end Rect.area
procedure Rect.print()
begin
print("rect "); print(self.w); print("x"); print(self.h)
end Rect.print
procedure Circle.area(): integer
begin
return 3 * self.radius * self.radius // coarse
end Circle.area
procedure Circle.print()
begin
print("circle r="); print(self.radius)
end Circle.print
procedure describe(s: Shape) // accepts any type with area and print
begin
s.print(); print(" area="); println(s.area())
end describe
var
r: Rect
c: Circle
begin
r.w := 4; r.h := 3
c.radius := 5
describe(@r) // rect 4x3 ar
> (注:更多技术实现细节与完整 API 文档请查阅原项目 README)
---
**作者发帖说明(HN)**:
I have been developing Micron (for "MicroOberon"), a lean, statically typed, modular systems programming language that combines Oberon’s simplicity and explicit module structure with C’s low-level capabilities and interoperability.
Micron is not my first langage design. I first built Oberon+, a modernized and unified Oberon, in order to under stand, by implementing it and by porting real systems to it, what Wirth’s design actually gives and where it fails. The systems ported to Oberon+ included the 2013 Project Oberon System and different versions of Oberon System 3. I then explored the opposite direction with Luon, a higher-level language for managed application progamming. Micron is the synthesis of this and my 30 years experience with C/C++ in systems programming.
The goal of Micron is to use the same language for boot code, device drivers, an operating system kernel, and ordinary applications, without requiring a garbage collector or routing machine access through a separate, unchecked system dialect (such as Oberon's SYSTEM.ADR/GET/PUT which operates on LONGINT). The language supports explicit memory layouts, signed and unsigned integer types, pointer operations, and direct calls to C without wrappers. One of its central design choices is language levels. Each module declars the level and thus the feature it needs, ranging from code that requires neither stack nor heap to garbage-collected objects. A module can import only modules at the same or a lower level. For exam...