Skip to content

Commit 3a29498

Browse files
committed
const compiler implementation
1 parent 431e90f commit 3a29498

File tree

1 file changed

+5
-23
lines changed

1 file changed

+5
-23
lines changed

src/compiler/compiler.go

Lines changed: 5 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -457,29 +457,6 @@ func (c *compiler) VisitBadDecl(d *ast.BadDecl) ast.VisitResult {
457457
}
458458

459459
func (c *compiler) VisitConstDecl(d *ast.ConstDecl) ast.VisitResult {
460-
// TODO
461-
Typ := c.toIrType(d.Type) // get the llvm type
462-
var varLocation value.Value
463-
if c.scp.enclosing == nil { // global scope
464-
// globals are first assigned in ddp_main or module_init
465-
// so we assign them a default value here
466-
globalDef := c.mod.NewGlobalDef(d.Name(), Typ.DefaultValue())
467-
globalDef.Visibility = enum.VisibilityDefault
468-
varLocation = globalDef
469-
} else {
470-
c.commentNode(c.cbb, d, d.Name())
471-
varLocation = c.NewAlloca(Typ.IrType())
472-
}
473-
474-
addInitializer := func() {
475-
initVal, _, isTemp := c.evaluate(d.Val) // evaluate the initial value
476-
477-
c.claimOrCopy(varLocation, initVal, Typ, isTemp)
478-
}
479-
addInitializer()
480-
481-
c.scp.addVar(d.Name(), varLocation, Typ, false)
482-
483460
return ast.VisitRecurse
484461
}
485462

@@ -682,6 +659,11 @@ func (c *compiler) VisitBadExpr(e *ast.BadExpr) ast.VisitResult {
682659
}
683660

684661
func (c *compiler) VisitIdent(e *ast.Ident) ast.VisitResult {
662+
if decl, isConst := e.Declaration.(*ast.ConstDecl); isConst {
663+
c.evaluate(decl.Val)
664+
return ast.VisitRecurse
665+
}
666+
685667
Var := c.scp.lookupVar(e.Declaration.Name()) // get the alloca in the ir
686668
c.commentNode(c.cbb, e, e.Literal.Literal)
687669

0 commit comments

Comments
 (0)