diff --git a/README.md b/README.md index 4916630..4fc5fc1 100644 --- a/README.md +++ b/README.md @@ -1,7 +1,7 @@ # nim-extensions -Extensions for the nim programming language. +Extensions for the Nim programming language. -These extensions aim to improve the usability of nim in practical applications. +These extensions aim to improve the usability of Nim in practical applications. Extensions so far include: @@ -12,7 +12,7 @@ Extensions so far include: usage: ```nim - import oop_macro + import extensions/oop class BaseObject: # inherits from RootObj # attributes/properties var @@ -33,5 +33,5 @@ Extensions so far include: There's also an example in the code that demonstrates the inheritance that can be run by executing: ```bash - nim c -r oop_macro.nim + nim c -r tests/test1.nim ``` diff --git a/src/extensions/oop.nim b/src/extensions/oop.nim index 625c441..a93dd02 100644 --- a/src/extensions/oop.nim +++ b/src/extensions/oop.nim @@ -3,7 +3,7 @@ import strutils {.hint[XDeclaredButNotUsed]: off.} -macro new*(obj: untyped): untyped {.immediate.}= +macro new*(obj: untyped): untyped = if obj.kind == nnkObjConstr or obj.kind == nnkCall: var args: seq[NimNode] = @[] var newObj = copyNimTree(obj) @@ -45,7 +45,7 @@ macro new*(obj: untyped): untyped {.immediate.}= macro class*(head: untyped, body: untyped): untyped = - + # object reference name inside methods. # ie: self, self let objReference = "self" @@ -288,58 +288,5 @@ macro class*(head: untyped, body: untyped): untyped = # OfInherit # Ident !"RootObj" # Empty <= We want to replace self - typeDecl#[[0]]#[0][2][0][2] = recList + typeDecl[0][2][0][2] = recList result.insert(0, typeDecl) - -when isMainModule: - class Animal of RootObj: - var - name: string - age: int - - method init*(name: string, age: int)= - self.name = name - self.age = age - echo "I am a new Animal, ", self.name - - method stuff(s:string): string = s - method vocalize: string = "..." - method ageHumanYrs: int = self.age # `self` is injected - - class Dog of Animal: - method vocalize: string = "woof" - method ageHumanYrs: int = self.age * 7 - - class Cat of Animal: - method vocalize: string = - # call the base class method - self.vocalizeAnimal() & "meow" - - class Tiger of Cat: - method init(name: string="Bob", age: int)= - self.initAnimal(name, age) - echo "I am a new tiger" - method vocalize: string = - # no need for super.super! - self.vocalizeAnimal() & "Rawr!" - - var animals: seq[Animal] = @[] - animals.add(new Dog(name: "Sparky", age: 10)) - animals.add(new Cat(name: "Mitten", age: 10)) - animals.add(new Tiger(name: "Jean", age: 2)) - - for a in animals: - echo a.name, " says ", a.vocalize() - echo a.ageHumanYrs() - - # prints: - # I am a new Animal, Sparky - # I am a new Animal, Mitten - # I am a new Animal, Jean - # I am a new tiger - # Sparky says woof - # 70 - # Mitten says ...meow - # 10 - # Jean says ...Rawr! - # 2 diff --git a/tests/test1.nim b/tests/test1.nim index d364ce5..573f711 100644 --- a/tests/test1.nim +++ b/tests/test1.nim @@ -1,4 +1,4 @@ -import extensions/oop_macro +import extensions/oop import strutils