home *** CD-ROM | disk | FTP | other *** search
/ MacHack 1998 / MacHack 1998.toast / Programming Contest / Problems Folder / Problem 10 - Interpreter / Solution.p < prev   
Encoding:
Text File  |  1998-06-08  |  2.3 KB  |  71 lines  |  [TEXT/CWIE]

  1. (*
  2. Problem 10 - Interpreter
  3.  
  4. Write an interpreter for this simple 32-bit integer language with 26 registers labeled A-Z.
  5.  
  6. type RegisterArray = array[0..25] of UInt32;
  7.  
  8. procedure Interpret( source: Handle; var result: RegisterArray );
  9.  
  10. source consists of a number of lines of the form:
  11.  
  12. [<label>:][<tab><instruction><tab><operand-list>]<cr>
  13.  
  14. <label> is a sequence of at least two alphanumerics (0-9a-zA-Z_) (casesensitive).
  15. <operand-list> is a sequence of operators seperated by commas
  16. other than the tabs and crs, no white space is allowed.
  17.  
  18. <instruction> (and appropriate operands are:
  19.  
  20. MOVE    <value>,<register>            (set <register> to <value>)
  21. ADD     <value1>,<value2>,<register>  (set <register> to <value1>+<value2>)
  22. SUB     <value1>,<value2>,<register>  (set <register> to <value1>-<value2>)
  23. JMP     <label>                       (jump to line labeled with <label>)
  24. JEQ     <value1>,<value2>,<label>     (jump to <label> if <value1> = <value2>)
  25. JLE     <value1>,<value2>,<label>     (jump to <label> if <value1> <= <value2>)
  26. JSR     <label>                       (jump to subroutine at <label>)
  27. RTS                                   (return from subroutine)
  28. PUSH    <value>                       (push value on to stack)
  29. POP     <register>                    (pop value from stack)
  30. [others?]
  31.  
  32. <register> is a single letter
  33. <number> is an optioanl minus sign followed by decimal digits
  34. <value> is a <register> or a <number>
  35.  
  36. Note that the data stack and subroutine stack are independent stacks.  A RTS is used to stop the program (that is, consider that you have JSRed to the initial line of the source handle).  For example:
  37.  
  38.     PUSH    10
  39.     JSR    setA
  40.     JSE setB
  41.     RTS
  42. setA:
  43.     POP    A
  44.     RTS
  45. setB:    MOVE    A,B
  46.     RTS
  47.     
  48. Interpret takes the source handle and JSRs to it, presetting the registers according to the register array.  When the code returns, the register array is set to the final value of all the registers.  Both subroutine and data stacks should be large (at least 1000 entries each).
  49. *)
  50.  
  51. unit Solution;
  52.  
  53. interface
  54.  
  55. // Do not modify the interface
  56.  
  57.     uses
  58.         Types, Files;
  59.         
  60. type RegisterArray = array[0..25] of SInt32;
  61.  
  62. procedure Interpret( source: Handle; var result: RegisterArray );
  63.  
  64. implementation
  65.  
  66. // Fill in your solution and then submit this folder
  67.  
  68. // Team Name: FILL IN YOUR TEAM NAME!
  69.  
  70. end.
  71.