From 55dd8edea874056784ade62830e836d9f216f2ee Mon Sep 17 00:00:00 2001 From: engboris Date: Thu, 9 Oct 2025 00:25:39 +0200 Subject: [PATCH 1/3] Use macros in Turing machine example and add attempts in watcher --- bin/sgen.ml | 12 +++---- examples/stack.sg | 17 +++++---- examples/turing.sg | 86 ++++++++++++++++++++++++++++------------------ 3 files changed, 68 insertions(+), 47 deletions(-) diff --git a/bin/sgen.ml b/bin/sgen.ml index d2260c3..0249f67 100644 --- a/bin/sgen.ml +++ b/bin/sgen.ml @@ -81,24 +81,24 @@ let watch input_file timeout = let _ = run_with_timeout input_file timeout in (* Polling approach - check file modification time *) - let rec poll_loop last_mtime = + let rec poll_loop last_mtime attempt = Unix.sleepf 0.5; try let stat = Unix.stat abs_path in let current_mtime = stat.Unix.st_mtime in if Float.(current_mtime > last_mtime) then ( - Stdlib.Printf.printf "\n\n--- File changed, re-running ---\n%!"; + Stdlib.Printf.printf "\n\n--- File changed, re-running (attempt #%d) ---\n%!" attempt; let _ = run_with_timeout input_file timeout in - poll_loop current_mtime ) - else poll_loop last_mtime + poll_loop current_mtime (attempt + 1) ) + else poll_loop last_mtime attempt with Unix.Unix_error _ -> Stdlib.Printf.eprintf "Error accessing file, retrying...\n%!"; Unix.sleepf 1.0; - poll_loop last_mtime + poll_loop last_mtime attempt in let initial_stat = Unix.stat abs_path in - poll_loop initial_stat.Unix.st_mtime + poll_loop initial_stat.Unix.st_mtime 2 let preprocess_only input_file = let expr = parse input_file in diff --git a/examples/stack.sg b/examples/stack.sg index 856a4f8..c83ac11 100644 --- a/examples/stack.sg +++ b/examples/stack.sg @@ -1,18 +1,21 @@ +(:= (init C) (+stack 0 [])) + + [(-stack 4 _)] +)> diff --git a/examples/turing.sg b/examples/turing.sg index f037e90..7d34b19 100644 --- a/examples/turing.sg +++ b/examples/turing.sg @@ -1,40 +1,58 @@ +(:= (initial Q) { + [(-i [C|W]) (+m Q [e e] C W)] + [(-i []) (+m Q e e e)] +}) + +(:= (accept Q) [(-m qa L e R) accept]) +(:= (reject Q) [(-m qr L C R) reject]) + +(:= (if Q1 then Q2) [(-m Q1 L e R) (+m Q2 L e R)]) +(:= (skip right on Q , D) [(-m Q L D [C|R]) (+m Q [D|L] C R)]) + +(:= (if C1 on Q1 then Q2 , write C2 , right) + [(-m Q1 L C1 [C|R]) (+m Q2 [C2|L] C R)]) + +(:= (if C1 on Q1 then Q2 , write C2 , left) + [(-m Q1 [C|L] C1 R) (+m Q2 L C [C2|R])]) + ' Turing machine accepting words with as many 'a' as 'b' (:= mt { - 'initial - [(-i [C|W]) (+m q0 [e e] C W)] - [(-i []) (+m q0 e e e)] - 'accept - [(-m q0 L e R) (+m qa L e R)] - [(-m qa L e R) accept] - 'initial skip - [(-m q0 L sep [C|R]) (+m q0 [sep|L] C R)] + #(initial q0) + ' accept + #(accept qa) + #(if q0 then qa) + ' reject + #(reject qr) + #(if q2 then qr) + #(if q3 then qr) + ' initial skip + #(skip right on q0 , sep) 'mark - [(-m q0 L a [C|R]) (+m q2 [sep|L] C R)] - [(-m q0 L b [C|R]) (+m q3 [sep|L] C R)] + #(if a on q0 then q2 , write sep , right) + #(if b on q0 then q3 , write sep , right) 'skip - [(-m q2 L a [C|R]) (+m q2 [a|L] C R)] - [(-m q2 L sep [C|R]) (+m q2 [sep|L] C R)] - [(-m q3 L b [C|R]) (+m q3 [b|L] C R)] - [(-m q3 L sep [C|R]) (+m q3 [sep|L] C R)] + #(skip right on q2 , a) + #(skip right on q2 , sep) + #(skip right on q3 , b) + #(skip right on q3 , sep) 'join - [(-m q2 [C|L] b R) (+m q1 L C [sep|R])] - [(-m q3 [C|L] a R) (+m q1 L C [sep|R])] + #(if b on q2 then q1 , write sep , left) + #(if a on q3 then q1 , write sep , left) 'return - [(-m q1 [C|L] a R) (+m q1 L C [a|R])] - [(-m q1 [C|L] b R) (+m q1 L C [b|R])] - [(-m q1 [C|L] sep R) (+m q1 L C [sep|R])] - [(-m q1 L e [C|R]) (+m q0 [e|L] C R)] - 'reject - [(-m q2 L e R) (+m qr L e R)] - [(-m q3 L e R) (+m qr L e R)] - [(-m qr L C R) reject]}) - -(show (interact @(+i [a e]) #mt)) -(show (interact @(+i [b e]) #mt)) -(show (interact @(+i [a b b e]) #mt)) - -(show (interact @(+i [e]) #mt)) -(show (interact @(+i [a b e]) #mt)) -(show (interact @(+i [a a b b e]) #mt)) -(show (interact @(+i [a b b a e]) #mt)) -(show (interact @(+i [a b a b e]) #mt)) + #(if a on q1 then q1 , write a , left) + #(if b on q1 then q1 , write b , left) + #(if sep on q1 then q1 , write sep , left) + #(if e on q1 then q0 , write e , right) +}) + +(:= (word W) (+i W)) + +(show (interact @#(word [a e]) #mt)) +(show (interact @#(word [b e]) #mt)) +(show (interact @#(word [a b b e]) #mt)) + +(show (interact @#(word [e]) #mt)) +(show (interact @#(word [a b e]) #mt)) +(show (interact @#(word [a a b b e]) #mt)) +(show (interact @#(word [a b b a e]) #mt)) +(show (interact @#(word [a b a b e]) #mt)) From c1ab12545cb7ae3a605170a87f81a20945e17205 Mon Sep 17 00:00:00 2001 From: engboris Date: Thu, 9 Oct 2025 00:26:59 +0200 Subject: [PATCH 2/3] Format --- bin/sgen.ml | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/bin/sgen.ml b/bin/sgen.ml index 0249f67..6d46de2 100644 --- a/bin/sgen.ml +++ b/bin/sgen.ml @@ -87,7 +87,8 @@ let watch input_file timeout = let stat = Unix.stat abs_path in let current_mtime = stat.Unix.st_mtime in if Float.(current_mtime > last_mtime) then ( - Stdlib.Printf.printf "\n\n--- File changed, re-running (attempt #%d) ---\n%!" attempt; + Stdlib.Printf.printf + "\n\n--- File changed, re-running (attempt #%d) ---\n%!" attempt; let _ = run_with_timeout input_file timeout in poll_loop current_mtime (attempt + 1) ) else poll_loop last_mtime attempt From e755c5d18208f8a7c6d1083f2c68259fda884333 Mon Sep 17 00:00:00 2001 From: engboris Date: Thu, 9 Oct 2025 23:21:40 +0200 Subject: [PATCH 3/3] Move docs --- ERROR_RECOVERY.md => docs/error_recovery_implementation.md | 0 .../incremental_parsing_implementation.md | 0 2 files changed, 0 insertions(+), 0 deletions(-) rename ERROR_RECOVERY.md => docs/error_recovery_implementation.md (100%) rename INCREMENTAL_PARSER.md => docs/incremental_parsing_implementation.md (100%) diff --git a/ERROR_RECOVERY.md b/docs/error_recovery_implementation.md similarity index 100% rename from ERROR_RECOVERY.md rename to docs/error_recovery_implementation.md diff --git a/INCREMENTAL_PARSER.md b/docs/incremental_parsing_implementation.md similarity index 100% rename from INCREMENTAL_PARSER.md rename to docs/incremental_parsing_implementation.md