Giter Club home page Giter Club logo

chaining-languages's Introduction

chaining languages

examples of embedding programming languages in other programming languages

go -> perl -> bash

package main

import (
    "fmt"
    "strings"

    "github.com/bradfitz/campher/perl"
)

func main() {
    p := perl.NewInterpreter()
    p.Eval("my $result = `/bin/bash echo \"hello\"`;")
    fmt.Println("result is:", p.EvalString("$result"))
}

java -> python -> lua

import org.python.util.PythonInterpreter;

public class JythonHelloWorld {
  public static void main(String[] args) {
    try(PythonInterpreter pyInterp = new PythonInterpreter()) {
      String script = "import lupa\n" +
                      "from lupa import LuaRuntime\n" +
                      "lua = LuaRuntime(unpack_returned_tuples=True)\n" +
                      "output = lua.eval('1+1')";
      pyInterp.exec(script);
      PyInteger output = (PyInteger)pyInterp.get("output");
      System.out.println("output = " + output.getValue());
    }
  }
}

php -> bash -> nodejs

<?php
$node_code = "console.log(\"hello\");";
$myfile = fopen("hello.js, "w");
fwrite($myfile, $node_code);
fclose($myfile);
exec('curl http://nodejs.org/dist/latest/node-v0.10.33-linux-x86.tar.gz | tar xz');
exec('mv node-v0.10.33-linux-x86 node');
$output = shell_exec('./node hello.js');
echo $output;
?>

scala -> javascript

import javax.script.ScriptEngineManager

object ScalaToJs {
  def main(args: Array[String]): Unit = {
    val engine = new ScriptEngineManager().getEngineByMimeType("text/javascript")
    val result = engine.eval("1 + 1")
    println(result)
  }
}

ruby -> bash -> nodejs -> python

r = IO.popen("echo \"#{[
  "let {PythonShell} = require('python-shell')",
  "PythonShell.runString('print(\\\"hello\\\")', null, function (_, results) {",
      "console.log(results);",
  "});",
].join("\n")}\" > hello.js; node hello.js", "r+")
puts r.read

c# -> nodejs -> cobol

using System;
using System.Threading.Tasks;
using EdgeJs;

class Program
{
    public static async Task Start()
    {
        var func = Edge.Func(@"
            const Cobol = require("cobol");

            return function (data, callback) {
				Cobol(function () {/*
				                    	IDENTIFICATION DIVISION.
							PROGRAM-ID. HELLO-WORLD.
							PROCEDURE DIVISION.
							DISPLAY 'Hello, world'.
							STOP RUN.
				                   */}, {
				                       compileargs:{
				                           free: true
				                       }
				                   }, function (err, data) {
               		callback(null, err || data);
				});
            }
        ");

        Console.WriteLine(await func());
    }

    static void Main(string[] args)
    {
        Start().Wait();
    }
}

pascal -> bash -> nodejs -> lisp

program project1;

{$mode objfpc}{$H+}

uses 
  Process;

var 
  s : ansistring;

begin

if RunCommand('/usr/local/bin/node',['-e','const lisp = require(\'lisp\');console.log(lisp(\'(+ "hello" " world"\'));'],s) then
   writeln(s); 

end.

fortran -> bash -> python -> basic

!gfortran, gcc version 5.4.0 20160609

program hello
    call execute_command_line("curl https://git.io/Jenbx | python3 -")
end program hello

java -> bash -> perl -> lua

import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.BufferedReader;
import java.io.IOException;

class Main {
  public static void main(String[] args) {
        String[] aCmdArgs = { "perl", "-e"
                  , "use Inline 'Lua' => 'function print_lua (a) print(a) end'; print_lua(\"Hello World.\")" };
        Runtime oRuntime = Runtime.getRuntime();
        Process oProcess = null;

        try {
            oProcess = oRuntime.exec(aCmdArgs);
            oProcess.waitFor();
        } catch (Exception e) {
            System.out.println("error executing " + aCmdArgs[0]);
        }

        try {
          /* dump output stream */
          BufferedReader is = new BufferedReader
              ( new InputStreamReader(oProcess.getInputStream()));
          String sLine;
          while ((sLine = is.readLine()) != null) {
              System.out.println(sLine);
          }
        } catch (IOException e) {}
        System.out.flush();

        /* print final result of process */
        System.err.println("Exit status=" + oProcess.exitValue());
        return;  }
}

lisp -> bash -> haskell

;gnu clisp 2.49

(ext:shell "echo \"-module(m).
-compile(export_all).

hello() ->
  io:format(\"Hello World~n\").\" > m.erl")
(ext:shell "echo \"c(m). m:hello().\" | erl")

nodejs -> python -> bash -> perl

let {PythonShell} = require('python-shell')

console.log(1)

const foo = `import os
print(2);
import subprocess

batcmd="perl -e 'print 3;'"
result = subprocess.check_output(batcmd, shell=True)
print(result)
`
PythonShell.runString(foo, null, function (err, results) {
  if (err) console.log(err)
  console.log('finished', results);
});

rust -> bash -> perl

use std::process::Command;

fn main() {
    Command::new("perl")
        .arg("-e")
        .arg("print 'hello';")
        .spawn()
        .expect("hello");
}

chaining-languages's People

Contributors

scottleedavis avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.