Added constant folding for default arguments in function declarations

This commit is contained in:
Nocturn9x 2021-10-15 15:33:12 +02:00
parent 13efaa5e17
commit 31c6f300fa
2 changed files with 6 additions and 1 deletions

View File

@ -519,7 +519,7 @@ proc `$`*(self: ASTNode): string =
result &= &"Var(name={self.name}, value={self.value}, const={self.isConst}, let={self.isLet}, static={self.isStatic}, private={self.isPrivate})"
of funDecl:
var self = FunDecl(self)
result &= &"FunDecl(name={self.name}, body={self.body}, arguments=[{self.arguments.join(\", \")}], async={self.isAsync}, generator={self.isGenerator}, static={self.isStatic}, private={self.isPrivate})"
result &= &"FunDecl(name={self.name}, body={self.body}, arguments=[{self.arguments.join(\", \")}], defaults=[{self.defaults.join(\", \")}], async={self.isAsync}, generator={self.isGenerator}, static={self.isStatic}, private={self.isPrivate})"
of classDecl:
var self = ClassDecl(self)
result &= &"Class(name={self.name}, body={self.body}, parents=[{self.parents.join(\", \")}], static={self.isStatic}, private={self.isPrivate})"

View File

@ -290,6 +290,11 @@ proc optimizeNode(self: Optimizer, node: ASTNode): ASTNode =
for keyword in node.arguments.keyword:
newArgs.keyword.add((name: keyword.name, value: self.optimizeNode(keyword.value)))
result = CallExpr(kind: callExpr, callee: node.callee, arguments: newArgs)
of funDecl:
var decl = FunDecl(node)
for i, node in decl.defaults:
decl.defaults[i] = self.optimizeNode(node)
result = decl
of blockStmt:
var newBlock = newBlockStmt(@[])
for node in BlockStmt(node).code: