Skip to content

Commit

Permalink
Adding boolean and string types (#6)
Browse files Browse the repository at this point in the history
* adding boolean and string types
* update example

Signed-off-by: vsoch <vsoch@users.noreply.github.com>
Co-authored-by: vsoch <vsoch@users.noreply.github.com>
  • Loading branch information
vsoch and vsoch committed Oct 12, 2021
1 parent 83c21b7 commit ee08140
Show file tree
Hide file tree
Showing 12 changed files with 107 additions and 22 deletions.
55 changes: 49 additions & 6 deletions examples/cpp/simple/1/codegen.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,61 @@
"name": "Function",
"parameters": [
{
"name": "fpIntDvtvhdxzlqqyhtfxqs",
"type": "long long",
"name": "fpIntNirpvxxjljseoeq",
"type": "std::string",
"is_signed": false,
"is_pointer": false,
"value": "12893673844757825883"
"value": "\"rijpkyzbtlx\""
},
{
"name": "fpIntVsdvmhwkyypr",
"type": "int",
"name": "fpFloatPfsdlnaiffqe",
"type": "long double",
"is_complex": false,
"is_pointer": false,
"value": "27105.92540864471443"
},
{
"name": "fpIntNauekcseuyuls",
"type": "short",
"is_signed": true,
"is_pointer": false,
"value": "21157"
},
{
"type": "struct",
"value": "",
"name": "structHsqlmtcfjxgalrdce",
"is_pointer": true,
"fields": [
{
"name": "fpIntVztmefdjczvtfppzxgu",
"type": "long long",
"is_signed": true,
"is_pointer": true,
"value": "-2676140095681027988"
}
]
},
{
"name": "fpFloatWbxnrgynsukt",
"type": "double",
"is_complex": false,
"is_pointer": false,
"value": "35637.78088195861942"
},
{
"name": "fpIntZitcfqtgcwgg",
"type": "std::size_t",
"is_signed": false,
"is_pointer": true,
"value": "857754104"
"value": "27173"
},
{
"name": "fpFloatCpeqyqqtqlwrpp",
"type": "long double",
"is_complex": true,
"is_pointer": false,
"value": "13697830667.942846202"
}
]
}
Expand Down
13 changes: 10 additions & 3 deletions examples/cpp/simple/1/foo.c
Original file line number Diff line number Diff line change
@@ -1,11 +1,18 @@
#include <cstdio>
#include <ostream>
#include <iostream>
#include <string>
#include "foo.h"

void Function(unsigned long long fpIntDvtvhdxzlqqyhtfxqs, unsigned int * fpIntVsdvmhwkyypr) {
void Function( std::string fpIntNirpvxxjljseoeq, long double fpFloatPfsdlnaiffqe, signed short fpIntNauekcseuyuls, STRUCTstructHsqlmtcfjxgalrdce * structHsqlmtcfjxgalrdce, double fpFloatWbxnrgynsukt, std::size_t * fpIntZitcfqtgcwgg, long double fpFloatCpeqyqqtqlwrpp) {

std::cout << "fpIntDvtvhdxzlqqyhtfxqs " << fpIntDvtvhdxzlqqyhtfxqs << std::endl;
std::cout << "fpIntVsdvmhwkyypr " << &fpIntVsdvmhwkyypr << std::endl;
std::cout << "fpIntNirpvxxjljseoeq" << fpIntNirpvxxjljseoeq << std::endl;
std::cout << "fpFloatPfsdlnaiffqe " << fpFloatPfsdlnaiffqe << std::endl;
std::cout << "fpIntNauekcseuyuls" << fpIntNauekcseuyuls << std::endl;
std::cout << "structHsqlmtcfjxgalrdce" << structHsqlmtcfjxgalrdce->fpIntVztmefdjczvtfppzxgu << std::endl;

std::cout << "fpFloatWbxnrgynsukt " << fpFloatWbxnrgynsukt << std::endl;
std::cout << "fpIntZitcfqtgcwgg" << &fpIntZitcfqtgcwgg << std::endl;
std::cout << "fpFloatCpeqyqqtqlwrpp " << fpFloatCpeqyqqtqlwrpp << std::endl;

}
8 changes: 6 additions & 2 deletions examples/cpp/simple/1/foo.h
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
#pragma once

#include <cstdint>
#include <string>

// Structs used in the function should be declared first


struct STRUCTstructHsqlmtcfjxgalrdce {
signed long long fpIntVztmefdjczvtfppzxgu = -2676140095681027988;
};

void Function(unsigned long long fpIntDvtvhdxzlqqyhtfxqs, unsigned int * fpIntVsdvmhwkyypr);
void Function( std::string fpIntNirpvxxjljseoeq, long double fpFloatPfsdlnaiffqe, signed short fpIntNauekcseuyuls, STRUCTstructHsqlmtcfjxgalrdce * structHsqlmtcfjxgalrdce, double fpFloatWbxnrgynsukt, std::size_t * fpIntZitcfqtgcwgg, long double fpFloatCpeqyqqtqlwrpp);
Binary file modified examples/cpp/simple/1/libfoo.so
Binary file not shown.
12 changes: 9 additions & 3 deletions examples/cpp/simple/1/main.c
Original file line number Diff line number Diff line change
@@ -1,12 +1,18 @@
#include "foo.h"
#include <iostream>
#include <string>

int main() {

// Initialize each formal param
unsigned long long fpIntDvtvhdxzlqqyhtfxqs = 12893673844757825883;
unsigned int fpIntVsdvmhwkyypr = 857754104;
std::string fpIntNirpvxxjljseoeq = "rijpkyzbtlx";
long double fpFloatPfsdlnaiffqe = 27105.92540864471443;
signed short fpIntNauekcseuyuls = 21157;
STRUCTstructHsqlmtcfjxgalrdce structHsqlmtcfjxgalrdce;;
double fpFloatWbxnrgynsukt = 35637.78088195861942;
std::size_t fpIntZitcfqtgcwgg = 27173;
long double fpFloatCpeqyqqtqlwrpp = 13697830667.942846202;

// bigcall(1, 2, 3, 4, 5, bigthing);
Function(fpIntDvtvhdxzlqqyhtfxqs, &fpIntVsdvmhwkyypr);
Function(fpIntNirpvxxjljseoeq, fpFloatPfsdlnaiffqe, fpIntNauekcseuyuls, &structHsqlmtcfjxgalrdce, fpFloatWbxnrgynsukt, &fpIntZitcfqtgcwgg, fpFloatCpeqyqqtqlwrpp);
}
1 change: 1 addition & 0 deletions examples/cpp/simple/foo.c
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#include <cstdio>
#include <ostream>
#include <iostream>
#include <string>
#include "foo.h"

void {{ .Function | GetFunctionName }}({{ .Function | AsFormalParams }}) {
Expand Down
1 change: 1 addition & 0 deletions examples/cpp/simple/foo.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#pragma once

#include <cstdint>
#include <string>

// Structs used in the function should be declared first
{{ .Function | DeclareStructs }}
Expand Down
1 change: 1 addition & 0 deletions examples/cpp/simple/main.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#include "foo.h"
#include <iostream>
#include <string>

int main() {

Expand Down
19 changes: 13 additions & 6 deletions generate/cpp/integral.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
package cpp

// INTEGRAL Possible integral types
import (
"github.com/buildsi/codegen/utils"
)

// INTEGRAL Possible integral types NOTE __int128 removed for now, need help to define!
func GetIntegralTypes(withinStruct bool) []string {
if withinStruct {
return []string{"char", "short", "int", "long", "long long"}
return []string{"char", "short", "int", "long", "long long", "std::string", "bool"}
}
return []string{"char", "short", "int", "std::size_t", "long", "long long", "__int128"}
return []string{"char", "short", "int", "std::size_t", "long", "long long", "std::string", "bool"}
}

// Integral Types
Expand Down Expand Up @@ -77,8 +81,11 @@ func (p IntegralFormalParam) Reference() string {
// Prefix of an integral formal param
func (p IntegralFormalParam) Prefix() string {

// size T always unsigned
if p.Type == "std::size_t" {
// These are integral types that are not signed/unsigned
skips := []string{"std::string", "std::size_t", "bool"}

// size T, string, and bool always unsigned
if utils.IncludesString(p.Type, skips) {
return ""
}
if p.IsSigned {
Expand Down Expand Up @@ -106,5 +113,5 @@ func (p IntegralFormalParam) Print() string {
return ""
}
// TODO we will want custom printing based on the type here
return "std::cout << \"" + p.Name + " \" << " + p.Reference() + " << std::endl;"
return "std::cout << \"" + p.Name + "\" << " + p.Reference() + " << std::endl;"
}
2 changes: 1 addition & 1 deletion generate/cpp/struct.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ func (p StructureParam) Print() string {
sep = "->"
}
for _, field := range p.Fields {
result += " std::cout << \"" + p.Name + "\"<< " + p.Name + sep + field.GetFieldName() + " << std::endl;\n"
result += " std::cout << \"" + p.Name + "\" << " + p.Name + sep + field.GetFieldName() + " << std::endl;\n"
}
return result
}
14 changes: 14 additions & 0 deletions generate/cpp/values.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@ func GetIntegralValue(integralType string, isSigned bool, name string) string {
switch integralType {
case "int":
return getIntValue(isSigned)
case "bool":
return getBoolValue()
case "std::string":
return getStringValue()
case "char":
return getCharValue()
case "short":
Expand Down Expand Up @@ -132,6 +136,16 @@ func getSizeTValue() string {
return fmt.Sprintf("%d", utils.RandomInt(65535))
}

// Get a boolean value
func getBoolValue() string {
return fmt.Sprintf("%v", utils.RandomBool())
}

// Get a string value
func getStringValue() string {
return "\"" + utils.RandomName() + "\""
}

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

Expand Down
3 changes: 2 additions & 1 deletion utils/random.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package utils

import (
"math/rand"
"strings"
"time"
)

Expand Down Expand Up @@ -79,5 +80,5 @@ func RandomName() string {
for i := range result {
result[i] = charset[seed.Intn(len(charset))]
}
return string(result)
return strings.Trim(string(result), " ")
}

0 comments on commit ee08140

Please sign in to comment.