Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
45 commits
Select commit Hold shift + click to select a range
53b4343
feat: Add james-to-scheme.scm module with macro-based James to Scheme…
dckc Jan 10, 2025
79ccf6f
feat: Enhance james-to-scheme macro with return, object literal, and …
dckc Jan 10, 2025
5284358
feat: Modify james-to-scheme.scm to evaluate and test counter macro e…
dckc Jan 10, 2025
58fc217
fix: Correct let macro expansion in james-to-scheme.scm
dckc Jan 10, 2025
9c61493
feat: Add += macro for incrementing variables in Scheme DSL
dckc Jan 10, 2025
188e6d9
feat: Add fn macro to expand into lambda with begin
dckc Jan 10, 2025
c620d54
feat: Add %r macro for creating hash-table records with key-value pairs
dckc Jan 10, 2025
67724a0
feat: Enhance fn macro to support let bindings with scoped expressions
dckc Jan 10, 2025
1731b04
refactor: Enhance fn macro to handle let bindings anywhere in functio…
dckc Jan 10, 2025
9557abf
fix: Correct process-fn-body macro to handle single expression case
dckc Jan 10, 2025
1008f36
refactor: Simplify process-fn-body macro to handle multiple expressions
dckc Jan 10, 2025
abf519c
refactor: Replace `let` with `var` in DSL macro implementation
dckc Jan 10, 2025
8f461f8
fix: Handle var binding in macro with standalone and multi-expression…
dckc Jan 10, 2025
5fff681
feat: Add var keyword support to fn macro syntax-rules
dckc Jan 10, 2025
949b34b
feat: Add support for var bindings in process-fn-body macro
dckc Jan 10, 2025
a41bd37
refactor: Simplify process-fn-body macro with recursive var binding h…
dckc Jan 10, 2025
aee86dd
refactor: Improve process-fn-body macro to handle multiple expression…
dckc Jan 10, 2025
9098a47
refactor: Simplify and clean up macro expansion logic in dsl-1.scm
dckc Jan 10, 2025
563b23b
refactor: Fix macro expansion for process-fn-body with rest parameter
dckc Jan 10, 2025
88f3b92
refactor: Improve macro expansion for function body processing in DSL
dckc Jan 10, 2025
bf5e928
refactor: Improve process-fn-body macro pattern matching and handling
dckc Jan 10, 2025
0a64310
refactor: Fix macro expansion for process-fn-body with correct syntax
dckc Jan 10, 2025
f728623
feat: Add $ macro for method call syntax in DSL
dckc Jan 10, 2025
6981a31
refactor: Update %r macro test to use methods instead of fields
dckc Jan 10, 2025
d785a5d
refactor: Update `$` macro to use hash-ref for method retrieval
dckc Jan 10, 2025
389ed00
feat: Add test case for %r macro with simple number properties
dckc Jan 10, 2025
818cc1f
feat: Add @ macro for simplified hash-ref field access
dckc Jan 10, 2025
071f7bd
feat: Add `-=` macro for in-place subtraction
dckc Jan 10, 2025
6a265be
feat: Add module macro with defconst support for body processing
dckc Jan 10, 2025
c0c4570
feat: Add test for module macro with circle area computation
dckc Jan 10, 2025
892fbd4
feat: Add counter object test with makeOffsetCounter method to dsl-1.scm
dckc Jan 10, 2025
01249a4
feat: Export 'return' identifier in dsl-1 module to resolve unbound v…
dckc Jan 10, 2025
d463b26
refactor: Modify process-body macro to handle return statements corre…
dckc Jan 10, 2025
463baac
refactor: Replace let* with module for counter test implementation
dckc Jan 10, 2025
b62fb5a
refactor: Replace let with defconst in counter test
dckc Jan 10, 2025
7daa2b8
feat: Add recursive doubler test case to dsl-1.scm
dckc Jan 10, 2025
0f6179b
refactor: Replace module with let in doubler test implementation
dckc Jan 10, 2025
85bd63e
fix: Correct recursive doubler macro to properly return value
dckc Jan 10, 2025
c78c69a
refactor: Replace display calls with console.log method
dckc Jan 10, 2025
57f2f97
feat: Add #:rest support to fn macro for variadic arguments
dckc Jan 10, 2025
0da8ace
feat: Add SRFI-88 keyword objects module import to dsl-1.scm
dckc Jan 10, 2025
0edaaae
refactor: Replace #:rest with rest: in fn macro and console.log imple…
dckc Jan 10, 2025
9d90e39
refactor: Remove commented-out SRFI-88 module import
dckc Jan 10, 2025
21f1608
fix: Modify += and -= macros to return new value after operation
dckc Jan 10, 2025
c3eb6ac
replace james-to-scheme with dsl-1
dckc Jan 10, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions counter.scm
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
(module
(defconst makeCounter
(fn (init)
(var value init)
(return (%r
(: increment (fn () (+= value 1)))
(: decrement (fn () (-= value 1)))
(: makeOffsetCounter
(fn (delta)
(return (makeCounter (+ value delta)))))))))
(defconst c1 (makeCounter 1))
($ c1 increment)
($ console log ($ c1 decrement)))
189 changes: 189 additions & 0 deletions james-to-scheme.scm
Original file line number Diff line number Diff line change
@@ -0,0 +1,189 @@
;; DSL macros for James-like syntax

(define-module (rockit james scheme-skin)
#:use-module (ice-9 hash-table)
;; #:use-module (srfi srfi-88) ;; keyword objects
#:export (+= fn %r $ @ module return))

;; Macro for field access using (@ obj field)
(define-syntax @
(syntax-rules ()
((_ obj field)
(hash-ref obj 'field))))

;; Macro for method calls using ($ target method args ...)
(define-syntax $
(syntax-rules ()
((_ target method args ...)
((hash-ref target 'method) args ...))))

;; Macro to expand %r into hash-table creation
(define-syntax %r
(syntax-rules (:)
((_ (: key value) ...)
(let ((tbl (make-hash-table)))
(hash-set! tbl 'key value) ...
tbl))))

;; Helper macro to process body and handle bindings
(define-syntax process-body
(syntax-rules (var defconst return)
;; When we see a var binding followed by more expressions
((_ ((var name val) rest ...))
(let ((name val))
(process-body (rest ...))))
;; When we see just a var binding alone
((_ ((var name val)))
(let ((name val))
name))
;; When we see a defconst binding followed by more expressions
((_ ((defconst name val) rest ...))
(let ((name val))
(process-body (rest ...))))
;; When we see just a defconst binding alone
((_ ((defconst name val)))
(let ((name val))
name))
;; Handle return - stop processing and return the value
((_ ((return val) rest ...))
val)
((_ ((return val)))
val)
;; Empty case
((_ ())
(if #f #f))
;; Single expression case
((_ (expr))
expr)
;; Multiple expressions case
((_ (first rest ...))
(begin
first
(process-body (rest ...))))))

;; Macro to expand fn into lambda with begin, handling let bindings and rest args
(define-syntax fn
(syntax-rules (var rest:)
((_ (args ... rest: rest-arg) body ...)
(lambda (args ... . rest-arg)
(process-body (body ...))))
((_ (args ...) body ...)
(lambda (args ...)
(process-body (body ...))))))

;; Macro to handle module bodies similar to fn but with defconst
(define-syntax module
(syntax-rules (defconst)
((_ body ...)
(let ()
(process-body (body ...))))))

;; Macro to expand += into a set! with addition
(define-syntax +=
(syntax-rules ()
((_ var val)
(let ((new-val (+ var val)))
(set! var new-val)
new-val))))

;; Macro to expand -= into a set! with subtraction
(define-syntax -=
(syntax-rules ()
((_ var val)
(let ((new-val (- var val)))
(set! var new-val)
new-val))))

;; Define console object with log method
(define console
(%r (: log (fn (first rest: rest)
(display first)
(for-each (lambda (arg)
(display " ")
(display arg))
rest)
(newline)))))

;; Test case
(let ((x 5))
($ console log "x before:" x)
(+= x 1)
($ console log "x after:" x))

;; Test fn macro
($ console log "\nTesting fn macro:")
(let ((add2 (fn (x)
($ console log "Adding 2 to" x)
(+ x 2))))
($ console log "Result:" (add2 40)))

;; Test %r macro with methods
($ console log "\nTesting %r macro with methods:")
(let ((point (%r
(: get-x (fn () 10))
(: get-y (fn () 20)))))
($ console log "Point x:" ($ point get-x))
($ console log "Point y:" ($ point get-y)))

;; Test %r macro with simple properties
($ console log "\nTesting %r macro with properties:")
(let ((point (%r
(: x 10)
(: y 20))))
($ console log "Point x:" (@ point x))
($ console log "Point y:" (@ point y)))

;; Test fn macro with var binding
($ console log "\nTesting fn macro with var:")
(let ((make-adder (fn (x)
($ console log "Computing with input:" x)
(var result (+ x 2))
($ console log "Computed result:" result)
result)))
($ console log "Result:" (make-adder 40)))

;; Test module macro
($ console log "\nTesting module macro:")
(let ((result (module
(defconst pi 3.14159)
(defconst radius 2)
(defconst area (* pi (* radius radius)))
($ console log "Computing circle area with radius:" radius)
area)))
($ console log "Circle area:" result))

;; Test simple recursive doubler
($ console log "\nTesting simple recursive doubler:")
(let ()
(define make-doubler
(fn (init)
(var value init)
(return (%r
(: double (fn ()
($ console log "Doubling:" value)
(if (< value 4)
($ (make-doubler (* value 2)) double)
value)))))))
(define d1 (make-doubler 1))
($ d1 double))

;; Test counter object with makeOffsetCounter
($ console log "\nTesting counter with offset:")
(let ()
(define make-counter
(fn (init)
(var value init)
(return (%r
(: increment (fn () (+= value 1)))
(: decrement (fn () (-= value 1)))
(: makeOffsetCounter
(fn (delta)
(return (make-counter (+ value delta)))))))))
(define c1 (make-counter 5))
($ console log "Initial counter at 5")
($ c1 increment)
($ console log "After increment:" ($ c1 decrement))
(define c2 ($ c1 makeOffsetCounter 10))
($ console log "New counter with +10 offset")
($ c2 increment)
($ console log "After increment:" ($ c2 decrement)))