jcubic / jcubic/lips

define-library and import (R7RS)

Open
#129 3 comments 0 reactions 0 assignees View on GitHub
enhancement in progress
Dominant language
JavaScript
Stars
507
Forks
45
Avg merge
11h 22m
Merged PRs (30d)
4

Description

This feature should be added to version 1.0

This is early implementation of module macro before I've knew about `define-library` or even before I've started working on R7RS spec, it can be refactored into R7RS library.

```scheme
;; -----------------------------------------------------------------------------
(define-macro (module name . body)
"(module module-name . body)

Macro for defining modules inside you can use define to create functions.
And use export name to add that name to defined environment."
(let ((parent (. (current-environment) '__parent__))
(module-name (gensym)))
`(let ((,module-name (new-module ,(symbol->string name))))
(define-macro (export name) `(--> ,,module-name (set ',name ,name)))
,@body
(--> ,parent (set ',name ,module-name)))))

;; -----------------------------------------------------------------------------
(define-macro (import name module)
"(input function-name module)

Macro for importing function from module"
`(define ,name (--> ,module (get ',name))))

;; -----------------------------------------------------------------------------
(define-class Module Object
(constructor
(lambda (self name env)
(set-obj! self 'env env)
(set-obj! self 'name name)))
(get
(lambda (self name)
(--> self.env (get name))))
(set
(lambda (self name function)
(--> self.env (set name function))))
(toString
(lambda (self)
(string-append "#"))))

;; -----------------------------------------------------------------------------
(define (new-module name)
"(new-module name)

Create new empty module object with empty env."
(let ((env (new lips.Environment
(string-append "module-" (--> name (toLowerCase))))))
(new Module name env)))

;; -----------------------------------------------------------------------------
(module Math
(define (square_ x)
"(Math::square x)

Function square argument"
(* x x))
(export square_))

(import square_ Math)

(display (square_ 10))
(newline)
```

Contributor guide

Open the contributing guide

Research direction

Start by comparing the proposed module and import macros with the R7RS define-library and import requirements. Use the Scheme example in the issue as the behavioral reference, then identify the interpreter entry points and tests that would need to cover library definition, exported names, and imports. Done means the feature is implemented for version 1.0 and its behavior is verified.

Written by the indexing model from the issue text.

Assessment

Tech stack
javascript
Domain
compilers
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.