-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathtype.ijs
executable file
·82 lines (67 loc) · 1.6 KB
/
type.ijs
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
#!/usr/bin/jconsole
require 'strings'
e =: &.>
lines =: <;._2 :. unlines
unlines =: ;@:(,&LF e) :. lines
n=: #all =: {.@> comments=: lines 0 :0
Builtin
Char
Error
K Complex
O Composition
Function application
Name
Quasiquote
Z Integer
Real
List
)
typedefs =: lines 0 :0
B uint8_t
C int8_t
E Str
O struct { U r; V f; U l; V* x; } *
F struct { U r; V f; U l; V* x; } *
N Str
Q Str
Z int64_t
R double
K struct { R a; R b; }
L struct { U r; T t; U c; U l; U o; P p; } *
)
classes =: 'const arith func comp'
(classes) =: ;: 'ECZRK ZRK BOFNQ OFL'
classes =: 'all ',classes,' nconst'
nconst =: all-.const
echo^:(0 e.~:all) 'Error: non-unique letters'
echo^:(all-.@-:&(/:~){.@>typedefs) 'Error: typedefs don''t match types'
NB. type.h
preamble =: 0 :0
// Generated; see type.ijs
#include <stdlib.h>
#include <stdint.h>
typedef intptr_t I;
typedef void* P;
typedef char* Str;
typedef uintptr_t U;
typedef U T;
typedef struct { T t; P p; } V;
#define ON_TYPES(t, f) ON_##t##_TYPES(f)
)
post =: 0 : 0
#include "mem.h"
#include "apply.h"
)
def =: '#define '
join =: 1 :'[,m,]'
typesum =: '(',')',~' + 'join/@:(,&'_t'"0)
fmt =: rplc '\n';LF;'%T';];'%t';tolower
fmte =: (&fmt)e
types =: unlines ('typedef ',2&}.,' ',{.,';'"_)e typedefs
val =: unlines comments ((def,]),' //',[)e all,e '_t'<@,"1]_12{."1":,.2^i.n
get =: unlines '#define %T(v) (*(%T*)((v).p))'fmte all,'V'
typeclasses =: unlines ((18{.def,toupper,'_t '"_),typesum@:".)e ;:classes
fs=: [: ' 'join/ ('f(',],')'"_)"0
ON =: unlines ('#define ON_',toupper,'_TYPES(f) ',fs@".)e ;:classes
'type.h' (1!:2<)~ }:unlines preamble;types;val;get;typeclasses;ON;post
exit ''