a custom Lisp REPL using Swifties

Related tags

Editor swifties-repl
Overview

Swifties REPL

intro

This projects aims to demonstrate how to implement a custom Lisp REPL using Swifties.

Swifties v5

Return evaluates completed forms,
(reset) clears the stack and Ctrl+D quits.

  (func fibrec [n:Int] [Int]
      (if (< $n 2) $n (+ (fibrec (-- $n)) (fibrec (- $n 2)))))

  (fibrec 10)
[55]

quirks

  • The stack is directly exposed to user code, just like in Forth.
  • Primitives, Macros and Functions are called on reference outside of call forms.
  • Parens are used for calls only, brackets for lists of things.

stacks

c swaps the specified number of items.

  1 2 3 cc
[1 2 3 2 3]

d drops the specified number of items.

1 2 3 4 dd
[1 2]

s swaps the specified number of items.

  1 2 3 4 5 ss
[1 4 5 2 3]

stash replaces the the stack with its contents as a single item.

  1 2 3 stash
[[1 2 3]]

Stack literals are enclosed in brackets.

  [1 2 3]
[[1 2 3]]

splat replaces the top item (which is required to be iterable) with its items.

  (splat [1 2 3])
[1 2 3]

bindings

Values may be bound to identifiers using let.

  (let [x 35 y 7]
      (+ $x $y))
[42]

functions

New functions may be defined using func.

  (func fibrec [n:Int] [Int]
      (if (< $n 2) $n (+ (fibrec (-- $n)) (fibrec (- $n 2)))))

  (fibrec 10)
[55]

The same thing could be accomplished without bindings by manipulating the stack, if one was so inclined.

  (func fibrecs [Int] [Int]
    c (if (< _ 2) _ (do -- c fibrecs s -- fibrecs +)))

  (fibrecs 10)
[55]

It also runs somewhat faster.

(bench 100 (fibrec 10))
[366]

(bench 100 (fibrecs 10))
[366 357]

The algorithm can definitely be improved, note that I had to change n from 10 to 50 to even get something worth measuring.

  (func fibtail1 [n:Int a:Int b:Int] [Int]
      (if (z? $n) $a (if (one? $n) $b (fibtail1 (-- $n) $b (+ $a $b)))))

  (bench 100 (fibtail1 50 0 1))
[149]

Since the recursive call is in tail position, recall may be used to trigger tail call optimization.

  (func fibtail2 [n:Int a:Int b:Int] [Int]
      (if (z? $n) $a (if (one? $n) $b (recall (-- $n) $b (+ $a $b)))))

  (bench 100 (fibtail2 50 0 1))
[149 115]

dots

. may be used to shift arguments to the left of the target syntactically.

  (1.+ 2)
[3]

multimethods

Functions are upgraded to multimethods as soon as multiple definitions share the same name. Multimethods delegate to the most specific applicable function, this makes them somewhat more expensive to call.

  (func foo [] [String] "n/a")
  (func foo [_:Int] [String] "Int")
  (func foo [_:Any] [String] "Any")

  (foo 42)
["Int"]

  (foo t)
["Int" "Any"]

  (foo)
["Int" "Any" "n/a"]

booleans

Every value has a boolean representation; most are true, but integers are false when zero, strings and stacks when empty etc.

and returns the first argument if false, else the last.

  (and 0 t)
[0]

  (and t 42)
[0 42]

or returns the first argument if true, else the last.

  (or 42 f)
[42]

  (or f 42)
[42 42]

notreturns t if the argument is false, else f.

  (not 0)
[t]

  (not 42)
[t f]

strings

String literals are enclosed in double quotes.

  "foo"
["foo"]

characters

Character literals are prefixed with \.

  \a
[\a]

quotes

Forms may be quoted by prefixing with '.

  'foo '(x y z)  
['foo '(x y z)]

, may be used to splice values into quoted expressions.

  (let [foo 42] '[,$foo])
['[42]]

Splicing multiple values is just as easy.

  (let [foo [1 2 3]] '[,(splat $foo)])
['[1 2 3]]

pairs

Pairs may be formed using :.

  1:2
[1:2]

iterators

for may be used to iterate any sequence.

  (for 3)
[0 1 2]

: may be used to bind the current value within the loop body.

  (for c:"foo" $c)
[\f \o \o]

map may be used to transform sequences.

  (map &++ [1 2 3])
[Iter]

  (splat _)
[2 3 4]

continuations

The current continuation may be captured using suspend and evaluated using restore.

  (suspend "was here") 42
[Cont(2) "was here"]

  d (restore _)
[42]

todo

  • add macros
You might also like...
An experiment for using SwiftUI's custom timing Animation to create an orbital-like animation.
An experiment for using SwiftUI's custom timing Animation to create an orbital-like animation.

Orbital-SwiftUI-Animation An experiment for using SwiftUI's custom timing curve to create an orbital-like animation. How it looks: How it works: Apply

A Swift framework for using custom emoji in strings.
A Swift framework for using custom emoji in strings.

Emojica – a Swift framework for using custom emoji in strings. What does it do? Emojica allows you to replace the standard emoji in your iOS apps with

Completely customizable progress based loaders drawn using custom CGPaths written in Swift
Completely customizable progress based loaders drawn using custom CGPaths written in Swift

FillableLoaders Completely customizable progress based loaders drawn using custom CGPaths written in Swift Waves Plain Spike Rounded Demo: Changelog:

A prototype of custom tab bar using SwiftUI with techniques of mask + matchedGeometryEffect
A prototype of custom tab bar using SwiftUI with techniques of mask + matchedGeometryEffect

SliderTabBar A prototype of custom tab bar using SwiftUI with techniques of mask

Movies app written in Swift 5 using the Custom API created on the Mocky website
Movies app written in Swift 5 using the Custom API created on the Mocky website

Movie App shows you collections of TV streaming and other movies. Movie app writ

Stepper-View - Stepper view using with UICollectionView with Custom animation & Transation
Stepper-View - Stepper view using with UICollectionView with Custom animation & Transation

Stepper view using with UICollectionView with Custom animation & Transation. 🚀

StoryboardUsingCustomViews - Storyboard Using Custom Views
StoryboardUsingCustomViews - Storyboard Using Custom Views

Storyboard Using Custom Views Vista creada con: Storyboard + Constraints + Progr

CustomTabBar - A Custom TabBar Built Using Swift
CustomTabBar - A Custom TabBar Built Using Swift

CustomTabBar Thanks to Riccardo Cipolleschi for his awesome tutorial. The TabBar

Wallet App UI with custom Animation using SwiftUI 3.0 🤪
Wallet App UI with custom Animation using SwiftUI 3.0 🤪

iOS Wallet App UI Wallet App UI with custom Animation using SwiftUI 3.0 for educational purposes. Video Preview Screenshots Features SwiftUI Animation

VerticalFlowLayout - This implementation is built using a UICollectionView and a custom flowLayout.
VerticalFlowLayout - This implementation is built using a UICollectionView and a custom flowLayout.

VerticalFlowLayout This implementation is built using a UICollectionView and a custom flowLayout. Table of contents Requirements Installation CocoaPod

A Collection of PropertyWrappers to make custom Serialization of Swift Codable Types easy

CodableWrappers Simplified Serialization with Property Wrappers Move your Codable and (En/De)coder customization to annotations! struct YourType: Coda

ChainPageCollectionView  A custom View with two level chained collection views and fancy transition animation
ChainPageCollectionView A custom View with two level chained collection views and fancy transition animation

ChainPageCollectionView A custom View with two level chained collection views and fancy transition animation. Demo Requirements iOS 9.0+ Xcode 8 Insta

Ease is an event driven animation system that combines the observer pattern with custom spring animations as observers
Ease is an event driven animation system that combines the observer pattern with custom spring animations as observers

Ease is an event driven animation system that combines the observer pattern with custom spring animations as observers. It's magic. Features Animate a

Crossroad is an URL router focused on handling Custom URL Scheme
Crossroad is an URL router focused on handling Custom URL Scheme

Crossroad is an URL router focused on handling Custom URL Scheme. Using this, you can route multiple URL schemes and fetch arguments and parameters easily.

Programmatically load custom fonts into your iOS and tvOS app.

FontBlaster Programmatically load custom fonts into your iOS and tvOS app. About Say goodbye to importing custom fonts via property lists as FontBlast

An inkwell to use custom fonts on the fly.
An inkwell to use custom fonts on the fly.

Inkwell Introduction In brief, Inkwell is a font library to use custom fonts on the fly. Inkwell takes responsibilities for: Downloading fonts from Go

Font management (System & Custom) for iOS and tvOS
Font management (System & Custom) for iOS and tvOS

UIFontComplete Font management (System & Custom) for iOS and tvOS Usage No more wasted time searching for names of UIFont fonts and no more surprises

 ALCameraViewController - A camera view controller with custom image picker and image cropping.
ALCameraViewController - A camera view controller with custom image picker and image cropping.

ALCameraViewController A camera view controller with custom image picker and image cropping. Features Front facing and rear facing camera Simple and c

A custom ImageView that is used to cover the surface of other view like a scratch card, user can swipe the mulch to see the view below.
A custom ImageView that is used to cover the surface of other view like a scratch card, user can swipe the mulch to see the view below.

MCScratchImageView GIF Showcase Requirments iOS 8.0+ Xcode 7.2+ Swift 4.0 Installation CocoaPods pod "MCScratchImageView" Manually Just drag MCScratch

Owner
Andreas Nilsson
I am by nature a dealer in words, and words are the most powerful drug known to humanity.
Andreas Nilsson
Use any custom view as custom callout view for MKMapView with cool animations. Use any image as annotation view.

MapViewPlus About MapViewPlus gives you the missing methods of MapKit which are: imageForAnnotation and calloutViewForAnnotationView delegate methods.

Okhan Okbay 162 Nov 16, 2022
:octocat: AdaptiveController is a 'Progressive Reduction' Swift UI module for adding custom states to Native or Custom iOS UI elements. Swift UI component by @Ramotion

ADAPTIVE TAB BAR 'Progressive Reduction' module for adding custom states to Native or Custom UI elements. We specialize in the designing and coding of

Ramotion 2k Nov 9, 2022
null 13 Oct 28, 2022
Custom-Transition - A repo about custom transition between two view controllers

Custom-Transition in SWIFT This is a repo about custom transition between two vi

Prakash Chandra Awal 0 Jan 6, 2022
Custom-TopBarController - A Custom TopBar Controller With Swift

TopBarController Верстка Для IPhone и IPod вертска адаптивная, для IPad frane To

Fadeev Sergey 1 Aug 2, 2022
Custom-action-sheet- - Custom action sheet with swift

Custom-action-sheet- Usage let alertController: UIAlertControllerDimmed = UIAler

Girisankar G 0 Jan 19, 2022
Custom UITextFields effects inspired by Codrops, built using Swift

TextFieldEffects I fell in love with the text inputs effects in this article. As an exercise I decided to recreate as many of them as I can using Swif

Raul Riera 5.8k Dec 26, 2022
SwiftUI & Combine app using MovieDB API. With a custom Flux (Redux) implementation.

MovieSwiftUI MovieSwiftUI is an application that uses the MovieDB API and is built with SwiftUI. It demos some SwiftUI (& Combine) concepts. The goal

Thomas Ricouard 6.2k Jan 8, 2023
Custom MacBook login screen and pam modules using multipeer connectivity and usb hardware checks with iOS app for sign in.

Custom MacBook login screen and pam modules using multipeer connectivity and usb hardware checks with iOS app for sign in.

null 2 Aug 17, 2022
Sample code for Core ML using ResNet50 provided by Apple and a custom model generated by coremltools.

CoreML-samples This is the sample code for Core ML using ResNet50 provided by Apple. ResNet50 can categorize the input image to 1000 pre-trained categ

Yuta Akizuki 39 Nov 11, 2022