LingDong- / wax

A tiny programming language that transpiles to C, C++, Java, TypeScript, Python, C#, Swift, Lua and WebAssembly 🚀
https://waxc.netlify.app/
MIT License
793 stars 45 forks source link

read or input from file or console #11

Open jkleiser opened 3 years ago

jkleiser commented 3 years ago

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

LingDong- commented 3 years ago

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 commented 3 years ago

@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...