Code Monkey home page Code Monkey logo

slang's Introduction

slang

Simple s-expressions language implemented in Haskell

Contains a couple of command-line programs to run a slang-based program:

  • slerp - Pass it the name of a file with slang code and it will run the program contained and print the result to standard out.
  • sli - The slang REPL.

Compile these programs using ghc --make and passing in either 'slerp.hs' or 'sli.hs' depending on which program you wish to compile.

Current Features

  • Interpreted
  • Function closures
  • Dynamic typing
  • REPL

Syntax

Uses a standard lisp syntax with all the fun of parentheses.

Adding two numbers

>> (+ 1 2)
3

Defining a function

>> (defn add (x y) (+ x y))

slang's People

Contributors

acolley avatar

Stargazers

Noisy Not avatar

Watchers

James Cloos avatar  avatar

slang's Issues

ExprData

As part of the refactor create an ExprData type that carries around an Expr with other implicit data. This can use a Reader monad to store the required data which will be things like line/column numbers for use in errors.

Related to #16.

Define a 'let*' 'macro'

let* will allow later bindings in the (name, expression) list to refer to earlier bindings when being defined.

Implement 'to-str' function

Implement a way to convert an expression to a more convenient, human-readable form.

Some examples for string representations:

Fun -> "fn"
Number 1 -> "1"

Port to OCaml or Rust?

After hitting a wall with IO the language probably needs to be re-written in something else that isn't as pure as Haskell but supports the same pattern-matching and recursive logic programming as Haskell so that the logic can be ported easily.

Refactor to use Monad transformers and state

The lexer, parser and evaluator should all use Monad transformers to pass around state and other associated information to get more useful error messages.

In addition we can then use a Monad transformer during evaluation to get access to the IO Monad and execute the program with IO.

The sort of structure we want is to be able to store information like line/column numbers of each token at the lexer stage. Then in the parser stage use these line/column numbers if any errors come up (also passing around the string that represents the program so that it can be highlighted when printing error messages).

The parser will also associate these line/column numbers with each expression that it parses.

Finally, when it comes to the evaluation stage each expression will have access to some information about where it is in the original file (line/column number) and if any errors occur more information can be displayed (if a function received too many arguments we can display the name of the function and whereabouts it was called).

Add a Char type

Add a Char fundamental type to the language. To avoid clashes with Haskell's Char type it should probably be called "Chr" internally.

Write a 'defn' macro

This should work in a similar way to the 'def' macro, however this also allows a function to be recursive.

It should bind a name to a function whose name is also the binding name in the 'defn'.

Without this there is no way to define recursive functions, as they cannot refer to themselves in a let expression.

Interpreter tool

A tool that takes a file containing slang source code and interprets this, producing the result of evaluating the file to stdout.

Call it something like 'slerp' (slang + interp(ret))

Add line numbers

Associate a line number with tokens when they are being parsed in the Lexer.

Also associate a first/last line number with expressions when they are parsed in the Parser.

Make Number operations global functions

Make Addition, Subtraction, Multiplication and Division functions at the top-level of a program so they can be used in the same way as any other functions (first-class values).

This relies on issue #2

Implement multi argument functions

At the moment any functions that need more than one argument are curried, leading to lots of unnecessary calling parentheses.

To get around this either:

  1. Syntactical sugar for calling curried functions (e.g. only have to have one set of parens and can pass a list of arguments to the function)
  2. Have functions not curried by default (this is the same as other Lisp-likes (e.g. Racket) and instead take a list of parameters.

REPL tool

Create a Read Eval Print Loop tool that prompts the user for stdin containing a slang expression and evaluates it, printing the result of the expression to stdout.

Add IO

By adding IO to the language it can support actual use cases.

Add a fundamental function called "writeBytes" that takes a writeable handle as its first argument and a list of bytes to write to that handle.

Add a fundamental function called "write" that takes a writeable handle as its first argument and a string to write to that handle.

Add a fundamental function called "readByte" that takes a handle as its first argument and attempts to read a single byte from that handle.

Add a fundamental function called "readChar" that takes a handle as its first argument and attempts to read a single UTF-8 encoded char from that handle.

Add a fundamental function called "readLine" that takes a handle as its first argument and attempts to buffer read a UTF-8 encoded string from that handle delimited by an end of line character ("\n\r").

Write a 'def' macro in Eval.hs

The 'macro' should expand to a let expression that encompasses the whole program up to that point with the binding being made.

e.g.

(def foo 10)
(def bar 10)

(+ foo bar)

expands to

(let foo 10
  (let bar 10
    (+ foo bar)))

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.