mruby: generating readable c code - c

I am beginning with mruby, and I need a little in generating readable .c code using mrbc. I was following this article :
Here it is mentioned :
$ mruby/bin/mrbc -Cinit_tester test_program.rb
will produce test_program.c with some content.
but on my machine when I run this command it says :
mrbc: output file should be specified to compile multiple files
Then I tried
$ mruby/bin/mrbc -Binit_tester test_program.rb
which works , generates c files but its contents are only bytecode:
#include <stdint.h>
const uint8_t init_tester[] = {0x45,0x54,0x49,0x52,0x30,0x30,0x30,0x33,0x73,0x0d,0x00,0x00,0x00,0x65,0x4d,0x41,0x54,0x5a,0x30,0x30,0x30,0x30,0x49,0x52,0x45,0x50,0x00,0x00,0x00,0x47,0x30,0x30,0x30,0x30,0x00,0x00,0x00,0x3f,0x00,0x01,0x00,0x04,0x00,0x00,0x00,0x00,0x00,0x04,0x06,0x00,0x80,0x00,0x3d,0x00,0x00,0x01,0xa0,0x00,0x80,0x00,0x4a,0x00,0x00,0x00,0x00,0x00,0x00,0x01,0x00,0x00,0x0b,0x68,0x65,0x6c,0x6c,0x6f,0x20,0x77,0x6f,0x72,0x6c,0x64,0x00,0x00,0x00,0x01,0x00,0x04,0x70,0x75,0x74,0x73,0x00,0x45,0x4e,0x44,0x00,0x00,0x00,0x00,0x08, };
Which is basically byte code of the mruby code that we have put in c code.
If you look at the blog m under section Readable C Code (.c), this should have actually generated c code.
why is the mrbc not generating readable c code ?

why is the mrbc not generating readable c code?
Well, mrbc is a compiler to generate the binary format of ruby code with RiteVM understands so there is no way of generating a readable C code.
Instead with -v option you can see AST and VM codes of your code
(I prefer to pass -c option too since mrbc will generate *.mrb files without it) .

Related

Hiding C sources when using lcov with a language transpiled to C

I'm experimenting with a language (ooc) that transpiles into C and is then compiled using gcc. I want to check code coverage for a project written in this language and display it using lcov and genhtml. The C code has #line references throughout the file pointing to the corresponding file in the original source.
So far so good, I have it setup and working. But lcov seems to generate results for both the orignal source and the generated C code, which bloats the output.
How can I hide results for the C code?
Managed to solve this on my own.
After the initial lcov call, but before genhtml, call for example:
lcov --remove <info-file> '/usr/include/*' -o <info-file>
Where <info-file> is the .info-file generated by lcov in the initial step.

YouCompleteMe suggests only "local" used code

I'm trying to use YCM for the first time so in order to make it work I decided to give a chance for the YCM-Generator, which generates the .ycm_extra_conf.py file automatically based on the makefile.
So far my program is just a simple hello world.
#include <stdio.h>
int main()
{
printf("Hello World!");
return 0;
}
I'm using the CMakeLists.txt trick to generate the makefile.
file(GLOB sources *.h *.c)
add_executable(Foo ${sources})
then after executing the YCM-Generator script, I get this output
Running cmake in '/tmp/tmp_YknVy'... $ cmake
/home/pedro/Desktop/Projetos/teste
Running make... $ make -i -j4
Cleaning up...
Build completed in 1.5 sec
Collected 2 relevant entries for C compilation (0 discarded).
Collected 0 relevant entries for C++ compilation (0 discarded).
Created YCM config file with 0 C flags
YCM plugin does find the .ycm_extra_conf.py file, but the auto-completion doesn't work right, for example, if I type "floa", it doesn't suggests "float", but It only suggests things that I used before like "int" or "printf".
Am I missing something or this is working as intended?
So I fixed it.
For c it does require a .ycm_extra_conf.py , while a friend of mine could make it work without one in c++.
The auto complete only suggest automatically functions that were previously used, if you don't remember a function name you have to press <Ctrl-Space>
YCM-Generator didn't do the job, so I modified the example file myself following the comments.
If you are used to Visual Assist, the auto complete works but it's really weak if compared to VA, which is a shame... I really hope someone port that plugin to Linux.

#include won't load header in C

I've recently been trying to program some simple code for the NES. I wrote a batch file that takes the file I wrote in C and turns it into a .nes file but every time I run it, my C file can't load a header. Here's the script that's causing problems.
#include <nes.h>
Note that I have very little knowledge of C but I'd still like to know what's happening.
where is "nes.h" located when you run the compiler? if it is in the current directory, then
#include "nes.h"
will do.
otherwise you must tell the compiler in which directory to find it using -I parameter
gcc ... -I ...

How to use bin2h?

I'm trying to use bin2h to convert a font file (font.ttf) into a C file but it won't work.
Can someone please tell me the syntax to save the output to a text file?
I've been trying to figure this out but nothing is working, and it's driving me insane. I'm really frustrated because I know the tool is working (I got it to work like a year ago) but I can't remember how I used it.
The example syntax on that site doesn't really help...
Please
Thanks to Lightness Races in Orbit's comment below I finally got the syntax right!
bin2h -cz font < font.ttf > output.h
That's working, thanks
Perhaps you are looking at the usage example on the website and not realising that it is a program that you execute from shell? It is not a line of C code.
So if you want to use this from a C program, you will need to execute it through a function like system or exec. However, since its output is a line of C code, you'd be better off running it from within your build script to create a C script, that you'd then link in to the rest of your program.
Example (in C++ as my C is rusty — port to C as required):
Source code for main.cpp
#include <iostream>
#include "eula.h"
int main()
{
std::cout << std::string(eula, eula_size) << std::endl;
}
Build commands
$ bin2h -cz eula < eula.txt > eula.h
$ g++ main.cpp -o myProgram
Execution command
$ ./myProgram
I would just write my own.
Here's the algorithm:
Open the source code file as text output.
Open the font file as binary input.
Write the array declaration to the output file, something like:
static const unsigned char font[] =
{
While the font file is not empty do:
Read unsigned char from font file, using binary read methods.
Output the unsigned char, in text format, to the source file.
end-while
Write the ending brace and semicolon to the source file.

How can I cleanly include Ruby code to be written to git's post-commit hook within C code?

I have some C code which writes Ruby code into Git's post-commit hook. The way this is currently being pulled off is by embedding the Ruby code directly in a C string like so...
char * post_commit_hook = <<Ruby code here>>
It's then written directly to .git/hooks/post-commit by way of fprintf.
This is somewhat ugly and difficult to maintain IMO, and I was wondering if there was some way to move the Ruby code into its own file. I tried looking for ways to have GNU make to do text replacement on the fly, but somehow that still feels like a hack. Anyone have any ideas?
Put your code into its own file and generate a C header from that via make and a scripting language of your choice.
For example, the following make rule
hook.h : hook.rb
./rb2h POST_COMMIT_HOOK < $< > $#
together with this Perl script rb2h
#!perl -w
print "#define $ARGV[0] \\\n";
chomp, s/\\/\\\\/g, s/"/\\"/g, print "\t\"$_\" \\\n" for <STDIN>;
print "\t\"\"\n"
will generate a file hook.h which defines the macro POST_COMMIT_HOOK containing your code.

Resources