Lisparser
Lisparser is another parser invented by User:Hakerh400 (the previous one was Elevated Parser). Lisparser uses a Lisp-like syntax to parse a Lisp-like source code. This parser does not have an intermediate state. It does not construct abstract syntax tree initially (if we do not count pure lists as tree nodes). Instead, it starts to interpret the program while the program is still being parsed. It saves a lot of time and speeds up the computation.
Overview
The source code of a program that can be parsed using Lisparser consists of elements. Each element is either a list or an identifier. A list contains zero or more elements. An identifier can contain any character except whitespace and parentheses.
Example of a program's source code:
(program (var x 5) (var y 7) (var z (+ x y)) (print z) )
Lisparser can be used to write a simple parser to parse and execute this program (parsing and executing happens at the same time in Lisparser).
Commands
Lisparser has the following commands:
(s x)- Checks whether elementxis a scalar (an identifier)(v x)- Checks whether elementxis a vector (a list)(ident x)- Asserts that elementxis an identifier(ident x y)- Asserts that elementxis an identifier whose string representation isy(list x)- Asserts that elementxis a list(uni x)- Asserts thatxhas exactly one element and returns that element(n x)- Asserts thatxis a list and returns its length(m x)- Asserts thatxis an ident and returns its name as a string(is-nat x)- Checks whetherxis an identifier representing a natural number (but asserts thatxis an identifier)(is-int x)- Checks whetherxis an identifier representing an integer (but asserts thatxis an identifier)(get-nat x)- Asserts thatxis an identifier representing a natural number and returns its value as a bigint(get-int x)- Asserts thatxis an identifier representing an integer and returns its value as a bigint(len x y z)- Asserts thatxis a list whose length satisfies(lenp x y)- Asserts thatxis a list whose length satisfies(lenm x y)- Asserts thatxis a list whose length satisfies(e x i)- Asserts thatxis a list with at least elements and return -th element (0-indexed)(a x i f)- Asserts thatxis a list with at least elements and return its elements as an array starting from index with unary functionfapplied to each element(ta x t f)- Asserts thatxis a list with at least 1 element and that the first element is an identifier whose string representation ist, and return its elements as an array starting from index 1 with unary functionfapplied to each element(empty x)- Checks whetherxis an empty list (but asserts thatxis a list)(type x t)- Asserts thatxis a list with at least 1 element and that the first element is an identifier whose string representation ist(fst x)- Same as(e x 0)(snd x)- Same as(e x 1)(last x)- Same as(e x (- (n x) 1))- this is basically the last element ofx(elems x)- Same(a x 0 (lambda (x) x))- this simply returns all the elements ofx(which is implicitly asserted to be a list)(ch-num x)- For an ident returns 0, for a list returns the number of elements(get-ch x i)- Similar to(e x i), but can be used only in recursive iterations (not very important to explain that)(top-down x f)- Iteratexas a tree using a top-down iterator callingffor each element(bottom-up x f)- Iteratexas a tree using a bottom-up iterator callingffor each element(iter x f)- Iteratexusing a top-down and then bottom-up iterator callingffor each element and passing an extra argument that is 0 for top-down and 1 for bottom-up(cont)- Skip a branch in the iteration process(break)- Skip the entire sub-tree in the iteration process(to-str x)- Returns a stringified representation ofx(spacing-type x s)- For listxsets the spacing type tos(used when callingto-str)(spacing-start x i)- For listxsets the spacing start to index (used when callingto-str) - basically all whitespace between elements before index are space characters, whilesis used starting from
Exceptions
(err x msg)- Throws an error with messagemsgwith all relevant information (source file path, line, stack trace and the message)(warn x msg)- Same aserr, but shows a warning instead of throwing an error.
Integration with Node.js
The parser is written in Node.js and implements all commands as JavaScript methods. It uses generator functions trick to allow unlimited recursion depth. For example, you can recursively traverse a list containing hundreds of millions of nested lists without triggering a stack overflow.
Examples
Brainlisp is an example of a program that uses Lisparser. See the Brainlisp interpreter.
Interpreters
These interpreters use the parser.