diff --git a/src/squeaknim.nim b/src/squeaknim.nim index 2024f1b..d8fa6f6 100644 --- a/src/squeaknim.nim +++ b/src/squeaknim.nim @@ -10,12 +10,15 @@ const var dllName {.compileTime.}: string = "SqueakNimTest" stCode {.compileTime.}: string = "" + gPrefix {.compileTime.}: string = "" -template setModulename*(s: string) = +template setModulename*(s, prefix: string) = ## Sets the DLL name. This is also used to set the 'category' in the generated - ## classes. + ## classes. 'prefix' is added to every generated class that wraps a Nim + ## object. static: dllName = s + gPrefix = prefix template writeExternalLibrary*() = static: @@ -37,22 +40,8 @@ template writeSmallTalkCode*(filename: string) = proc mapTypeToC(symbolicType: NimNode): string {.compileTime.} = let t = symbolicType.getType if symbolicType.kind == nnkSym and t.typeKind == ntyObject: - return $symbolicType + return gPrefix & $symbolicType case t.typeKind - of ntyTuple: - result = $t - when false: - let tt = if t.kind == nnkBracketExpr: t else: t.getType - expectKind t, nnkBracketExpr - result = "struct {" - for i in 0 .. < tt.len: - result.addf "$# Field$#;\n", mapTypeToC(tt[i]), $i - result.add "}" - of ntyArray, ntyArrayConstr: - # implement this! - result = "XXX" - of ntyOpenArray: - result = mapTypeToC(t[1]) & "* " & intType of ntyPtr, ntyVar: expectKind t, nnkBracketExpr result = mapTypeToC(t[1]) & "*" @@ -110,17 +99,17 @@ macro exportSt*(className: string; body: stmt): stmt = apicall.add(mapTypeToC(typ)) inc counter apicall.add(") module: '" & dllName & "'>\n" & - " ^self externalCallFailed.\n") - stCode.add(st & "\n\"Generated by NimSqueak\"\n" & apicall) + "\t^self externalCallFailed.\n") + stCode.add(st & "\n\t\"Generated by NimSqueak\"\n\t" & apicall) -macro wrapObject*(name: string; typ: stmt): stmt = +macro wrapObject*(typ: stmt): stmt = ## Declares a SmallTalk wrapper class. - let name = name.strVal.capitalize var t = typ.getType() if t.typeKind == ntyTypeDesc: expectKind t, nnkBracketExpr t = t[1] - + expectKind t, nnkSym + let name = gPrefix & ($t).capitalize if t.kind != nnkObjectTy: t = t.getType expectKind t, nnkObjectTy t = t[1] @@ -128,7 +117,7 @@ macro wrapObject*(name: string; typ: stmt): stmt = var fields = "" for i in 0.. < t.len: expectKind t[i], nnkSym - fields.addf "($# '$#')\n", $t[i], mapTypeToC(t[i]) + fields.addf "\t($# '$#')\n", $t[i], mapTypeToC(t[i]) let st = """ExternalStructure subclass: #$1 instanceVariableNames: '' @@ -142,10 +131,9 @@ $1 class !$1 class methodsFor: 'field definition' stamp: 'SqueakNim'! fields ^#( - $3 +$3 )! ! - $1 compileFields! $1 defineFields. """ % [name, dllName, fields] diff --git a/tests/test1.nim b/tests/test1.nim index bf0bf21..992d136 100644 --- a/tests/test1.nim +++ b/tests/test1.nim @@ -6,13 +6,13 @@ type Vector3 = object x, y, z: MyFloat -setModulename "urhonimo" +setModulename "urhonimo", "UR" -wrapObject("Vector3", Vector3) +wrapObject(Vector3) writeExternalLibrary() -proc foo(a, b: Vector3; c: openArray[int]): cstring {.exportSt: "bar".} = +proc foo(a, b: Vector3): cstring {.exportSt: "bar".} = result = "some string here" writeSmallTalkCode("test1.st")