Skip to content

Instantly share code, notes, and snippets.

@stisa
stisa / htmldsl.nim
Last active February 10, 2017 17:19
htmldsl in nim
import strutils
import macros except body
# TODO: avoid all the quasi-same macros.
# TODO: expand spec (links,style,img,src etc)
# TODO: allow specifying id, class
# TODO: render the ast to proper html, with proper indentation
# TODO: group similar kinds together, eg div,body,section,header,footer,etc are all containers.
# TODO: less verbose macros for a? also macro for id,class for other nodes
@stisa
stisa / localStorage.nim
Created January 31, 2017 18:10
Ugly js ffi example, the name of the module is important
proc setItem (item:cstring,to:cstring){.importc, exportc:"localStorage.$1"}
proc getItem (item:cstring):string{.importc, exportc:"localStorage.$1"}
proc log(x:string) = {.emit :"console.log(`x`);"}
localStorage.setItem("hi","world")
log localStorage.getItem("hi") #> world
#!/bin/sh
# SEE: http://lists.nongnu.org/archive/html/tinycc-devel/2016-10/msg00146.html
# Config
DEFAULT_DEST="win32/reprobuild" # relative to top source dir
TMP_DIR="_tmp" # dir for intermidiate files inside each output dir
# Builds one windows tcc distribution
# args: TARGET_DIR, CC, {I386|X86_64}, [LIBS_CC, LIBS_AR]
# Doesn't touch anything outside of $TARGET_DIR - which must not exist on entry.
# libtcc1 objects are compiled with CC_LIBS and joined into libtcc1.a with AR_LIBS
C:\Users\silvio\Documents\Dev\nim\eval>nim c -r --cc:tcc testing.nim
Hint: used config file 'C:\Dev\Nim-devel\config\nim.cfg' [Conf]
Hint: system [Processing]
Hint: testing [Processing]
CC: testing
CC: stdlib_system
In file included from c:/users/silvio/documents/dev/nim/eval/nimcache/testing.c:9:
C:/Dev/Nim-devel/lib/nimbase.h:227: error: include file 'limits.h' not found
In file included from c:/users/silvio/documents/dev/nim/eval/nimcache/stdlib_system.c:9:
C:/Dev/Nim-devel/lib/nimbase.h:227: error: include file 'limits.h' not found
type A = object
data: ref seq[float]
proc `$`[T](a:ref seq[T]):string = $(a[])
var ar : A
new ar.data
ar.data[] = @[0.0,1.0,2.0]
echo ar
## Original: https://nim-by-example.github.io/oop/
## Running the above with non ref objects errors ##
type Animal = object of RootObj
name: string
age: int
method vocalize(this: Animal): string = "..."
method ageHumanYrs(this: Animal): int = this.age
@stisa
stisa / benchshift.nim
Last active September 25, 2016 08:04
import times
proc possiblyleakingshift[T](a: var openarray[T]; d: int) =
if d == 0: discard # do nothing
elif abs(d) >= a.len: # reset all
zeroMem(addr a[0], a.len*sizeof(T))
elif d > 0: # right shift
moveMem(addr a[d], addr a[0], (a.len - d)*sizeof(T))
zeroMem(addr a[0], d*sizeof(T))
elif d < 0: # left shift
@stisa
stisa / fromimport.nim
Last active September 23, 2016 08:56
## first.nim ###
proc test*(): string = "test1"
echo test()
## second.nim ###
from first import test
proc test2*(): string = "test2"
echo test2()
@stisa
stisa / dynprocptr.nim
Last active September 22, 2016 19:26
## lib.nim #########
proc test1(): string {.exportc, dynlib .}=
result = "test"
## user.nim ########
import dynlib, osproc
# compile libs so I don't have to do it by hand
let l1 = execCmd("nim c --app:lib lib.nim")
import zmq
# Example in cpp:
# http://zguide.zeromq.org/cpp:mspoller
var spm2 = zmq.connect("tcp://localhost:5556", zmq.Dealer)
var conn2 = zmq.listen("tcp://*:5556", zmq.ROUTER)
var spm = zmq.connect("tcp://localhost:5555",zmq.Dealer)
var conn = zmq.listen("tcp://*:5555", zmq.ROUTER)