Commit 0f2cdcd2560c77e45b83e170de705732d4d29af2
1 parent
92457a15
squeaknim can handle 'var arrary'
Showing
1 changed file
with
16 additions
and
7 deletions
src/squeaknim.nim
... | ... | @@ -94,18 +94,27 @@ template writeSmallTalkCode*(filename: string) = |
94 | 94 | static: |
95 | 95 | writeFile(filename, stCode) |
96 | 96 | |
97 | -proc mapTypeToC(symbolicType: NimNode): string {.compileTime.} = | |
98 | - if symbolicType.kind == nnkEmpty: return "void" | |
97 | +proc mapTypeToC(symbolicType: NimNode; isResultType: bool): string {.compileTime.} = | |
98 | + if symbolicType.kind == nnkEmpty and isResultType: return "void" | |
99 | 99 | let t = symbolicType.getType |
100 | 100 | if symbolicType.kind == nnkSym and t.typeKind == ntyObject: |
101 | 101 | return gPrefix & $symbolicType |
102 | 102 | case t.typeKind |
103 | 103 | of ntyPtr, ntyVar: |
104 | + if t.typeKind == ntyVar and isResultType: | |
105 | + quit "cannot wrap 'var T' as a result type" | |
104 | 106 | expectKind t, nnkBracketExpr |
105 | - result = mapTypeToC(t[1]) & "*" | |
107 | + let base = t[1] | |
108 | + if base.getType.typeKind == ntyArray: | |
109 | + expectKind base, nnkBracketExpr | |
110 | + result = mapTypeToC(base[2], isResultType) & "*" | |
111 | + else: | |
112 | + result = mapTypeToC(base, isResultType) & "*" | |
106 | 113 | of ntyArray: |
114 | + if isResultType: | |
115 | + quit "cannot wrap array as a result type" | |
107 | 116 | expectKind t, nnkBracketExpr |
108 | - result = mapTypeToC(t[2]) & "*" | |
117 | + result = mapTypeToC(t[2], isResultType) & "*" | |
109 | 118 | of ntyCString: result = "char*" |
110 | 119 | of ntyPointer: result = "void*" |
111 | 120 | of ntyInt: result = intType |
... | ... | @@ -145,7 +154,7 @@ macro exportSt*(body: stmt): stmt = |
145 | 154 | else: |
146 | 155 | st.add(": " & ident) |
147 | 156 | # return type: |
148 | - var apicall = "<cdecl: " & mapTypeToC(params[0]) & " '" & | |
157 | + var apicall = "<cdecl: " & mapTypeToC(params[0], true) & " '" & | |
149 | 158 | procName & "' (" |
150 | 159 | var counter = 0 |
151 | 160 | # parameter types: |
... | ... | @@ -158,7 +167,7 @@ macro exportSt*(body: stmt): stmt = |
158 | 167 | if counter > 0: |
159 | 168 | apicall.add(" ") |
160 | 169 | st.addf(" $1: $1", name) |
161 | - apicall.add(mapTypeToC(typ)) | |
170 | + apicall.add(mapTypeToC(typ, false)) | |
162 | 171 | inc counter |
163 | 172 | apicall.add(") module: '" & dllName & "'>\C" & |
164 | 173 | "\t^self externalCallFailed\C!\C\C") |
... | ... | @@ -180,7 +189,7 @@ macro wrapObject*(typ: stmt; wrapFields=false): stmt = |
180 | 189 | if $wrapFields == "true": |
181 | 190 | for i in 0.. < t.len: |
182 | 191 | expectKind t[i], nnkSym |
183 | - fields.addf "\t\t($# '$#')\C", $t[i], mapTypeToC(t[i]) | |
192 | + fields.addf "\t\t($# '$#')\C", $t[i], mapTypeToC(t[i], false) | |
184 | 193 | |
185 | 194 | let st = ("ExternalStructure subclass: #$1\C" & |
186 | 195 | "\tinstanceVariableNames: ''\C" & | ... | ... |