{"id":50,"date":"2024-12-07T12:37:34","date_gmt":"2024-12-07T12:37:34","guid":{"rendered":"https:\/\/www.soapyfrog.com\/?p=50"},"modified":"2024-12-07T12:37:34","modified_gmt":"2024-12-07T12:37:34","slug":"stretching-the-definition-of-basic-even-further","status":"publish","type":"post","link":"https:\/\/www.soapyfrog.com\/index.php\/2024\/12\/07\/stretching-the-definition-of-basic-even-further\/","title":{"rendered":"Stretching the definition of BASIC even further"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">In the last post I pondered if it&#8217;s still BASIC without <code>GOTO<\/code>. I think it is, but then how far can you go with a language before it loses its identity? Or worse, before it starts to look like Visual Basic? \ud83d\ude31<br><br>I&#8217;ve abandoned variable (and function) suffixes having semantic meaning. It&#8217;s just <code>name<\/code> and <code>left(name,3)<\/code> instead of <code>name$ <\/code>and <code>left$(name$,3)<\/code>.<br><br>I&#8217;ve abandoned <code>GOTO<\/code> and <code>GOSUB<\/code>. And of course, line numbers.<br><br>But I&#8217;ve kept <code>LET<\/code> (mandatory on some BASICs and optional on most).<br>I&#8217;ve kept <code>DIM<\/code> but I&#8217;m using this only for arrays as it was originally intended.<br>The standard numeric and text functions are all there.<br>There&#8217;s <code>FOR NEXT<\/code> and <code>WHILE WEND<\/code>.<br><br>I&#8217;ve resisted the urge (so far) to use lambdas, but as we&#8217;ll see, we do have closures, and I suspect function references and lambdas will make an appearance later on.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">What else?<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Scoping<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Variables are scoped to the block in which they are defined.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>let x=9 ' int\nsub foo()\n  let x=0.1 ' float\n  if x>0.0 \n    let x=\"fish\" ' string\n  endif\n  def bar(x as bool) as bool = not x\nend\nfor n=1 to 5:next\nprint n \n' error, n was scoped to for loop<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Here there are <em>four<\/em> different <code>x<\/code> variables in different scopes, and the variable <code>n<\/code> is scoped to the for loop and so not visible outside.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">If one were to be typing in old BASIC programs into SoapyBASIC this will certainly catch people out. A price worth paying for stronger semantic structure, I think. <\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Dynamic Arrays<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">In SoapyBASIC, you can create an array in the usual way (apart from declaring type):<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>dim q&#91;3] of int\nq&#91;0]=10\nq&#91;1]=12\nq&#91;2]=23\nq&#91;3]=77 ' error, out of bounds<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">But you can also create arrays as literals and they can be explicitly grown:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>let r=&#91;3,5,17]\nlet t=q+r '&#91;10,12,23,3,5,16]<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">They can be spread and coalesced through function calls.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>let p=&#91;3,7,11]\nfunc sum(ns... as int) as int\n  let t=0\n  for n in ns\n    t=t+n\n  next\n  return n\nendfunc\nprint sum(...p) ' 21\nprint sum(4,5,6) ' 15<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Arrays can be nested and returned from functions:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>func mkarr(n as int, v as float) as array of float\n  dim a&#91;n] of float\n  for x=0 to n-1:a&#91;x]=v:next\n  return a\nendfunc\nlet vs=mkarr(30,2.5)\n\nlet vss=&#91;vs,vs]\nprint vss is array of array of float 'true<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">I still there&#8217;s enough BASICness in the above, but also some genuinely useful structure and semantics.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Strings<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">As my SoapyBASIC interpreter (and now in progress, compiler) is written in Swift, my BASIC strings are fully Unicode.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Unlike Rust that just uses utf8 and offers other models, SoapyBASIC strings are proper strings of characters, and these represent (usually) a grapheme cluster.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>> print len(\"caf\u00e9\") '4\n> let acute=chr(&amp;h301)\n> print len(\"cafe\"+acute) '4\n> print len(\"\ud83d\ude00\") '1\n> print utf8(\"\ud83d\ude00\") '&#91;240, 159, 152, 128]\n> print scalars(\"\ud83d\ude00\") '&#91;128512]\n> print from_utf8(&#91;240, 159, 152, 128]) '\ud83d\ude00<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">So for most cases, BASIC programs will just work with strings, even if the program encounters unicode. It can offer conversions between code points, scalars and utf8 sequences. <\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>> let s=\"cafe\"+chr(&amp;h301)\n> print s len(s) scalars(s)\ncaf\u00e9 4 &#91;99, 97, 102, 101, 769]\n> let t=formc(s)\n> print t len(t) scalars(t)\ncaf\u00e9 4 &#91;99, 97, 102, 233]<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Summary<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">I think I&#8217;ve struck a good balance between BASICness and modern utility.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">As I&#8217;m now writing a proper compiler for SoapyBASIC (it&#8217;s an interpreter at the moment), I may choose some different paths here, as I&#8217;m quite likely to have the runtime for my compiler <em>NOT<\/em> written in Swift (probably just C), so the convenience of Swift won&#8217;t be there.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><\/p>\n","protected":false},"excerpt":{"rendered":"<p>In the last post I pondered if it&#8217;s still BASIC without GOTO. I think it is, but then how far can you go with a language before it loses its identity? Or worse, before it starts to look like Visual Basic? \ud83d\ude31 I&#8217;ve abandoned variable (and function) suffixes having semantic meaning. It&#8217;s just name and [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[14,13],"tags":[17,19,12,20],"class_list":["post-50","post","type-post","status-publish","format-standard","hentry","category-lang-design","category-soapybasic","tag-basic","tag-language-design","tag-soapybasic","tag-swift-lang"],"_links":{"self":[{"href":"https:\/\/www.soapyfrog.com\/index.php\/wp-json\/wp\/v2\/posts\/50","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.soapyfrog.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.soapyfrog.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.soapyfrog.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.soapyfrog.com\/index.php\/wp-json\/wp\/v2\/comments?post=50"}],"version-history":[{"count":2,"href":"https:\/\/www.soapyfrog.com\/index.php\/wp-json\/wp\/v2\/posts\/50\/revisions"}],"predecessor-version":[{"id":53,"href":"https:\/\/www.soapyfrog.com\/index.php\/wp-json\/wp\/v2\/posts\/50\/revisions\/53"}],"wp:attachment":[{"href":"https:\/\/www.soapyfrog.com\/index.php\/wp-json\/wp\/v2\/media?parent=50"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.soapyfrog.com\/index.php\/wp-json\/wp\/v2\/categories?post=50"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.soapyfrog.com\/index.php\/wp-json\/wp\/v2\/tags?post=50"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}