Skip to content

Commit

Permalink
update iterators and accessor storage
Browse files Browse the repository at this point in the history
Signed-off-by: George Lemon <[email protected]>
  • Loading branch information
georgelemon committed Jan 19, 2024
1 parent 01f52eb commit aabe78e
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 33 deletions.
75 changes: 45 additions & 30 deletions src/tim/engine/compilers/html.nim
Original file line number Diff line number Diff line change
Expand Up @@ -466,38 +466,48 @@ proc evalConcat(c: var HtmlCompiler, node: Node, scopetables: var seq[ScopeTable
add c.output, someValue.toString()
else: discard

template loopEvaluator(items: Node) =
case items.nt:
of ntLitString:
for x in items.sVal:
newScope(scopetables)
node.loopItem.varValue = ast.Node(nt: ntLitString, sVal: $(x))
c.varExpr(node.loopItem, scopetables)
c.evaluateNodes(node.loopBody, scopetables)
clearScope(scopetables)
of ntLitArray:
for x in items.arrayItems:
newScope(scopetables)
node.loopItem.varValue = x
c.varExpr(node.loopItem, scopetables)
c.evaluateNodes(node.loopBody, scopetables)
clearScope(scopetables)
of ntLitObject:
for x, y in items.objectItems:
newScope(scopetables)
node.loopItem.varValue = y
c.varExpr(node.loopItem, scopetables)
c.evaluateNodes(node.loopBody, scopetables)
clearScope(scopetables)
else:
let x = @[ntLitString, ntLitArray, ntLitObject]
compileErrorWithArgs(typeMismatch, [$(items.nt), x.join(" ")])

proc evalLoop(c: var HtmlCompiler, node: Node, scopetables: var seq[ScopeTable]) =
# Evaluates a `for` loop
let some = c.getScope(node.loopItems.identName, scopetables)
if likely(some.scopeTable != nil):
let items = some.scopeTable[node.loopItems.identName]
case items.varValue.nt:
of ntLitString:
for x in items.varValue.sVal:
newScope(scopetables)
node.loopItem.varValue = ast.Node(nt: ntLitString, sVal: $(x))
c.varExpr(node.loopItem, scopetables)
c.evaluateNodes(node.loopBody, scopetables)
clearScope(scopetables)
of ntLitArray:
for x in items.varValue.arrayItems:
newScope(scopetables)
node.loopItem.varValue = x
c.varExpr(node.loopItem, scopetables)
c.evaluateNodes(node.loopBody, scopetables)
clearScope(scopetables)
of ntLitObject:
for x, y in items.varValue.objectItems:
newScope(scopetables)
node.loopItem.varValue = y
c.varExpr(node.loopItem, scopetables)
c.evaluateNodes(node.loopBody, scopetables)
clearScope(scopetables)
else:
let x = @[ntLitString, ntLitArray, ntLitObject]
compileErrorWithArgs(typeMismatch, [$(items.varValue.nt), x.join(" ")])
else:
compileErrorWithArgs(undeclaredVariable, [node.loopItems.identName])
case node.loopItems.nt
of ntIdent:
let some = c.getScope(node.loopItems.identName, scopetables)
if likely(some.scopeTable != nil):
let items = some.scopeTable[node.loopItems.identName]
loopEvaluator(items.varValue)
else: compileErrorWithArgs(undeclaredVariable, [node.loopItems.identName])
of ntDotExpr:
let items = c.dotEvaluator(node.loopItems, scopetables)
if likely(items != nil):
loopEvaluator(items)
else: compileErrorWithArgs(undeclaredVariable, [node.loopItems.identName])
else: return

proc typeCheck(c: var HtmlCompiler, x, node: Node): bool =
if unlikely(x.nt != node.nt):
Expand Down Expand Up @@ -625,6 +635,11 @@ proc getAttrs(c: var HtmlCompiler, attrs: HtmlAttributes, scopetables: var seq[S
if likely(x != nil):
add attrStr, x.toString()
else: return # undeclaredVariable
of ntDotExpr:
let x = c.dotEvaluator(attrNode, scopetables)
if likely(x != nil):
add attrStr, x.toString()
else: return # undeclaredVariable
else: discard
add result, attrStr.join(" ")
if not skipQuote and i != len:
Expand Down
5 changes: 3 additions & 2 deletions src/tim/engine/parser.nim
Original file line number Diff line number Diff line change
Expand Up @@ -356,8 +356,9 @@ proc parseAttributes(p: var Parser, attrs: var HtmlAttributes, el: TokenTuple) {
attrs[attrKey.value] = @[attrValue]
walk p
else:
attrs[attrKey.value] = @[ast.newIdent(p.curr)]
walk p
let x = p.pIdent()
if likely(x != nil):
attrs[attrKey.value] = @[x]
else: errorWithArgs(duplicateAttribute, attrKey, [attrKey.value])
else: break
# errorWithArgs(invalidAttribute, p.prev, [p.prev.value])
Expand Down
1 change: 0 additions & 1 deletion tim.nimble
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ requires "denim#head"
requires "checksums"
requires "flatty"
requires "supersnappy"
requires "stashtable"
# requires "httpx"
# requires "websocketx"

Expand Down

0 comments on commit aabe78e

Please sign in to comment.