Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- (typedef sexp (is (list of syntax-construct))
- (typedef data
- (prop (Ast: ast))
- (prop (Raw: data-string))
- )
- (typedef syntax-construct
- (is oneof(sexp, data, char, number, atom, string)))
- (typedef ast (is list of (syntax-construct)))
- (typedef atom-char
- (is (one-of ((char: (is alpha)), $_)))
- (typedef sexp
- (is list of syntax-construct))
- (def parse-ast (InStream: in-stream) -> ast | error
- (parse-ast InStream (new ast))
- (def parse-ast (InStream: in-stream, CurrentAst: ast) -> ast | error ->
- (case (apply syntax-rule (inStream, (inStream:peak-char)))
- (S: syntax-construct =>
- (CurrentAst:insert s)
- (parse-ast InStream, CurrentAst))
- (eof =>
- CurrentAst))
- )
- (def parse-construct (InStream: in-stream) -> syntax-construct | error
- (apply syntax-rule (in-stream, in-stream:peek())))
- ;; syntax rules
- (def syntax-rule (InStream: in-stream, (_: atom-char)) -> atom
- (read-atom in-stream))
- (def syntax-rule (InStream: in-stream, $$) -> char
- (let $$ (InStream.read-char)
- (read-char in-stream)))
- (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)
- (read-number in-stream))
- (def syntax-rule (InStream: in-stream, eof): -> eof
- (let eof (InStream:read-char)
- eof))
- (def syntax-rule (InStream: in-stream, $')
- (let $' (InStream:read-char)
- (read-data InStream)))
- (def syntax-rule (InStream: in-stream, $")
- (let $" (InStream:read-char)
- (read-string InStream)))
- (def syntax-rule (InStream: in-stream, $()
- (let $( (InStream:read-char)
- (read-sexp InStream)))
- (def syntax-rule (InStream: in-stream, (_: (is (char (is whitespace)))))
- ;; excess whitespace just read until something meaningful
- (InStream:read-char));
- ;; methods implementing particular reading
- (def read-atom (InStream: in-stream)
- (let (Atom: list of char)
- (apply (clojure parse-atom ()
- (let CurrentChar (in-stream:read-char)
- (if (CurrentChar:is-whitespace)
- Atom
- (Atom:append CurrentChar)
- (parse-atom)
- ))))
- ))
- (def read-char (InStream: in-stream) -> char
- (let $$ (InputStream::read-char)
- (InputStream::read-char)))
- (def read-number (InStream: in-stream) -> number
- (let (Number: list of digit)
- (apply (clojure parse-number ()
- (case (in stream:read-char)
- (C: (is-whitespace)) =>
- Number)
- (C: (is digit)) =>
- (Number:append C)
- (parse-number)
- ))))
- ))
- (def read-string (InnStream: in-stream)
- (let (String (InStream:read-until $"))
- return String))
- (def read-data (InStream: in-stream)
- (let (Data (new data))
- (assign Data.Raw (InStream:read-until $'))
- (assign Data.Ast (parse-ast Data.Raw))
- Data))
- (def read-sexp (InStream: in-stream)
- (let (Data (new sexp))
- (clojure parse-sexp ()
- (case (let FirstChar (InStream:get-char))
- ($( => Data)
- (_ =>
- (Data:insert (parse-construct InStream))
- (let $, (InStream:read-char))
- (parse-sexp)
- )
- )
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement