all repos — min @ e0184c18c1f51c3ad49624641bc8bc31f385ce01

A small but practical concatenative programming language.

Removed comm module and server support.
h3rald h3rald@h3rald.com
Wed, 24 Aug 2016 21:05:00 +0200
commit

e0184c18c1f51c3ad49624641bc8bc31f385ce01

parent

6b09ec7405d5cc3332a67e51c5a3e56533b8ec5c

6 files changed, 4 insertions(+), 250 deletions(-)

jump to
D core/server.nim

@@ -1,121 +0,0 @@

-import - net, - asynchttpserver, - asyncdispatch, - httpclient, - streams, - critbits, - pegs, - strutils - -import - regex, - types, - parser, - interpreter, - utils - - -proc validUrl(req: Request, url: string): bool = - 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" - let s = newStringStream(req.body) - var i = interpreter - i.open(s, filename) - discard i.parser.getToken() - 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 = @["", "", ""] - template route(req, peg: expr, op: stmt): stmt {.immediate.}= - if req.url.path.find(peg, matches) != -1: - op - req.route peg"^\/?$": - if not req.validMethod("GET"): - raiseServer(Http405, "Method Not Allowed: " & req.reqMethod) - return "MiNiM Host '$1'" % [i.link.name] - req.route peg"^\/exec\/?$": - if not req.validMethod("POST"): - raiseServer(Http405, "Method Not Allowed: " & req.reqMethod) - return exec(req, i, hosts) - raiseServer(Http400, "Bad Request: POST "& req.url.path) - - -proc init*(link: ref MinLink) {.thread.} = - proc handleHttpRequest(req: Request): Future[void] {.async.} = - var res: string - var code: HttpCode = Http200 - try: - res = req.process(link.interpreter, link.hosts) - except MinServerError: - let e: MinServerError = (MinServerError)getCurrentException() - res = e.msg - code = e.code - await req.respond(code, res) - asyncCheck link.server.serve(link.port, handleHttpRequest, link.address) - -proc remoteExec*(i: var MinInterpreter, host, content: string): string {.gcsafe.}= - if i.link.hosts.hasKey(host): - let url = "http://" & i.link.hosts[host] & "/exec" - 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: In): CritBitTree[string] {.gcsafe.}= - var cmd = "" - for key, val in i.link.hosts.pairs: - cmd = cmd & """ ($1 "$2")""" % [key, val] - cmd = "(" & cmd.strip & ") comm %set-hosts" - for key, val in i.link.hosts.pairs: - if key != i.link.name: - result[key] = i.remoteExec(key, cmd) - -proc checkHost*(link: ref MinLink) = - let host = link.address & ":" & $link.port - try: - link.hosts = cfgGet("hosts").critbit - except: - discard - var name: string - for key, value in link.hosts.pairs: - if host == value: - name = key - break - if not name.isNil: - link.hosts.excl name - link.hosts[link.name] = host - discard link.interpreter.syncHosts() - -proc executeOnHost*(i: var MinInterpreter, host: string, q: MinValue): string = - if not i.link.hosts.hasKey(host): - raiseInvalid("Unknown host: " & host) - return i.remoteExec(host, $q & " unquote") - -proc newMinLink*(name, address: string, port: int, i: var MinInterpreter): ref MinLink = - var link: ref MinLink = new MinLink - result = link - result.server = newAsyncHttpServer() - result.name = name - result.address = address - result.port = port.Port - i.link = result - result.interpreter = i
M core/types.nimcore/types.nim

@@ -78,14 +78,6 @@ filename*: string

debugging*: bool evaluating*: bool unsafe*: bool - link*: ref MinLink - MinLink* = object - hosts*: CritBitTree[string] - name*: string - address*: string - port*: Port - server*: AsyncHttpServer - interpreter*: MinInterpreter In* = var MinInterpreter Val* = var MinValue MinOperator* = proc (i: In) {.gcsafe, closure.}

@@ -95,8 +87,6 @@ MinUndefinedError* = ref object of ValueError

MinInvalidError* = ref object of ValueError MinEmptyStackError* = ref object of ValueError MinOutOfBoundsError* = ref object of ValueError - MinServerError* = ref object of SystemError - code*: HttpCode MinRuntimeError* = ref object of SystemError qVal*: seq[MinValue]
M core/utils.nimcore/utils.nim

@@ -81,9 +81,6 @@

proc raiseEmptyStack*() = raise MinEmptyStackError(msg: "Insufficient items on the stack") -proc raiseServer*(code: HttpCode, msg: string) = - raise MinServerError(msg: msg, code: code) - proc getString*(v: MinValue): string = if v.isSymbol: return v.symVal
D lib/min_comm.nim

@@ -1,72 +0,0 @@

-import strutils, critbits, streams -import - ../core/types, - ../core/parser, - ../core/interpreter, - ../core/utils, - ../core/server - -# I/O - - -proc comm_module*(i: In) = - i.define("comm") - - .symbol("reg") do (i: In): - var host, address: MinValue - i.reqTwoStringLike(host, address) - i.link.hosts[host.getString] = address.getString - for host, response in i.syncHosts().pairs: - echo host, ": ", response - cfgSet("hosts", %i.link.hosts) - - .symbol("unreg") do (i: In): - i.link.hosts.excl(i.link.name) - for host, response in i.syncHosts().pairs: - echo host, ": ", response - cfgSet("hosts", %i.link.hosts) - - .symbol("set-hosts") do (i: In): - var q: MinValue - i.reqQuotation(q) - for key, val in i.link.hosts.pairs: - i.link.hosts.excl(key) - for pair in q.qVal: - let vals = pair.qVal - if not pair.isQuotation or vals.len != 2 or not vals[0].isStringLike or not vals[1].isStringLike: - raiseInvalid("Invalid host quotation") - i.link.hosts.replace(vals[0].getString, vals[1].getString) - cfgSet("hosts", %i.link.hosts) - i.push(@["OK".newSym].newVal) - - .symbol("hosts") do (i: In): - var q = newSeq[MinValue](0).newVal - for key, val in i.link.hosts.pairs: - q.qVal.add(@[key.newSym, val.newSym].newVal) - i.push q - - .symbol("host") do (i: In): - i.push i.link.name.newVal - - .symbol("to-host") do (i: In): - var h, q: MinValue - i.reqStringLikeAndQuotation(h, q) - let host = h.getString - i.eval i.executeOnHost(host, q) - - .symbol("to-all-hosts") do (i: In): - var q: MinValue - i.reqQuotation(q) - var res = "" - for host in i.link.hosts.keys: - if host != i.link.name: - res = res & " " & i.executeOnHost(host, q) - i.eval("($1)" % res) - - .symbol("hostsync") do (i: In): - var res = "" - for key, value in i.syncHosts().pairs: - res = res & " " & value - i.eval "($1) dprint!" % res - - .finalize()
M lib/prelude.minlib/prelude.min

@@ -8,7 +8,6 @@ #num

#stack #sys #time -#comm ; Common sigils (bind) (.) sigil

@@ -20,7 +19,6 @@ (run) (&) sigil

(load) (@) sigil (call) (%) sigil (module) (=) sigil -(to-host) (>) sigil (dget) (/) sigil (ddel) (-) sigil

@@ -51,7 +49,6 @@ 'filter :select

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

@@ -83,4 +80,3 @@ (dprint pop) :dprint!

(put pop) :put! (:ms :q :check (check) (ms sleep q) while) :interval (call pop) :call! -(host print! ":" print! " (OK)" put! ('OK) >* dprint!) :hostcheck
M minim.nimminim.nim

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

-import streams, critbits, parseopt2, strutils, os, asyncdispatch +import streams, critbits, parseopt2, strutils, os import core/types, core/parser, core/interpreter, core/utils, - core/server, vendor/linenoise import lib/min_lang,

@@ -14,17 +13,11 @@ lib/min_str,

lib/min_logic, lib/min_time, lib/min_io, - lib/min_sys, - lib/min_comm + lib/min_sys const version* = "1.0.0-dev" var REPL = false var DEBUGGING = false -var PORT = 7500 -var ADDRESS = "0.0.0.0" -var SRVTHREAD: Thread[ref MinLink] -var SERVER = false -var HOSTNAME = "" const USE_LINENOISE = true

@@ -43,9 +36,6 @@ filename A minim file to interpret (default: STDIN).

Options: -e, --evaluate Evaluate a minim program inline -h, --help Print this help - -a, --address Specify server address (default: 0.0.0.0) - -p, --port Specify server port (default: 7500) - -s, --server Start server remote command execution -v, --version Print the program version -i, --interactive Start MiNiM's Read Eval Print Loop"""

@@ -61,6 +51,7 @@ sep = " "

for s in CURRSCOPE.symbols.keys: if startsWith(s, w): linenoiseAddCompletion completions, words.join(" ") & sep & s + proc prompt(s: string): string = var res = linenoise(s) discard $linenoiseHistoryAdd(res)

@@ -76,7 +67,6 @@ i.stack_module

i.str_module i.sys_module i.time_module - i.comm_module i.eval PRELUDE

@@ -144,15 +134,6 @@ of cmdArgument:

file = key of cmdLongOption, cmdShortOption: case key: - of "port", "p": - PORT = val.parseInt - of "address", "a": - if val.strip.len > 0: - ADDRESS = val - of "server", "s": - if val.strip.len > 0: - HOSTNAME = val - SERVER = true of "debug", "d": DEBUGGING = true of "evaluate", "e":

@@ -173,32 +154,15 @@

if not cfgfile().existsFile: cfgfile().writeFile("{}") -if REPL or SERVER: +if REPL: echo "MiNiM v"&version if s != "": minimString(s, DEBUGGING) elif file != "": minimFile file, DEBUGGING -elif SERVER: - var i = newMinInterpreter(DEBUGGING) - let host = ADDRESS & ":" & $PORT - if HOSTNAME == "": - HOSTNAME = host - var link = newMinLink(HOSTNAME, ADDRESS, PORT, i) - i.link.checkHost() - echo "Host '", HOSTNAME,"' started on ", HOSTNAME - proc srv(link: ref MinLink) = - link.init() - runForever() - createThread(SRVTHREAD, srv, link) - i.minimRepl elif REPL: minimRepl DEBUGGING quit(0) else: minimFile stdin, "stdin", DEBUGGING - -if SERVER: - joinThreads([SRVTHREAD]) -