snail
所属分类:编程语言基础
开发工具:Haskell
文件大小:0KB
下载次数:0
上传日期:2023-08-24 13:36:49
上 传 者:
sh-1993
说明: 腹足类的编程语言,
(A programming language for gastropods,)
文件列表:
CHANGELOG.md (298, 2023-09-07)
LICENSE (1071, 2023-09-07)
Makefile (1009, 2023-09-07)
example/ (0, 2023-09-07)
example/Main.hs (1751, 2023-09-07)
flake.lock (2046, 2023-09-07)
flake.nix (2326, 2023-09-07)
package.yaml (1395, 2023-09-07)
snail-files/ (0, 2023-09-07)
snail-files/basic.snail (341, 2023-09-07)
snail-files/fail-comment.snail (7, 2023-09-07)
snail-files/fail-empty.snail (0, 2023-09-07)
snail-files/fail-nil.snail (12, 2023-09-07)
snail-files/fail-quotes-2.snail (19, 2023-09-07)
snail-files/fail-quotes-3.snail (13, 2023-09-07)
snail-files/fail-quotes.snail (24, 2023-09-07)
snail-files/fail.snail (52, 2023-09-07)
snail-files/nil.snail (4, 2023-09-07)
snail.cabal (3175, 2023-09-07)
src/ (0, 2023-09-07)
src/Snail.hs (159, 2023-09-07)
src/Snail/ (0, 2023-09-07)
src/Snail/Ast.hs (1441, 2023-09-07)
src/Snail/Characters.hs (573, 2023-09-07)
src/Snail/IO.hs (749, 2023-09-07)
src/Snail/Lexer.hs (3588, 2023-09-07)
src/Snail/ToText.hs (632, 2023-09-07)
test/ (0, 2023-09-07)
test/Gen.hs (330, 2023-09-07)
test/Snail/ (0, 2023-09-07)
test/Snail/IOSpec.hs (2125, 2023-09-07)
test/Snail/LexerSpec.hs (6193, 2023-09-07)
test/Snail/ToTextSpec.hs (962, 2023-09-07)
test/Spec.hs (208, 2023-09-07)
test/files/ (0, 2023-09-07)
test/files/fennel-reference.fnl (4824, 2023-09-07)
test/files/learnxyz.cl (3256, 2023-09-07)
test/files/r5rs_pitfalls.scm (10355, 2023-09-07)
... ...
# Snail
A no-semantics programming language for gastropods.
## Why?
My colleagues and I are going to start working through [Types and Programming
Languages][tapl]. In the book you implement languages of varying feature sets.
The book implements these languages in OCaml, however I had this Lisp parser
essentially ready for awhile. There are a handful of "Write you a Scheme
Interpreters"-like tutorials and they all use a parser relatively similar to
this one. However, there are some pretty subtle issues with most of the ones I
have seen. For example, the two examples below parse as two lexemes in a lot of
examples. Even Haskell's parser has [this issue][haskell-parse-issue]!
```
(1a)
(1 a)
```
## Is this really a programming language?
From the ["Programming language" Wikipedia page][pl-wikipedia],
> A programming language is a system of notation for writing computer programs.
> The description of a programming language is usually split into the two components of syntax (form) and semantics (meaning)
Snail is used for writing interpreters or compilers. However, it doesn't define
**any** semantics. So, maybe?
## Syntax (form)
Snail describes valid lexemes, text literals, and s-expressions. The valid
lexemes are approximately from R5RS Scheme but this may change in the future.
We also use Haskell's line and block comments. Here is a valid snail program,
```
-- Prints `hello "world"` to the console
(print "hello \"world\"")
-- Prints 3 to the console
(print (+ 1 2))
{-
Defines a function to add two numbers
Applies the function to generate 3
Prints 3 to the console
-}
(let
(f (lambda (x y) (+ x y)))
(print (f 2 1)))
(quote hello)
(nil)
(print true)
(print false)
-- end comment
```
Reminder, this program has no semantics. It is your job to take Snail's
Abstract Syntax Tree (AST) and define the semantics of an interpreter or
compiler.
## Getting the AST
You have two options: `readSnailFile` or `parseSnail`.
`readSnailFile` can be used like this, assuming you have put some valid snail
into a file `./hello.snail`,
```haskell
import Snail
printSnail :: IO ()
printSnail = do
eResults <- readSnailFile "./hello.snail"
case eResults of
Right ast -> print ast
Left failureString -> print failureString
```
`parseSnail` doesn't require `IO` the only parameter is `Text`. This is useful
for one-line programs, e.g.,
```haskell
{-# LANGUAGE OverloadedStrings #-}
import Snail
example :: Either String [SnailAst]
example = parseSnail "(print false)"
```
## Example Interpreters
1. The `arith` language from [Types and Programming Languages][tapl]: https://github.com/chiroptical/snail-arith/blob/main/src/Lib.hs
2. Languages from [essentials-of-compilation][essentials-of-compilation]: https://github.com/chiroptical/essentials-of-compilation (each chapter is a module)
## Differences from S-Cargot
I was recently reminded of [s-cargot][s-cargot]. From their GitHub,
> S-Cargot is a library for parsing and emitting S-expressions, designed to be
> flexible, customizable, and extensible.
This is quite different from Snail. Snail is not flexible, customizable, or
extensible. I have chosen a syntax and either you use it or you don't.
With S-Cargot, you can build a custom S-expression parser. Snail provides one.
I don't think you can re-write Snail using S-Cargot? It doesn't appear to
support `'(x)` or `(let [x 10] x)`. Please correct me if I missed something.
I tried to mimic their example
(https://github.com/aisamanra/s-cargot/blob/master/example/example.hs) in
./example/Main.hs.
## Resources for implementing interpreters and compilers
These are resources I am personally interested in exploring, there are **many** others
- [Types and Programming Languages][tapl]
- [Essentials of Compilation][essentials-of-compilation]
- [Compiler Design][cs4410]
- [Compiler Construction][cse131]
[tapl]: https://www.cis.upenn.edu/~bcpierce/tapl
[haskell-parse-issue]: https://twitter.com/chiroptical/status/1471568781906518018
[pl-wikipedia]: https://en.wikipedia.org/wiki/Programming_language
[essentials-of-compilation]: https://mitpress.mit.edu/9780262047760/essentials-of-compilation
[s-cargot]: https://github.com/aisamanra/s-cargot
[cs4410]: https://course.ccs.neu.edu/cs4410sp21/
[cse131]: https://ucsd-cse131-f19.github.io/
近期下载者:
相关文件:
收藏者: