Skip to content

Instantly share code, notes, and snippets.

@toomasv
Last active June 26, 2019 03:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save toomasv/bdc164798f48ed2287c67eddabf7e04f to your computer and use it in GitHub Desktop.
Save toomasv/bdc164798f48ed2287c67eddabf7e04f to your computer and use it in GitHub Desktop.
@GiuseppeChillemi's path dialect
Red [
Description: {Attempt to implement @GiuseppeChillemi's "path dialect"}
Date: 22-Jun-2019
Author: "Toomas Vooglaid"
]
system/lexer/pre-load: func [src][
chr: complement charset {/[ "#}
path: [some chr #"/" some chr]
mpath: [path change #"[" "/(" to #"]" change skip #")" any [#"/" mpath]]
parse src [any [s: mpath e: (insert s "mget " e: skip e 5) :e | skip]]
]
mget: function ['p][
switch/default type?/word p [
path! [
either found: find p paren! [
p: get copy/part p found
either any-function? :p [
either empty? f: first found [p][
f: to-block f
blk: do compose [(:p) (f)]
either 1 < length? found [
remove found
p: select blk found
mget :p
][blk]
]
][
par: take found
if not empty? par [
switch type?/word r: first par [
word! [
case [
path? p [p: back insert copy p r]
word? p [p: to-path reduce [r p]]
]
]
path! [p: append r p]
]
]
if 0 < length? found [
case [
path? p [p: append copy p found]
word? p [p: back insert copy found p]
]
]
mget :p
]
][;[get :p]
w: system/words
forall p [
w: select w p/1
if all [
find [path! word!] type?/word w
#">" = first frm: form w
][
probe w: get load next frm
if all [
find [path! word!] type?/word w
#">" = first probe frm: form w
][probe w: load next frm mget :w]
]
]
w
]
]
word! [
get :p
]
][:p]
]
; Examples with pre-load func
comment [
do %mget.red
y: [i b c xx d] xx: [h j k]
y/c[]
;== [h j k]
write %tmp [data "my string" value 22]
change find/tail y 'c func [][read %tmp]
y/c[]
;== {[data "my string" value 22]}
change find/tail y 'c func [f][load f]
y/c[%tmp]
;== [data "my string" value 22]
y/c[%tmp]/data
;== "my string"
y: [i b c j/delta d] xx: [h j [delta 22] k]
y/c[xx]
;== 22
y: [i b c j/delta d] xx: [h j [delta [gamma 33]] k]
y/c[xx]/gamma
;== 33
]
;Examples with pre-load removed
comment [
do %mget.red
y: [i b c xx d] xx: [h j k]
mget y/c/()
;== [h j k]
write %tmp [data "my string" value 22]
change find/tail y 'c func [][read %tmp]
mget y/c/()
;== {[data "my string" value 22]}
change find/tail y 'c func [f][load f]
mget y/c/(%tmp)
;== [data "my string" value 22]
mget y/c/(%tmp)/data
;== "my string"
y: [i b c j/delta d] xx: [h j [delta 22] k]
mget y/c/(xx)
;== 22
y: [i b c j/delta d] xx: [h j [delta [gamma 33]] k]
mget y/c/(xx)/gamma
;== 33
]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment