<?xml version="1.0" encoding="UTF-8"?><?xml-stylesheet href="/scripts/pretty-feed-v3.xsl" type="text/xsl"?><rss version="2.0" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:h="http://www.w3.org/TR/html4/"><channel><title>Norb&apos;s Space</title><description>Where knowledge and ideas flow.</description><link>https://norb.space</link><item><title>Writing a Compiler for Rust</title><link>https://norb.space/blog/writing-a-compiler-for-rust</link><guid isPermaLink="true">https://norb.space/blog/writing-a-compiler-for-rust</guid><description>Thoughts on writing Rusty, a compiler for Rx.</description><pubDate>Tue, 21 Apr 2026 00:00:00 GMT</pubDate><content:encoded>&lt;p&gt;&lt;a href=&quot;https://github.com/RayZh-hs/rusty&quot;&gt;Rusty&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;This semester, one coursework of mine required me to write a compiler for a subset of Rust named Rx. I chose Kotlin for this project, mostly to get familiar with the language, and partly because its sealed classes fit compiler data structures rather nicely. I named the compiler Rusty.&lt;/p&gt;
&lt;p&gt;I spent a lot of time on this project, and I learnt much more from it than I expected. In this post, I will not go through how to make your own compiler step by step. Instead, I want to write down some of the design decisions and tradeoffs I faced when writing Rusty, especially the parts where a modern language-like frontend forced me away from the cleaner textbook story.&lt;/p&gt;
&lt;h2&gt;A Standard Compiler Pipeline&lt;/h2&gt;
&lt;p&gt;A typical compiler pipeline consists of these stages:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;&lt;strong&gt;Preprocessing&lt;/strong&gt;: Removes macros and sometimes comments.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Lexical Analysis&lt;/strong&gt;: Converts source code into a token stream.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Parsing&lt;/strong&gt;: Converts the token stream into an Abstract Syntax Tree (AST).&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Semantic Analysis&lt;/strong&gt;: Checks the AST for semantic errors.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;IR Generation&lt;/strong&gt;: Converts the AST into an Intermediate Representation (IR).&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Optimization&lt;/strong&gt;: Optimizes the IR for better performance.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Code Generation&lt;/strong&gt;: Converts IR into target machine code or assembly.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Linking&lt;/strong&gt;: Combines multiple object files into a single executable.&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;For me, linking was not a major problem since Rx only supports single-file compilation. I also did not need to implement Rust macros, lifetimes, or the full trait system. The required middle-end target was LLVM IR, although in my humble opinion, LLVM IR without sufficient attributes is sometimes too permissive to express the guarantees that a strict language like Rust gives you.&lt;/p&gt;
&lt;p&gt;Rusty eventually became a fairly standard pipeline:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-text&quot;&gt;source -&gt; preprocess -&gt; lex -&gt; parse -&gt; semantic -&gt; LLVM-like IR -&gt; optimize -&gt; RISC-V assembly
&lt;/code&gt;&lt;/pre&gt;
&lt;h2&gt;Tricks for the Lexer&lt;/h2&gt;
&lt;p&gt;Textbooks often suggest using a list of regular expressions to recognize the next token. This is powerful, but it also hides a surprising amount of cost. Badly worded regex expressions easily throws the lexer into exponential time complexity. In most cases, we can develop more targeted and faster methods by looking at the actual language grammar.&lt;/p&gt;
&lt;p&gt;After reading the Rx syntax, I found a simple rule: the type of the next token can usually be determined by looking at the first character, with only a few extra checks.&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;An &lt;strong&gt;identifier or keyword&lt;/strong&gt; starts with an alphabetic character or &lt;code&gt;_&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;A &lt;strong&gt;number&lt;/strong&gt; starts with a digit. It can contain digits, underscores, and suffixes like &lt;code&gt;1_024i32&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;A &lt;strong&gt;normal string literal&lt;/strong&gt; starts with &lt;code&gt;&quot;&lt;/code&gt;, while byte strings and C strings start with prefixes such as &lt;code&gt;b&quot;&lt;/code&gt; and &lt;code&gt;c&quot;&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;A &lt;strong&gt;raw string literal&lt;/strong&gt; starts with patterns like &lt;code&gt;r&quot;&lt;/code&gt;, &lt;code&gt;r#&quot;&lt;/code&gt;, &lt;code&gt;br&quot;&lt;/code&gt;, or &lt;code&gt;cr&quot;&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;A &lt;strong&gt;character literal&lt;/strong&gt; starts with &lt;code&gt;&apos;&lt;/code&gt;, and byte character literals start with &lt;code&gt;b&apos;&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;Symbols and operators can be tokenized greedily, because the ambiguous operators in Rx are short and bounded.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;So the Rusty lexer first classifies the next token with a small &lt;code&gt;peekClass&lt;/code&gt; function. After that, each class has a specialized scanner. Identifiers and numbers scan until a word boundary. Escaped strings skip escaped characters. Raw strings count the number of &lt;code&gt;#&lt;/code&gt; symbols and search for the matching closing delimiter. Operators try the longest known spelling first.&lt;/p&gt;
&lt;p&gt;I do not infer that every language should have a hand-written lexer. The important lesson is that a unified mechanism is not automatically better. For a language subset with clear lexical boundaries, a targeted lexer can stay simple and run in low-constant $O(n)$ time. In practice, this made lexing cheap enough that it never became the bottleneck of Rusty, unlike the works of many other students in the course.&lt;/p&gt;
&lt;h2&gt;Parsing Without Being Clever&lt;/h2&gt;
&lt;p&gt;The parser is a recursive descent parser built around the AST node hierarchy. Kotlin sealed classes worked well here: most AST families can be represented as closed sets of node variants, and later visitors can use &lt;code&gt;when&lt;/code&gt; expressions to make missing cases visible.&lt;/p&gt;
&lt;p&gt;There is an old temptation to make parsing &quot;smart&quot; by attaching too much meaning to it. I tried to avoid that. The parser&apos;s job in Rusty is to construct the syntax tree and reject syntactically invalid programs. It does not decide whether an identifier is a variable, a function, a type, or an associated item. It does not decide whether an integer literal should be &lt;code&gt;i32&lt;/code&gt; or &lt;code&gt;usize&lt;/code&gt;. It does not decide whether &lt;code&gt;self.foo()&lt;/code&gt; is valid. Those questions are semantic questions, and mixing them into parsing would have made later passes harder to reason about.&lt;/p&gt;
&lt;p&gt;This separation paid off when the language subset changed during the project. Some syntactic forms, such as tuples and match expressions, could still be parsed or represented, while semantic analysis became the place that rejected removed features with useful errors. In a course project, where the spec can evolve, that flexibility is very valuable.&lt;/p&gt;
&lt;h2&gt;Semantic Analysis for Modern Languages&lt;/h2&gt;
&lt;p&gt;Before Rx, the course traditionally used a compiler for Mx, a subset of C. In Mx, semantic analysis is relatively straightforward. Many checks can be done in a single pass because declarations mostly obey a C-like order and the language has fewer context-sensitive rules.&lt;/p&gt;
&lt;p&gt;Rx is different. Constants can be used before their textual definition. Functions can appear out of order. Functions can nest inside other functions. Blocks, loops, &lt;code&gt;if&lt;/code&gt; expressions, &lt;code&gt;break&lt;/code&gt;, and &lt;code&gt;return&lt;/code&gt; all participate in type inference. Variables can shadow other variables. Mutability matters, especially when references, fields, and indexing are involved. This makes semantic analysis much more complicated and, in my experience, much more interesting.&lt;/p&gt;
&lt;p&gt;I eventually settled on a five-pass semantic pipeline:&lt;/p&gt;
&lt;h3&gt;Item Name Collection&lt;/h3&gt;
&lt;p&gt;The first pass builds the scope tree and records top-level item names. At this point, many symbols are only shells. A struct symbol knows that a struct exists and has certain field names, but the field types may still be unresolved. A function symbol knows that a function exists and has a signature slot, but some parameter and return types may not be filled in yet.&lt;/p&gt;
&lt;p&gt;This pass also creates child scopes for crates, function parameter lists, function bodies, blocks, traits, &lt;code&gt;impl&lt;/code&gt; blocks, and loops. That sounds like housekeeping, but it is one of the most important design choices in Rusty. Once scopes are explicit, every later pass can walk the same tree and ask a clear question: &quot;What does this identifier mean from here?&quot;&lt;/p&gt;
&lt;h3&gt;&lt;code&gt;impl&lt;/code&gt; Injection&lt;/h3&gt;
&lt;p&gt;Rust syntax lets you write methods away from the type definition:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-rust&quot;&gt;struct Point {
    x: i32,
    y: i32,
}

impl Point {
    fn norm2(self) -&gt; i32 {
        self.x * self.x + self.y * self.y
    }
}
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;For later phases, it is much more convenient if &lt;code&gt;Point&lt;/code&gt; directly knows its associated methods. Rusty therefore performs an &lt;code&gt;impl&lt;/code&gt; injection pass: it finds the target type of each &lt;code&gt;impl&lt;/code&gt; block and injects the functions and constants from the impl scope into the corresponding struct or enum symbol.&lt;/p&gt;
&lt;p&gt;Trait impls add another layer. Rusty checks that implemented functions and constants match the trait header-level requirements: no extra names, no missing names, matching method receiver kind, and matching arity. This is still far from a complete Rust trait solver, but it is enough to make the subset coherent and to keep method lookup from degenerating into ad hoc searches later.&lt;/p&gt;
&lt;h3&gt;Item Type Resolution&lt;/h3&gt;
&lt;p&gt;After names and associated items are available, Rusty resolves item-level types. Struct fields get concrete semantic types. Function parameter and return types are resolved. Constants get their declared types and can be evaluated through the static resolver. The &lt;code&gt;main&lt;/code&gt; function receives a special return treatment because the language and judge expectations treat it more like an exit point than an ordinary value-returning function.&lt;/p&gt;
&lt;p&gt;The key tradeoff here is patience. It is tempting to resolve expression bodies as soon as you see a function, but doing so too early creates problems for out-of-order definitions. By resolving item headers first, Rusty makes all callable and nameable things visible before checking executable code.&lt;/p&gt;
&lt;h3&gt;Function Parameter Declaration&lt;/h3&gt;
&lt;p&gt;Function parameters are not just strings in a signature. They are variables inside the function body scope. This is especially important for destructuring patterns and for &lt;code&gt;self&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;Rusty has a dedicated pass that enters each function&apos;s definition scope and declares parameter symbols there. If a method has a receiver, the pass creates a variable symbol named &lt;code&gt;self&lt;/code&gt; with the correct type and mutability. For ordinary parameters, it extracts variables from typed patterns and declares them in order.&lt;/p&gt;
&lt;p&gt;Separating this from item type resolution may look verbose, but it keeps the model clean. A function signature belongs to the containing scope; its parameter variables belong to the function&apos;s inner scope. Treating those as different facts avoids many small bugs in lookup and shadowing.&lt;/p&gt;
&lt;h3&gt;Function Bodies Tracing&lt;/h3&gt;
&lt;p&gt;The final semantic pass walks executable code. This is where most of the &quot;Rust-like&quot; complexity appears.&lt;/p&gt;
&lt;p&gt;Rusty resolves every expression type and remembers the result in a type memory attached to the AST. It checks implicit and explicit casts, binary and unary operators, integer literal bounds, field access, method calls, indexing, reference and dereference expressions, struct literals, assignment targets, &lt;code&gt;return&lt;/code&gt;, &lt;code&gt;break&lt;/code&gt;, &lt;code&gt;continue&lt;/code&gt;, and block result types.&lt;/p&gt;
&lt;p&gt;Some examples of the checks in this pass:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Assignment requires a valid left-value. Assigning to an immutable binding is rejected.&lt;/li&gt;
&lt;li&gt;Assigning through a reference requires a mutable reference.&lt;/li&gt;
&lt;li&gt;Indexing auto-dereferences references to arrays, but still respects mutability when used as an assignment target.&lt;/li&gt;
&lt;li&gt;&lt;code&gt;if&lt;/code&gt; branches must infer a compatible common type.&lt;/li&gt;
&lt;li&gt;&lt;code&gt;loop&lt;/code&gt; and &lt;code&gt;while&lt;/code&gt; bodies use &lt;code&gt;break&lt;/code&gt; expressions to infer the loop&apos;s result type.&lt;/li&gt;
&lt;li&gt;&lt;code&gt;return&lt;/code&gt; expressions contribute to the expected function return type.&lt;/li&gt;
&lt;li&gt;Integer literals are progressively constrained by their surrounding context.&lt;/li&gt;
&lt;li&gt;Code after a known &lt;code&gt;exit()&lt;/code&gt; is rejected as unreachable.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;This pass is long, and I do not think that is automatically a bad thing. A semantic checker is the part of a compiler where the language&apos;s actual rules collect. The danger is not length by itself; the danger is mixing unrelated responsibilities. Rusty&apos;s function tracing pass is large, but its responsibility is still one thing: simulate the meaning of executable code well enough to assign types and reject invalid programs.&lt;/p&gt;
&lt;h2&gt;Afterthoughts: On AST Tree Walking&lt;/h2&gt;
&lt;p&gt;In the prior two phases Rusty depended heavily on tree walking, the textbook way to traverse an AST. In hindsight, I think this may not be a good fit after all. In many cases we need not walk the entire tree, but only need to visit a small selection of AST node types. Another issue arises with state management. Here&apos;s an example: Most traversals require block awareness - knowing which block we are presently inside, and a list up to the prelude block. In traditional tree walking, we would need to reconstruct this data each time anew, and we cannot easily share the code between passes. I settled with a dirty way of:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Creating auxiliary data structures to store the state of the current traversal, for instance &lt;code&gt;ScopeMaintainerCompanion&lt;/code&gt; which maintains the current scope, and &lt;code&gt;SelfResolverCompanion&lt;/code&gt; which resolves the &lt;code&gt;self&lt;/code&gt; syntax to the correct type.&lt;/li&gt;
&lt;li&gt;Wiring them up into &lt;code&gt;SimpleVisitorBase&lt;/code&gt; to create mixed versions of visitor bases, like &lt;code&gt;ScopeAwareVisitorBase&lt;/code&gt;.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Since, unlike Python with its almighty decorators, and Rust with its magical macros, Kotlin does not have a good way to mix these behaviors in, so I ended up with an overwhelming number of visitor base classes, each containing boilerplate code, because I dislike using mixins.&lt;/p&gt;
&lt;p&gt;Today, were I to redesign this section I would have used a context store. Instead of traversing the entire tree time and again, I would have stored all the data needed in a context object, forming a sparse tree that mirrors the AST. I would be able to provide jump-points from which I can freely index the tree, filtering out what I need, and mutating the context like what &lt;code&gt;self&lt;/code&gt; is in scope, etc. This would have made the code much cleaner, and would have allowed me to boost the speed of the semantic analysis phase greatly by cutting down on deep function calls and repeated calculations.&lt;/p&gt;
&lt;h2&gt;Afterthoughts: A Small Type System Is Still a Type System&lt;/h2&gt;
&lt;p&gt;Another afterthought when implementing Rx&apos;s semantic analysis is that &quot;subset&quot; does not mean &quot;simple&quot;. Even a simplified Rust needs a real semantic model to handle type inference and constant analysis.&lt;/p&gt;
&lt;p&gt;Take an example: we can assign literal &lt;code&gt;100&lt;/code&gt; to both &lt;code&gt;i32&lt;/code&gt; and &lt;code&gt;u32&lt;/code&gt;, but we cannot assign &lt;code&gt;-100&lt;/code&gt; to &lt;code&gt;u32&lt;/code&gt;. We cannot assign &lt;code&gt;I32MAX + 1&lt;/code&gt; to &lt;code&gt;i32&lt;/code&gt;, but we can assign its negative &lt;code&gt;-I32MAX - 1&lt;/code&gt; to &lt;code&gt;i32&lt;/code&gt;. There are literals that can be interpreted as multiple types, and, strongly typed as Rx is, there are still hidden &quot;implicit casts&quot; that cast a literal to one concrete type. Couple this with tuples and structs, and the type system becomes a non-trivial part of the compiler.&lt;/p&gt;
&lt;p&gt;Rusty&apos;s semantic layer distinguishes types, symbols, and values:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-text&quot;&gt;SemanticType   -&gt; what shape a thing has
SemanticSymbol -&gt; what name was declared and where
SemanticValue  -&gt; what constant value is known
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;That separation saved me from several design traps. A struct type is not the same as the symbol that declared the struct. A constant symbol is not the same as the value it evaluates to. A variable&apos;s type is not the same as the loaded value that will later appear in IR.&lt;/p&gt;
&lt;p&gt;This matters because the compiler often needs only one of the three. During field lookup, we mostly care about the type. During shadowing checks, we care about the symbol and its scope. During array repeat expressions, we need an actual constant value. Keeping those concepts separate makes the code a little more verbose, but much less confusing.&lt;/p&gt;
&lt;h2&gt;Generating IR&lt;/h2&gt;
&lt;p&gt;Rusty uses a &lt;a href=&quot;https://github.com/RayZh-hs/LLVM&quot;&gt;Kotlin LLVM-like IR library&lt;/a&gt;, which I wrote during this course. It is strange that the only Kotlin-native LLVM library is old and does not support modern LLVM features. Perhaps Kotlin is simply suit best suited for infrastructure work like this due to a relatively heavy overhead. The IR constructor performs several ordered steps:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;Register the prelude and runtime-facing functions.&lt;/li&gt;
&lt;li&gt;Register struct layouts.&lt;/li&gt;
&lt;li&gt;Generate helper functions for struct sizes where needed.&lt;/li&gt;
&lt;li&gt;Register function signatures before generating bodies.&lt;/li&gt;
&lt;li&gt;Generate function bodies.&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;The &quot;register first, generate later&quot; idea is important for the same reason as semantic item collection: functions can call functions that are defined later in the source file.&lt;/p&gt;
&lt;p&gt;The IR generator also has to decide how source-language values map to IR values. Unit-derived types produce no storage. Strings become private global arrays with a pointer to their first byte. Struct and array values often travel by pointer because aggregate copying in LLVM IR must be handled carefully. Large aggregate assignments are lowered with explicit memory copies instead of pretending every value is a small scalar.&lt;/p&gt;
&lt;p&gt;A trick worth documenting here: Since I wanted to test my IR code on my x86 machine, I could not hardcode the size of structs into the generated LLVM IR. Instead, I generated a helper function for each struct that returns its size. It creates a 2-array of that type, and returns the offset of the second element from the first, using pointer arithmetic. This is a good debugging strategy (of course, it is optimized way in later phases).&lt;/p&gt;
&lt;h2&gt;Optimization: To Save the Day&lt;/h2&gt;
&lt;p&gt;Arguably, optimization is the hardest and most important part of a compiler. It took me around a month to implement the entire, production-ready optimization pipeline for Rusty.&lt;/p&gt;
&lt;p&gt;Optimizations mainly take the form of IR transformations. Rusty performs 20 IR transformation passes in sequence:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;SizeInliningPass&lt;/li&gt;
&lt;li&gt;FunctionInliningPass (threshold 40)&lt;/li&gt;
&lt;li&gt;SmallMemcpyLoweringPass&lt;/li&gt;
&lt;li&gt;InstCombineCleanupPass&lt;/li&gt;
&lt;li&gt;PointerSlotForwardingPass&lt;/li&gt;
&lt;li&gt;ScalarReplacementOfAggregatesPass&lt;/li&gt;
&lt;li&gt;Mem2RegPass&lt;/li&gt;
&lt;li&gt;AggressiveDeadCodeEliminationPass&lt;/li&gt;
&lt;li&gt;InstCombineCleanupPass&lt;/li&gt;
&lt;li&gt;IdenticalGepReductionPass&lt;/li&gt;
&lt;li&gt;GlobalValueNumberingPass&lt;/li&gt;
&lt;li&gt;InstCombineCleanupPass&lt;/li&gt;
&lt;li&gt;LoopInvariantCodeMotionPass&lt;/li&gt;
&lt;li&gt;LoopAddressReductionPass&lt;/li&gt;
&lt;li&gt;InstCombineCleanupPass&lt;/li&gt;
&lt;li&gt;LoopCounterPromotionPass&lt;/li&gt;
&lt;li&gt;InstCombineCleanupPass&lt;/li&gt;
&lt;li&gt;CFGSimplifyPass&lt;/li&gt;
&lt;li&gt;AggressiveDeadCodeEliminationPass&lt;/li&gt;
&lt;li&gt;InstCombineCleanupPass&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;I will not go into the details of each pass, but I will pick two that I think worth special mention.&lt;/p&gt;
&lt;h3&gt;Small Memcpy Lowering Pass&lt;/h3&gt;
&lt;pre&gt;&lt;code class=&quot;language-llvm&quot;&gt;; before  (copy 8 bytes, repeat count 1)
call void @aux.func.memfill(ptr %dst, ptr %src, i32 8, i32 1)

; after
%c.load = load i64, ptr %src   ; &quot;lowered small memcpy&quot;
store i64 %c.load, ptr %dst    ; &quot;lowered small memcpy&quot;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The idea is, for fixed-size structs who can fit into a single register, we can lower the &lt;code&gt;memcpy&lt;/code&gt; call into a load and store instruction which, hopefully later can be turned into a register move by Mem2Reg. We &quot;reinterpret&quot; the memory as a single integer type, and load/store it in one go. This is a very efficient way to handle small struct copies, and due to its numerous nature, it helps quite a lot.&lt;/p&gt;
&lt;h3&gt;Mem2Reg Pass&lt;/h3&gt;
&lt;pre&gt;&lt;code class=&quot;language-llvm&quot;&gt;; before
%x = alloca i32
store i32 0, ptr %x
br ... ; then store i32 1 on one path
%v = load i32, ptr %x

; after
%x.phi = phi i32 [ 0, %entry ], [ 1, %then ]
; %v replaced by %x.phi; alloca/loads/stores removed
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;This is definitely the most important optimization pass in Rusty. It is a classic SSA construction pass, which promotes stack-allocated values to registers. The final production-ready version relies on escape analysis.&lt;/p&gt;
&lt;p&gt;The nice thing about writing a rust-like language is that we can make a lot of assumptions about the LLVM IR we generate. In practice, most of the escape analysis is trivially done in the IR generator, and passed down in the form of attributes. This saved me a lot of time: Recovering this from LLVM IR is non-trivial.&lt;/p&gt;
&lt;h3&gt;A failed attempt: Jump Lowering Pass&lt;/h3&gt;
&lt;pre&gt;&lt;code class=&quot;language-llvm&quot;&gt;; before
block1:
    %cond1 = icmp eq i32 %x, 0
    br i1 %cond1, label %block2, label %block4
block2:
    %cond2 = icmp eq i32 %y, 0
    br i1 %cond2, label %block3, label %block4

;after:
block12:
    %cond1 = icmp eq i32 %x, 0
    %cond2 = icmp eq i32 %y, 0
    %cond12 = and i1 %cond1, %cond2
    br i1 %cond12, label %block3, label %block4
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Here I mention an attempt that ended up making the compiler slower. Last year, when taking the CPU course, I learnt that speculative execution is a must for modern processors, and that branch misprediction is a major performance killer. Therefore, I thought, if I could merge blocks and replace multiple branches with a single branch, with precalculated and merged conditions, I can cut down on the number of branches and improve performance.&lt;/p&gt;
&lt;p&gt;However, in practice, this optimization was not effective. In my view there are two possible explanations:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;Modern branch predictions are very good, and they seldom fail.&lt;/li&gt;
&lt;li&gt;This modification disabled the Branch Preset Optimization in Assembly Generation, which we will cover later, disrupting the performance of the generated assembly greatly.&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;Nevertheless, this is a very interesting finding, and I present it to you as a cautionary tale: Not all optimizations that seem reasonable are actually effective.&lt;/p&gt;
&lt;h2&gt;Lowering to RISC-V&lt;/h2&gt;
&lt;p&gt;The last stage of Rusty lowers IR into RISC-V assembly. The main target configuration is RV64 with integer and multiply/divide instructions, while the judge runtime path uses Clang to build compatible &lt;code&gt;rv64gc/lp64d&lt;/code&gt; runtime assembly.&lt;/p&gt;
&lt;p&gt;The backend is split into three main pieces:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;A register allocator.&lt;/li&gt;
&lt;li&gt;A stack frame materializer.&lt;/li&gt;
&lt;li&gt;An instruction translator.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;The register allocator uses liveness and use-def information to build an interference graph. Values that are too large for a register are forced onto the stack. Smaller values are colored into physical caller-saved and callee-saved registers, with deterministic tie-breaking so that output remains stable for debugging.&lt;/p&gt;
&lt;p&gt;The inference graph, if unpruned, is $O(n^2)$ in size. This leads to severe performance problems for large functions. We can think of this issue as a scheduling problem: if there are more than $k$ live values at a program point, and only $k$ registers, then some values must be spilled. When building the graph, we can draw on a heuristic to spill, for instance, the most-distantly used value (yes, we are doing optimizations offline, so we know the future). This allows us to prune the graph to $O(kn)$ size, and total complexity is $O(k^2 + kn)$. A great improvement!&lt;/p&gt;
&lt;p&gt;After allocation, the stack frame materializer assigns concrete stack objects for spills, allocas, saved callee-saved registers, return addresses, call temporaries, and outgoing stack arguments. Only after this step does the translator know enough to emit correct prologues, epilogues, loads, stores, and call sequences.&lt;/p&gt;
&lt;p&gt;Finally, the translator lowers each IR instruction into RISC-V assembly. Small values can be moved through registers. Aggregate values are copied through memory. &lt;code&gt;getelementptr&lt;/code&gt; becomes address arithmetic based on computed type layouts, and small structs, those within 64 bits, are loaded directly. Phi nodes are handled as edge moves before branch targets. Function calls preserve the required registers and pass arguments through &lt;code&gt;a0&lt;/code&gt; to &lt;code&gt;a7&lt;/code&gt; or the stack.&lt;/p&gt;
&lt;p&gt;Many optimizations done in the Assembly Generation phase are due to the flexible nature of the RISC-V instruction set. A very telling example is the lowering of conditional jumps, which terminate most loops. I noticed, by comparison with the GCC-generated assembly, that in many cases,&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;%case = icmp eq i32 %x, 0
br i1 %case, label %then, label %else
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Need not be lowered to a register comparison and a conditional branch. Instead, it can be lowered to a single &lt;code&gt;beqz&lt;/code&gt; instruction, which is shorter and, it turns out, much faster. Indeed, this accounted for 10% in optimization score in the judge&apos;s test cases.&lt;/p&gt;
&lt;h2&gt;Closing Thoughts&lt;/h2&gt;
&lt;p&gt;Writing Rusty made me appreciate how much of compiler construction is about choosing the right boundaries. Lexing was fast because it used language-specific structure. Parsing stayed manageable because it did not try to solve semantic problems. Semantic analysis became understandable only after being split into multiple passes. The design of such a compiler is not about writing clever code, but about making the right tradeoffs and keeping the responsibilities of each component clear.&lt;/p&gt;
&lt;p&gt;A lesson throughout this entire project is that, the textbook way is never the only way. The textbook way way may be universal, but each language has its special characteristics. A compiler for a modern language must be aware of those characteristics, and to take advantage of them, and here lies the real challenge and joy of compiler design.&lt;/p&gt;
&lt;p&gt;It also made me aware that everyday tools like &lt;code&gt;rustc&lt;/code&gt;, &lt;code&gt;clang&lt;/code&gt; and &lt;code&gt;gcc&lt;/code&gt; are so immensely powerful and complex. Despite nearly a year of work, Rusty still lags behind &lt;code&gt;gcc&lt;/code&gt; O2 optimization in almost every task, let alone O3 and Ofast. A wise instructor of mine once said that &quot;there is no magic in the world&quot;, and that applies to compiler design perfectly. The small, compact binary that we use daily to transform our high-level code into machine code is the result of decades of research, engineering, and optimization. It is a testament to the depth and breadth of knowledge required to build such tools.&lt;/p&gt;
&lt;p&gt;The final compiler is still small compared with a real Rust compiler, but it is large enough to contain many of the same design pressures. That is what made the project worthwhile. A subset can remove features, but it cannot remove the need for coherent design.&lt;/p&gt;
&lt;p&gt;If you are interested, the source code is available at &lt;a href=&quot;https://github.com/RayZh-hs/rusty&quot;&gt;github.com/RayZh-hs/rusty&lt;/a&gt;. Head to the &lt;code&gt;target/oj-testing&lt;/code&gt; branch to see the modified rv64im edition with full optimization suite.&lt;/p&gt;</content:encoded><h:img src="/_astro/thumbnail.DKdb2k6i.jpg"/><enclosure url="/_astro/thumbnail.DKdb2k6i.jpg"/></item><item><title>Simply Nord</title><link>https://norb.space/blog/simply-nord</link><guid isPermaLink="true">https://norb.space/blog/simply-nord</guid><description>Building a beautiful, minimalistic theme for KDE Plasma 6.</description><pubDate>Wed, 18 Feb 2026 16:00:00 GMT</pubDate><content:encoded>&lt;p&gt;Two weeks ago I decided to make the switch from Windows to Arch Linux. When selecting a desktop environment, I found myself choosing between KDE Plasma and GNOME. Having used GNOME before, I decided to give KDE Plasma a try, since I have heard that it is highly customizable.&lt;/p&gt;
&lt;p&gt;I like KDE Plasma for its clean interface and powerful customization options, but the default theme seems less modern compared to the polished and unified look of GNOME. Therefore I decided to remix my own theme for KDE Plasma, based on the popular Nord color palette.&lt;/p&gt;
&lt;p&gt;Technically, &lt;em&gt;Simply Nord&lt;/em&gt; is not a theme, but rather a collection of themes, kwin scripts, widgets, panels and many more customizations, all combined to provide a beautiful, minimalistic desktop experience. I drew inspiration from GNOME, Dank Material Theme, and multiple desktop designs, recreating a keyboard-driven workflow that blends productivity and aesthetics.&lt;/p&gt;
&lt;p&gt;&lt;img src=&quot;https://norb.space/_image?href=%2F_astro%2Fdesktop.CuVo6_j3.jpg&amp;#x26;w=3200&amp;#x26;h=2000&amp;#x26;f=webp&quot; alt=&quot;desktop&quot;&gt;&lt;/p&gt;
&lt;p&gt;{{ Figure 1: Screenshot from the Simply Nord Theme. }}&lt;/p&gt;
&lt;p&gt;In this blog post, I will share the process of creating &lt;em&gt;Simply Nord&lt;/em&gt;, so that you can easily set it up on your KDE Plasma desktop.&lt;/p&gt;
&lt;h2&gt;Designing the Workflow&lt;/h2&gt;
&lt;p&gt;Looks are important, but what is more important is the &lt;strong&gt;workflow&lt;/strong&gt; behind the design. It is always important to take a while to think about how you &lt;strong&gt;wish to access&lt;/strong&gt; the myriad of applications and actions provided by the desktop environment. Think about how you want to launch applications, access opened windows, switch between workspaces, and so on. Be consistent in your design, and use your desired workflow to guide your design decisions.&lt;/p&gt;
&lt;p&gt;&lt;img src=&quot;workflow-light.svg&quot; alt=&quot;workflow&quot;&gt;&lt;img src=&quot;workflow-dark.svg&quot; alt=&quot;workflow&quot;&gt;&lt;/p&gt;
&lt;p&gt;{{ Figure 2: The workflow diagram of the Simply Nord Theme. }}&lt;/p&gt;
&lt;p&gt;For &lt;em&gt;Simply Nord&lt;/em&gt;, the workflow I chose is aggressively keyboard-driven. I got rid of the task manager altogether, and chose to rely on KRunner to launch applications and the Window Switcher to switch between opened windows. I work on a laptop, and frequently do not use a mouse, so I extended the touchpad gestures. I use three-finger swipes to open the Overview Effect, and four-finger swipes to switch between workspaces. The strong emphasis on keyboard and touchpad navigation allowed me to create a clean and minimalistic desktop.&lt;/p&gt;
&lt;p&gt;If you are interested in adopting the theme, but are uncomfortable with a keyboard-driven workflow, you should consider adding a task manager and modifying the shortkeys to your liking. This is the primary reason why I chose to produce a blog tutorial instead of a ready-to-use, drop-and-go konsave archive.&lt;/p&gt;
&lt;h2&gt;First Things First: Backup Your Theme&lt;/h2&gt;
&lt;p&gt;Before you start modifying your desktop, it is always a good idea to make a backup of your current configuration. The latest edition of KDE Plasma already provides built-in support for exporting and importing desktop configurations, but for complex setups like this I prefer to use &lt;a href=&quot;http://github.com/Prayag2/konsave&quot;&gt;Konsave&lt;/a&gt; since it is more flexible.&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-bash&quot;&gt;konsave --save kde-standard
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;After this, you should be ready to start modifying your desktop. You can always restore the backup using the following command:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-bash&quot;&gt;konsave --apply kde-standard
&lt;/code&gt;&lt;/pre&gt;
&lt;h2&gt;Playing with Panels&lt;/h2&gt;
&lt;p&gt;&lt;img src=&quot;https://norb.space/_image?href=%2F_astro%2Fpanels.QVw6pBsr.jpg&amp;#x26;w=3199&amp;#x26;h=1770&amp;#x26;f=webp&quot; alt=&quot;panels&quot;&gt;&lt;/p&gt;
&lt;p&gt;In KDE Plasma, the default panel closely resembles the Windows taskbar. It spans the entire width of the screen and contains primarily a task manager as well as the system tray. Classic design as it is, it does not fit the modern and minimalistic look I was going for. Therefore, I moved the panel to the top of the screen, and turned it into a top bar. You can do so by right-clicking on the panel, selecting &lt;em&gt;Show Panel Configuration&lt;/em&gt; and modifying the settings to:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Position: Top&lt;/li&gt;
&lt;li&gt;Visibility: Always Visible&lt;/li&gt;
&lt;li&gt;Floating: Disabled&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;The last two configuration options disable the &quot;hiding&quot; behavior of the panel, making it always visible. The default panel is a bit too thick, so I reduced the height to 25 pixels.&lt;/p&gt;
&lt;p&gt;&lt;img src=&quot;top-bar-light.svg&quot; alt=&quot;top-bar&quot;&gt;&lt;img src=&quot;top-bar-dark.svg&quot; alt=&quot;top-bar&quot;&gt;&lt;/p&gt;
&lt;p&gt;As for widgets, I removed the task manager and displayed the time in the center, using &lt;code&gt;ddd d MMM,&lt;/code&gt; to display the date besides the time within the same line. The leftside &lt;code&gt;Toggle Overview&lt;/code&gt; button is from the &lt;a href=&quot;https://store.kde.org/p/2132554&quot;&gt;KDE Store&lt;/a&gt; with a custom icon.&lt;/p&gt;
&lt;h3&gt;Addressing the Animation Issue&lt;/h3&gt;
&lt;p&gt;One issue I encountered is that, after removing all task managers from the screen, the &lt;code&gt;Squash&lt;/code&gt; animation for minimizing windows stops working. I have reported this as a &lt;a href=&quot;https://bugs.kde.org/show_bug.cgi?id=516180&quot;&gt;bug&lt;/a&gt;, and hopefully in a future update this will be fixed.&lt;/p&gt;
&lt;p&gt;In the meantime, you can use &lt;a href=&quot;https://github.com/RayZh-hs/Squash-Plus/&quot;&gt;my fork of Squash Plus&lt;/a&gt; which provides a &quot;Minimize To&quot; configuration option. You can set this to any of the fixed positions on the screen, which fixes this issue.&lt;/p&gt;
&lt;p&gt;In my case, I set it to the top left corner, which is where the &lt;code&gt;Toggle Overview&lt;/code&gt; button is located, and where the magic corner is set to toggle the overview too. This way it seems that windows are minimized to the Overview screen, which makes for consistency in the design.&lt;/p&gt;
&lt;h2&gt;Hotkeys and Touchpad Gestures&lt;/h2&gt;
&lt;p&gt;KDE Desktop provides a powerful system for configuring hotkeys under the &quot;Keyboard &gt; Shortcuts&quot; settings. The configuration I am using:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;code&gt;Alt + Space&lt;/code&gt;: Open KRunner&lt;/li&gt;
&lt;li&gt;&lt;code&gt;Alt + F4&lt;/code&gt;: &lt;a href=&quot;https://store.kde.org/p/2138867&quot;&gt;Close Window or Log Out&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;Meta + E&lt;/code&gt;: Open Dolphin&lt;/li&gt;
&lt;li&gt;&lt;code&gt;Meta + C&lt;/code&gt;: Open Chrome&lt;/li&gt;
&lt;li&gt;&lt;code&gt;Meta + K&lt;/code&gt;: Open Konsole&lt;/li&gt;
&lt;li&gt;Additional KWin Script: Geometry Change&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;However, the almighty KDE Plasma seems to be in surprising lack of touchpad customization options. To solve this issue I resorted to using &lt;a href=&quot;https://github.com/taj-ny/InputActions&quot;&gt;Input Actions&lt;/a&gt;, a community-developed tool that provides cross-desktop support for input device triggers. Here is the script I am using to configure the touchpad gestures:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-yaml&quot;&gt;touchpad:
  gestures:
    # Three-finger swipe to cycle between tabs
    - type: swipe
      fingers: 3
      direction: left_right
      actions:
        # 1. Start: Hold Alt and tap Tab once to open the menu
        - on: begin
          input:
            - keyboard: [ +leftalt, tab ]

        # 2. Moving Left: Cycle backwards (Shift+Tab)
        - on: update
          interval: -75
          input:
            - keyboard: [ leftshift+tab ]

        # 3. Moving Right: Cycle forwards (Tab)
        - on: update
          interval: 75
          input:
            - keyboard: [ tab ]

        # 4. End: Release Alt to select the window
        - on: end
          input:
            - keyboard: [ -leftalt ]

        # 5. Cancel: Release Alt if gesture is interrupted
        - on: cancel
          input:
            - keyboard: [ -leftalt ]

    # 3 Fingers Up -&gt; Spreadsheet View (Desktop Grid)
    - type: swipe
      fingers: 3
      direction: up
      actions:
        - on: begin
          input:
            - keyboard: [ leftmeta+w ]

    # Wait for 3 finger down to exit spreadsheet view
    - type: swipe
      fingers: 3
      direction: down
      actions:
        - on: begin
          input:
            - keyboard: [ leftmeta+w ]

    # 4 Fingers Up -&gt; Task View (Desktop Grid)
    - type: swipe
      fingers: 4
      direction: up
      actions:
        - on: begin
          input:
            - keyboard: [ leftmeta+g ]
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;This configuration modifies the three-finger swipe to open the Overview Effect, and the four-finger swipe to switch between workspaces. You can modify the configuration to your liking, and add more gestures if you wish.&lt;/p&gt;
&lt;h2&gt;Color Theme Support&lt;/h2&gt;
&lt;blockquote&gt;
&lt;p&gt;Light attracts bugs. Dark attracts developers.&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;I set out by browsing the KDE Store for &lt;a href=&quot;https://www.nordtheme.com/&quot;&gt;Nord&lt;/a&gt; color schemes. Several options are available, but I ended up choosing &lt;a href=&quot;https://store.kde.org/p/1801641&quot;&gt;Nordic Bluish KDE&lt;/a&gt; theme for most setups:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Colors: Nordic Bluish&lt;/li&gt;
&lt;li&gt;Application Style: Kvantum (I will cover this later)&lt;/li&gt;
&lt;li&gt;Plasma Style: Nordic Bluish&lt;/li&gt;
&lt;li&gt;Window Decorations: Nordic&lt;/li&gt;
&lt;li&gt;Icons: Breeze Dark (See more in &lt;a href=&quot;#icons&quot;&gt;Icons&lt;/a&gt;)&lt;/li&gt;
&lt;li&gt;Cursors: Nordic-Cursors&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Compared with builtin color theme support, I prefer to use &lt;a href=&quot;https://github.com/tsujan/Kvantum&quot;&gt;Kvantum&lt;/a&gt; to provide better support for qt applications, since it comes with enhanced flexibility and capability. &lt;a href=&quot;https://github.com/HimDek/Utterly-Nord-Plasma&quot;&gt;Utterly Nord Plasma&lt;/a&gt; is a qt theme with great support for acrylic and blur effects, and is the one I am using for &lt;em&gt;Simply Nord&lt;/em&gt;. I especially like its dropdown menu design, which is brings out the subtle use of colors in the Nord palette.&lt;/p&gt;
&lt;p&gt;&lt;img src=&quot;https://norb.space/_image?href=%2F_astro%2Fdropdown-menu.TFywa2YJ.png&amp;#x26;w=1579&amp;#x26;h=1085&amp;#x26;f=webp&quot; alt=&quot;dropdown-menu&quot;&gt;&lt;/p&gt;
&lt;p&gt;Ensure that you have &quot;Blur&quot; enabled in &quot;Desktop Effects&quot; settings. It should be pointed out that that the default Utterly Nord Plasma theme is rather aggressively transparent, which makes some windows such as Kate and Dolphin look weird on light backgrounds. The workaround I found is to use the &quot;Kvantum Solid&quot; variation of the theme and, under Application Themes, apply it to windows that are frequently moved above light backgrounds with the &quot;Application Themes&quot; section in Kvantum.&lt;/p&gt;
&lt;p&gt;Another tip: I found that the Nordic Bluish color scheme in Dolphin is a bit unclear. The solution is to &quot;Settings &gt; Configure &gt; Window Color Scheme&quot; and use another. I strongly recommend &lt;a href=&quot;https://store.kde.org/p/1633673&quot;&gt;Nordic Darker&lt;/a&gt;, which looks crisp and clear with the default white Breeze Icons.&lt;/p&gt;
&lt;h2&gt;Wallpaper and Screens&lt;/h2&gt;
&lt;p&gt;The wallpaper that I am using comes from a &lt;a href=&quot;https://www.reddit.com/r/wallpaper/comments/106xrta/1920x1080_nordic_mountains/&quot;&gt;Reddit post&lt;/a&gt;. It should be noted that for some reason the image you download from the post is of 4717×2984 resolution despite it being said to be 1920×1080. For best quality you can downscale it to 1080p and then upscale it back to 4K using a tool like &lt;a href=&quot;https://github.com/upscayl/upscayl&quot;&gt;Upscayl&lt;/a&gt;. This will significantly reduce blur. Or, simply click the link below to download the wallpaper that I upscaled:&lt;/p&gt;
&lt;p&gt;&lt;a href=&quot;/download/simply-nord-wallpaper-4x.png&quot; title=&quot;attachment&quot;&gt;Upscaled Wallpaper&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;Remember to set the wallpaper in &lt;strong&gt;two places&lt;/strong&gt;: the &quot;Login Screen (SDDM)&quot; and the &quot;Screen Locking&quot; settings. Set both to &quot;Breeze&quot; and use the configuration button to set the wallpaper to the one you downloaded.&lt;/p&gt;
&lt;h2&gt;Icons&lt;/h2&gt;
&lt;p&gt;Icons are arguably the hardest part to configure in a typical KDE Plasma desktop. Many of the icon sets that I found seem to be incomplete, and using them leads to visual inconsistencies. Therefore, I settled on using the default Breeze Dark icons as the base, and building icon overrides across applications.&lt;/p&gt;
&lt;p&gt;It turns out that KRunner has a dedicated &lt;code&gt;~/.config/krunnerrc&lt;/code&gt; configuration file, where you can specify icon sets to use for KRunner specifically. For the nord theme, both &lt;a href=&quot;https://github.com/zayronxio/Zafiro-icons&quot;&gt;Zafiro Icons&lt;/a&gt; and its fork &lt;a href=&quot;https://github.com/zayronxio/Zafiro-Nord-Dark&quot;&gt;Zafiro Nord Dark&lt;/a&gt; works perfectly. You can use them by cloning them (or cloning and copying the &lt;code&gt;Dark&lt;/code&gt; folder) into &lt;code&gt;~/.local/share/icons/&lt;/code&gt; and setting:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;[Icons]
Theme=Zafiro-Nord-Dark
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;However, I noticed that both icon sets modify the top search bar of KRunner, which I dislike. To resolve this issue, we will build a custom icon set that inherits the zafiro icons but overrides the search bar icons.&lt;/p&gt;
&lt;p&gt;&lt;img src=&quot;https://norb.space/_image?href=%2F_astro%2Fkrunner.CE3bADzJ.png&amp;#x26;w=2042&amp;#x26;h=1163&amp;#x26;f=webp&quot; alt=&quot;krunner&quot;&gt;
{{ Figure 3: The KRunner with modified Zafiro Icon Set. }}&lt;/p&gt;
&lt;p&gt;To this end, we would first need to figure out the names of these icons used. This is surprisingly difficult, since I have not found any builtin tool in KDE Plasma to inspect icons. After much research, I found &lt;a href=&quot;https://github.com/etherfield/breeze-icons-debug&quot;&gt;Breeze Icons Debug&lt;/a&gt; by etherfield, an icon set that replaces all icons with an image consisting of the icon id. This allowed me to easily identify the icons used in the KRunner search bar, and I built this custom icon set:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-plaintext&quot;&gt;~/.local/share/icons/KRunner-Icons-Override
├── actions
│   ├── 24
│   │   ├── edit-clear-locationbar-rtl.svg
│   │   └── search.svg
│   └── 48
│       ├── configure.svg
│       ├── expand.svg
│       ├── question.svg
│       └── window-pin.svg
├── append
└── index.theme
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The &lt;code&gt;index.theme&lt;/code&gt; file is as follows:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-ini&quot;&gt;[Icon Theme]
Name=Simply Nord: KRunner Icons Override
Comment=KRunner override icons for theme Simply Nord
Inherits=Zafiro-Nord-Dark,breeze-dark

Directories=actions/24,actions/48

[actions/24]
Size=24
Context=Actions
Type=Fixed

[actions/48]
Size=48
Context=Actions
Type=Fixed
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;In the directories, I copied icons sampled from the Breeze Dark icon set. I will provide the python I used to extract these icons and follow symbolic links:&lt;/p&gt;
&lt;p&gt;&lt;a href=&quot;/download/icon-append.py&quot; title=&quot;attachment&quot;&gt;Python Script for Icon Extraction &amp;#x26; Append&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;As you have seen in Image 3, the search bar icons are now overridden to match the Breeze Dark icons, while the rest of the KRunner icons are inherited from the Zafiro Nord Dark icon set. The result is a vastly more consistent and polished look for KRunner.&lt;/p&gt;
&lt;p&gt;Similarly, you can override the folder icons in Dolphin to adjust the colored folders to match the Nord palette.&lt;/p&gt;
&lt;h2&gt;Final Touch: Fonts&lt;/h2&gt;
&lt;p&gt;Fonts matter. People often complain about the default fonts in Linux look ... well, a bit outdated, but this problem is absolutely solvable. First, install fonts of your choice. I am using &lt;a href=&quot;https://www.google.com/get/noto/&quot;&gt;Noto Sans&lt;/a&gt; for user interface, and &lt;a href=&quot;https://www.jetbrains.com/lp/mono/&quot;&gt;JetBrains Mono&lt;/a&gt; for monospace usage like in terminals. When it comes to Asian typography, &lt;a href=&quot;https://source.typekit.com/source-han-serif/&quot;&gt;Source Hans Serif&lt;/a&gt; is my primary choice for documents.&lt;/p&gt;
&lt;p&gt;Change the font in the digital clock in the top bar to 8pt Noto Sans. See how much that affects the overall feel of the desktop? We consider fonts as the final touch to the desktop configuration, since they make up much of the micro-details that make the desktop brilliant.&lt;/p&gt;
&lt;p&gt;Font management in linux can be tricky, since what you ask for is not always what you get. For example, the famous &quot;Arial&quot; font is actually replaced by &quot;Liberation Sans&quot; in most Linux distributions.&lt;/p&gt;
&lt;p&gt;For me, I noticed that the default font system used &quot;FreeSans&quot; for symbol glyphs, which led to issues where the arrow symbol is not vertically centered and so on. Whenever you encounter such issues, you can use the &lt;code&gt;fontconfig&lt;/code&gt; system to set up font fallbacks and overrides.&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;fc-match -s &quot;sans-serif:charset=2192&quot; | head
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;This will show you the font fallback chain for the arrow symbol (modify 2192 to the codepoint of the symbol you are looking up). To resolve such issues, create your own configuration files in &lt;code&gt;~/.config/fontconfig/conf.d/&lt;/code&gt; and specify font fallback rules. One trick that I found was that in most circusmstances, Noto Sans Symbols seemed to have better glyph design.&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-xml&quot;&gt;&amp;#x3C;?xml version=&apos;1.0&apos;?&gt;
&amp;#x3C;!DOCTYPE fontconfig SYSTEM &apos;urn:fontconfig:fonts.dtd&apos;&gt;
&amp;#x3C;fontconfig&gt;
 &amp;#x3C;!-- Strip Noto Sans Symbols of Latin characters --&gt;
 &amp;#x3C;match target=&quot;scan&quot;&gt;
  &amp;#x3C;test name=&quot;family&quot;&gt;
   &amp;#x3C;string&gt;Noto Sans Symbols&amp;#x3C;/string&gt;
  &amp;#x3C;/test&gt;
  &amp;#x3C;edit mode=&quot;assign&quot; name=&quot;charset&quot;&gt;
   &amp;#x3C;minus&gt;
    &amp;#x3C;name&gt;charset&amp;#x3C;/name&gt;
    &amp;#x3C;charset&gt;
     &amp;#x3C;range&gt;
      &amp;#x3C;int&gt;0x0030&amp;#x3C;/int&gt;
      &amp;#x3C;int&gt;0x0039&amp;#x3C;/int&gt;
     &amp;#x3C;/range&gt;
     &amp;#x3C;!-- 0-9 --&gt;
     &amp;#x3C;range&gt;
      &amp;#x3C;int&gt;0x0041&amp;#x3C;/int&gt;
      &amp;#x3C;int&gt;0x005A&amp;#x3C;/int&gt;
     &amp;#x3C;/range&gt;
     &amp;#x3C;!-- A-Z --&gt;
     &amp;#x3C;range&gt;
      &amp;#x3C;int&gt;0x0061&amp;#x3C;/int&gt;
      &amp;#x3C;int&gt;0x007A&amp;#x3C;/int&gt;
     &amp;#x3C;/range&gt;
     &amp;#x3C;!-- a-z --&gt;
    &amp;#x3C;/charset&gt;
   &amp;#x3C;/minus&gt;
  &amp;#x3C;/edit&gt;
 &amp;#x3C;/match&gt;
 &amp;#x3C;!-- When querying sans-serif fonts, resort to Noto Sans Symbols for symbols --&gt;
 &amp;#x3C;match target=&quot;pattern&quot;&gt;
  &amp;#x3C;test name=&quot;family&quot; qual=&quot;any&quot;&gt;
   &amp;#x3C;string&gt;Arial&amp;#x3C;/string&gt;
  &amp;#x3C;/test&gt;
  &amp;#x3C;edit binding=&quot;strong&quot; mode=&quot;prepend&quot; name=&quot;family&quot;&gt;
   &amp;#x3C;string&gt;Noto Sans Symbols&amp;#x3C;/string&gt;
  &amp;#x3C;/edit&gt;
 &amp;#x3C;/match&gt;
 &amp;#x3C;match target=&quot;pattern&quot;&gt;
  &amp;#x3C;test name=&quot;family&quot; qual=&quot;any&quot;&gt;
   &amp;#x3C;string&gt;Roboto&amp;#x3C;/string&gt;
  &amp;#x3C;/test&gt;
  &amp;#x3C;edit binding=&quot;strong&quot; mode=&quot;prepend&quot; name=&quot;family&quot;&gt;
   &amp;#x3C;string&gt;Noto Sans Symbols&amp;#x3C;/string&gt;
  &amp;#x3C;/edit&gt;
 &amp;#x3C;/match&gt;
 &amp;#x3C;match target=&quot;pattern&quot;&gt;
  &amp;#x3C;test name=&quot;family&quot; qual=&quot;any&quot;&gt;
   &amp;#x3C;string&gt;sans-serif&amp;#x3C;/string&gt;
  &amp;#x3C;/test&gt;
  &amp;#x3C;edit binding=&quot;strong&quot; mode=&quot;prepend&quot; name=&quot;family&quot;&gt;
   &amp;#x3C;string&gt;Noto Sans Symbols&amp;#x3C;/string&gt;
  &amp;#x3C;/edit&gt;
 &amp;#x3C;/match&gt;
 &amp;#x3C;dir&gt;~/.local/share/fonts&amp;#x3C;/dir&gt;
&amp;#x3C;/fontconfig&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;This script effectively applys Noto Sans Symbols as the fallback font for all sans-serif symbol fonts. We need to strip Noto Sans Symbols of Latin characters since it contains such a sample sequence, which will otherwise take precedence over fonts the browser queries for letters.&lt;/p&gt;
&lt;p&gt;Each time you modify the font configuration, make sure to clear the font cache:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-bash&quot;&gt;fc-cache -f
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Take some time to experiment with fonts in all your applications, and make sure that each modification you make does not introduce new issues in font rendering. This will help you achieve a consistent and polished look across your desktop.&lt;/p&gt;
&lt;p&gt;That&apos;s all! When you are done, you should have a beautiful, minimalistic KDE Plasma desktop ready to go. Happy theming!&lt;/p&gt;</content:encoded><h:img src="/_astro/thumbnail.xN6SHI3V.jpg"/><enclosure url="/_astro/thumbnail.xN6SHI3V.jpg"/></item><item><title>Getting Modular</title><link>https://norb.space/blog/getting-modular</link><guid isPermaLink="true">https://norb.space/blog/getting-modular</guid><description>Design notes on mango submodules.</description><pubDate>Thu, 30 Oct 2025 00:00:00 GMT</pubDate><content:encoded>&lt;p&gt;https://github.com/Mango-CLI/Mango&lt;/p&gt;
&lt;h2&gt;TL;DR&lt;/h2&gt;
&lt;p&gt;Mango has got submodule support at last. Update, and enjoy.&lt;/p&gt;
&lt;hr&gt;
&lt;h2&gt;What This Blog is About&lt;/h2&gt;
&lt;p&gt;If you are seeking information about the recent submodule update, or wish to learn how to use it, you can utilize the new builtin &lt;code&gt;mango @help&lt;/code&gt; command to look up detailed information about all commands.&lt;/p&gt;
&lt;p&gt;If you wish to develop your own templates and submodules, you will find documentation in the README.md file, and a multitude of demo templates in the &lt;a href=&quot;https://github.com/Mango-CLI/builtins.mango&quot;&gt;builtins repo&lt;/a&gt; should serve as a great starting point. A detailed online documentation entailing all mango features is planned, as will be released soon.&lt;/p&gt;
&lt;p&gt;So why am I writing this blog?&lt;/p&gt;
&lt;p&gt;The simple answer is, I wish to document the process I went through designing submodules and templates for Mango. &lt;strong&gt;Each step of design, each choice taken or reverted bear careful analysis and reasoning.&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;So this blog is not going to be a detailed tour on how to use submodules. Instead, it will guide you through a journey behind and beyond the mango submodule system you see now. If you want to dive deeper, and truly understand modularity in mango, you have come to the right place. If it so happens that you want to introduce modularity or reusability to one of your brilliant projects, you may also find some inspiration here.&lt;/p&gt;
&lt;p&gt;So sit back, and let&apos;s begin.&lt;/p&gt;
&lt;h2&gt;Craving Modularity&lt;/h2&gt;
&lt;p&gt;For me, one common use case of Mango is managing my schoolwork. Most of the time we rely on the famous Canvas Infrastructure, and I found myself repeatedly downloading assignments, doing them locally, and uploading them back. Mango helps me automate this process.&lt;/p&gt;
&lt;p&gt;The problem is, different courses have different requirements, and I had to write the same canvas-handling code over and over again, which is tedious and annoying. I wished for a method to reuse the code across different mango repos.&lt;/p&gt;
&lt;p&gt;Another point that I came across is that, in the old mango system, there is no good way to update the mango builtin commands. Users are free to roam the home repo and modify any file. In fact, they are encouraged to tailor their home mango to fit their own needs. Yet, this freedom comes at a cost: there is no way to systematically update the home repo, as rebooting the home repo will remove all user-defined scripts within.&lt;/p&gt;
&lt;p&gt;Mango prides itself on being an extremely lightweight yet flexible script manager, yet the simplicity of the old mango system has placed a limit on its extensibility. &lt;strong&gt;A simple, tiled script layout is easy to understand, but hard to extend.&lt;/strong&gt; There was in truth no good way to manage dependencies between scripts, or to publish reusable code.&lt;/p&gt;
&lt;h2&gt;A Vague Idea&lt;/h2&gt;
&lt;p&gt;So I started by asking myself:&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;What does modularity mean to Mango? What should it look like?&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;The first question is easy to answer: I want to incorporate a selection of &quot;reusable code&quot; from other repos, or other creators, into my own mango repo. This reusable code is either a dependency for my own code, or a set of commands that I want to use directly in this context. Unlike my own commands, such a segment should instead be &lt;strong&gt;externally maintained, and easily updatable&lt;/strong&gt;.&lt;/p&gt;
&lt;p&gt;The second question is trickier. There were lots of possible designs. What should submodules be? Should they be a a script that enacts a side effect on the mango repo? Or a archive that follows some given convention designed to provide functionality to the repo? How can the user access this reusable codebase? How should submodules be managed and updated?&lt;/p&gt;
&lt;p&gt;There is no end to the questions to be answered, so I started out by investigating existing modularity systems.&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Git Submodules: Git allows users to include other git repos as subdirectories of their own repo. They can &quot;clone&quot; others&apos; repos directly into their own, facilitating code reuse. There is no &quot;central hub&quot; for git submodules, and users manage submodules per project. Git keeps track of submodules and their versions in a special &lt;code&gt;.gitmodules&lt;/code&gt; file.&lt;/li&gt;
&lt;li&gt;Linux Package Managers: Most linux systems ship with package managers, which provide a unified method to extend the os with third party software. Packages are served from central repositories, and are locally bookmarked strictly by name and version. They are installed in autonomous directories, and exposed to the user via binaries in the system PATH.&lt;/li&gt;
&lt;li&gt;Vim Extensions: Vim has a rich ecosystem of extensions, and numerous famous extension managers. Extensions are usually placed in a special directory. Their functionality comes from following directory structures.&lt;/li&gt;
&lt;li&gt;Python packages: Python also uses naming conventions and directory structures to define packages. They can be installed from central repositories, or can be user-defined.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;So the problem boils down to several key design choices:&lt;/p&gt;
&lt;blockquote&gt;
&lt;ol&gt;
&lt;li&gt;Is there a central repository for submodules, or is it decentralized?&lt;/li&gt;
&lt;li&gt;What format should a submodule take?&lt;/li&gt;
&lt;li&gt;Where should submodules be installed? In a central directory, or scattered project-wise? If the latter is true, how can we keep track of them?&lt;/li&gt;
&lt;/ol&gt;
&lt;/blockquote&gt;
&lt;p&gt;The first question is simple. The topmost merit of having a central repository is security. However, it places a great limit on the freedom of publishers. Mango, as a user-driven script management tool, should make it easy for users to create and share content. &lt;strong&gt;Users should decide what they trust and what to use in their own mango repos.&lt;/strong&gt; Therefore, I decided to go with a decentralized design. (Besides, I do not have the resources to maintain a central repository.)&lt;/p&gt;
&lt;p&gt;The second question I found inspiration from git. Any git repo can be a submodule, which creates an isomorphism between git submodules and git repos. This is great design, as it allows for greater flexibility, as well as minimal workload for the developer to translate existing repos into submodules.&lt;/p&gt;
&lt;p&gt;The last problem is indeed the trickiest. After much deliberation, I decided to go with a project-wise submodule system, since versioning may vary from project to project. Yet I found it pretty hard to decide on a single design. Here are some ideas put forward during the design process:&lt;/p&gt;
&lt;p&gt;Regarding the construct of submodules:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;The git repo itself is the &lt;code&gt;.mango&lt;/code&gt; folder renamed. It contains &lt;code&gt;.instructions&lt;/code&gt; and all other files.&lt;/li&gt;
&lt;li&gt;The git repo wraps around the &lt;code&gt;.mango&lt;/code&gt; folder, so &lt;code&gt;.git&lt;/code&gt; and &lt;code&gt;.mango&lt;/code&gt; are parallel directories. This enables users to place other files outside the mango implementation, like a README.md and a license file.&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;Regarding where to place submodules and how to track them:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;Submodules are scattered across the repo like that in git. A special file (e.g. &lt;code&gt;.submodules&lt;/code&gt;) that lists the relative directory of each submodule, relative to the .mango folder.&lt;/li&gt;
&lt;li&gt;The submodule directories placed at top level, and keep a *.submodule format.&lt;/li&gt;
&lt;li&gt;The submodule directories placed inside a special &lt;code&gt;.submodules&lt;/code&gt; folder.&lt;/li&gt;
&lt;li&gt;Submodules are not statically documented. Users use paths to invoke them, like using &lt;code&gt;mango path/to/submodule:command&lt;/code&gt;. There is no way short of using brute force to list them though.&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;Primarily, I chose option 1 for construct, and option 3 for placement and tracking.&lt;/p&gt;
&lt;h2&gt;The Price of a Flat Design&lt;/h2&gt;
&lt;blockquote&gt;
&lt;p&gt;A chance of isomorphism should never be missed in architecture design.&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;I chose these two because they yield a relatively flat file system, without unnecessary time overhead. Thus I reasoned to myself: Mango is designed to be simple, and by far all mango repos adhere to being a flat structure, making it simpler for users to understand, navigate and edit. Unlike git, where outside commands handle the &lt;code&gt;.mango&lt;/code&gt; folder, users should be encouraged, if not required, to directly interact with the mango folder. It is for this reason that I placed great emphasis on keeping directory hierarchies flat and understandable.&lt;/p&gt;
&lt;p&gt;However, just when I was about to finish implementing the submodule system, I realized some issues with this design.&lt;/p&gt;
&lt;p&gt;First and foremost, this design makes it hard to build submodules. When working inside a directory that &lt;em&gt;would be&lt;/em&gt; cloned into as a submodule, the original mango method of searching up the directory tree for &lt;code&gt;.mango&lt;/code&gt; folders would fail to capture the repo, thus disabling users from utilizing &lt;code&gt;mango&lt;/code&gt; to test their work. This can be mitigated by checking not only &lt;code&gt;.mango&lt;/code&gt; directories but also &lt;code&gt;.instructions&lt;/code&gt; files in the mango cli tool, but it seems rather unintuitive and dirty.&lt;/p&gt;
&lt;p&gt;Worse still, there is no reliable and programmatic way to call submodules from submodules, due to the unavailability of the &lt;code&gt;mango&lt;/code&gt; command inside submodule directories. This is a serious limitation, as it prevents users from building more complex submodule systems.&lt;/p&gt;
&lt;p&gt;The problems, in essence, arises from an asymmetry between &lt;code&gt;.mango&lt;/code&gt; folders and submodule ones. The former is recognized by the name, the latter by a position. The lack of isomorphism here created unintended complexities when it comes to development and deployment.&lt;/p&gt;
&lt;p&gt;When designing systems, one should seek consistency and isomorphism wherever possible. This is a lesson learned the hard way. &lt;strong&gt;A chance of isomorphism should never be missed in architecture design.&lt;/strong&gt;&lt;/p&gt;
&lt;h2&gt;A Better Design&lt;/h2&gt;
&lt;p&gt;After much deliberation, I chose to adopt a better, more consistent design, at the price of more deeply nested directories. We will come to this design choice later.&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;~/.mango
├── .instructions
├── .submodules
│   └── builtins
|       ├── .mango
|       |   ├── .templates.registry
|       |   |   └── (template folders)
│       |   ├── .instructions
│       |   ├── .on-add
│       |   ├── .on-add.literal
|       |   ├── .on-install
│       |   └── (builtin mango commands)
|       └── (license and readme files)
├── .templates.registry
|   └── (user defined template folders)
├── .submodules.registry
|   └── (user defined submodules)
└── (user defined mango scripts)
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;In this design, each submodule &lt;strong&gt;is&lt;/strong&gt; a repo, and the &lt;code&gt;.mango&lt;/code&gt; directory resides inside the submodule folder, alongside other files like README.md and LICENSE. This generates a powerful isomorphism that any mango repo is a valid submodule, and any submodule is a valid mango repo. This greatly simplifies development and testing for submodules, and developers can now use &lt;code&gt;mango&lt;/code&gt; commands directly inside submodule directories to access their submodules.&lt;/p&gt;
&lt;p&gt;When submodules are installed, they are git cloned into the special &lt;code&gt;.submodules&lt;/code&gt; folder, and a &lt;code&gt;.on-install&lt;/code&gt; hook is called to perform any additional setup. This design keeps submodules organized and lightweight. Henceforth, submodules are updated using git pull, and removed by simply deleting their folders, no side effects.&lt;/p&gt;
&lt;p&gt;Besides flexibility, another reason I deemed the design a fair tradeoff is that creators should be &lt;strong&gt;discouraged&lt;/strong&gt; from directly editing and referencing submodule files. Come to think of it, submodules are supposed to expose API endpoints for users to call, not files to edit. By nesting them deeper in the directory tree, we can place a deliberate syntax barrier between users and submodule files, encouraging them to use &lt;code&gt;mango&lt;/code&gt; commands as an intermediate layer to access submodules&apos; functionality. This effect, while not intended at first, is indeed beneficial to the overall user experience.&lt;/p&gt;
&lt;h2&gt;What About Templates?&lt;/h2&gt;
&lt;p&gt;So far we have all been talking about submodules, but what about templates?&lt;/p&gt;
&lt;p&gt;The answer is simple: In mango, templates are simply just submodules. They follow the same rule, and the only difference is that, typically, the &lt;code&gt;.on-add&lt;/code&gt; hook is defined to &lt;strong&gt;replace the host repo&apos;s .mango directory with the one inside the template&lt;/strong&gt;. That&apos;s it!&lt;/p&gt;
&lt;p&gt;In order for developers to build submodules and templates more easily, I included two builtin templates: the &lt;em&gt;submodule&lt;/em&gt; template and the &lt;em&gt;template&lt;/em&gt; template. Users can use them with &lt;code&gt;mango @init&lt;/code&gt; to quickly scaffold their own submodule or template repos.&lt;/p&gt;
&lt;p&gt;After they have finished developing their submodules or templates, they can either choose to publish them on git hosting services like github, or store them locally. Two registries are maintained in the host mango repo: &lt;code&gt;.submodules.registry&lt;/code&gt; and &lt;code&gt;.templates.registry&lt;/code&gt;. They act as local git remotes for users, including bare clones of the submodule/template repos, and will be searched when users invoke submodules and templates.&lt;/p&gt;
&lt;h2&gt;Conclusion&lt;/h2&gt;
&lt;p&gt;We&apos;re done! After a month of design and development, mango submodules are finally here. We can now use templates and submodules both online and locally, and reuse code across different mango repos. We have setup a consistent and powerful modularity system that adheres to mango&apos;s core philosophy of simplicity and flexibility.&lt;/p&gt;
&lt;p&gt;Since its first release, mango has come a long way. This submodule system is yet another milestone in mango&apos;s journey towards being a creatively-designed script management tool. I hope you would enjoy using it. Best regards, Norb, head of &lt;a href=&quot;https://github.com/Mango-CLI&quot;&gt;Mango-CLI&lt;/a&gt;.&lt;/p&gt;</content:encoded><h:img src="/_astro/thumbnail.DgA94e1T.png"/><enclosure url="/_astro/thumbnail.DgA94e1T.png"/></item><item><title>Introducing MkData</title><link>https://norb.space/blog/introducing-mkdata</link><guid isPermaLink="true">https://norb.space/blog/introducing-mkdata</guid><description>Simple but powerful batch data generator based on Python.</description><pubDate>Tue, 07 Oct 2025 16:00:00 GMT</pubDate><content:encoded>&lt;h2&gt;A Simple Goal&lt;/h2&gt;
&lt;p&gt;It all started a year ago, when, as a freshman majoring computer science, I found myself in constant need of generating large sets of structured data to test algorithms implemented in C++. Most contented themselves with writing small scripts in Python or native C++ to generate random data, but I wanted something more efficient and scalable. I talked with &lt;a href=&quot;https://github.com/RogerFlowey&quot;&gt;RogerFlowey&lt;/a&gt;, and after some heated discussions, MkData was born.&lt;/p&gt;
&lt;p&gt;The idea is simple enough: Programming languages are powerful, yet this power means that for specialized tasks like data generation, boilerplate code is often needed (for instance, to format output). All we need is a flexible &lt;a href=&quot;https://en.wikipedia.org/wiki/Domain-specific_language&quot;&gt;domain-specific language&lt;/a&gt; that can express the structure of the data we want to generate, and a command-line tool (an interpreter) that will generate the data for accordingly.&lt;/p&gt;
&lt;h2&gt;A Journey of Iterations&lt;/h2&gt;
&lt;h3&gt;From GUI to CLI&lt;/h3&gt;
&lt;p&gt;Previously, I had made some attempts at building batch generation tools. The first generation of MkData looks like this:&lt;/p&gt;
&lt;p&gt;&lt;img src=&quot;https://norb.space/_image?href=%2F_astro%2Fmkdata-old.CfMWHZVC.png&amp;#x26;w=3155&amp;#x26;h=1692&amp;#x26;f=webp&quot; alt=&quot;mkdata-old&quot;&gt;&lt;/p&gt;
&lt;p&gt;I was learning frontend at that time, so I used Vue for the UI. It was a fun project, and (more or less) a good-looking one, but being web app limited its practical functionality. I soon realized that a GUI is not ideal for a generator. It makes much more sense to focus on core language features, and expose a command-line interface for users to interact with.&lt;/p&gt;
&lt;p&gt;I decided to rewrite MkData from scratch, this time as a Python package. After installation, it functions as a command-line tool.&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-bash&quot;&gt;pip install mkdata
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Or, with other package managers:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-bash&quot;&gt;pipx install mkdata
uv tool add mkdata
&lt;/code&gt;&lt;/pre&gt;
&lt;h3&gt;Nailing the Syntax&lt;/h3&gt;
&lt;p&gt;Deciding on the syntax of MkData took time. I wanted it to be &lt;strong&gt;as concise as possible&lt;/strong&gt;, without sacrificing extensibility. After several iterations (as a ton of discussions with RogerFlowey), I settled with a half-Pythonic, half-Cpp-ish syntax.&lt;/p&gt;
&lt;p&gt;&lt;img src=&quot;https://norb.space/_image?href=%2F_astro%2Fmkdata-p0-render.BcjjMhD3.png&amp;#x26;w=1956&amp;#x26;h=995&amp;#x26;f=webp&quot; alt=&quot;MkData Syntax Preview&quot;&gt;&lt;/p&gt;
&lt;p&gt;It might not be perfect, but is as close as we could get to our goal. The beautiful thing is that, since MkData is based on Python, it evaluates all expressions and blocks using Python&apos;s own interpreter. This means that &lt;strong&gt;all Python expressions are valid in MkData&lt;/strong&gt;, making it extremely powerful and extensible. There is even a &lt;code&gt;@python&lt;/code&gt; directive that allows you to write arbitrary Python code blocks in MkData files.&lt;/p&gt;
&lt;p&gt;In accordance to C++ redirection, MkData provides a &lt;code&gt;@redirect&lt;/code&gt; directive to redirect output to files, stdout or stderr. In combination with loops, it can be used to generate multiple files in one go.&lt;/p&gt;
&lt;p&gt;One of the most annoying things about writing data generation scripts is formatting output. The formatting syntax in MkData is done solely through appending format specifiers to the end of each enactive statement, like &lt;code&gt;\n&lt;/code&gt; for newline. This makes it easy to read and write.&lt;/p&gt;
&lt;p&gt;The builtins of MkData (like &lt;code&gt;rint&lt;/code&gt; for random int, &lt;code&gt;rstr&lt;/code&gt; for random string), are, in fact. defined in a python script, which is run at launch-time to capture its environment. If future versions, MkData will support user-defined starting scripts, allowing users to define their own builtins. &lt;a href=&quot;https://github.com/RayZh-hs/mkdata/tree/feature/extend-preimport&quot;&gt;A reform of the standard library&lt;/a&gt; is currently underway at the time of writing, which aims to provide extensive utility as well as a uniform interface for builtins.&lt;/p&gt;
&lt;h3&gt;Speeding Up&lt;/h3&gt;
&lt;p&gt;MkData is implemented in Python, which is not the fastest language out there, but we have done our best to optimize it. Before execution, the script is stepped through and blocks are compiled into &lt;a href=&quot;https://stackoverflow.com/questions/22443939/python-built-in-function-compile-what-is-it-used-for&quot;&gt;Python Code Objects&lt;/a&gt;, and after the tree is fully constructed, it is executed in a single pass. This makes MkData fast enough for most use cases.&lt;/p&gt;
&lt;h2&gt;What&apos;s Next?&lt;/h2&gt;
&lt;p&gt;MkData is still a young project, and there are many features requested so far. Some ideas we have in mind include:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Full support for user-defined startup scripts;&lt;/li&gt;
&lt;li&gt;Support for global seeding (implemented in &lt;a href=&quot;https://github.com/RayZh-hs/mkdata/tree/feature/seed&quot;&gt;this branch&lt;/a&gt;);&lt;/li&gt;
&lt;li&gt;Extend the builtin library (see &lt;a href=&quot;https://github.com/RayZh-hs/mkdata/tree/feature/extend-preimport&quot;&gt;this branch&lt;/a&gt;).&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;We have also created a VSCode extension for MkData, providing syntax highlighting and snippets. Feel free to check it out on the &lt;a href=&quot;https://marketplace.visualstudio.com/items?itemName=rayzh.mkdata-intellisense&quot;&gt;VSCode Marketplace&lt;/a&gt;.&lt;/p&gt;
&lt;hr&gt;
&lt;p&gt;Here is the source code for all projects mentioned in this post:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a href=&quot;https://github.com/RayZh-hs/mkdata&quot;&gt;MkData&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://github.com/RayZh-hs/mkdata-intellisense&quot;&gt;MkData Intellisense&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;If you have any questions or suggestions, feel free to open an issue on the MkData repository, or reach out to me via &lt;a href=&quot;mailto:ray_zh@sjtu.edu.cn&quot;&gt;email&lt;/a&gt;. Thank you for reading!&lt;/p&gt;
&lt;p&gt;Attribution: Thumbnail image by Eren Namlı on Unsplash&lt;/p&gt;</content:encoded><h:img src="/_astro/thumbnail.BM7yrBd7.jpg"/><enclosure url="/_astro/thumbnail.BM7yrBd7.jpg"/></item><item><title>Introducing Mango</title><link>https://norb.space/blog/introducing-mango</link><guid isPermaLink="true">https://norb.space/blog/introducing-mango</guid><description>A light-weight script management tool based on the file system.</description><pubDate>Fri, 29 Aug 2025 16:00:00 GMT</pubDate><content:encoded>&lt;p&gt;https://github.com/Mango-CLI/Mango&lt;/p&gt;
&lt;p&gt;Script management has always been vital to productivity in software development. Almost every aspiring developer has in his or her toolkit an arsenal of scripts to automate repetitive tasks, streamline workflows, and boost their everyday work efficiency.&lt;/p&gt;
&lt;p&gt;Yet a global &lt;code&gt;/usr/local/bin&lt;/code&gt; directory is not the solution for scope-specific or project-specific scripts. Most builders (pnpm, gradle, uv, etc.) have their own way for allowing users to define and manage scripts within the context of their projects, leading to a kaleidoscope of approaches towards the same problem. Furthermore, global vs. project-specific is about as fine-grained as you can get for customizing user scripts.&lt;/p&gt;
&lt;h2&gt;What Mango is About&lt;/h2&gt;
&lt;p&gt;And yes, here is where mango comes in: To fill in the gap of unified script management, and to provide a file-system based approach to organizing and running scripts.&lt;/p&gt;
&lt;p&gt;Mango allows you to manage your scripts across projects, using folders as scopes to fine-tune your script organization. This offers flexibility beyond traditional hook-based ways adopted by package managers.&lt;/p&gt;
&lt;h2&gt;Designing Mango&lt;/h2&gt;
&lt;p&gt;The core idea of Mango is incredibly simple. Every folder that contains a &lt;code&gt;.mango&lt;/code&gt; folder is a mango repo. Whenever the user invokes:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-bash&quot;&gt;mango command-name args
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The mango executable searches up the directory tree for a &lt;code&gt;.mango&lt;/code&gt; folder. If such a folder is found, it maps the command name to a script file registered in that mango repo registry. Appending an &lt;code&gt;@&lt;/code&gt; symbol to the command name enables recursive search (mango will search up dir if no match is found in the first mango repo).&lt;/p&gt;
&lt;p&gt;When the user installs mango, a &lt;code&gt;.mango&lt;/code&gt; folder is created in the user&apos;s home directory, making that directory the user&apos;s &lt;strong&gt;home mango&lt;/strong&gt;. Common utilities will be installed in the home mango, making them globally available via &lt;code&gt;mango @command-name&lt;/code&gt;. So yes: Mango is managed by mango!&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-bash&quot;&gt;mango @init --template bash
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;This will make the current repo a mango repo because &lt;code&gt;init&lt;/code&gt; is a command registered in the home mango, linking to a python script that sets up the folder and initializes the mango environment.&lt;/p&gt;
&lt;p&gt;The same is true for &lt;strong&gt;all mango commands&lt;/strong&gt;. They are nothing &lt;em&gt;magic&lt;/em&gt;, just files in the home mango that you can play around or modify. This makes mango extremely extensible and customizable.&lt;/p&gt;
&lt;h2&gt;Why Mango Matters&lt;/h2&gt;
&lt;p&gt;Mango is yet another script management system. It is nothing fancy, but it does address the need for a unified and extensible scripting solution, in a simple yet elegant way. It is designed to be easy to use, easy to extend, and easy to integrate into existing workflows. This is why personally, I deem Mango a welcoming addition to my workflow, and I have been using it ever since I finished the project.&lt;/p&gt;
&lt;p&gt;If you are interested in the Mango project, feel free to check out the &lt;a href=&quot;https://github.com/Mango-CLI/Mango&quot;&gt;GitHub repository&lt;/a&gt;. We welcome contributions, suggestions, and feedback from the community!&lt;/p&gt;
&lt;p&gt;Happy scripting!&lt;/p&gt;</content:encoded><h:img src="/_astro/thumbnail.Ba09MPNR.jpg"/><enclosure url="/_astro/thumbnail.Ba09MPNR.jpg"/></item></channel></rss>