From 0d3319399bcb707a7747e2f04c5e0631c7eebdbd Mon Sep 17 00:00:00 2001 From: Andrew Owen Date: Wed, 7 Aug 2024 10:53:41 -0600 Subject: [PATCH 1/7] Update README.md, make go version number consistent be code and prose --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index e65c1cdf..ee07c3aa 100644 --- a/README.md +++ b/README.md @@ -228,7 +228,7 @@ docker run -ti refaktor/rye ### Building Rye from source -Use official documentation or lines below to install Golang 1.19.3 https://go.dev/doc/install (at the time of writing): +Use official documentation or lines below to install Golang 1.21.5 https://go.dev/doc/install (at the time of writing): wget https://go.dev/dl/go1.21.5.linux-amd64.tar.gz rm -rf /usr/local/go && tar -C /usr/local -xzf go1.21.5.linux-amd64.tar.gz From 57507d3e54adbb6b6322950e67863cba344e6987 Mon Sep 17 00:00:00 2001 From: refaktor Date: Fri, 9 Aug 2024 00:47:19 +0200 Subject: [PATCH 2/7] fixed examples from new-server to http-server --- evaldo/builtins_smtpd.go | 2 +- examples/examples-wip/main_0.6.rye | 2 +- examples/examples-wip/websockets_chat.rye | 2 +- examples/examples-wip/websockets_chat_A.rye | 2 +- examples/examples-wip/websockets_chat_b.rye | 2 +- examples/examples-wip/websockets_chat_c.rye | 2 +- examples/webapp_1/main.rye | 2 +- examples/webapp_1/main0.6.rye | 2 +- examples/webapp_1/main_0.1.rye | 2 +- examples/webapp_1/main_0.2.rye | 2 +- examples/webapp_1/main_0.3.rye | 2 +- examples/webapp_1/main_0.4.rye | 2 +- examples/webapp_1/main_0.5.rye | 2 +- examples/webapp_1/main_0.6.rye | 2 +- examples/webapp_1/main_0.rye | 2 +- examples/webapp_1/main_1.rye | 2 +- examples/webapp_1/main_real_1.rye | 2 +- examples/webapp_2/main_0.6.rye | 2 +- examples/webapp_2/main_0.7.rye | 2 +- examples/webapp_2/main_0.8.rye | 2 +- examples/webapp_4/webserver.rye | 2 +- examples/webapp_spacememo/main.rye | 2 +- examples/webapp_spacememo/main_0.9.rye | 2 +- examples/webserver/ctrl_download.rye | 2 +- examples/webserver/ctrl_download_nocomm.rye | 4 ++-- examples/webserver/hello.rye | 2 +- examples/webserver/main.rye | 10 +++++----- examples/webserver/public_html.rye | 2 +- examples/webserver/static_folder.rye | 2 +- examples/webserver/upload.rye | 2 +- examples/webserver/websocket.rye | 2 +- 31 files changed, 36 insertions(+), 36 deletions(-) diff --git a/evaldo/builtins_smtpd.go b/evaldo/builtins_smtpd.go index 0a893565..8827a8f5 100755 --- a/evaldo/builtins_smtpd.go +++ b/evaldo/builtins_smtpd.go @@ -16,7 +16,7 @@ import ( var Builtins_smtpd = map[string]*env.Builtin{ - "new-smtpd": { + "smtp-server": { Argsn: 1, Doc: "Creates SMTP server", Fn: func(ps *env.ProgramState, arg0 env.Object, arg1 env.Object, arg2 env.Object, arg3 env.Object, arg4 env.Object) env.Object { diff --git a/examples/examples-wip/main_0.6.rye b/examples/examples-wip/main_0.6.rye index 550c068e..85fc3018 100644 --- a/examples/examples-wip/main_0.6.rye +++ b/examples/examples-wip/main_0.6.rye @@ -52,6 +52,6 @@ handle-api: fn { w r } { } } -new-server ":8080" +http-server ":8080" |handle "/api" ?handle-api |serve diff --git a/examples/examples-wip/websockets_chat.rye b/examples/examples-wip/websockets_chat.rye index 0f1cddbc..50271d11 100644 --- a/examples/examples-wip/websockets_chat.rye +++ b/examples/examples-wip/websockets_chat.rye @@ -23,6 +23,6 @@ hub: fn { e } { go-with event ?hub -new-server ":8080" +http-server ":8080" |handle-ws "/chat" ?handle-chat |serve diff --git a/examples/examples-wip/websockets_chat_A.rye b/examples/examples-wip/websockets_chat_A.rye index 52e3159c..6dc7fe08 100644 --- a/examples/examples-wip/websockets_chat_A.rye +++ b/examples/examples-wip/websockets_chat_A.rye @@ -19,6 +19,6 @@ hub: fn { e } { go-with event ?hub -new-server ":8080" +http-server ":8080" |handle-ws "/chat" ?handle-chat |serve diff --git a/examples/examples-wip/websockets_chat_b.rye b/examples/examples-wip/websockets_chat_b.rye index c20f0272..09b2aa1e 100644 --- a/examples/examples-wip/websockets_chat_b.rye +++ b/examples/examples-wip/websockets_chat_b.rye @@ -35,6 +35,6 @@ hub: fn { e } { go-with event ?hub -new-server ":8080" +http-server ":8080" |handle-ws "/chat" ?handle-chat |serve diff --git a/examples/examples-wip/websockets_chat_c.rye b/examples/examples-wip/websockets_chat_c.rye index a132ec4d..f0a7ea45 100644 --- a/examples/examples-wip/websockets_chat_c.rye +++ b/examples/examples-wip/websockets_chat_c.rye @@ -19,6 +19,6 @@ hub: fn { ch } { go-with events ?hub -new-server ":8080" +http-server ":8080" |handle-ws "/chat" ?handle-chat |serve diff --git a/examples/webapp_1/main.rye b/examples/webapp_1/main.rye index 32df8d15..2012b3da 100644 --- a/examples/webapp_1/main.rye +++ b/examples/webapp_1/main.rye @@ -76,7 +76,7 @@ handle-private-api: fn { s c } { } } -new-server ":8080" +http-server ":8080" |handle "/pub-api" ?handle-public-api |handle "/priv-api" ?handle-private-api |handle-files-from "/" %static/ diff --git a/examples/webapp_1/main0.6.rye b/examples/webapp_1/main0.6.rye index eb2ebcb4..56194770 100644 --- a/examples/webapp_1/main0.6.rye +++ b/examples/webapp_1/main0.6.rye @@ -44,6 +44,6 @@ handle-api: fn { w r } { } } -new-server ":8080" +http-server ":8080" |handle "/api" ?handle-api |serve diff --git a/examples/webapp_1/main_0.1.rye b/examples/webapp_1/main_0.1.rye index eef118a8..cdacdd8b 100644 --- a/examples/webapp_1/main_0.1.rye +++ b/examples/webapp_1/main_0.1.rye @@ -14,6 +14,6 @@ handle-api: fn { w r } { write w "Hiiii!" } -new-server ":8080" +http-server ":8080" |handle "/api" ?handle-api |serve diff --git a/examples/webapp_1/main_0.2.rye b/examples/webapp_1/main_0.2.rye index dcf7aa6f..14a4ac37 100644 --- a/examples/webapp_1/main_0.2.rye +++ b/examples/webapp_1/main_0.2.rye @@ -19,6 +19,6 @@ handle-api: fn { w r } { print hello-resource -new-server ":8080" +http-server ":8080" |handle "/api" ?handle-api |serve diff --git a/examples/webapp_1/main_0.3.rye b/examples/webapp_1/main_0.3.rye index c420e858..0cf8260b 100644 --- a/examples/webapp_1/main_0.3.rye +++ b/examples/webapp_1/main_0.3.rye @@ -27,6 +27,6 @@ handle-api: fn { w r } { print hello-resource -new-server ":8080" +http-server ":8080" |handle "/api" ?handle-api |serve diff --git a/examples/webapp_1/main_0.4.rye b/examples/webapp_1/main_0.4.rye index c420e858..0cf8260b 100644 --- a/examples/webapp_1/main_0.4.rye +++ b/examples/webapp_1/main_0.4.rye @@ -27,6 +27,6 @@ handle-api: fn { w r } { print hello-resource -new-server ":8080" +http-server ":8080" |handle "/api" ?handle-api |serve diff --git a/examples/webapp_1/main_0.5.rye b/examples/webapp_1/main_0.5.rye index 7473025b..843f1b02 100644 --- a/examples/webapp_1/main_0.5.rye +++ b/examples/webapp_1/main_0.5.rye @@ -46,6 +46,6 @@ handle-api: fn { w r } { } } -new-server ":8080" +http-server ":8080" |handle "/api" ?handle-api |serve diff --git a/examples/webapp_1/main_0.6.rye b/examples/webapp_1/main_0.6.rye index 550c068e..85fc3018 100644 --- a/examples/webapp_1/main_0.6.rye +++ b/examples/webapp_1/main_0.6.rye @@ -52,6 +52,6 @@ handle-api: fn { w r } { } } -new-server ":8080" +http-server ":8080" |handle "/api" ?handle-api |serve diff --git a/examples/webapp_1/main_0.rye b/examples/webapp_1/main_0.rye index 9ff39f68..678d972a 100644 --- a/examples/webapp_1/main_0.rye +++ b/examples/webapp_1/main_0.rye @@ -44,6 +44,6 @@ handle-api: fn { w r } { } } -new-server ":8080" +http-server ":8080" |handle "/api" ?handle-api |serve diff --git a/examples/webapp_1/main_1.rye b/examples/webapp_1/main_1.rye index c0daf12e..0025ce7e 100644 --- a/examples/webapp_1/main_1.rye +++ b/examples/webapp_1/main_1.rye @@ -31,6 +31,6 @@ handle-public-api: fn { s c } { } } -new-server ":8080" +http-server ":8080" |handle "/pub-api" ?handle-public-api |serve diff --git a/examples/webapp_1/main_real_1.rye b/examples/webapp_1/main_real_1.rye index 32df8d15..2012b3da 100644 --- a/examples/webapp_1/main_real_1.rye +++ b/examples/webapp_1/main_real_1.rye @@ -76,7 +76,7 @@ handle-private-api: fn { s c } { } } -new-server ":8080" +http-server ":8080" |handle "/pub-api" ?handle-public-api |handle "/priv-api" ?handle-private-api |handle-files-from "/" %static/ diff --git a/examples/webapp_2/main_0.6.rye b/examples/webapp_2/main_0.6.rye index f79f6169..7666d290 100644 --- a/examples/webapp_2/main_0.6.rye +++ b/examples/webapp_2/main_0.6.rye @@ -66,6 +66,6 @@ handle-api: fn { w r } { db: open postgres://webapp2:password@webapp2 -new-server ":8080" +http-server ":8080" |handle "/api" ?handle-api |serve diff --git a/examples/webapp_2/main_0.7.rye b/examples/webapp_2/main_0.7.rye index 8dcb6532..cf2b0ba9 100644 --- a/examples/webapp_2/main_0.7.rye +++ b/examples/webapp_2/main_0.7.rye @@ -77,7 +77,7 @@ db: open postgres://webapp2:password@/webapp2 ; the server -new-server ":8080" +http-server ":8080" |handle "/api" ?handle-api |serve diff --git a/examples/webapp_2/main_0.8.rye b/examples/webapp_2/main_0.8.rye index 6e3056a1..baff4cb0 100644 --- a/examples/webapp_2/main_0.8.rye +++ b/examples/webapp_2/main_0.8.rye @@ -82,7 +82,7 @@ db: open postgres://webapp2:password@/webapp2 ; the server -new-server ":8080" +http-server ":8080" |handle "/api" ?handle-api |serve diff --git a/examples/webapp_4/webserver.rye b/examples/webapp_4/webserver.rye index 33ddef75..f74eb712 100644 --- a/examples/webapp_4/webserver.rye +++ b/examples/webapp_4/webserver.rye @@ -14,7 +14,7 @@ handle-api: fn { w r } { ; the server -new-server ":8080" +http-server ":8080" |handle "/" fn { w req } { write w page "Demo" "Use the /api, Luke" } |handle "/api" ?handle-api |serve diff --git a/examples/webapp_spacememo/main.rye b/examples/webapp_spacememo/main.rye index 6e3d38e7..328039e1 100644 --- a/examples/webapp_spacememo/main.rye +++ b/examples/webapp_spacememo/main.rye @@ -39,7 +39,7 @@ sess: "main-session" ; the server -new-server ":8080" +http-server ":8080" |handle "/api" ?handle-api |serve diff --git a/examples/webapp_spacememo/main_0.9.rye b/examples/webapp_spacememo/main_0.9.rye index 0826c731..ad62b8aa 100644 --- a/examples/webapp_spacememo/main_0.9.rye +++ b/examples/webapp_spacememo/main_0.9.rye @@ -130,7 +130,7 @@ sess: "main-session" ; the server -new-server ":8080" +http-server ":8080" |handle "/api" ?handle-api |serve diff --git a/examples/webserver/ctrl_download.rye b/examples/webserver/ctrl_download.rye index 1b4a0fd3..b9a6427c 100644 --- a/examples/webserver/ctrl_download.rye +++ b/examples/webserver/ctrl_download.rye @@ -38,7 +38,7 @@ handle-file-get: fn { w r } { } ; setup the server -new-server ":8080" +http-server ":8080" |handle "/get" ?handle-file-get |handle "/" new-static-handler %www |serve diff --git a/examples/webserver/ctrl_download_nocomm.rye b/examples/webserver/ctrl_download_nocomm.rye index 6021030e..a431b4a6 100644 --- a/examples/webserver/ctrl_download_nocomm.rye +++ b/examples/webserver/ctrl_download_nocomm.rye @@ -26,7 +26,7 @@ handle-file-get: fn { w r } { file .copy w } -new-server ":8080" +http-server ":8080" |handle "/get" ?handle-file-get |serve @@ -59,7 +59,7 @@ new-server ":8080" ; |send-file\attachment w "application/x-pkcs12" "mycert.p12" ; } -; new-server ":8080" +; http-server ":8080" ; |flaskish-handle "/get" ?get-file ; |serve diff --git a/examples/webserver/hello.rye b/examples/webserver/hello.rye index 773b91b6..7bb6266f 100644 --- a/examples/webserver/hello.rye +++ b/examples/webserver/hello.rye @@ -3,7 +3,7 @@ rye .needs { http } -new-server ":8081" +http-server ":8081" |handle "/hello" fn { w r } { .write "Hello world" } |serve diff --git a/examples/webserver/main.rye b/examples/webserver/main.rye index c4334721..05f63910 100644 --- a/examples/webserver/main.rye +++ b/examples/webserver/main.rye @@ -7,13 +7,13 @@ rye .needs { http } ; serve hello world -new-server ":8081" +http-server ":8081" |handle "/hello" fn { w r } { .write "Hello world" } |serve ; serve all static files from www folder -new-server ":8080" +http-server ":8080" |handle "/" new-static-handler %www ; TODO rename, integrate http-dir |serve @@ -22,7 +22,7 @@ new-server ":8080" sh: new-static-handler %static -new-server ":8082" +http-server ":8082" |handle "/static/" sh |handle "/" fn { w r } { .write now } |serve @@ -37,7 +37,7 @@ get-user: fn { w r } { |to-json |write* w } -new-server ":8080" +http-server ":8080" |handle "/user/" get-user |serve @@ -51,7 +51,7 @@ upload: fn { w r } { copy holder file } -new-server ":8080" +http-server ":8080" |handle "/" new-static-handler %www |handle "/upload" ?upload |serve diff --git a/examples/webserver/public_html.rye b/examples/webserver/public_html.rye index 6fd85048..f5d1538e 100644 --- a/examples/webserver/public_html.rye +++ b/examples/webserver/public_html.rye @@ -3,7 +3,7 @@ rye .needs { http } -new-server ":8082" +http-server ":8082" |handle "/" new-static-handler %public_html |serve diff --git a/examples/webserver/static_folder.rye b/examples/webserver/static_folder.rye index f79ca49d..fabb2f60 100644 --- a/examples/webserver/static_folder.rye +++ b/examples/webserver/static_folder.rye @@ -6,7 +6,7 @@ rye .needs { http } sh: new-static-handler %static -new-server ":8083" +http-server ":8083" |handle "/static/" strip-prefix sh "/static/" |handle "/" fn { w r } { .write to-string now } |serve diff --git a/examples/webserver/upload.rye b/examples/webserver/upload.rye index d2cc8b39..5e066197 100644 --- a/examples/webserver/upload.rye +++ b/examples/webserver/upload.rye @@ -23,7 +23,7 @@ upload: fn { w r } { write w "OK" } -new-server ":8080" +http-server ":8080" |handle "/" new-static-handler %www |handle "/upload" ?upload |serve diff --git a/examples/webserver/websocket.rye b/examples/webserver/websocket.rye index ee96f3c8..bd6fb07e 100644 --- a/examples/webserver/websocket.rye +++ b/examples/webserver/websocket.rye @@ -3,7 +3,7 @@ rye .needs { http } -new-server ":9090" +http-server ":9090" |handle-ws "/echo" fn { s ctx } { forever { read s |write* s } } |handle-ws "/captcha" fn { s ctx } { write s "Hasta la vista," From 7c03f2a189366e3ed19215dfeb7580fbd6a7d0ed Mon Sep 17 00:00:00 2001 From: Rory Molinari Date: Fri, 9 Aug 2024 17:02:32 -0400 Subject: [PATCH 3/7] Fix examples/adventofcode/2022/3 - fix obsolete method names split-every, intersect - use modwords where needed - insert |unique calls where needed for problem semantics --- examples/adventofcode/2022/3/main.rye | 25 +++++++++---------------- examples/adventofcode/2022/3/main2.rye | 24 ++++++++++++------------ 2 files changed, 21 insertions(+), 28 deletions(-) diff --git a/examples/adventofcode/2022/3/main.rye b/examples/adventofcode/2022/3/main.rye index 66a3926a..dd48d64b 100644 --- a/examples/adventofcode/2022/3/main.rye +++ b/examples/adventofcode/2022/3/main.rye @@ -11,25 +11,18 @@ read\lines %rucksacks.txt :lines |fold 'priority 0 { - .length? / 2 :mid , - .split-every mid |pass { .first :left } |second - |intersect left |fold 'priority1 0 { + .length? / 2 |to-integer ::mid , + .split\every mid |pass { .first ::left } |second + |intersection left |unique |fold 'priority1 0 { .get-priority + priority1 - } |+ priority + } |+ priority } |print ; part 2 - lines .split-every 3 |fold 'priority 0 { - -> 0 :line0 , - -> 1 :line1 , - -> 2 |intersect line1 |intersect line0 - |get-priority + priority + lines .split\every 3 |fold 'priority 0 { + -> 0 ::line0 , + -> 1 ::line1 , + -> 2 |intersection line1 |intersection line0 + |unique |get-priority + priority } |print - - - - - - - diff --git a/examples/adventofcode/2022/3/main2.rye b/examples/adventofcode/2022/3/main2.rye index b9a25e28..6e4f69c8 100644 --- a/examples/adventofcode/2022/3/main2.rye +++ b/examples/adventofcode/2022/3/main2.rye @@ -9,21 +9,21 @@ ; part 1 - read\lines %rucksacks.txt :lines |add-up { - .length? / 2 :mid , - .split-every mid - |with { .first :left , .second } - |intersect left |add-up { .get-priority } - } |print + read\lines %rucksacks.txt :lines |map { + .length? / 2 |to-integer ::mid , + .split\every mid + |with { .first ::left , .second } + |intersection left |unique .map { .get-priority } |sum + } |sum |print ; part 2 - lines .split-every 3 |add-up { - -> 0 :line0 , - -> 1 :line1 , - -> 2 |intersect line1 |intersect line0 - |get-priority - } |print + lines .split\every 3 |map { + -> 0 ::line0 , + -> 1 ::line1 , + -> 2 |intersection line1 |intersection line0 + |unique .get-priority + } |sum |print From 6f28cc41425b97a471afbe594818573b643da509 Mon Sep 17 00:00:00 2001 From: Rory Molinari Date: Fri, 9 Aug 2024 20:21:59 -0400 Subject: [PATCH 4/7] Address #305: function names in error messages Some functions are named incorrectly in their own error messages. Examples: - split\every appears as "split-every" in its own error messages - most of the cases I found have this form: backslash appears as hyphen - to-eyr and eyr\full both appear as "eyr" - this appears to be a copy/paste issue; there are some other occurrences - near-zero appears as "is-zero" - perhaps an artifact of a renaming - this function's descriptive string was also wrong --- evaldo/builtins.go | 26 +++++++++++++------------- evaldo/builtins_eyr.go | 8 ++++---- evaldo/builtins_math.go | 4 ++-- 3 files changed, 19 insertions(+), 19 deletions(-) diff --git a/evaldo/builtins.go b/evaldo/builtins.go index 4a6d9aa4..a038f91c 100644 --- a/evaldo/builtins.go +++ b/evaldo/builtins.go @@ -2387,11 +2387,11 @@ var builtins = map[string]*env.Builtin{ return ps.Res default: ps.ErrorFlag = true - return MakeArgError(ps, 2, []env.Type{env.BlockType}, "do-in") + return MakeArgError(ps, 2, []env.Type{env.BlockType}, "do\\in") } default: ps.ErrorFlag = true - return MakeArgError(ps, 1, []env.Type{env.CtxType}, "do-in") + return MakeArgError(ps, 1, []env.Type{env.CtxType}, "do\\in") } }, @@ -2420,11 +2420,11 @@ var builtins = map[string]*env.Builtin{ return ps.Res default: ps.ErrorFlag = true - return MakeArgError(ps, 2, []env.Type{env.BlockType}, "do-in") + return MakeArgError(ps, 2, []env.Type{env.BlockType}, "do\\in\\try") } default: ps.ErrorFlag = true - return MakeArgError(ps, 1, []env.Type{env.CtxType}, "do-in") + return MakeArgError(ps, 1, []env.Type{env.CtxType}, "do\\in\\try") } }, @@ -2471,11 +2471,11 @@ var builtins = map[string]*env.Builtin{ return ps.Res default: ps.ErrorFlag = true - return MakeArgError(ps, 2, []env.Type{env.BlockType}, "do-in") + return MakeArgError(ps, 2, []env.Type{env.BlockType}, "do\\par") } default: ps.ErrorFlag = true - return MakeArgError(ps, 1, []env.Type{env.CtxType}, "do-in") + return MakeArgError(ps, 1, []env.Type{env.CtxType}, "do\\par") } }, @@ -5064,7 +5064,7 @@ var builtins = map[string]*env.Builtin{ return ps.Res } default: - return MakeArgError(ps, 1, []env.Type{env.IntegerType}, "recur-if\\1") + return MakeArgError(ps, 1, []env.Type{env.IntegerType}, "recur-if") } }, }, @@ -6278,13 +6278,13 @@ var builtins = map[string]*env.Builtin{ case env.String: return util.StringToFieldsWithQuoted(str.Value, sepa.Value, quote.Value) default: - return MakeArgError(ps, 3, []env.Type{env.StringType}, "split-quoted") + return MakeArgError(ps, 3, []env.Type{env.StringType}, "split\\quoted") } default: - return MakeArgError(ps, 2, []env.Type{env.StringType}, "split-quoted") + return MakeArgError(ps, 2, []env.Type{env.StringType}, "split\\quoted") } default: - return MakeArgError(ps, 1, []env.Type{env.StringType}, "split-quoted") + return MakeArgError(ps, 1, []env.Type{env.StringType}, "split\\quoted") } }, }, @@ -6329,7 +6329,7 @@ var builtins = map[string]*env.Builtin{ } return *env.NewBlock(*env.NewTSeries(spl2)) default: - return MakeArgError(ps, 2, []env.Type{env.IntegerType}, "split-every") + return MakeArgError(ps, 2, []env.Type{env.IntegerType}, "split\\every") } case env.Block: switch sepa := arg1.(type) { @@ -6341,10 +6341,10 @@ var builtins = map[string]*env.Builtin{ } return *env.NewBlock(*env.NewTSeries(spl2)) default: - return MakeArgError(ps, 2, []env.Type{env.IntegerType}, "split-every") + return MakeArgError(ps, 2, []env.Type{env.IntegerType}, "split\\every") } default: - return MakeArgError(ps, 1, []env.Type{env.StringType, env.BlockType}, "split-every") + return MakeArgError(ps, 1, []env.Type{env.StringType, env.BlockType}, "split\\every") } }, }, diff --git a/evaldo/builtins_eyr.go b/evaldo/builtins_eyr.go index 25123d4e..7bb3e940 100755 --- a/evaldo/builtins_eyr.go +++ b/evaldo/builtins_eyr.go @@ -302,7 +302,7 @@ var Builtins_eyr = map[string]*env.Builtin{ CompileRyeToEyr(&bloc, ps, eBlock) return *eBlock default: - return MakeArgError(ps, 1, []env.Type{env.BlockType}, "eyr") + return MakeArgError(ps, 1, []env.Type{env.BlockType}, "to-eyr") } }, }, @@ -320,7 +320,7 @@ var Builtins_eyr = map[string]*env.Builtin{ ps.Ser = ser return ps.Res default: - return MakeArgError(ps, 1, []env.Type{env.BlockType}, "eyr") + return MakeArgError(ps, 1, []env.Type{env.BlockType}, "eyr\\full") } }, }, @@ -343,10 +343,10 @@ var Builtins_eyr = map[string]*env.Builtin{ ps.Ser = ser return ps.Res default: - return MakeArgError(ps, 2, []env.Type{env.BlockType}, "eyr-loop") + return MakeArgError(ps, 2, []env.Type{env.BlockType}, "eyr\\loop") } default: - return MakeArgError(ps, 1, []env.Type{env.IntegerType}, "eyr-loop") + return MakeArgError(ps, 1, []env.Type{env.IntegerType}, "eyr\\loop") } }, }, diff --git a/evaldo/builtins_math.go b/evaldo/builtins_math.go index b64a83f2..77fd4dfa 100644 --- a/evaldo/builtins_math.go +++ b/evaldo/builtins_math.go @@ -814,7 +814,7 @@ var Builtins_math = map[string]*env.Builtin{ }, "near-zero": { Argsn: 1, - Doc: "Returns true if two decimals are close.", + Doc: "Returns true if a decimal is close to zero.", Fn: func(ps *env.ProgramState, arg0 env.Object, arg1 env.Object, arg2 env.Object, arg3 env.Object, arg4 env.Object) env.Object { var fa float64 switch a := arg0.(type) { @@ -823,7 +823,7 @@ var Builtins_math = map[string]*env.Builtin{ case env.Integer: fa = float64(a.Value) default: - return MakeArgError(ps, 1, []env.Type{env.IntegerType, env.BlockType}, "is-zero") + return MakeArgError(ps, 1, []env.Type{env.IntegerType, env.BlockType}, "near-zero") } // const epsilon = math.SmallestNonzeroFloat64 const epsilon = 0.0000000000001 // math.SmallestNonzeroFloat64 From 0ef5138cdff25911a6f928c87638764d3ab88145 Mon Sep 17 00:00:00 2001 From: refaktor Date: Sat, 10 Aug 2024 03:16:43 +0200 Subject: [PATCH 5/7] fixed the ctrl-c (sleep/serve) issue https://github.com/refaktor/rye/issues/301 and added integer explicit // division like Python/ruby --- evaldo/builtins.go | 49 +- evaldo/repl.go | 28 +- loader/loader.go | 4 +- main_wasm.go | 5 +- runner/runner.go | 12 + tests/basics.rye | 15 +- util/microliner.go | 1139 ++++++++++++++++++++++---------------------- 7 files changed, 676 insertions(+), 576 deletions(-) diff --git a/evaldo/builtins.go b/evaldo/builtins.go index 4a6d9aa4..8310fe38 100644 --- a/evaldo/builtins.go +++ b/evaldo/builtins.go @@ -1262,7 +1262,7 @@ var builtins = map[string]*env.Builtin{ }, "_/": { // ** Argsn: 2, - Doc: "Divided two numbers.", + Doc: "Divide two numbers and return a decimal.", Pure: true, Fn: func(ps *env.ProgramState, arg0 env.Object, arg1 env.Object, arg2 env.Object, arg3 env.Object, arg4 env.Object) env.Object { switch a := arg0.(type) { @@ -1305,6 +1305,51 @@ var builtins = map[string]*env.Builtin{ } }, }, + "_//": { // ** + Argsn: 2, + Doc: "Divides two numbers and return truncated integer.", + Pure: true, + Fn: func(ps *env.ProgramState, arg0 env.Object, arg1 env.Object, arg2 env.Object, arg3 env.Object, arg4 env.Object) env.Object { + switch a := arg0.(type) { + case env.Integer: + switch b := arg1.(type) { + case env.Integer: + if b.Value == 0 { + ps.FailureFlag = true + return MakeBuiltinError(ps, "Can't divide by Zero.", "_/") + } + return *env.NewInteger(a.Value / b.Value) + case env.Decimal: + if b.Value == 0.0 { + ps.FailureFlag = true + return MakeBuiltinError(ps, "Can't divide by Zero.", "_/") + } + return *env.NewInteger(a.Value / int64(b.Value)) + default: + return MakeArgError(ps, 2, []env.Type{env.IntegerType, env.DecimalType}, "_/") + } + case env.Decimal: + switch b := arg1.(type) { + case env.Integer: + if b.Value == 0 { + ps.FailureFlag = true + return MakeBuiltinError(ps, "Can't divide by Zero.", "_/") + } + return *env.NewInteger(int64(a.Value) / b.Value) + case env.Decimal: + if b.Value == 0.0 { + ps.FailureFlag = true + return MakeBuiltinError(ps, "Can't divide by Zero.", "_/") + } + return *env.NewInteger(int64(a.Value) / int64(b.Value)) + default: + return MakeArgError(ps, 2, []env.Type{env.IntegerType, env.DecimalType}, "_/") + } + default: + return MakeArgError(ps, 1, []env.Type{env.IntegerType, env.DecimalType}, "_/") + } + }, + }, "_=": { // *** Argsn: 2, Doc: "Checks if two Rye values are equal.", @@ -7842,7 +7887,7 @@ func registerBuiltin(ps *env.ProgramState, word string, builtin env.Builtin) { // in future a map will probably not be a map but an array and builtin will also support the Kind value idxk := 0 - if strings.Index(word, "//") > 0 { + if word != "_//" && strings.Index(word, "//") > 0 { temp := strings.Split(word, "//") word = temp[1] idxk = ps.Idx.IndexWord(temp[0]) diff --git a/evaldo/repl.go b/evaldo/repl.go index c2981cb0..28ce565e 100644 --- a/evaldo/repl.go +++ b/evaldo/repl.go @@ -10,6 +10,7 @@ import ( "path/filepath" "regexp" "strings" + "syscall" "github.com/eiannone/keyboard" @@ -320,7 +321,6 @@ func DoRyeRepl(es *env.ProgramState, dialect string, showResults bool) { // here fmt.Println(err) return } - defer keyboard.Close() c := make(chan util.KeyEvent) r := Repl{ @@ -332,31 +332,49 @@ func DoRyeRepl(es *env.ProgramState, dialect string, showResults bool) { // here ml := util.NewMicroLiner(c, r.recieveMessage, r.recieveLine) r.ml = ml - ctx := context.Background() - defer ctx.Done() + ctx, cancel := context.WithCancel(context.Background()) + + defer cancel() + + // ctx := context.Background() + // defer os.Exit(0) + // defer ctx.Done() + defer keyboard.Close() go func(ctx context.Context) { for { select { case <-ctx.Done(): + // fmt.Println("Done") return default: + // fmt.Println("Select default") r, k, keyErr := keyboard.GetKey() if err != nil { fmt.Println(keyErr) break } if k == keyboard.KeyCtrlC { - ctx.Done() + // fmt.Println("Ctrl C 1") + cancel() + syscall.Kill(os.Getpid(), syscall.SIGINT) + //ctx.Done() + // fmt.Println("") + // return + //break + // os.Exit(0) } c <- constructKeyEvent(r, k) } } }(ctx) - _, err = ml.MicroPrompt("x> ", "", 0) + // fmt.Println("MICRO") + _, err = ml.MicroPrompt("x> ", "", 0, ctx) if err != nil { fmt.Println(err) } + // fmt.Println("END") + } /* THIS WAS DISABLED TEMP FOR WASM MODE .. 20250116 func DoGeneralInput(es *env.ProgramState, prompt string) { diff --git a/loader/loader.go b/loader/loader.go index dc5613f4..72f61aa0 100644 --- a/loader/loader.go +++ b/loader/loader.go @@ -345,7 +345,7 @@ func parseOpword(v *Values, d Any) (Any, error) { word := v.Token() force := 0 var idx int - if len(word) == 1 || word == "<<" || word == "<-" || word == "<~" || word == ">=" || word == "<=" { + if len(word) == 1 || word == "<<" || word == "<-" || word == "<~" || word == ">=" || word == "<=" || word == "//" { // onecharopwords < > + * ... their naming is equal to _< _> _* ... idx = wordIndex.IndexWord("_" + word) } else { @@ -467,7 +467,7 @@ func newParser() *Parser { // TODO -- add string eaddress path url time ONECHARWORDS <- < [<>*+-=/] > NORMOPWORDS <- < ("_"[<>*+-=/]) > PIPEARROWS <- ">>" / "~>" / "->" - OPARROWS <- "<<" / "<~" / "<-" / ">=" / "<=" + OPARROWS <- "<<" / "<~" / "<-" / ">=" / "<=" / "//" LETTER <- < [a-zA-Z^(` + "`" + `] > LETTERORNUM <- < [a-zA-Z0-9-?=.\\!_+<>\]*()] > LETTERORNUMNOX <- < [a-zA-Z0-9-?=.\\!_+\]*()] > diff --git a/main_wasm.go b/main_wasm.go index fd9b0016..f59f188c 100755 --- a/main_wasm.go +++ b/main_wasm.go @@ -4,6 +4,7 @@ package main import ( + "context" "fmt" "regexp" "strings" @@ -99,7 +100,9 @@ func main() { jsCallback = js.Global().Get("receiveMessageFromGo") jsCallback2 = js.Global().Get("receiveLineFromGo") - ml.MicroPrompt("x> ", "", 0) + ctx := context.Background() + + ml.MicroPrompt("x> ", "", 0, ctx) /* for { key := <-c diff --git a/runner/runner.go b/runner/runner.go index ac02de4b..7607a9de 100644 --- a/runner/runner.go +++ b/runner/runner.go @@ -542,6 +542,18 @@ func main_rye_repl(_ io.Reader, _ io.Writer, subc bool, here bool, lang string, } } + // c := make(chan os.Signal, 1) + // signal.Notify(c, os.Interrupt, syscall.SIGTERM) + + //go func() { + // sig := <-c + // fmt.Println() + // fmt.Println("Captured signal:", sig) + // Perform cleanup or other actions here + // os.Exit(0) + //}() + //fmt.Println("Waiting for signal") + evaldo.DoRyeRepl(es, lang, evaldo.ShowResults) } diff --git a/tests/basics.rye b/tests/basics.rye index cecff73d..c0798169 100644 --- a/tests/basics.rye +++ b/tests/basics.rye @@ -200,14 +200,23 @@ section "Working with numbers" ; TODO } - ` - group "+-*/" + + group "+-*/ //" "" { { integer decimal } { integer decimal } } { - } + equal { 5 / 2 } 2.5 + equal { 5.0 / 2 } 2.5 + equal { 5 / 2.0 } 2.5 + equal { 5.0 / 2.0 } 2.5 + equal { 5 // 2 } 2 + equal { 5.0 // 2 } 2 + equal { 5 // 2.0 } 2 + equal { 5.0 // 2.0 } 2 + } + ` group "!=><+" "" { { integer decimal } { integer decimal } } diff --git a/util/microliner.go b/util/microliner.go index fa081cf6..2b4885fb 100644 --- a/util/microliner.go +++ b/util/microliner.go @@ -2,6 +2,7 @@ package util import ( "bytes" + "context" "fmt" "strconv" "strings" @@ -455,7 +456,7 @@ func (s *MLState) refreshSingleLine(prompt []rune, buf []rune, pos int) error { } // signals end-of-file by pressing Ctrl-D. -func (s *MLState) MicroPrompt(prompt string, text string, pos int) (string, error) { +func (s *MLState) MicroPrompt(prompt string, text string, pos int, ctx1 context.Context) (string, error) { // history related historyEnd := "" var historyPrefix []string @@ -542,623 +543,635 @@ startOfHere: // mainLoop: for { - trace("POS: ") - trace(pos) - // receive next character from channel - next := <-s.next - // s.sendBack(next) - // err := nil - // LBL haveNext: - /* if err != nil { - if s.shouldRestart != nil && s.shouldRestart(err) { - goto restart - } - return "", err - }*/ - - // historyAction = false - //switch v := next.(type) { - //case string: - //} - /* if pos == len(line) && !s.multiLineMode && - len(p)+len(line) < s.columns*4 && // Avoid countGlyphs on large lines - countGlyphs(p)+countGlyphs(line) < s.columns-1 {*/ - ///// pLen := countGlyphs(p) - if next.Ctrl { - switch strings.ToLower(next.Key) { - case "c": - return "", nil - case "a": - pos = 0 - // s.needRefresh = true - case "e": - pos = len(line) - // s.needRefresh = true - case "b": - if pos > 0 { - pos -= len(getSuffixGlyphs(line[:pos], 1)) - //s.needRefresh = true - } else { - s.doBeep() - } - case "f": // right - if pos < len(line) { - pos += len(getPrefixGlyphs(line[pos:], 1)) - // s.needRefresh = true - } else { - s.doBeep() - } - case "k": // delete remainder of line - if pos >= len(line) { - // s.doBeep() - } else { - // if killAction > 0 { - // s.addToKillRing(line[pos:], 1) // Add in apend mode - // } else { - // s.addToKillRing(line[pos:], 0) // Add in normal mode - // } - // killAction = 2 // Mark that there was a kill action - line = line[:pos] - s.needRefresh = true + + select { + case <-ctx1.Done(): + // fmt.Println("Exitin due to coancelation") + return "", nil + default: + trace("POS: ") + trace(pos) + // receive next character from channel + next := <-s.next + // s.sendBack(next) + // err := nil + // LBL haveNext: + /* if err != nil { + if s.shouldRestart != nil && s.shouldRestart(err) { + goto restart } - case "l": - s.eraseScreen() - s.needRefresh = true - case "u": // delete to beginning of line - if pos == 0 { - s.doBeep() - } else { - line = line[pos:] + return "", err + }*/ + + // historyAction = false + //switch v := next.(type) { + //case string: + //} + /* if pos == len(line) && !s.multiLineMode && + len(p)+len(line) < s.columns*4 && // Avoid countGlyphs on large lines + countGlyphs(p)+countGlyphs(line) < s.columns-1 {*/ + ///// pLen := countGlyphs(p) + if next.Ctrl { + switch strings.ToLower(next.Key) { + case "c": + /* return "", ErrPromptAborted + line = line[:0] pos = 0 + s.restartPrompt() */ + // fmt.Print("case C") + return "", nil + case "a": + pos = 0 + // s.needRefresh = true + case "e": + pos = len(line) + // s.needRefresh = true + case "b": + if pos > 0 { + pos -= len(getSuffixGlyphs(line[:pos], 1)) + //s.needRefresh = true + } else { + s.doBeep() + } + case "f": // right + if pos < len(line) { + pos += len(getPrefixGlyphs(line[pos:], 1)) + // s.needRefresh = true + } else { + s.doBeep() + } + case "k": // delete remainder of line + if pos >= len(line) { + // s.doBeep() + } else { + // if killAction > 0 { + // s.addToKillRing(line[pos:], 1) // Add in apend mode + // } else { + // s.addToKillRing(line[pos:], 0) // Add in normal mode + // } + // killAction = 2 // Mark that there was a kill action + line = line[:pos] + s.needRefresh = true + } + case "l": + s.eraseScreen() s.needRefresh = true + case "u": // delete to beginning of line + if pos == 0 { + s.doBeep() + } else { + line = line[pos:] + pos = 0 + s.needRefresh = true + } + case "n": + histNext() + case "p": + histPrev() } - case "n": - histNext() - case "p": - histPrev() - } - } else if next.Alt { - switch strings.ToLower(next.Key) { - case "b": - if pos > 0 { - var spaceHere, spaceLeft, leftKnown bool - for { - pos-- - if pos == 0 { - break - } - if leftKnown { - spaceHere = spaceLeft - } else { - spaceHere = unicode.IsSpace(line[pos]) + } else if next.Alt { + switch strings.ToLower(next.Key) { + case "b": + if pos > 0 { + var spaceHere, spaceLeft, leftKnown bool + for { + pos-- + if pos == 0 { + break + } + if leftKnown { + spaceHere = spaceLeft + } else { + spaceHere = unicode.IsSpace(line[pos]) + } + spaceLeft, leftKnown = unicode.IsSpace(line[pos-1]), true + if !spaceHere && spaceLeft { + break + } } - spaceLeft, leftKnown = unicode.IsSpace(line[pos-1]), true - if !spaceHere && spaceLeft { - break + } else { + s.doBeep() + } + case "f": + if pos < len(line) { + var spaceHere, spaceLeft, hereKnown bool + for { + pos++ + if pos == len(line) { + break + } + if hereKnown { + spaceLeft = spaceHere + } else { + spaceLeft = unicode.IsSpace(line[pos-1]) + } + spaceHere, hereKnown = unicode.IsSpace(line[pos]), true + if spaceHere && !spaceLeft { + break + } } + } else { + s.doBeep() } - } else { - s.doBeep() - } - case "f": - if pos < len(line) { - var spaceHere, spaceLeft, hereKnown bool + case "d": // Delete next word + if pos == len(line) { + s.doBeep() + break + } + // Remove whitespace to the right + var buf []rune // Store the deleted chars in a buffer for { - pos++ - if pos == len(line) { + if pos == len(line) || !unicode.IsSpace(line[pos]) { break } - if hereKnown { - spaceLeft = spaceHere - } else { - spaceLeft = unicode.IsSpace(line[pos-1]) - } - spaceHere, hereKnown = unicode.IsSpace(line[pos]), true - if spaceHere && !spaceLeft { + buf = append(buf, line[pos]) + line = append(line[:pos], line[pos+1:]...) + } + // Remove non-whitespace to the right + for { + if pos == len(line) || unicode.IsSpace(line[pos]) { break } + buf = append(buf, line[pos]) + line = append(line[:pos], line[pos+1:]...) + trace(buf) } - } else { - s.doBeep() - } - case "d": // Delete next word - if pos == len(line) { - s.doBeep() - break - } - // Remove whitespace to the right - var buf []rune // Store the deleted chars in a buffer - for { - if pos == len(line) || !unicode.IsSpace(line[pos]) { - break - } - buf = append(buf, line[pos]) - line = append(line[:pos], line[pos+1:]...) + s.needRefresh = true + // Save the result on the killRing + /*if killAction > 0 { + s.addToKillRing(buf, 2) // Add in prepend mode + } else { + s.addToKillRing(buf, 0) // Add in normal mode + } */ + // killAction = 2 // Mark that there was some killing + // case "bs": // Erase word + // pos, line, killAction = s.eraseWord(pos, line, killAction) } - // Remove non-whitespace to the right - for { - if pos == len(line) || unicode.IsSpace(line[pos]) { - break + } else { + switch next.Code { + case 13: // Enter + historyStale = true + s.lastLineString = false + // trace2("NL") + if len(line) > 0 && unicode.IsSpace(line[len(line)-1]) { + s.sendBack(fmt.Sprintf("%s⏎\n\r%s", color_emph, reset)) + if s.inString { + s.lastLineString = true + } + } else { + s.sendBack("\n\r") } - buf = append(buf, line[pos]) - line = append(line[:pos], line[pos+1:]...) - trace(buf) - } - s.needRefresh = true - // Save the result on the killRing - /*if killAction > 0 { - s.addToKillRing(buf, 2) // Add in prepend mode - } else { - s.addToKillRing(buf, 0) // Add in normal mode - } */ - // killAction = 2 // Mark that there was some killing - // case "bs": // Erase word - // pos, line, killAction = s.eraseWord(pos, line, killAction) - } - } else { - switch next.Code { - case 13: // Enter - historyStale = true - s.lastLineString = false - // trace2("NL") - if len(line) > 0 && unicode.IsSpace(line[len(line)-1]) { - s.sendBack(fmt.Sprintf("%s⏎\n\r%s", color_emph, reset)) - if s.inString { - s.lastLineString = true + xx := s.enterLine(string(line)) + pos = 0 + if xx == "next line" { + multiline = true + } else { + s.sendBack("\n\r") } - } else { - s.sendBack("\n\r") - } - xx := s.enterLine(string(line)) - pos = 0 - if xx == "next line" { - multiline = true - } else { - s.sendBack("\n\r") - } - line = make([]rune, 0) - trace(line) - goto startOfHere - case 8: // Backspace - if pos <= 0 { - s.doBeep() - } else { - // pos += 1 - n := len(getSuffixGlyphs(line[:pos], 1)) - trace("<---line--->") - trace(line[:pos-n]) - trace(line[pos:]) - trace(n) - trace(pos) + line = make([]rune, 0) trace(line) - // line = append(line[:pos-n], ' ') - line = append(line[:pos-n], line[pos:]...) - // line = line[:pos-1] - trace(line) - // line = append(line[:pos-n], line[pos:]...) - pos -= n - s.needRefresh = true - } - case 46: // Del - if pos >= len(line) { - s.doBeep() - } else { - n := len(getPrefixGlyphs(line[pos:], 1)) - line = append(line[:pos], line[pos+n:]...) - s.needRefresh = true - } - case 39: // Right - if pos < len(line) { - pos += len(getPrefixGlyphs(line[pos:], 1)) - } else { - s.doBeep() - } - case 37: // Left - if pos > 0 { - pos -= len(getSuffixGlyphs(line[:pos], 1)) - traceTop(pos, 3) - } else { - s.doBeep() - } - case 38: // Up - histPrev() - case 40: // Down - histNext() - case 36: // Home - pos = 0 - case 35: // End - pos = len(line) - default: - trace("***************************** ALARM *******************") - vs := []rune(next.Key) - v := vs[0] - - if pos >= countGlyphs(p)+countGlyphs(line) { - line = append(line, v) - //s.sendBack(fmt.Sprintf("%c", v)) - s.needRefresh = true // JM --- - pos++ - } else { - line = append(line[:pos], append([]rune{v}, line[pos:]...)...) - pos++ - s.needRefresh = true + goto startOfHere + case 8: // Backspace + if pos <= 0 { + s.doBeep() + } else { + // pos += 1 + n := len(getSuffixGlyphs(line[:pos], 1)) + trace("<---line--->") + trace(line[:pos-n]) + trace(line[pos:]) + trace(n) + trace(pos) + trace(line) + // line = append(line[:pos-n], ' ') + line = append(line[:pos-n], line[pos:]...) + // line = line[:pos-1] + trace(line) + // line = append(line[:pos-n], line[pos:]...) + pos -= n + s.needRefresh = true + } + case 46: // Del + if pos >= len(line) { + s.doBeep() + } else { + n := len(getPrefixGlyphs(line[pos:], 1)) + line = append(line[:pos], line[pos+n:]...) + s.needRefresh = true + } + case 39: // Right + if pos < len(line) { + pos += len(getPrefixGlyphs(line[pos:], 1)) + } else { + s.doBeep() + } + case 37: // Left + if pos > 0 { + pos -= len(getSuffixGlyphs(line[:pos], 1)) + traceTop(pos, 3) + } else { + s.doBeep() + } + case 38: // Up + histPrev() + case 40: // Down + histNext() + case 36: // Home + pos = 0 + case 35: // End + pos = len(line) + default: + trace("***************************** ALARM *******************") + vs := []rune(next.Key) + v := vs[0] + + if pos >= countGlyphs(p)+countGlyphs(line) { + line = append(line, v) + //s.sendBack(fmt.Sprintf("%c", v)) + s.needRefresh = true // JM --- + pos++ + } else { + line = append(line[:pos], append([]rune{v}, line[pos:]...)...) + pos++ + s.needRefresh = true + } } } - } - - /* } else { - line = append(line[:pos], append([]rune{v}, line[pos:]...)...) - pos++ - s.needRefresh = true - } */ - /* case rune: - switch v { - case cr, lf: - if s.needRefresh { - err := s.refresh(p, line, pos) - if err != nil { - return "", err - } - } - if s.multiLineMode { - s.resetMultiLine(p, line, pos) - } - trace() - break mainLoop - case ctrlA: // Start of line - pos = 0 - s.needRefresh = true - case ctrlE: // End of line - pos = len(line) + /* } else { + line = append(line[:pos], append([]rune{v}, line[pos:]...)...) + pos++ s.needRefresh = true - case ctrlB: // left - if pos > 0 { - pos -= len(getSuffixGlyphs(line[:pos], 1)) + } */ + + /* case rune: + switch v { + case cr, lf: + if s.needRefresh { + err := s.refresh(p, line, pos) + if err != nil { + return "", err + } + } + if s.multiLineMode { + s.resetMultiLine(p, line, pos) + } + trace() + break mainLoop + case ctrlA: // Start of line + pos = 0 s.needRefresh = true - } else { - s.doBeep() - } - case ctrlF: // right - if pos < len(line) { - pos += len(getPrefixGlyphs(line[pos:], 1)) + case ctrlE: // End of line + pos = len(line) s.needRefresh = true - } else { - s.doBeep() - } - case ctrlD: // del - if pos == 0 && len(line) == 0 { - // exit - return "", io.EOF - } + case ctrlB: // left + if pos > 0 { + pos -= len(getSuffixGlyphs(line[:pos], 1)) + s.needRefresh = true + } else { + s.doBeep() + } + case ctrlF: // right + if pos < len(line) { + pos += len(getPrefixGlyphs(line[pos:], 1)) + s.needRefresh = true + } else { + s.doBeep() + } + case ctrlD: // del + if pos == 0 && len(line) == 0 { + // exit + return "", io.EOF + } - // ctrlD is a potential EOF, so the rune reader shuts down. - // Therefore, if it isn't actually an EOF, we must re-startPrompt. - s.restartPrompt() + // ctrlD is a potential EOF, so the rune reader shuts down. + // Therefore, if it isn't actually an EOF, we must re-startPrompt. + s.restartPrompt() - if pos >= len(line) { - s.doBeep() - } else { - n := len(getPrefixGlyphs(line[pos:], 1)) - line = append(line[:pos], line[pos+n:]...) - s.needRefresh = true - } - case ctrlK: // delete remainder of line - if pos >= len(line) { - s.doBeep() - } else { - if killAction > 0 { - s.addToKillRing(line[pos:], 1) // Add in apend mode + if pos >= len(line) { + s.doBeep() } else { - s.addToKillRing(line[pos:], 0) // Add in normal mode + n := len(getPrefixGlyphs(line[pos:], 1)) + line = append(line[:pos], line[pos+n:]...) + s.needRefresh = true } + case ctrlK: // delete remainder of line + if pos >= len(line) { + s.doBeep() + } else { + if killAction > 0 { + s.addToKillRing(line[pos:], 1) // Add in apend mode + } else { + s.addToKillRing(line[pos:], 0) // Add in normal mode + } - killAction = 2 // Mark that there was a kill action - line = line[:pos] - s.needRefresh = true - } - case ctrlP: // up - historyAction = true - if historyStale { - historyPrefix = s.getHistoryByPrefix(string(line)) - historyPos = len(historyPrefix) - historyStale = false - } - if historyPos > 0 { - if historyPos == len(historyPrefix) { - historyEnd = string(line) + killAction = 2 // Mark that there was a kill action + line = line[:pos] + s.needRefresh = true } - historyPos-- - line = []rune(historyPrefix[historyPos]) - pos = len(line) - s.needRefresh = true - } else { - s.doBeep() - } - case ctrlN: // down - historyAction = true - if historyStale { - historyPrefix = s.getHistoryByPrefix(string(line)) - historyPos = len(historyPrefix) - historyStale = false - } - if historyPos < len(historyPrefix) { - historyPos++ - if historyPos == len(historyPrefix) { - line = []rune(historyEnd) - } else { + case ctrlP: // up + historyAction = true + if historyStale { + historyPrefix = s.getHistoryByPrefix(string(line)) + historyPos = len(historyPrefix) + historyStale = false + } + if historyPos > 0 { + if historyPos == len(historyPrefix) { + historyEnd = string(line) + } + historyPos-- line = []rune(historyPrefix[historyPos]) + pos = len(line) + s.needRefresh = true + } else { + s.doBeep() } - pos = len(line) + case ctrlN: // down + historyAction = true + if historyStale { + historyPrefix = s.getHistoryByPrefix(string(line)) + historyPos = len(historyPrefix) + historyStale = false + } + if historyPos < len(historyPrefix) { + historyPos++ + if historyPos == len(historyPrefix) { + line = []rune(historyEnd) + } else { + line = []rune(historyPrefix[historyPos]) + } + pos = len(line) + s.needRefresh = true + } else { + s.doBeep() + } + case ctrlT: // transpose prev glyph with glyph under cursor + if len(line) < 2 || pos < 1 { + s.doBeep() + } else { + if pos == len(line) { + pos -= len(getSuffixGlyphs(line, 1)) + } + prev := getSuffixGlyphs(line[:pos], 1) + next := getPrefixGlyphs(line[pos:], 1) + scratch := make([]rune, len(prev)) + copy(scratch, prev) + copy(line[pos-len(prev):], next) + copy(line[pos-len(prev)+len(next):], scratch) + pos += len(next) + s.needRefresh = true + } + case ctrlL: // clear screen + s.eraseScreen() s.needRefresh = true - } else { - s.doBeep() - } - case ctrlT: // transpose prev glyph with glyph under cursor - if len(line) < 2 || pos < 1 { - s.doBeep() - } else { - if pos == len(line) { - pos -= len(getSuffixGlyphs(line, 1)) + case ctrlC: // reset + trace("^C") + if s.multiLineMode { + s.resetMultiLine(p, line, pos) + } + if s.ctrlCAborts { + return "", ErrPromptAborted + } + line = line[:0] + pos = 0 + fmt.Print(prompt) + s.restartPrompt() + case ctrlH, bs: // Backspace + if pos <= 0 { + s.doBeep() + } else { + n := len(getSuffixGlyphs(line[:pos], 1)) + line = append(line[:pos-n], line[pos:]...) + pos -= n + s.needRefresh = true + } + case ctrlU: // Erase line before cursor + if killAction > 0 { + s.addToKillRing(line[:pos], 2) // Add in prepend mode + } else { + s.addToKillRing(line[:pos], 0) // Add in normal mode } - prev := getSuffixGlyphs(line[:pos], 1) - next := getPrefixGlyphs(line[pos:], 1) - scratch := make([]rune, len(prev)) - copy(scratch, prev) - copy(line[pos-len(prev):], next) - copy(line[pos-len(prev)+len(next):], scratch) - pos += len(next) + + killAction = 2 // Mark that there was some killing + line = line[pos:] + pos = 0 s.needRefresh = true - } - case ctrlL: // clear screen - s.eraseScreen() - s.needRefresh = true - case ctrlC: // reset - trace("^C") - if s.multiLineMode { - s.resetMultiLine(p, line, pos) - } - if s.ctrlCAborts { - return "", ErrPromptAborted - } - line = line[:0] - pos = 0 - fmt.Print(prompt) - s.restartPrompt() - case ctrlH, bs: // Backspace - if pos <= 0 { - s.doBeep() - } else { - n := len(getSuffixGlyphs(line[:pos], 1)) - line = append(line[:pos-n], line[pos:]...) - pos -= n + case ctrlW: // Erase word + pos, line, killAction = s.eraseWord(pos, line, killAction) + case ctrlY: // Paste from Yank buffer + line, pos, next, err = s.yank(p, line, pos) + goto haveNext + case ctrlR: // Reverse Search + line, pos, next, err = s.reverseISearch(line, pos) s.needRefresh = true - } - case ctrlU: // Erase line before cursor - if killAction > 0 { - s.addToKillRing(line[:pos], 2) // Add in prepend mode - } else { - s.addToKillRing(line[:pos], 0) // Add in normal mode - } - - killAction = 2 // Mark that there was some killing - line = line[pos:] - pos = 0 - s.needRefresh = true - case ctrlW: // Erase word - pos, line, killAction = s.eraseWord(pos, line, killAction) - case ctrlY: // Paste from Yank buffer - line, pos, next, err = s.yank(p, line, pos) - goto haveNext - case ctrlR: // Reverse Search - line, pos, next, err = s.reverseISearch(line, pos) - s.needRefresh = true - goto haveNext - case tab: // Tab completion - line, pos, next, err = s.tabComplete(p, line, pos) - goto haveNext - // Catch keys that do nothing, but you don't want them to beep - case esc: - // DO NOTHING - // Unused keys - case ctrlG: - // JM experimenting 20200108 - //for _, l := range codelines { - // MoveCursorDown(2) - // fmt.Print(l) - // } - //MoveCursorDown(len(codelines)) - return "", ErrJMCodeUp - //line = []rune(codelines[len(codelines)-1]) - //pos = len(line) - s.needRefresh = true - case ctrlS, ctrlO, ctrlQ, ctrlV, ctrlX, ctrlZ: - fallthrough - // Catch unhandled control codes (anything <= 31) - case 0, 28, 29, 30, 31: - s.doBeep() - default: - if pos == len(line) && !s.multiLineMode && - len(p)+len(line) < s.columns*4 && // Avoid countGlyphs on large lines - countGlyphs(p)+countGlyphs(line) < s.columns-1 { - line = append(line, v) - fmt.Printf("%c", v) - s.needRefresh = true // JM --- - pos++ - - } else { - line = append(line[:pos], append([]rune{v}, line[pos:]...)...) - pos++ + goto haveNext + case tab: // Tab completion + line, pos, next, err = s.tabComplete(p, line, pos) + goto haveNext + // Catch keys that do nothing, but you don't want them to beep + case esc: + // DO NOTHING + // Unused keys + case ctrlG: + // JM experimenting 20200108 + //for _, l := range codelines { + // MoveCursorDown(2) + // fmt.Print(l) + // } + //MoveCursorDown(len(codelines)) + return "", ErrJMCodeUp + //line = []rune(codelines[len(codelines)-1]) + //pos = len(line) s.needRefresh = true - } - if s_instr == 2 && string(v) == "\"" { - s_instr = 0 - } - } - case action: - switch v { - case del: - if pos >= len(line) { - s.doBeep() - } else { - n := len(getPrefixGlyphs(line[pos:], 1)) - line = append(line[:pos], line[pos+n:]...) - } - case left: - if pos > 0 { - pos -= len(getSuffixGlyphs(line[:pos], 1)) - } else { + case ctrlS, ctrlO, ctrlQ, ctrlV, ctrlX, ctrlZ: + fallthrough + // Catch unhandled control codes (anything <= 31) + case 0, 28, 29, 30, 31: s.doBeep() - } - case wordLeft, altB: - if pos > 0 { - var spaceHere, spaceLeft, leftKnown bool - for { - pos-- - if pos == 0 { - break - } - if leftKnown { - spaceHere = spaceLeft - } else { - spaceHere = unicode.IsSpace(line[pos]) - } - spaceLeft, leftKnown = unicode.IsSpace(line[pos-1]), true - if !spaceHere && spaceLeft { - break - } + default: + if pos == len(line) && !s.multiLineMode && + len(p)+len(line) < s.columns*4 && // Avoid countGlyphs on large lines + countGlyphs(p)+countGlyphs(line) < s.columns-1 { + line = append(line, v) + fmt.Printf("%c", v) + s.needRefresh = true // JM --- + pos++ + + } else { + line = append(line[:pos], append([]rune{v}, line[pos:]...)...) + pos++ + s.needRefresh = true + } + if s_instr == 2 && string(v) == "\"" { + s_instr = 0 } - } else { - s.doBeep() - } - case right: - if pos < len(line) { - pos += len(getPrefixGlyphs(line[pos:], 1)) - } else { - s.doBeep() } - case wordRight, altF: - if pos < len(line) { - var spaceHere, spaceLeft, hereKnown bool - for { - pos++ - if pos == len(line) { - break + case action: + switch v { + case del: + if pos >= len(line) { + s.doBeep() + } else { + n := len(getPrefixGlyphs(line[pos:], 1)) + line = append(line[:pos], line[pos+n:]...) + } + case left: + if pos > 0 { + pos -= len(getSuffixGlyphs(line[:pos], 1)) + } else { + s.doBeep() + } + case wordLeft, altB: + if pos > 0 { + var spaceHere, spaceLeft, leftKnown bool + for { + pos-- + if pos == 0 { + break + } + if leftKnown { + spaceHere = spaceLeft + } else { + spaceHere = unicode.IsSpace(line[pos]) + } + spaceLeft, leftKnown = unicode.IsSpace(line[pos-1]), true + if !spaceHere && spaceLeft { + break + } } - if hereKnown { - spaceLeft = spaceHere - } else { - spaceLeft = unicode.IsSpace(line[pos-1]) + } else { + s.doBeep() + } + case right: + if pos < len(line) { + pos += len(getPrefixGlyphs(line[pos:], 1)) + } else { + s.doBeep() + } + case wordRight, altF: + if pos < len(line) { + var spaceHere, spaceLeft, hereKnown bool + for { + pos++ + if pos == len(line) { + break + } + if hereKnown { + spaceLeft = spaceHere + } else { + spaceLeft = unicode.IsSpace(line[pos-1]) + } + spaceHere, hereKnown = unicode.IsSpace(line[pos]), true + if spaceHere && !spaceLeft { + break + } } - spaceHere, hereKnown = unicode.IsSpace(line[pos]), true - if spaceHere && !spaceLeft { - break + } else { + s.doBeep() + } + case up: + historyAction = true + if historyStale { + historyPrefix = s.getHistoryByPrefix(string(line)) + historyPos = len(historyPrefix) + historyStale = false + } + if historyPos > 0 { + if historyPos == len(historyPrefix) { + historyEnd = string(line) } + historyPos-- + line = []rune(historyPrefix[historyPos]) + pos = len(line) + } else { + s.doBeep() } - } else { - s.doBeep() - } - case up: - historyAction = true - if historyStale { - historyPrefix = s.getHistoryByPrefix(string(line)) - historyPos = len(historyPrefix) - historyStale = false - } - if historyPos > 0 { - if historyPos == len(historyPrefix) { - historyEnd = string(line) + case down: + historyAction = true + if historyStale { + historyPrefix = s.getHistoryByPrefix(string(line)) + historyPos = len(historyPrefix) + historyStale = false } - historyPos-- - line = []rune(historyPrefix[historyPos]) - pos = len(line) - } else { - s.doBeep() - } - case down: - historyAction = true - if historyStale { - historyPrefix = s.getHistoryByPrefix(string(line)) - historyPos = len(historyPrefix) - historyStale = false - } - if historyPos < len(historyPrefix) { - historyPos++ - if historyPos == len(historyPrefix) { - line = []rune(historyEnd) + if historyPos < len(historyPrefix) { + historyPos++ + if historyPos == len(historyPrefix) { + line = []rune(historyEnd) + } else { + line = []rune(historyPrefix[historyPos]) + } + pos = len(line) } else { - line = []rune(historyPrefix[historyPos]) + s.doBeep() } + case home: // Start of line + pos = 0 + case end: // End of line pos = len(line) - } else { - s.doBeep() - } - case home: // Start of line - pos = 0 - case end: // End of line - pos = len(line) - case altD: // Delete next word - if pos == len(line) { - s.doBeep() - break - } - // Remove whitespace to the right - var buf []rune // Store the deleted chars in a buffer - for { - if pos == len(line) || !unicode.IsSpace(line[pos]) { + case altD: // Delete next word + if pos == len(line) { + s.doBeep() break } - buf = append(buf, line[pos]) - line = append(line[:pos], line[pos+1:]...) - } - // Remove non-whitespace to the right - for { - if pos == len(line) || unicode.IsSpace(line[pos]) { - break + // Remove whitespace to the right + var buf []rune // Store the deleted chars in a buffer + for { + if pos == len(line) || !unicode.IsSpace(line[pos]) { + break + } + buf = append(buf, line[pos]) + line = append(line[:pos], line[pos+1:]...) } - buf = append(buf, line[pos]) - line = append(line[:pos], line[pos+1:]...) - } - // Save the result on the killRing - if killAction > 0 { - s.addToKillRing(buf, 2) // Add in prepend mode - } else { - s.addToKillRing(buf, 0) // Add in normal mode - } - killAction = 2 // Mark that there was some killing - case altBs: // Erase word - pos, line, killAction = s.eraseWord(pos, line, killAction) - case winch: // Window change - if s.multiLineMode { - if s.maxRows-s.cursorRows > 0 { - s.moveDown(s.maxRows - s.cursorRows) + // Remove non-whitespace to the right + for { + if pos == len(line) || unicode.IsSpace(line[pos]) { + break + } + buf = append(buf, line[pos]) + line = append(line[:pos], line[pos+1:]...) + } + // Save the result on the killRing + if killAction > 0 { + s.addToKillRing(buf, 2) // Add in prepend mode + } else { + s.addToKillRing(buf, 0) // Add in normal mode } - for i := 0; i < s.maxRows-1; i++ { - s.cursorPos(0) - s.eraseLine() - s.moveUp(1) + killAction = 2 // Mark that there was some killing + case altBs: // Erase word + pos, line, killAction = s.eraseWord(pos, line, killAction) + case winch: // Window change + if s.multiLineMode { + if s.maxRows-s.cursorRows > 0 { + s.moveDown(s.maxRows - s.cursorRows) + } + for i := 0; i < s.maxRows-1; i++ { + s.cursorPos(0) + s.eraseLine() + s.moveUp(1) + } + s.maxRows = 1 + s.cursorRows = 1 } - s.maxRows = 1 - s.cursorRows = 1 } + s.needRefresh = true + } */ + //if true || s.needRefresh { //&& !s.inputWaiting() { + // ALWAYS REFRESH SO WE HAVE JUST ONE TRUTH + err := s.refresh(p, line, pos) + if err != nil { + return "", err } - s.needRefresh = true - } */ - //if true || s.needRefresh { //&& !s.inputWaiting() { - // ALWAYS REFRESH SO WE HAVE JUST ONE TRUTH - err := s.refresh(p, line, pos) - if err != nil { - return "", err - } - // } else { - ///// s.cursorPos(pLen + pos) - // } - /*if !historyAction { - historyStale = true + // } else { + ///// s.cursorPos(pLen + pos) + // } + /*if !historyAction { + historyStale = true + } + if killAction > 0 { + killAction-- + }*/ } - if killAction > 0 { - killAction-- - }*/ } // return string(line), nil } From c83b286804863763919cfa695b05750578a7dd2e Mon Sep 17 00:00:00 2001 From: refaktor Date: Sat, 10 Aug 2024 03:23:38 +0200 Subject: [PATCH 6/7] golangci fix --- evaldo/repl.go | 6 ++++-- util/microliner.go | 1 - 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/evaldo/repl.go b/evaldo/repl.go index 28ce565e..d94b1229 100644 --- a/evaldo/repl.go +++ b/evaldo/repl.go @@ -356,7 +356,10 @@ func DoRyeRepl(es *env.ProgramState, dialect string, showResults bool) { // here if k == keyboard.KeyCtrlC { // fmt.Println("Ctrl C 1") cancel() - syscall.Kill(os.Getpid(), syscall.SIGINT) + err1 := syscall.Kill(os.Getpid(), syscall.SIGINT) + if err1 != nil { + fmt.Println(err.Error()) // TODO -- temprorary just printed + } //ctx.Done() // fmt.Println("") // return @@ -374,7 +377,6 @@ func DoRyeRepl(es *env.ProgramState, dialect string, showResults bool) { // here fmt.Println(err) } // fmt.Println("END") - } /* THIS WAS DISABLED TEMP FOR WASM MODE .. 20250116 func DoGeneralInput(es *env.ProgramState, prompt string) { diff --git a/util/microliner.go b/util/microliner.go index 2b4885fb..dce127b5 100644 --- a/util/microliner.go +++ b/util/microliner.go @@ -543,7 +543,6 @@ startOfHere: // mainLoop: for { - select { case <-ctx1.Done(): // fmt.Println("Exitin due to coancelation") From 4b775e53e20b68e607f95fe3ea22a80b74bd6f71 Mon Sep 17 00:00:00 2001 From: refaktor Date: Sat, 10 Aug 2024 23:52:41 +0200 Subject: [PATCH 7/7] trying to make it compile on windows. syscall,Kill is missing on windows --- evaldo/repl.go | 4 ++-- util/os_windows.go | 15 +++++++++++++++ util/os_windows_not.go | 10 ++++++++++ 3 files changed, 27 insertions(+), 2 deletions(-) create mode 100644 util/os_windows.go create mode 100644 util/os_windows_not.go diff --git a/evaldo/repl.go b/evaldo/repl.go index d94b1229..88a44736 100644 --- a/evaldo/repl.go +++ b/evaldo/repl.go @@ -10,7 +10,6 @@ import ( "path/filepath" "regexp" "strings" - "syscall" "github.com/eiannone/keyboard" @@ -356,7 +355,8 @@ func DoRyeRepl(es *env.ProgramState, dialect string, showResults bool) { // here if k == keyboard.KeyCtrlC { // fmt.Println("Ctrl C 1") cancel() - err1 := syscall.Kill(os.Getpid(), syscall.SIGINT) + err1 := util.KillProcess(os.Getpid()) + // err1 := syscall.Kill(os.Getpid(), syscall.SIGINT) if err1 != nil { fmt.Println(err.Error()) // TODO -- temprorary just printed } diff --git a/util/os_windows.go b/util/os_windows.go new file mode 100644 index 00000000..32757d10 --- /dev/null +++ b/util/os_windows.go @@ -0,0 +1,15 @@ +//go:build windows +// +build windows + +package util + +import "syscall" + +func KillProcess(pid int) error { + handle, err := syscall.OpenProcess(syscall.PROCESS_TERMINATE, false, uint32(pid)) + if err != nil { + return err + } + defer syscall.CloseHandle(handle) + return syscall.TerminateProcess(handle, 0) +} diff --git a/util/os_windows_not.go b/util/os_windows_not.go new file mode 100644 index 00000000..ab320c0d --- /dev/null +++ b/util/os_windows_not.go @@ -0,0 +1,10 @@ +//go:build !windows +// +build !windows + +package util + +import "syscall" + +func KillProcess(pid int) error { + return syscall.Kill(pid, syscall.SIGTERM) +}