{"id":3570,"date":"2014-09-02T23:41:46","date_gmt":"2014-09-02T23:41:46","guid":{"rendered":"http:\/\/www.readytext.co.uk\/?p=3570"},"modified":"2020-04-05T17:01:22","modified_gmt":"2020-04-05T17:01:22","slug":"looking-inside-tex-c-helps-me-to-see","status":"publish","type":"post","link":"https:\/\/www.readytext.co.uk\/?p=3570","title":{"rendered":"Looking inside TeX: C helps me to see"},"content":{"rendered":"<p>\u00a0<\/p>\n<h1>Introduction: From WEB to C, a bit of history\/background<\/h1>\n<p>For some time I&#8217;d wanted to build TeX (the original Knuth version) from the WEB source code, but the relatively complex process to generate C from WEB meant it was one of those &#8220;tasks&#8221; I kept putting off. Well, back in early 2013 I finally decided to have a go and, eventually, I managed to create a <a href=\"https:\/\/www.readytext.co.uk\/?p=2529\">Windows port\/build of the Web2C executable<\/a> and associated tools. Using those tools I was finally able to generate TeX.C from TeX.WEB and compile a working TeX executable. As part of that exercise I decided remove the Kpathsea path-searching library from my build of TeX, replacing it with a simple recursive directory search \u2013 based, at the moment, on compile-time options (which I plan to make fully configurable \u2013 probably with a Lua-based config file).<\/p>\n<h2>Why am I doing this&#8230; ?<\/h2>\n<p>I ask myself this on many occasions&#8230; Having <a href=\"https:\/\/www.readytext.co.uk\/?p=3122\"> &#8220;ported&#8221; LuaTeX to a native Windows build<\/a>, I already have a TeX-based system to explore via Visual Studio (and LuaTeX is written in clean C, no need of Web2C). I guess it&#8217;s mainly curiosity but there is also the fact I can &#8220;tweak + explore&#8221; some parts of Knuthian TeX and rapidly and easily re-compile it \u2013 the C code base of Knuthian TeX is tiny fraction of LuaTeX and is thus far, far quicker to compile. I also don&#8217;t want to risk doing something dumb and somehow wrecking my port\/build of LuaTeX.<\/p>\n<h1>Poking around inside TeX.C<\/h1>\n<p>Although I have quite a collection of books on TeX, I&#8217;ve always found it really, <em>really<\/em> hard to understand how TeX \u2013 the language and program \u2013 actually works. So, for me, I find it much more instructive to <em>watch<\/em> how some bits of TeX actually work by stepping through the C code as TeX is executing \u2013 single-stepping via the Visual Studio interface. However, before attempting to do that I spent some time using regular expressions to &#8220;tidy up&#8221; the machine generated C code produced by Web2C \u2013 the raw C code (produced by Web2C) is almost impossible to read\/follow. At present, the &#8220;tidied C code&#8221; is still far from &#8220;easily legible code&#8221;, but it&#8217;s gradually improving, especially as I copy\/paste explanatory text from TeX.WEB into TeX.C. Many parts of TeX (algorithms) are truly fiendishly complex (line-breaking, hyphenation, math typesetting, etc&#8230;) so I doubt I&#8217;ll spend too much time probing those inner depths. Whilst being in awe at the sophistication and complexity of the algorithms inside TeX, I do confess that, at times, the C code is, in places, somewhat spaghetti-like. For example, there is a significant number of global variables and some individual globals are used for more than 1 purpose. Additionally, there is extensive use of &#8220;goto&#8221; statements, causing the code to jump all over the place.<\/p>\n<h2>Some confusion starts to ease<\/h2>\n<p>Despite the difficulty in following the execution of TeX.C, it is nevertheless fascinating to watch TeX actually run: Parsing the input file, acting on catcode values, creating tokens, defining macros, building boxes, running the page-builder and shipping out pages. Although I&#8217;m only just starting to explore TeX via C code, it has, for me, started to lift some of the confusion surrounding the TeX language \u2013 even if I have barely scratched the surface of this truly extraordinary program.<\/p>\n<h2>A new series of posts&#8230;?<\/h2>\n<p>My plan is to write a series of short, but fairly frequent, posts based on some aspects of TeX&#8217;s internals: To relate\/use those internals to explain, with examples, some parts of the TeX language semantics. At least, in areas that I found tricky to understand and ones that, I hope, might be instructive\/useful for others.<\/p>\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u00a0 Introduction: From WEB to C, a bit of history\/background For some time I&#8217;d wanted to build TeX (the original Knuth version) from the WEB source code, but the relatively complex process to generate C from WEB meant it was one of those &#8220;tasks&#8221; I kept putting off. Well, back in early 2013 I finally [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[28,14],"tags":[],"class_list":["post-3570","post","type-post","status-publish","format-standard","hentry","category-c-programming-miscellaneous","category-tex-general"],"blocksy_meta":[],"_links":{"self":[{"href":"https:\/\/www.readytext.co.uk\/index.php?rest_route=\/wp\/v2\/posts\/3570","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.readytext.co.uk\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.readytext.co.uk\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.readytext.co.uk\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.readytext.co.uk\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=3570"}],"version-history":[{"count":13,"href":"https:\/\/www.readytext.co.uk\/index.php?rest_route=\/wp\/v2\/posts\/3570\/revisions"}],"predecessor-version":[{"id":4022,"href":"https:\/\/www.readytext.co.uk\/index.php?rest_route=\/wp\/v2\/posts\/3570\/revisions\/4022"}],"wp:attachment":[{"href":"https:\/\/www.readytext.co.uk\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=3570"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.readytext.co.uk\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=3570"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.readytext.co.uk\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=3570"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}