Can I use 2 languages to make 1 program? [duplicate] - c

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
Writing a program in 2 languages?
Note: I've seen Writing a program in 2 languages? but found no good answer IMO.
Can I use 2, 3, or even 4 languages to make a single program? What do I need to do to accomplish this (probably in Perl and C)? I am using Windows 7 64-bit.

As people have already mentioned, yes you can as long as there are bindings. Inline::C provides easy access to C from Perl, although I find XS to be almost as easy once you start to get the hang of it.
For now:
#!/usr/bin/env perl
use strict;
use warnings;
use Inline C => <<'END_C';
char* name () {
return "Joel";
}
END_C
print name() . "\n";
So that begs the question, what are you trying to do? If you need something more in-depth, you can look at the source for my (work in progress) Math::GSLx::ODEIV2.

The relevant Perl documentation is perlembed if you want to execute Perl code from C and perlxstut if you want to execute C code from Perl.

One approach is to use some of the Perl modules in the
Inline::* namespace. For example, Inline::C allows you to write C subroutines directly in your Perl source file. The module transparently handles compiling the C routines to a shared library and dynamically loading the library. There are 'Inline' modules for a variety of languages - in varying states of completeness and support.

Sure, you can use as many languages as you want. You just need the proper tools to bind the units together. Each language has its own builtin support to interoperate with other languages, and often libraries that do that as well. The exact tools you need would depend on the languages you use, but every language I know supports at least binding to C functions.

Check out chapter 21.3. Extending Perl (Using C from Perl) in Programming Perl.

If you're looking for Perl binding for C, take a look at swig.

Related

Turning strings into code?

So let's say I have a string containing some code in C, predictably read from a file that has other things in it besides normal C code. How would I turn this string into code usable by the program? Do I have to write an entire interpreter, or is there a library that already does this for me? The code in question may call subroutines that I declared in my actual C file, so one that only accounts for stock C commands may not work.
Whoo. With C this is actually pretty hard.
You've basically got a couple of options:
interpret the code
To do this, you'll hae to write an interpreter, and interpreting C is a fairly hard problem. There have been C interpreters available in the past, but I haven't read about one recently. In any case, unless you reallY really need this, writing your own interpreter is a big project.
Googling does show a couple of open-source (partial) C interpreters, like picoc
compile and dynamically load
If you can capture the code and wrap it so it makes a syntactically complete C source file, then you can compile it into a C dynamically loadable library: a DLL in Windows, or a .so in more variants of UNIX. Then you could load the result at runtime.
Now, what normally would lead someone to do this is a need to be able to express some complicated scripting functions. Have you considered the possibility of using a different language? Python, Scheme (guile) and Lua are easily available to add as a scripting language to a C application.
C has nothing of this nature. That's because C is compiled, and the compiler needs to do a lot of building of the code before the code starts running (hence receives a string as input) that it can't really change on the fly that easily. Compiled languages have a rigidity to them while interpreted languages have a flexibility.
You're thinking of Perl, Python PHP etc. and so called "fourth generation languages." I'm sure there's a technical term in c.s. for this flexibility, but C doesn't have it. You'll need to switch to one of these languages (and give up performance) if you have a task that requires this sort of string use much. Check out Perl's /e flag with regexes, for instance.
In C, you'll need to design your application so you don't need to do this. This is generally quite doable, as for its non-OO-ness and other deficiencies many huge, complex applications run on well-written C just fine.

Do dictionaries exist in C?

In C, can you create a dictionary? I come from a Objective-C background so I would like to know if there is anything similar to NSDictionary.
You can create anything you want in C. You just won't have native language support for most of it.
You can create a dictionary in C, but there is no dictionary built in to the standard C library.
A quick search on Google code shows that there are open-source (and generously licensed) C dictionary implementations here and here.
Posix does have a limited hash table -- see hcreate(), hsearch() and hdestroy() that can be used by a C program.
A discussion of the limitations appears in this stackoverflow question.
Without OOP and templates, it would be hard to implement a hash table or a balanced tree that is truly general, easy to use and performant, and therefore worthy to be in the run-time library that comes with the language.
That being said, you can always implement your own, or just use C++ (see unordered_map or map).

Is there a way to translate C code to Ruby?

I'm sitting with massive amounts of legacy C code that needs to be converted to Ruby for a project.
I've seen Ruby to C translators online, but not the other way around. Would there be a simple way to approach this particular problem?
You'll either have to write a C to Ruby translator, which is possible but the effort might not be justifiable, or you could split the C code up into smaller modules that you can create Ruby wrappers for as a first step. Once they're all wrapped in Ruby and the main control flow is done in Ruby, you can write a test harness (both to verify correctness of your replacement code and to aid reverse engineering) and start replacing the C modules with Ruby modules.
The divide & conquer approach should work with regular Ruby if you use the modules as native extensions but obviously this will cause further problems if you're targeting something like JRuby as your runtime environment. If you want to do something similar in JRuby as per your comment, you're looking at wrapping the C modules in JNI and calling through from the JVM that way. Either way will allow your C code to interact with the Ruby code, but the two approaches are not interchangeable.
Neither approach is going to be quick and both are going to be a lot of work.
You could:
Write a C interpreter in Ruby (very hard).
Wrap the compiled C code with something like SWIG (much easier).
Programming in C and programming in Ruby bear completely different programming paradigms. So while the old saying that you can write Fortran (or C in this case) code in any language is true, the Ruby code that you would eventually get by machine translation wouldn't be Ruby at all, except syntactically.
So, IMHO, any way other than manual (and done by proficient Rubyists, I might add) would be either impossible, or at least not useful at all.
Maybe you can try this https://github.com/jackieju/CPP2Ruby/
It's fresh for cpp but existed longer for c to ruby
You can try Abap2Ruby. It can also convert cpp to ruby.

How do I compile Perl code within a C program?

I have a C program with an embedded Perl interpreter. I want to be able to precompile some Perl code from within the program. How do I do that?
Rationale (if anyone is interested) is to be able to compile it once, store the parse tree, and execute many times (as long as the compiled code does not change).
Thanks!
Madhu
PS: I am using Perl-5.8, though it would be good to know if Perl-6.0 makes this easier in any way.
This is the default behavior when you embed the Perl interpreter in a C program. After you've run perl_parse() to parse the Perl program, you can use perl_run() and call_argv() over and over with the same parsed Perl program.
You can use perlcc to create executable or C sources.
To compile your C code, you would need to use perl's library (could be -lperl or -llibperl).
In reference to Perl 6, it's not complete. But 5.10.0 might have some bug fixes over 5.8.

C Analog To STL

Just because I'm curious--is there any C analog to the functionality of the STL in C++? I've seen mention of a GTK+ library called glib that a few people consider fills the bill but are there other libraries that would provide STL functionality in C?
Yes, glib is a pretty good choice: it includes a lot of utilities for manipulating containers like linked lists, arrays, hash tables, etc. And there is also an object-oriented framework called GObject that you can use to make objects with signals and slots in C (albeit with rather verbose function call names like gobject_set_property, since C doesn't have any syntax for objects). And there is also code for main loops so you can write event-driven programs.
For more info see wikipedia: https://en.wikipedia.org/wiki/GLib
Glib was originally part of GTK, but the non-GUI code has been completely factored out so that you can use it in command-line programs: http://library.gnome.org/devel/glib/stable/
CLIB
Adding another option (full disclosure, I am the author); if you can compile and link C++, you can have a look into libcdada, which has a pure C API, but uses libstdc++ as a backend for most of the containers:
https://github.com/msune/libcdada
Well since STL's very nature is based on templates which C doesn't have, it would be difficult to even come close to the STL in C. The best you could hope for is some collection classes which manipulate void* pointers to unknown object.

Resources