From 53b43438e6279f563308a9f7764d8f85d8748c14 Mon Sep 17 00:00:00 2001 From: "Dan Connolly (aider)" Date: Thu, 9 Jan 2025 23:54:06 -0600 Subject: [PATCH 01/45] feat: Add james-to-scheme.scm module with macro-based James to Scheme translation --- james-to-scheme.scm | 65 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 65 insertions(+) create mode 100644 james-to-scheme.scm diff --git a/james-to-scheme.scm b/james-to-scheme.scm new file mode 100644 index 0000000..9091a43 --- /dev/null +++ b/james-to-scheme.scm @@ -0,0 +1,65 @@ +;; JAMES: Scheme macro-based implementation + +(define-module (rockit james scheme) + #:use-module (ice-9 match) + #:use-module (ice-9 regex) + #:export (expand-james->scheme)) + +;; Special forms that need custom handling +(define james-special-forms + '(fn defn begin def let module + if cond while for switch + try throw return break continue)) + +;; Convert James expressions to Scheme +(define-syntax expand-james->scheme + (syntax-rules () + ;; Function definition + ((_ (defn (name params ...) body ...)) + (define (name params ...) + (let () body ...))) + + ;; Lambda expression + ((_ (fn (params ...) body ...)) + (lambda (params ...) + (let () body ...))) + + ;; Let binding + ((_ (let var val)) + (define var val)) + + ;; Method call (.-> obj (method args)) + ((_ (.-> obj methods ...)) + ((compose methods ...) obj)) + + ;; Object literal + ((_ (%r props ...)) + (make-hash-table props ...)) + + ;; Array literal + ((_ #(items ...)) + (vector items ...)) + + ;; Basic operators map directly + ((_ (+ args ...)) (+ args ...)) + ((_ (- args ...)) (- args ...)) + ((_ (* args ...)) (* args ...)) + ((_ (/ args ...)) (/ args ...)) + + ;; Default - pass through unchanged + ((_ expr) expr))) + +;; Helper to convert a James expression to Scheme +(define (james->scheme expr) + (syntax->datum + (expand-james->scheme expr))) + +;; Example usage: +#| +(james->scheme + '(defn (make-counter init) + (let value init) + (return + (%r (increment (lambda () (set! value (+ value 1)))) + (decrement (lambda () (set! value (- value 1)))))))) +|# From 79ccf6fcb620b8f3b654611e809c444862fa9d18 Mon Sep 17 00:00:00 2001 From: "Dan Connolly (aider)" Date: Fri, 10 Jan 2025 00:01:48 -0600 Subject: [PATCH 02/45] feat: Enhance james-to-scheme macro with return, object literal, and set! support --- james-to-scheme.scm | 35 ++++++++++++++++++++++------------- 1 file changed, 22 insertions(+), 13 deletions(-) diff --git a/james-to-scheme.scm b/james-to-scheme.scm index 9091a43..c89eb4c 100644 --- a/james-to-scheme.scm +++ b/james-to-scheme.scm @@ -3,7 +3,7 @@ (define-module (rockit james scheme) #:use-module (ice-9 match) #:use-module (ice-9 regex) - #:export (expand-james->scheme)) + #:export (expand-james->scheme james->scheme)) ;; Special forms that need custom handling (define james-special-forms @@ -28,13 +28,19 @@ ((_ (let var val)) (define var val)) + ;; Return statement + ((_ (return expr)) + expr) + ;; Method call (.-> obj (method args)) ((_ (.-> obj methods ...)) ((compose methods ...) obj)) - ;; Object literal - ((_ (%r props ...)) - (make-hash-table props ...)) + ;; Object literal with property definitions + ((_ (%r (prop val) ...)) + (let ((tbl (make-hash-table))) + (hash-set! tbl (quote prop) val) ... + tbl)) ;; Array literal ((_ #(items ...)) @@ -45,6 +51,7 @@ ((_ (- args ...)) (- args ...)) ((_ (* args ...)) (* args ...)) ((_ (/ args ...)) (/ args ...)) + ((_ (set! var val)) (set! var val)) ;; Default - pass through unchanged ((_ expr) expr))) @@ -54,12 +61,14 @@ (syntax->datum (expand-james->scheme expr))) -;; Example usage: -#| -(james->scheme - '(defn (make-counter init) - (let value init) - (return - (%r (increment (lambda () (set! value (+ value 1)))) - (decrement (lambda () (set! value (- value 1)))))))) -|# +;; Test counter example +(define counter-test + '(defn (make-counter init) + (let value init) + (return + (%r (increment (fn () (set! value (+ value 1)))) + (decrement (fn () (set! value (- value 1)))))))) + +(display "Testing counter example:\n") +(write (james->scheme counter-test)) +(newline) From 5284358013b9f12e8381f9543f35f9227dcccc79 Mon Sep 17 00:00:00 2001 From: "Dan Connolly (aider)" Date: Fri, 10 Jan 2025 00:04:55 -0600 Subject: [PATCH 03/45] feat: Modify james-to-scheme.scm to evaluate and test counter macro expansion --- james-to-scheme.scm | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/james-to-scheme.scm b/james-to-scheme.scm index c89eb4c..6c6f0a1 100644 --- a/james-to-scheme.scm +++ b/james-to-scheme.scm @@ -70,5 +70,17 @@ (decrement (fn () (set! value (- value 1)))))))) (display "Testing counter example:\n") -(write (james->scheme counter-test)) -(newline) +(let ((counter (eval (expand-james->scheme counter-test) (current-module)))) + (display "Created counter with initial value 0\n") + (let ((c (counter 0))) + (display "Initial counter object: ") + (write c) + (newline) + (hash-ref c 'increment) + (display "After increment: ") + (write c) + (newline) + (hash-ref c 'decrement) + (display "After decrement: ") + (write c) + (newline))) From 58fc217927a734c10eecb3035b1f08c81aceeac7 Mon Sep 17 00:00:00 2001 From: "Dan Connolly (aider)" Date: Fri, 10 Jan 2025 00:05:36 -0600 Subject: [PATCH 04/45] fix: Correct let macro expansion in james-to-scheme.scm --- james-to-scheme.scm | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/james-to-scheme.scm b/james-to-scheme.scm index 6c6f0a1..425802b 100644 --- a/james-to-scheme.scm +++ b/james-to-scheme.scm @@ -26,7 +26,7 @@ ;; Let binding ((_ (let var val)) - (define var val)) + (let ((var val)) var)) ;; Return statement ((_ (return expr)) From 9c61493ded949f153a57689dc5cf3e445972c4c5 Mon Sep 17 00:00:00 2001 From: "Dan Connolly (aider)" Date: Fri, 10 Jan 2025 00:08:22 -0600 Subject: [PATCH 05/45] feat: Add += macro for incrementing variables in Scheme DSL --- dsl-1.scm | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) create mode 100644 dsl-1.scm diff --git a/dsl-1.scm b/dsl-1.scm new file mode 100644 index 0000000..7cae232 --- /dev/null +++ b/dsl-1.scm @@ -0,0 +1,16 @@ +;; DSL macros for James-like syntax + +(define-module (rockit dsl-1) + #:export (+=)) + +;; Macro to expand += into a set! with addition +(define-syntax += + (syntax-rules () + ((_ var val) + (set! var (+ var val))))) + +;; Test case +(let ((x 5)) + (display "x before: ") (display x) (newline) + (+= x 1) + (display "x after: ") (display x) (newline)) From 188e6d904cf70a3e15956f0c9dc7d9339ad71146 Mon Sep 17 00:00:00 2001 From: "Dan Connolly (aider)" Date: Fri, 10 Jan 2025 00:10:03 -0600 Subject: [PATCH 06/45] feat: Add fn macro to expand into lambda with begin --- dsl-1.scm | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) diff --git a/dsl-1.scm b/dsl-1.scm index 7cae232..85e23c2 100644 --- a/dsl-1.scm +++ b/dsl-1.scm @@ -1,7 +1,13 @@ ;; DSL macros for James-like syntax (define-module (rockit dsl-1) - #:export (+=)) + #:export (+= fn)) + +;; Macro to expand fn into lambda with begin +(define-syntax fn + (syntax-rules () + ((_ (args ...) body ...) + (lambda (args ...) (begin body ...))))) ;; Macro to expand += into a set! with addition (define-syntax += @@ -14,3 +20,14 @@ (display "x before: ") (display x) (newline) (+= x 1) (display "x after: ") (display x) (newline)) + +;; Test fn macro +(display "\nTesting fn macro:\n") +(let ((add2 (fn (x) + (display "Adding 2 to ") + (display x) + (newline) + (+ x 2)))) + (display "Result: ") + (display (add2 40)) + (newline)) From c620d5430e1c93f27d4f370cc6a7e0b8b4446e6a Mon Sep 17 00:00:00 2001 From: "Dan Connolly (aider)" Date: Fri, 10 Jan 2025 00:17:06 -0600 Subject: [PATCH 07/45] feat: Add %r macro for creating hash-table records with key-value pairs --- dsl-1.scm | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/dsl-1.scm b/dsl-1.scm index 85e23c2..1ec5404 100644 --- a/dsl-1.scm +++ b/dsl-1.scm @@ -1,7 +1,16 @@ ;; DSL macros for James-like syntax (define-module (rockit dsl-1) - #:export (+= fn)) + #:use-module (ice-9 hash-table) + #:export (+= fn %r)) + +;; 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)))) ;; Macro to expand fn into lambda with begin (define-syntax fn @@ -31,3 +40,9 @@ (display "Result: ") (display (add2 40)) (newline)) + +;; Test %r macro +(display "\nTesting %r macro:\n") +(let ((point (%r (: x 10) (: y 20)))) + (display "Point x: ") (display (hash-ref point 'x)) (newline) + (display "Point y: ") (display (hash-ref point 'y)) (newline)) From 67724a0f3af2943544e170967d8f2f90bf2bd9fc Mon Sep 17 00:00:00 2001 From: "Dan Connolly (aider)" Date: Fri, 10 Jan 2025 00:22:03 -0600 Subject: [PATCH 08/45] feat: Enhance fn macro to support let bindings with scoped expressions --- dsl-1.scm | 22 ++++++++++++++++++++-- 1 file changed, 20 insertions(+), 2 deletions(-) diff --git a/dsl-1.scm b/dsl-1.scm index 1ec5404..52f0fcc 100644 --- a/dsl-1.scm +++ b/dsl-1.scm @@ -12,9 +12,15 @@ (hash-set! tbl 'key value) ... tbl)))) -;; Macro to expand fn into lambda with begin +;; Macro to expand fn into lambda with begin, handling let bindings (define-syntax fn - (syntax-rules () + (syntax-rules (let) + ;; Match (fn (args ...) (let name val) rest ...) + ((_ (args ...) (let name val) rest ...) + (lambda (args ...) + (let ((name val)) + rest ...))) + ;; Default case without let ((_ (args ...) body ...) (lambda (args ...) (begin body ...))))) @@ -46,3 +52,15 @@ (let ((point (%r (: x 10) (: y 20)))) (display "Point x: ") (display (hash-ref point 'x)) (newline) (display "Point y: ") (display (hash-ref point 'y)) (newline)) + +;; Test fn macro with let binding +(display "\nTesting fn macro with let:\n") +(let ((make-adder (fn (x) + (let result (+ x 2)) + (display "Computed result: ") + (display result) + (newline) + result))) + (display "Result: ") + (display (make-adder 40)) + (newline)) From 1731b0448191bc79515c952ad6b096afb00b87a3 Mon Sep 17 00:00:00 2001 From: "Dan Connolly (aider)" Date: Fri, 10 Jan 2025 00:23:26 -0600 Subject: [PATCH 09/45] refactor: Enhance fn macro to handle let bindings anywhere in function body --- dsl-1.scm | 31 +++++++++++++++++++++++-------- 1 file changed, 23 insertions(+), 8 deletions(-) diff --git a/dsl-1.scm b/dsl-1.scm index 52f0fcc..e3b4079 100644 --- a/dsl-1.scm +++ b/dsl-1.scm @@ -12,17 +12,29 @@ (hash-set! tbl 'key value) ... tbl)))) +;; Helper macro to process fn body and handle let bindings +(define-syntax process-fn-body + (syntax-rules (let) + ;; When we see a let, wrap the rest in its scope + ((_ ((let name val) rest ...)) + (let ((name val)) + (process-fn-body (rest ...)))) + ;; No more expressions + ((_ ()) + (begin)) + ;; Regular expression followed by more + ((_ (first rest ...)) + (begin first (process-fn-body (rest ...)))) + ;; Single expression + ((_ (expr)) + (begin expr)))) + ;; Macro to expand fn into lambda with begin, handling let bindings (define-syntax fn - (syntax-rules (let) - ;; Match (fn (args ...) (let name val) rest ...) - ((_ (args ...) (let name val) rest ...) - (lambda (args ...) - (let ((name val)) - rest ...))) - ;; Default case without let + (syntax-rules () ((_ (args ...) body ...) - (lambda (args ...) (begin body ...))))) + (lambda (args ...) + (process-fn-body (body ...)))))) ;; Macro to expand += into a set! with addition (define-syntax += @@ -56,6 +68,9 @@ ;; Test fn macro with let binding (display "\nTesting fn macro with let:\n") (let ((make-adder (fn (x) + (display "Computing with input: ") + (display x) + (newline) (let result (+ x 2)) (display "Computed result: ") (display result) From 9557abfe69b85951a528b3623040e822137aa960 Mon Sep 17 00:00:00 2001 From: "Dan Connolly (aider)" Date: Fri, 10 Jan 2025 00:23:55 -0600 Subject: [PATCH 10/45] fix: Correct process-fn-body macro to handle single expression case --- dsl-1.scm | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/dsl-1.scm b/dsl-1.scm index e3b4079..c58bfaa 100644 --- a/dsl-1.scm +++ b/dsl-1.scm @@ -27,7 +27,7 @@ (begin first (process-fn-body (rest ...)))) ;; Single expression ((_ (expr)) - (begin expr)))) + expr))) ;; Macro to expand fn into lambda with begin, handling let bindings (define-syntax fn From 1008f36aa229f8ab96018cfb6de61f7f9a8d0d1b Mon Sep 17 00:00:00 2001 From: "Dan Connolly (aider)" Date: Fri, 10 Jan 2025 00:24:23 -0600 Subject: [PATCH 11/45] refactor: Simplify process-fn-body macro to handle multiple expressions --- dsl-1.scm | 12 +++--------- 1 file changed, 3 insertions(+), 9 deletions(-) diff --git a/dsl-1.scm b/dsl-1.scm index c58bfaa..bc0795c 100644 --- a/dsl-1.scm +++ b/dsl-1.scm @@ -19,15 +19,9 @@ ((_ ((let name val) rest ...)) (let ((name val)) (process-fn-body (rest ...)))) - ;; No more expressions - ((_ ()) - (begin)) - ;; Regular expression followed by more - ((_ (first rest ...)) - (begin first (process-fn-body (rest ...)))) - ;; Single expression - ((_ (expr)) - expr))) + ;; Regular expressions list + ((_ (expr ...)) + (begin expr ...)))) ;; Macro to expand fn into lambda with begin, handling let bindings (define-syntax fn From abf519cff49d84cc631b9f99a73783c9dd411710 Mon Sep 17 00:00:00 2001 From: "Dan Connolly (aider)" Date: Fri, 10 Jan 2025 00:25:30 -0600 Subject: [PATCH 12/45] refactor: Replace `let` with `var` in DSL macro implementation --- dsl-1.scm | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/dsl-1.scm b/dsl-1.scm index bc0795c..0bd7e29 100644 --- a/dsl-1.scm +++ b/dsl-1.scm @@ -12,11 +12,11 @@ (hash-set! tbl 'key value) ... tbl)))) -;; Helper macro to process fn body and handle let bindings +;; Helper macro to process fn body and handle var bindings (define-syntax process-fn-body - (syntax-rules (let) - ;; When we see a let, wrap the rest in its scope - ((_ ((let name val) rest ...)) + (syntax-rules (var) + ;; When we see a var, wrap the rest in its scope + ((_ ((var name val) rest ...)) (let ((name val)) (process-fn-body (rest ...)))) ;; Regular expressions list @@ -59,13 +59,13 @@ (display "Point x: ") (display (hash-ref point 'x)) (newline) (display "Point y: ") (display (hash-ref point 'y)) (newline)) -;; Test fn macro with let binding -(display "\nTesting fn macro with let:\n") +;; Test fn macro with var binding +(display "\nTesting fn macro with var:\n") (let ((make-adder (fn (x) (display "Computing with input: ") (display x) (newline) - (let result (+ x 2)) + (var result (+ x 2)) (display "Computed result: ") (display result) (newline) From 8f461f892ab40cff56c64f21ed6db63919533714 Mon Sep 17 00:00:00 2001 From: "Dan Connolly (aider)" Date: Fri, 10 Jan 2025 00:26:00 -0600 Subject: [PATCH 13/45] fix: Handle var binding in macro with standalone and multi-expression cases --- dsl-1.scm | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/dsl-1.scm b/dsl-1.scm index 0bd7e29..104c2ba 100644 --- a/dsl-1.scm +++ b/dsl-1.scm @@ -15,10 +15,14 @@ ;; Helper macro to process fn body and handle var bindings (define-syntax process-fn-body (syntax-rules (var) - ;; When we see a var, wrap the rest in its scope + ;; When we see a var binding followed by more expressions ((_ ((var name val) rest ...)) (let ((name val)) (process-fn-body (rest ...)))) + ;; When we see just a var binding alone + ((_ ((var name val))) + (let ((name val)) + name)) ;; Regular expressions list ((_ (expr ...)) (begin expr ...)))) From 5fff6819afd7d08e8d7a38e4bf3d3faabe45d1a2 Mon Sep 17 00:00:00 2001 From: "Dan Connolly (aider)" Date: Fri, 10 Jan 2025 00:28:47 -0600 Subject: [PATCH 14/45] feat: Add var keyword support to fn macro syntax-rules --- dsl-1.scm | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/dsl-1.scm b/dsl-1.scm index 104c2ba..2926830 100644 --- a/dsl-1.scm +++ b/dsl-1.scm @@ -29,7 +29,7 @@ ;; Macro to expand fn into lambda with begin, handling let bindings (define-syntax fn - (syntax-rules () + (syntax-rules (var) ((_ (args ...) body ...) (lambda (args ...) (process-fn-body (body ...)))))) From 949b34bd629223e2e07514bafc7a2f05e51eef2d Mon Sep 17 00:00:00 2001 From: "Dan Connolly (aider)" Date: Fri, 10 Jan 2025 00:33:02 -0600 Subject: [PATCH 15/45] feat: Add support for var bindings in process-fn-body macro --- dsl-1.scm | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/dsl-1.scm b/dsl-1.scm index 2926830..ab62ee7 100644 --- a/dsl-1.scm +++ b/dsl-1.scm @@ -23,7 +23,12 @@ ((_ ((var name val))) (let ((name val)) name)) - ;; Regular expressions list + ;; Regular expressions list - handle var bindings one at a time + ((_ ((var name val) expr ...)) + (let ((name val)) + (process-fn-body (expr ...)))) + + ;; Default case for regular expressions ((_ (expr ...)) (begin expr ...)))) From a41bd37642c5d301b1c4071a3757c91693d3f3d6 Mon Sep 17 00:00:00 2001 From: "Dan Connolly (aider)" Date: Fri, 10 Jan 2025 00:35:31 -0600 Subject: [PATCH 16/45] refactor: Simplify process-fn-body macro with recursive var binding handling --- dsl-1.scm | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/dsl-1.scm b/dsl-1.scm index ab62ee7..85ebc4e 100644 --- a/dsl-1.scm +++ b/dsl-1.scm @@ -23,14 +23,13 @@ ((_ ((var name val))) (let ((name val)) name)) - ;; Regular expressions list - handle var bindings one at a time - ((_ ((var name val) expr ...)) - (let ((name val)) - (process-fn-body (expr ...)))) - ;; Default case for regular expressions ((_ (expr ...)) - (begin expr ...)))) + (match (car expr) + [(var name val) + (let ((name val)) + (process-fn-body (cdr expr)))] + [_ (begin expr ...)])))) ;; Macro to expand fn into lambda with begin, handling let bindings (define-syntax fn From aee86dd22bb71c685978aa9c85c03a5062902420 Mon Sep 17 00:00:00 2001 From: "Dan Connolly (aider)" Date: Fri, 10 Jan 2025 00:37:22 -0600 Subject: [PATCH 17/45] refactor: Improve process-fn-body macro to handle multiple expressions recursively --- dsl-1.scm | 18 +++++++++++++----- 1 file changed, 13 insertions(+), 5 deletions(-) diff --git a/dsl-1.scm b/dsl-1.scm index 85ebc4e..096ffc6 100644 --- a/dsl-1.scm +++ b/dsl-1.scm @@ -25,11 +25,19 @@ name)) ;; Default case for regular expressions ((_ (expr ...)) - (match (car expr) - [(var name val) - (let ((name val)) - (process-fn-body (cdr expr)))] - [_ (begin expr ...)])))) + (if (null? expr) + (if #f #f) ; return void + (match (car expr) + [(var name val) + (let ((name val)) + (process-fn-body (cdr expr)))] + [first + (let ((rest (cdr expr))) + (if (null? rest) + first + (begin + first + (process-fn-body rest))))]))))) ;; Macro to expand fn into lambda with begin, handling let bindings (define-syntax fn From 9098a473844008c928e7b8444b1d7fa2eb88c419 Mon Sep 17 00:00:00 2001 From: Dan Connolly Date: Fri, 10 Jan 2025 00:39:03 -0600 Subject: [PATCH 18/45] refactor: Simplify and clean up macro expansion logic in dsl-1.scm --- dsl-1.scm | 16 +++------------- 1 file changed, 3 insertions(+), 13 deletions(-) diff --git a/dsl-1.scm b/dsl-1.scm index 096ffc6..81d76d7 100644 --- a/dsl-1.scm +++ b/dsl-1.scm @@ -25,19 +25,9 @@ name)) ;; Default case for regular expressions ((_ (expr ...)) - (if (null? expr) - (if #f #f) ; return void - (match (car expr) - [(var name val) - (let ((name val)) - (process-fn-body (cdr expr)))] - [first - (let ((rest (cdr expr))) - (if (null? rest) - first - (begin - first - (process-fn-body rest))))]))))) + (begin + exprt + (process-fn-body rest))))) ;; Macro to expand fn into lambda with begin, handling let bindings (define-syntax fn From 563b23bc98dbe3d3afe8a4b78375967fffdad360 Mon Sep 17 00:00:00 2001 From: Dan Connolly Date: Fri, 10 Jan 2025 00:40:02 -0600 Subject: [PATCH 19/45] refactor: Fix macro expansion for process-fn-body with rest parameter --- dsl-1.scm | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/dsl-1.scm b/dsl-1.scm index 81d76d7..1625fed 100644 --- a/dsl-1.scm +++ b/dsl-1.scm @@ -27,7 +27,7 @@ ((_ (expr ...)) (begin exprt - (process-fn-body rest))))) + (process-fn-body (rest ...)))))) ;; Macro to expand fn into lambda with begin, handling let bindings (define-syntax fn From 88f3b9285831a5658043f0826984a773a38ae90b Mon Sep 17 00:00:00 2001 From: Dan Connolly Date: Fri, 10 Jan 2025 00:46:11 -0600 Subject: [PATCH 20/45] refactor: Improve macro expansion for function body processing in DSL --- dsl-1.scm | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/dsl-1.scm b/dsl-1.scm index 1625fed..2c2d5c4 100644 --- a/dsl-1.scm +++ b/dsl-1.scm @@ -23,11 +23,14 @@ ((_ ((var name val))) (let ((name val)) name)) - ;; Default case for regular expressions - ((_ (expr ...)) + ;; just an expression + ((_ ((expr)) + expr)) + ;; expression with more after it + ((_ ((expr rest ...)) (begin - exprt - (process-fn-body (rest ...)))))) + expr + (process-fn-body (rest ...))))))) ;; Macro to expand fn into lambda with begin, handling let bindings (define-syntax fn From bf5e928c35f25c58a4edafc2449575aaec92a491 Mon Sep 17 00:00:00 2001 From: "Dan Connolly (aider)" Date: Fri, 10 Jan 2025 00:46:24 -0600 Subject: [PATCH 21/45] refactor: Improve process-fn-body macro pattern matching and handling --- dsl-1.scm | 19 +++++++++++-------- 1 file changed, 11 insertions(+), 8 deletions(-) diff --git a/dsl-1.scm b/dsl-1.scm index 2c2d5c4..b59faa1 100644 --- a/dsl-1.scm +++ b/dsl-1.scm @@ -18,19 +18,22 @@ ;; When we see a var binding followed by more expressions ((_ ((var name val) rest ...)) (let ((name val)) - (process-fn-body (rest ...)))) + (process-fn-body rest))) ;; When we see just a var binding alone ((_ ((var name val))) (let ((name val)) name)) - ;; just an expression - ((_ ((expr)) - expr)) - ;; expression with more after it - ((_ ((expr rest ...)) + ;; Empty case + ((_ ()) + (if #f #f)) + ;; Single expression case + ((_ (expr)) + expr) + ;; Multiple expressions case + ((_ (first rest ...)) (begin - expr - (process-fn-body (rest ...))))))) + first + (process-fn-body rest))))) ;; Macro to expand fn into lambda with begin, handling let bindings (define-syntax fn From 0a643105366559fc2877b2b8015e3fcabbcb404e Mon Sep 17 00:00:00 2001 From: Dan Connolly Date: Fri, 10 Jan 2025 00:53:13 -0600 Subject: [PATCH 22/45] refactor: Fix macro expansion for process-fn-body with correct syntax --- dsl-1.scm | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/dsl-1.scm b/dsl-1.scm index b59faa1..0c44b05 100644 --- a/dsl-1.scm +++ b/dsl-1.scm @@ -18,7 +18,7 @@ ;; When we see a var binding followed by more expressions ((_ ((var name val) rest ...)) (let ((name val)) - (process-fn-body rest))) + (process-fn-body (rest ...)))) ;; When we see just a var binding alone ((_ ((var name val))) (let ((name val)) @@ -33,7 +33,7 @@ ((_ (first rest ...)) (begin first - (process-fn-body rest))))) + (process-fn-body (rest ...)))))) ;; Macro to expand fn into lambda with begin, handling let bindings (define-syntax fn From f7286235399c5d2d234555ec2589b2ad342884b4 Mon Sep 17 00:00:00 2001 From: "Dan Connolly (aider)" Date: Fri, 10 Jan 2025 00:53:14 -0600 Subject: [PATCH 23/45] feat: Add $ macro for method call syntax in DSL --- dsl-1.scm | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/dsl-1.scm b/dsl-1.scm index 0c44b05..3c03b92 100644 --- a/dsl-1.scm +++ b/dsl-1.scm @@ -2,7 +2,13 @@ (define-module (rockit dsl-1) #:use-module (ice-9 hash-table) - #:export (+= fn %r)) + #:export (+= fn %r $)) + +;; Macro for method calls using ($ target method args ...) +(define-syntax $ + (syntax-rules () + ((_ target method args ...) + (target 'method args ...)))) ;; Macro to expand %r into hash-table creation (define-syntax %r @@ -68,8 +74,8 @@ ;; Test %r macro (display "\nTesting %r macro:\n") (let ((point (%r (: x 10) (: y 20)))) - (display "Point x: ") (display (hash-ref point 'x)) (newline) - (display "Point y: ") (display (hash-ref point 'y)) (newline)) + (display "Point x: ") (display ($ point x)) (newline) + (display "Point y: ") (display ($ point y)) (newline)) ;; Test fn macro with var binding (display "\nTesting fn macro with var:\n") From 6981a31d1ca051553bf57f51eadfae4ebebce6f8 Mon Sep 17 00:00:00 2001 From: "Dan Connolly (aider)" Date: Fri, 10 Jan 2025 00:55:11 -0600 Subject: [PATCH 24/45] refactor: Update %r macro test to use methods instead of fields --- dsl-1.scm | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/dsl-1.scm b/dsl-1.scm index 3c03b92..d390a02 100644 --- a/dsl-1.scm +++ b/dsl-1.scm @@ -73,9 +73,11 @@ ;; Test %r macro (display "\nTesting %r macro:\n") -(let ((point (%r (: x 10) (: y 20)))) - (display "Point x: ") (display ($ point x)) (newline) - (display "Point y: ") (display ($ point y)) (newline)) +(let ((point (%r + (: get-x (fn () 10)) + (: get-y (fn () 20))))) + (display "Point x: ") (display ($ point get-x)) (newline) + (display "Point y: ") (display ($ point get-y)) (newline)) ;; Test fn macro with var binding (display "\nTesting fn macro with var:\n") From d785a5df879ca1053cab28def1cba714f3533ced Mon Sep 17 00:00:00 2001 From: "Dan Connolly (aider)" Date: Fri, 10 Jan 2025 00:56:25 -0600 Subject: [PATCH 25/45] refactor: Update `$` macro to use hash-ref for method retrieval --- dsl-1.scm | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/dsl-1.scm b/dsl-1.scm index d390a02..705a093 100644 --- a/dsl-1.scm +++ b/dsl-1.scm @@ -8,7 +8,7 @@ (define-syntax $ (syntax-rules () ((_ target method args ...) - (target 'method args ...)))) + ((hash-ref target 'method) args ...)))) ;; Macro to expand %r into hash-table creation (define-syntax %r From 389ed007d636c67cf531bc4309b0c52461129f31 Mon Sep 17 00:00:00 2001 From: "Dan Connolly (aider)" Date: Fri, 10 Jan 2025 00:57:22 -0600 Subject: [PATCH 26/45] feat: Add test case for %r macro with simple number properties --- dsl-1.scm | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/dsl-1.scm b/dsl-1.scm index 705a093..93dde89 100644 --- a/dsl-1.scm +++ b/dsl-1.scm @@ -71,14 +71,22 @@ (display (add2 40)) (newline)) -;; Test %r macro -(display "\nTesting %r macro:\n") +;; Test %r macro with methods +(display "\nTesting %r macro with methods:\n") (let ((point (%r (: get-x (fn () 10)) (: get-y (fn () 20))))) (display "Point x: ") (display ($ point get-x)) (newline) (display "Point y: ") (display ($ point get-y)) (newline)) +;; Test %r macro with simple properties +(display "\nTesting %r macro with properties:\n") +(let ((point (%r + (: x 10) + (: y 20)))) + (display "Point x: ") (display (hash-ref point 'x)) (newline) + (display "Point y: ") (display (hash-ref point 'y)) (newline)) + ;; Test fn macro with var binding (display "\nTesting fn macro with var:\n") (let ((make-adder (fn (x) From 818cc1f0a7f0d39b0309b92302db9488fe962999 Mon Sep 17 00:00:00 2001 From: "Dan Connolly (aider)" Date: Fri, 10 Jan 2025 00:59:08 -0600 Subject: [PATCH 27/45] feat: Add @ macro for simplified hash-ref field access --- dsl-1.scm | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/dsl-1.scm b/dsl-1.scm index 93dde89..dc80fdc 100644 --- a/dsl-1.scm +++ b/dsl-1.scm @@ -2,7 +2,13 @@ (define-module (rockit dsl-1) #:use-module (ice-9 hash-table) - #:export (+= fn %r $)) + #:export (+= fn %r $ @)) + +;; 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 $ @@ -84,8 +90,8 @@ (let ((point (%r (: x 10) (: y 20)))) - (display "Point x: ") (display (hash-ref point 'x)) (newline) - (display "Point y: ") (display (hash-ref point 'y)) (newline)) + (display "Point x: ") (display (@ point x)) (newline) + (display "Point y: ") (display (@ point y)) (newline)) ;; Test fn macro with var binding (display "\nTesting fn macro with var:\n") From 071f7bd349951b8c836968fcf1b22cde925a6f85 Mon Sep 17 00:00:00 2001 From: "Dan Connolly (aider)" Date: Fri, 10 Jan 2025 01:00:54 -0600 Subject: [PATCH 28/45] feat: Add `-=` macro for in-place subtraction --- dsl-1.scm | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/dsl-1.scm b/dsl-1.scm index dc80fdc..7bc19fc 100644 --- a/dsl-1.scm +++ b/dsl-1.scm @@ -60,6 +60,12 @@ ((_ var val) (set! var (+ var val))))) +;; Macro to expand -= into a set! with subtraction +(define-syntax -= + (syntax-rules () + ((_ var val) + (set! var (- var val))))) + ;; Test case (let ((x 5)) (display "x before: ") (display x) (newline) From 6a265be90ce73af2497bcf3934df9cba8b9cf1e4 Mon Sep 17 00:00:00 2001 From: "Dan Connolly (aider)" Date: Fri, 10 Jan 2025 01:03:49 -0600 Subject: [PATCH 29/45] feat: Add module macro with defconst support for body processing --- dsl-1.scm | 29 ++++++++++++++++++++++------- 1 file changed, 22 insertions(+), 7 deletions(-) diff --git a/dsl-1.scm b/dsl-1.scm index 7bc19fc..6476edb 100644 --- a/dsl-1.scm +++ b/dsl-1.scm @@ -2,7 +2,7 @@ (define-module (rockit dsl-1) #:use-module (ice-9 hash-table) - #:export (+= fn %r $ @)) + #:export (+= fn %r $ @ module)) ;; Macro for field access using (@ obj field) (define-syntax @ @@ -24,17 +24,25 @@ (hash-set! tbl 'key value) ... tbl)))) -;; Helper macro to process fn body and handle var bindings -(define-syntax process-fn-body - (syntax-rules (var) +;; Helper macro to process body and handle bindings +(define-syntax process-body + (syntax-rules (var defconst) ;; When we see a var binding followed by more expressions ((_ ((var name val) rest ...)) (let ((name val)) - (process-fn-body (rest ...)))) + (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)) ;; Empty case ((_ ()) (if #f #f)) @@ -45,14 +53,21 @@ ((_ (first rest ...)) (begin first - (process-fn-body (rest ...)))))) + (process-body (rest ...)))))) ;; Macro to expand fn into lambda with begin, handling let bindings (define-syntax fn (syntax-rules (var) ((_ (args ...) body ...) (lambda (args ...) - (process-fn-body (body ...)))))) + (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 += From c0c457081d56c21b527b594a1b5b16bc3034367f Mon Sep 17 00:00:00 2001 From: "Dan Connolly (aider)" Date: Fri, 10 Jan 2025 01:05:26 -0600 Subject: [PATCH 30/45] feat: Add test for module macro with circle area computation --- dsl-1.scm | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/dsl-1.scm b/dsl-1.scm index 6476edb..3f927e8 100644 --- a/dsl-1.scm +++ b/dsl-1.scm @@ -128,3 +128,17 @@ (display "Result: ") (display (make-adder 40)) (newline)) + +;; Test module macro +(display "\nTesting module macro:\n") +(let ((result (module + (defconst pi 3.14159) + (defconst radius 2) + (defconst area (* pi (* radius radius))) + (display "Computing circle area with radius: ") + (display radius) + (newline) + area))) + (display "Circle area: ") + (display result) + (newline)) From 892fbd40004b6a3f31e981945c396771af9299f2 Mon Sep 17 00:00:00 2001 From: "Dan Connolly (aider)" Date: Fri, 10 Jan 2025 01:07:52 -0600 Subject: [PATCH 31/45] feat: Add counter object test with makeOffsetCounter method to dsl-1.scm --- dsl-1.scm | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/dsl-1.scm b/dsl-1.scm index 3f927e8..baddcee 100644 --- a/dsl-1.scm +++ b/dsl-1.scm @@ -142,3 +142,26 @@ (display "Circle area: ") (display result) (newline)) + +;; Test counter object with makeOffsetCounter +(display "\nTesting counter with offset:\n") +(let* ((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))))))))) + (c1 (make-counter 5))) + (display "Initial counter at 5\n") + ($ c1 increment) + (display "After increment: ") + (display ($ c1 decrement)) + (newline) + (let ((c2 ($ c1 makeOffsetCounter 10))) + (display "New counter with +10 offset\n") + ($ c2 increment) + (display "After increment: ") + (display ($ c2 decrement)) + (newline))) From 01249a4107592d9843ff7f508785e3d28c8c2ade Mon Sep 17 00:00:00 2001 From: "Dan Connolly (aider)" Date: Fri, 10 Jan 2025 01:09:09 -0600 Subject: [PATCH 32/45] feat: Export 'return' identifier in dsl-1 module to resolve unbound variable error --- dsl-1.scm | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/dsl-1.scm b/dsl-1.scm index baddcee..26895f7 100644 --- a/dsl-1.scm +++ b/dsl-1.scm @@ -2,7 +2,7 @@ (define-module (rockit dsl-1) #:use-module (ice-9 hash-table) - #:export (+= fn %r $ @ module)) + #:export (+= fn %r $ @ module return)) ;; Macro for field access using (@ obj field) (define-syntax @ From d463b26ca71e828eb45ec71d01858ef4a07c6323 Mon Sep 17 00:00:00 2001 From: "Dan Connolly (aider)" Date: Fri, 10 Jan 2025 01:10:04 -0600 Subject: [PATCH 33/45] refactor: Modify process-body macro to handle return statements correctly --- dsl-1.scm | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/dsl-1.scm b/dsl-1.scm index 26895f7..77c1fb8 100644 --- a/dsl-1.scm +++ b/dsl-1.scm @@ -26,7 +26,7 @@ ;; Helper macro to process body and handle bindings (define-syntax process-body - (syntax-rules (var defconst) + (syntax-rules (var defconst return) ;; When we see a var binding followed by more expressions ((_ ((var name val) rest ...)) (let ((name val)) @@ -43,6 +43,11 @@ ((_ ((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)) From 463baacd5944d6332ef684003bb637e8f753366c Mon Sep 17 00:00:00 2001 From: "Dan Connolly (aider)" Date: Fri, 10 Jan 2025 01:11:40 -0600 Subject: [PATCH 34/45] refactor: Replace let* with module for counter test implementation --- dsl-1.scm | 42 ++++++++++++++++++++++-------------------- 1 file changed, 22 insertions(+), 20 deletions(-) diff --git a/dsl-1.scm b/dsl-1.scm index 77c1fb8..94d6d6d 100644 --- a/dsl-1.scm +++ b/dsl-1.scm @@ -150,23 +150,25 @@ ;; Test counter object with makeOffsetCounter (display "\nTesting counter with offset:\n") -(let* ((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))))))))) - (c1 (make-counter 5))) - (display "Initial counter at 5\n") - ($ c1 increment) - (display "After increment: ") - (display ($ c1 decrement)) - (newline) - (let ((c2 ($ c1 makeOffsetCounter 10))) - (display "New counter with +10 offset\n") - ($ c2 increment) - (display "After increment: ") - (display ($ c2 decrement)) - (newline))) +(module + (defconst 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))))))))) + (defconst c1 (make-counter 5)) + (display "Initial counter at 5\n") + ($ c1 increment) + (display "After increment: ") + (display ($ c1 decrement)) + (newline) + (let ((c2 ($ c1 makeOffsetCounter 10))) + (display "New counter with +10 offset\n") + ($ c2 increment) + (display "After increment: ") + (display ($ c2 decrement)) + (newline))) From b62fb5a8fcc1e373157497ed3369034ff0fe569f Mon Sep 17 00:00:00 2001 From: "Dan Connolly (aider)" Date: Fri, 10 Jan 2025 01:18:03 -0600 Subject: [PATCH 35/45] refactor: Replace let with defconst in counter test --- dsl-1.scm | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/dsl-1.scm b/dsl-1.scm index 94d6d6d..a027ed1 100644 --- a/dsl-1.scm +++ b/dsl-1.scm @@ -166,9 +166,9 @@ (display "After increment: ") (display ($ c1 decrement)) (newline) - (let ((c2 ($ c1 makeOffsetCounter 10))) - (display "New counter with +10 offset\n") - ($ c2 increment) - (display "After increment: ") - (display ($ c2 decrement)) - (newline))) + (defconst c2 ($ c1 makeOffsetCounter 10)) + (display "New counter with +10 offset\n") + ($ c2 increment) + (display "After increment: ") + (display ($ c2 decrement)) + (newline)) From 7daa2b802e279a29059b9d4ee4a8d1bcac8dd839 Mon Sep 17 00:00:00 2001 From: "Dan Connolly (aider)" Date: Fri, 10 Jan 2025 01:31:10 -0600 Subject: [PATCH 36/45] feat: Add recursive doubler test case to dsl-1.scm --- dsl-1.scm | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/dsl-1.scm b/dsl-1.scm index a027ed1..0e786ba 100644 --- a/dsl-1.scm +++ b/dsl-1.scm @@ -148,6 +148,23 @@ (display result) (newline)) +;; Test simple recursive doubler +(display "\nTesting simple recursive doubler:\n") +(module + (defconst make-doubler + (fn (init) + (var value init) + (return (%r + (: double (fn () + (display "Doubling: ") + (display value) + (newline) + (if (< value 4) + (($ (make-doubler (* value 2)) double)) + value))))))) + (defconst d1 (make-doubler 1)) + ($ d1 double)) + ;; Test counter object with makeOffsetCounter (display "\nTesting counter with offset:\n") (module From 0f6179ba25aaa84d751dcac6640bdbf8017b98f0 Mon Sep 17 00:00:00 2001 From: "Dan Connolly (aider)" Date: Fri, 10 Jan 2025 01:31:40 -0600 Subject: [PATCH 37/45] refactor: Replace module with let in doubler test implementation --- dsl-1.scm | 28 ++++++++++++++-------------- 1 file changed, 14 insertions(+), 14 deletions(-) diff --git a/dsl-1.scm b/dsl-1.scm index 0e786ba..83bc162 100644 --- a/dsl-1.scm +++ b/dsl-1.scm @@ -150,20 +150,20 @@ ;; Test simple recursive doubler (display "\nTesting simple recursive doubler:\n") -(module - (defconst make-doubler - (fn (init) - (var value init) - (return (%r - (: double (fn () - (display "Doubling: ") - (display value) - (newline) - (if (< value 4) - (($ (make-doubler (* value 2)) double)) - value))))))) - (defconst d1 (make-doubler 1)) - ($ d1 double)) +(let () + (define make-doubler + (fn (init) + (var value init) + (return (%r + (: double (fn () + (display "Doubling: ") + (display value) + (newline) + (if (< value 4) + (($ (make-doubler (* value 2)) double)) + value))))))) + (define d1 (make-doubler 1)) + ($ d1 double)) ;; Test counter object with makeOffsetCounter (display "\nTesting counter with offset:\n") From 85bd63e177f4b320f0035b4ff2eb5c36634a304b Mon Sep 17 00:00:00 2001 From: "Dan Connolly (aider)" Date: Fri, 10 Jan 2025 01:32:52 -0600 Subject: [PATCH 38/45] fix: Correct recursive doubler macro to properly return value --- dsl-1.scm | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/dsl-1.scm b/dsl-1.scm index 83bc162..2f0b9af 100644 --- a/dsl-1.scm +++ b/dsl-1.scm @@ -160,7 +160,7 @@ (display value) (newline) (if (< value 4) - (($ (make-doubler (* value 2)) double)) + ($ (make-doubler (* value 2)) double) value))))))) (define d1 (make-doubler 1)) ($ d1 double)) From c78c69a09ddcb797e5bf0a4f022a1ca8a212e724 Mon Sep 17 00:00:00 2001 From: "Dan Connolly (aider)" Date: Fri, 10 Jan 2025 01:36:09 -0600 Subject: [PATCH 39/45] refactor: Replace display calls with console.log method --- dsl-1.scm | 107 ++++++++++++++++++++++++------------------------------ 1 file changed, 48 insertions(+), 59 deletions(-) diff --git a/dsl-1.scm b/dsl-1.scm index 2f0b9af..3062b9d 100644 --- a/dsl-1.scm +++ b/dsl-1.scm @@ -86,79 +86,72 @@ ((_ var val) (set! var (- var val))))) +;; Define console object with log method +(define console + (%r (: log (fn args + (for-each (lambda (arg) + (display arg) + (display " ")) + args) + (newline))))) + ;; Test case (let ((x 5)) - (display "x before: ") (display x) (newline) + ($ console log "x before:" x) (+= x 1) - (display "x after: ") (display x) (newline)) + ($ console log "x after:" x)) ;; Test fn macro -(display "\nTesting fn macro:\n") +($ console log "\nTesting fn macro:") (let ((add2 (fn (x) - (display "Adding 2 to ") - (display x) - (newline) + ($ console log "Adding 2 to" x) (+ x 2)))) - (display "Result: ") - (display (add2 40)) - (newline)) + ($ console log "Result:" (add2 40))) ;; Test %r macro with methods -(display "\nTesting %r macro with methods:\n") +($ console log "\nTesting %r macro with methods:") (let ((point (%r (: get-x (fn () 10)) (: get-y (fn () 20))))) - (display "Point x: ") (display ($ point get-x)) (newline) - (display "Point y: ") (display ($ point get-y)) (newline)) + ($ console log "Point x:" ($ point get-x)) + ($ console log "Point y:" ($ point get-y))) ;; Test %r macro with simple properties -(display "\nTesting %r macro with properties:\n") +($ console log "\nTesting %r macro with properties:") (let ((point (%r (: x 10) (: y 20)))) - (display "Point x: ") (display (@ point x)) (newline) - (display "Point y: ") (display (@ point y)) (newline)) + ($ console log "Point x:" (@ point x)) + ($ console log "Point y:" (@ point y))) ;; Test fn macro with var binding -(display "\nTesting fn macro with var:\n") +($ console log "\nTesting fn macro with var:") (let ((make-adder (fn (x) - (display "Computing with input: ") - (display x) - (newline) + ($ console log "Computing with input:" x) (var result (+ x 2)) - (display "Computed result: ") - (display result) - (newline) + ($ console log "Computed result:" result) result))) - (display "Result: ") - (display (make-adder 40)) - (newline)) + ($ console log "Result:" (make-adder 40))) ;; Test module macro -(display "\nTesting module macro:\n") +($ console log "\nTesting module macro:") (let ((result (module (defconst pi 3.14159) (defconst radius 2) (defconst area (* pi (* radius radius))) - (display "Computing circle area with radius: ") - (display radius) - (newline) + ($ console log "Computing circle area with radius:" radius) area))) - (display "Circle area: ") - (display result) - (newline)) + ($ console log "Circle area:" result)) ;; Test simple recursive doubler -(display "\nTesting simple recursive doubler:\n") +($ console log "\nTesting simple recursive doubler:") (let () (define make-doubler (fn (init) (var value init) (return (%r (: double (fn () - (display "Doubling: ") - (display value) - (newline) + ($ console log "Doubling:" value) (if (< value 4) ($ (make-doubler (* value 2)) double) value))))))) @@ -166,26 +159,22 @@ ($ d1 double)) ;; Test counter object with makeOffsetCounter -(display "\nTesting counter with offset:\n") -(module - (defconst 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))))))))) - (defconst c1 (make-counter 5)) - (display "Initial counter at 5\n") - ($ c1 increment) - (display "After increment: ") - (display ($ c1 decrement)) - (newline) - (defconst c2 ($ c1 makeOffsetCounter 10)) - (display "New counter with +10 offset\n") - ($ c2 increment) - (display "After increment: ") - (display ($ c2 decrement)) - (newline)) +($ 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))) From 57f2f970759fd6e90b4d3dbcd16d8494771e043d Mon Sep 17 00:00:00 2001 From: "Dan Connolly (aider)" Date: Fri, 10 Jan 2025 01:38:44 -0600 Subject: [PATCH 40/45] feat: Add #:rest support to fn macro for variadic arguments --- dsl-1.scm | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) diff --git a/dsl-1.scm b/dsl-1.scm index 3062b9d..b7f081d 100644 --- a/dsl-1.scm +++ b/dsl-1.scm @@ -60,9 +60,12 @@ first (process-body (rest ...)))))) -;; Macro to expand fn into lambda with begin, handling let bindings +;; Macro to expand fn into lambda with begin, handling let bindings and rest args (define-syntax fn - (syntax-rules (var) + (syntax-rules (var #:rest) + ((_ (args ... #:rest rest-arg) body ...) + (lambda (args ... . rest-arg) + (process-body (body ...)))) ((_ (args ...) body ...) (lambda (args ...) (process-body (body ...)))))) @@ -88,11 +91,12 @@ ;; Define console object with log method (define console - (%r (: log (fn args + (%r (: log (fn (first #:rest rest) + (display first) (for-each (lambda (arg) - (display arg) - (display " ")) - args) + (display " ") + (display arg)) + rest) (newline))))) ;; Test case From 0da8ace07a17b861df3b0c6b12fa445410c45184 Mon Sep 17 00:00:00 2001 From: Dan Connolly Date: Fri, 10 Jan 2025 01:44:19 -0600 Subject: [PATCH 41/45] feat: Add SRFI-88 keyword objects module import to dsl-1.scm --- dsl-1.scm | 1 + 1 file changed, 1 insertion(+) diff --git a/dsl-1.scm b/dsl-1.scm index b7f081d..98939e6 100644 --- a/dsl-1.scm +++ b/dsl-1.scm @@ -2,6 +2,7 @@ (define-module (rockit dsl-1) #: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) From 0edaaaedd93e249189c2c3ab5ae66292f391949c Mon Sep 17 00:00:00 2001 From: "Dan Connolly (aider)" Date: Fri, 10 Jan 2025 01:44:20 -0600 Subject: [PATCH 42/45] refactor: Replace #:rest with rest: in fn macro and console.log implementation --- dsl-1.scm | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/dsl-1.scm b/dsl-1.scm index 98939e6..c73e612 100644 --- a/dsl-1.scm +++ b/dsl-1.scm @@ -63,8 +63,8 @@ ;; 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 ...) + (syntax-rules (var rest:) + ((_ (args ... rest: rest-arg) body ...) (lambda (args ... . rest-arg) (process-body (body ...)))) ((_ (args ...) body ...) @@ -92,7 +92,7 @@ ;; Define console object with log method (define console - (%r (: log (fn (first #:rest rest) + (%r (: log (fn (first rest: rest) (display first) (for-each (lambda (arg) (display " ") From 9d90e39f8805cf24b81b982fd1236360185d3122 Mon Sep 17 00:00:00 2001 From: Dan Connolly Date: Fri, 10 Jan 2025 01:47:54 -0600 Subject: [PATCH 43/45] refactor: Remove commented-out SRFI-88 module import --- dsl-1.scm | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/dsl-1.scm b/dsl-1.scm index c73e612..9c419b0 100644 --- a/dsl-1.scm +++ b/dsl-1.scm @@ -2,7 +2,7 @@ (define-module (rockit dsl-1) #:use-module (ice-9 hash-table) - #:use-module (srfi srfi-88) ;; keyword objects + ;; #:use-module (srfi srfi-88) ;; keyword objects #:export (+= fn %r $ @ module return)) ;; Macro for field access using (@ obj field) From 21f16088504a115cf4250bbd4c2d34bdafa1e2c8 Mon Sep 17 00:00:00 2001 From: "Dan Connolly (aider)" Date: Fri, 10 Jan 2025 01:47:55 -0600 Subject: [PATCH 44/45] fix: Modify += and -= macros to return new value after operation --- dsl-1.scm | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/dsl-1.scm b/dsl-1.scm index 9c419b0..c13b462 100644 --- a/dsl-1.scm +++ b/dsl-1.scm @@ -82,13 +82,17 @@ (define-syntax += (syntax-rules () ((_ var val) - (set! var (+ 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) - (set! var (- var val))))) + (let ((new-val (- var val))) + (set! var new-val) + new-val)))) ;; Define console object with log method (define console From c3eb6ac9fa08def4f81d259c89db73013f3970b1 Mon Sep 17 00:00:00 2001 From: Dan Connolly Date: Fri, 10 Jan 2025 01:56:43 -0600 Subject: [PATCH 45/45] replace james-to-scheme with dsl-1 --- counter.scm | 13 +++ dsl-1.scm | 189 -------------------------------- james-to-scheme.scm | 261 ++++++++++++++++++++++++++++++-------------- 3 files changed, 195 insertions(+), 268 deletions(-) create mode 100644 counter.scm delete mode 100644 dsl-1.scm diff --git a/counter.scm b/counter.scm new file mode 100644 index 0000000..ee32923 --- /dev/null +++ b/counter.scm @@ -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))) diff --git a/dsl-1.scm b/dsl-1.scm deleted file mode 100644 index c13b462..0000000 --- a/dsl-1.scm +++ /dev/null @@ -1,189 +0,0 @@ -;; DSL macros for James-like syntax - -(define-module (rockit dsl-1) - #: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))) diff --git a/james-to-scheme.scm b/james-to-scheme.scm index 425802b..e0fc957 100644 --- a/james-to-scheme.scm +++ b/james-to-scheme.scm @@ -1,86 +1,189 @@ -;; JAMES: Scheme macro-based implementation +;; DSL macros for James-like syntax -(define-module (rockit james scheme) - #:use-module (ice-9 match) - #:use-module (ice-9 regex) - #:export (expand-james->scheme james->scheme)) +(define-module (rockit james scheme-skin) + #:use-module (ice-9 hash-table) + ;; #:use-module (srfi srfi-88) ;; keyword objects + #:export (+= fn %r $ @ module return)) -;; Special forms that need custom handling -(define james-special-forms - '(fn defn begin def let module - if cond while for switch - try throw return break continue)) +;; Macro for field access using (@ obj field) +(define-syntax @ + (syntax-rules () + ((_ obj field) + (hash-ref obj 'field)))) -;; Convert James expressions to Scheme -(define-syntax expand-james->scheme +;; Macro for method calls using ($ target method args ...) +(define-syntax $ (syntax-rules () - ;; Function definition - ((_ (defn (name params ...) body ...)) - (define (name params ...) - (let () body ...))) - - ;; Lambda expression - ((_ (fn (params ...) body ...)) - (lambda (params ...) - (let () body ...))) - - ;; Let binding - ((_ (let var val)) - (let ((var val)) var)) - - ;; Return statement - ((_ (return expr)) + ((_ 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 ...)))))) - ;; Method call (.-> obj (method args)) - ((_ (.-> obj methods ...)) - ((compose methods ...) obj)) +;; 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 ...)))))) - ;; Object literal with property definitions - ((_ (%r (prop val) ...)) - (let ((tbl (make-hash-table))) - (hash-set! tbl (quote prop) val) ... - tbl)) - - ;; Array literal - ((_ #(items ...)) - (vector items ...)) - - ;; Basic operators map directly - ((_ (+ args ...)) (+ args ...)) - ((_ (- args ...)) (- args ...)) - ((_ (* args ...)) (* args ...)) - ((_ (/ args ...)) (/ args ...)) - ((_ (set! var val)) (set! var val)) - - ;; Default - pass through unchanged - ((_ expr) expr))) - -;; Helper to convert a James expression to Scheme -(define (james->scheme expr) - (syntax->datum - (expand-james->scheme expr))) - -;; Test counter example -(define counter-test - '(defn (make-counter init) - (let value init) - (return - (%r (increment (fn () (set! value (+ value 1)))) - (decrement (fn () (set! value (- value 1)))))))) - -(display "Testing counter example:\n") -(let ((counter (eval (expand-james->scheme counter-test) (current-module)))) - (display "Created counter with initial value 0\n") - (let ((c (counter 0))) - (display "Initial counter object: ") - (write c) - (newline) - (hash-ref c 'increment) - (display "After increment: ") - (write c) - (newline) - (hash-ref c 'decrement) - (display "After decrement: ") - (write c) - (newline))) +;; 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)))