diff --git a/compiler/compiler/constants.go b/compiler/compiler/constants.go index aa6bebd..b6f5289 100644 --- a/compiler/compiler/constants.go +++ b/compiler/compiler/constants.go @@ -23,17 +23,21 @@ func (c *Compiler) compileConstantNode(v *parser.ConstantNode) value.Value { case parser.NUMBER: var intType *types.Int = i64 + if v.TargetType != nil { + intType = c.parserTypeToType(v.TargetType).(*types.Int) + } // Use context to detect which type that should be returned // Is used to detect if a number should be i32 or i64 etc... - var wantedType types.Type - if len(c.contextAssignDest) > 0 { - wantedType = c.contextAssignDest[len(c.contextAssignDest)-1].Type - } + // todo: better strategy + // var wantedType types.Type + // if len(c.contextAssignDest) > 0 { + // wantedType = c.contextAssignDest[len(c.contextAssignDest)-1].Type + // } // Create the correct type of int based on context - if t, ok := wantedType.(*types.Int); ok { - intType = t - } + // if t, ok := wantedType.(*types.Int); ok { + // intType = t + // } return value.Value{ Value: constant.NewInt(intType.Type, v.Value), diff --git a/compiler/parser/node.go b/compiler/parser/node.go index f725252..847a224 100644 --- a/compiler/parser/node.go +++ b/compiler/parser/node.go @@ -146,9 +146,10 @@ func (on OperatorNode) String() string { type ConstantNode struct { baseNode - Type DataType - Value int64 - ValueStr string + Type DataType + TargetType TypeNode + Value int64 + ValueStr string } type DataType uint8 diff --git a/compiler/parser/parser.go b/compiler/parser/parser.go index 0e65f78..bc968d6 100644 --- a/compiler/parser/parser.go +++ b/compiler/parser/parser.go @@ -190,6 +190,11 @@ func (p *parser) parseOneWithOptions(withAheadParse, withArithAhead, withIdentif Type: NUMBER, Value: int64(len(items)), } + for _, i := range items { + if ii, ok := i.(*ConstantNode); ok { + ii.TargetType = sliceItemType + } + } res = &InitializeSliceNode{ Type: sliceItemType, Len: len, @@ -239,6 +244,11 @@ func (p *parser) parseOneWithOptions(withAheadParse, withArithAhead, withIdentif items := p.parseUntil(lexer.Item{Type: lexer.OPERATOR, Val: "}"}) p.inAllocRightHand = prevInAlloc + for _, i := range items { + if ii, ok := i.(*ConstantNode); ok { + ii.TargetType = arrayItemType + } + } // Array init res = &InitializeArrayNode{ Type: arrayItemType,