1. Hello, Connector/NET(F#) World!

    Posted on 4月 5th, 2013 by cx20

    Connector/NET(F#)

    Connector/NET は、.NET ベースの MySQL 接続用 API である。
    以下は F# による Connector/NET ライブラリを使用した MySQL への接続例となっている。

    ソースコード(JScript.NET + Connector/NET + MySQL)

    open System;
    open MySql.Data.MySqlClient;
     
    let conStr = "server=localhost;user id=root;password=P@ssW0rd"
    let con = new MySqlConnection(conStr)
    let sqlStr = "SELECT 'Hello, Connector/NET World' AS Message"
    let cmd = new MySqlCommand(sqlStr, con)
    con.Open()
    let reader = cmd.ExecuteReader()
    while reader.Read() do
        printfn "%s" (reader.GetName(0))
        printfn "---------------------" 
        printfn "%s" (reader.GetString(0))
    reader.Close()
    con.Close()

    コンパイル方法

    C:¥> fsc Hello.fs -r:MySql.Data.dll

    実行結果

    MESSAGE
    ---------------------
    Hello, Connector/NET World!
  2. Hello, Connector/NET(JScript.NET) World!

    Posted on 4月 4th, 2013 by cx20

    Connector/NET(JScript.NET)

    Connector/NET は、.NET ベースの MySQL 接続用 API である。
    以下は JScript.NET による Connector/NET ライブラリを使用した MySQL への接続例となっている。

    ソースコード(JScript.NET + Connector/NET + MySQL)

    import System;
    import System.Data;
    import System.Drawing;
    import MySql.Data.MySqlClient;
     
    main();
     
    function main() {
        var conStr = "server=localhost;user id=root;password=P@ssW0rd";
        var sqlStr = "SELECT 'Hello, Connector/NET World!' AS Message";
     
        var con = new MySqlConnection(conStr);
        var cmd = new MySqlCommand(sqlStr, con);
        con.Open();
        var reader = cmd.ExecuteReader();
        while( reader.Read() )
        {
            Console.WriteLine( reader.GetName(0) );
            Console.WriteLine( "---------------------" );
            Console.WriteLine( reader[0] );
        }
        reader.Close();
        con.Close();
    }

    コンパイル方法

    C:¥> jsc /r:MySql.Data.dll Hello.js

    実行結果

    MESSAGE
    ---------------------
    Hello, Connector/NET World!
  3. Hello, Connector/NET(C++/CLI) World!

    Posted on 4月 3rd, 2013 by cx20

    Connector/NET(C++/CLI)

    Connector/NET は、.NET ベースの MySQL 接続用 API である。
    以下は C++/CLI による Connector/NET ライブラリを使用した MySQL への接続例となっている。

    ソースコード(C++/CLI + Connector/NET + MySQL)

    #using <System.dll>
    #using <System.Data.dll>
    #using <MySql.Data.dll>
     
    using namespace System;
    using namespace MySql::Data::MySqlClient;
     
    int main( array<System::String^>^ args )
    {
        String^ conStr = "server=localhost;user id=root;password=P@ssW0rd";
        String^ sqlStr = "SELECT 'Hello, Connector/NET World!' AS Message";
     
        MySqlConnection^ con = gcnew MySqlConnection(conStr);
        MySqlCommand^ cmd = gcnew MySqlCommand(sqlStr, con);
        con->Open();
        MySqlDataReader^ reader = cmd->ExecuteReader();
        while( reader->Read() )
        {
            Console::WriteLine( reader->GetName(0) );
            Console::WriteLine( "---------------------" );
            Console::WriteLine( reader[0] );
        }
        reader->Close();
        con->Close();
     
       return 0;
    }

    コンパイル方法

    C:¥> SET LIBPATH=C:\Program Files (x86)\MySQL\Connector NET 6.5.4\Assemblies\v2.0;%LIBPATH%
    C:¥> cl Hello.cpp /clr

    実行結果

    MESSAGE
    ---------------------
    Hello, Connector/NET World!
  4. Hello, Connector/NET(VB.NET) World!

    Posted on 4月 2nd, 2013 by cx20

    Connector/NET(VB.NET)

    Connector/NET は、.NET ベースの MySQL 接続用 API である。
    以下は VB.NET による Connector/NET ライブラリを使用した MySQL への接続例となっている。

    ソースコード(VB.NET + Connector/NET + MySQL)

    Imports System
    Imports MySql.Data.MySqlClient
     
    Class Hello
        Shared Sub Main()
            Dim conStr As String = "server=localhost;user id=root;password=P@ssW0rd"
            Dim sqlStr As String = "SELECT 'Hello, Connector/NET World!' AS Message"
     
            Dim con As MySqlConnection = New MySqlConnection(conStr)
            Dim cmd As MySqlCommand = New MySqlCommand(sqlStr, con)
            con.Open()
            Dim reader As MySqlDataReader = cmd.ExecuteReader()
            While reader.Read()
                Console.WriteLine( reader.GetName(0) )
                Console.WriteLine( "---------------------" )
                Console.WriteLine( reader(0) )
            End While
            reader.Close()
            con.Close()
        End Sub
    End Class

    コンパイル方法

    C:¥> vbc /r:MySql.Data.dll Hello.vb

    実行結果

    MESSAGE
    ---------------------
    Hello, Connector/NET World!
  5. Hello, Connector/NET World!

    Posted on 4月 1st, 2013 by cx20

    Connector/NET

    Connector/NET は、.NET ベースの MySQL 接続用 API である。
    以下は C# による Connector/NET ライブラリを使用した MySQL への接続例となっている。

    ソースコード(C# + Connector/NET + MySQL)

    using System;
    using MySql.Data.MySqlClient;
     
    class Hello
    {
        static void Main( String[] args )
        {
            string conStr = "server=localhost;user id=root;password=P@ssW0rd";
            string sqlStr = "SELECT 'Hello, Connector/NET World!' AS Message";
     
            MySqlConnection con = new MySqlConnection(conStr);
            MySqlCommand  cmd = new MySqlCommand(sqlStr, con);
            con.Open();
            MySqlDataReader reader = cmd.ExecuteReader();
            while( reader.Read() )
            {
                Console.WriteLine( reader.GetName(0) );
                Console.WriteLine( "---------------------" );
                Console.WriteLine( reader[0] );
            }
            reader.Close();
            con.Close();
        }
    }

    コンパイル方法

    C:¥> csc /r:MySql.Data.dll Hello.cs

    実行結果

    MESSAGE
    ---------------------
    Hello, Connector/NET World!
  6. Hello, Windows Forms(Tcl/CSharp) World!

    Posted on 3月 31st, 2013 by cx20

    Windows Forms(Tcl/CSharp)

    Windows フォーム(Windows Forms)は .NET Framework におけるユーザーインターフェイス基盤である。Windows アプリケーションにおけるウィンドウやダイアログに対応する。
    以下は Tcl/CSharp における Windows フォーム の例となっている。

    ソースコード

    package require java
     
    java::load -gac System.Windows.Forms.dll
    java::import -package System.Windows.Forms Form Application Label
    set form [java::new Form]
    java::prop $form Text "Hello, World!"
    java::prop $form Size [java::new System.Drawing.Size 640 480]
    set label1 [java::new Label]
    java::prop $label1 Text "Hallo, Tcl/CSharp World!"
    java::prop $label1 Size [java::new System.Drawing.Size 320 20]
    [java::prop $form Controls] Add $label1
     
    java::call Application Run $form

    コンパイル方法

    C:¥> tclsh Hello.tcl

    実行結果

    +------------------------------------------+
    |Hello, World!                    [_][~][X]|
    +------------------------------------------+
    |Hello, Windows Forms(Tcl/CSharp) World!   |
    |                                          |
    |                                          |
    |                                          |
    |                                          |
    |                                          |
    |                                          |
    |                                          |
    |                                          |
    |                                          |
    +------------------------------------------+
  7. Hello, Tcl/CSharp World!

    Posted on 3月 27th, 2013 by cx20

    Tcl/CSharp

    Tcl/CSharp は Tcl の Java 実装である「Tcl/Java」の C# 移植版である。

    ソースコード

    puts "Hello, Tcl/CSharp World!"

    ソースコード(.NET ライブラリを使用)

    package require java
    java::call System.Console WriteLine "Hello, Tcl/CSharp World!"

    実行方法

    C:¥> tclsh hello.tcl

    実行結果

    Hello, Tcl/CSharp World!
  8. Hello, Win32 API(CNI) World!

    Posted on 3月 26th, 2013 by cx20

    Win32 API(CNI)

    Win32 API は、Windows の機能にアクセスする為の API(Application Programming Interface)である。
    以下は、GCJ にて CNI を利用した Win32 API の呼出し例となっている。

    ソースコード(Java)

    public class Hello {
        public static native int messageBox( int hwnd, String text, String caption, int type );
     
        public static void main( String[] args ) {
            messageBox( 0, "Hello, Win32 API World!

    クラスファイル作成

    C:¥> gcj -C Hello.java

    C++ ヘッダファイル作成

    C:¥> gcjh Hello.class

    ソースコード(C++ ヘッダファイル)

    // DO NOT EDIT THIS FILE - it is machine generated -*- c++ -*-
     
    #ifndef __Hello__
    #define __Hello__
     
    #pragma interface
     
    #include <java/lang/Object.h>
    #include <gcj/array.h>
     
    extern "Java"
    {
        class Hello;
    }
     
    class Hello : public ::java::lang::Object
    {
     
    public:
      Hello();
      static jint messageBox(jint, ::java::lang::String *, ::java::lang::String *, jint);
      static void main(JArray< ::java::lang::String * > *);
      static ::java::lang::Class class$;
    };
     
    #endif // __Hello__

    ソースコード(C++ 実装ファイル)

    #include <gcj/cni.h>
    #include <windows.h>
    #include "Hello.h"
     
    jint Hello::messageBox(jint hwnd, ::java::lang::String* text, ::java::lang::String* caption, jint type)
    {
        jchar* _text = JvGetStringChars( text );
        jchar* _caption = JvGetStringChars( caption );
        int result = MessageBoxW( (HWND)hwnd, (LPCWSTR)_text, (LPCWSTR)_caption, (UINT)type );
        return result;
    }

    コンパイル方法

    C:¥> gcj -c Hello.cpp
    C:¥> gcj --main=Hello -o Hello Hello.o Hello.class

    実行結果

    ---------------------------
    Hello, World!
    ---------------------------
    Hello, Win32 API World!
    ---------------------------
    OK   
    ---------------------------
  9. Hello, GCJ World!

    Posted on 3月 25th, 2013 by cx20

    GCJ

    GCJ は GNU Compiler for the Java の略で、GCC に含まれるコンパイラの1つである。
    Java のライブラリが使用できるほか、CNI(Compiled Native Interface)を用いることで、C++ で作成したライブラリを呼び出すことが可能となっている。

    ソースコード(Java)

    public class Hello {
        public static native void printf( String format );
     
        public static void main( String[] args ) {
            printf( "Hello, GCJ World!n" );
        }
    }

    クラスファイル作成

    C:¥> gcj -C Hello.java

    C++ ヘッダファイル作成

    C:¥> gcjh Hello.class

    ソースコード(C++ ヘッダファイル)

     
    // DO NOT EDIT THIS FILE - it is machine generated -*- c++ -*-
     
    #ifndef __Hello__
    #define __Hello__
     
    #pragma interface
     
    #include <java/lang/Object.h>
    #include <gcj/array.h>
     
    extern "Java"
    {
        class Hello;
    }
     
    class Hello : public ::java::lang::Object
    {
     
    public:
      Hello();
      static void printf(::java::lang::String *);
      static void main(JArray< ::java::lang::String * > *);
      static ::java::lang::Class class$;
    };
     
    #endif // __Hello__

    ソースコード(C++ 実装ファイル)

    #include <stdio.h>
    #include <java/lang/String.h>
    #include "Hello.h"
     
    void ::Hello::printf(::java::lang::String* format )
    {
        int len = format->length();
        for ( int i = 0; i < len; i++ )
        {
            ::printf( "%c", format->charAt(i) );
        }
    }

    コンパイル方法

    C:¥> gcj -c Hello.cpp
    C:¥> gcj --main=Hello -o Hello Hello.o Hello.class

    実行結果

    Hello, GCJ World!
  10. Hello, Emscripten World!

    Posted on 3月 22nd, 2013 by cx20

    Emscripten

    Emscripten は LLVM アセンブリコードより JavaScript のコードを生成する変換ツールである。
    clang/clang++ を用いることにより C/C++ より JavaScript へのコード変換が可能となっている。

    ソースコード(C言語)

    #include <stdio.h>
     
    int main( int argc, char* argv[] )
    {
        printf( "Hello, Emscripten World!\n" );
        return 0;
    }

    上記コードを LLVM に変換した場合、以下のコードが生成される。

    ソースコード(LLVM Assembler)

    ; ModuleID = 'hello.c'
    target datalayout = "e-p:32:32:32-i1:8:8-i8:8:8-i16:16:16-i32:32:32-i64:64:64-f32:32:32-f64:64:64-f80:128:128-v64:64:64-v128:128:128-a0:0:64-f80:32:32-n8:16:32-S32"
    target triple = "i686-w64-mingw32"
     
    @str = private unnamed_addr constant [25 x i8] c"Hello, Emscripten World!\00"
     
    define i32 @main(i32 %argc, i8** nocapture %argv) nounwind {
      %puts = tail call i32 @puts(i8* getelementptr inbounds ([25 x i8]* @str, i32 0, i32 0))
      ret i32 0
    }
     
    declare i32 @puts(i8* nocapture) nounwind

    上記コードを JavaScript に変換した場合、以下のコードが生成される。

    ソースコード(JavaScript)

       (中略)
          :
    var FUNCTION_TABLE = [0, 0];
    function _main($argc, $argv) {
      var label = 0;
      var $puts=_puts(((5242880)|0));
      return 0;
    }
    Module["_main"] = _main;
    // Warning: printing of i64 values may be slightly rounded! No deep i64 math used, so precise i64 code not included
    var i64Math = null;
    // === Auto-generated postamble setup entry stuff ===
    Module.callMain = function callMain(args) {
      var argc = args.length+1;
      function pad() {
        for (var i = 0; i < 4-1; i++) {
          argv.push(0);
        }
      }
      var argv = [allocate(intArrayFromString("/bin/this.program"), 'i8', ALLOC_STATIC) ];
      pad();
      for (var i = 0; i < argc-1; i = i + 1) {
        argv.push(allocate(intArrayFromString(args[i]), 'i8', ALLOC_STATIC));
        pad();
      }
      argv.push(0);
      argv = allocate(argv, 'i32', ALLOC_STATIC);
      var ret;
      ret = Module['_main'](argc, argv, 0);
      return ret;
    }
    function run(args) {
      args = args || Module['arguments'];
      if (runDependencies > 0) {
        Module.printErr('run() called, but dependencies remain, so not running');
        return 0;
      }
      if (Module['preRun']) {
        if (typeof Module['preRun'] == 'function') Module['preRun'] = [Module['preRun']];
        var toRun = Module['preRun'];
        Module['preRun'] = [];
        for (var i = toRun.length-1; i >= 0; i--) {
          toRun[i]();
        }
        if (runDependencies > 0) {
          // a preRun added a dependency, run will be called later
          return 0;
        }
      }
      function doRun() {
        var ret = 0;
        calledRun = true;
        if (Module['_main']) {
          preMain();
          ret = Module.callMain(args);
          if (!Module['noExitRuntime']) {
            exitRuntime();
          }
        }
        if (Module['postRun']) {
          if (typeof Module['postRun'] == 'function') Module['postRun'] = [Module['postRun']];
          while (Module['postRun'].length > 0) {
            Module['postRun'].pop()();
          }
        }
        return ret;
      }
      if (Module['setStatus']) {
        Module['setStatus']('Running...');
        setTimeout(function() {
          setTimeout(function() {
            Module['setStatus']('');
          }, 1);
          doRun();
        }, 1);
        return 0;
      } else {
        return doRun();
      }
    }
    Module['run'] = Module.run = run;
    // {{PRE_RUN_ADDITIONS}}
    if (Module['preInit']) {
      if (typeof Module['preInit'] == 'function') Module['preInit'] = [Module['preInit']];
      while (Module['preInit'].length > 0) {
        Module['preInit'].pop()();
      }
    }
    initRuntime();
    var shouldRunNow = true;
    if (Module['noInitialRun']) {
      shouldRunNow = false;
    }
    if (shouldRunNow) {
      var ret = run();
    }
    // {{POST_RUN_ADDITIONS}}
      // {{MODULE_ADDITIONS}}
    // EMSCRIPTEN_GENERATED_FUNCTIONS: ["_main"]

    コンパイル方法(C言語 → LLVM Assembler)

    C:¥> clang hello.c -S -O4 -o hello.ll

    コンパイル方法(LLVM Assembler → JavaScript)

    C:¥> python emcc hello.ll -o hello.js

    実行方法(Node.js による実行例)

    C:¥> node Hello.js

    実行結果

    Hello, Emscripten World!