Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/thedevtop/libsexp
S-Expression parser library
https://github.com/thedevtop/libsexp
parser s-expressions swift
Last synced: 26 days ago
JSON representation
S-Expression parser library
- Host: GitHub
- URL: https://github.com/thedevtop/libsexp
- Owner: TheDevtop
- License: mit
- Created: 2024-10-19T16:23:25.000Z (3 months ago)
- Default Branch: main
- Last Pushed: 2024-10-22T07:35:12.000Z (3 months ago)
- Last Synced: 2024-11-02T12:07:50.659Z (2 months ago)
- Topics: parser, s-expressions, swift
- Language: Swift
- Homepage:
- Size: 13.7 KB
- Stars: 1
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Libsexp & Libcadr
An S-Expression parser library, written in Swift.
Lisp and its innovative constructs, continue to inspire me.
The philosophy of combination and abstraction, as formulated in [SICP](https://web.mit.edu/6.001/6.037/sicp.pdf) left me wanting to write my own Lisp environment.
This repository is the first step in that direction.I hope you may find use in this.
### On usage
There are two important functions, **decode** and **encode**.
```swift
public func Decode(_ input: String) -> Sexp
```
Decode takes in a string and outputs an S-Expression object.
If any error occurs, it returns an S-Expression containing the `:error` atom.```swift
public func Encode(_ exp: Sexp) -> String
```
Encode takes an S-Expression object and returns a string that contains the S-Expression representation of the object.### On type hierarchy
An S-Expression can be one of several types.
List types:
- List `()`, `(+ 1 2 3)`Value types:
- Quote `"Cited string"`, `'Quoted string'`
- Number `42`, `-69`
- Float `4.20`
- Atom (based on Elixir atoms) `:err`, `:foobar`
- Boolean `:true` or `:false`
- Symbol `+`, `foobar`, `write-file`The order in which there are listed, is the order in which they are parsed.
Thus there is a hierarchy of types, which determines the output of the decode function.### On atoms
Atoms are different from symbols, in that atoms won't evaluate to anything.
The symbol `foo` may evaluate to a procedure, a number, or even an atom.
The atom `:foo` will always resolve to `:foo`.There are also two special atoms: `:ok` and `:err`.
These atoms perform the same function as the [result](https://doc.rust-lang.org/std/result/) type in Rust.### On cons, car, and cadr
Besided the S-Expression code found in libsexp, there is also **libcadr**.
This library contains the `Cons` type, and the `Car()`, and `Cdr()` functions.```swift
/// Get the first element of a list
public func Car(_ lexp: libsexp.List) -> libsexp.Sexp/// Get the other elements of a list
public func Cdr(_ lexp: libsexp.List) -> libsexp.List/// Construct cell
public typealias Cons = (car: libsexp.Sexp, cdr: libsexp.List)/// Construct a cell
public func Construct(_ exp: libsexp.Sexp) -> libcadr.Cons
```
With `Construct()` you can create an Cons cell from any S-Expression.