awaitLet
This commit is contained in:
@@ -369,6 +369,41 @@ class Macros {
|
|||||||
return null;
|
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;
|
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);
|
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:
|
// Type check syntax:
|
||||||
map["the"] = (wholeExp:ReaderExp, args:Array<ReaderExp>, k:KissState) -> {
|
map["the"] = (wholeExp:ReaderExp, args:Array<ReaderExp>, k:KissState) -> {
|
||||||
wholeExp.checkNumArgs(2, 2, '(the [type] [value])');
|
wholeExp.checkNumArgs(2, 2, '(the [type] [value])');
|
||||||
|
|||||||
@@ -8,5 +8,7 @@ import kiss.Kiss;
|
|||||||
import kiss.Prelude;
|
import kiss.Prelude;
|
||||||
import Externs;
|
import Externs;
|
||||||
|
|
||||||
|
using StringTools;
|
||||||
|
|
||||||
@:build(kiss.Kiss.build("src/Main.kiss"))
|
@:build(kiss.Kiss.build("src/Main.kiss"))
|
||||||
class Main {}
|
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 []))
|
(unless pdfs (set pdfs []))
|
||||||
(deflocal nextPdf (paths.shift))
|
(deflocal nextPdf (paths.shift))
|
||||||
(print nextPdf)
|
(if (nextPdf.endsWith ".pdf")
|
||||||
(.then (PDFDocument.load (Fs.readFileSync nextPdf))
|
(awaitLet [pdf (PDFDocument.load (Fs.readFileSync (print nextPdf)))]
|
||||||
(lambda [pdf]
|
|
||||||
(pdfs.push pdf)
|
(pdfs.push pdf)
|
||||||
(if paths (loadAll paths callback pdfs)
|
(if paths
|
||||||
|
(loadAll paths callback pdfs)
|
||||||
(callback pdfs)))
|
(callback pdfs)))
|
||||||
(lambda [error]
|
(when paths
|
||||||
(throw #|'error $error loading $nextPdf'|#))))
|
(loadAll paths callback pdfs))))
|
||||||
|
|
||||||
(defun main []
|
(defun main []
|
||||||
(let [[sourceDir numPages] (Sys.args)]
|
(let [[sourceDir numPages] (Sys.args)]
|
||||||
(loadAll (for file (Fs.readdirSync sourceDir) (+ sourceDir "/" file))
|
(loadAll (for file (Fs.readdirSync sourceDir) (+ sourceDir "/" file))
|
||||||
(lambda [:Array<PDFDocument> inputPdfs]
|
(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
|
(awaitLet [saladPdf (PDFDocument.create)
|
||||||
(.then (PDFDocument.create) (lambda [saladPdf]
|
pages (Promise.all
|
||||||
(.then
|
(for _ (range 0 (Std.parseInt numPages))
|
||||||
(Promise.all
|
|
||||||
(for _ #|0... Std.parseInt(numPages)|#
|
|
||||||
(let [:PDFDocument pdf (nth inputPdfs (Std.random inputPdfs.length))
|
(let [:PDFDocument pdf (nth inputPdfs (Std.random inputPdfs.length))
|
||||||
page (Std.random (pdf.getPageCount))]
|
page (Std.random (pdf.getPageCount))]
|
||||||
(saladPdf.copyPages pdf [page]))))
|
(saladPdf.copyPages pdf [page]))))]
|
||||||
(lambda [pages]
|
|
||||||
(doFor page pages (saladPdf.addPage (first page)))
|
(doFor page pages (saladPdf.addPage (first page)))
|
||||||
(.then (saladPdf.save) (lambda [bytesOut]
|
(awaitLet [bytesOut (saladPdf.save)]
|
||||||
(Fs.writeFileSync "out.pdf" bytesOut)))))))))))
|
(Fs.writeFileSync "out.pdf" bytesOut)))))))
|
||||||
|
|||||||
Reference in New Issue
Block a user