Skip to content

Commit

Permalink
fully implemented sugar
Browse files Browse the repository at this point in the history
  • Loading branch information
HugoGranstrom committed Nov 18, 2024
1 parent 591f8ed commit a1c3d0b
Showing 1 changed file with 60 additions and 14 deletions.
74 changes: 60 additions & 14 deletions sandbox/minib/minib.nim
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# types.nim
import std/[tables, os, strformat, hashes, macros, genasts, strutils]
import std/[tables, os, strformat, hashes, macros, genasts, strutils, sequtils]
import "$nim/compiler/pathutils"
import print
type
Expand Down Expand Up @@ -98,6 +98,7 @@ macro newNbBlock(typeName: untyped, body: untyped): untyped =
(typeName[1].strVal, typeName[2])

let capitalizedTypeName = typeNameStr.capitalizeAscii
let lowercasedTypeName = typeNameStr.toLower

# body is:
# StmtList
Expand Down Expand Up @@ -151,9 +152,47 @@ macro newNbBlock(typeName: untyped, body: untyped): untyped =

# Next: generate initializer with prefilled kind
# Be vary of how we write the kind. SHould it be normalized or exactly like the user wrote it?
# It's more predictable if it is like the user wrote it
# It's more predictable if it is like the user wrote it. But more reasonable to normalize it...

assert false
var procParams = @[capitalizedTypeName.ident] & toSeq(fieldsList.children)
var objectConstructor = nnkObjConstr.newTree(
capitalizedTypeName.ident
)
objectConstructor.add newColonExpr(ident"kind", capitalizedTypeName.newLit)
for (fName, _) in fields:
objectConstructor.add newColonExpr(fName.ident, fName.ident)
var procBody = newStmtList(objectConstructor)

var procName = ident("new" & capitalizedTypeName)
let initializer = newProc(procName, procParams, procBody)

echo "init:\n", initializer.repr

# Next: generate nbImageToHtml from toHtmlBody
let renderProcName = (lowercasedTypeName & "ToHtml").ident
let renderProc = genAst(name = renderProcName, body = toHtmlBody, blk = ident"blk", nb = ident"nb", typeName=capitalizedTypeName.ident):
proc name*(blk: NbBlock, nb: Nb): string =
let blk = typeName(blk)
body


# Next: generate these lines:
# nbToHtml.funcs["NbImage"] = nbImageToHtml
# addNbBlockToJson(NbImage)
let hookAssignements = genAst(key = capitalizedTypeName.newLit, f = renderProcName, typeName = capitalizedTypeName.ident):
nbToHtml.funcs[key] = f
addNbBlockToJson(typeName)

result = newStmtList(
typeDefinition,
initializer,
renderProc,
hookAssignements
)

echo result.repr

#assert false

# themes.nim
import std / strutils
Expand Down Expand Up @@ -232,17 +271,17 @@ newNbBlock(nbText):
markdown(blk.text, config=initGfmConfig())
]#

type
NbImage = ref object of NbBlock
url: string
template nbImage*(turl: string) =
let blk = NbImage(url: turl, kind: "NbImage")
nb.add blk
func nbImageToHtml*(blk: NbBlock, nb: Nb): string =
let blk = blk.NbImage
"<img src= '" & blk.url & "'>"
nbToHtml.funcs["NbImage"] = nbImageToHtml
addNbBlockToJson(NbImage)
# type
# NbImage = ref object of NbBlock
# url: string
# template nbImage*(turl: string) =
# let blk = NbImage(url: turl, kind: "NbImage")
# nb.add blk
# func nbImageToHtml*(blk: NbBlock, nb: Nb): string =
# let blk = blk.NbImage
# "<img src= '" & blk.url & "'>"
# nbToHtml.funcs["NbImage"] = nbImageToHtml
# addNbBlockToJson(NbImage)
#[ the above could be shortened with sugar to:
newNbBlock(nbImage):
url: string
Expand All @@ -255,6 +294,13 @@ newNbBlock(nbImage):
toHtml:
"<img src= '" & blk.url & "'>"

func image*(nb: var Nb, url: string) =
let blk = newNbImage(url=url)
nb.add blk

template nbImage*(url: string) =
nb.image(url)

type
NbDetails = ref object of NbContainer
summary: string
Expand Down

0 comments on commit a1c3d0b

Please sign in to comment.