charly-lang / charly

🐈 The Charly Programming Language | Written by @KCreate
https://charly-lang.github.io/charly/
MIT License
199 stars 10 forks source link

IO Functions #4

Closed KCreate closed 7 years ago

KCreate commented 8 years ago

Abstract

The io module should contain methods to interact with the file system at a pretty low level. You should be able to open streams to files, sockets etc.

The fs module will contain high-level bindings to read and write to files, run and to receive network requests.

Each file will be a charly object containing an integer and possibly a filename. The interpreter will internally keep a mapping table between these numbers and the actual resources on the system. That way we don't have to create new primitive types just to represent system resources.

File access should be async by default (just wrap native crystal methods). (is this even possible?)

Code

const fs = require("fs")
const net = require("net")

# Open and write to a file, closing after the block has run
fs.open("./myfile.txt", ->(file) {
  file.write("hello world")
  file.write("\n")
  file.write("what are you doing")
})

# Read a file
fs.read("./myfile.txt", ->(content) {
  print(content)
})

# Read a file without callback
fs.read("./myfile.txt") # => hello world\nwhat are you doing

# Open a HTTP server on localhost at port 3000
net.create_server("localhost", 3000, ->(req, res) {
  print("Request for: " + req.path)
  res.status(200).send("You are talking to charly!")
})

net module

The actual definition of the net module should be included in another PR. The above example is just a basic example of how something like this could work. It should be a node.js-like wrapper around the native crystal methods.

KCreate commented 7 years ago

This is being worked on in #160

KCreate commented 7 years ago

Implemented via #160