generated from openpeeps/pistachio
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: George Lemon <[email protected]>
- Loading branch information
1 parent
7c2f980
commit 69f7c3a
Showing
7 changed files
with
110 additions
and
9 deletions.
There are no files selected for viewing
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 © 2023 OpenPeeps & Contributors — All rights reserved. | ||
MIT license. [Made by Humans from OpenPeeps](https://github.com/openpeeps).<br> | ||
Copyright © 2024 OpenPeeps & Contributors — All rights reserved. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
switch("path", "$projectDir/../src") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
import unittest | ||
|
||
import voodoo | ||
|
||
# todo |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" |