{"id":355,"date":"2024-09-24T20:14:52","date_gmt":"2024-09-24T20:14:52","guid":{"rendered":"https:\/\/permutationcity.co.uk\/bp\/?p=355"},"modified":"2024-10-01T18:41:26","modified_gmt":"2024-10-01T18:41:26","slug":"choosing-wren","status":"publish","type":"post","link":"https:\/\/permutationcity.co.uk\/bp\/2024\/09\/24\/choosing-wren\/","title":{"rendered":"Choosing Wren?"},"content":{"rendered":"\n<p>One of my friends was thinking about <a href=\"https:\/\/wren.io\/\">Wren<\/a> earlier in the year to run scripts in a game engine.\nIt was created by Bob Nystrom who wrote\n<a href=\"https:\/\/gameprogrammingpatterns.com\/\">Game Programming Patterns<\/a>\nwhich I <a href=\"https:\/\/permutationcity.co.uk\/bp\/2024\/04\/16\/game-programming-patterns\/\">reviewed earlier<\/a>.\nI thought Wren looked interesting so it went on my list of topics to explore.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"the-language\">The language<\/h2>\n\n\n\n<p>It describes itself:<\/p>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<p>Think Smalltalk in a Lua-sized package with a dash of Erlang and wrapped up in a familiar, modern syntax.<\/p>\n<\/blockquote>\n\n\n\n<p>Alongside the summary:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Small with the virtual machine at only &#8220;4,000 semicolons&#8221;.<\/li>\n\n\n\n<li>Fast single-pass compiler with tight bytecode.<\/li>\n\n\n\n<li>Class-based from the ground up.<\/li>\n\n\n\n<li>Concurrent with lightweight <a href=\"https:\/\/en.wikipedia.org\/wiki\/Fiber_(computer_science)\">fibres<\/a>.<\/li>\n<\/ul>\n\n\n\n<p>And looks like this:<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;showPanel&quot;:true,&quot;languageLabel&quot;:&quot;language&quot;,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;clike&quot;,&quot;mime&quot;:&quot;text\/x-c++src&quot;,&quot;theme&quot;:&quot;material&quot;,&quot;lineNumbers&quot;:false,&quot;styleActiveLine&quot;:false,&quot;lineWrapping&quot;:true,&quot;readOnly&quot;:true,&quot;fileName&quot;:&quot;C++&quot;,&quot;language&quot;:&quot;C++&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;cpp&quot;}\">System.print(&quot;Hello, world!&quot;)\n\nclass Wren {\n  flyTo(city) {\n    System.print(&quot;Flying to %(city)&quot;)\n  }\n}\n\nvar adjectives = Fiber.new {\n  [&quot;small&quot;, &quot;clean&quot;, &quot;fast&quot;].each {|word| Fiber.yield(word) }\n}\n\nwhile (!adjectives.isDone) System.print(adjectives.call())<\/pre><\/div>\n\n\n\n<p>I&#8217;ve read over the guides and it comes out as a mostly vanilla scripting language.\nI don&#8217;t mean that as a slight.\nIf you understand C-style object-oriented languages then you should get to grips with it very fast.\nThe documentation is easy to read,\nI went through it in a couple of sessions.<\/p>\n\n\n\n<p>We&#8217;ll rush through the documentation:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><a href=\"https:\/\/wren.io\/syntax.html#comments\">Comments<\/a>: single line, multiline, nestable<\/li>\n\n\n\n<li><a href=\"https:\/\/wren.io\/syntax.html#identifiers\">Identifiers<\/a>: initial underscore used to indicate class fields<\/li>\n\n\n\n<li><a href=\"https:\/\/wren.io\/syntax.html#newlines\">Newlines<\/a>: separate statements<\/li>\n\n\n\n<li><a href=\"https:\/\/wren.io\/syntax.html#blocks\">Blocks<\/a>: curly braces<\/li>\n\n\n\n<li><a href=\"https:\/\/wren.io\/values.html\">Immutable values<\/a>: <a href=\"https:\/\/wren.io\/values.html#booleans\"><code>Bool<\/code><\/a>, <a href=\"https:\/\/wren.io\/values.html#numbers\"><code>Num<\/code><\/a>, <a href=\"https:\/\/wren.io\/values.html#strings\"><code>String<\/code><\/a>, <a href=\"https:\/\/wren.io\/values.html#ranges\"><code>Range<\/code><\/a>, <a href=\"https:\/\/wren.io\/values.html#null\"><code>Null<\/code><\/a><\/li>\n\n\n\n<li>Mutable values: <a href=\"https:\/\/wren.io\/lists.html\"><code>List<\/code><\/a>, <a href=\"https:\/\/wren.io\/maps.html\"><code>Map<\/code><\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/wren.io\/method-calls.html\">Methods<\/a>: object-based, class-based, overloadable by <a href=\"https:\/\/en.wikipedia.org\/wiki\/Arity\">arity<\/a>, <a href=\"https:\/\/wren.io\/method-calls.html#getters\">getters<\/a>, <a href=\"https:\/\/wren.io\/method-calls.html#setters\">setters<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/wren.io\/control-flow.html\">Control flow<\/a>: <a href=\"https:\/\/wren.io\/control-flow.html#if-statements\"><code>if<\/code><\/a>, <a href=\"https:\/\/wren.io\/control-flow.html#while-statements\"><code>while<\/code><\/a>, <a href=\"https:\/\/wren.io\/control-flow.html#for-statements\"><code>for<\/code><\/a> over range, short-circuit <a href=\"https:\/\/wren.io\/control-flow.html#logical-operators\">logic operators<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/wren.io\/variables.html\">Variables<\/a>: <a href=\"https:\/\/wren.io\/variables.html#scope\">true block scope<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/wren.io\/functions.html\">Functions<\/a>: separate from methods, quick syntax for <a href=\"https:\/\/wren.io\/functions.html#block-arguments\">passing blocks to functions<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/wren.io\/classes.html\">Classes<\/a>: <a href=\"https:\/\/wren.io\/classes.html#methods\">methods<\/a>, <a href=\"https:\/\/wren.io\/classes.html#getters\">getters<\/a>, <a href=\"https:\/\/wren.io\/classes.html#setters\">setters<\/a>, <a href=\"https:\/\/wren.io\/classes.html#operators\">operator overloading<\/a>, <a href=\"https:\/\/wren.io\/classes.html#implicit-this\">implicit <code>this<\/code><\/a>, <a href=\"https:\/\/wren.io\/classes.html#constructors\">named constructors<\/a>, <a href=\"https:\/\/wren.io\/classes.html#fields\">private fields<\/a>, <a href=\"https:\/\/wren.io\/classes.html#metaclasses-and-static-members\">static fields<\/a>, <a href=\"https:\/\/wren.io\/classes.html#inheritance\">inheritance<\/a> does not include static methods or constructors, <a href=\"https:\/\/wren.io\/classes.html#super\">invoking parent&#8217;s methods<\/a>, class and method <a href=\"https:\/\/wren.io\/classes.html#attributes\">attributes<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/wren.io\/concurrency.html\">Concurrency<\/a>: fibres with <a href=\"https:\/\/wren.io\/concurrency.html#invoking-fibers\">invoke<\/a> and <a href=\"https:\/\/wren.io\/concurrency.html#yielding\">yield<\/a>, can be used for <a href=\"https:\/\/wren.io\/error-handling.html#handling-runtime-errors\">runtime error handling<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/wren.io\/embedding\/\">Embedding<\/a>: include as static \/ dynamic library or directly as source, <a href=\"https:\/\/wren.io\/embedding\/slots-and-handles.html\">exchanging data with VM<\/a>, <a href=\"https:\/\/wren.io\/embedding\/calling-wren-from-c.html\">calling the VM<\/a><\/li>\n<\/ul>\n\n\n\n<p>It&#8217;s all pretty nice, either as expected or easily learned. Building it as object-oriented from the start gives it a consistency that some language lack. Personally I prefer statically to dynamically typed languages but for small tasks it doesn&#8217;t matter as much. Normally I dislike using just an underscore to indicate class fields but that&#8217;s partly because it gets used for so many other things. If it&#8217;s mandated in the language it should give consistency. Method overloading isn&#8217;t essential but it&#8217;s nice to see. The syntactic sugar for <a href=\"https:\/\/wren.io\/functions.html#block-arguments\">passing blocks to functions<\/a> would be interesting to use. Building the virtual machine into an existing system is easy although passing data back and forth looks fiddly but not hard. That said people have already gone out and made some <a href=\"https:\/\/github.com\/wren-lang\/wren\/wiki\/Language-Bindings\">bindings<\/a> already. It feels like a scripting language that hasn&#8217;t scrimped on the details.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"how-to-choose\">How to choose<\/h2>\n\n\n\n<p>I like the look of Wren but is it the right choice?\nThis is purely hypothetical for me as I don&#8217;t need a scripting language right now but\nhaving to choose tools, libraries or system is a fairly common problem.<\/p>\n\n\n\n<p>With a brief search I was able to find a long list of\n<a href=\"https:\/\/github.com\/dbohdan\/embedded-scripting-languages\">embedded scripting languages<\/a>.\nIt includes Wren but many others, names I recognise and ones I don&#8217;t.\nI heard of <a href=\"https:\/\/en.wikipedia.org\/wiki\/Lua_(programming_language)\">Lua<\/a>\nas an embeddable scripting language years ago,\nthere are 10 different projects for that in the list.\nI associate <a href=\"https:\/\/en.wikipedia.org\/wiki\/JavaScript\">JavaScript<\/a>\nwith browsers and there are another 10 more projects here.\nThere are big name languages, sometimes cut down for scripting:\n<a href=\"https:\/\/hacadacompiler.sourceforge.io\/\">Ada<\/a>,\n<a href=\"https:\/\/gitlab.com\/zsaleeba\/picoc\">C<\/a>,\n<a href=\"https:\/\/github.com\/beanshell\/beanshell\/\">Java<\/a>,\n<a href=\"https:\/\/github.com\/remobjects\/pascalscript\">Pascal<\/a>,\n<a href=\"https:\/\/github.com\/symisc\/PH7\">PHP<\/a>,\n<a href=\"https:\/\/github.com\/pocketpy\/pocketpy\">Python<\/a> and\n<a href=\"https:\/\/github.com\/mrubyc\/mrubyc\">Ruby<\/a>.\nThere are names I don&#8217;t recognise:\n<a href=\"https:\/\/github.com\/mattn\/anko\/\">Anko<\/a>,\n<a href=\"https:\/\/github.com\/berry-lang\/berry\">Berry<\/a>,\n<a href=\"https:\/\/dhall-lang.org\/\">Dhall<\/a>,\n<a href=\"https:\/\/github.com\/gluon-lang\/gluon\">Gluon<\/a>,\n<a href=\"https:\/\/janet-lang.org\/\">Janet<\/a>,\n<a href=\"https:\/\/github.com\/red\/red\">Red<\/a>,\n<a href=\"http:\/\/squirrel-lang.org\/\">Squirrel<\/a> and\n<a href=\"https:\/\/github.com\/Ratstail91\/Toy\">Toy<\/a>.\nMany many options to research.\nIt&#8217;s not just languages where you find lots of options.\nI mentioned <a href=\"https:\/\/permutationcity.co.uk\/bp\/2024\/09\/03\/saving-pennies\/#little-vs-big\">JSON parsers<\/a>\nrecently and found a list of 43 of them.\nHow do you make a choice?<\/p>\n\n\n\n<p>First ask what you&#8217;re really trying to do.\nIf you want to include an embedded scripting language in your game engine then, maybe,\nyou want entities within your game to have flexible behaviours.\nMaybe you that don&#8217;t want to recompile the code after every design change.\nMaybe it&#8217;s going to be part of the game itself.\nWho is going to be writing these scripts?\nIs it coders, designers, artists or the players themselves.\nDo you expect them to be familiar with programming languages or completely unfamiliar?\nWill they be willing to take time to learn or does the language have to be understandable.\nThere is a gamut of languages including\n<a href=\"https:\/\/en.wikipedia.org\/wiki\/Visual_programming_language\">visual ones<\/a> or\n<a href=\"https:\/\/en.wikipedia.org\/wiki\/Esoteric_programming_language\">esoteric ones<\/a>.\nYou&#8217;ll probably know roughly what you want but it doesn&#8217;t hurt to formalise it.\nIf you don&#8217;t have specifics for some areas that&#8217;s fine,\njust keep what you do have in mind.<\/p>\n\n\n\n<p>Some of these things might be non-negotiable and\nthat could easily be price or licencing arrangements.\nFor an individual you might not have the money to shell out for a library.\nFor a company it&#8217;s more likely to be possible but they might not be willing,\nthere&#8217;s not a budget for it.\nCompanies tend to have core products they work on and\ntheir developers will have expertise in those.\nIf you&#8217;re looking at implementing a language or a library outside of that it&#8217;s good to at least consider a purchase.\nDeveloper time isn&#8217;t free and creating something in house takes time to start with and\nthen has to be maintained afterwards.\nOn the other hand the right licence might get you software for free but come with strings attached.\nAny sort of <a href=\"https:\/\/en.wikipedia.org\/wiki\/Copyleft\">copyleft<\/a>\nlicence is probably no go for a commercial product.\nHowever there are a \n<a href=\"https:\/\/en.wikipedia.org\/wiki\/Comparison_of_free_and_open-source_software_licenses\">great many sorts of licences<\/a>\nand we should be thankful for that.\nIf your company doesn&#8217;t already have a policy for these it should get one.\nIdeally a list of acceptable licences a developer can use without further investigation and\nmethods for fulfilling the terms of those licences.\nOften that&#8217;s just releasing the licence alongside the finished software.<\/p>\n\n\n\n<p>The implementation language may or may not be a barrier to a library.\nUnless you&#8217;re getting a pre-built library I&#8217;d certainly prefer something I&#8217;m familiar with.\nIf you&#8217;re having to debug issues being able to get in there and see what&#8217;s happening is useful.\nOn the other hand you could treat it as a black box,\nthat&#8217;s probably what you&#8217;ll have to do if it&#8217;s a commercial product.\nIt might matter how hard it&#8217;s going to be to integrate into your build system.\nWith some that can be fairly simple.\nI&#8217;ve been doing a lot of work with <a href=\"https:\/\/vcpkg.io\/\">vcpkg<\/a> recently\nthat allows you to build packages locally and then cache them for later.\nOnce it&#8217;s in place that can mean only a few lines to integrate a new package.\nAs it happens <a href=\"https:\/\/vcpkg.io\/en\/package\/wren\">Wren<\/a> is available on there,\nso is <a href=\"https:\/\/vcpkg.io\/en\/package\/lua\">Lua<\/a>.\nAnother alternative is <a href=\"https:\/\/cmake.org\/\">cmake<\/a> and\nit&#8217;s <a href=\"https:\/\/cmake.org\/cmake\/help\/latest\/module\/ExternalProject.html\">ExternalProject<\/a>\nwhich is a bit longer but more easily customisable.<\/p>\n\n\n\n<p>For many companies it might matter how much support the library will have. Some projects will be under active development, some might only be getting occasional bug fixes, and some haven&#8217;t been updated in a decade. It certainly can be a risk if their might be bugs but further development is unlikely. With an open source project you can always go in and try and fix it yourself but that means time and effort. With a closed source product then you can&#8217;t see the code but, hopefully, the sellers will be able to provide support. Such support might not be immediate so you could be left having to work around problems. If you can find a open source project that&#8217;s under active development, ideally with multiple contributors, that is probably best.<\/p>\n\n\n\n<p>So far these are all fairly large scale considerations.\nThat means you can, say, go through a big list of embedded scripting languages and\nthrow out those that are obviously unsuited for the job.\nOnce you&#8217;ve done that you&#8217;re left with a list of candidates.\nIt&#8217;s time to metaphorically roll up your sleeves and go through them one by one.\nIf there are a lot of them you should probably try and get a rough impression to prioritise further investigation.\nWhen you have a smaller number there is definitely going to be further reading.\nYou might want to try building or integrating things to see how it goes.<\/p>\n\n\n\n<p>This can be tricky.\nHow much time do you spend?\nThe more you investigate each library, or language, the better idea whether it will suit your purposes.\nOn the other hand all that is time and effort.\nIt could be that you don&#8217;t need a perfect system.\nIf I were looking for another JSON parser I would:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Check the licencing to make sure it&#8217;s acceptable.<\/li>\n\n\n\n<li>See if the API looks okay.<\/li>\n\n\n\n<li>Make sure it builds.<\/li>\n\n\n\n<li>Try integrating it and see what happens.<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"in-the-end\">In the end<\/h2>\n\n\n\n<p>If you want to decide to use Wren, Lua or Moonscript you&#8217;re going to have decide for yourself. I think it looks nice but that&#8217;s just me. Meanwhile I&#8217;m wondering what unit testing framework is best. I&#8217;ve used <a href=\"https:\/\/google.github.io\/googletest\/\">GoogleTest<\/a>, <a href=\"https:\/\/www.boost.org\/doc\/libs\/1_75_0\/libs\/test\/doc\/html\/index.html\">Boost Test<\/a>, <a href=\"https:\/\/github.com\/unittest-cpp\/unittest-cpp\">unittest-cpp<\/a> and <a href=\"https:\/\/github.com\/catchorg\/Catch2\">Catch2<\/a>. There are undoubtedly more out there, <a href=\"https:\/\/en.wikipedia.org\/wiki\/List_of_unit_testing_frameworks#C++\">looks like it<\/a>. I&#8217;ve got some reading to do.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>One of my friends was thinking about Wren earlier in the year to run scripts in a game engine. It was created by Bob Nystrom who wrote Game Programming Patterns which I reviewed earlier. I thought Wren looked interesting so it went on my list of topics to explore. The language It describes itself: Think [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_import_markdown_pro_load_document_selector":0,"_import_markdown_pro_submit_text_textarea":"","footnotes":""},"categories":[34],"tags":[38],"class_list":["post-355","post","type-post","status-publish","format-standard","hentry","category-review","tag-language"],"_links":{"self":[{"href":"https:\/\/permutationcity.co.uk\/bp\/wp-json\/wp\/v2\/posts\/355","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/permutationcity.co.uk\/bp\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/permutationcity.co.uk\/bp\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/permutationcity.co.uk\/bp\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/permutationcity.co.uk\/bp\/wp-json\/wp\/v2\/comments?post=355"}],"version-history":[{"count":4,"href":"https:\/\/permutationcity.co.uk\/bp\/wp-json\/wp\/v2\/posts\/355\/revisions"}],"predecessor-version":[{"id":375,"href":"https:\/\/permutationcity.co.uk\/bp\/wp-json\/wp\/v2\/posts\/355\/revisions\/375"}],"wp:attachment":[{"href":"https:\/\/permutationcity.co.uk\/bp\/wp-json\/wp\/v2\/media?parent=355"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/permutationcity.co.uk\/bp\/wp-json\/wp\/v2\/categories?post=355"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/permutationcity.co.uk\/bp\/wp-json\/wp\/v2\/tags?post=355"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}