Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

C codegen #1

Open
rizo opened this issue Sep 4, 2020 · 3 comments
Open

C codegen #1

rizo opened this issue Sep 4, 2020 · 3 comments

Comments

@rizo
Copy link

rizo commented Sep 4, 2020

Hey! :)

Just came across the BARE format (thanks to your project!) and was surprised by how similar it is to a custom format I created for my company's product. I'm immediately sold and started thinking about integrating BARE.

We currently use it between C and OCaml apps, so my natural question is can we add C codegen? I haven't looked extensively but it doesn't seem like there's currently a C code generator out there.

Thoughts?

@c-cube
Copy link
Owner

c-cube commented Sep 4, 2020

Hola!

I think it's an interesting idea, if you're willing to implement it (reusing the parser and AST, of course). There are lots of questions around the best way to represent some of these constructs in C (map, optional, unions), but why not.

Bonus points if you also generate conversions OCaml<->C for the types 😉

@rizo
Copy link
Author

rizo commented Sep 4, 2020

Yeah maps might be tricky. I don't need them for my use-case at the moment, but I'll try to think of something.

I'm definitely happy to contribute this if we get to implement it (CC @rigon)!

Bonus points if you also generate conversions OCaml<->C for the types 😉

Yes! Man, I wish there was a DSL to write C in OCaml! 😄

@c-cube
Copy link
Owner

c-cube commented Sep 4, 2020

Great!

For maps (or even lists), I imagine it's reasonable to allow the generated code to allocate at least a bit (this is not flatbuffers/capnproto!).

So I think something like that could work (forgive me, my C is a bit… rusty):

BARE:

type Complex {
  im: f64
  re: f64
}

type Foo {
  a: int
  b: []string
  c: []Complex
  d: map[int]bool
}

generated C (types):

struct Complex {
  double re;
  double im;
};

struct ComplexArray {
  data: Complex*;
  len: size_t;
};


struct StringArray {
  data: char**;
  len: size_t;
};

struct MapIntBool {
  keys: int*;
  values: bool*; // or some bitvector…
  len: size_t;
}

struct Foo {
  long a;
  StringArray b;
  ComplexArray c;
  MapIntBool d;
};

and then use malloc to create the arrays/association lists?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants