awaitLet
This commit is contained in:
@@ -369,6 +369,41 @@ class Macros {
|
||||
return null;
|
||||
};
|
||||
|
||||
// TODO macros for ifLet, whenLet (for assign then truthy check), caseLet (for extracting from enums)
|
||||
|
||||
function awaitLet(wholeExp:ReaderExp, exps:Array<ReaderExp>, k:KissState) {
|
||||
wholeExp.checkNumArgs(2, null, "(awaitLet [[promise bindings...]] [body...])");
|
||||
var bindingList = switch (exps[0].def) {
|
||||
case ListExp(bindingExps) if (bindingExps.length > 0 && bindingExps.length % 2 == 0):
|
||||
bindingExps;
|
||||
default:
|
||||
throw CompileError.fromExp(exps[0], 'awaitLet bindings should be a list expression with an even number of sub expressions (at least 2)');
|
||||
};
|
||||
var firstName = bindingList.shift();
|
||||
var firstValue = bindingList.shift();
|
||||
return CallExp(FieldExp("then", firstValue).withPosOf(wholeExp), [
|
||||
CallExp(Symbol("lambda").withPosOf(wholeExp), [
|
||||
ListExp([firstName]).withPosOf(wholeExp),
|
||||
if (bindingList.length == 0) {
|
||||
CallExp(Symbol("begin").withPosOf(wholeExp), exps.slice(1)).withPosOf(wholeExp);
|
||||
} else {
|
||||
awaitLet(wholeExp, [ListExp(bindingList).withPosOf(wholeExp)].concat(exps.slice(1)), k);
|
||||
}
|
||||
]).withPosOf(wholeExp),
|
||||
// Handle rejections:
|
||||
CallExp(Symbol("lambda").withPosOf(wholeExp), [
|
||||
ListExp([Symbol("reason").withPosOf(wholeExp)]).withPosOf(wholeExp),
|
||||
CallExp(Symbol("throw").withPosOf(wholeExp), [
|
||||
// TODO generalize CompileError to KissError which will also handle runtime errors
|
||||
// with the same source position format
|
||||
StrExp("rejected promise").withPosOf(wholeExp)
|
||||
]).withPosOf(wholeExp)
|
||||
]).withPosOf(wholeExp)
|
||||
]).withPosOf(wholeExp);
|
||||
}
|
||||
|
||||
macros["awaitLet"] = awaitLet;
|
||||
|
||||
return macros;
|
||||
}
|
||||
|
||||
|
@@ -255,8 +255,6 @@ class SpecialForms {
|
||||
ESwitch(k.forCaseParsing().convert(args[0]), args.slice(1).map(Helpers.makeSwitchCase.bind(_, k)), defaultExpr).withMacroPosOf(wholeExp);
|
||||
};
|
||||
|
||||
// TODO macros for ifLet, expectLet, which extract from enums
|
||||
|
||||
// Type check syntax:
|
||||
map["the"] = (wholeExp:ReaderExp, args:Array<ReaderExp>, k:KissState) -> {
|
||||
wholeExp.checkNumArgs(2, 2, '(the [type] [value])');
|
||||
|
@@ -8,5 +8,7 @@ import kiss.Kiss;
|
||||
import kiss.Prelude;
|
||||
import Externs;
|
||||
|
||||
using StringTools;
|
||||
|
||||
@:build(kiss.Kiss.build("src/Main.kiss"))
|
||||
class Main {}
|
||||
|
@@ -1,28 +1,25 @@
|
||||
(defun loadAll [paths :Function callback &opt :Array<PDFDocument> pdfs]
|
||||
(defun loadAll [:Array<String> paths :Function callback &opt :Array<PDFDocument> pdfs]
|
||||
(unless pdfs (set pdfs []))
|
||||
(deflocal nextPdf (paths.shift))
|
||||
(print nextPdf)
|
||||
(.then (PDFDocument.load (Fs.readFileSync nextPdf))
|
||||
(lambda [pdf]
|
||||
(pdfs.push pdf)
|
||||
(if paths (loadAll paths callback pdfs)
|
||||
(callback pdfs)))
|
||||
(lambda [error]
|
||||
(throw #|'error $error loading $nextPdf'|#))))
|
||||
(if (nextPdf.endsWith ".pdf")
|
||||
(awaitLet [pdf (PDFDocument.load (Fs.readFileSync (print nextPdf)))]
|
||||
(pdfs.push pdf)
|
||||
(if paths
|
||||
(loadAll paths callback pdfs)
|
||||
(callback pdfs)))
|
||||
(when paths
|
||||
(loadAll paths callback pdfs))))
|
||||
|
||||
(defun main []
|
||||
(let [[sourceDir numPages] (Sys.args)]
|
||||
(loadAll (for file (Fs.readdirSync sourceDir) (+ sourceDir "/" file))
|
||||
(lambda [:Array<PDFDocument> inputPdfs]
|
||||
// TODO make an awaitLet macro that .thens a promise or chain of promises (so the bindings are sequential) into a binding
|
||||
(.then (PDFDocument.create) (lambda [saladPdf]
|
||||
(.then
|
||||
(Promise.all
|
||||
(for _ #|0... Std.parseInt(numPages)|#
|
||||
(let [:PDFDocument pdf (nth inputPdfs (Std.random inputPdfs.length))
|
||||
page (Std.random (pdf.getPageCount))]
|
||||
(saladPdf.copyPages pdf [page]))))
|
||||
(lambda [pages]
|
||||
(doFor page pages (saladPdf.addPage (first page)))
|
||||
(.then (saladPdf.save) (lambda [bytesOut]
|
||||
(Fs.writeFileSync "out.pdf" bytesOut)))))))))))
|
||||
(awaitLet [saladPdf (PDFDocument.create)
|
||||
pages (Promise.all
|
||||
(for _ (range 0 (Std.parseInt numPages))
|
||||
(let [:PDFDocument pdf (nth inputPdfs (Std.random inputPdfs.length))
|
||||
page (Std.random (pdf.getPageCount))]
|
||||
(saladPdf.copyPages pdf [page]))))]
|
||||
(doFor page pages (saladPdf.addPage (first page)))
|
||||
(awaitLet [bytesOut (saladPdf.save)]
|
||||
(Fs.writeFileSync "out.pdf" bytesOut)))))))
|
||||
|
Reference in New Issue
Block a user