All Projects → uraimo → Swiftylisp

uraimo / Swiftylisp

Licence: mit
A minimal LISP implemented in Swift

Programming Languages

swift
15916 projects
language
365 projects
lisp
113 projects

Projects that are alternatives of or similar to Swiftylisp

Smallbasic
SmallBASIC is a fast and easy to learn BASIC language interpreter ideal for everyday calculations, scripts and prototypes. SmallBASIC includes trigonometric, matrices and algebra functions, a built in IDE, a powerful string library, system, sound, and graphic commands along with structured programming syntax
Stars: ✭ 78 (-26.42%)
Mutual labels:  interpreter
Abrvalg
Python-like programming language interpreter written in Python
Stars: ✭ 83 (-21.7%)
Mutual labels:  interpreter
Sniprun
A neovim plugin to run lines/blocs of code (independently of the rest of the file), supporting multiples languages
Stars: ✭ 93 (-12.26%)
Mutual labels:  interpreter
Tagha
Minimal, low-level, fast, and self-contained register-based bytecode virtual machine/runtime environment.
Stars: ✭ 79 (-25.47%)
Mutual labels:  interpreter
Internettools
XPath/XQuery 3.1 interpreter for Pascal with compatibility modes for XPath 2.0/XQuery 1.0/3.0, custom and JSONiq extensions, XML/HTML parsers and classes for HTTP/S requests
Stars: ✭ 82 (-22.64%)
Mutual labels:  interpreter
Magpie
🐦 Successor of my monkey Interpreter(support for class, linq, sql, net, http, fmt, json and A realtime syntax highlighting REPL).
Stars: ✭ 88 (-16.98%)
Mutual labels:  interpreter
Oh
A new Unix shell.
Stars: ✭ 1,206 (+1037.74%)
Mutual labels:  interpreter
Endbasic
BASIC environment with a REPL, a web interface, and RPi support written in Rust
Stars: ✭ 106 (+0%)
Mutual labels:  interpreter
Aceto
A programming language based on a 2D Hilbert curve grid
Stars: ✭ 83 (-21.7%)
Mutual labels:  interpreter
Mages
🎩 MAGES is a very simple, yet powerful, expression parser and interpreter.
Stars: ✭ 92 (-13.21%)
Mutual labels:  interpreter
Rs Monkey Lang
Monkey Programming Language written in Rust.
Stars: ✭ 80 (-24.53%)
Mutual labels:  interpreter
Xstateful
A wrapper for xstate that stores state, handles transitions, emits events for state changes and actions/activities, and includes an optional reducer framework for updating state and invoking side-effects
Stars: ✭ 81 (-23.58%)
Mutual labels:  interpreter
Feral
Feral programming language reference implementation
Stars: ✭ 89 (-16.04%)
Mutual labels:  interpreter
Goto
Goto is an interpreted programming language written in go.
Stars: ✭ 79 (-25.47%)
Mutual labels:  interpreter
Root
The official repository for ROOT: analyzing, storing and visualizing big data, scientifically
Stars: ✭ 1,377 (+1199.06%)
Mutual labels:  interpreter
Python lite
[WIP] A simple, lightweight implementation of python3 language.
Stars: ✭ 77 (-27.36%)
Mutual labels:  interpreter
Hdbf
Hyper-Dimensional Brainfuck
Stars: ✭ 87 (-17.92%)
Mutual labels:  interpreter
Angu
A small DSL/interpreter that can be used to evaluate simple expressions
Stars: ✭ 105 (-0.94%)
Mutual labels:  interpreter
Lispe
An implementation of a full fledged Lisp interpreter with Data Structure, Pattern Programming and High level Functions with Lazy Evaluation à la Haskell.
Stars: ✭ 105 (-0.94%)
Mutual labels:  interpreter
Wasm Forth
A Forth implementation compiling to WebAssembly.
Stars: ✭ 92 (-13.21%)
Mutual labels:  interpreter

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.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].