Skip to content

Commit

Permalink
adding example to assert types
Browse files Browse the repository at this point in the history
Signed-off-by: vsoch <vsoch@users.noreply.github.com>
  • Loading branch information
vsoch committed Oct 29, 2021
1 parent 158752f commit 87137c7
Show file tree
Hide file tree
Showing 13 changed files with 81 additions and 152 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -260,6 +260,7 @@ to a function, we use a pipe. Thus, you need to know the functions that are supp
|function| DeclareArgs | Make multi-line declarations of parameters to pass into the function |
|function| GetFunctionName | Just print the name of the function |
|function| PrintArgs | just do a println of each named param, usually for debugging |
|function| AssertArgs | an assertion for equality of each |
|function| DeclareStructs | write declarations for structures |
You'll also notice there is a Makefile in the folder - likely when we generate many of these
Expand Down
3 changes: 0 additions & 3 deletions config/config.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package config

import (
"fmt"
"io/ioutil"
"log"
"path/filepath"
Expand Down Expand Up @@ -68,7 +67,6 @@ func loadRendering(item interface{}) Rendering {
settings := item.(map[string]interface{})["render"]
renders := map[string]Render{}
mapstructure.Decode(settings, &renders)
fmt.Println(renders)

// Default needs to be true
for key, render := range renders {
Expand All @@ -80,7 +78,6 @@ func loadRendering(item interface{}) Rendering {
} else {
render.Parameters.Pointers = usePointer.(bool)
}
fmt.Println(render)
renders[key] = render
}
rendering.Renders = renders
Expand Down
131 changes: 25 additions & 106 deletions examples/cpp/simple/1/codegen.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,127 +3,46 @@
"name": "Function",
"parameters": [
{
"type": "struct",
"value": "",
"name": "structEjknkgadfa",
"name": "fpFloatYstjspseqcigbiup",
"type": "double",
"is_complex": false,
"is_pointer": false,
"fields": [
{
"name": "fpFloatLuzkhkoxhyjkn",
"type": "float",
"is_complex": false,
"is_pointer": false,
"value": "64316.06133035861504"
},
{
"name": "fpIntVraaodeeavanyhdz",
"type": "int",
"is_signed": false,
"is_pointer": true,
"value": "1144873560"
},
{
"name": "fpIntXvzdloucfqfaazabgf",
"type": "long long",
"is_signed": true,
"is_pointer": true,
"value": "9217545440300276469"
},
{
"name": "fpFloatCsnvyrxala",
"type": "float",
"is_complex": true,
"is_pointer": false,
"value": "9361592366087721.203"
},
{
"name": "fpIntJfntojpcbnobhyzlu",
"type": "char",
"is_signed": true,
"is_pointer": true,
"value": "'v'"
},
{
"name": "fpFloatQythcelcmpldgugdnz",
"type": "double",
"is_complex": false,
"is_pointer": false,
"value": "2609453.9312241003"
}
],
"is_union": false
"value": "1125828000295628.9607"
},
{
"name": "fpIntJpqjsedhnpvnvtjfxm",
"type": "long",
"is_signed": true,
"is_pointer": true,
"value": "-1195503477"
"name": "fpFloatJhqfrgrwhnujepxtci",
"type": "double",
"is_complex": false,
"is_pointer": false,
"value": "216542.7049063265426"
},
{
"name": "fpFloatOkxtgxtfbgmvoezfcmz",
"name": "fpFloatVrzjnfwhunhbw",
"type": "float",
"is_complex": false,
"is_pointer": false,
"value": "14410591851.764390300"
},
{
"name": "fpFloatJjwsedhfyjgwfntl",
"type": "long double",
"is_complex": true,
"is_pointer": false,
"value": "2848223.570775133493"
"value": "14472.359365019037764"
},
{
"name": "fpIntYoiwjlejeux",
"type": "long",
"name": "fpIntJjswlhecncmdiegikrm",
"type": "int",
"is_signed": true,
"is_pointer": false,
"value": "-1206782476"
"is_pointer": true,
"value": "915663051"
},
{
"name": "fpFloatYtpwgmomqgnq",
"type": "float",
"name": "fpFloatWifjrqddpyqerholqu",
"type": "long double",
"is_complex": true,
"is_pointer": false,
"value": "124838372252039654.38"
},
{
"type": "union",
"value": "",
"name": "unionGplgcipqwtlgfz",
"is_pointer": true,
"fields": [
{
"name": "fpIntLlbkcrdenfsqxvhjyqy",
"type": "short",
"is_signed": true,
"is_pointer": false,
"value": "-22420"
},
{
"name": "fpIntIeijlyuyho",
"type": "char",
"is_signed": true,
"is_pointer": true,
"value": "'Q'"
},
{
"name": "fpFloatUkvgmvngoibibz",
"type": "double",
"is_complex": false,
"is_pointer": false,
"value": "262822.4884931115707"
},
{
"name": "fpIntEkjetkvotspayy",
"type": "short",
"is_signed": true,
"is_pointer": false,
"value": "-12165"
},
{
"name": "fpFloatTxtyuzacgvurrwek",
"type": "float",
"is_complex": false,
"is_pointer": false,
"value": "1351860.2488992992004"
}
],
"is_union": true
"value": "162.0738501236674422"
}
]
}
Expand Down
26 changes: 8 additions & 18 deletions examples/cpp/simple/1/foo.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,25 +3,15 @@
#include <iostream>
#include <string>
#include "foo.h"
#include <assert.h>

void Function(STRUCTstructEjknkgadfa structEjknkgadfa, signed long * fpIntJpqjsedhnpvnvtjfxm, float fpFloatOkxtgxtfbgmvoezfcmz, signed long fpIntYoiwjlejeux, float fpFloatYtpwgmomqgnq, UNIONunionGplgcipqwtlgfz * unionGplgcipqwtlgfz) {

std::cout << "structEjknkgadfa" << structEjknkgadfa.fpFloatLuzkhkoxhyjkn << std::endl;
std::cout << "structEjknkgadfa" << structEjknkgadfa.fpIntVraaodeeavanyhdz << std::endl;
std::cout << "structEjknkgadfa" << structEjknkgadfa.fpIntXvzdloucfqfaazabgf << std::endl;
std::cout << "structEjknkgadfa" << structEjknkgadfa.fpFloatCsnvyrxala << std::endl;
std::cout << "structEjknkgadfa" << structEjknkgadfa.fpIntJfntojpcbnobhyzlu << std::endl;
std::cout << "structEjknkgadfa" << structEjknkgadfa.fpFloatQythcelcmpldgugdnz << std::endl;

std::cout << "fpIntJpqjsedhnpvnvtjfxm" << &fpIntJpqjsedhnpvnvtjfxm << std::endl;
std::cout << "fpFloatOkxtgxtfbgmvoezfcmz " << fpFloatOkxtgxtfbgmvoezfcmz << std::endl;
std::cout << "fpIntYoiwjlejeux" << fpIntYoiwjlejeux << std::endl;
std::cout << "fpFloatYtpwgmomqgnq " << fpFloatYtpwgmomqgnq << std::endl;
std::cout << "unionGplgcipqwtlgfz" << unionGplgcipqwtlgfz->fpIntLlbkcrdenfsqxvhjyqy << std::endl;
std::cout << "unionGplgcipqwtlgfz" << unionGplgcipqwtlgfz->fpIntIeijlyuyho << std::endl;
std::cout << "unionGplgcipqwtlgfz" << unionGplgcipqwtlgfz->fpFloatUkvgmvngoibibz << std::endl;
std::cout << "unionGplgcipqwtlgfz" << unionGplgcipqwtlgfz->fpIntEkjetkvotspayy << std::endl;
std::cout << "unionGplgcipqwtlgfz" << unionGplgcipqwtlgfz->fpFloatTxtyuzacgvurrwek << std::endl;
void Function(double fpFloatYstjspseqcigbiup, double fpFloatJhqfrgrwhnujepxtci, float fpFloatVrzjnfwhunhbw, long double fpFloatJjwsedhfyjgwfntl, signed int * fpIntJjswlhecncmdiegikrm, long double fpFloatWifjrqddpyqerholqu) {

assert (fpFloatYstjspseqcigbiup == 1125828000295628.9607);
assert (fpFloatJhqfrgrwhnujepxtci == 216542.7049063265426);
assert (fpFloatVrzjnfwhunhbw == 14410591851.764390300);
assert (fpFloatJjwsedhfyjgwfntl == 14472.359365019037764);
assert (* fpIntJjswlhecncmdiegikrm == 915663051);
assert (fpFloatWifjrqddpyqerholqu == 162.0738501236674422);

}
19 changes: 2 additions & 17 deletions examples/cpp/simple/1/foo.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,6 @@
#include <string>

// Structs used in the function should be declared first

struct STRUCTstructEjknkgadfa {
float fpFloatLuzkhkoxhyjkn = 64316.06133035861504;
unsigned int fpIntVraaodeeavanyhdz = 1144873560;
signed long long fpIntXvzdloucfqfaazabgf = 9217545440300276469;
float fpFloatCsnvyrxala = 9361592366087721.203;
signed char fpIntJfntojpcbnobhyzlu = 'v';
double fpFloatQythcelcmpldgugdnz = 2609453.9312241003;
};
union UNIONunionGplgcipqwtlgfz {
signed short fpIntLlbkcrdenfsqxvhjyqy;
signed char fpIntIeijlyuyho;
double fpFloatUkvgmvngoibibz;
signed short fpIntEkjetkvotspayy;
float fpFloatTxtyuzacgvurrwek = 1351860.2488992992004;
};


void Function(STRUCTstructEjknkgadfa structEjknkgadfa, signed long * fpIntJpqjsedhnpvnvtjfxm, float fpFloatOkxtgxtfbgmvoezfcmz, signed long fpIntYoiwjlejeux, float fpFloatYtpwgmomqgnq, UNIONunionGplgcipqwtlgfz * unionGplgcipqwtlgfz);
void Function(double fpFloatYstjspseqcigbiup, double fpFloatJhqfrgrwhnujepxtci, float fpFloatVrzjnfwhunhbw, long double fpFloatJjwsedhfyjgwfntl, signed int * fpIntJjswlhecncmdiegikrm, long double fpFloatWifjrqddpyqerholqu);
Binary file modified examples/cpp/simple/1/libfoo.so
Binary file not shown.
14 changes: 7 additions & 7 deletions examples/cpp/simple/1/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,13 @@
int main() {

// Initialize each formal param
STRUCTstructEjknkgadfa structEjknkgadfa;;
signed long fpIntJpqjsedhnpvnvtjfxm = -1195503477;
float fpFloatOkxtgxtfbgmvoezfcmz = 2848223.570775133493;
signed long fpIntYoiwjlejeux = -1206782476;
float fpFloatYtpwgmomqgnq = 124838372252039654.38;
UNIONunionGplgcipqwtlgfz unionGplgcipqwtlgfz;;
double fpFloatYstjspseqcigbiup = 1125828000295628.9607;
double fpFloatJhqfrgrwhnujepxtci = 216542.7049063265426;
float fpFloatVrzjnfwhunhbw = 14410591851.764390300;
long double fpFloatJjwsedhfyjgwfntl = 14472.359365019037764;
signed int fpIntJjswlhecncmdiegikrm = 915663051;
long double fpFloatWifjrqddpyqerholqu = 162.0738501236674422;

// bigcall(1, 2, 3, 4, 5, bigthing);
Function(structEjknkgadfa, &fpIntJpqjsedhnpvnvtjfxm, fpFloatOkxtgxtfbgmvoezfcmz, fpIntYoiwjlejeux, fpFloatYtpwgmomqgnq, &unionGplgcipqwtlgfz);
Function(fpFloatYstjspseqcigbiup, fpFloatJhqfrgrwhnujepxtci, fpFloatVrzjnfwhunhbw, fpFloatJjwsedhfyjgwfntl, &fpIntJjswlhecncmdiegikrm, fpFloatWifjrqddpyqerholqu);
}
3 changes: 2 additions & 1 deletion examples/cpp/simple/foo.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@
#include <iostream>
#include <string>
#include "foo.h"
#include <assert.h>

void {{ .Function | GetFunctionName }}({{ .Function | AsFormalParams }}) {

{{ .Function | PrintArgs }}
{{ .Function | AssertArgs }}
}
5 changes: 5 additions & 0 deletions generate/cpp/float.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,11 @@ func (p FloatFormalParam) GetFieldName() string {
return p.Name
}

// Assert an integral formal param
func (p FloatFormalParam) Assert() string {
return " assert (" + p.GetName() + " == " + p.GetValue() + ");\n"
}

// Print prints an float formal param
func (p FloatFormalParam) Print() string {
// TODO we will want more custom formatting based on the type here
Expand Down
8 changes: 8 additions & 0 deletions generate/cpp/integral.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,14 @@ func (p IntegralFormalParam) GetRawType() string {
return p.Type
}

// Assert an integral formal param
func (p IntegralFormalParam) Assert() string {
if p.Type == "__int128" {
return ""
}
return " assert (" + p.GetName() + " == " + p.GetValue() + ");\n"
}

// Print prints an integral formal param
func (p IntegralFormalParam) Print() string {
// TODO not sure how to do this one
Expand Down
13 changes: 13 additions & 0 deletions generate/cpp/struct.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,19 @@ func (p StructureParam) Reference() string {
return p.Name
}

// Assert a structure param
func (p StructureParam) Assert() string {
result := ""
sep := "."
if p.IsPointer {
sep = "->"
}
for _, field := range p.Fields {
result += " assert (" + p.GetName() + sep + field.GetFieldName() + " == " + field.GetValue() + ");\n"
}
return result
}

// Print a general param
func (p StructureParam) Print() string {
result := ""
Expand Down
9 changes: 9 additions & 0 deletions generate/cpp/templates.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,15 @@ var templateHelpers template.FuncMap = map[string]interface{}{
return render
},

// AssertArgs prints an assertion statement for each param
"AssertArgs": func(f Function) string {
render := ""
for _, param := range f.FormalParams {
render += param.Assert()
}
return render
},

// DeclareArgs declares a variable for each argument
"DeclareArgs": func(f Function) string {
render := ""
Expand Down
1 change: 1 addition & 0 deletions generate/cpp/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ type FormalParam interface {
GetRawType() string
Prefix() string
Print() string
Assert() string
Reference() string
DeclareFormalParam() string
DeclareValue() string
Expand Down

0 comments on commit 87137c7

Please sign in to comment.