-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathmkapi.bas
152 lines (136 loc) · 4.2 KB
/
mkapi.bas
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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
rem
rem generate api methods from api.json
rem
tload "api.json", s, 1
api = array(s)
func get_method_name(method)
local result
if (method.rtn == "void") then
result = "invokeVoid"
else if (method.rtn == "int") then
result = "invokeInt"
else if (method.rtn == "boolean") then
result = "invokeBool"
else if (method.rtn == "float") then
result = "invokeFloat"
endif
if (method.arg == "void") then
result += "Void"
else if (method.arg == "int") then
result += "Int"
else if (method.arg == "boolean") then
result += "Bool"
else if (method.arg == "float") then
result += "Float"
endif
return result
end
sub generate_command(objName, method)
local lname = lower(method.name)
local uname = upper(method.name)
local param_count = iff(method.arg == "void", 0, 1)
local invoke = get_method_name(method)
local cmd_name, err_name
if (isstring(objName)) then
cmd_name = lower(objName) + "_" + lname
err_name = objName + "." + method.name
else
cmd_name = lname
err_name = method.name
endif
if (objName == "IOIO") then
print "static int cmd_" + cmd_name + "(int argc, slib_par_t *arg, var_s *retval) {"
else
print "static int cmd_" + cmd_name + "(var_s *self, int argc, slib_par_t *arg, var_s *retval) {"
endif
print " int result = 0;"
print " if (argc != " + param_count + ") {"
print " error(retval, \"" + err_name + "\", " + param_count + ");"
print " } else {"
local getter, indent
if (objName == "IOIO") then
getter = "g_ioioTask->"
indent = " "
else
getter = "g_ioTaskMap.at(id)."
indent = " "
print " int id = get_io_class_id(self, retval);"
print " if (id != -1) {"
endif
local argument = ""
if (method.arg == "boolean" || method.arg == "int") then
print indent + "auto value = get_param_int(argc, arg, 0, 0);"
argument = ", value"
else if (method.arg == "float") then
print indent + "auto value = get_param_num(argc, arg, 0, 0);"
argument = ", value"
endif
if (method.rtn == "void") then
print indent + "result = " + getter + invoke + "(\"" + method.name + "\"" + argument + ", retval);"
else if (method.rtn == "boolean" || method.rtn == "int") then
print indent + "result = " + getter + invoke + "(\"" + method.name + "\"" + argument + ", retval);"
else if (method.rtn == "float") then
print indent + "result = " + getter + invoke + "(\"" + method.name + "\"" + argument + ", retval);"
endif
if (objName != "IOIO") then
print " }"
endif
print " }"
print " return result;"
print "}"
print
end
sub generate_constructor(byref obj)
print "static void create_" + lower(obj.name) + "(var_t *map) {"
local method
for method in obj.methods
print " v_create_callback(map, \"" + method.name + "\", cmd_" + lower(obj.name) + "_" + lower(method.name) + ");"
next
print "}"
print
end
sub generate_open_function(byref obj)
local pin
local openFunc = "open(pin1, "
print "static int cmd_open" + lower(obj.name) + "(int argc, slib_par_t *params, var_t *retval) {"
print " int result;"
print " int pin1 = get_param_int(argc, params, 0, -1);"
if (isnumber(obj.pins) && obj.pins > 0) then
openFunc = "open" + obj.pins + "(pin1, "
for pin = 2 to obj.pins
openFunc += "pin" + pin + ", "
print " int pin" + pin + " = get_param_int(argc, params, " + (pin - 1) + ", -1);"
next
endif
print " int id = ++g_nextId;"
print " IOTask &instance = g_ioTaskMap[id];"
print " if (instance.create(CLASS_" + upper(obj.name) + ", retval) &&"
print " instance." + openFunc + "retval)) {"
print " map_init_id(retval, id, CLASS_IOTASK_ID);"
print " create_" + lower(obj.name) + "(retval);"
print " result = 1;"
print " } else {"
print " g_ioTaskMap.erase(id);"
print " result = 0;"
print " }"
print " return result;"
print "}"
print
end
for obj in api
if (obj.nogen == 0) then
for method in obj.methods
generate_command(obj.name, method)
next
endif
next
for obj in api
if (len(obj.methods) > 0 && obj.name != "IOIO" and obj.nogen == 0) then
generate_constructor(obj)
endif
next
for obj in api
if (obj.name != "IOIO") then
generate_open_function(obj)
endif
next