Switched to CamelCase.

This commit is contained in:
Joey Payne 2015-07-07 21:07:16 -06:00
commit ae642be0dc

View file

@ -6,50 +6,50 @@ import strutils
macro new*(obj: untyped): untyped {.immediate.}= macro new*(obj: untyped): untyped {.immediate.}=
if obj.kind == nnkObjConstr or obj.kind == nnkCall: if obj.kind == nnkObjConstr or obj.kind == nnkCall:
var args: seq[NimNode] = @[] var args: seq[NimNode] = @[]
var new_obj = copyNimTree(obj) var newObj = copyNimTree(obj)
# delete the parameters from the object # delete the parameters from the object
# since we are going to use init() instead # since we are going to use init() instead
new_obj.del(1, len(new_obj)-1) newObj.del(1, len(newObj)-1)
# create a symbol that we can use # create a symbol that we can use
var sym = genSym(nskVar, "init_obj") var sym = genSym(nskVar, "initObj")
# self = init_obj # self = initObj
var self_eq = newNimNode(nnkExprEqExpr).add(ident"self", sym) var selfEq = newNimNode(nnkExprEqExpr).add(ident"self", sym)
args.add(self_eq) args.add(selfEq)
for ch in obj.children: for ch in obj.children:
if ch.kind == nnkExprColonExpr: if ch.kind == nnkExprColonExpr:
# some_arg = some_val # someArg = someVal
args.add(newNimNode(nnkExprEqExpr).add(ch[0], ch[1])) args.add(newNimNode(nnkExprEqExpr).add(ch[0], ch[1]))
# init(self=init_obj, some_arg=someval, ...) # init(self=initObj, someArg=someval, ...)
var init_call = newCall(ident"init", args) var initCall = newCall(ident"init", args)
template new_object(symbol, obj_node, init_func)= template newObject(symbol, objNode, initFunc)=
var symbol = obj_node var symbol = objNode
when compiles(init_func): when compiles(initFunc):
init_func initFunc
symbol symbol
result = getAst(new_object(sym, new_obj, init_call)) result = getAst(newObject(sym, newObj, initCall))
else: else:
# otherwise, just call system.new on the object # otherwise, just call system.new on the object
# since we don't care about it # since we don't care about it
template new_object(obj)= template newObject(obj)=
var init_obj = obj var initObj = obj
system.new(init_obj) system.new(initObj)
result = getAst(new_object(obj)) result = getAst(newObject(obj))
macro class*(head: untyped, body: untyped): untyped = macro class*(head: untyped, body: untyped): untyped =
# object reference name inside methods. # object reference name inside methods.
# ie: self, self # ie: self, self
let obj_reference = "self" let objReference = "self"
var export_class: bool = false var exportClass: bool = false
var typeName, baseName: NimNode var typeName, baseName: NimNode
@ -80,11 +80,11 @@ macro class*(head: untyped, body: untyped): untyped =
# Prefix # Prefix
# Ident !"of" # Ident !"of"
# Ident !"RootObj" # Ident !"RootObj"
export_class = true exportClass = true
typeName = head[1] typeName = head[1]
baseName = head[2][1] baseName = head[2][1]
elif head.kind == nnkInfix and $head[0] == "*": elif head.kind == nnkInfix and $head[0] == "*":
export_class = true exportClass = true
typeName = head[1] typeName = head[1]
else: else:
quit "Invalid node: " & head.lispRepr quit "Invalid node: " & head.lispRepr
@ -112,7 +112,7 @@ macro class*(head: untyped, body: untyped): untyped =
# StmtList # StmtList
# StrLit ... # StrLit ...
# MethodDef # MethodDef
# Ident !"age_human_yrs" # Ident !"ageHumanYrs"
# Empty # Empty
# Empty # Empty
# FormalParams # FormalParams
@ -138,20 +138,20 @@ macro class*(head: untyped, body: untyped): untyped =
# return `baseName`(self) # return `baseName`(self)
# result.add(super) # result.add(super)
template set_node_name(n2, proc_name, type_name)= template setNodeName(n2, procName, typeName)=
if n2.name.kind == nnkIdent: if n2.name.kind == nnkIdent:
proc_name = $(n2.name.toStrLit()) procName = $(n2.name.toStrLit())
n2.name = ident(proc_name & type_name) n2.name = ident(procName & typeName)
elif n2.name.kind == nnkPostFix: elif n2.name.kind == nnkPostFix:
if n2.name[1].kind == nnkIdent: if n2.name[1].kind == nnkIdent:
proc_name = $(n2.name[1].toStrLit()) procName = $(n2.name[1].toStrLit())
n2.name[1] = ident(proc_name & type_name) n2.name[1] = ident(procName & typeName)
elif n2.name[1].kind == nnkAccQuoted: elif n2.name[1].kind == nnkAccQuoted:
proc_name = $(n2.name[1][0].toStrLit()) procName = $(n2.name[1][0].toStrLit())
n2.name[1][0] = ident(proc_name & type_name) n2.name[1][0] = ident(procName & typeName)
elif n2.name.kind == nnkAccQuoted: elif n2.name.kind == nnkAccQuoted:
proc_name = $(n2.name[0].toStrLit()) procName = $(n2.name[0].toStrLit())
n2.name[0] = ident(proc_name & type_name) n2.name[0] = ident(procName & typeName)
result.add(n2) result.add(n2)
# Make forward declarations so that function order # Make forward declarations so that function order
@ -161,7 +161,7 @@ macro class*(head: untyped, body: untyped): untyped =
of nnkMethodDef, nnkProcDef: of nnkMethodDef, nnkProcDef:
# inject `self: T` into the arguments # inject `self: T` into the arguments
let n = copyNimTree(node) let n = copyNimTree(node)
n.params.insert(1, newIdentDefs(ident(obj_reference), typeName)) n.params.insert(1, newIdentDefs(ident(objReference), typeName))
# clear the body so we only get a # clear the body so we only get a
# declaration # declaration
n.body = newEmptyNode() n.body = newEmptyNode()
@ -169,10 +169,10 @@ macro class*(head: untyped, body: untyped): untyped =
# forward declare the inheritable method # forward declare the inheritable method
let n2 = copyNimTree(n) let n2 = copyNimTree(n)
let type_name = $(typeName.toStrLit()) let typeName = $(typeName.toStrLit())
var proc_name = "" var procName = ""
set_node_name(n2, proc_name, type_name) setNodeName(n2, procName, typeName)
else: else:
discard discard
@ -183,18 +183,18 @@ macro class*(head: untyped, body: untyped): untyped =
of nnkMethodDef, nnkProcDef: of nnkMethodDef, nnkProcDef:
# inject `self: T` into the arguments # inject `self: T` into the arguments
let n = copyNimTree(node) let n = copyNimTree(node)
n.params.insert(1, newIdentDefs(ident(obj_reference), typeName)) n.params.insert(1, newIdentDefs(ident(objReference), typeName))
# Copy the proc or method for inheritance # Copy the proc or method for inheritance
# ie: procName_ClassName() # ie: procName_ClassName()
let n2 = copyNimTree(node) let n2 = copyNimTree(node)
n2.params.insert(1, newIdentDefs(ident(obj_reference), typeName)) n2.params.insert(1, newIdentDefs(ident(objReference), typeName))
let type_name = $(typeName.toStrLit()) let typeName = $(typeName.toStrLit())
var proc_name = $(n2.name.toStrLit()) var procName = $(n2.name.toStrLit())
var is_assignment = proc_name.contains("=") var isAssignment = procName.contains("=")
set_node_name(n2, proc_name, type_name) setNodeName(n2, procName, typeName)
# simply call the class method from here # simply call the class method from here
# proc procName= # proc procName=
@ -202,11 +202,11 @@ macro class*(head: untyped, body: untyped): untyped =
var p: seq[NimNode] = @[] var p: seq[NimNode] = @[]
for i in 1..n.params.len-1: for i in 1..n.params.len-1:
p.add(n.params[i][0]) p.add(n.params[i][0])
if is_assignment: if isAssignment:
let dot = newDotExpr(ident(obj_reference), ident(proc_name & type_name)) let dot = newDotExpr(ident(objReference), ident(procName & typeName))
n.body = newStmtList(newAssignment(dot, p[1])) n.body = newStmtList(newAssignment(dot, p[1]))
else: else:
n.body = newStmtList(newCall(proc_name & type_name, p)) n.body = newStmtList(newCall(procName & typeName, p))
result.add(n) result.add(n)
@ -239,27 +239,27 @@ macro class*(head: untyped, body: untyped): untyped =
# Ident !"int" # Ident !"int"
# Empty # Empty
var type_decl: NimNode var typeDecl: NimNode
template declare_type_export(tname, bname)= template declareTypeExport(tname, bname)=
type tname* = ref object of bname type tname* = ref object of bname
template declare_type(tname, bname)= template declareType(tname, bname)=
type tname = ref object of bname type tname = ref object of bname
if baseName == nil: if baseName == nil:
if export_class: if exportClass:
type_decl = getAst(declare_type_export(typeName, RootObj)) typeDecl = getAst(declareTypeExport(typeName, RootObj))
else: else:
type_decl = getAst(declare_type(typeName, RootObj)) typeDecl = getAst(declareType(typeName, RootObj))
else: else:
if export_class: if exportClass:
type_decl = getAst(declare_type_export(typeName, baseName)) typeDecl = getAst(declareTypeExport(typeName, baseName))
else: else:
type_decl = getAst(declare_type(typeName, baseName)) typeDecl = getAst(declareType(typeName, baseName))
# Inspect the tree structure: # Inspect the tree structure:
# #
# echo type_decl.treeRepr # echo typeDecl.treeRepr
# -------------------- # --------------------
# StmtList # StmtList
# TypeSection # TypeSection
@ -272,8 +272,8 @@ macro class*(head: untyped, body: untyped): untyped =
# OfInherit # OfInherit
# Ident !"RootObj" # Ident !"RootObj"
# Empty <= We want to replace self # Empty <= We want to replace self
type_decl[0][0][2][0][2] = recList typeDecl[0][0][2][0][2] = recList
result.insert(0, type_decl) result.insert(0, typeDecl)
class Animal of RootObj: class Animal of RootObj:
@ -288,24 +288,24 @@ class Animal of RootObj:
method stuff(s:string): string = s method stuff(s:string): string = s
method vocalize: string = "..." method vocalize: string = "..."
method age_human_yrs: int = self.age # `self` is injected method ageHumanYrs: int = self.age # `self` is injected
class Dog of Animal: class Dog of Animal:
method vocalize: string = "woof" method vocalize: string = "woof"
method age_human_yrs: int = self.age * 7 method ageHumanYrs: int = self.age * 7
class Cat of Animal: class Cat of Animal:
method vocalize: string = method vocalize: string =
# call the base class method # call the base class method
self.vocalize_animal() & "meow" self.vocalizeAnimal() & "meow"
class Tiger of Cat: class Tiger of Cat:
method init(name: string="Bob", age: int)= method init(name: string="Bob", age: int)=
self.init_animal(name, age) self.initAnimal(name, age)
echo "I am a new tiger" echo "I am a new tiger"
method vocalize: string = method vocalize: string =
# no need for super.super! # no need for super.super!
self.vocalize_animal() & "Rawr!" self.vocalizeAnimal() & "Rawr!"
if isMainModule: if isMainModule:
var animals: seq[Animal] = @[] var animals: seq[Animal] = @[]
@ -315,7 +315,7 @@ if isMainModule:
for a in animals: for a in animals:
echo a.name, " says ", a.vocalize() echo a.name, " says ", a.vocalize()
echo a.age_human_yrs() echo a.ageHumanYrs()
# prints: # prints:
# I am a new Animal, Sparky # I am a new Animal, Sparky