{"id":13438510,"url":"https://github.com/justinmeiners/lisp-interpreter","last_synced_at":"2025-03-25T22:31:35.434Z","repository":{"id":28289705,"uuid":"115947011","full_name":"justinmeiners/lisp-interpreter","owner":"justinmeiners","description":"Embeddable lisp/scheme interpreter written in C.","archived":false,"fork":false,"pushed_at":"2022-11-20T05:43:29.000Z","size":2359,"stargazers_count":144,"open_issues_count":1,"forks_count":10,"subscribers_count":9,"default_branch":"master","last_synced_at":"2024-10-28T00:23:08.011Z","etag":null,"topics":["c","hacktoberfest","interpreter","lisp","lisp-interpreter","scheme","scheme-interpreter","scheme-language"],"latest_commit_sha":null,"homepage":"","language":"Scheme","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/justinmeiners.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2018-01-01T20:51:26.000Z","updated_at":"2024-10-06T09:32:18.000Z","dependencies_parsed_at":"2022-08-24T17:50:47.830Z","dependency_job_id":null,"html_url":"https://github.com/justinmeiners/lisp-interpreter","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/justinmeiners%2Flisp-interpreter","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/justinmeiners%2Flisp-interpreter/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/justinmeiners%2Flisp-interpreter/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/justinmeiners%2Flisp-interpreter/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/justinmeiners","download_url":"https://codeload.github.com/justinmeiners/lisp-interpreter/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":222099519,"owners_count":16931428,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["c","hacktoberfest","interpreter","lisp","lisp-interpreter","scheme","scheme-interpreter","scheme-language"],"created_at":"2024-07-31T03:01:06.132Z","updated_at":"2024-10-29T19:07:40.591Z","avatar_url":"https://github.com/justinmeiners.png","language":"Scheme","funding_links":[],"categories":["C","Scheme"],"sub_categories":[],"readme":"\nLisp Interpreter\n================\n\n\u003e Any sufficiently complicated C or Fortran program contains an ad hoc, informally-specified, bug-ridden, slow implementation of half of Common Lisp. -- Philip Greenspun\n\nAn embeddable lisp/scheme interpreter written in C.\nIt includes a subset of R5RS with some extensions from MIT Scheme.\n\nI created this while reading [SICP](https://github.com/justinmeiners/sicp-excercises) to improve my knowledge of lisp\nand to make an implementation that allows me to easily add scripting to my own programs.\n\n### Philosophy\n\n- **Simple**: This project doesn't aim to be optimal, or fully standards compliant.\n    It is just a robust foundation for scripting. \n    It is implemented as a recursive AST walker on the C stack.\n\n    If you need more try [s7](https://ccrma.stanford.edu/software/snd/snd/s7.html) or [chicken](https://www.call-cc.org)\n\n- **Unintrusive**: Just copy in the header file. Turn on and off major features with build macros. It should be portable between major platforms.\n\n- **Unsurprising**: You should be able to read the source code and understand how it works.\n  The C API should work how you expect.\n\n- **First class data**: Lisp s-expressions are undervalued as an alternative to JSON or XML.\n    Preprocessor flags can remove most scheme features if you just want to read s-expressions\n    and manipulate them in C.\n\n### Features\n\n- C99, no dependencies, two files.\n- Core lisp language `if`, `let`, `do`, `lambda`, `cons`, `eval`, etc.\n- Subset of scheme R5RS library: lists, vectors, hash tables, integers, real numbers, characters, strings, and integers.\n- Common lisp goodies: unhygenic macros (`define-macro`), `push`, `dotimes`.\n- Easy to integrate C functions.\n- Exact [garbage collection](#garbage-collection) with explicit invocation.\n- REPL command line tool.\n- Efficient parsing and manipulation of large data files.\n\n### Non-Features\n\n- Compiler or VM.\n- Full numeric tower.\n- Full call/cc. This only supports simple stack jumps.\n- syntax rules.\n- UNIX system interface/IO library.\n\n### Examples\n\n### Interactive programming with Read, eval, print loop.\n```bash\n$ ./lisp\n\u003e (define (sqr x) (* x x)))\n\u003e (define length 40)\n\u003e (define area 0)\n\u003e (set! area (sqr length))\n1600\n```\n\n### Quickstart\n\n```c\nLispContext ctx = lisp_init();\nlisp_load_lib(ctx);\n\nLispError error;\nLisp program = lisp_read(\"(+ 1 2)\", \u0026error, ctx);\nLisp result = lisp_eval(program, \u0026error, ctx);\n\nif (error != LISP_ERROR_NONE)\n    lisp_print(result); ; =\u003e 3\n\nlisp_shutdown(ctx);\n```\n\n### Loading Data\n\nLisp s-expressions can be used as a lightweight substitute to JSON or XML.\nLooking up keys which are reused is even more efficient due to symbol comparison.\n\nJSON\n```json\n{\n   \"name\" : \"Bob Jones\",\n   \"age\" : 54,\n   \"city\" : \"SLC\",\n}\n```\n\nLisp\n```scheme\n#((name . \"Bob Jones\")\n  (age . 54)\n  (city . \"SLC\"))\n```\nLoading the structure in C.\n\n```c\nLispContext ctx = lisp_init();\n// load lisp structure\nLisp data = lisp_read_file(file, ctx);\n// get value for age\nLisp age_entry = lisp_avector_ref(data, lisp_make_symbol(\"AGE\", ctx), ctx);\n// ...\nlisp_shutdown(ctx);\n```\n\n### Calling C functions\n\nC functions can be used to extend the interpreter, or call into C code.\n\n```c\nLisp integer_range(Lisp args, LispError* e, LispContext ctx)\n{\n    // first argument\n    LispInt start = lisp_int(lisp_car(args));\n    args = lisp_cdr(args);\n    // second argument\n    LispInt end = lisp_int(lisp_car(args));\n\n    if (end \u003c start)\n    {\n        *e = LISP_ERROR_OUT_OF_BOUNDS;\n        return lisp_null();\n    }\n\n    LispInt n = end - start;\n    Lisp numbers = lisp_make_vector(n, ctx);\n\n    for (LispInt i = 0; i \u003c n; ++i)\n        lisp_vector_set(numbers, i, lisp_make_int(start + i));\n\n    return numbers;\n}\n// ...\n\n// wrap in Lisp object\nLisp func = lisp_make_func(integers_in_range);\n\n// add to enviornment with symbol INTEGER-RANGE\nLisp env = lisp_env_global(ctx);\nlisp_env_define(env, lisp_make_symbol(\"INTEGER-RANGE\", ctx), func, ctx);\n```\n\nIn Lisp\n```scheme\n(integer-range 5 15)\n; =\u003e #(5 6 7 8 9 10 11 12 13 14)\n```\nConstants can also be stored in the environment in a similar fashion.\n\n```c\nLisp pi = lisp_make_real(3.141592);\nlisp_env_define(env, lisp_make_symbol(\"PI\", ctx), pi, ctx);\n```\n### Macros\n\nCommon Lisp style (`defmacro`) is available with the name `define-macro`.\n\n    (define-macro nil! (lambda (x)\n      `(set! ,x '()))\n\n### Garbage Collection\n\nGarbage is only collected if it is explicitly told to.\nYou can invoke the garbage collector in C:\n\n    lisp_collect(ctx);\n\nOR in lisp code:\n\n    (gc-flip)\n\nNote that whenever a collect is issued\nANY `Lisp` value in `C`which is not accessible\nthrough the global environment may become invalid.\nBe careful what variables you hold onto in C.\n\nDon't call `eval` in a custom defined C function unless you know what you are doing.\n\nSee [internals](INTERNALS.md) for more details.\n\n## Documentation\n\nFor the language refer to [MIT Scheme](https://groups.csail.mit.edu/mac/ftpdir/scheme-7.4/doc-html/scheme_toc.html)\nwith the understanding that not everything is missing.\nIf we do implement a feature that MIT scheme has, we will try to follow their specificaiton.\n\nFor the C API refer to the header and sample programs (`repl.c`, `printer.c`).\n\n## Project License\n\nCopyright (c) 2020 Justin Meiners\n\nPermission to use, copy, modify, and distribute this software for any\npurpose with or without fee is hereby granted, provided that the above\ncopyright notice and this permission notice appear in all copies.\n\nTHE SOFTWARE IS PROVIDED \"AS IS\" AND THE AUTHOR DISCLAIMS ALL WARRANTIES\nWITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF\nMERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR\nANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES\nWHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN\nACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF\nOR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.\n\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjustinmeiners%2Flisp-interpreter","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fjustinmeiners%2Flisp-interpreter","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjustinmeiners%2Flisp-interpreter/lists"}