home *** CD-ROM | disk | FTP | other *** search
/ Power-Programmierung / CD1.mdf / euphoria / get.e < prev    next >
Text File  |  1994-01-31  |  5KB  |  289 lines

  1.         ---------------------
  2.         -- Input an Object --
  3.         ---------------------
  4.  
  5. -- Read a Euphoria object from an input stream.
  6. -- get(filenumber) returns {error_status, input_value}
  7.  
  8. -- error status values returned:
  9. global constant GET_SUCCESS = 0,
  10.         GET_EOF = -1,
  11.         GET_FAIL = 1
  12.  
  13. constant UNDEFINED_CHAR = -2
  14.  
  15. constant TRUE = 1
  16.  
  17. type natural(integer x)
  18.     return x >= 0
  19. end type
  20.  
  21. type char(integer x)
  22.     return x >= UNDEFINED_CHAR and x <= 255
  23. end type
  24.  
  25. natural input_file
  26.  
  27. char ungot_char
  28. ungot_char = UNDEFINED_CHAR
  29.  
  30.  
  31. function get_char()
  32. -- read next logical char in input stream
  33.     char temp
  34.  
  35.     if ungot_char = UNDEFINED_CHAR then
  36.     return getc(input_file)
  37.     else
  38.     temp = ungot_char
  39.     ungot_char = UNDEFINED_CHAR
  40.     return temp
  41.     end if
  42. end function
  43.  
  44.  
  45. procedure unget_char(char c)
  46. -- "unget" a character - push it back on the input stream
  47.     ungot_char = c
  48. end procedure
  49.  
  50.  
  51. procedure skip_blanks()
  52. -- skip white space
  53.     char c
  54.  
  55.     while TRUE do
  56.     c = get_char()
  57.     if not find(c, " \t\n") then
  58.         exit
  59.     end if
  60.     end while
  61.     unget_char(c)
  62. end procedure
  63.  
  64. constant ESCAPE_CHARS = "nt'\"\\r",
  65.      ESCAPED_CHARS = "\n\t'\"\\\r"
  66.  
  67. function escape_char(char c)
  68. -- return escape character
  69.     natural i
  70.  
  71.     i = find(c, ESCAPE_CHARS)
  72.     if i = 0 then
  73.     return GET_FAIL
  74.     else
  75.     return ESCAPED_CHARS[i]
  76.     end if
  77. end function
  78.  
  79.  
  80. function get_qchar()
  81. -- get a single-quoted character
  82.  
  83.     char c
  84.  
  85.     c = get_char()
  86.     if c = '\\' then
  87.     c = escape_char(get_char())
  88.     if c = GET_FAIL then
  89.         return {GET_FAIL, 0}
  90.     end if
  91.     end if
  92.     if get_char() != '\'' then
  93.     return {GET_FAIL, 0}
  94.     else
  95.     return {GET_SUCCESS, c}
  96.     end if
  97. end function
  98.  
  99.  
  100. function get_string()
  101. -- get a double-quoted character string
  102.     sequence text
  103.     char c
  104.  
  105.     text = ""
  106.     while TRUE do
  107.     c = get_char()
  108.     if c = GET_EOF or c = '\n' then
  109.         return {GET_FAIL, 0}
  110.     end if
  111.     if c = '"' then
  112.         exit
  113.     elsif c = '\\' then
  114.         c = escape_char(get_char())
  115.         if c = GET_FAIL then
  116.         return {GET_FAIL, 0}
  117.         end if
  118.     end if
  119.     text = text & c
  120.     end while
  121.     return {GET_SUCCESS, text}
  122. end function
  123.  
  124. type plus_or_minus(integer x)
  125.     return x = -1 or x = +1
  126. end type
  127.  
  128. function get_number()
  129. -- read a number
  130.     char c
  131.     plus_or_minus sign, e_sign
  132.     natural ndigits
  133.     integer hex_digit
  134.     atom mantissa, dec, e_mag, exponent
  135.  
  136.     sign = +1
  137.     mantissa = 0
  138.     e_sign = +1
  139.     e_mag = 0
  140.     ndigits = 0
  141.  
  142.     c = get_char()
  143.  
  144.     -- process sign
  145.     if c = '-' then
  146.     sign = -1
  147.     elsif c != '+' then
  148.     unget_char(c)
  149.     end if
  150.  
  151.     -- get mantissa
  152.     c = get_char()
  153.     if c = '#' then
  154.     -- process hex integer and return
  155.     while TRUE do
  156.         c = get_char()
  157.         hex_digit = find(c, "0123456789ABCDEF")-1
  158.         if hex_digit >= 0 then
  159.         ndigits = ndigits + 1
  160.         mantissa = mantissa * 16 + hex_digit
  161.         else
  162.         unget_char(c)
  163.         if ndigits > 0 then
  164.             return {GET_SUCCESS, sign * mantissa}
  165.         else
  166.             return {GET_FAIL, 0}
  167.         end if
  168.         end if
  169.     end while    
  170.     end if
  171.     -- decimal integer or floating point
  172.     while find(c, "0123456789") do
  173.     ndigits = ndigits + 1
  174.     mantissa = mantissa * 10 + (c - '0')
  175.     c = get_char()
  176.     end while
  177.     if c = '.' then
  178.     -- get fraction
  179.     c = get_char()
  180.     dec = 10
  181.     while find(c, "0123456789") do
  182.         ndigits = ndigits + 1
  183.         mantissa = mantissa + (c - '0') / dec
  184.         dec = dec * 10
  185.         c = get_char()
  186.     end while
  187.     end if
  188.     if ndigits = 0 then
  189.     return {GET_FAIL, 0}
  190.     end if
  191.     if c = 'e' or c = 'E' then
  192.     -- get exponent sign
  193.     c = get_char()
  194.     if c = '-' then
  195.         e_sign = -1
  196.     elsif c != '+' then
  197.         unget_char(c)
  198.     end if
  199.     -- get exponent magnitude 
  200.     c = get_char()
  201.     if find(c, "0123456789") then
  202.         e_mag = c - '0'
  203.         c = get_char()
  204.         while find(c, "0123456789") do
  205.         e_mag = e_mag * 10 + c - '0'
  206.         c = get_char()                
  207.         end while
  208.         unget_char(c)
  209.     else
  210.         return {GET_FAIL, 0} -- no exponent
  211.     end if
  212.     else
  213.     unget_char(c)
  214.     end if
  215.     exponent = 1
  216.     if e_sign >= 0 then
  217.     for i = 1 to e_mag do
  218.         exponent = exponent * 10
  219.     end for
  220.     else
  221.     for i = 1 to e_mag do
  222.         exponent = exponent * 0.1
  223.     end for
  224.     end if
  225.     return {GET_SUCCESS, sign * mantissa * exponent}
  226. end function
  227.  
  228.  
  229. function Get()
  230. -- read a Euphoria data object as a string of characters
  231. -- and return {error_flag, value}
  232.     char c
  233.     sequence s, e
  234.  
  235.     skip_blanks()
  236.     c = get_char()
  237.  
  238.     if find(c, "-+.0123456789#") then
  239.     unget_char(c)
  240.     return get_number()
  241.  
  242.     elsif c = '{' then
  243.     -- process a sequence
  244.     s = {}
  245.     while TRUE do
  246.         skip_blanks()
  247.         c = get_char()
  248.         if c = '}' then
  249.         return {GET_SUCCESS, s}
  250.         else
  251.         unget_char(c)
  252.         end if
  253.         e = Get()
  254.         if e[1] != GET_SUCCESS then
  255.         return e
  256.         end if
  257.         s = append(s, e[2])
  258.         skip_blanks()
  259.         c = get_char()
  260.         if c = '}' then
  261.         return {GET_SUCCESS, s}
  262.         elsif c != ',' then
  263.         return {GET_FAIL, 0}
  264.         end if
  265.     end while
  266.  
  267.     elsif c = '\"' then
  268.     return get_string()
  269.  
  270.     elsif c = '\'' then
  271.     return get_qchar()
  272.  
  273.     elsif c = -1 then
  274.     return {GET_EOF, 0}
  275.  
  276.     else
  277.     return {GET_FAIL, 0}
  278.  
  279.     end if
  280. end function
  281.  
  282.  
  283. global function get(natural file_no)
  284. -- main routine, sets input_file
  285.     input_file = file_no
  286.     return Get()
  287. end function
  288.  
  289.