go away spammer

Author Topic: Code embedding with High-Level programing languages?  (Read 2089 times)

0 Members and 1 Guest are viewing this topic.

Offline SciTech02Topic starter

  • Robot Overlord
  • ****
  • Posts: 136
  • Helpful? 3
  • Life's but a green duck with soy sauce. -MegaHAL
Code embedding with High-Level programing languages?
« on: December 07, 2008, 12:34:32 AM »
This may be an obvious question, but it's been on my mind lately.  I've been researching some internet programing languages and I noticed that code from one language could be inserted right into the middle of another code, written in another programing language.  This process is referred to as embedding.  For example, a PHP code is embedded right into a HTML code on this image I found on Wikipedia (the colored syntax is the PHP code):


(This is also done with Java, in the various "Java Apps" on various websites [written in HTML])

Now this could be a useful feature in robot programing; I could create a script in a simple, general language and use a more complicated language in sections were it is needed (it would be a hybrid code).  However, I have not seen this implemented in the high-level languages we use (a C++ code embedded into a Java or V.B. code, etc.).  Is it possible to do this with high-level languages, or is embedding only a feature of HTML?  If it is possible, would it require separate compilers (for each embedded language)?
Check out the Evolution Robotics, ER1 robot, and ERSP Resource Page: http://www.societyofrobots.com/member_tutorials/node/336

Offline cosminprund

  • Robot Overlord
  • ****
  • Posts: 284
  • Helpful? 8
Re: Code embedding with High-Level programing languages?
« Reply #1 on: December 07, 2008, 01:26:05 AM »
What you've shown is not actually a feature of HTML but a feature of PHP. PHP is a language developed to make writing dynamic web pages easy sot you'd expect an easy way to generate plain HTML. It's not apparent in the image you've shown but the sample you're showing is actually from a PHP file, not a HTML file (even those it's mostly HTML).

When it comes to embedding JavaScript into the HTML file - that's true embedding but the JavaScript is actually interpreted by the web browser (Internet Explorer, Mozilla, Opera, Safari, WhatYouUse). With some effort this approach can be used to embed anything into anything but this doesn't mean a language is embedded into an other language, this means we're embedding some interpretable code into some storage (ex: JavaScript into HTML). This doesn't automatically work and embedding an interpreter into an application is a big deal and it can't be done on a tiny MCU!

If we're talking about general-purpose languages it doesn't usually make sense to allow one compiler to compile two kinds of code. That's because any programming language can be used to write any kind of program so one language doesn't need the help of an other language to acheave an purpose. Ex: You can write any program in C++ or in BASIC. It doesn't make sense for C++ to compile BASIC because it doesn't add anything usefull. It doesn't make sense for BASIC to compile C++ because it doesn't add anything usefull. What you might find in high-level languages is a way to embed ASSEMBLER code - that's because compiled code is not as efficient as code written directly into assembler so being able to embed assembler code directly into a C file or a C++ file makes sense.

An other technique that may be used to make two distinct languages work within the same program is to compile two distinct files separately and then link them together using an linker. This requires the two separate languages to generate "object code" into some common format and then have an available linker to link the two pieces together. Ex: you can compile a PAS file and a C file with the GCC compiler and then use the linker to link them together into the same exe. Again, this is not exactly easy and is usually used when part of the program is allready written.

The conclusion would be simple: You can write any code in any language so you don't need to embed one language into an other.

Offline Admin

  • Administrator
  • Supreme Robot
  • *****
  • Posts: 11,703
  • Helpful? 173
    • Society of Robots
Re: Code embedding with High-Level programing languages?
« Reply #2 on: December 10, 2008, 03:07:00 AM »
Quote
The conclusion would be simple: You can write any code in any language so you don't need to embed one language into an other.
If I was writing a java program, but there was this really nice open source C program I wanted to include in my java program, it'd be nicer if the compiler can handle both languages rather than forcing me to rewrite that large C program into java.

Offline cosminprund

  • Robot Overlord
  • ****
  • Posts: 284
  • Helpful? 8
Re: Code embedding with High-Level programing languages?
« Reply #3 on: December 10, 2008, 04:28:08 AM »
Quote
The conclusion would be simple: You can write any code in any language so you don't need to embed one language into an other.
If I was writing a java program, but there was this really nice open source C program I wanted to include in my java program, it'd be nicer if the compiler can handle both languages rather than forcing me to rewrite that large C program into java.

Hmmm. That was "need" from the compiler's perspective. And I'm sure the java compiler would feel offended by asking it to compile C code, with all it's dirty and unpolished pointers and pointer arithmetic. It might refuse to compile any java code from that point on  ;D

 


Get Your Ad Here