Skip to content

Instantly share code, notes, and snippets.

@zacharycarter
Created March 6, 2018 21:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save zacharycarter/a4058a6e536e45c6eb88b493eb34708d to your computer and use it in GitHub Desktop.
Save zacharycarter/a4058a6e536e45c6eb88b493eb34708d to your computer and use it in GitHub Desktop.
import compiler/[ast, idents, modulegraphs, modules, vm, vmdef], macros, ospaths, tables, threadpool
export vm
let graph* = newModuleGraph()
let identCache* = newIdentCache()
macro api*(impl: untyped): untyped =
let moduleName = splitFile(lineInfoObj(callsite()).filename).name
let module = graph.makeModule(moduleName)
let ctx = newCtx(module, identCache)
ctx.mode = emRepl
registerAdditionalOps(ctx)
result = newStmtList()
let templateDef = newNimNode(nnkTemplateDef)
let postfix = newNimNode(nnkPostfix)
postfix.add(ident("*"), ident("expose"))
let identDefs = newNimNode(nnkIdentDefs)
identDefs.add(ident("procName"))
identDefs.add(ident("procBody"))
identDefs.add(ident("untyped"))
identDefs.add(newEmptyNode())
let formalParams = newNimNode(nnkFormalParams)
formalParams.add(newEmptyNode(), identDefs)
let dirtyPragma = newNimNode(nnkPragma)
dirtyPragma.add(ident("dirty"))
let templateBody = newStmtList()
let cmd = newNimNode(nnkCommand)
cmd.add(newDotExpr(ident("ctx"), ident("registerCallback")))
let innerInfix = newNimNode(nnkInfix)
innerInfix.add(ident("&"))
innerInfix.add(ident("moduleName"))
innerInfix.add(newStrLitNode("."))
let outerInfix = newNimNode(nnkInfix)
outerInfix.add(ident("&"), innerInfix, newCall(ident("astToStr"), ident("procName")))
cmd.add(outerInfix)
let lamb = newNimNode(nnkLambda)
let lambFormalParams = newNimNode(nnkFormalParams)
let lambIdentDefs = newNimNode(nnkIdentDefs)
lambIdentDefs.add(ident("a"))
lambIdentDefs.add(ident("VmArgs"))
lambIdentDefs.add(newEmptyNode())
lambFormalParams.add(newEmptyNode(), lambIdentDefs)
lamb.add(newEmptyNode(), newEmptyNode(), newEmptyNode(), lambFormalParams, newEmptyNode(), newEmptyNode(), newStmtList(ident("procBody")))
cmd.add(lamb)
templateBody.add(cmd)
templateDef.add(
postfix, newEmptyNode(), newEmptyNode(), formalParams, dirtyPragma, newEmptyNode(), templateBody
)
result.add(templateDef)
result.add(impl)
lib/pure/os.nim(324, 13) Error: cannot 'importc' variable at compile time
VM is only allowed to 'cast' between integers of same size
import bgfxdotnim, bgfxdotnim/[platform], ens, macros, compiler/[ast, idents, modulegraphs, modules, vm, vmdef]
proc init*(pd: ptr bgfx_platform_data_t) =
bgfx_set_platform_data(pd)
let res = bgfx_init(BGFX_RENDERER_TYPE_COUNT, 0'u16, 0, nil, nil)
assert res
bgfx_reset(800, 600, 1)
bgfx_set_debug(BGFX_DEBUG_TEXT or BGFX_DEBUG_PROFILER)
proc frame*() =
discard bgfx_frame(false)
proc shutdown*() =
bgfx_shutdown()
api:
expose add:
setResult(a, getInt(a, 0) + getInt(a, 1))
expose sub:
setResult(a, getInt(a, 0) - getInt(a, 1))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment