Advertisement
Guest User

Untitled

a guest
Jun 4th, 2017
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Racket 2.75 KB | None | 0 0
  1. #lang pl 04
  2.  
  3. ;; Defining two new types
  4. (define-type BIT = (U 0 1))
  5. (define-type Bit-List = (Listof BIT))
  6.  
  7. ;; The actual interpreter
  8. #| BNF for the ROL language:
  9.  
  10.  <ROL> ::= { reg-len = <num> <RegE>}
  11.  
  12.  <RegE> ::= <Bits>
  13.           | {and <RegE> <RegE>}
  14.           | {or <RegE> <RegE>}
  15.           | {shl <RegE>}
  16.           | {with {<id> <RegE>} <RegE>}
  17.           | {fun { <id> } <RegE>}
  18.           | {call <RegE> <RegE>}
  19.           | <id>
  20.          
  21.  <Bits> ::= 0
  22.           | 1
  23.           | {<Bits>}
  24.  
  25.  |#
  26. (define-type RegE
  27.   [Reg Bit-List]
  28.   [And RegE RegE]
  29.   [Or RegE RegE]
  30.   [Shl RegE]
  31.   [Id Symbol]
  32.   [With Symbol RegE RegE]
  33.   [Fun Symbol RegE]
  34.   [Call RegE RegE] )
  35. ;; Next is a technical function that converts (casts)
  36. ;; (any) list into a bit-list. We use it in parse-sexpr.
  37. (: list->bit-list : (Listof Any) -> Bit-List)
  38. ;; to cast a list of bits as a bit-list
  39. (define (list->bit-list lst)
  40.   (cond [(null? lst) null]
  41.         [(eq? (first lst) 1)
  42.          (cons 1 (list->bit-list (rest lst)))]
  43.         [else (cons 0 (list->bit-list (rest lst)))]))
  44.  
  45. (: parse-sexpr : Sexpr -> RegE)
  46. ;; to convert the main s-expression into ROL
  47. (define (parse-sexpr sexpr)
  48.   (match sexpr
  49.     []
  50.     < --fill in-- > ;; remember to make sure specified register length is at least 1
  51.     [else (error 'parse-sexpr "bad syntax in ~s" sexpr)]))
  52. (: parse-sexpr-RegL : Sexpr Number -> RegE)
  53. ;; to convert s-expressions into RegEs
  54. (define (parse-sexpr-RegL sexpr reg-len)
  55.   (match sexpr
  56.     [(list (and a (or 1 0)) ... ) (< --fill in-- >
  57.                                      (error 'parse-sexpr "wrong number of bits in ~s" a))]
  58.     [< --fill in-- >]
  59.     [< --fill in-- >]
  60.     …
  61.     [< --fill in-- >]
  62.     [else (error 'parse-sexpr "bad syntax in ~s" sexpr)]))
  63. (: parse : String -> RegE)
  64. ;; parses a string containing a RegE expression to a RegE AST
  65. (define (parse str)
  66.   (parse-sexpr (string->sexpr str)))
  67.  
  68.  
  69. ;; tests
  70. (test (run "{ reg-len = 4 {1 0 0 0}}") => '(1 0 0 0))
  71. (test (run "{ reg-len = 4 {shl {1 0 0 0}}}") => '(0 0 0 1))
  72. (test (run "{ reg-len = 4 {and {shl {1 0 1 0}}{shl {1 0 1 0}}}}") => '(0 1 0 1))
  73. (test (run "{ reg-len = 4 { or {and {shl {1 0 1 0}} {shl {1 0 0 1}}} {1 0 1 0}}}") => '(1 0 1 1))
  74. (test (run "{ reg-len = 2 { or {and {shl {1 0}} {1 0}} {1 0}}}") => '(1 0))
  75. (test (run "{ reg-len = 4 {with {x {1 1 1 1}} {shl y}}}") =error> "free identifier")
  76. (test (run "{ reg-len = 2 { with {x { or {and {shl {1 0}} {1 0}} {1 0}}} {shl x}}}") => '(0 1))
  77. (test (run "{ reg-len = 4 {or {1 1 1 1} {0 1 1}}}") =error>"wrong number of bits in")
  78. (test (run "{ reg-len = 0 {}}") =error> "Register length must be at least 1")
  79. (test (run "{ reg-len = 3 {with {identity {fun {x} x}} {with {foo {fun {x} {or x {1 1 0}}}} {call {call identity foo} {0 1 0}}}}}"))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement