2 ms·
Red's Parse dialect [1] (basically PEG on steroids, think OMeta [2]) has this kind of functionality build-in, but it's not limited to strings and can work with
by 9214 7y ago
Red's Parse dialect [1] (basically PEG on steroids, think OMeta [2]) has this kind of functionality build-in, but it's not limited to strings and can work with any series-like datatypes, including code itself. Consider:
; multi-line string
source: {
func main() {
fmt.Println("hello world")
}
}
parse source [thru "fmt.Println(" copy arguments to ")"]
foo: does [
print rejoin [{fmt.Println(fmt.Sprintf("Red says %s", } arguments "))"]
]
pattern: [set var skip '<= set rest skip]
; code (which is represented by heterogeneous data structure called "block")
example: [
if (width <= 1280 && height <= 800) [
return 1
]
]
parse example rule: [
some [
change pattern (random rejoin [var rest])
| ahead paren! into rule
| skip
]
]
bar: does [probe example]
Running foo and bar yields:
>> foo bar
fmt.Println(fmt.Sprintf("Red says %s", "hello world"))
[
if ("21t8w0dhi" && "tig8e0h0h") [
return 1
]
]
Of course, it's not as specialized and dovetailed to string rewriting as Comby (i.e. it's more general), but the trick is that you can build Comby-like template engine with Parse itself; this idea of building tools with linguistic abstraction is tied into Red's (and Rebol's) design: data-rich homoiconicity and idea of dialects (aka embeded DSLs) in general. Curious to see if Comby can offer similar functionality across multitude of languages and data formats.
[1]: https://www.red-lang.org/2013/11/041-introducing-parse.html https://www.red-lang.org/2013/11/041-introducing-parse.html
[2]: https://en.wikipedia.org/wiki/OMeta https://en.wikipedia.org/wiki/OMeta