Advertisement
Guest User

Untitled

a guest
Dec 14th, 2017
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lisp 3.21 KB | None | 0 0
  1. (typedef sexp (is (list of syntax-construct))
  2. (typedef data
  3.     (prop (Ast: ast))
  4.     (prop (Raw: data-string))
  5. )
  6.  
  7. (typedef syntax-construct
  8.     (is oneof(sexp, data, char, number, atom, string)))
  9.  
  10.  
  11. (typedef ast (is list of (syntax-construct)))
  12.  
  13. (typedef atom-char
  14. (is (one-of ((char: (is alpha)), $_)))
  15.  
  16. (typedef sexp
  17.     (is list of syntax-construct))
  18.  
  19.  
  20. (def parse-ast (InStream: in-stream) -> ast | error
  21.     (parse-ast InStream (new ast))
  22.  
  23. (def parse-ast (InStream: in-stream, CurrentAst: ast) -> ast | error ->
  24. (case (apply syntax-rule (inStream, (inStream:peak-char)))
  25.     (S: syntax-construct =>
  26.         (CurrentAst:insert s)
  27.         (parse-ast InStream, CurrentAst))
  28.     (eof =>
  29.         CurrentAst))
  30. )
  31.  
  32.  
  33. (def parse-construct (InStream: in-stream) -> syntax-construct | error
  34. (apply syntax-rule (in-stream, in-stream:peek())))
  35.  
  36.  
  37. ;; syntax rules
  38. (def syntax-rule (InStream: in-stream, (_: atom-char)) -> atom
  39. (read-atom in-stream))
  40. (def syntax-rule (InStream: in-stream, $$) -> char
  41. (let $$ (InStream.read-char)
  42.     (read-char in-stream)))
  43. (def syntax-rule ((InStream: in-stream), (_: (is digit))) -> number ;; this can be more thouroughly specified i.e. only 0-9 i.e. is integer(0, 9)
  44. (read-number in-stream))
  45. (def syntax-rule (InStream: in-stream, eof): -> eof
  46. (let eof (InStream:read-char)
  47.     eof))
  48. (def syntax-rule (InStream: in-stream, $')
  49. (let $' (InStream:read-char)
  50.         (read-data InStream)))
  51. (def syntax-rule (InStream: in-stream, $")
  52. (let $" (InStream:read-char)
  53.         (read-string InStream)))
  54. (def syntax-rule (InStream: in-stream, $()
  55. (let $( (InStream:read-char)
  56.         (read-sexp InStream)))
  57. (def syntax-rule (InStream: in-stream, (_: (is (char (is whitespace)))))
  58.     ;; excess whitespace just read until something meaningful
  59.     (InStream:read-char));
  60.  
  61.  
  62.  
  63.  
  64.  
  65.  
  66. ;; methods implementing particular reading
  67. (def read-atom (InStream: in-stream)
  68. (let (Atom: list of char)
  69. (apply (clojure parse-atom ()
  70.     (let CurrentChar (in-stream:read-char)
  71.     (if (CurrentChar:is-whitespace)
  72.        Atom
  73.        (Atom:append CurrentChar)
  74.        (parse-atom)
  75.        ))))
  76. ))
  77.  
  78. (def read-char (InStream: in-stream) -> char
  79. (let $$ (InputStream::read-char)
  80.     (InputStream::read-char)))
  81.  
  82.  
  83. (def read-number (InStream: in-stream) -> number
  84. (let (Number: list of digit)
  85.     (apply (clojure parse-number ()
  86.         (case (in stream:read-char)
  87.         (C: (is-whitespace)) =>
  88.             Number)
  89.         (C: (is digit)) =>
  90.            (Number:append C)
  91.            (parse-number)
  92.            ))))
  93.     ))
  94. (def read-string (InnStream: in-stream)
  95. (let (String (InStream:read-until $"))
  96.    return String))
  97.  
  98. (def read-data (InStream: in-stream)
  99.     (let (Data (new data))
  100.        (assign Data.Raw (InStream:read-until $'))
  101.        (assign Data.Ast (parse-ast Data.Raw))
  102.    Data))
  103.  
  104.  
  105. (def read-sexp (InStream: in-stream)
  106.     (let (Data (new sexp))
  107.            (clojure parse-sexp ()
  108.                (case (let FirstChar (InStream:get-char))
  109.                    ($( => Data)
  110.                    (_ =>
  111.                        (Data:insert (parse-construct InStream))
  112.                        (let $, (InStream:read-char))
  113.                        (parse-sexp)
  114.        )
  115.     )
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement