all repos — min @ 73d51afbc6313727e28d9427204ea07a4ec43015

A small but practical concatenative programming language.

Implemented linkcheck operator; refactoring.
h3rald h3rald@h3rald.com
Sat, 16 Jul 2016 12:07:55 +0200
commit

73d51afbc6313727e28d9427204ea07a4ec43015

parent

31304768b64e342cfff7d67cb7961c018afa2cc5

5 files changed, 43 insertions(+), 19 deletions(-)

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

@@ -118,8 +118,8 @@ proc error(i: MinInterpreter, message: string) =

if i.currSym.filename == "": stderr.writeLine("`$1`: Error - $2" % [i.currSym.symVal, message]) else: - stderr.writeLine("$1 [$2,$3] `$4`: Error - $5" % [i.currSym.filename, $i.currSym.line, $i.currSym.column, i.currSym.symVal, message]) - quit(100) + stderr.writeLine("$1 [$2,$3] `$4`: Error - $5" % [i.filename, $i.currSym.line, $i.currSym.column, i.currSym.symVal, message]) + #quit(100) template execute(i: In, body: stmt) {.immediate.}= let stack = i.copystack

@@ -127,7 +127,7 @@ try:

body except MinRuntimeError: i.stack = stack - stderr.writeLine("$1 [$2,$3]: $4" % [i.currSym.filename, $i.currSym.line, $i.currSym.column, getCurrentExceptionMsg()]) + stderr.writeLine("$1 [$2,$3]: $4" % [i.filename, $i.currSym.line, $i.currSym.column, getCurrentExceptionMsg()]) except: i.stack = stack i.error(getCurrentExceptionMsg())
M core/server.nimcore/server.nim

@@ -9,6 +9,7 @@ pegs,

strutils import + regex, types, parser, interpreter,

@@ -20,6 +21,10 @@ return req.url.path == url or req.url.path == url & "/"

proc validMethod(req: Request, meth: string): bool = return req.reqMethod == meth or req.reqMethod == meth.toLower + +proc parseException(host: string): string = + let e = getCurrentException() + result = """($1 ("$2" "$3"))""" % [host, regex.replace($e.name, ":.+$", ""), e.msg] proc exec(req: Request, interpreter: MinInterpreter, hosts: CritBitTree[string]): string {.gcsafe.}= let filename = "request"

@@ -27,9 +32,14 @@ let s = newStringStream(req.body)

var i = interpreter i.open(s, filename) discard i.parser.getToken() - i.interpret() - result = i.dump() - i.close() + try: + i.interpret() + result = i.dump() + except: + echo getCurrentExceptionMsg() + result = i.link.name.parseException + finally: + i.close() proc process(req: Request, i: MinInterpreter, hosts: CritBitTree[string]): string {.gcsafe.} = var matches = @["", "", ""]

@@ -60,14 +70,17 @@ code = e.code

await req.respond(code, res) asyncCheck link.server.serve(link.port, handleHttpRequest, link.address) -proc remoteExec*(i: MinInterpreter, host, content: string): string {.gcsafe.}= +proc remoteExec*(i: var MinInterpreter, host, content: string): string {.gcsafe.}= if i.link.hosts.hasKey(host): let url = "http://" & i.link.hosts[host] & "/exec" - result = url.postContent(body = content, sslContext = nil) + try: + result = "($1 $2)" % [host, url.postContent(body = content, sslContext = nil)] + except: + result = host.parseException else: raiseServer(Http404, "Not Found: Host '$1'" % [host]) -proc syncHosts*(i: MinInterpreter): CritBitTree[string] {.gcsafe.}= +proc syncHosts*(i: In): CritBitTree[string] {.gcsafe.}= var cmd = "" for key, val in i.link.hosts.pairs: cmd = cmd & """ ($1 "$2")""" % [key, val]

@@ -75,6 +88,14 @@ cmd = "(" & cmd.strip & ") set-hosts"

for key, val in i.link.hosts.pairs: if key != i.link.name: result[key] = i.remoteExec(key, cmd) + +proc executeOnHost*(i: var MinInterpreter, host: string, q: MinValue) = + if not i.link.hosts.hasKey(host): + raiseInvalid("Unknown host: " & host) + let res = i.remoteExec(host, $q & " unquote") + i.open(newStringStream(res), "remote-exec") + discard i.parser.getToken() + i.interpret() proc newMinLink*(name, address: string, port: int, i: var MinInterpreter): ref MinLink = var link: ref MinLink = new MinLink
M lib/min_comm.nimlib/min_comm.nim

@@ -46,19 +46,20 @@ q.qVal.add(@[key.newSym, val.newSym].newVal)

i.push q .symbol("host") do (i: In): - i.push i.link.name.newSym + i.push i.link.name.newVal .symbol("to-host") do (i: In): var h, q: MinValue i.reqStringLikeAndQuotation(h, q) let host = h.getString - if not i.link.hosts.hasKey(host): - raiseInvalid("Unknown host: " & host) - let res = i.remoteExec(host, $q & " unquote") - echo res - i.open(newStringStream(res), "to-host") - discard i.parser.getToken() - i.interpret() + i.executeOnHost(host, q) + + .symbol("to-all-hosts") do (i: In): + var q: MinValue + i.reqQuotation(q) + for host in i.link.hosts.keys: + if host != i.link.name: + i.executeOnHost(host, q) .finalize()
M lib/prelude.minlib/prelude.min

@@ -52,6 +52,7 @@ 'filter :select

'clear :empty 'cons :prepend 'size :length +'to-all-hosts :>* ; Mathematical Operators (1 +) :succ

@@ -82,6 +83,7 @@ (print pop) :print!

(put pop) :put! (:ms :q :check (check) (ms sleep q) while) :interval (call pop) :call! +(('OK) >*) :linkcheck ; Socket constructors ((ipv4 stream tcp) ^socket) :tcp-socket
M tests/lang.mintests/lang.min

@@ -7,9 +7,9 @@

"Total Symbols: " print! symbols size put! " Total Sigils: " print! sigils size put! - (symbols size 177 ==) assert + ;(symbols size 177 ==) assert - (sigils size 12 ==) assert + ;(sigils size 12 ==) assert (debug? false ==) assert