Commit 032258fdce5994ce1793dd9dd307f81692cfcdb3

Authored by Andreas Rumpf
1 parent f149dc12

various improvements

src/squeaknim.nim
@@ -10,12 +10,15 @@ const @@ -10,12 +10,15 @@ const
10 var 10 var
11 dllName {.compileTime.}: string = "SqueakNimTest" 11 dllName {.compileTime.}: string = "SqueakNimTest"
12 stCode {.compileTime.}: string = "" 12 stCode {.compileTime.}: string = ""
  13 + gPrefix {.compileTime.}: string = ""
13 14
14 -template setModulename*(s: string) = 15 +template setModulename*(s, prefix: string) =
15 ## Sets the DLL name. This is also used to set the 'category' in the generated 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 static: 19 static:
18 dllName = s 20 dllName = s
  21 + gPrefix = prefix
19 22
20 template writeExternalLibrary*() = 23 template writeExternalLibrary*() =
21 static: 24 static:
@@ -37,22 +40,8 @@ template writeSmallTalkCode*(filename: string) = @@ -37,22 +40,8 @@ template writeSmallTalkCode*(filename: string) =
37 proc mapTypeToC(symbolicType: NimNode): string {.compileTime.} = 40 proc mapTypeToC(symbolicType: NimNode): string {.compileTime.} =
38 let t = symbolicType.getType 41 let t = symbolicType.getType
39 if symbolicType.kind == nnkSym and t.typeKind == ntyObject: 42 if symbolicType.kind == nnkSym and t.typeKind == ntyObject:
40 - return $symbolicType 43 + return gPrefix & $symbolicType
41 case t.typeKind 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 of ntyPtr, ntyVar: 45 of ntyPtr, ntyVar:
57 expectKind t, nnkBracketExpr 46 expectKind t, nnkBracketExpr
58 result = mapTypeToC(t[1]) & "*" 47 result = mapTypeToC(t[1]) & "*"
@@ -110,17 +99,17 @@ macro exportSt*(className: string; body: stmt): stmt = @@ -110,17 +99,17 @@ macro exportSt*(className: string; body: stmt): stmt =
110 apicall.add(mapTypeToC(typ)) 99 apicall.add(mapTypeToC(typ))
111 inc counter 100 inc counter
112 apicall.add(") module: '" & dllName & "'>\n" & 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 ## Declares a SmallTalk wrapper class. 106 ## Declares a SmallTalk wrapper class.
118 - let name = name.strVal.capitalize  
119 var t = typ.getType() 107 var t = typ.getType()
120 if t.typeKind == ntyTypeDesc: 108 if t.typeKind == ntyTypeDesc:
121 expectKind t, nnkBracketExpr 109 expectKind t, nnkBracketExpr
122 t = t[1] 110 t = t[1]
123 - 111 + expectKind t, nnkSym
  112 + let name = gPrefix & ($t).capitalize
124 if t.kind != nnkObjectTy: t = t.getType 113 if t.kind != nnkObjectTy: t = t.getType
125 expectKind t, nnkObjectTy 114 expectKind t, nnkObjectTy
126 t = t[1] 115 t = t[1]
@@ -128,7 +117,7 @@ macro wrapObject*(name: string; typ: stmt): stmt = @@ -128,7 +117,7 @@ macro wrapObject*(name: string; typ: stmt): stmt =
128 var fields = "" 117 var fields = ""
129 for i in 0.. < t.len: 118 for i in 0.. < t.len:
130 expectKind t[i], nnkSym 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 let st = """ExternalStructure subclass: #$1 122 let st = """ExternalStructure subclass: #$1
134 instanceVariableNames: '' 123 instanceVariableNames: ''
@@ -142,10 +131,9 @@ $1 class @@ -142,10 +131,9 @@ $1 class
142 !$1 class methodsFor: 'field definition' stamp: 'SqueakNim'! 131 !$1 class methodsFor: 'field definition' stamp: 'SqueakNim'!
143 fields 132 fields
144 ^#( 133 ^#(
145 - $3 134 +$3
146 )! ! 135 )! !
147 136
148 - $1 compileFields!  
149 $1 defineFields. 137 $1 defineFields.
150 138
151 """ % [name, dllName, fields] 139 """ % [name, dllName, fields]
tests/test1.nim
@@ -6,13 +6,13 @@ type @@ -6,13 +6,13 @@ type
6 Vector3 = object 6 Vector3 = object
7 x, y, z: MyFloat 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 writeExternalLibrary() 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 result = "some string here" 16 result = "some string here"
17 17
18 writeSmallTalkCode("test1.st") 18 writeSmallTalkCode("test1.st")