Last active
July 19, 2021 16:01
-
-
Save nikodemus/2ececebfd6c786a6d7bdcbd575fa9dcf to your computer and use it in GitHub Desktop.
Splitting a file in Foolang
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
--- | |
First 'scripting' excursion in Foolang! | |
Input is file containing stuff like this: | |
CLOSURE 1: | |
args: ["out"] | |
body: | |
(self | |
displayOn: out) | |
-- | |
CLOSURE 2: | |
args: [] | |
body: | |
return ordered | |
-- | |
etc. (Indented 4 spaces extra for clarity here!) | |
I want to split them into files like CLOSURE_1, etc in a directry, each file containing | |
the `args:` and `body:` sections, but not the `CLOSURE` header line. | |
--- | |
class Main { inFile outDir log } | |
direct method run: command in: system | |
(self inFile: system currentDirectory / command first | |
outDir: system currentDirectory / command second | |
log: system output) | |
splitClosures! | |
method log: message | |
log print: message. | |
log flush! | |
method splitClosures | |
-- Reverse so we can pop from the end. | |
let lines = inFile lines asList reverse. | |
{ lines isEmpty } | |
whileFalse: { self splitOneClosure: lines }! | |
method splitOneClosure: lines | |
-- Handle final empty line | |
let header = lines pop. | |
header isEmpty | |
ifTrue: { return True }. | |
-- Headers are: "CLOSURE 123:"-style | |
let closureId = (header replace: " " with: "_") butlast. | |
self log: "Doing: {closureId}". | |
-- All lines upto "--" are the 'data'. | |
let data = StringOutput | |
with: { |out| | |
let line = lines pop. | |
self log: ".". | |
{ line == "--" } | |
whileFalse: { out println: line. | |
self log: ".". | |
line = lines pop } }. | |
-- Write it out. | |
(outDir / closureId) forWrite truncateExisting | |
createOrOpen: { |f| f print: data }. | |
self log: "\n"! | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment