Representation-of-Numeric-and-Textual-Data.html 6.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. <html lang="en">
  2. <head>
  3. <title>Representation of Numeric and Textual Data - avram - a virtual machine code interpreter</title>
  4. <meta http-equiv="Content-Type" content="text/html">
  5. <meta name="description" content="avram - a virtual machine code interpreter">
  6. <meta name="generator" content="makeinfo 4.13">
  7. <link title="Top" rel="start" href="index.html#Top">
  8. <link rel="up" href="Virtual-Machine-Specification.html#Virtual-Machine-Specification" title="Virtual Machine Specification">
  9. <link rel="prev" href="File-Format.html#File-Format" title="File Format">
  10. <link rel="next" href="Filter-Mode-Interface.html#Filter-Mode-Interface" title="Filter Mode Interface">
  11. <link href="http://www.gnu.org/software/texinfo/" rel="generator-home" title="Texinfo Homepage">
  12. <meta http-equiv="Content-Style-Type" content="text/css">
  13. <style type="text/css"><!--
  14. pre.display { font-family:inherit }
  15. pre.format { font-family:inherit }
  16. pre.smalldisplay { font-family:inherit; font-size:smaller }
  17. pre.smallformat { font-family:inherit; font-size:smaller }
  18. pre.smallexample { font-size:smaller }
  19. pre.smalllisp { font-size:smaller }
  20. span.sc { font-variant:small-caps }
  21. span.roman { font-family:serif; font-weight:normal; }
  22. span.sansserif { font-family:sans-serif; font-weight:normal; }
  23. --></style>
  24. </head>
  25. <body>
  26. <div class="node">
  27. <a name="Representation-of-Numeric-and-Textual-Data"></a>
  28. <p>
  29. Next:&nbsp;<a rel="next" accesskey="n" href="Filter-Mode-Interface.html#Filter-Mode-Interface">Filter Mode Interface</a>,
  30. Previous:&nbsp;<a rel="previous" accesskey="p" href="File-Format.html#File-Format">File Format</a>,
  31. Up:&nbsp;<a rel="up" accesskey="u" href="Virtual-Machine-Specification.html#Virtual-Machine-Specification">Virtual Machine Specification</a>
  32. <hr>
  33. </div>
  34. <h3 class="section">2.4 Representation of Numeric and Textual Data</h3>
  35. <p>As noted already, virtual code applications are specified by functions
  36. operating on elements of a set having the properties described in
  37. <a href="Raw-Material.html#Raw-Material">Raw Material</a>, which are convenient to envision as ordered binary trees or
  38. <a name="index-trees-158"></a><a name="index-g_t_0040code_007bnil_007d-159"></a>pairs of <code>nil</code>. However, virtual code applications normally deal
  39. with numeric or textual data, for example when they refer to the
  40. contents of a text file. It is therefore necessary for the application
  41. and the virtual machine emulator to agree on a way of describing textual
  42. or numeric data with these trees.
  43. <p>The purpose of this section is to explain the basic data structures used
  44. in the exchange of information between <code>avram</code> and a virtual code
  45. application. For example, an explanation is needed for statements like
  46. &ldquo;an application invoked with the <samp><span class="option">--baz</span></samp> option is expected to
  47. return a pair <code>(</code><var>foo</var><code>,</code><var>bar</var><code>)</code>, where <var>foo</var> is a
  48. <a name="index-strings-160"></a><a name="index-character-strings-161"></a><a name="index-lists-162"></a>list of character strings <small class="dots">...</small>&rdquo;, that are made subsequently in this
  49. document. Such statements should be understood as referring to the trees
  50. representing the pairs, lists, character strings, etc., according to the
  51. conventions explained below.
  52. <dl>
  53. <dt><em>Characters</em><dd><a name="index-character-codes-163"></a>An arbitrarily chosen set of 256 trees is used to represent the
  54. character set. They are listed in <a href="Character-Table.html#Character-Table">Character Table</a>. For example,
  55. the letter <code>A</code> is represented by
  56. <code>(nil,(((nil,(nil,(nil,nil))),nil),(nil,nil)))</code>. That means that
  57. when an application wants the letter <code>A</code> written to a text file, it
  58. returns something with this tree in it.
  59. <br><dt><em>Booleans</em><dd><a name="index-booleans-164"></a>The value of <code>false</code> is represented by <code>nil</code>, and the value of
  60. <code>true</code> is represented by <code>(nil,nil)</code>.
  61. <br><dt><em>Pairs</em><dd><a name="index-pairs-165"></a>Given any two items of data <var>x1</var> and <var>x2</var>, having the respective
  62. representations <var>r1</var> and <var>r2</var>, the pair <code>(</code><var>x1</var><code>,</code><var>x2</var><code>)</code> has the
  63. representation <code>cons(</code><var>r1</var><code>,</code><var>r2</var><code>)</code>.
  64. <br><dt><em>Lists</em><dd><a name="index-lists-166"></a>A list of the items <var>x1</var>, <var>x2</var> <small class="dots">...</small> <var>xn</var> with respective
  65. representations <var>r1</var> through <var>rn</var> is represented by the tree
  66. <code>cons(</code><var>r1</var><code>,cons(</code><var>r2</var><code>...cons(</code><var>rn</var><code>,nil)...))</code>. In other words,
  67. lists are represented as pairs whose left sides are the heads and whose
  68. right sides are the tails. The empty list is identified with
  69. <code>nil</code>. Lists of arbitrary finite length can be accommodated.
  70. <br><dt><em>Naturals</em><dd><a name="index-naturals-167"></a>A number of the form <var>b0</var><code> + 2</code><var>b1</var><code> + 4</code><var>b2</var><code> + ... +
  71. 2^n </code><var>bn</var>, where each <var>b</var><code>i</code> is <code>0</code> or <code>1</code>, is
  72. represented by a tree of the form
  73. <code>cons(</code><var>t0</var><code>,cons(</code><var>t1</var><code>...cons(</code><var>tn</var><code>,nil)...))</code>
  74. where each <var>t</var><code>i</code> is <code>nil</code> if the corresponding
  75. <var>b</var><code>i</code> is <code>0</code>, and <code>(nil,nil)</code> otherwise. Note that
  76. the numbers <var>b</var><code>i</code> are exactly the bits written in the binary
  77. expansion of the number, with <var>b0</var> being the least significant
  78. bit.
  79. <br><dt><em>Strings</em><dd><a name="index-strings-168"></a><a name="index-character-strings-169"></a>are represented as lists of characters.
  80. </dl>
  81. <p><a name="index-types-170"></a><code>avram</code> imposes no more of a &ldquo;type discipline&rdquo; than necessary to
  82. a workable interface between it and an application. This selection of
  83. types and constructors should not be seen as constraining what a
  84. compiler writer may wish to have in a source language.
  85. </body></html>