all repos — min @ e75710f65e283494c4f422f4f90564560539abb4

A small but practical concatenative programming language.

Started (re-)implementing net module.
h3rald h3rald@h3rald.com
Sun, 19 Nov 2017 10:24:19 +0100
commit

e75710f65e283494c4f422f4f90564560539abb4

parent

e812b81589a9cad2fb762d4350ce9ecc370e18ee

3 files changed, 63 insertions(+), 0 deletions(-)

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

@@ -69,6 +69,8 @@ of minFloat: floatVal*: BiggestFloat

of minQuotation: qVal*: seq[MinValue] scope*: ref MinScope + obj*: pointer + objType*: string of minString: strVal*: string of minSymbol: symVal*: string of minBool: boolVal*: bool
A lib/min_net.nim

@@ -0,0 +1,59 @@

+import net, nativesockets +import + ../core/parser, + ../core/value, + ../core/interpreter, + ../core/utils + +# Time + + +proc net_module*(i: In)= + let def = i.define() + + def.symbol("socket") do (i: In): + let vals = i.expect "dict" + var q = vals[0] + # (ipv4 stream tcp) + if q.qVal.len < 3 or not (q.qVal[0].isSymbol and q.qVal[1].isSymbol and q.qVal[2].isSymbol): + raiseInvalid("Quotation must contain three symbols for <domain> <type> <protocol>") + let values = q.qVal + if not ["ipv4", "ipv6"].contains(values[0].symVal): + raiseInvalid("Domain symbol must be 'ipv4' or 'ipv6'") + if not ["stream", "dgram"].contains(values[1].symVal): + raiseInvalid("Type symbol must be 'stream' or 'dgram'") + if not ["tcp", "udp"].contains(values[2].symVal): + raiseInvalid("Protocol symbol must be 'tcp' or 'udp'") + var + domain: Domain + sockettype: SockType + protocol: Protocol + sDomain, sSockType, sProtocol: string + # Process domain + if values[0].symVal == "ipv4": + sDomain = "ipv4" + domain = AF_INET + else: + sDomain = "ipv6" + domain = AF_INET6 + if values[1].symVal == "stream": + sSockType = "stream" + sockettype = SOCK_STREAM + else: + sSockType = "dgram" + sockettype = SOCK_DGRAM + if values[2].symVal == "tcp": + sProtocol = "tcp" + protocol = IPPROTO_TCP + else: + sProtocol = "udp" + protocol = IPPROTO_UDP + var socket = newSocket(domain, sockettype, protocol) + var skt = newSeq[MinValue](0).newVal(i.scope) + i.dset(skt, "domain".newSym, sDomain.newVal) + i.dset(skt, "type".newSym, sSockType.newVal) + i.dset(skt, "protocol".newSym, sProtocol.newVal) + skt.objType = "socket" + skt.obj = socket[].addr + + def.finalize("net")
M min.nimmin.nim

@@ -35,6 +35,7 @@ lib/min_sys,

lib/min_fs when not defined(lite): + import lib/min_net import lib/min_crypto import lib/min_math

@@ -133,6 +134,7 @@ i.time_module

i.fs_module when not defined(lite): i.crypto_module + i.net_module i.math_module i.eval PRELUDE, "<prelude>" i.eval MINRC.readFile()