Skip to content

Instantly share code, notes, and snippets.

@mjendrusch
Last active October 20, 2016 17:42
Show Gist options
  • Save mjendrusch/7d1b9d09b081f09c7418e3c6b42b5910 to your computer and use it in GitHub Desktop.
Save mjendrusch/7d1b9d09b081f09c7418e3c6b42b5910 to your computer and use it in GitHub Desktop.
Compile-time fields for Nim
#
# Compile Time Fields
# This module is provided under
#
# The MIT License (MIT)
#
# Copyright (c) 2016 Michael Jendrusch
#
# Permission is hereby granted, free of charge, to any
# person obtaining a copy of this software and associated
# documentation files (the "Software"), to deal in the
# Software without restriction, including without limitation
# the rights to use, copy, modify, merge, publish, distribute,
# sublicense, and/or sell copies of the Software, and to
# permit persons to whom the Software is furnished to do so,
# subject to the following conditions:
#
# The above copyright notice and this permission notice shall
# be included in all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
# OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
# HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
# WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
# OTHER DEALINGS IN THE SOFTWARE.
#
#----------------------------------------------------------------------
#
# This module implements variable compile-time fields for
# Nim, using a heap-like data structure. This allows for
# an easier implementation of features like extended
# compile-time checking for certain types, as might be
# used in the standard library's JsFFI.
import tables
template initHeap*(ptrName, ptrType: untyped): untyped =
## Construct the necessary `type`s, `var`s and `proc`s
## for handling variable compile-time fields.
type
## The compile-time pointer type for the type in question.
ptrName = distinct int
## The compile-time heap type for the type in question.
`ptrType Heap` = Table[ptrName, ptrType]
proc `==`(a, b: ptrName): bool =
## necessary for Tables to work.
int(a) == int(b)
var
## The count of elements on the compile-time heap.
counter {. gensym, compileTime .}: int = 0
## The compile-time heap for the type `ptrType`
heap {. gensym, compileTime .}: `ptrType Heap` =
initTable[ptrName, ptrType]()
template `[]`(pt: static[ptrName]): ptrType =
## Dereference a compile-time pointer
heap[pt]
template `[]=`(pt: static[ptrName]; val: ptrType) =
## Assign to a compile-time pointer
static:
heap[pt] = val
template `new ptrName`(): static[ptrName] =
## "Allocate memory" for a compile-time pointer
static:
inc counter
ptrName(counter)
when isMainModule:
# A small usage example, giving a type compile-time fields
import math
initHeap(ptrInt, int)
initHeap(ptrFloat, float)
type
HasCtIntFields[a, b: static[ptrInt]; c: static[ptrFloat]] = object
template newHasCt(aVal, bVal: static[int]; cVal: static[float]): auto =
const aInt = newPtrInt()
const bInt = newPtrInt()
const cFloat = newPtrFloat()
var res: HasCtIntFields[aInt, bInt, cFloat]
res.a[] = aVal
res.b[] = bVal
res.c[] = cVal
res
var test = newHasCt(1, 55, 0.31)
static:
echo test.a[]
echo test.b[]
echo test.c[]
var test1 = newHasCt(
test.a[] - test.b[],
test.a[] + test.b[],
math.sqrt(pow(float(test.a[]), 2)+ pow(float(test.b[]), 2)) * test.c[]
)
static:
echo test1.a[]
echo test1.b[]
echo test1.c[]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment