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

Swap write arguments #1812

Merged
merged 6 commits into from
Nov 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions examples/rosetta/create a file on magnetic tape.art
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
write "TAPE.FILE" {
{
This code
should be able to write
a file
to magnetic tape
}
} >> "TAPE.FILE"
8 changes: 4 additions & 4 deletions examples/rosetta/create a file.art
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
output: "output.txt"
docs: "docs"

write output ""
write.directory docs ø
"" >> output
write.directory ø docs

write join.path ["/" output] ""
write.directory join.path ["/" docs] ø
"" >> join.path ["/" output]
write.directory ø join.path ["/" docs]
4 changes: 2 additions & 2 deletions examples/rosetta/file input-output.art
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
source: read "input.txt"
write "output.txt" source
source >> "output.txt"

print source
print source
2 changes: 1 addition & 1 deletion examples/rosetta/fractal tree.art
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,4 @@ svg: {
drawTree svg 0.5*width height trunkLength startingAngle
'svg ++ "</svg>"

write "fractal.svg" svg
svg >> "fractal.svg"
2 changes: 1 addition & 1 deletion examples/rosetta/goldbach's comet.art
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,4 @@ print ["\nG(1000000) =" G 1000000]
csv: join.with:",\n" map select 4..2000 => even? 'x ->
~"|x|, |G x|"

write "comet.csv" csv
csv >> "comet.csv"
2 changes: 1 addition & 1 deletion examples/rosetta/hello world - line printer.art
Original file line number Diff line number Diff line change
@@ -1 +1 @@
write "/dev/lp0" "Hello World\n"
"Hello World\n" >> "/dev/lp0"
2 changes: 1 addition & 1 deletion examples/rosetta/json.art
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,4 @@ object: #[
married: false
]

print write.json ø object
print write.json object ø
2 changes: 1 addition & 1 deletion examples/rosetta/make directory path.art
Original file line number Diff line number Diff line change
@@ -1 +1 @@
write.directory "path/to/some/directory" ø
write.directory ø "path/to/some/directory"
4 changes: 2 additions & 2 deletions examples/rosetta/remove lines from a file.art
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ removeFileLines: function [filename, start, cnt][
]

final: previous\[0..start-1] ++ previous\[start+cnt..dec size previous]
write filename join.with:"\n" final
(join.with:"\n" final) >> filename
]

removeFileLines "myfile.txt" 3 10
removeFileLines "myfile.txt" 3 10
4 changes: 2 additions & 2 deletions examples/rosetta/take notes on the command line.art
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,5 @@ switch empty? arg [
][
output: (to :string now) ++ "\n" ++
"\t" ++ (join.with:" " to [:string] arg) ++ "\n"
write.append notes output
]
write.append output notes
]
2 changes: 1 addition & 1 deletion examples/rosetta/write entire file.art
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
contents: "Hello World!"
write "output.txt" contents
contents >> "output.txt"
30 changes: 15 additions & 15 deletions src/library/Files.nim
Original file line number Diff line number Diff line change
Expand Up @@ -494,11 +494,11 @@ proc defineLibrary*() =
builtin "write",
alias = doublearrowright,
op = opNop,
rule = PrefixPrecedence,
rule = InfixPrecedence,
description = "write content to file at given path",
args = {
"file" : {String,Null},
"content" : {Any}
"content" : {Any},
"file" : {String,Null}
},
attrs = {
"append" : ({Logical},"append to given file"),
Expand All @@ -510,36 +510,36 @@ proc defineLibrary*() =
returns = {Nothing},
example = """
; write some string data to given file path
write "somefile.txt" "Hello world!"
write "Hello world!" "somefile.txt"
..........
; we can also write any type of data as JSON
write.json "data.json" myData
write.json myData "data.json"
..........
; append to an existing file
write.append "somefile.txt" "Yes, Hello again!"
write.append "Yes, Hello again!" "somefile.txt"
""":
#=======================================================
when defined(SAFE): Error_OperationNotPermitted("write")

if yKind==Bytecode:
if xKind==Bytecode:
let dataS = codify(newBlock(y.trans.constants), unwrapped=true, safeStrings=true)
let codeS = y.trans.instructions
discard writeBytecode(dataS, codeS, x.s)
let codeS = x.trans.instructions
discard writeBytecode(dataS, codeS, y.s)
else:
if (hadAttr("directory")):
createDir(x.s)
createDir(y.s)
else:
if (hadAttr("binary")):
writeToFile(x.s, y.n, append = (hadAttr("append")))
writeToFile(y.s, x.n, append = (hadAttr("append")))
else:
if (hadAttr("json")):
let rez = jsonFromValue(y, pretty=(not hadAttr("compact")))
if x.kind==String:
writeToFile(x.s, rez, append = (hadAttr("append")))
let rez = jsonFromValue(x, pretty=(not hadAttr("compact")))
if y.kind==String:
writeToFile(y.s, rez, append = (hadAttr("append")))
else:
push(newString(rez))
else:
writeToFile(x.s, y.s, append = (hadAttr("append")))
writeToFile(y.s, x.s, append = (hadAttr("append")))

builtin "zip",
alias = unaliased,
Expand Down
Loading
Loading