This repository has been archived by the owner on Feb 9, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
export-to-lua.scm
86 lines (71 loc) · 2.57 KB
/
export-to-lua.scm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
; This file is a redefinition of export-rpc-call to export the procedures
; defined here to the equivalent Lua definitions for ipfs.lua.
(import chicken.irregex)
(define (endpoint->proc-name endpoint)
(irregex-replace/all "[-/]" endpoint "_"))
(define (reader/writer->lua reader/writer)
(if (not reader/writer)
"nil"
(case reader/writer
((reader/json) "reader_json")
((reader/json+) "reader_json")
((reader/plain) "reader_plain")
(else (error "Unexpected `reader/writer`" reader/writer)))))
(define (required?->lua required?)
(case required?
((yes) "Yes")
((no) "No")
(else (error "Unexpected `required?`"))))
(define (ftype->lua ftype)
(if (list? ftype)
(string-append (symbol->string (car ftype)) "(" (symbol->string (cadr ftype)) ")")
(symbol->string ftype)))
(define-syntax export-rpc-call
(syntax-rules ()
((export-rpc-call "internal" () name arguments flags)
(export-rpc-call "internal" (reader/json) name arguments flags))
((export-rpc-call "internal" (default-reader) name arguments flags)
(export-rpc-call "internal" (default-reader #f) name arguments flags))
((export-rpc-call
"internal"
(default-reader default-writer)
name
((atype required?) ...)
((ftype flag) ...))
(print
" IPFS." (endpoint->proc-name name) " = make_ipfs_endpoint(\"" name "\",\n"
" " (reader/writer->lua 'default-reader) ",\n"
;" " (reader/writer->lua 'default-writer) ",\n"
; Arguments
" {" (string-append "{" atype ", " required? "},") ... "},\n"
; Parameters
" {\n"
(string-append " [\"" flag "\"]=" ftype ",\n")
...
" }\n"
" )\n"
))
((export-rpc-call reader/writer (name (atype argument required?) ...) (ftype flag) ...)
(export-rpc-call "internal"
reader/writer (symbol->string 'name)
(((symbol->string 'atype) (required?->lua 'required?)) ...)
(((ftype->lua 'ftype) (symbol->string 'flag)) ...)))))
(print
"return function(tbl)\n"
" local Array = tbl.Array\n"
" local Bool = tbl.Bool\n"
" local Int = tbl.Int\n"
" local No = tbl.No\n"
" local String = tbl.String\n"
" local UInt = tbl.UInt\n"
" local Yes = tbl.Yes\n"
" local make_ipfs_endpoint = tbl.make_ipfs_endpoint\n"
" local reader_json = tbl.reader_json\n"
" local reader_plain = tbl.reader_plain\n"
"\n"
" local IPFS = {}\n"
)
(include "ipfs.v0.endpoints.scm")
(print
" return IPFS\n"
"end")