all repos — min @ 357efcbe09d7585c9fd84dd7063387dd046b80ca

A small but practical concatenative programming language.

Re-implemented net module using new object API.
h3rald h3rald@h3rald.com
Fri, 24 Jun 2016 20:16:46 +0200
commit

357efcbe09d7585c9fd84dd7063387dd046b80ca

parent

b697fc22b09e22f2ddb54bcbd920a76fdd598c9d

3 files changed, 57 insertions(+), 72 deletions(-)

jump to
M lib/min_lang.nimlib/min_lang.nim

@@ -155,7 +155,6 @@ raiseUndefined("Symbol '$1' not found in scope '$2'" % [s, i.scope.fullname])

# Restore original quotation sProc(i) i.scope = origScope - i.push @[q] #var symbols, target: MinValue #i.reqTwoQuotations symbols, target
M lib/min_net.nimlib/min_net.nim

@@ -17,12 +17,15 @@ i.reqObject "socket", q

q.body(i) socketSymbol("domain") do (q: Val, i: In): + i.push q i.push q.qVal[0].symVal.newVal socketSymbol("type") do (q: Val, i: In): + i.push q i.push q.qVal[1].symVal.newVal socketSymbol("protocol") do (q: Val, i: In): + i.push q i.push q.qVal[2].symVal.newVal socketSymbol("close") do (q: Val, i: In):

@@ -40,6 +43,51 @@ i.push "0.0.0.0".newVal

q.scope.symbols["port"] = proc (i:In) = i.push port socket.listen() + i.push q + +socketSymbol("accept") do (q: Val, i: In): + # Open same socket type as server + i.eval "$1 net %^socket" % [$q.qVal[0..2].newVal] + var clientVal: MinValue + i.reqObject "socket", clientVal + var client = clientVal.to(Socket) + var address = "" + q.to(Socket).acceptAddr(client, address) + clientVal.qVal.add address.newSym + i.push clientVal + +socketSymbol("connect") do (q: Val, i: In): + var q, address, port: MinValue + i.reqInt port + i.reqString address + q.to(Socket).connect(address.strVal, Port(port.intVal)) + q.qVal.add address.strVal.newSym + q.qVal.add port + q.scope.symbols["client-address"] = proc (i:In) = + i.push address.strVal.newVal + q.scope.symbols["client-port"] = proc (i:In) = + i.push port + i.push q + +socketSymbol("send") do (q: Val, i: In): + var s: MinValue + i.reqString s + q.to(Socket).send s.strVal + i.push q + +socketSymbol("recv") do (q: Val, i: In): + var size: MinValue + i.reqInt size + var s = "" + discard q.to(Socket).recv(s, size.intVal.int) + i.push q + i.push s.newVal + +socketSymbol("recv-line") do (q: Val, i: In): + var s = "" + q.to(Socket).readLine(s) + i.push @[q] + i.push s.newVal define("net")

@@ -82,73 +130,11 @@ q.scope.symbols["type"] = symbols["type"](q, i)

q.scope.symbols["protocol"] = symbols["protocol"](q, i) q.scope.symbols["close"] = symbols["close"](q, i) q.scope.symbols["listen"] = symbols["listen"](q, i) - i.push @[q] - - #.symbol("listen") do (i: In): - # var port, q: MinValue - # i.reqObject "socket", q - # i.reqInt port - # var socket = q.to(Socket) - # socket.bindAddr(Port(port.intVal)) - # q.qVal.add "0.0.0.0".newSym - # q.qVal.add port - # q.scope.symbols["address"] = proc (i:In) = - # i.push "0.0.0.0".newVal - # q.scope.symbols["port"] = proc (i:In) = - # i.push port - # socket.listen() - # i.push @[q] - - .symbol("accept") do (i: In): - var server: MinValue - i.reqObject "socket", server - # Open same socket type as server - i.eval "$1 net %open-socket" % [$server.qVal[0..2].newVal] - var clientVal: MinValue - i.reqObject "socket", clientVal - var client = clientVal.to(Socket) - var address = "" - server.to(Socket).acceptAddr(client, address) - clientVal.qVal.add address.newSym - i.push @[clientVal] - - .symbol("connect") do (i: In): - var q, address, port: MinValue - i.reqInt port - i.reqString address - i.reqObject "socket", q - q.to(Socket).connect(address.strVal, Port(port.intVal)) - q.qVal.add address.strVal.newSym - q.qVal.add port - q.scope.symbols["client-address"] = proc (i:In) = - i.push address.strVal.newVal - q.scope.symbols["client-port"] = proc (i:In) = - i.push port - i.push @[q] - - .symbol("send") do (i: In): - var q, s: MinValue - i.reqString s - i.reqObject "socket", q - q.to(Socket).send s.strVal - i.push @[q] - - - .symbol("recv") do (i: In): - var size, q: MinValue - i.reqInt size - i.reqObject "socket", q - var s = "" - discard q.to(Socket).recv(s, size.intVal.int) - i.push @[q] - i.push s.newVal - - .symbol("recv-line") do (i: In): - var q: MinValue - i.reqObject "socket", q - var s = "" - q.to(Socket).readLine(s) - i.push @[q] - i.push s.newVal + q.scope.symbols["accept"] = symbols["accept"](q, i) + q.scope.symbols["connect"] = symbols["connect"](q, i) + q.scope.symbols["send"] = symbols["send"](q, i) + q.scope.symbols["recv"] = symbols["recv"](q, i) + q.scope.symbols["recv-line"] = symbols["recv-line"](q, i) + i.push q .finalize()
M tests/lang.mintests/lang.min

@@ -3,7 +3,7 @@ #test

"lang" describe - (symbols size 181 ==) assert + (symbols size put 175 ==) assert (sigils size 10 ==) assert

@@ -34,7 +34,7 @@ ; Extend an existing scope

(defmod (#mymath (-) :myminus) => .mymath - 5 2 mymath %myminus pop 3 ==) assert + 5 2 mymath %myminus 3 ==) assert (defmod mymath inspect ("myplus") ==) assert

@@ -45,7 +45,7 @@

("2 2 +" "tests/testload.min" fwrite @testload 4 ==) assert "tests/testload.min" rm - (defmod 2 2 mymath %myplus pop 4 ==) assert + (defmod 2 2 mymath %myplus 4 ==) assert (1 2 3 4 getstack (1 2 3 4) ==) assert