Skip to content
العربية

Coming from C

Like C, Meem compiles to machine code (through LLVM), and its memory is manual. But it always checks arithmetic and indexing, and it writes errors as values in the signature.

In C In Meem
int8_t .. int64_t, uint8_t .. uint64_t, float, double, bool عدد8 .. عدد64, طبيعي8 .. طبيعي64, كسر32, كسر64, منطقي; widening is implicit and narrowing is explicit and checked (عدد8(ن))
zero-terminated char * نص: UTF-8 with its length, and طول counts characters, not bytes
struct هيكل (struct): passed by value; several return values with a tuple: (خارج عدد64, باقي عدد64)
a tagged union an تعداد (enum) with a payload: دائرة(نق كسر64), unpacked with حالات (since 0.15.0)
fixed array / growable array مصفوفة[نوع, حجم] (copied on assignment) / قائمة[نوع]
malloc / free allocation from an allocator (usually implicit) / حرر(س)
T * and NULL مؤشر[نوع] with جديد and محتوى, and لاشيء; no pointer arithmetic
void * from a library مؤشر_خام, readable only inside خطر
a return code and errno !نوع and خطأ.اسم, and handling them is mandatory
goto cleanup خاتمه and خاتمه_عند_خطأ
#include استورد
printf اطبع with commas, and رياضيات.نسق and نسق_عدد for formatting
undefined overflow a runtime error with code 3; wrapping is explicit: جمع_ملتف (wrapping add) and its siblings for one operation, --fast for the whole program
calling a C library خارجي with explicit C types
Two functions from a C library
خارجي "m" داله جذر_c(س كسر64) كسر64 باسم "sqrt"
خارجي داله طول_c(ن نص) طبيعي64 باسم "strlen"
اطبع(جذر_c(2.0))
اطبع("ميم بالبايتات: ", طول_c("ميم"), "، وبالأحرف: ", طول("ميم"))
Output recorded from the compilermeem 17_خارجي.م · meem 0.17.3+434e13f
1.4142135623730951ميم بالبايتات: 6، وبالأحرف: 3
Overflow is an error, not undefined behaviour
كبير = 9223372036854775807
اطبع(كبير)
اطبع(كبير + 1)
Output recorded from the compilermeem ذاكرة_تجاوز.م · meem 0.17.3+434e13f
9223372036854775807
[خطأ تشغيل E0341] سطر 3:11 - تجاوز النوع 'عدد64' في العملية '+'  اطبع(كبير + 1)

Exit code: 3

In the error catalog: E0341

What will surprise you (from writing real programs with 0.9.1, reviewed on 0.17.1)

Section titled “What will surprise you (from writing real programs with 0.9.1, reviewed on 0.17.1)”
  • Two modes, as in Zig: meem برنامج.م runs the program with a memory detector that catches use after free and double free, naming both lines, while meem build builds without the detector, and in that file use after free through another name is undefined behaviour, as in C. Build with --debug to keep the detector.
  • حرر does not follow: freeing a list of lists leaves the inner lists, like free on an array of pointers.
  • Characters before bytes: طول and جزء work in characters; byte operations have explicit names (طول_بايت, جزء_بايت, بايتات_نص, since 0.14.0).
  • Generics by copying: داله اكبر[ن](...) and هيكل زوج[ا, ب]: are copied for each combination of types, and a constraint is a trait ([ن يحقق قابل_للمقارنة], since 0.17.0); there is no dynamic polymorphism.
  • A هيكل_سي must be defined before the declaration that uses it.
  • An error is a value with a small context (since 0.11.0): a name, a message and a cause, and «نشأ في» (originated in) with the line of the first ارجع that returned it; there is no full stack trace.
  • No printf and no variadic functions, and no structs by value to C.
  • The static build (musl) is slower than --dynamic in allocation-heavy programs.
  • Bounds and overflow are checked by default, with a message giving the line and the ID.
  • خاتمه and خاتمه_عند_خطأ run in the right order across functions.
  • A static executable of tens of kilobytes, built in about a second.
  • Algorithmic code performs on a par with C (Dijkstra and a search tree).
  • خارجي is one line per function, and the symbol is checked at compile time.

Memory in Meem · What is خطر? · Meem compared with C and Zig · Calling C