all repos — min @ deca3939ac32bee5104de66fb10ae50af3946252

A small but practical concatenative programming language.

lib/min_net.nim

 1
 2
 3
 4
 5
 6
 7
 8
 9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
import net, nativesockets
import 
  ../core/types,
  ../core/parser,
  ../core/interpreter, 
  ../core/utils

# Network

define("net")

  .symbol("open") do (i: In):
    echo "all good"
    var q: MinValue
    i.reqQuotation q
    # (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 vals = q.qVal
    if not ["ipv4", "ipv6"].contains(vals[0].symVal):
      raiseInvalid("Domain symbol must be 'ipv4' or 'ipv6'")
    if not ["stream", "dgram"].contains(vals[1].symVal):
      raiseInvalid("Type symbol must be 'stream' or 'dgram'")
    if not ["tcp", "udp"].contains(vals[2].symVal):
      raiseInvalid("Protocol symbol must be 'tcp' or 'udp'")
    var 
      domain: Domain
      sockettype: SockType
      protocol: Protocol
    # Process domain
    if vals[0].symVal == "ipv4":
      domain = AF_INET
    else:
      domain = AF_INET6
    if vals[1].symVal == "stream":
      sockettype = SOCK_STREAM
    else:
      sockettype = SOCK_DGRAM
    if vals[2].symVal == "tcp":
      protocol = IPPROTO_TCP
    else:
      protocol = IPPROTO_UDP
    var socket = newSocket(domain, sockettype, protocol)
    echo "all good"
    q.objType = "socket"
    q.obj = socket.addr
    i.push @[q]
    

  .symbol("close") do (i: In):
    discard

  .symbol("listen") do (i: In):
    discard

  .symbol("accept") do (i: In):
    discard

  .symbol("connect") do (i: In):
    discard

  .symbol("send") do (i: In):
    discard

  .symbol("recv") do (i: In):
    discard

  .symbol("bind") do (i: In):
    discard

  .finalize()