Skip to content

Commit

Permalink
[#2] curly braces can handle basic expressions
Browse files Browse the repository at this point in the history
Signed-off-by: George Lemon <[email protected]>
  • Loading branch information
georgelemon committed Aug 5, 2023
1 parent 785c98b commit 2ac483f
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 17 deletions.
1 change: 1 addition & 0 deletions src/bro.nims
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
--deepcopy:on
--define:msgpack_obj_to_map
--define:httpxServerName:"BroStyleSync"
--define:nimPreviewHashRef

when defined release:
--opt:speed
Expand Down
19 changes: 11 additions & 8 deletions src/bropkg/engine/compiler.nim
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,8 @@ proc toString(c: var Compiler, v: Node, scope: ScopeTable = nil): string =
add accVal[0], $(hash(accVar.callIdent[1..^1])) # variable name without `$`
of ntInt:
add accVal[0], $(hash($accVar.iVal))
of ntMathStmt, ntInfix:
add accVal[0], $(hash(accVar))
else: discard
accVal[1] = c.toString(c.getValue(accVar, scope))
add accValues, accVal
Expand Down Expand Up @@ -181,6 +183,8 @@ proc getValue(c: var Compiler, v: Node, scope: ScopeTable): Node =
handleVariableValue(v, scope)
of ntInfix:
result = newBool(c.evalInfix(v.infixLeft, v.infixRight, v.infixOp, scope))
of ntMathStmt:
result = c.evalMathInfix(v.mathLeft, v.mathRight, v.mathInfixOp, scope)
else:
result = v

Expand Down Expand Up @@ -297,21 +301,20 @@ proc handleCommand(c: var Compiler, node: Node, scope: ScopeTable = nil) =
stdout.styledWriteLine(fgGreen, "Debug", fgDefault, meta, fgDefault,
getTypeInfo(node.cmdValue) & "\n" & $(output))
of ntMathStmt:
let total = c.evalMathInfix(node.cmdValue.mathLeft,
node.cmdValue.mathRight, node.cmdValue.mathInfixOp, scope)
let output =
if total.nt == ntInt:
$(total.iVal)
else:
$(total.fVal)
let
total =
c.evalMathInfix(node.cmdValue.mathLeft, node.cmdValue.mathRight,
node.cmdValue.mathInfixOp, scope)
output =
if total.nt == ntInt: $(total.iVal)
else: $(total.fVal)
stdout.styledWriteLine(fgGreen, "Debug", fgDefault, meta, fgDefault,
getTypeInfo(node.cmdValue) & "\n" & $(output))
# of ntInfo:
# stdout.styledWriteLine(fgGreen, "Debug", fgDefault, meta, fgMagenta,
# "[[" & $(node.cmdValue.nodeType) & "]]")
else:
let varValue = c.getValue(node.cmdValue, scope)
# echo varValue
if likely(varValue != nil):
var output: string
case varValue.nt:
Expand Down
8 changes: 7 additions & 1 deletion src/bropkg/engine/handlers/pLiteral.nim
Original file line number Diff line number Diff line change
Expand Up @@ -69,14 +69,20 @@ newPrefixProc "parseAccQuoted":
walk p, 2
while p.curr isnot tkRC:
if unlikely(p.curr is tkEOF): return nil
add result.accVal, indent("$bro" & $(hash(p.curr.value)), tkSymbol.wsno)
if p.curr is tkIdentifier:
add result.accVal, indent("$bro" & $(hash(p.curr.value)), tkSymbol.wsno)
let varNode = p.parseVarCall(tk, "$" & p.curr.value, scope)
if likely(varNode != nil):
add result.accVars, varNode
else: return nil
else:
let prefixInfixNode = p.getPrefixOrInfix(scope, includeOnly = {tkInteger})
case prefixInfixNode.nt
of ntInt, ntBool:
add result.accVal, indent("$bro" & $(hash(p.curr.value)), tkSymbol.wsno)
of ntMathStmt, ntInfix:
add result.accVal, indent("$bro" & $(hash(prefixInfixNode)), tkSymbol.wsno)
else: discard
if likely(prefixInfixNode != nil):
add result.accVars, prefixInfixNode
else: return nil
Expand Down
8 changes: 0 additions & 8 deletions src/bropkg/engine/parser.nim
Original file line number Diff line number Diff line change
Expand Up @@ -423,14 +423,6 @@ proc parseCompOp(p: var Parser, left: Node, scope: var seq[ScopeTable]): Node =
proc parseMathOp(p: var Parser, left: Node, scope: var seq[ScopeTable]): Node =
walk p
result = p.getPrefixOrInfix(includeOnly = {tkInteger, tkFloat, tkVarCall, tkIdentifier, tkFnCall}, scope = scope)
# case p.curr.kind
# of tkInteger:
# result = p.parseInt(scope)
# of tkFloat:
# result = p.parseFloat(scope)
# of tkVarCall:
# result = p.parseCallCommand(scope)
# else: discard

proc getInfixFn(p: var Parser): InfixFunction =
case p.curr.kind
Expand Down

0 comments on commit 2ac483f

Please sign in to comment.