Commit 032258fdce5994ce1793dd9dd307f81692cfcdb3

Authored by Andreas Rumpf
1 parent f149dc12

various improvements

src/squeaknim.nim
... ... @@ -10,12 +10,15 @@ const
10 10 var
11 11 dllName {.compileTime.}: string = "SqueakNimTest"
12 12 stCode {.compileTime.}: string = ""
  13 + gPrefix {.compileTime.}: string = ""
13 14  
14   -template setModulename*(s: string) =
  15 +template setModulename*(s, prefix: string) =
15 16 ## Sets the DLL name. This is also used to set the 'category' in the generated
16   - ## classes.
  17 + ## classes. 'prefix' is added to every generated class that wraps a Nim
  18 + ## object.
17 19 static:
18 20 dllName = s
  21 + gPrefix = prefix
19 22  
20 23 template writeExternalLibrary*() =
21 24 static:
... ... @@ -37,22 +40,8 @@ template writeSmallTalkCode*(filename: string) =
37 40 proc mapTypeToC(symbolicType: NimNode): string {.compileTime.} =
38 41 let t = symbolicType.getType
39 42 if symbolicType.kind == nnkSym and t.typeKind == ntyObject:
40   - return $symbolicType
  43 + return gPrefix & $symbolicType
41 44 case t.typeKind
42   - of ntyTuple:
43   - result = $t
44   - when false:
45   - let tt = if t.kind == nnkBracketExpr: t else: t.getType
46   - expectKind t, nnkBracketExpr
47   - result = "struct {"
48   - for i in 0 .. < tt.len:
49   - result.addf "$# Field$#;\n", mapTypeToC(tt[i]), $i
50   - result.add "}"
51   - of ntyArray, ntyArrayConstr:
52   - # implement this!
53   - result = "XXX"
54   - of ntyOpenArray:
55   - result = mapTypeToC(t[1]) & "* " & intType
56 45 of ntyPtr, ntyVar:
57 46 expectKind t, nnkBracketExpr
58 47 result = mapTypeToC(t[1]) & "*"
... ... @@ -110,17 +99,17 @@ macro exportSt*(className: string; body: stmt): stmt =
110 99 apicall.add(mapTypeToC(typ))
111 100 inc counter
112 101 apicall.add(") module: '" & dllName & "'>\n" &
113   - " ^self externalCallFailed.\n")
114   - stCode.add(st & "\n\"Generated by NimSqueak\"\n" & apicall)
  102 + "\t^self externalCallFailed.\n")
  103 + stCode.add(st & "\n\t\"Generated by NimSqueak\"\n\t" & apicall)
115 104  
116   -macro wrapObject*(name: string; typ: stmt): stmt =
  105 +macro wrapObject*(typ: stmt): stmt =
117 106 ## Declares a SmallTalk wrapper class.
118   - let name = name.strVal.capitalize
119 107 var t = typ.getType()
120 108 if t.typeKind == ntyTypeDesc:
121 109 expectKind t, nnkBracketExpr
122 110 t = t[1]
123   -
  111 + expectKind t, nnkSym
  112 + let name = gPrefix & ($t).capitalize
124 113 if t.kind != nnkObjectTy: t = t.getType
125 114 expectKind t, nnkObjectTy
126 115 t = t[1]
... ... @@ -128,7 +117,7 @@ macro wrapObject*(name: string; typ: stmt): stmt =
128 117 var fields = ""
129 118 for i in 0.. < t.len:
130 119 expectKind t[i], nnkSym
131   - fields.addf "($# '$#')\n", $t[i], mapTypeToC(t[i])
  120 + fields.addf "\t($# '$#')\n", $t[i], mapTypeToC(t[i])
132 121  
133 122 let st = """ExternalStructure subclass: #$1
134 123 instanceVariableNames: ''
... ... @@ -142,10 +131,9 @@ $1 class
142 131 !$1 class methodsFor: 'field definition' stamp: 'SqueakNim'!
143 132 fields
144 133 ^#(
145   - $3
  134 +$3
146 135 )! !
147 136  
148   - $1 compileFields!
149 137 $1 defineFields.
150 138  
151 139 """ % [name, dllName, fields]
... ...
tests/test1.nim
... ... @@ -6,13 +6,13 @@ type
6 6 Vector3 = object
7 7 x, y, z: MyFloat
8 8  
9   -setModulename "urhonimo"
  9 +setModulename "urhonimo", "UR"
10 10  
11   -wrapObject("Vector3", Vector3)
  11 +wrapObject(Vector3)
12 12  
13 13 writeExternalLibrary()
14 14  
15   -proc foo(a, b: Vector3; c: openArray[int]): cstring {.exportSt: "bar".} =
  15 +proc foo(a, b: Vector3): cstring {.exportSt: "bar".} =
16 16 result = "some string here"
17 17  
18 18 writeSmallTalkCode("test1.st")
... ...