Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

read or input from file or console #11

Open
jkleiser opened this issue Dec 29, 2020 · 2 comments
Open

read or input from file or console #11

jkleiser opened this issue Dec 29, 2020 · 2 comments

Comments

@jkleiser
Copy link

It would be interesting see an example of how to read or input something from file or console, if that is possible.

@LingDong-
Copy link
Owner

Hi @jkleiser,

Thanks for bringing up this issue! Currently IO is not in wax's standard library, but could be achieved through the (asm "...") expression to directly use the target language's IO routines.

Similar to the math standard library:

https://github.com/LingDong-/wax/blob/main/src/std/math.wax

An IO library could look something like:

; io.wax

(@pragma once)

(extern readfile  (param path str) (result str))
(extern writefile (param path str) (param data str))

(@if TARGET_TS
  (asm "const fs = require('fs');")
  (asm "const readfile = (path:string)=>fs.readFileSync(path).toString()\n")
  (asm "const writefile = (path:string,data:string)=>fs.writeFileSync(path,data)\n")
)

(@if TARGET_PY
  (asm "readfile = lambda path: open(path,'r').read()\n")
  (asm "writefile = lambda path, data: open(path,'w').write(data)\n")
)

; ... code for other targets ...

Then in another file

(@include "io.wax")

(let data str (call readfile "path/to/my/file.txt"))

There's still a bit of work to wrap it for all the target languages, and for reading from stdin. But yeah I am planning to include this in wax standard library soon.

Thanks!

@ghost
Copy link

ghost commented May 15, 2021

@LingDong- OK it's weird but i'm now active at my own fork of Wax, Maybe i'm gonna later do it cause i have a lot of plans!
https://github.com/Rabios/wax

idk if you read it but my fork work well...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants