Up to Main Index                             Up to Journal for April, 2023

                    JOURNAL FOR THURSDAY 20TH APRIL, 2023
______________________________________________________________________________

SUBJECT: Unsigned integer example and new dim built-in
   DATE: Thu 20 Apr 19:55:53 BST 2023

With people waiting for me to release the work I’ve been doing adding unsigned
integers to Mere, I’ve been asked if I can show an example of it being used.
Seems some people are not sure if they do, or don’t, like the unsigned integer
syntax and want to see more examples of it.

Hrm, okay. Let me rummage around and see what I have. At the moment I only
have one example — apart from all the tests I’m updating/writing. I’m also bug
fixing thanks to failing tests :)

This example program displays the first 128 ASCII characters in a table:


    >cat ascii.mr
    /*
    A mere ASCII table...

    A program to display an ASCII reference table with the ASCII values
    displayed in binary, hexadecimal, octal and decimal.
    */

    columns = 3 // Number of columns to use

    // Fill ASCII data with non-printable control codes
    table = [uint](
      0x00u "NUL", 0x01u "SOH", 0x02u "STX", 0x03u "ETX", 0x04u "EOT",
      0x05u "ENQ", 0x06u "ACK", 0x07u "\\a", 0x08u "\\b", 0x09u "\\t",
      0x0Au "\\n", 0x0Bu "\\v", 0x0Cu "\\f", 0x0Du "\\r", 0x0Eu "SO",
      0x0Fu "SI",  0x10u "DLE", 0x11u "DC1", 0x12u "DC2", 0x13u "DC3",
      0x14u "DC4", 0x15u "NAK", 0x16u "SYN", 0x17u "ETB", 0x18u "CAN",
      0x19u "EM",  0x1Au "SUB", 0x1Bu "ESC", 0x1Cu "FS",  0x1Du "GS",
      0x1Eu "RS",  0x1Fu "US",  0x7Fu "DEL"
    )

    // Add directly printable ASCII characters
    x = 0x20u
    xloop:
      table[x] = sprintf "%2c" x
      if x++ < 0x7Fu; goto xloop

    l = len table

    // Calculate rows per column
    rows = l / columns
    if l % columns <> 0; rows++

    // Build headings based on number of columns
    header = dim []string() 2
    h = 0
    hloop:
      header[0] += sprintf "%-8s %3s %3s %3s %3s  ",
                           "Binary"   "Hex" "Oct" "Dec" "Asc"
      header[1] += sprintf "%-8s %3s %3s %3s %3s  ",
                           "--------" "---" "---" "---" "---"
      if (h += rows) < l; goto hloop

    // Print headings
    println header[0]
    println header[1]

    // Print table
    r = 0
    rloop:
      c = r
      cloop:
        printf "%08b x%02[1]X %03[1]o %03[1]d %-3s  " c table[uint c]
        if (c += rows) < l; goto cloop
      println
      if r++ < rows; goto rloop

    >mere ascii.mr
    Binary  Hex Oct Dec Asc  Binary  Hex Oct Dec Asc  Binary  Hex Oct Dec Asc
    ------- --- --- --- ---  ------- --- --- --- ---  ------- --- --- --- ---
    0000000 x00 000 000 NUL  0101011 x2B 053 043  +   1010110 x56 126 086  V
    0000001 x01 001 001 SOH  0101100 x2C 054 044  ,   1010111 x57 127 087  W
    0000010 x02 002 002 STX  0101101 x2D 055 045  -   1011000 x58 130 088  X
    0000011 x03 003 003 ETX  0101110 x2E 056 046  .   1011001 x59 131 089  Y
    0000100 x04 004 004 EOT  0101111 x2F 057 047  /   1011010 x5A 132 090  Z
    0000101 x05 005 005 ENQ  0110000 x30 060 048  0   1011011 x5B 133 091  [
    0000110 x06 006 006 ACK  0110001 x31 061 049  1   1011100 x5C 134 092  \
    0000111 x07 007 007 \a   0110010 x32 062 050  2   1011101 x5D 135 093  ]
    0001000 x08 010 008 \b   0110011 x33 063 051  3   1011110 x5E 136 094  ^
    0001001 x09 011 009 \t   0110100 x34 064 052  4   1011111 x5F 137 095  _
    0001010 x0A 012 010 \n   0110101 x35 065 053  5   1100000 x60 140 096  `
    0001011 x0B 013 011 \v   0110110 x36 066 054  6   1100001 x61 141 097  a
    0001100 x0C 014 012 \f   0110111 x37 067 055  7   1100010 x62 142 098  b
    0001101 x0D 015 013 \r   0111000 x38 070 056  8   1100011 x63 143 099  c
    0001110 x0E 016 014 SO   0111001 x39 071 057  9   1100100 x64 144 100  d
    0001111 x0F 017 015 SI   0111010 x3A 072 058  :   1100101 x65 145 101  e
    0010000 x10 020 016 DLE  0111011 x3B 073 059  ;   1100110 x66 146 102  f
    0010001 x11 021 017 DC1  0111100 x3C 074 060  <   1100111 x67 147 103  g
    0010010 x12 022 018 DC2  0111101 x3D 075 061  =   1101000 x68 150 104  h
    0010011 x13 023 019 DC3  0111110 x3E 076 062  >   1101001 x69 151 105  i
    0010100 x14 024 020 DC4  0111111 x3F 077 063  ?   1101010 x6A 152 106  j
    0010101 x15 025 021 NAK  1000000 x40 100 064  @   1101011 x6B 153 107  k
    0010110 x16 026 022 SYN  1000001 x41 101 065  A   1101100 x6C 154 108  l
    0010111 x17 027 023 ETB  1000010 x42 102 066  B   1101101 x6D 155 109  m
    0011000 x18 030 024 CAN  1000011 x43 103 067  C   1101110 x6E 156 110  n
    0011001 x19 031 025 EM   1000100 x44 104 068  D   1101111 x6F 157 111  o
    0011010 x1A 032 026 SUB  1000101 x45 105 069  E   1110000 x70 160 112  p
    0011011 x1B 033 027 ESC  1000110 x46 106 070  F   1110001 x71 161 113  q
    0011100 x1C 034 028 FS   1000111 x47 107 071  G   1110010 x72 162 114  r
    0011101 x1D 035 029 GS   1001000 x48 110 072  H   1110011 x73 163 115  s
    0011110 x1E 036 030 RS   1001001 x49 111 073  I   1110100 x74 164 116  t
    0011111 x1F 037 031 US   1001010 x4A 112 074  J   1110101 x75 165 117  u
    0100000 x20 040 032      1001011 x4B 113 075  K   1110110 x76 166 118  v
    0100001 x21 041 033  !   1001100 x4C 114 076  L   1110111 x77 167 119  w
    0100010 x22 042 034  "   1001101 x4D 115 077  M   1111000 x78 170 120  x
    0100011 x23 043 035  #   1001110 x4E 116 078  N   1111001 x79 171 121  y
    0100100 x24 044 036  $   1001111 x4F 117 079  O   1111010 x7A 172 122  z
    0100101 x25 045 037  %   1010000 x50 120 080  P   1111011 x7B 173 123  {
    0100110 x26 046 038  &   1010001 x51 121 081  Q   1111100 x7C 174 124  |
    0100111 x27 047 039  '   1010010 x52 122 082  R   1111101 x7D 175 125  }
    0101000 x28 050 040  (   1010011 x53 123 083  S   1111110 x7E 176 126  ~
    0101001 x29 051 041  )   1010100 x54 124 084  T   1111111 x7F 177 127 DEL
    0101010 x2A 052 042  *   1010101 x55 125 085  U
    >


Pretty, isn’t it? :|

Astute readers will notice an odd line in the middle of the code:


    header = dim []string() 2


After some user feedback, I’ve added a dimension array “dim” built-in. This
allows for the easy creation of arrays with a specified length, all elements
are set to the zero value of the array’s type. For example:


    w = dim []int() 5
    x = dim []uint() 3
    y = dim []string() 7
    z = dim w 2           // here we use w's type for z's array
    println literal w
    println literal x
    println literal y
    println literal z


When run, the above code produces:


    []int(0 0 0 0 0)
    []uint(0u 0u 0u)
    []string("" "" "" "" "" "" "")
    []int(0 0)


This can be combined with normal array definitions. For example:


    x = []int 1, dim []int() 7, 9
    println literal x


Will produce:


    []int(1 0 0 0 0 0 0 0 9)


Using this technique the ASCII table, in the first example, could have been
initialised as:


    // Fill ASCII data with non-printable control codes
    table = []string(
      "NUL", "SOH", "STX", "ETX", "EOT", "ENQ", "ACK", "\\a", "\\b", "\\t",
      "\\n", "\\v", "\\f", "\\r", "SO",  "SI",  "DLE", "DC1", "DC2", "DC3",
      "DC4", "NAK", "SYN", "ETB", "CAN", "EM",  "SUB", "ESC", "FS",  "GS",
      "RS",  "US",  dim []string() 95, "DEL",
    )

    // Add directly printable ASCII characters
    x = 0x20
    xloop:
      table[x] = sprintf "%2c" x
      if x++ < 0x7F; goto xloop


But then I wouldn’t have an example of using unsigned integers to show :(

Bug fixes, unsigned integers and the dim built-in will be available in the
next release.

--
Diddymus


  Up to Main Index                             Up to Journal for April, 2023