Advertisement
Guest User

Untitled

a guest
Jan 22nd, 2018
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Scheme 7.83 KB | None | 0 0
  1. #!/afs/cats.ucsc.edu/courses/cmps112-wm/usr/racket/bin/mzscheme -qr
  2. ;; $Id: sbi.scm,v 1.3 2016-09-23 18:23:20-07 - - $
  3. ;;
  4. ;; NAME
  5. ;;    sbi.scm - silly basic interpreter
  6. ;;
  7. ;; SYNOPSIS
  8. ;;    sbi.scm filename.sbir
  9. ;;
  10. ;; DESCRIPTION
  11. ;;    The file mentioned in argv[1] is read and assumed to be an SBIR
  12. ;;    program, which is the executed.  Currently it is only printed.
  13. ;;
  14.  
  15.  
  16. ;; here we create our tables
  17. (define *label-table* (make-hash))
  18. (define *variable-table* (make-hash))
  19. (define *function-table* (make-hash))
  20.  
  21.  
  22. ;; here we fill the function table with the prelim. functions
  23. ;; I think we write somethin like this to uses functions
  24. ;; (printf "2 ^ 16 = ~s~n" ((symbol-get '^) 2.0 16.0))
  25.  
  26. ;; I changed symbol-get and symbol-put to require the table
  27. ;; that their grabbing from so that way we could use them for
  28. ;; other hash tables :-)
  29. (define (symbol-get table key)
  30.         (hash-ref table key))
  31. (define (symbol-put! table key value)
  32.         (printf "~n~nputting ~s in table:~s  as ~s~n~n" key table value)
  33.         (hash-set! table key value))
  34.  
  35. (for-each
  36.     (lambda (pair)
  37.             (symbol-put! *function-table* (car pair) (cadr pair)))
  38.     `(
  39.  
  40.         (log10_2 0.301029995663981195213738894724493026768189881)
  41.         (sqrt_2  1.414213562373095048801688724209698078569671875)
  42.         (div     ,(lambda (x y) (floor (/ x y))))
  43.         (log10   ,(lambda (x) (/ (log x) (log 10.0))))
  44.         (mod     ,(lambda (x y) (- x (* (div x y) y))))
  45.         (quot    ,(lambda (x y) (truncate (/ x y))))
  46.         (rem     ,(lambda (x y) (- x (* (quot x y) y))))
  47.         (+       ,+)
  48.         (*       ,*)
  49.         (-       ,-)
  50.         (/       ,/)
  51.         (^       ,expt)
  52.         (ceil    ,ceiling)
  53.         (exp     ,exp)
  54.         (floor   ,floor)
  55.         (log     ,log)
  56.         (sqrt    ,sqrt)
  57.         (sin     ,sin)
  58.         (cos     ,cos)
  59.         (tan     ,tan)
  60.         (atan    ,atan)
  61.         (let     , (lambda x (interpret-let x)))
  62.         (print   , (lambda x (displayln x))) ;;(map (lambda (x) displayln x (y))));;                  (displayln (car x))))  
  63.  
  64.      ))
  65. ;; now we fill the variable table with the prelim values pi & e
  66. (for-each
  67.     (lambda (pair)
  68.             (symbol-put! *variable-table* (car pair) (cadr pair)))
  69.     `(
  70.  
  71.         (e       2.718281828459045235360287471352662497757247093)
  72.         (pi      3.141592653589793238462643383279502884197169399)
  73. ))
  74.  
  75.  
  76. ;; here I create 'methods'
  77. ;; label table
  78. (define (get-label line-list)
  79.     (if (null? (cdr line-list))
  80.         null
  81.         (if (null? (cadr line-list))
  82.             null
  83.             (if (symbol? (cadr line-list))
  84.                 (hash-set! *label-table* (cadr line-list) (cddr line-list))
  85.                 null))))
  86.  
  87. (define (get-stmt line-list)
  88.     (if (null? (cdr line-list))
  89.         null
  90.         (if (null? (cadr line-list))
  91.             null
  92.             (if (not (symbol? (cadr line-list)))
  93.                 (cadr line-list)
  94.                 null))))
  95.  
  96.  
  97. ;; goal find first smt on a line and evaluate the statement
  98. (define (get-stmt line)
  99.      (if (null? (cdr line))
  100.         null
  101.         (if (null? (cadr line))
  102.             null
  103.             (if (symbol? (cadr line)) ;; if a label
  104.                 (cddr line);; return the next item
  105.                 (cadr line))))) ;; otherwise return stmt vthis may be wrong
  106.  
  107. ;; this method runs a single line of code
  108. ;; for this we first check if there is a label
  109. ;; or if there is a stmt
  110. ;; then we get the stmt and check the car of it
  111. ;; see if the cadr is equal to one of the six  key words
  112. ;; depending on which one we perform at action with the items
  113. ;; after the car
  114. ;; then look for next item on that list of line
  115.  
  116. ;; if the cdr is null
  117. ;; - skip
  118. ;; now check if there is a label or no label
  119.  
  120.  
  121.  
  122. ;; (begin (printf"statement not null "))  <---trash stmnt]
  123. ;; printf("The interpreter line was not null")))
  124. ;;(define (interpret-line line)
  125. ;;    (if (null? (  get-stmt line))
  126. ;;        null))
  127.        
  128.     ;;;;;;
  129. ;;        ())
  130. ;;    ( if (eq? (car (get-stmt line)) ("print"))
  131. ;;          (printf "~s~n" cadr (get-stmt line))
  132. ;;          ()) ;; do nothing, IDK IF U CAN DO THIS
  133. ;;    ( if) ;;  more if statements follow
  134.  
  135.  
  136.  
  137. (define *stderr* (current-error-port))
  138.  
  139. (define *run-file*
  140.     (let-values
  141.         (((dirpath basepath root?)
  142.             (split-path (find-system-path 'run-file))))
  143.         (path->string basepath))
  144. )
  145.  
  146. (define (die list)
  147.     (for-each (lambda (item) (display item *stderr*)) list)
  148.     (newline *stderr*)
  149.     (exit 1)
  150. )
  151.  
  152. (define (usage-exit)
  153.     (die `("Usage: " ,*run-file* " filename"))
  154. )
  155.  
  156. (define (readlist-from-inputfile filename)
  157.     (let ((inputfile (open-input-file filename)))
  158.          (if (not (input-port? inputfile))
  159.              (die `(,*run-file* ": " ,filename ": open failed"))
  160.              (let ((program (read inputfile)))
  161.                   (close-input-port inputfile)
  162.                          program))))
  163.  
  164. (define (write-program-by-line filename program)
  165.     (printf "==================================================~n")
  166.     (printf "~a: ~s~n" *run-file* filename)
  167.     (printf "==================================================~n")
  168.     (printf "(~n")
  169.     ;; this map function returns a list named 'program' which is the result of the procedure
  170.     ;; (map (lambda (line) (printf "~s~n" line)) program) ;; this is where we print out each line
  171.     ;; first pass grabbing labels
  172.     ;; second pass interpret
  173.     ;;(map (lambda (line) (get-stmt line)) program)
  174.     ;;(map (lambda (line) ()))
  175.     ;; (printf ")~n Output: ~n")
  176.    
  177.     ;;(map (lambda (line) (get-label line)) program)    
  178.     (eval-expr program)
  179.  )
  180.  
  181.  
  182. ;; implementing LET statement
  183. (define (interpret-let expr)
  184.    (printf "let-interpretting.. ~s ~n" expr)
  185.    (printf "car is ~s ~n" (car expr))
  186.    (printf "cdr is ~s ~n" (cdr expr))
  187.    ;;(printf (pair? expr))
  188.    (cond ((null? expr) #f)
  189.        ((symbol? (car expr)) ;; if we see a variable, put in var table
  190.            ;; (begin
  191.                ;;  (print"symbol -interp let ~n")
  192.                  (symbol-put! *variable-table* (car expr)  (eval-expr (cadr expr))));;))
  193.         ((pair? (car expr)) ;; if we see a list recur into that expr and also recur on whatever is after
  194.             (begin
  195.                  (print "pair interp-let ~n")
  196.                  (interpret-let (car expr))
  197.                  (interpret-let (cdr expr))))
  198.         ((number? (car expr));; if we see a num this is an error
  199.             (printf "error, a number may not be the head of a list"))
  200.         ((hash-has-key? *function-table* (car expr)) ;; if we see somesort of operation do it
  201.              (eval-expr expr))
  202.         (else #f))      
  203. )
  204.  
  205. ;; implementing DIM statement
  206. (define (interpret-dim expr)
  207.     ;; what do we put here? do we make a vector? a list?
  208.     (symbol-put *variable-table* (car expr) )
  209. )
  210.  
  211. (define (eval-expr expr)
  212.    (printf "evaluating...~s ~n" expr)
  213.    (cond ((number? expr) expr)
  214.          ((symbol? expr)
  215.             (if (hash-has-key? *variable-table* expr)
  216.                 (symbol-get *variable-table* expr)
  217.                 (hash-ref *function-table* expr #f)))
  218.          ((pair? expr)  
  219.                 (if (eq? (car expr) `let)
  220.                     (interpret-let (cdr expr))
  221.                     (if (hash-has-key? *function-table* (car expr))
  222.                         (apply (symbol-get *function-table* (car expr)) (map eval-expr (cdr expr)))                                                        ;;(apply (hash-ref *function-table* (car expr)))
  223.                         (map eval-expr (cdr expr)))))
  224.          ((null? expr) #f)
  225.          (else  expr))
  226.  
  227. )
  228.  
  229. (define (main arglist)
  230.     (if (or (null? arglist) (not (null? (cdr arglist))))
  231.         (usage-exit)
  232.         (let* ((sbprogfile (car arglist))
  233.                (program (readlist-from-inputfile sbprogfile)))
  234.               (write-program-by-line sbprogfile program))))
  235.  
  236. (main (vector->list (current-command-line-arguments)))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement