all repos — min @ 8f9bb0a462dae9300f337e0ead2bc6170a6b9825

A small but practical concatenative programming language.

Added support for sgregex library.
h3rald h3rald@h3rald.com
Sat, 28 May 2016 22:48:06 +0200
commit

8f9bb0a462dae9300f337e0ead2bc6170a6b9825

parent

a0598c9364e04d2c6691d8620e379fe7e82a9562

2 files changed, 34 insertions(+), 11 deletions(-)

jump to
M core/regex.nimcore/regex.nim

@@ -33,6 +33,13 @@

proc replace*(str, pattern, repl: string): string = return replace(str, pattern, repl, "") +proc `=~`*(str, r: string): seq[string] = + let m = r.search("(s)?/(.+?)?/(.+?)?/([mis]{0,3})") + if m[1] == "s" and m[3] != "": + return @[replace(str, m[2], m[3], m[4])] + else: + return search(str, m[2], m[4]) + when isMainModule: proc tmatch(str, pattern: string) =

@@ -47,6 +54,8 @@

proc treplace(str, pattern, repl: string) = echo str, " =~ ", "s/", pattern, "/", repl, "/", " -> ", str.replace(pattern, repl) + proc toperator(str, pattern: string) = + echo str, " =~ ", pattern, " -> ", str =~ pattern "HELLO".tmatch("^H(.*)O$") "HELLO".tmatch("^H(.*)S$")

@@ -57,4 +66,6 @@ "127.0.0.1".treplace("^([0-9]{1,3})\\.([0-9]{1,3})\\.([0-9]{1,3})\\.([0-9]{1,3})$", "$4.$3.$1.$2")

"127.0.0.1".treplace("[0-9]+", "255") "Hello".tsearch("HELLO", "i") "Hello\nWorld!".tsearch("HELLO.WORLD", "mis") + "Testing".toperator("s/test/eat/i") +
M lib/str.nimlib/str.nim

@@ -1,10 +1,10 @@

import tables, strutils -import ../vendor/slre import ../core/types, ../core/parser, ../core/interpreter, - ../core/utils + ../core/utils, + ../core/regex define("str")

@@ -17,24 +17,23 @@ i.push e.newVal

else: i.error errIncorrect, "Two strings are required on the stack" - .symbol("match") do (i: In): + .symbol("search") do (i: In): let reg = i.pop let str = i.pop if str.isString and reg.isString: - var matches = str.strVal.match(reg.strVal) - var res = newSeq[MinValue](0) - for s in matches: - res.add s.newVal + var matches = str.strVal.search(reg.strVal) + var res = newSeq[MinValue](matches.len) + for i in 0..matches.len-1: + res[i] = matches[i].newVal i.push res.newVal else: i.error(errIncorrect, "Two strings are required on the stack") - .symbol("match?") do (i: In): + .symbol("match") do (i: In): let reg = i.pop let str = i.pop if str.isString and reg.isString: - var matches = str.strVal.match(reg.strVal) - if matches.len > 0: + if str.strVal.match(reg.strVal): i.push true.newVal else: i.push false.newVal

@@ -46,8 +45,21 @@ let s_replace = i.pop

let reg = i.pop let s_find = i.pop if reg.isString and s_replace.isString and s_find.isString: - i.push s_find.strVal.gsub(reg.strVal, s_replace.strVal).newVal + i.push regex.replace(s_find.strVal, reg.strVal, s_replace.strVal).newVal else: i.error(errIncorrect, "Three strings are required on the stack") + + .symbol("=~") do (i: In): + let reg = i.pop + let str = i.pop + if str.isString and reg.isString: + let results = str.strVal =~ reg.strVal + var res = newSeq[MinValue](0) + for r in results: + res.add(r.newVal) + i.push res.newVal + else: + i.error(errIncorrect, "Two strings are required on the stack") + .finalize()