<?xml version="1.0" encoding="utf-8"?><feed xmlns="http://www.w3.org/2005/Atom" ><generator uri="https://jekyllrb.com/" version="3.10.0">Jekyll</generator><link href="https://alleych.github.io/feed.xml" rel="self" type="application/atom+xml" /><link href="https://alleych.github.io/" rel="alternate" type="text/html" /><updated>2025-09-13T02:09:28+00:00</updated><id>https://alleych.github.io/feed.xml</id><title type="html">Hello World :)</title><subtitle>This blog is to journal my experience doing GSoC 2025 @ GNOME</subtitle><author><name>Alley Chaggar</name></author><entry><title type="html">Final Report</title><link href="https://alleych.github.io/gnome/final-report/" rel="alternate" type="text/html" title="Final Report" /><published>2025-09-13T00:00:00+00:00</published><updated>2025-09-13T00:00:00+00:00</updated><id>https://alleych.github.io/gnome/final-report</id><content type="html" xml:base="https://alleych.github.io/gnome/final-report/"><![CDATA[<h1 id="intro">Intro:</h1>
<p>Hi everyone, it’s the end of GSoc! I had a great experience throughout this whole process. I’ve learned so much. 
This is essentially the ‘final report’ for GSoC, but not my final report for this project in general by a long shot. 
I still have so much more I want to do, but here is what I’ve done so far.</p>

<h1 id="project">Project:</h1>
<p>JSON, YAML, and/or XML emitting and parsing integration into Vala’s compiler.</p>

<h1 id="mentor">Mentor:</h1>
<p>I would like to thank <a href="https://mastodon.social/@lw64@chaos.social">Lorenz Wildberg</a> for being my mentor for this project, as well as the <a href="https://mastodon.social/@vala_lang">Vala community</a>.</p>

<h1 id="description">Description:</h1>
<p>The main objective of this project is to integrate direct syntax support for parsing and emitting JSON, XML, and/or YAML formats in Vala. 
 This will cut back the boilerplate code, making it more user-friendly and efficient for developers working with these formatting languages.</p>

<h1 id="what-ive-done">What I’ve done:</h1>
<h2 id="research">Research</h2>
<ul>
  <li>I’ve done significant research in both JSON and YAML parsing and emitting in various languages like C#, Java, Rust and Python.</li>
  <li>Looked into how Vala currently handles JSON using JSON GLib classes, and I then modelled the C code after the examples I collected.</li>
  <li>Modelled the JSON module after other modules in the codegen, specifically, mainly after Dbus, Gvariant, GObject, and GTK.</li>
</ul>

<h2 id="custom-json-overrides-and-attribute">Custom JSON Overrides and Attribute</h2>
<ul>
  <li>Created Vala syntax sugar specifically making a [JSON] attribute to do serialization.</li>
  <li>Built support for custom overrides as in mapping JSON keys to differently named fields/properties.</li>
  <li>Reduced boilerplate by generating C code behind the scenes.</li>
</ul>

<h2 id="structs">Structs</h2>
<ul>
  <li>I’ve created both Vala functions to deserialize and serialize structs using JSON boxed functions.</li>
  <li>I created a Vala  <code class="language-vala highlighter-rouge"><span class="n">generate_struct_serialize_func</span></code> function to create a C code function called  <code class="language-vala highlighter-rouge"><span class="n">_</span><span class="p">%</span><span class="n">s_serialize_func</span></code> to serialize fields.</li>
  <li>
    <p>I then created a Vala function  <code class="language-vala highlighter-rouge"><span class="n">generate_struct_to_json</span></code> to create a C code function called  <code class="language-vala highlighter-rouge"><span class="n">_json_</span><span class="p">%</span><span class="n">s_serialize_mystruct</span></code> to fully serialize the struct by using boxed serialize functions.
<br /></p>
  </li>
  <li>I created a Vala  <code class="language-vala highlighter-rouge"><span class="n">generate_struct_deserialize_func</span></code> function to create a C code function called  <code class="language-vala highlighter-rouge"><span class="n">_</span><span class="p">%</span><span class="n">s_deserialize_func</span></code> to deserialize fields.</li>
  <li>I then created a Vala function  <code class="language-vala highlighter-rouge"><span class="n">generate_struct_to_json</span></code> to create a C code function called  <code class="language-vala highlighter-rouge"><span class="n">_json_</span><span class="p">%</span><span class="n">s_deserialize_mystruct</span></code> to fully deserialize the struct by using boxed deserialized functions.</li>
</ul>

<h2 id="gobjects">GObjects</h2>
<ul>
  <li>I’ve created both Vala functions to deserialize and serialize GObjects using json_gobject_serialize and JSON generator.</li>
  <li>
    <p>I then created a Vala function  <code class="language-vala highlighter-rouge"><span class="n">generate_gclass_to_json</span></code> to create a C code function called  <code class="language-vala highlighter-rouge"><span class="n">_json_</span><span class="p">%</span><span class="n">s_serialize_gobject_myclass</span></code> to fully serialize GObjects.
<br /></p>
  </li>
  <li>I created a Vala  <code class="language-vala highlighter-rouge"><span class="n">generate_gclass_from_json</span></code> function to create a C code function called  <code class="language-vala highlighter-rouge"><span class="n">_json_</span><span class="p">%</span><span class="n">s_deserialize_class</span></code> to deserialize fields.</li>
</ul>

<h2 id="non-gobjects">Non-GObjects</h2>
<ul>
  <li>I’ve done serializing of non-GObjects using JSON GLib’s builder functions.</li>
  <li>I then created a Vala function  <code class="language-vala highlighter-rouge"><span class="n">generate_class_to_json</span></code> to create a C code function called  <code class="language-vala highlighter-rouge"><span class="n">_json_</span><span class="p">%</span><span class="n">s_serialize_myclass</span></code> to fully serialize non-objects that aren’t
inheriting from Object or Json.Serializable.</li>
</ul>

<h1 id="future-work">Future Work:</h1>
<h2 id="research-1">Research</h2>
<ul>
  <li>Research still needs to be put into integrating XML and determining which library to use.</li>
  <li>The integration of YAML and other formatting languages not only JSON, YAML, or XML.</li>
</ul>

<h2 id="custom-overrides-and-attributes">Custom Overrides and Attributes</h2>
<ul>
  <li>I want to create more specialized attributes for JSON that only do serialization or deserialization. Such as [JsonDeserialize] and [JsonSerialize] or something similar.</li>
  <li>[JSON] attribute needs to do both deserializing and serializing, and at the moment, the deserializing code has problems.</li>
  <li>XML, YAML, and other formating languages will follow very similar attribute patterns:  <code class="language-vala highlighter-rouge"><span class="na">[Yaml]</span><span class="p">,</span> <span class="p">[</span><span class="n">Xml</span><span class="p">],</span> <span class="p">[</span><span class="n">Json</span><span class="p">]</span></code>.</li>
</ul>

<h2 id="bugs">Bugs</h2>
<ul>
  <li>unref c code functions are calling nulls, which shouldn’t be the cause. They need proper types going through.</li>
  <li>Deserializing prompts a redefinition that needs to be corrected.</li>
  <li>Overridden GObject properties need to have setters made to be able to get the values.</li>
</ul>

<h1 id="links">Links</h1>
<ul>
  <li>Here is the <a href="https://gitlab.gnome.org/AlleyChaggar/vala/-/blob/098c51eb28c99d4d9fa4786d84109782fe8cf2c3/codegen/valajsonmodule.vala">JSON module</a> that has majority of the code.</li>
  <li>Here is the <a href="https://gitlab.gnome.org/GNOME/vala/-/merge_requests/468">merge request</a>.</li>
</ul>]]></content><author><name>Alley Chaggar</name></author><category term="gnome" /><category term="Vala" /><summary type="html"><![CDATA[Intro: Hi everyone, it’s the end of GSoc! I had a great experience throughout this whole process. I’ve learned so much. This is essentially the ‘final report’ for GSoC, but not my final report for this project in general by a long shot. I still have so much more I want to do, but here is what I’ve done so far.]]></summary></entry><entry><title type="html">Challenges</title><link href="https://alleych.github.io/gnome/challenges/" rel="alternate" type="text/html" title="Challenges" /><published>2025-07-31T07:30:00+00:00</published><updated>2025-07-31T07:30:00+00:00</updated><id>https://alleych.github.io/gnome/challenges</id><content type="html" xml:base="https://alleych.github.io/gnome/challenges/"><![CDATA[<h1 id="debugging-and-my-challenges">Debugging and My Challenges</h1>

<p>For the past two weeks, I’ve been debugging the <a href="https://gitlab.gnome.org/AlleyChaggar/vala/-/blob/098c51eb28c99d4d9fa4786d84109782fe8cf2c3/codegen/valajsonmodule.vala">json module</a>. I hooked up the JSON module into the codebase hierarchy by modifying <a href="https://gitlab.gnome.org/AlleyChaggar/vala/-/blob/main/codegen/valagsignalmodule.vala?ref_type=heads">valagsignalmodule.vala</a> to extend the JSON module, which, before extended the <a href="https://gitlab.gnome.org/GNOME/vala/-/blob/main/codegen/valagobjectmodule.vala?ref_type=heads">GObject module</a>. Running the test case called <a href="https://gitlab.gnome.org/AlleyChaggar/vala/-/blob/alley/json-glib-module/tests/annotations/json.vala?ref_type=heads">json.vala</a>, crashes the program.
<br /></p>

<p>In the beginning, I was having quite the difficulty trying to use <code class="language-vala highlighter-rouge"><span class="n">gdb</span></code> and <code class="language-vala highlighter-rouge"><span class="n">coredumpctl</span></code> to investigate the crash. I kept doing:</p>

<div class="language-vala highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="p">./</span><span class="n">autogen</span><span class="p">.</span><span class="n">sh</span> <span class="p">--</span><span class="n">enable</span><span class="p">-</span><span class="n">debug</span>
<span class="n">make</span> 
</code></pre></div></div>
<p>/</p>
<div class="language-vala highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="p">./</span><span class="n">configure</span> <span class="p">--</span><span class="n">enable</span><span class="p">-</span><span class="n">debug</span>
<span class="n">make</span>
</code></pre></div></div>
<p>Then I’d run commands like:</p>

<div class="language-vala highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="n">coredumpctl</span> <span class="n">gdb</span>
<span class="n">coredumpctl</span> <span class="n">info</span>
</code></pre></div></div>
<p>It simply wasn’t working when I built it this way with the following coredumpctl commands. It wasn’t showing the debug symbols that I needed to be able to see the functions that were causing the program to crash. When I built Vala using GNOME Builder’s build button, it also didn’t work.
<br /></p>

<p>Lorenz, my mentor, helped me a lot on this issue. How we were able to fix this was first, I needed to build Vala by doing</p>

<div class="language-vala highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="p">./</span><span class="n">configure</span> <span class="p">--</span><span class="n">enable</span><span class="p">-</span><span class="n">debug</span>
<span class="n">make</span>
</code></pre></div></div>

<p>Then I needed to run the test in the ‘build terminal’ in GNOME Builder <code class="language-vala highlighter-rouge"><span class="n">compiler</span><span class="p">/</span><span class="n">valac</span> <span class="p">--</span><span class="n">pkg</span> <span class="n">json</span><span class="p">-</span><span class="n">glib</span><span class="p">-</span><span class="m">1.0</span> <span class="n">tests</span><span class="p">/</span><span class="n">annotations</span><span class="p">/</span><span class="n">json</span><span class="p">.</span><span class="n">vala</span></code>
<br /></p>

<p>Then, in a regular terminal, I ran:</p>

<div class="language-vala highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="n">gdb</span> <span class="n">compiler</span><span class="p">/.</span><span class="n">libs</span><span class="p">/</span><span class="n">lt</span><span class="p">-</span><span class="nf">valac</span>
<span class="p">(</span><span class="n">gdb</span><span class="p">)</span> <span class="n">run</span> <span class="p">--</span><span class="n">pkg</span> <span class="n">json</span><span class="p">-</span><span class="n">glib</span><span class="p">-</span><span class="m">1.0</span> <span class="n">tests</span><span class="p">/</span><span class="n">annotations</span><span class="p">/</span><span class="n">json</span><span class="p">.</span><span class="nf">vala</span>
<span class="p">(</span><span class="n">gdb</span><span class="p">)</span> <span class="n">bt</span>
</code></pre></div></div>
<p>Once I ran these commands, I was finally able to see the functions causing the crash to happen.</p>

<div class="language-vala highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="m">123456</span><span class="cp">#6  0x00007ffff7a1ef37 in vala_ccode_constant_construct_string (object_type=Python Exception &lt;class 'gdb.error'&gt;: value has been optimized out</span>
<span class="p">,</span> <span class="n">_name</span><span class="p">=</span><span class="m">0x5555563ef1c0</span> <span class="s">"anything"</span><span class="p">)</span> <span class="n">at</span> <span class="p">/</span><span class="n">home</span><span class="p">/</span><span class="n">alley</span><span class="p">/</span><span class="n">Desktop</span><span class="p">/</span><span class="n">vala</span><span class="p">/</span><span class="n">ccode</span><span class="p">/</span><span class="n">valaccodeconstant</span><span class="p">.</span><span class="n">vala</span><span class="p">:</span><span class="m">41</span>
<span class="cp">#7  0x00007ffff7a1f9f7 in vala_ccode_constant_new_string (_name=0x5555563ef1c0 "anything") at /home/alley/Desktop/vala/ccode/valaccodeconstant.vala:40</span>
<span class="cp">#8  0x00007ffff7a10918 in vala_json_module_json_builder (self=0x55555558c810) at /home/alley/Desktop/vala/codegen/valajsonmodule.vala:292</span>
<span class="cp">#9  0x00007ffff7a0f07d in vala_json_module_generate_class_to_json (self=0x55555558c810, cl=0x555557199120) at /home/alley/Desktop/vala/codegen/valajsonmodule.vala:191</span>
<span class="cp">#10 0x00007ffff7a127f4 in vala_json_module_real_generate_class_init (base=0x55555558c810, cl=0x555557199120) at /home/alley/Desktop/vala/codegen/valajsonmodule.vala:410</span>
</code></pre></div></div>

<p>This snippet of the backtrace shows that the function vala_json_module_json_builder () on line 292 was the actual crash culprit.
<br /></p>

<p>After I fixed the debug symbols, my git push decided not to work properly for a few days. So I was manually editing my changes on GitLab. My theory for git push not working is that Kwalletmanager had a problem, and so the credentials stopped working, which hanged the git push. Either way, I fixed it by switching my repo to SSH. I’ll investigate why the HTTP side of git stopped working, and I’ll fix it.</p>

<h1 id="guadec">GUADEC</h1>

<p>This was my first-ever GUADEC event that I’ve ever watched. I watched it online, and I particularly found the lightning talks to be my favourite parts. They were all short, sweet, and to the point. It was also kind of comedic how fast people talked to fit everything they wanted to say in a short time span.
<br /></p>

<p>Some talks I particularly found interesting are:
<br /></p>

<ol>
  <li>
    <p>The open source game called <a href="https://github.com/endlessm/threadbare">Threadbare</a> by Endless Access. As someone who is a game programming graduate, it instantly caught my eye and had my attention. I’ll definitely be checking it out and trying to contribute to it.</p>
  </li>
  <li>
    <p>Carlos Garnacho’s talk about GNOME on our TVs. The idea of GNOME expanding onto smart TVs opens up a whole new area of usability and user experience. It got me thinking about the specs of a regular TV set and how GNOME can adapt to and enhance that experience. The possibilities are exciting, and I’m curious to see how far this concept goes.
<br /></p>
  </li>
</ol>

<p>Overall, GUADEC made me feel more connected to the GNOME community even though I joined remotely. I’d love to have GAUDEC hosted in Toronto :)</p>]]></content><author><name>Alley Chaggar</name></author><category term="gnome" /><category term="Vala" /><summary type="html"><![CDATA[Debugging and My Challenges]]></summary></entry><entry><title type="html">YAML Research</title><link href="https://alleych.github.io/gnome/yaml-research-init/" rel="alternate" type="text/html" title="YAML Research" /><published>2025-07-18T05:00:00+00:00</published><updated>2025-07-18T05:00:00+00:00</updated><id>https://alleych.github.io/gnome/yaml-research-init</id><content type="html" xml:base="https://alleych.github.io/gnome/yaml-research-init/"><![CDATA[<h1 id="intro">Intro</h1>

<p>Hi everyone, sorry for the late post. Midterms are this week for GSoC, which means I’m halfway through GSoC. It’s been an incredible experience so far, and I know it’s going to continue to be great.</p>

<h1 id="api-vs-abi">API vs. ABI</h1>

<p>What is the difference between an application programming interface versus an application binary interface? In the beginning, this question tripped me out and confused me, because I wasn’t familiar with ABIs. Understanding what an ABI is has helped me decide which libraries I should consider using in the codegen phase. When talking about Vala, Vala is designed to use a C ABI. First, let’s understand what an API and ABI are separately and then compare them.</p>

<h2 id="api">API</h2>

<p>Personally, I think the understanding of APIs is more popular and well-known than ABIs. An API is usually, at a high level, defined by two software components or computers communicating with each other using a set of definitions and protocols. This definition I always thought was pretty vague and expansive. When dealing with code-level APIs, I like to understand it as APIs are existing entities in the user code (source code) that have functions, constants, structures, etc. You can think of it as when you write code, you access libraries through an API. For example, when you write <code class="language-vala highlighter-rouge"><span class="nf">print</span><span class="p">(</span><span class="err">'</span><span class="n">hello</span> <span class="n">world</span><span class="err">'</span><span class="p">)</span></code> in Python, <code class="language-vala highlighter-rouge"><span class="nf">print</span><span class="p">()</span></code> is a part of Python’s standard library API.</p>

<h2 id="abi">ABI</h2>

<p>ABI, on the other hand, is very similar, but instead of the compiler time, they are executed during runtime. Runtime means when your program is done compiling (going through the lexical, syntax, semantic analysis, etc) and the machine is actually running your executable. Its goals are very low-level and entails how compiled code should interact, particularly in the context of operating systems and libraries. It has protocols and standards for how the OS handles your program, such as storage, memory, hardware, and about how your compiled binary works with other compiled components.</p>

<h1 id="yaml-libraries">YAML Libraries</h1>

<p>I’ve started to look into YAML and XML (mainly YAML). I’ve looked into many different libraries dealing with YAML, some such as <a href="https://github.com/pluie-org/lib-yaml">pluie-yaml</a>, <a href="https://github.com/rainwoodman/libyaml-glib">libyaml-glib</a>, <a href="https://github.com/jimmuhk/glib-yaml">glib-yaml</a>, and <a href="https://github.com/yaml/libyaml/tree/master">libyaml</a>. To my understanding and research, there are no well-maintained YAML libraries that integrate GObject or GLib. The goal is to find a well-maintained library that I can use in the codegen.</p>

<h2 id="pluie-yaml">Pluie yaml</h2>

<p>I mentioned <a href="https://github.com/pluie-org/lib-yaml">pluie-yaml</a>, but this library isn’t a C library like <a href="https://gitlab.gnome.org/GNOME/json-glib/-/tree/main?ref_type=heads">json-glib</a>, it’s a shared Vala library. The good thing is that the codegen can use pure Vala libraries because Vala libraries have a C ABI, however, the bad part is that this library is not well-maintained. The last activity was 7 years ago.</p>

<h2 id="libyaml-glib">Libyaml glib</h2>

<p><a href="https://github.com/rainwoodman/libyaml-glib">Libyaml-glib</a> is a GLib binding of libyaml, plus a GObject builder that understands YAML. Just like pluie, it’s not a C library. It’s written in Vala. And just like pluie, it’s not well-maintained, with the last activity even stretching to longer, 9 years ago.</p>

<h2 id="glib-yaml">Glib yaml</h2>

<p><a href="https://github.com/jimmuhk/glib-yaml">Glib-yaml</a> is a GLib-based YAML parser written in C. It again, just like the other libraries, doesn’t pass the maintenance check since it’s been years of no updates or commits in the repo. Going all the way back to 13 years ago. It’s also only a parser, and it doesn’t serialize or emit YAML, so even if it were well-maintained, I’d still need to emit YAML either manually or find another library that does so.</p>

<h2 id="libyaml">Libyaml</h2>

<p>In conclusion, <a href="https://github.com/yaml/libyaml/tree/master">libyaml</a> is the C library that I will be using for parsing and emitting YAML. It has a C ABI, and it’s the most well-maintained out of all of the other libraries. Vala already has a VAPI file binding it, <a href="https://gitlab.gnome.org/GNOME/vala-extra-vapis/-/blob/master/yaml-0.1.vapi?ref_type=heads">yaml-0.1.vapi</a>. However, there is no GObject or GLib integration, unlike json-glib, but that should be fine.</p>]]></content><author><name>Alley Chaggar</name></author><category term="gnome" /><category term="Vala" /><summary type="html"><![CDATA[Intro]]></summary></entry><entry><title type="html">Demystifying The Codegen Phase Part 2</title><link href="https://alleych.github.io/gnome/codegen-part2/" rel="alternate" type="text/html" title="Demystifying The Codegen Phase Part 2" /><published>2025-07-02T07:30:00+00:00</published><updated>2025-07-02T07:30:00+00:00</updated><id>https://alleych.github.io/gnome/codegen-part2</id><content type="html" xml:base="https://alleych.github.io/gnome/codegen-part2/"><![CDATA[<h1 id="intro">Intro</h1>
<p>Hello again, I’m here to update my findings and knowledge about Vala. Last blog, I talked about the codegen phase, as intricate as it is, I’m finding some very helpful information that I want to share.</p>

<h1 id="looking-at-the-outputted-c-code">Looking at The Outputted C Code</h1>
<p>While doing the JSON module, I’m constantly looking at C code. Back and forth, back and forth, having more than 1 monitor is very helpful in times like these. 
<br /></p>

<p>At the beginning of GSoC I didn’t know much of C, and that has definitely changed. I’m still not fluent in it, but I can finally read the code and understand it without too much brain power. For the <a href="https://gitlab.gnome.org/AlleyChaggar/vala/-/blob/alley/json-glib-module/codegen/valajsonmodule.vala">JsonModule</a> I’m creating, I first looked at how users can currently (de)serialize JSON. I went scouting json-glib examples since then, and for now, I will be using json-glib. In the future, however, I’ll look at other ways in which we can have JSON more streamlined in Vala, whether that means growing away from json-glib or not.</p>

<p>Using the command ‘valac -C yourfilename.vala’, you’ll be able to see the C code that Valac generates. If you were to look into it, you’d see a bunch of temporary variables and C functions. It can be a little overwhelming to see all this if you don’t know C.
<br /></p>

<p>When writing JSON normally with minimal customization and without the JsonModule’s support. You would be writing it like this:</p>

<div class="language-vala highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="n">Json</span><span class="p">.</span><span class="n">Node</span> <span class="n">node</span> <span class="p">=</span> <span class="n">Json</span><span class="p">.</span><span class="nf">gobject_serialize</span> <span class="p">(</span><span class="n">person</span><span class="p">);</span>
<span class="n">Json</span><span class="p">.</span><span class="n">Generator</span> <span class="n">gen</span> <span class="p">=</span> <span class="k">new</span> <span class="n">Json</span><span class="p">.</span><span class="nf">Generator</span> <span class="p">();</span>
<span class="n">gen</span><span class="p">.</span><span class="nf">set_root</span><span class="p">(</span><span class="n">node</span><span class="p">);</span>
<span class="kt">string</span> <span class="n">result</span> <span class="p">=</span> <span class="n">gen</span><span class="p">.</span><span class="nf">to_data</span> <span class="p">(</span><span class="k">null</span><span class="p">);</span>
<span class="nf">print</span> <span class="p">(</span><span class="s">"%s\n"</span><span class="p">,</span> <span class="n">result</span><span class="p">);</span> 
</code></pre></div></div>

<p>This code is showing one way to serialize a GObject class using json-glib. 
<br />
The code below is a snippet of C code that Valac outputs for this example. Again, to be able to see this, you have to use the -C command when running your Vala code.</p>

<div class="language-c highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="k">static</span> <span class="kt">void</span>
<span class="nf">_vala_main</span> <span class="p">(</span><span class="kt">void</span><span class="p">)</span>
<span class="p">{</span>
		<span class="n">Person</span><span class="o">*</span> <span class="n">person</span> <span class="o">=</span> <span class="nb">NULL</span><span class="p">;</span>
		<span class="n">Person</span><span class="o">*</span> <span class="n">_tmp0_</span><span class="p">;</span>
		<span class="n">JsonNode</span><span class="o">*</span> <span class="n">node</span> <span class="o">=</span> <span class="nb">NULL</span><span class="p">;</span>
		<span class="n">JsonNode</span><span class="o">*</span> <span class="n">_tmp1_</span><span class="p">;</span>
		<span class="n">JsonGenerator</span><span class="o">*</span> <span class="n">gen</span> <span class="o">=</span> <span class="nb">NULL</span><span class="p">;</span>
		<span class="n">JsonGenerator</span><span class="o">*</span> <span class="n">_tmp2_</span><span class="p">;</span>
		<span class="n">gchar</span><span class="o">*</span> <span class="n">_result_</span> <span class="o">=</span> <span class="nb">NULL</span><span class="p">;</span>
		<span class="n">gchar</span><span class="o">*</span> <span class="n">_tmp3_</span><span class="p">;</span>
		<span class="n">_tmp0_</span> <span class="o">=</span> <span class="n">person_new</span> <span class="p">();</span>
		<span class="n">person</span> <span class="o">=</span> <span class="n">_tmp0_</span><span class="p">;</span>
		<span class="n">person_set_name</span> <span class="p">(</span><span class="n">person</span><span class="p">,</span> <span class="s">"Alley"</span><span class="p">);</span>
		<span class="n">person_set_age</span> <span class="p">(</span><span class="n">person</span><span class="p">,</span> <span class="mi">2</span><span class="p">);</span>
		<span class="n">_tmp1_</span> <span class="o">=</span> <span class="n">json_gobject_serialize</span> <span class="p">((</span><span class="n">GObject</span><span class="o">*</span><span class="p">)</span> <span class="n">person</span><span class="p">);</span>
		<span class="n">node</span> <span class="o">=</span> <span class="n">_tmp1_</span><span class="p">;</span>
		<span class="n">_tmp2_</span> <span class="o">=</span> <span class="n">json_generator_new</span> <span class="p">();</span>
		<span class="n">gen</span> <span class="o">=</span> <span class="n">_tmp2_</span><span class="p">;</span>
		<span class="n">json_generator_set_root</span> <span class="p">(</span><span class="n">gen</span><span class="p">,</span> <span class="n">node</span><span class="p">);</span>
		<span class="n">_tmp3_</span> <span class="o">=</span> <span class="n">json_generator_to_data</span> <span class="p">(</span><span class="n">gen</span><span class="p">,</span> <span class="nb">NULL</span><span class="p">);</span>
		<span class="n">_result_</span> <span class="o">=</span> <span class="n">_tmp3_</span><span class="p">;</span>
		<span class="n">g_print</span> <span class="p">(</span><span class="s">"%s</span><span class="se">\n</span><span class="s">"</span><span class="p">,</span> <span class="n">_result_</span><span class="p">);</span>
		<span class="n">_g_free0</span> <span class="p">(</span><span class="n">_result_</span><span class="p">);</span>
		<span class="n">_g_object_unref0</span> <span class="p">(</span><span class="n">gen</span><span class="p">);</span>
		<span class="n">__vala_JsonNode_free0</span> <span class="p">(</span><span class="n">node</span><span class="p">);</span>
		<span class="n">_g_object_unref0</span> <span class="p">(</span><span class="n">person</span><span class="p">);</span>
<span class="p">}</span>
</code></pre></div></div>

<p>You can see many tempary variables denoted by the names <code class="language-vala highlighter-rouge"><span class="n">__tmp</span><span class="p">*</span><span class="n">_</span></code>, but you can also see JsonNode being called, you can see Json’s generator being called and setting root, and you can even see json gobject serialize. All of this was in our Vala code, and now it’s all in the C code, having temporary variables containing them to be successfully compiled to C code.</p>

<h1 id="the-jsonmodule">The jsonmodule</h1>
<p>If you may recall the Codegen is the clash of Vala code, but also writing to C code. The steps I’m taking for the <a href="https://gitlab.gnome.org/AlleyChaggar/vala/-/blob/alley/json-glib-module/codegen/valajsonmodule.vala">JsonModule</a> are looking at the examples to (de)serialize then looking at how the example compiled to C. Since the whole purpose of my work is to write how the C should look like. I’m mainly going off of C’s <code class="language-vala highlighter-rouge"><span class="n">_vala_main</span></code> function when determining which C code I should put into my module, but I’m also going off of what the Vala code the user put.</p>

<div class="language-vala highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="c1">// serializing gobject classes</span>
	<span class="k">void</span> <span class="nf">generate_gclass_to_json</span> <span class="p">(</span><span class="n">Class</span> <span class="n">cl</span><span class="p">)</span> <span class="p">{</span>
		<span class="n">cfile</span><span class="p">.</span><span class="nf">add_include</span> <span class="p">(</span><span class="s">"json-glib/json-glib.h"</span><span class="p">);</span>

		<span class="k">var</span> <span class="n">to_json_class</span> <span class="p">=</span> <span class="k">new</span> <span class="nf">CCodeFunction</span> <span class="p">(</span><span class="s">"_json_%s_serialize_myclass"</span><span class="p">.</span><span class="nf">printf</span> <span class="p">(</span><span class="nf">get_ccode_lower_case_name</span> <span class="p">(</span><span class="n">cl</span><span class="p">,</span> <span class="k">null</span><span class="p">)),</span> <span class="s">"void"</span><span class="p">);</span>
		<span class="n">to_json_class</span><span class="p">.</span><span class="nf">add_parameter</span> <span class="p">(</span><span class="k">new</span> <span class="nf">CCodeParameter</span> <span class="p">(</span><span class="s">"gobject"</span><span class="p">,</span> <span class="s">"GObject *"</span><span class="p">));</span>
		<span class="n">to_json_class</span><span class="p">.</span><span class="nf">add_parameter</span> <span class="p">(</span><span class="k">new</span> <span class="nf">CCodeParameter</span> <span class="p">(</span><span class="s">"value"</span><span class="p">,</span> <span class="s">" GValue *"</span><span class="p">));</span>
		<span class="n">to_json_class</span><span class="p">.</span><span class="nf">add_parameter</span> <span class="p">(</span><span class="k">new</span> <span class="nf">CCodeParameter</span> <span class="p">(</span><span class="s">"pspec"</span><span class="p">,</span> <span class="s">"GParamSpec *"</span><span class="p">));</span>
		
		<span class="c1">//...</span>

		<span class="k">var</span> <span class="n">Json_gobject_serialize</span> <span class="p">=</span> <span class="k">new</span> <span class="nf">CCodeFunctionCall</span> <span class="p">(</span><span class="k">new</span> <span class="nf">CCodeIdentifier</span> <span class="p">(</span><span class="s">"json_gobject_serialize"</span><span class="p">));</span>
		<span class="n">Json_gobject_serialize</span><span class="p">.</span><span class="nf">add_argument</span> <span class="p">(</span><span class="k">new</span> <span class="nf">CCodeIdentifier</span> <span class="p">(</span><span class="s">"gobject"</span><span class="p">));</span>

		<span class="c1">// Json.Node node = Json.gobject_serialize (person); - vala code</span>
		<span class="n">Json_gobject_serialize</span><span class="p">.</span><span class="nf">add_argument</span> <span class="p">(</span><span class="k">new</span> <span class="nf">CCodeIdentifier</span> <span class="p">(</span><span class="s">"gobject"</span><span class="p">));</span>
		<span class="k">var</span> <span class="n">node_decl_right</span> <span class="p">=</span> <span class="k">new</span> <span class="nf">CCodeVariableDeclarator</span> <span class="p">(</span><span class="s">"node"</span><span class="p">,</span> <span class="n">Json_gobject_serialize</span><span class="p">);</span>
		<span class="k">var</span> <span class="n">node_decl_left</span> <span class="p">=</span> <span class="k">new</span> <span class="nf">CCodeDeclaration</span> <span class="p">(</span><span class="s">"JsonNode *"</span><span class="p">);</span>
		<span class="n">node_decl_left</span><span class="p">.</span><span class="nf">add_declarator</span> <span class="p">(</span><span class="n">node_decl_right</span><span class="p">);</span>

		<span class="c1">// Json.Generator gen = new Json.Generator (); - vala code</span>
		<span class="k">var</span> <span class="n">gen_decl_right</span> <span class="p">=</span> <span class="k">new</span> <span class="nf">CCodeVariableDeclarator</span> <span class="p">(</span><span class="s">"generator"</span><span class="p">,</span> <span class="n">json_gen_new</span><span class="p">);</span>
		<span class="k">var</span> <span class="n">gen_decl_left</span> <span class="p">=</span> <span class="k">new</span> <span class="nf">CCodeDeclaration</span> <span class="p">(</span><span class="s">"JsonGenerator *"</span><span class="p">);</span>
		<span class="n">gen_decl_left</span><span class="p">.</span><span class="nf">add_declarator</span> <span class="p">(</span><span class="n">gen_decl_right</span><span class="p">);</span>

		<span class="c1">// gen.set_root(node); - vala code</span>
		<span class="k">var</span> <span class="n">json_gen_set_root</span> <span class="p">=</span> <span class="k">new</span> <span class="nf">CCodeFunctionCall</span> <span class="p">(</span><span class="k">new</span> <span class="nf">CCodeIdentifier</span> <span class="p">(</span><span class="s">"json_generator_set_root"</span><span class="p">));</span>
		<span class="n">json_gen_set_root</span><span class="p">.</span><span class="nf">add_argument</span> <span class="p">(</span><span class="k">new</span> <span class="nf">CCodeIdentifier</span> <span class="p">(</span><span class="s">"generator"</span><span class="p">));</span>
		<span class="n">json_gen_set_root</span><span class="p">.</span><span class="nf">add_argument</span> <span class="p">(</span><span class="k">new</span> <span class="nf">CCodeIdentifier</span> <span class="p">(</span><span class="s">"node"</span><span class="p">));</span>
		<span class="c1">//...</span>
</code></pre></div></div>
<p>The code snippet above is a work in progress method in the JsonModule that I created called ‘generate_gclass_to_json’ to generate serialization for GObject classes. I’m creating a C code function and passing parameters through it. I’m also filling the body with how the example code did the serializing in the first code snippet. Instead of the function calls being created in <code class="language-vala highlighter-rouge"><span class="n">_vala_main</span></code> (by the user), they’ll have their own function that will instantly get created by the module instead.</p>

<div class="language-c highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="k">static</span> <span class="kt">void</span> <span class="n">_json_</span><span class="o">%</span><span class="n">s_serialize_myclass</span> <span class="p">(</span><span class="n">GObject</span> <span class="o">*</span><span class="n">gobject</span><span class="p">,</span> <span class="n">GValue</span> <span class="o">*</span><span class="n">value</span><span class="p">,</span> <span class="n">GParamSpec</span> <span class="o">*</span><span class="n">pspec</span><span class="p">)</span>
<span class="p">{</span>
	<span class="n">JsonNode</span> <span class="o">*</span><span class="n">node</span> <span class="o">=</span> <span class="n">Json_gobject_serialize</span> <span class="p">(</span><span class="n">gobject</span><span class="p">);</span>
	<span class="n">JsonGenerator</span> <span class="o">*</span><span class="n">generator</span> <span class="o">=</span> <span class="n">json_generator_new</span> <span class="p">();</span>
	<span class="n">json_generator_set_root</span> <span class="p">(</span><span class="n">generator</span><span class="p">,</span> <span class="n">node</span><span class="p">);</span>
	<span class="c1">//...</span>
<span class="p">}</span>
</code></pre></div></div>
<p>Comparing the differences with the original Vala code and the compiled code (C code), it takes the Vala code shape, but it’s written in C.</p>]]></content><author><name>Alley Chaggar</name></author><category term="gnome" /><category term="Vala" /><summary type="html"><![CDATA[Intro Hello again, I’m here to update my findings and knowledge about Vala. Last blog, I talked about the codegen phase, as intricate as it is, I’m finding some very helpful information that I want to share.]]></summary></entry><entry><title type="html">Demystifying The Codegen Phase Part 1</title><link href="https://alleych.github.io/gnome/codegen-part1/" rel="alternate" type="text/html" title="Demystifying The Codegen Phase Part 1" /><published>2025-06-18T16:30:00+00:00</published><updated>2025-06-18T16:30:00+00:00</updated><id>https://alleych.github.io/gnome/codegen-part1</id><content type="html" xml:base="https://alleych.github.io/gnome/codegen-part1/"><![CDATA[<h1 id="intro">Intro</h1>

<p>I want to start off and say I’m really glad that my last blog was helpful to many wanting to understand Vala’s compiler. I hope this blog will also be just as informative and helpful. I want to talk a little about the basics of the compiler again, but this time, catering to the codegen phase. The phase that I’m actually working on, but has the least information in the Vala Docs.</p>

<p>Last blog, I briefly mentioned the directories <a href="https://gitlab.gnome.org/GNOME/vala/-/tree/main/codegen?ref_type=heads">codegen</a> and <a href="https://gitlab.gnome.org/GNOME/vala/-/tree/main/ccode?ref_type=heads">ccode</a> being part of the codegen phase. This blog will be going more into depth about it. The codegen phase takes the AST and outputs the C code tree (ccode* objects), so that it can be generated to C code more easily, usually by GCC or another C compiler you installed. When dealing with this phase, it’s really beneficial to know and understand at least a little bit of C.</p>

<h1 id="ccode-directory"><a href="https://gitlab.gnome.org/GNOME/vala/-/tree/main/ccode?ref_type=heads">ccode</a> Directory</h1>
<ul>
  <li>Many of the files in the ccode directory are derived from the class CCodeNode, <a href="https://gitlab.gnome.org/GNOME/vala/-/blob/main/ccode/valaccodenode.vala?ref_type=heads">valaccodenode.vala</a>.</li>
  <li>
    <p>The files in this directory represent C Constructs. For example, the <a href="https://gitlab.gnome.org/GNOME/vala/-/blob/main/ccode/valaccodefunction.vala?ref_type=heads">valaccodefunction.vala</a> file represents a C code function. Regular C functions have function names, parameters, return types, and bodies that add logic. Essentially, what this class specifically does, is provide the building blocks for building a function in C.</p>

    <p><img src="https://logicmojo.com/assets/dist/new_pages/images/Cfunction3.png" width="200" /></p>

    <div class="language-vala highlighter-rouge"><div class="highlight"><pre class="highlight"><code>   <span class="c1">//...</span>
  	<span class="n">writer</span><span class="p">.</span><span class="nf">write_string</span> <span class="p">(</span><span class="n">return_type</span><span class="p">);</span>
      <span class="k">if</span> <span class="p">(</span><span class="n">is_declaration</span><span class="p">)</span> <span class="p">{</span>
          <span class="n">writer</span><span class="p">.</span><span class="nf">write_string</span> <span class="p">(</span><span class="s">" "</span><span class="p">);</span>
      <span class="p">}</span> <span class="k">else</span> <span class="p">{</span>
          <span class="n">writer</span><span class="p">.</span><span class="nf">write_newline</span> <span class="p">();</span>
      <span class="p">}</span>
      <span class="n">writer</span><span class="p">.</span><span class="nf">write_string</span> <span class="p">(</span><span class="n">name</span><span class="p">);</span>
      <span class="n">writer</span><span class="p">.</span><span class="nf">write_string</span> <span class="p">(</span><span class="s">" ("</span><span class="p">);</span>
      <span class="kt">int</span> <span class="n">param_pos_begin</span> <span class="p">=</span> <span class="p">(</span><span class="n">is_declaration</span> <span class="p">?</span> <span class="n">return_type</span><span class="p">.</span><span class="nf">char_count</span> <span class="p">()</span> <span class="p">+</span> <span class="m">1</span> <span class="p">:</span> <span class="m">0</span> <span class="p">)</span> <span class="p">+</span> <span class="n">name</span><span class="p">.</span><span class="nf">char_count</span> <span class="p">()</span> <span class="p">+</span> <span class="m">2</span><span class="p">;</span>

      <span class="kt">bool</span> <span class="n">has_args</span> <span class="p">=</span> <span class="p">(</span><span class="n">CCodeModifiers</span><span class="p">.</span><span class="n">PRINTF</span> <span class="k">in</span> <span class="n">modifiers</span> <span class="p">||</span> <span class="n">CCodeModifiers</span><span class="p">.</span><span class="n">SCANF</span> <span class="k">in</span> <span class="n">modifiers</span><span class="p">);</span>
 <span class="c1">//...</span>
</code></pre></div>    </div>
  </li>
</ul>

<p>This code snippet is part of the ccodefunction file, and what it’s doing is overriding the ‘write’ function that is originally from ccodenode. It’s actually writing out the C function.</p>

<h1 id="codegen-directory"><a href="https://gitlab.gnome.org/GNOME/vala/-/tree/main/codegen?ref_type=heads">codegen</a> Directory</h1>
<ul>
  <li>The files in this directory are higher-level components responsible for taking the compiler’s internal representation, such as the AST and transforming it into the C code model ccode objects.</li>
  <li>
    <p>Going back to the example of the ccodefunction, codegen will take a function node from the abstract syntax tree (AST), and will create a new ccodefunction object. It then fills this object with information like the return type, function name, parameters, and body, which are all derived from the AST. Then the CCodeFunction.write() (the code above) will generate and write out the C function.</p>

    <div class="language-vala highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="c1">//...</span>
<span class="k">private</span> <span class="k">void</span> <span class="nf">add_get_property_function</span> <span class="p">(</span><span class="n">Class</span> <span class="n">cl</span><span class="p">)</span> <span class="p">{</span>
		<span class="k">var</span> <span class="n">get_prop</span> <span class="p">=</span> <span class="k">new</span> <span class="nf">CCodeFunction</span> <span class="p">(</span><span class="s">"_vala_%s_get_property"</span><span class="p">.</span><span class="nf">printf</span> <span class="p">(</span><span class="nf">get_ccode_lower_case_name</span> <span class="p">(</span><span class="n">cl</span><span class="p">,</span> <span class="k">null</span><span class="p">)),</span> <span class="s">"void"</span><span class="p">);</span>
		<span class="n">get_prop</span><span class="p">.</span><span class="n">modifiers</span> <span class="p">=</span> <span class="n">CCodeModifiers</span><span class="p">.</span><span class="n">STATIC</span><span class="p">;</span>
		<span class="n">get_prop</span><span class="p">.</span><span class="nf">add_parameter</span> <span class="p">(</span><span class="k">new</span> <span class="nf">CCodeParameter</span> <span class="p">(</span><span class="s">"object"</span><span class="p">,</span> <span class="s">"GObject *"</span><span class="p">));</span>
		<span class="n">get_prop</span><span class="p">.</span><span class="nf">add_parameter</span> <span class="p">(</span><span class="k">new</span> <span class="nf">CCodeParameter</span> <span class="p">(</span><span class="s">"property_id"</span><span class="p">,</span> <span class="s">"guint"</span><span class="p">));</span>
		<span class="n">get_prop</span><span class="p">.</span><span class="nf">add_parameter</span> <span class="p">(</span><span class="k">new</span> <span class="nf">CCodeParameter</span> <span class="p">(</span><span class="s">"value"</span><span class="p">,</span> <span class="s">"GValue *"</span><span class="p">));</span>
		<span class="n">get_prop</span><span class="p">.</span><span class="nf">add_parameter</span> <span class="p">(</span><span class="k">new</span> <span class="nf">CCodeParameter</span> <span class="p">(</span><span class="s">"pspec"</span><span class="p">,</span> <span class="s">"GParamSpec *"</span><span class="p">));</span>
  
		<span class="nf">push_function</span> <span class="p">(</span><span class="n">get_prop</span><span class="p">);</span>
<span class="c1">//...</span>
</code></pre></div>    </div>
  </li>
</ul>

<p>This code snippet is from <a href="https://gitlab.gnome.org/GNOME/vala/-/blob/main/codegen/valagobjectmodule.vala?ref_type=heads">valagobjectmodule.vala</a> and it’s calling CCodeFunction (again from the <a href="https://gitlab.gnome.org/GNOME/vala/-/blob/main/ccode/valaccodefunction.vala?ref_type=heads">valaccodefunction.vala</a>) and adding the parameters, which is calling <a href="https://gitlab.gnome.org/GNOME/vala/-/blob/main/ccode/valaccodeparameter.vala?ref_type=heads">valaccodeparameter.vala</a>. What this would output is something that looks like this in C:</p>

<pre><code class="language-C">    void _vala_get_property (GObject *object, guint property_id, GValue *value, GParamSpec *pspec) {
       //... 
    }
</code></pre>

<h1 id="why-do-all-this">Why do all this?</h1>
<p>Now you might ask why? Why separate codegen and ccode?</p>

<ul>
  <li>We split things into codegen and ccode to keep the compiler organized, readable, and maintainable. It prevents us from having to constantly write C code representations from scratch all the time.</li>
  <li>It also reinforces the idea of polymorphism and the ability that objects can behave differently depending on their subclass.</li>
  <li>And it lets us do hidden generation by adding new helper functions, temporary variables, or inlined optimizations after the AST and before the C code output.</li>
</ul>

<h1 id="jsonmodule">Jsonmodule</h1>
<p>I’m happy to say that I am making a lot of progress with the JSON module I mentioned last blog. The <a href="https://gitlab.gnome.org/AlleyChaggar/vala/-/blob/alley/json-glib-module/codegen/valajsonmodule.vala?ref_type=heads">JSON module</a> follows very closely other modules in the codegen, specifically like the <a href="https://gitlab.gnome.org/GNOME/vala/-/blob/main/codegen/valagobjectmodule.vala?ref_type=heads">gtk module</a> and the <a href="https://gitlab.gnome.org/GNOME/vala/-/blob/main/codegen/valagobjectmodule.vala?ref_type=heads">gobject module</a>. It will be calling ccode functions to make ccode objects and creating helper methods so that the user doesn’t need to manually override certain JSON methods.</p>]]></content><author><name>Alley Chaggar</name></author><category term="gnome" /><category term="Vala" /><summary type="html"><![CDATA[Intro]]></summary></entry><entry><title type="html">Compiler Knowledge</title><link href="https://alleych.github.io/gnome/compiler-knowledge/" rel="alternate" type="text/html" title="Compiler Knowledge" /><published>2025-06-04T14:25:00+00:00</published><updated>2025-06-04T14:25:00+00:00</updated><id>https://alleych.github.io/gnome/compiler-knowledge</id><content type="html" xml:base="https://alleych.github.io/gnome/compiler-knowledge/"><![CDATA[<h1 id="intro">Intro</h1>

<p>I apologize that I’m a little late updating my blog, but over the past two weeks, I’ve been diving into Vala’s compiler and exploring how JSON (de)serialization could be integrated. My mentor, Lorenz, and I agreed that focusing on JSON is a good beginning.</p>

<h1 id="understanding-the-vala-compiler">Understanding the Vala Compiler</h1>

<p>Learning the steps it takes to go from Vala code to C code is absolutely fascinating.</p>

<p>Vala’s Compiler 101</p>
<ul>
  <li>The first step in the compiler is the lexical analysis. This is handled by <a href="https://gitlab.gnome.org/GNOME/vala/-/blob/main/vala/valascanner.vala?ref_type=heads">valascanner.vala</a>, where your Vala code gets tokenized, which breaks up your code into chunks called tokens that are easier for the compiler to understand.</li>
</ul>

<div class="language-vala highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="k">switch</span> <span class="p">(</span><span class="n">begin</span><span class="p">[</span><span class="m">0</span><span class="p">])</span> <span class="p">{</span>
		<span class="k">case</span> <span class="sc">'f'</span><span class="p">:</span>
			<span class="k">if</span> <span class="p">(</span><span class="nf">matches</span> <span class="p">(</span><span class="n">begin</span><span class="p">,</span> <span class="s">"for"</span><span class="p">))</span> <span class="k">return</span> <span class="n">TokenType</span><span class="p">.</span><span class="n">FOR</span><span class="p">;</span>
			<span class="k">break</span><span class="p">;</span>
		<span class="k">case</span> <span class="sc">'g'</span><span class="p">:</span>
			<span class="k">if</span> <span class="p">(</span><span class="nf">matches</span> <span class="p">(</span><span class="n">begin</span><span class="p">,</span> <span class="s">"get"</span><span class="p">))</span> <span class="k">return</span> <span class="n">TokenType</span><span class="p">.</span><span class="n">GET</span><span class="p">;</span>
			<span class="k">break</span><span class="p">;</span>
</code></pre></div></div>
<p>The code above is a snippet of Vala’s scanner, it’s responsible for recognizing specific keywords like ‘for’ and ‘get’ and returning the appropriate token type.</p>

<ul>
  <li>
    <p>Next is syntax analysis and the creation of the abstract syntax tree (AST). In Vala, it’s managed by <a href="https://gitlab.gnome.org/GNOME/vala/-/blob/main/vala/valaparser.vala?ref_type=heads">valaparser.vala</a>, which checks if your code structure is correct, for example, if that pesky ‘}’ is missing.</p>

    <div class="language-vala highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="k">inline</span> <span class="kt">bool</span> <span class="nf">expect</span> <span class="p">(</span><span class="n">TokenType</span> <span class="n">type</span><span class="p">)</span> <span class="k">throws</span> <span class="n">ParseError</span> <span class="p">{</span>
	<span class="k">if</span> <span class="p">(</span><span class="nf">accept</span> <span class="p">(</span><span class="n">type</span><span class="p">))</span> <span class="p">{</span>
		<span class="k">return</span> <span class="k">true</span><span class="p">;</span>
	<span class="p">}</span>
  
	<span class="k">switch</span> <span class="p">(</span><span class="n">type</span><span class="p">)</span> <span class="p">{</span>
	<span class="k">case</span> <span class="n">TokenType</span><span class="p">.</span><span class="n">CLOSE_BRACE</span><span class="p">:</span>
		<span class="nf">safe_prev</span> <span class="p">();</span>
		<span class="nf">report_parse_error</span> <span class="p">(</span><span class="k">new</span> <span class="n">ParseError</span><span class="p">.</span><span class="nf">SYNTAX</span> <span class="p">(</span><span class="s">"following block delimiter %s missing"</span><span class="p">,</span> <span class="n">type</span><span class="p">.</span><span class="nf">to_string</span> <span class="p">()));</span>
		<span class="k">return</span> <span class="k">true</span><span class="p">;</span>
	<span class="k">case</span> <span class="n">TokenType</span><span class="p">.</span><span class="n">CLOSE_BRACKET</span><span class="p">:</span>
	<span class="k">case</span> <span class="n">TokenType</span><span class="p">.</span><span class="n">CLOSE_PARENS</span><span class="p">:</span>
	<span class="k">case</span> <span class="n">TokenType</span><span class="p">.</span><span class="n">SEMICOLON</span><span class="p">:</span>
		<span class="nf">safe_prev</span> <span class="p">();</span>
		<span class="nf">report_parse_error</span> <span class="p">(</span><span class="k">new</span> <span class="n">ParseError</span><span class="p">.</span><span class="nf">SYNTAX</span> <span class="p">(</span><span class="s">"following expression/statement delimiter %s missing"</span><span class="p">,</span> <span class="n">type</span><span class="p">.</span><span class="nf">to_string</span> <span class="p">()));</span>
		<span class="k">return</span> <span class="k">true</span><span class="p">;</span>
	<span class="k">default</span><span class="p">:</span>
		<span class="k">throw</span> <span class="k">new</span> <span class="n">ParseError</span><span class="p">.</span><span class="nf">SYNTAX</span> <span class="p">(</span><span class="s">"expected %s"</span><span class="p">,</span> <span class="n">type</span><span class="p">.</span><span class="nf">to_string</span> <span class="p">());</span>
	<span class="p">}</span>
<span class="p">}</span>
</code></pre></div>    </div>
    <p>This is a snippet of Vala’s parser, it tries to accept a specific token type, like again that ‘}’. If ‘}’ is there, it continues parsing. Else if not, it throws a syntax error.</p>
  </li>
  <li>
    <p>Then comes semantic analysis, the “meat and logic,” as I like to call it. This happens in <a href="https://gitlab.gnome.org/GNOME/vala/-/blob/main/vala/valasemanticanalyzer.vala?ref_type=heads">valasemanticanalyzer.vala</a>, where the compiler checks if things make sense. Do the types match? Are you using the correct number of parameters?</p>

    <div class="language-vala highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="k">public</span> <span class="kt">bool</span> <span class="nf">is_in_constructor</span> <span class="p">()</span> <span class="p">{</span>
      <span class="k">unowned</span> <span class="n">Symbol</span><span class="p">?</span> <span class="n">sym</span> <span class="p">=</span> <span class="n">current_symbol</span><span class="p">;</span>
      <span class="k">while</span> <span class="p">(</span><span class="n">sym</span> <span class="p">!=</span> <span class="k">null</span><span class="p">)</span> <span class="p">{</span>
          <span class="k">if</span> <span class="p">(</span><span class="n">sym</span> <span class="k">is</span> <span class="n">Constructor</span><span class="p">)</span> <span class="p">{</span>
              <span class="k">return</span> <span class="k">true</span><span class="p">;</span>
          <span class="p">}</span>
          <span class="n">sym</span> <span class="p">=</span> <span class="n">sym</span><span class="p">.</span><span class="n">parent_symbol</span><span class="p">;</span>
      <span class="p">}</span>
      <span class="k">return</span> <span class="k">false</span><span class="p">;</span>
  <span class="p">}</span>
</code></pre></div>    </div>
    <p>This code is a snippet of Vala’s semantic analyzer, which helps the compiler understand if the current code is a constructor. Starting from the current symbol, which represents where the compiler is in the code, it then moves through its parent symbols. If it finds a parent symbol that is a constructor, it returns true. Else if the parent symbol is null, it returns false.</p>
  </li>
  <li>
    <p>After that, the flow analysis phase, located in <a href="https://gitlab.gnome.org/GNOME/vala/-/blob/main/vala/valaflowanalyzer.vala?ref_type=heads">valaflowanalyzer.vala</a>, analyzes the execution order of the code. It figures out how control flows through the program, which is useful for things like variable initialization and unreachable code.</p>

    <div class="language-vala highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="k">public</span> <span class="k">override</span> <span class="k">void</span> <span class="nf">visit_lambda_expression</span> <span class="p">(</span><span class="n">LambdaExpression</span> <span class="n">le</span><span class="p">)</span> <span class="p">{</span>
	<span class="k">var</span> <span class="n">old_current_block</span> <span class="p">=</span> <span class="n">current_block</span><span class="p">;</span>
	<span class="k">var</span> <span class="n">old_unreachable_reported</span> <span class="p">=</span> <span class="n">unreachable_reported</span><span class="p">;</span>
	<span class="k">var</span> <span class="n">old_jump_stack</span> <span class="p">=</span> <span class="n">jump_stack</span><span class="p">;</span>
	<span class="nf">mark_unreachable</span> <span class="p">();</span>
	<span class="n">jump_stack</span> <span class="p">=</span> <span class="k">new</span> <span class="n">ArrayList</span><span class="p">&lt;</span><span class="n">JumpTarget</span><span class="p">&gt;</span> <span class="p">();</span>
  
	<span class="n">le</span><span class="p">.</span><span class="nf">accept_children</span> <span class="p">(</span><span class="k">this</span><span class="p">);</span>
  
	<span class="n">current_block</span> <span class="p">=</span> <span class="n">old_current_block</span><span class="p">;</span>
	<span class="n">unreachable_reported</span> <span class="p">=</span> <span class="n">old_unreachable_reported</span><span class="p">;</span>
	<span class="n">jump_stack</span> <span class="p">=</span> <span class="n">old_jump_stack</span><span class="p">;</span>
	<span class="p">}</span>
</code></pre></div>    </div>

    <p>The snippet of Vala’s flow analyzer ensures that control flow, like unreachable code or jump statements, is properly analyzed within the lambda expression.</p>
  </li>
  <li>
    <p>After all that, we now want to convert the Vala code into C code using a variety of Vala files in the directories <a href="https://gitlab.gnome.org/GNOME/vala/-/tree/main/ccode?ref_type=heads">ccode</a> and <a href="https://gitlab.gnome.org/GNOME/vala/-/tree/main/codegen?ref_type=heads">codegen</a>.</p>
  </li>
</ul>

<p>All of these classes inherit from <a href="https://gitlab.gnome.org/GNOME/vala/-/blob/main/vala/valacodevisitor.vala?ref_type=heads">valacodevisitor.vala</a>, which is basically the mother of classes that provides the <code class="language-vala highlighter-rouge"><span class="n">visit_</span><span class="p">*</span></code> methods that allow each phase in the compiler to walk the source code tree.</p>

<p>I know this brief isn’t all of what there is to understand about the compiler, but it’s a start. Also, let’s take a moment to appreciate everyone who has contributed to Vala’s compiler design, it’s truly an art 🎨</p>

<h1 id="the-coding-period-begins">The Coding Period Begins!!!</h1>

<p>Now that GSoC’s official coding period is here, I’m continuing my research on how to implement JSON support.</p>

<p>Right now, I’m still learning the codegen phase AKA the phase of converting vala into C, I’m exploring <a href="https://gitlab.gnome.org/GNOME/json-glib">json glib</a> and starting to work on a valajsonmodule.vala in the code gen.</p>

<p>Another thing I want to work on is the Vala docs. The docs aren’t bad, but I’ve realized the information is pretty limited the deeper you get into the compiler.</p>

<p>I’m excited that this is starting to slowly make sense, little by little.</p>]]></content><author><name>Alley Chaggar</name></author><category term="gnome" /><category term="gnome" /><category term="Vala" /><summary type="html"><![CDATA[Intro]]></summary></entry><entry><title type="html">Hi GSoC and GNOME</title><link href="https://alleych.github.io/gnome/hello-gnome/" rel="alternate" type="text/html" title="Hi GSoC and GNOME" /><published>2025-05-16T06:50:00+00:00</published><updated>2025-05-16T06:50:00+00:00</updated><id>https://alleych.github.io/gnome/hello-gnome</id><content type="html" xml:base="https://alleych.github.io/gnome/hello-gnome/"><![CDATA[<p><strong><em>Hey GNOME and GSoC! 👋</em></strong></p>

<p>This is my first blog post on Planet GNOME and actually, my first blog post ever! I’ll be using this space to share updates on my Google Summer of Code 2025 project and other GNOME contributions I make along the way or even after GSoC ends. Of course, most posts for now will focus on my GSoC work.</p>

<p><strong><em>🛠️ My GSoC Project: Improving JSON, XML, and YAML Integration in Vala</em></strong></p>

<p>My GSoC project is about improving how Vala handles JSON, XML, and YAML. Right now, working with these formatting languages in Vala can be tedious. I’ll be contributing improvements to make parsing and emitting data in these formats easier and more intuitive for developers.</p>

<p>I’ll also be diving into some compiler design topics and exploring Vala projects that already use these formats, to better understand current pain points and use cases.</p>

<p><strong><em>Thanks 🙏</em></strong></p>

<p>Also, big thanks to <a href="https://feborg.es/">Felipe Borges</a> for maintaining Planet GNOME, and to my mentor <a href="https://gitlab.gnome.org/lwildberg">Lorenz Wildberg</a>, who will be mentoring me on this project. 💙</p>]]></content><author><name>Alley Chaggar</name></author><category term="gnome" /><category term="gnome" /><category term="Vala" /><category term="First post" /><summary type="html"><![CDATA[Hey GNOME and GSoC! 👋]]></summary></entry></feed>