Git Product home page Git Product logo

lispcc's Introduction

LispCC

A simple and fun Scheme interpreter written in C++ (C++11).

Scheme is a simple but powerful functional language first developed in the 1990s at MIT.

I've enjoyed reading Peter Norvig's blog (How to Write a (Lisp) Interpreter (in Python)), in which he writes a Scheme interpreter in 90+ lines of Python. The C++ implementation is a bit more challenging and I thought I'd give it a try.

UC Berkley's CS61A: Structure and Interpretation of Computer Programs has also been very helpful.

Core Concepts

The interpreter is divided into 2 parts.

1. Parsing (Lexical Analysis)
  - Read input program
  - Break program into tokens
  - Analysis of tokens according our Abstract Syntax Tree
2. Evaluation & Execution 
  - Tokens are processed according to the semantics of Scheme
  - Carry out the computation

Storing Scheme Expressions

  • Scheme objects - defined in SList.cpp
  • Environment - stored as STL unordered_map

Overview of internal processes

Program => Parse => Abstract Syntax Tree => eval => result

Simple Demo Program

(define square (lambda (x) (* x x))
=> LAMBDA
(define pi 3.1416)
=> 3.141600
(if (> pi (square (pi))) (square (pi)) else (sin (sqrt (square (pi)))))
=> -0.000007

Installation

Prerequisites

GNU g++ -std=c++11

Installing

To clone this repo to your local machine:

$ git clone
$ cd SchemePlusPlus

Compile and Link:

$ make build

Run:

$ ./scheme

Clean:

$ make clean

How it works

Representation of Scheme objects in C++

Scheme Data Type Our Internal Representation
Numbers C++'s 'int' and 'double'
Symbols C++'s built-in std::string data type.
Booleans (#t, #f) C++'s built-in std::string data type.
Pairs and Lists The SList class, defined in SList.cpp.

List of Supported Scheme Expressions

quote, '
if ... else ...
define
set!
lambda
+, -, *, /, >, <, >=, <=
=, eqv?
abs
append
apply
begin
car
cdr
cons
length
list
list?
map
max
min
null?
number?
procedure?
symbol?
mod
sqrt
sin
cos
tan
asin
acos
atan

Files

  • FormattedIO.cpp: reading input from stdin
  • Parser.cpp: lexical analysis
  • SList.cpp: stores any scheme expression
  • NativeProcedures.cpp: defines the standard Scheme procedures
  • Environment.cpp: execution environement, can be nested
  • Main.cpp: REPL

Example Scheme Programs

Running Tests

Contributing

Feel free to contribute and/or create new issues

Versioning

I use SemVer for versioning. For the versions available, see the tags on this repository.

Authors

See also the list of contributors who participated in this project.

License

This project is licensed under the MIT License - see the LICENSE file for details

Acknowledgments

  • Peter Norvig, Director of Research, Google Inc.

lispcc's People

Contributors

jsun98 avatar

Watchers

James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.