Skip to content

Commit

Permalink
Merge pull request #1 from buildsi/add/finish-parsing-integral
Browse files Browse the repository at this point in the history
adding completed parsing of integral types
  • Loading branch information
vsoch committed Oct 2, 2021
2 parents 60905bb + 4308791 commit 20de95b
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 8 deletions.
5 changes: 3 additions & 2 deletions generate/cpp/generate.go
Original file line number Diff line number Diff line change
Expand Up @@ -121,11 +121,12 @@ func NewFormalParam() FormalParam {
func NewIntegral() FormalParam {

// Get the type beforehand to derive a random value for it
name := "fpInt" + strings.Title(utils.RandomName())
integralType := utils.RandomChoice(GetIntegralTypes())
isSigned := utils.RandomBool()
value := GetIntegralValue(integralType, isSigned)
value := GetIntegralValue(integralType, isSigned, name)

return IntegralFormalParam{Name: "fpInt" + strings.Title(utils.RandomName()),
return IntegralFormalParam{Name: name,
Type: integralType,
IsSigned: isSigned,
Value: value,
Expand Down
12 changes: 12 additions & 0 deletions generate/cpp/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,17 @@ type IntegralFormalParam struct {

// Declaration of an integral formal param
func (p IntegralFormalParam) Declaration(withValue bool) string {

result := p.Prefix() + " " + p.Type + " " + p.Name

if p.GetType() == "__int128" {
if withValue {
result += ";"
result += p.Value
return result
}
}

// A declaration outside of function params
if withValue {
result += " = " + p.Value + ";"
Expand All @@ -46,6 +55,9 @@ func (p IntegralFormalParam) Declaration(withValue bool) string {

// GetValue returns the string representation of the value
func (p IntegralFormalParam) GetValue() string {
if p.GetType() == "__int128" {
return p.GetName()
}
return p.Value
}

Expand Down
32 changes: 26 additions & 6 deletions generate/cpp/values.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
package cpp

import (
"github.com/buildsi/codegen/utils"
"fmt"
"log"

"github.com/buildsi/codegen/utils"
)

// GetIntegralValue returns an integral value depending on the type
func GetIntegralValue(integralType string, isSigned bool) string {
func GetIntegralValue(integralType string, isSigned bool, name string) string {

switch integralType {
case "int":
Expand All @@ -22,9 +24,9 @@ func GetIntegralValue(integralType string, isSigned bool) string {
case "size_t":
return getSizeTValue()

// TODO this one has weird syntax
// This is multiple lines
case "__int128":
return "123"
return getInt128Value(name)
}

log.Fatalf("Unrecognized integral type %s\n", integralType)
Expand Down Expand Up @@ -80,11 +82,29 @@ func getLongLong(isSigned bool) string {
}
return string(utils.RandomInt(9223372036854775807))
}
// TODO this is not right, what is long long unsigned in go?
return string(utils.RandomInt(9223372036854775807))
// Max unsigned is 18446744073709551615
return string(utils.RandomUint64())
}

// size_t on 64 bit will be 64 bit unsigned integer
func getSizeTValue() string {
return string(utils.RandomInt(65535))
}

// get an int128 value
func getInt128Value(name string) string {

// __int128_t c;
// c = 0x0000000000000006;
// c = c << 64;
// c += 0x0000000000000007;

// This needs custom parsing
firstPart := "0x" + fmt.Sprintf("%12d", utils.RandomInt(9999999999999999))
secondPart := "0x" + fmt.Sprintf("%12d", utils.RandomInt(9999999999999999))

result := "\n " + name + " = " + firstPart + ";\n"
result += " " + name + " << 64;\n"
result += " " + name + " = " + secondPart + ";\n"
return result
}
4 changes: 4 additions & 0 deletions utils/random.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,10 @@ func RandomInt(maxval int) int {
return seed.Intn(maxval)
}

func RandomUint64() uint64 {
return seed.Uint64()
}

// RandomBool returns a boolean choice at the user's threshold
func RandomBoolWeight(chanceTrue float32) bool {
return rand.Float32() < chanceTrue
Expand Down

0 comments on commit 20de95b

Please sign in to comment.