• Stars
    star
    126
  • Rank 284,543 (Top 6 %)
  • Language
    Swift
  • License
    MIT License
  • Created almost 8 years ago
  • Updated over 1 year ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

A minimal LISP implemented in Swift

SwiftyLISP

An embeddable LISP interpreter for Swift

Swift 4 compatible License: MIT

Summary

This framework contains an interpreter for the LISP defined in John McCarthy's micro-manual and described in this article.

Usage

The framework converts string literals in nested structures based on the SExpr enum that can then be evaluated.

Let's see some usage examples:

import SwiftyLisp

var expr:SExpr = "(cond ((atom (quote A)) (quote B)) ((quote true) (quote C)))"

print(expr)
dump(expr)
print(expr.eval()!)  //B

expr = "(car ( cdr  ( quote (1 2 \"aaaa\"   4 5 true 6 7 () ))))"
print(expr.eval()!)  //2

expr = "( (lambda (x y) (atom x)) a b)" 
print(expr.eval()!)  //true

expr = "(defun ff (x) (cond ((atom x) x) (true (ff (car x)))))"
print(expr.eval()!)
expr = "(ff (quote ((a b) c)))"
print(expr.eval()!)  //a

expr = "(eval (quote (atom (quote A)))"
print(expr.eval()!)  //true

expr = "(defun alt (x) (cond ((or (null x) (null (cdr x))) x) (true (cons (car x) (alt (cddr x))))))"
print(expr.eval()!)
expr = "(alt (quote (A B C D E))"
print(expr.eval()!)  //(A C E)
 

Here is a recap of the available operators:

Atom Structure Description
Print (print e) Prints its sub-expression
Eval (eval e) Eval evaluates its sub-expression
Quote (quote e) This atom once evaluated returns its sub-expression as is, e.g. (quote A) = A
Car (car l) Returns the first element of a non-empty sub-list, e.g. (car (quote (A B C))) = A
Cdr (cdr l) Returns all the elements of the sub-list after the first in a new list, e.g. (cdr (quote (A B C))) = (B C)
Cons (cons e l) Returns a new list with e as first element and then the content of the sublist e.g. (cons (quote A) (quote (B C))) = (A B C)
Equal (equal e1 e2) Returns an atom aptly named true if the two symbolic expressions are recursively equal and the empty list () (that serves as both nil and false value) if they are not, e.g. (equal (car (quote (A B))) = (quote A))
Atom (atom e) Returns true if the symbolic expression is an atom or an empty list if it is a lis, e.g. (atom A) = true
Cond (cond (p1 e1) (p2 e2) ... (pn en)) Returns the first e expression whose p predicate expression is not equal to the empty list. This is basically a conditional atom with a slightly more convoluted syntax than a common if construct. e.g. (cond ((atom (quote A)) (quote B)) ((quote true) (quote C) = B
List (list e1 e2 ... en) Returns a list of all the given expressions, identical to applying cons recursively to a sequence of expressions.
Lambda ( (lambda (v1 ... vn) e) p1 ... pn) Defines a lambda expression with body e that describes an anonymous function that uses a series of environment variables v. This function will be evaluated using the provided parameters as value for the variables. e.g. ((lambda (X Y) (cons (car x) y) (quote (A B)) (cdr (quote (C D)))) = (A D)
Defun (defun (v1 ... vn) e) Define a lambda expression and registers it in the current context to be used when we need it. We'll be able to define a function like (defun cadr (X) (car (cdr x))) and use it in another expression like (cadr (quote (A B C D))).

Additional examples, and the additional functions and acronyms defined in the paper, can be found in the test suite of the framework.

Installation

The library can be installed with either CocoaPods, Carthage or the SwiftPM.

To include it in your project using the Swift Package Manager, add a dependency to your Package.swift:

import PackageDescription

let package = Package(
    ...
    dependencies: [
        ...
        .Package(url: "https://github.com/uraimo/SwiftyLISP.git")
    ]
)

Regardless of how you added the framework to your project, import it with import SwiftyLisp.

License

The MIT License (MIT)

Copyright (c) 2016 Umberto Raimondi

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

More Repositories

1

Awesome-Swift-Playgrounds

A List of Awesome Swift Playgrounds
Swift
3,998
star
2

SwiftyGPIO

A Swift library for hardware projects on Linux/ARM boards with support for GPIOs/SPI/I2C/PWM/UART/1Wire.
Swift
1,301
star
3

run-on-arch-action

A Github Action that executes jobs/commands on non-x86 cpu architectures (ARMv6, ARMv7, aarch64, s390x, ppc64le, riscv64) via QEMU
Shell
681
star
4

buildSwiftOnARM

All you need to build Swift on a RaspberryPi or other ARM boards, updated to Swift 5.1.5
Shell
489
star
5

awesome-software-patreons

A curated list of awesome programmers and software projects you can support!
456
star
6

Bitter

A Swift Bits Manipulation/Bitwise Operations Toolkit
Swift
205
star
7

Swift-Playgrounds

Collection of Swift playgrounds used in my posts: From functional aspects of Swift to C interoperability.
Swift
139
star
8

pygments-vimstyles

Vim Styles as pygments CSS
CSS
37
star
9

WS281x.swift

A Swift library for WS281x (WS2811,WS2812*,WS2813*) RGB led strips, rings, sticks, matrices and more.
Swift
33
star
10

Nunchuck.swift

A Swift Library for the I2C Wii Nunchuck controller.
Swift
31
star
11

5110LCD_PCD8544.swift

A Swift library for the Nokia3310/5110 PCD8544 Monochrome LCD display
Swift
27
star
12

SwizzlingInSwift

Swift Method Swizzling Sample
Swift
18
star
13

SwiftyLISP-REPL

Basic REPL for SwiftyLISP
Swift
13
star
14

HD44780CharacterLCD.swift

A Swift library for 16x2/20x4 Character LCDs with the HD44780(or clones) controller
Swift
12
star
15

SG90Servo.swift

Swift library for the SG90 Servo Motor, adaptable for other servos (9g ES08A, SM-S4303R, S3003, etc...).
Swift
10
star
16

UBloxGPS.swift

A Swift library for boards with the u-Blox 6/7/8 family of A-GPS receivers
Swift
9
star
17

MPU-6050.swift

A Swift library for the MPU-6050 (and MPU-6000 family) Accelerometer and Gyroscope
Swift
7
star
18

buildSwiftOnARMInfra

Setup an infrastructure to build Swift on Linux/ARM
Shell
7
star
19

uistackview-sample

Sample code for www.uraimo.com/2015/09/08/ios9-uistackview-guide-swift/
Swift
7
star
20

RCWL-0516-Radar.swift

A Swift library for the RCWL-0516 Microwave Radar
Swift
5
star
21

DS18B20.swift

A Swift library for the DS18B20 digital temperature sensor
Swift
5
star
22

dotfiles

My dotfiles
Vim Script
4
star
23

MCP3008.swift

A Swift library for the MCP3008 (and MCP3002,MCP3004) 10 bits SPI ADC
Swift
4
star
24

barememtracer

C/C++ bare bones memory tracer
C
3
star
25

javabuild

An experimental Java build tool that mimics the Swift Package Manager
Java
3
star
26

DS1307.swift

A Swift library for the DS1307 (DS1302, DS3231) I2C Real-Time Clock
Swift
3
star
27

congo

Simple Go console package - See docs: http://godoc.org/github.com/uraimo/congo
Go
2
star