Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
Signed-off-by: George Lemon <[email protected]>
  • Loading branch information
georgelemon committed Apr 2, 2024
1 parent 7c2f980 commit 69f7c3a
Show file tree
Hide file tree
Showing 7 changed files with 110 additions and 9 deletions.
Binary file added .github/voodoo.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
28 changes: 19 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,30 +1,40 @@
<p align="center">
<img src="https://github.com/openpeeps/PKG/blob/main/.github/logo.png" width="90px"><br>
OpenPeeps repository template for developing libraries,<br>projects and other cool things. 👑 Written in Nim language
<img src="https://github.com/openpeeps/voodoo/blob/main/.github/voodoo.png" width="150px" height="160px"><br>
Working with Nim's macros 👑 is just Voodoo
</p>

<p align="center">
<code>nimble install {PKG}</code>
<code>nimble install voodoo</code>
</p>

<p align="center">
<a href="https://github.com/">API reference</a><br>
<img src="https://github.com/openpeeps/pistachio/workflows/test/badge.svg" alt="Github Actions"> <img src="https://github.com/openpeeps/pistachio/workflows/docs/badge.svg" alt="Github Actions">
<img src="https://github.com/openpeeps/voodoo/workflows/test/badge.svg" alt="Github Actions"> <img src="https://github.com/openpeeps/voodoo/workflows/docs/badge.svg" alt="Github Actions">
</p>

## 😍 Key Features
- [x] Open Source | `MIT` License
- [x] Written in Nim language

## Examples
...
```nim
import pkg/voodoo
type
Price* {.getters.} = object
net, gross: string
Product* {.getters.} = object
title, short_description: string
prices: Price
```

### ❤ Contributions & Support
- 🐛 Found a bug? [Create a new Issue](/issues)
- 👋 Wanna help? [Fork it!](/fork)
- 🐛 Found a bug? [Create a new Issue](https://github.com/openpeeps/voodoo/issues)
- 👋 Wanna help? [Fork it!](https://github.com/openpeeps/voodoo/fork)
- 😎 [Get €20 in cloud credits from Hetzner](https://hetzner.cloud/?ref=Hm0mYGM9NxZ4)
- 🥰 [Donate via PayPal address](https://www.paypal.com/donate/?hosted_button_id=RJK3ZTDWPL55C)

### 🎩 License
{PKG} | MIT license. [Made by Humans from OpenPeeps](https://github.com/openpeeps).<br>
Copyright &copy; 2023 OpenPeeps & Contributors &mdash; All rights reserved.
MIT license. [Made by Humans from OpenPeeps](https://github.com/openpeeps).<br>
Copyright &copy; 2024 OpenPeeps & Contributors &mdash; All rights reserved.
7 changes: 7 additions & 0 deletions src/voodoo.nim
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# Working with Nim's macros is just Voodoo
#
# (c) 2024 George Lemon | MIT License
# Made by Humans from OpenPeeps
# https://github.com/openpeeps/voodoo
import voodoo/getter
export getter
66 changes: 66 additions & 0 deletions src/voodoo/getter.nim
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
# Working with Nim's macros is just Voodoo
#
# (c) 2024 George Lemon | MIT License
# Made by Humans from OpenPeeps
# https://github.com/openpeeps/voodoo

import std/[macros, macrocache, strutils]

const genGetters = CacheTable"genGetters"
macro getters*(obj: untyped) =
# let impl = getImpl(x)
let objident = obj[0][0][1]
let objf = obj[2][0][2]
obj[0] = obj[0][0]
expectKind objf, nnkRecList
proc genProcIdent(x: NimNode): NimNode =
let fieldName = $(x)
var procName = "get"
var i: int
add procName, fieldName[i].toUpperAscii
inc i
while i < fieldName.len:
case fieldName[i]
of '_':
while fieldName[i] == '_':
inc i
add procName, fieldName[i].toUpperAscii
inc i
else:
add procName, fieldName[i]
inc i
result = ident(procName)

genGetters[objident.strVal] = newStmtList()
for f in objf:
var procName: NimNode
var returnType = f[^2]
var fieldName: string
for x in f[0..^3]:
case x.kind
of nnkIdent, nnkAccQuoted:
fieldName = $x
procName = genProcIdent(x)
else: discard
var body = newStmtList()
add body, newCommentStmtNode("Getter handle to return `" & $fieldName & "`")
add genGetters[objident.strVal],
newProc(
nnkPostfix.newTree(ident("*"), procName),
params = [
returnType,
nnkIdentDefs.newTree(
ident(objident.strVal[0].toLowerAscii & objident.strVal[1..^1]),
objident,
newEmptyNode()
),
],
body = body
)
obj

macro expandGetters* =
result = newStmtList()
for k, x in genGetters:
echo x.repr
add result, x
1 change: 1 addition & 0 deletions tests/config.nims
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
switch("path", "$projectDir/../src")
5 changes: 5 additions & 0 deletions tests/test1.nim
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import unittest

import voodoo

# todo
12 changes: 12 additions & 0 deletions voodoo.nimble
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# Package

version = "0.1.0"
author = "George Lemon"
description = "Working with Nim's macros is just Voodoo"
license = "MIT"
srcDir = "src"


# Dependencies

requires "nim >= 2.0.2"

0 comments on commit 69f7c3a

Please sign in to comment.