Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/afs/cats.ucsc.edu/courses/cmps112-wm/usr/racket/bin/mzscheme -qr
- ;; $Id: sbi.scm,v 1.3 2016-09-23 18:23:20-07 - - $
- ;;
- ;; NAME
- ;; sbi.scm - silly basic interpreter
- ;;
- ;; SYNOPSIS
- ;; sbi.scm filename.sbir
- ;;
- ;; DESCRIPTION
- ;; The file mentioned in argv[1] is read and assumed to be an SBIR
- ;; program, which is the executed. Currently it is only printed.
- ;;
- ;; here we create our tables
- (define *label-table* (make-hash))
- (define *variable-table* (make-hash))
- (define *function-table* (make-hash))
- ;; here we fill the function table with the prelim. functions
- ;; I think we write somethin like this to uses functions
- ;; (printf "2 ^ 16 = ~s~n" ((symbol-get '^) 2.0 16.0))
- ;; I changed symbol-get and symbol-put to require the table
- ;; that their grabbing from so that way we could use them for
- ;; other hash tables :-)
- (define (symbol-get table key)
- (hash-ref table key))
- (define (symbol-put! table key value)
- (printf "~n~nputting ~s in table:~s as ~s~n~n" key table value)
- (hash-set! table key value))
- (for-each
- (lambda (pair)
- (symbol-put! *function-table* (car pair) (cadr pair)))
- `(
- (log10_2 0.301029995663981195213738894724493026768189881)
- (sqrt_2 1.414213562373095048801688724209698078569671875)
- (div ,(lambda (x y) (floor (/ x y))))
- (log10 ,(lambda (x) (/ (log x) (log 10.0))))
- (mod ,(lambda (x y) (- x (* (div x y) y))))
- (quot ,(lambda (x y) (truncate (/ x y))))
- (rem ,(lambda (x y) (- x (* (quot x y) y))))
- (+ ,+)
- (* ,*)
- (- ,-)
- (/ ,(lambda (x y) (+ 0.0 (/ x y)) ))
- (^ ,expt)
- (ceil ,ceiling)
- (exp ,exp)
- (floor ,floor)
- (log ,log)
- (sqrt ,sqrt)
- (sin ,sin)
- (cos ,cos)
- (tan ,tan)
- (atan ,atan)
- (let , (lambda x (interpret-let x)))
- (print , (lambda x (displayln x)));; (lambda x (apply displayln x)))
- ))
- ;; now we fill the variable table with the prelim values pi & e
- (for-each
- (lambda (pair)
- (symbol-put! *variable-table* (car pair) (cadr pair)))
- `(
- (e 2.718281828459045235360287471352662497757247093)
- (pi 3.141592653589793238462643383279502884197169399)
- ))
- ;; here I create 'methods'
- ;; label table
- (define (get-label line-list)
- (if (null? (cdr line-list))
- null
- (if (null? (cadr line-list))
- null
- (if (symbol? (cadr line-list))
- (hash-set! *label-table* (cadr line-list) (cddr line-list))
- null))))
- (define (get-stmt line-list)
- (if (null? (cdr line-list))
- null
- (if (null? (cadr line-list))
- null
- (if (not (symbol? (cadr line-list)))
- (cadr line-list)
- null))))
- ;; goal find first smt on a line and evaluate the statement
- (define (get-stmt line)
- (if (null? (cdr line))
- null
- (if (null? (cadr line))
- null
- (if (symbol? (cadr line)) ;; if a label
- (cddr line);; return the next item
- (cadr line))))) ;; otherwise return stmt vthis may be wrong
- ;; this method runs a single line of code
- ;; for this we first check if there is a label
- ;; or if there is a stmt
- ;; then we get the stmt and check the car of it
- ;; see if the cadr is equal to one of the six key words
- ;; depending on which one we perform at action with the items
- ;; after the car
- ;; then look for next item on that list of line
- ;; if the cdr is null
- ;; - skip
- ;; now check if there is a label or no label
- ;; (begin (printf"statement not null ")) <---trash stmnt]
- ;; printf("The interpreter line was not null")))
- ;;(define (interpret-line line)
- ;; (if (null? ( get-stmt line))
- ;; null))
- ;;;;;;
- ;; ())
- ;; ( if (eq? (car (get-stmt line)) ("print"))
- ;; (printf "~s~n" cadr (get-stmt line))
- ;; ()) ;; do nothing, IDK IF U CAN DO THIS
- ;; ( if) ;; more if statements follow
- (define *stderr* (current-error-port))
- (define *run-file*
- (let-values
- (((dirpath basepath root?)
- (split-path (find-system-path 'run-file))))
- (path->string basepath))
- )
- (define (die list)
- (for-each (lambda (item) (display item *stderr*)) list)
- (newline *stderr*)
- (exit 1)
- )
- (define (usage-exit)
- (die `("Usage: " ,*run-file* " filename"))
- )
- (define (readlist-from-inputfile filename)
- (let ((inputfile (open-input-file filename)))
- (if (not (input-port? inputfile))
- (die `(,*run-file* ": " ,filename ": open failed"))
- (let ((program (read inputfile)))
- (close-input-port inputfile)
- program))))
- (define (write-program-by-line filename program)
- (printf "==================================================~n")
- (printf "~a: ~s~n" *run-file* filename)
- (printf "==================================================~n")
- (printf "(~n")
- ;; this map function returns a list named 'program' which is the result of the procedure
- ;; (map (lambda (line) (printf "~s~n" line)) program) ;; this is where we print out each line
- ;; first pass grabbing labels
- ;; second pass interpret
- ;;(map (lambda (line) (get-stmt line)) program)
- ;;(map (lambda (line) ()))
- ;; (printf ")~n Output: ~n")
- ;;(map (lambda (line) (get-label line)) program)
- (eval-expr program)
- )
- ;; implementing LET statement
- (define (interpret-let expr)
- (printf "let-interpretting.. ~s ~n" expr)
- (printf "car is ~s ~n" (car expr))
- (printf "cdr is ~s ~n" (cdr expr))
- ;;(printf (pair? expr))
- (cond ((null? expr) #f)
- ((symbol? (car expr)) ;; if we see a variable, put in var table
- ;; (begin
- ;; (print"symbol -interp let ~n")
- (symbol-put! *variable-table* (car expr) (eval-expr (cadr expr))));;))
- ((pair? (car expr)) ;; if we see a list recur into that expr and also recur on whatever is after
- (begin
- (print "pair interp-let ~n")
- (interpret-let (car expr))
- (interpret-let (cdr expr))))
- ((number? (car expr));; if we see a num this is an error
- (printf "error, a number may not be the head of a list"))
- ((hash-has-key? *function-table* (car expr)) ;; if we see somesort of operation do it
- (eval-expr expr))
- (else #f))
- )
- ;; implementing DIM statement
- (define (interpret-dim expr)
- ;; what do we put here? do we make a vector? a list?
- (printf "dim-interpretting.. ~s ~n" expr)
- (symbol-put! *variable-table* (car expr) (make-vector (cadr expr) [car expr]))
- )
- (define (eval-expr expr)
- (printf "evaluating...~s ~n" expr)
- (cond ((number? expr) expr)
- ((symbol? expr)
- (if (hash-has-key? *variable-table* expr)
- (symbol-get *variable-table* expr)
- (hash-ref *function-table* expr #f)))
- ((pair? expr)
- (if (eq? (car expr) `let)
- (interpret-let (cdr expr))
- (if (eq? (car expr) `dim)
- (interpret-dim (cadr expr))
- (if (hash-has-key? *function-table* (car expr))
- (apply (symbol-get *function-table* (car expr)) (map eval-expr (cdr expr))) ;;(apply (hash-ref *function-table* (car expr)))
- (map eval-expr (cdr expr))))))
- ((null? expr) #f)
- (else expr))
- )
- (define (main arglist)
- (if (or (null? arglist) (not (null? (cdr arglist))))
- (usage-exit)
- (let* ((sbprogfile (car arglist))
- (program (readlist-from-inputfile sbprogfile)))
- (write-program-by-line sbprogfile program))))
- (main (vector->list (current-command-line-arguments)))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement