Euphoria-quick-guide

提供:Dev Guides
移動先:案内検索

ユーフォリア-概要

Euphoriaは* E * nd-* U ser P ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''* *A *アプリケーション。 Euphoriaの最初の化身は、Robert CraigによってAtari Mega-STで作成され、1993年に最初にリリースされました。 現在、Rapid Deployment Softwareによって保守されています。

無料で、シンプルで、柔軟で、学習しやすく、解釈されますが、DOS、Windows、Linux、FreeBSDなどのための非常に高速な32ビットの高レベルプログラミング言語です。

Euphoriaは、Windows GUIプログラム、高速DOSゲーム、およびLinux/FreeBSD X Windowsプログラムの開発に使用されています。 Euphoriaは、CGI(Webベース)プログラミングにも使用できます。

陶酔の特徴

ここにユーフォリアの主要な機能のリストがあります-

  • これは、学習しやすく使いやすい、シンプルで柔軟、強力な言語定義です。
  • 動的なストレージ割り当てをサポートします。つまり、プログラマーがメモリの割り当てと解放を心配することなく、変数の増減を意味します。 ガベージコレクションは自動的に処理されます。
  • PerlやPythonなどの従来のインタープリターよりも非常に高速です。
  • Euphoriaプログラムは、Linux、FreeBSD、32ビットWindows、および任意のDOS環境で実行されます。
  • Euphoriaプログラムには、640Kのメモリ制限はありません。
  • Euphoria-To-Cトランスレータを最適化し、EuphoriaプログラムをCに変換し、Cコンパイラでコンパイルして実行可能(.exe)ファイルを取得します。 これにより、プログラムの速度が2〜5倍になります。
  • 基礎となるハードウェアは完全に隠されているため、プログラムはワード長、基になるビットレベルの値表現、バイト順などを認識しません。
  • Euphoriaのインストールには、フルスクリーンのソースデバッガ、実行プロファイラ、およびフルスクリーンのマルチファイルエディタが付属しています。
  • 実行時のエラー処理、添え字、型チェックをサポートしています。
  • これはオープンソース言語であり、完全に無料です。

プラットフォーム要件

Euphoriaは、Windows、Linux、FreeBSD、およびOSXで利用可能です。 これは、次のプラットフォームで必要な最低限のバージョンです-

  • * WIN32バージョン*-Windows 95またはそれ以降のバージョンのWindowsが必要です。 XPおよびVistaで正常に動作します。
  • * Linuxバージョン*-libc6以降を搭載した、合理的に最新のLinuxディストリビューションが必要です。 たとえば、Red Hat 5.2以降は正常に動作します。
  • * FreeBSDバージョン*-合理的に最新のFreeBSDディストリビューションが必要です。
  • * Mac OS Xバージョン*-合理的に最新のIntelベースのMacが必要です。

幸福感の制限

ここにユーフォリアのいくつかの顕著な制限があります-

  • Euphoriaは、プログラマーにとって十分にシンプル、高速、かつ柔軟ですが、多くの重要な機能の呼び出しサポートは提供しません。 たとえば、ネットワークプログラミング。
  • 幸福感は1993年に発明されましたが、この言語で書かれた本はまだ見つかりません。 また、その言語で利用できるドキュメントはあまりありません。

しかし、最近では、この言語は非常に急速に普及しており、すぐにその言語で利用できる便利なユーティリティや書籍を手に入れることができます。

ユーフォリアライセンス

この製品は無料でオープンソースであり、多くの人々の貢献により恩恵を受けています。 開発したEuphoriaプログラムを配布するための完全なロイヤリティフリーの権利があります。

euphoria.icoやeuphoria \ binにあるバイナリなどのアイコンファイルは、変更の有無にかかわらず配布される場合があります。

プログラムを*シュラウド*または*バインド*して、結果ファイルをロイヤリティフリーで配布できます。 Euphoria-To-Cトランスレータを使用する場合、サードパーティの追加の法的制限が適用される場合があります。

寛大なhttp://www.rapideuphoria.com/License.txt[オープンソースライセンス]により、Euphoriaは個人的および商業目的の両方で使用できます。 他の多くのオープンソースライセンスとは異なり、変更をオープンソースにする必要はありません。

幸福感-環境

この章では、さまざまなプラットフォームでのEuphoriaのインストールについて説明します。 手順に従って、Linux、FreeBSD、および32ビットWindowsにEuphoriaをインストールできます。 そのため、作業環境に基づいて手順を選択できます。

Linux、無料BSDインストール

公式Webサイトでは、EuphoriaをLinuxまたはBSD OSにインストールするための .tar.gz ファイルを提供しています。 Euphoriaの最新バージョンは、公式ウェブサイトからダウンロードできます-http://www.rapideuphoria.com/[Download Euphoria]。

tar.gzファイルを入手したら、次の3つの簡単な手順でLinuxまたはFree BSDマシンにEuphoriaをインストールします-.

  • ステップ1 *-ファイルのインストール

ダウンロードしたファイル euphoria-4.0b2.tar.gz を、Euphoriaをインストールするディレクトリに展開します。 次のように/homeディレクトリにインストールする場合は、-

$cp euphoria-4.0b2.tar.gz/home
$cd/home
$gunzip euphoria-4.0b2.tar.gz
$tar -xvf euphoria-4.0b2.tar

これにより、次のように /home/euphoria-4.0b2 ディレクトリ内にディレクトリ階層が作成されます-

$ls -l
-rw-r--r-- 1 1001 1001 2485 Aug 17 06:15 Jamfile
-rw-r--r-- 1 1001 1001 5172 Aug 20 12:37 Jamrules
-rw-r--r-- 1 1001 1001 1185 Aug 13 06:21 License.txt
drwxr-xr-x 2 1001 1001 4096 Aug 31 10:07 bin
drwxr-xr-x 7 1001 1001 4096 Aug 31 10:07 demo
-rw-r--r-- 1 1001 1001 366 Mar 18 09:02 file_id.diz
drwxr-xr-x 4 1001 1001 4096 Aug 31 10:07 include
-rw-r--r-- 1 1001 1001 1161 Mar 18 09:02 installu.doc
drwxr-xr-x 4 1001 1001 4096 Aug 31 10:07 source
drwxr-xr-x 19 1001 1001 4096 Sep 7 12:09 tests
drwxr-xr-x 2 1001 1001 4096 Aug 31 10:07 tutorial

-ファイル名euphoria-4.0b2.tar.gzは、利用可能な最新バージョンに依存します。 このチュートリアルでは、4.0b2バージョンの言語を使用しています。

  • ステップ2 *-パスの設定

Euphoriaをインストールしたら、シェルが必要なEuphoriaのバイナリとユーティリティを見つけることができるように適切なパスを設定する必要があります。 先に進む前に、次の3つの重要な環境変数を設定する必要があります-

  • /home/euphoria-4.0b2/binディレクトリを指すようにPATH環境変数を設定します。 * EUDIR環境変数を/home/euphoria-4.0b2を指すように設定します。 * EUINC環境変数が/home/euphoria-4.0b2/includeを指すように設定します。

これらの変数は次のように設定できます-

$export PATH=$PATH:/home/euphoria-4.0b2/bin
$export EUDIR=/home/euphoria-4.0b2
$export EUINC=/home/euphoria-4.0b2/include

-環境変数を設定するために使用される上記のコマンドは、シェルによって異なる場合があります。 これらのコマンドを実行して変数を設定するには、_bash_シェルを使用しました。

  • ステップ3 *-インストールの確認

Euphoriaを正常にインストールしたかどうかを確認します。

次のコマンドを実行します-

$eui -version

次の結果が得られた場合、Euphoriaが正常にインストールされたことを意味します。それ以外の場合は、戻ってすべての手順をもう一度確認する必要があります。

$eui -version
Euphoria Interpreter 4.0.0 beta 2 (r2670) for Linux
Using System Memory
$

それで、Euphoria Programming EnvironmentがUNIXマシンで準備でき、簡単な手順で複雑なプログラムの作成を開始できます。

WIN32およびDOSインストール

公式Webサイトでは、EuphoriaをWIN32またはDOS OSにインストールするための .exe ファイルを提供しています。 Euphoriaの最新バージョンは、公式ウェブサイトからダウンロードできます-http://www.rapideuphoria.com/[Download Euphoria]。

exeファイルを取得したら、WIN32またはDOSマシンにEuphoriaプログラミング言語をインストールするための3つの簡単な手順を示します-.

  • ステップ1 *-ファイルのインストール

ダウンロードした .exe セットアッププログラムをダブルクリックして、すべてのファイルをインストールします。 インストール用にeuphoria-40b2.exeファイルをダウンロードしました。

ファイル名euphoria-40b2.exeは、利用可能な最新バージョンによって異なります。 言語のバージョン4ベータ2を使用します。

デフォルトでは、EuphoriaはC:\ _ euphoria-40b2_ディレクトリにインストールされますが、目的の場所を選択することもできます。

  • ステップ2 *-マシンの再起動

マシンを再起動して、インストールを完了します。

  • ステップ3 *-インストールの確認

Euphoriaを正常にインストールしたかどうかを確認します。

次のコマンドを実行します-

c:\>eui -version

次の結果が得られた場合、Euphoriaが正常にインストールされたことを意味します。それ以外の場合は、戻ってすべての手順をもう一度確認する必要があります。

c:\>eui -version
Euphoria Interpreter 4.0.0 beta 2 (r2670) for Windows
Using Managed Memory

c:\>

つまり、Euphoria Programming EnvironmentはWIN32マシン上で準備ができており、簡単な手順で複雑なプログラムの作成を開始できます。

陶酔通訳

使用しているプラ​​ットフォームに応じて、Euphoriaには複数のインタープリターがあります-

  • メインのインタープリターは eui です。 Windowsプラットフォームでは、2つの選択肢があります。 eui を実行すると、コンソールウィンドウが作成されます。 *euiw を実行すると、コンソールは作成されず、GUIアプリケーションに適したものになります。
  • Euphoriaは、ファイル拡張子の選択を気にしません。 ただし、慣例により。コンソールベースのアプリケーションには、*。ex *拡張子が付いています。
  • GUIベースのアプリケーションの拡張子は .exw で、インクルードファイルの拡張子は .e です。

Euphoria-基本的な構文

Euphoria言語には、Perl、C、およびJavaと多くの類似点があります。 ただし、言語間には明確な違いがあります。 この章は、Euphoriaで期待される構文をすぐに理解できるように設計されています。

このチュートリアルでは、Linuxを使用しており、すべての例がLinuxプラットフォームで作成されていることを前提としています。 しかし、LinuxとWIN32のプログラム構文に顕著な違いはないことが観察されています。 したがって、WIN32で同じ手順を実行できます。

最初の幸福感プログラム

スクリプトで簡単なEuphoriaプログラムを作成しましょう。 test.exファイルに次のソースコードを入力して保存します。

#!/home/euphoria-4.0b2/bin/eui

puts(1, "Hello, Euphoria!\n")

Euphoriaインタプリタは_/home/euphoria-4.0b2/bin/_ディレクトリにあります。 今、このプログラムを次のように実行します-

$ chmod +x test.ex    # This is to make file executable
$ ./test.ex

これは、次の結果を生成します-

Hello, Euphoria!

このスクリプトは、2つの引数を取る組み込み関数* puts()*を使用しました。 最初の引数はファイル名またはデバイス番号を示し、2番目の引数は印刷する文字列を示します。 ここで、1はSTDOUTデバイスを示します。

陶酔識別子

Euphoria識別子は、変数、関数、クラス、モジュール、またはその他のオブジェクトを識別するために使用される名前です。 識別子は、A〜Zまたはa〜zの文字で始まり、その後に文字、数字、またはアンダースコアが続きます。

Euphoriaでは、識別子内で@、$、%などの句読点を使用できません。

Euphoriaは、大文字と小文字を区別するプログラミング言語です。 したがって、 Manpowermanpower は、Euphoriaの2つの異なる識別子です。 たとえば、有効な識別子は-

  • n
  • 色26
  • シェルソート
  • quick_sort
  • a_very_long_indentifier

予約語

次のリストは、Euphoriaの予約語を示しています。 これらの予約語は、定数、変数、またはその他の識別子名として使用できません。 Euphoriaキーワードには小文字のみが含まれます。

and exit override
as export procedure
break fallthru public
by for retry
case function return
constant global routine
continue goto switch
do if then
else ifdef to
elsedef include type
elsif label until
elsifdef loop while
end namespace with
entry not without
enum or xor

Euphoriaでは、式を作成して結果を計算できます。 ただし、Euphoriaでは、1つの式でデータのシーケンス全体に対して計算を実行できます。

単一の数値を処理するのと同じように、シーケンスを処理できます。 コピーしたり、サブルーチンに渡したり、ユニットとして計算したりできます。 たとえば-

{1,2,3} + 5

これは、シーケンス\ {1、2、3}とアトム5を追加して、結果のシーケンス\ {6、7、8}を取得する式です。 後続の章でシーケンスを学習します。

コードのブロック

Euphoriaを学習する際にプログラマが最初に遭遇する警告の1つは、プロシージャと関数の定義またはフロー制御のコードブロックを示す中括弧がないことです。 コードのブロックは、関連するキーワードで示されます。

次の例は、 if …​ then …​ end if ブロックを示しています-

if condition then
   code block comes here
end if

複数行ステートメント

通常、Euphoriaのステートメントは新しい行で終わります。 ただし、Euphoriaでは、複数の行に1つのステートメントを記述できます。 たとえば-

total = item_one +
   item_two +
   item_three

エスケープ文字

エスケープ文字は、バックスラッシュを使用して入力できます。 たとえば-

次の表は、バックスラッシュ表記で表すことができるエスケープ文字または印刷不能文字のリストです。

Backslash notation Description
\n Newline
\r Carriage return
\t Tab
\\ Backslash
\" Double quote
\' Single quote

Euphoriaのコメント

コメントはコンパイラによって無視され、実行速度には影響しません。 プログラム内でより多くのコメントを使用して読みやすくすることをお勧めします。

コメントテキストには3つの形式があります-

  • コメントは2つのダッシュで始まり、現在の行の終わりまで続きます。 複数行形式のコメントは、別の行で発生する場合でも、/ …​ */内に保持されます。
  • プログラムの最初の行でのみ、2文字のシーケンス「#!」で始まる特別なコメントを使用できます。

#!/home/euphoria-4.0b2/bin/eui
-- First comment
puts(1, "Hello, Euphoria!\n") -- second comment

/* This is a comment which extends over a number
   of text lines and has no impact on the program
*/

これは、次の結果を生成します-

Hello, Euphoria!

注意-「#!」で始まる特別なコメントを使用できます。 これは、ファイルがEuphoriaインタープリターによって実行されるべきであることをLinuxシェルに通知します。

陶酔-変数

変数は、値を保存するために予約されたメモリの場所に他なりません。 これは、変数を作成するときに、メモリ内にいくらかのスペースを確保することを意味します。

変数のデータ型に基づいて、インタプリタはメモリを割り当て、予約メモリに保存できるものを決定します。 したがって、異なるデータ型を変数に割り当てることにより、これらの変数に整数、小数、または文字を格納できます。 Euphoriaのデータ型については、別の章で説明しています。

これらのメモリ位置は変数と呼ばれます。なぜなら、それらの値は寿命の間に変更できるからです。

可変宣言

メモリ空間を確保するには、Euphoria変数を明示的に宣言する必要があります。 したがって、変数の宣言は、値を変数に割り当てる前に必須です。

変数宣言には、型名の後に宣言されている変数のリストが続きます。 たとえば-

integer x, y, z

sequence a, b, x

変数を宣言するときは、変数に名前を付け、プログラムの実行中に変数に合法的に割り当てられる値の種類を定義します。

変数を宣言するという単純な行為は、変数に値を割り当てません。 値を割り当てる前に読み取ろうとすると、_ "変数xyzに値が割り当てられたことがない" _として、Euphoriaは実行時エラーを発行します。

値の割り当て

等号(=)は、変数に値を割り当てるために使用されます。 変数は次の方法で割り当てることができます-

変数名=変数値

たとえば-

#!/home/euphoria/bin/eui

-- Here is the declaration of the variables.
integer counter
integer miles
sequence name

counter = 100 -- An integer assignment
miles = 1000.0 -- A floating point
name = "John" -- A string ( sequence )

printf(1, "Value of counter %d\n", counter )
printf(1, "Value of miles %f\n", miles )
printf(1, "Value of name %s\n", {name} )

ここで、100、1000.0、および「John」は、それぞれ_counter miles_、および_name_変数に割り当てられた値です。 このプログラムは、次の結果を生成します-

Value of counter 100
Value of miles 1000.000000
Value of name John

変数を初期化することを忘れないように、またそれがコードを読みやすくするために、宣言と代入を組み合わせることができます-

integer n = 5

これは次と同等です-

integer n
n = 5

識別子の範囲

識別子のスコープは、どのコードがそれにアクセスできるかの説明です。 識別子の同じスコープ内のコードはその識別子にアクセスでき、識別子がアクセスできないのと同じスコープ内のコードはアクセスできません。

変数のスコープは、変数が宣言される場所と方法によって異なります。

  • for、while、loop または switch 内で宣言されている場合、そのスコープは宣言で始まり、それぞれの end ステートメントで終了します。
  • if ステートメントでは、スコープは宣言から始まり、次の else、elsif 、または end if ステートメントで終わります。
  • 変数がルーチン内で宣言されている場合、変数のスコープは宣言で始まり、ルーチンの終了ステートメントで終了します。 これはプライベート変数として知られています。
  • 変数がルーチンの外部で宣言されている場合、そのスコープは宣言で始まり、宣言されたファイルの終わりで終わります。 これはモジュール変数として知られています。
  • スコープ修飾子を持たない*定数*のスコープは、宣言で始まり、宣言されたファイルの終わりと終わりです。
  • スコープ修飾子を持たない enum のスコープは、宣言で始まり、宣言されたファイルの終わりと終わりです。
  • スコープ修飾子を持たないすべての procedures、functions および types のスコープは、ソースファイルの先頭から始まり、宣言されているソースファイルの末尾で終わります。

スコープ修飾子を持たない定数、列挙型、モジュール変数、プロシージャ、関数、および型は、 locals と呼ばれます。 ただし、これらの識別子には、宣言の前にスコープ修飾子を付けることができます。これにより、スコープは宣言されているファイルを超えて拡張されます。

  • キーワード global が宣言の前にある場合、これらの識別子の範囲はアプリケーション全体に広がります。 アプリケーションファイル内のどこからでもコードによってアクセスできます。
  • キーワード public が宣言の前にある場合、スコープは、識別子が宣言されているファイルを明示的に含むファイル、または_public_宣言を含むファイルを_public_が順に含むファイルを含むすべてのファイルに拡張されます。
  • キーワード export が宣言の前にある場合、スコープは、識別子が宣言されているファイルを直接含むファイルのみに拡張されます。

Euphoriaファイルを別のファイルに*インクルード*すると、スコープ修飾子を使用して宣言された識別子のみが_include_を実行しているファイルにアクセスできます。 インクルードファイル内の他の宣言は、_include_を実行するファイルには見えません。

ユーフォリア-定数

定数は、プログラムの存続期間中に決して変更できない初期値が割り当てられる変数でもあります。 Euphoriaでは、次のように定数キーワードを使用して定数を定義できます-

constant MAX = 100
constant Upper = MAX - 10, Lower = 5
constant name_list = {"Fred", "George", "Larry"}

式の結果は、以前に定義された関数の呼び出しを含む定数にも割り当てることができますが、割り当てが行われると、定数変数の値は「ロックイン」されます。

定数はサブルーチン内で宣言できません。 スコープ修飾子を持たない*定数*のスコープは、宣言で始まり、宣言されたファイルの終わりと終わりです。

#!/home/euphoria-4.0b2/bin/eui

constant MAX = 100
constant Upper = MAX - 10, Lower = 5

printf(1, "Value of MAX %d\n", MAX )
printf(1, "Value of Upper %d\n", Upper )
printf(1, "Value of Lower %d\n", Lower )

MAX = MAX + 1
printf(1, "Value of MAX %d\n", MAX )

これは、次のエラーを生成します-

./test.ex:10
<0110>:: may not change the value of a constant
MAX = MAX + 1
   ^

Press Enter

あなたが例から最後の2行を削除した場合、それは次の結果を生成します-

Value of MAX 100
Value of Upper 90
Value of Lower 5

enums

列挙値は特別なタイプの定数で、最初の値はデフォルトで数値1になり、その後の各項目は1ずつ増加します。 列挙型は数値のみを取ることができます。

列挙型はサブルーチン内で宣言できません。 スコープ修飾子を持たない enum のスコープは、宣言で始まり、宣言されたファイルの終わりと終わりです。

#!/home/euphoria-4.0b2/bin/eui

enum ONE, TWO, THREE, FOUR

printf(1, "Value of ONE %d\n", ONE )
printf(1, "Value of TWO %d\n", TWO )
printf(1, "Value of THREE %d\n", THREE )
printf(1, "Value of FOUR %d\n", FOUR )

これにより、次の結果が生成されます–

Value of ONE 1
Value of TWO 2
Value of THREE 3
Value of FOUR 4

1つのアイテムに数値を割り当てることで、そのアイテムの値を変更できます。 後続の値も、デフォルト値が割り当てられていない限り、常に前の値に1を加えたものです。

#!/home/euphoria-4.0b2/bin/eui

enum ONE, TWO, THREE, ABC=10, XYZ

printf(1, "Value of ONE %d\n", ONE )
printf(1, "Value of TWO %d\n", TWO )
printf(1, "Value of THREE %d\n", THREE )
printf(1, "Value of ABC %d\n", ABC )
printf(1, "Value of XYZ %d\n", XYZ )

これは、次の結果を生成します-

Value of ONE 1
Value of TWO 2
Value of THREE 3
Value of ABC 10
Value of XYZ 11

シーケンスは整数インデックスを使用しますが、enumを使用すると、次のようなコードを記述できます-

enum X, Y
sequence point = { 0,0 }

point[X] = 3
point[Y] = 4

Euphoria-データ型

メモリに保存されるデータには多くの種類があります。 たとえば、人の年齢は数値として保存され、住所は英数字として保存されます。

Euphoriaにはいくつかの標準タイプがあり、それらを使用して可能な操作とそれぞれの保存方法を定義します。

Euphoriaには、次の4つの標準データ型があります-

  • 整数
  • atom
  • シーケンス

原子とシーケンスの理解は、Euphoriaを理解するための鍵です。

整数

Euphoria整数データ型には数値が格納されます。 彼らは次のように宣言され、定義されています-

integer var1, var2

var1 = 1
var2 = 100

整数型で宣言される変数は、-1073741824から+1073741823までの*整数*値を持つアトムでなければなりません。 最大約15桁の大きな整数値で正確な計算を実行できますが、整数ではなくアトムとして宣言できます。

原子

Euphoriaのすべてのデータオブジェクトは、アトムまたはシーケンスです。 アトムは単一の数値です。 原子は、任意の整数または倍精度の浮動小数点値を持つことができます。 陶酔原子は次のように宣言および定義されます

atom var1, var2, var3

var1 = 1000
var2 = 198.6121324234
var3 = 'E'

原子は、精度が15桁の10進数で約-1e300〜+ 1e300の範囲です。 個々の文字は atom であり、単一引用符を使用して入力する必要があります。 たとえば、次のすべての文は合法です-

-- Following is equivalent to the atom 66 - the ASCII code for B
char = 'B'

-- Following is equivalent to the sequence {66}
sentence = "B"

シーケンス

シーケンスは、インデックスを介してアクセスできる数値のコレクションです。 Euphoriaのすべてのデータオブジェクトは、アトムまたはシーケンスです。

配列インデックスが0から始まる他のプログラミング言語とは異なり、シーケンスインデックスは1から始まります。 陶酔シーケンスは、次のように宣言および定義されています-

sequence var1, var2, var3, var4

var1 = {2, 3, 5, 7, 11, 13, 17, 19}
var2 = {1, 2, {3, 3, 3}, 4, {5, {6}}}
var3 = {{"zara", "ali"}, 52389, 97.25}
var4 = {} -- the 0 element sequence

文字列は、二重引用符を使用して入力できる文字の単なる*シーケンス*です。 たとえば、次のすべての文は合法です-

word = 'word'
sentence = "ABCDEFG"

文字列は、他のシーケンスと同様に操作および操作できます。 たとえば、上記の文字列はシーケンスと完全に同等です-

sentence = {65, 66, 67, 68, 69, 70, 71}

link:/euphoria/euphoria_sequences [Euphoria − Sequences]でシーケンスの詳細を学習します。

オブジェクト

これは、Euphoriaのスーパーデータタイプであり、アトム、シーケンス、整数などの任意の値を取ることができます。 Euphoriaオブジェクトは次のように宣言および定義されています-

object var1, var2, var3

var1 = {2, 3, 5, 7, 11, 13, 17, 19}
var2 = 100
var3 = 'E'

オブジェクトには、次の値のいずれかがあります-

  • シーケンス
  • 原子
  • 整数
  • ファイル番号として使用される整数
  • 文字列シーケンス、または単一文字のアトム

幸福感-オペレーター

Euphoriaは、変数を操作するための豊富な演算子セットを提供します。 私たちはすべてのEuphoria演算子を次のグループに分けることができます-

  • 算術演算子
  • 関係演算子
  • 論理演算子
  • 割り当て演算子
  • その他の演算子

算術演算子

算術演算子は、代数で使用されるのと同じ方法で数式で使用されます。 次の表に算術演算子を示します。 整数変数Aが10を保持し、変数Bが20を保持すると仮定します-

リンク:/euphoria/euphoria_arithmatic_operators [例を表示]

Operator Description Example
+ Addition - Adds values on either side of the operator A + B will give 30
- Subtraction - Subtracts right hand operand from left hand operand A - B will give -10
* Multiplication - Multiplies values on either side of the operator A* B will give 200
/ Division - Divides left hand operand by right hand operand B/A will give 2
+ Unary plus - This has no impact on the variable value. +B gives 20
- Unary minus - This creates a negative value of the given variable. -B gives -20

関係演算子

Euphoria言語でサポートされている関係演算子は次のとおりです。 変数Aが10を保持し、変数Bが20を保持すると仮定します-

リンク:/euphoria/euphoria_relational_operators [例を表示]

Operator Description Example
= Checks if the value of two operands are equal or not, if yes then condition becomes true. (A = B) is not true.
!= Checks if the value of two operands are equal or not, if values are not equal then condition becomes true. (A != B) is true.
> Checks if the value of left operand is greater than the value of right operand, if yes then condition becomes true. (A > B) is not true.
< Checks if the value of left operand is less than the value of right operand, if yes then condition becomes true. (A < B) is true.
>= Checks if the value of left operand is greater than or equal to the value of right operand, if yes then condition becomes true. (A >= B) is not true.
Checks if the value of left operand is less than or equal to the value of right operand, if yes then condition becomes true. (A ⇐ B) is true.

論理演算子

次の表に、論理演算子を示します。 ブール変数Aが1を保持し、変数Bが0を保持すると仮定します-

リンク:/euphoria/euphoria_logical_operators [例を表示]

Operator Description Example
and Called Logical AND operator. If both the operands are non zero then then condition becomes true. (A and B) is false.
or Called Logical OR Operator. If any of the two operands are non zero then then condition becomes true. (A or B) is true.
xor Called Logical XOR Operator. Condition is true if one of them is true, if both operands are true or false then condition becomes false. (A xor B) is true.
not Called Logical NOT Operator which negates the result. Using this operator, true becomes false and false becomes true not(B) is true.

これらの演算子を1または0以外の数値に適用することもできます。 規則は次のとおりです。ゼロはfalseを意味し、ゼロ以外は_true_を意味します。

割り当て演算子

Euphoria言語でサポートされている次の代入演算子があります-

リンク:/euphoria/euphoria_assignment_operators [例を表示]

Operator Description Example
= Simple assignment operator, Assigns values from right side operands to left side operand C = A + B will assigne value of A + B into C
+= Add AND assignment operator, It adds right operand to the left operand and assign the result to left operand C += A is equivalent to C = C + A
-= Subtract AND assignment operator, It subtracts right operand from the left operand and assign the result to left operand C -= A is equivalent to C = C - A
*= Multiply AND assignment operator, It multiplies right operand with the left operand and assign the result to left operand C *= A is equivalent to C = C * A
/= Divide AND assignment operator, It divides left operand with the right operand and assign the result to left operand C/= A is equivalent to C = C/A
&= Concatenation operator C &= {2} is same as C = {C} & {2}

-割り当てステートメントで使用される等号記号「=」は演算子ではなく、構文の一部にすぎません。

その他の演算子

Euphoria Languageでサポートされている他の演算子はほとんどありません。

オペレーター

2つのオブジェクトは、「&」演算子を使用して連結できます。 結果は、連結されたオブジェクトの長さの合計に等しい長さのシーケンスです。

たとえば-

#!/home/euphoria-4.0b2/bin/eui

sequence a, b, c
a = {1, 2, 3}
b = {4}
c = {1, 2, 3} & {4}

printf(1, "Value of c[1] %d\n", c[1] )
printf(1, "Value of c[2] %d\n", c[2] )
printf(1, "Value of c[3] %d\n", c[3] )
printf(1, "Value of c[4] %d\n", c[4] )

これは、次の結果を生成します-

Value of c[1] 1
Value of c[2] 2
Value of c[3] 3
Value of c[4] 4

幸福度演算子の優先順位

演算子の優先順位は、式内の用語のグループ化を決定します。 これは、式の評価方法に影響します。 特定の演算子は、他の演算子よりも優先順位が高くなっています。たとえば、乗算演算子は加算演算子よりも優先順位が高くなります。

たとえば、x = 7 + 3 * 2

ここでは、演算子*の優先順位が+より高いため、xには20ではなく13が割り当てられます。

したがって、最初に3 * 2で始まり、7に追加されます。

ここでは、優先順位が最も高い演算子がテーブルの上部に表示され、最も低い演算子が下部に表示されます。 式内では、優先順位の高い演算子が最初に評価されます。

Category Operator Associativity
Postfix function/type calls  
Unary + - ! not Right to left
Multiplicative */ Left to right
Additive + - Left to right
Concatenation & Left to right
Relational > >= < ⇐ Left to right
Equality = != Left to right
Logical AND and Left to right
Logical OR or Left to right
Logical XOR xor Left to right
Comma , Left to right

ユーフォリア-分岐

分岐は、プログラミング言語の最も重要な側面です。 プログラムの作成中に、決定を下したり、指定された多数のオプションから1つのオプションを選択したりする必要がある場合があります。

次の図は、プログラムが特定の条件に基づいて2つのパスのいずれかを取る必要がある単純なシナリオを示しています。

ブランチング

Euphoriaは、次の3種類の意思決定(分岐または条件付き)ステートメントを提供します-

  • リンク:/euphoria/euphoria_if_statement [ifステートメント]
  • リンク:/euphoria/euphoria_switch_statement [switchステートメント]
  • リンク:/euphoria/euphoria_ifdef_statement [ifdefステートメント]

詳細にステートメントを見てみましょう-

陶酔-ループの種類

ループは、プログラミング言語のもう1つの最も重要な側面です。 プログラムの作成中に、同じステートメントを何度も実行する必要があり、場合によっては無限に実行される場合があります。

プロセスの継続時間、およびプロセスを停止または変更する方法を指定するには、いくつかの方法があります。 反復ブロックは非公式にループと呼ばれることがあり、ループ内のコードの各実行はループの反復と呼ばれます。

次の図は、ループの単純な論理フローを示しています-

ループ

Euphoriaは、次の3種類のループ文を提供します-

  • リンク:/euphoria/euphoria_while_statement [whileステートメント]
  • リンク:/euphoria/euphoria_until_statement [ステートメントまでループ]
  • リンク:/euphoria/euphoria_for_statement [forステートメント]

上記のすべてのステートメントは、さまざまな状況に基づいた柔軟性と使いやすさを提供します。 それらを一つずつ詳細に見てみましょう-

陶酔-フロー制御

プログラム実行フローとは、プログラム文が実行される順序を指します。 デフォルトでは、ステートメントは次々に実行されます。

しかしながら;多くの場合、タスクを完了させるために、実行の順序をデフォルトの順序から変更する必要があります。

Euphoriaには、ステートメントの実行順序を調整するために使用できる多数の_flow_制御ステートメントがあります。

_exit_ステートメント

ループを終了するには、キーワード exit を使用します。 これにより、フローはすぐに現在のループを離れ、ループの終了後に最初のステートメントから再開します。

構文

exit文の構文は次のとおりです-

exit [ "Label Name" ] [Number]
*exit* ステートメントは、オプションのラベル名または番号が指定されるまで、最新の最も内側のループを終了します。
*exit N* の特別な形式は *exit 0* です。 これにより、深さに関係なく、すべてのレベルのループが残ります。 制御は、最も外側のループブロックの後に続きます。 同様に、exit -1は2番目に外側のループを終了します。

#!/home/euphoria-4.0b2/bin/eui

integer b

for a = 1 to 16 do
   printf(1, "value of a %d\n", a)

   if a = 10 then
      b = a
      exit
   end if
end for

printf(1, "value of b %d\n", b)

これは、次の結果を生成します-

value of a 1
value of a 2
value of a 3
value of a 4
value of a 5
value of a 6
value of a 7
value of a 8
value of a 9
value of a 10
value of b 10

_break_ステートメント

*break* ステートメントは *exit* ステートメントとまったく同じように機能しますが、あらゆる種類のループステートメントではなく、ifステートメントまたはswitchステートメントに適用されます。

構文

breakステートメントの構文は次のとおりです-

break [ "Label Name" ] [Number]
*break* ステートメントは、オプションのラベル名または番号が指定されるまで、最新かつ最も内側のifまたはswitchブロックを終了します。
*break N* の特別な形式は *break 0* です。 これにより、深さに関係なく、ifブロックまたはswitchブロックが最も外側になります。 制御は、最も外側のブロックの後に続きます。 同様に、break -1は、2番目に外側のifブロックまたはswitchブロックなどを中断します。

#!/home/euphoria-4.0b2/bin/eui

integer a, b
sequence s = {'E','u', 'p'}

if s[1] = 'E' then
   a = 3

   if s[2] = 'u' then
      b = 1
      if s[3] = 'p' then
         break 0 -- leave topmost if block
      end if
      a = 2
   else
      b = 4
   end if
else
   a = 0
   b = 0
end if

printf(1, "value of a %d\n", a)
printf(1, "value of b %d\n", b)

これは、次の結果を生成します-

value of a 3
value of b 1

_continue_ステートメント

*continue* ステートメントは、次の反復に進み、反復の残りをスキップすることにより、適用されるループの実行を継続します。

次の反復に進むということは、条件変数インデックスをテストし、それがまだ境界内にあるかどうかをチェックすることを意味します。

構文

continueステートメントの構文は次のとおりです-

continue [ "Label Name" ] [Number]
*continue* ステートメントは、オプションのラベル名または番号が指定されるまで、最新かつ最も内側のループを繰り返します。
*continue N* の特別な形式は、 *continue 0* です。 これにより、深さに関係なく、最も外側のループが繰り返されます。 同様に、continue -1は2番目に外側のループから始まり、以下同様に続きます。

#!/home/euphoria-4.0b2/bin/eui

for a = 3 to 6 do
   printf(1, "value of a %d\n", a)

   if a = 4 then
      puts(1,"(2)\n")
      continue
   end if

   printf(1, "value of a %d\n", a*a)
end for
This would produce following result:

value of a 3
value of a 9
value of a 4
(2)
value of a 5
value of a 25
value of a 6
value of a 36

_retry_ステートメント

*retry* ステートメントは、次の反復に進み、残りの反復をスキップすることにより、適用されるループの実行を継続します。

構文

再試行ステートメントの構文は次のとおりです-

retry [ "Label Name" ] [Number]
*retry* ステートメントは、適用されるループの現在の反復の実行を再試行します。 ステートメントは、指定されたループの最初のステートメントに分岐し、何もテストせず、forループインデックスをインクリメントしません。
*retry N* の特別な形式は *retry 0* です。 これは、深さに関係なく、最も外側のループの実行を再試行します。 同様に、retry -1は2番目に外側のループを再試行します。

通常、再試行ステートメントを含むサブブロックには、exit、continue、breakなどの別のフロー制御キーワードも含まれます。 それ以外の場合、反復は無限に実行されます。

#!/home/euphoria-4.0b2/bin/eui

integer errors = 0
integer files_to_open = 10

for i = 1 to length(files_to_open) do
   fh = open(files_to_open[i], "rb")

   if fh = -1 then
      if errors > 5 then
         exit
      else
         errors += 1
         retry
      end if
   end if
   file_handles[i] = fh
end for

再試行はiの値を変更せずに同じファイルを再度開くため、exitステートメントが提供するループから抜け出す方法が必要です。

gotoステートメント

*goto* ステートメントは、ラベル付けされた場所でコードの実行を再開するようにコンピューターに指示します。

実行を再開する場所は、ステートメントのターゲットと呼ばれます。 現在のルーチン、またはルーチン外の場合は現在のファイルに存在するように制限されています。

構文

gotoステートメントの構文は次のとおりです-

goto "Label Name"

gotoステートメントのターゲットは、アクセス可能な任意の label ステートメントにすることができます-

label "Label Name"

ラベル名は、二重引用符で囲まれた定数文字列でなければなりません。 Euphoria識別子で違法な文字は、通常の文字列であるため、ラベル名に表示される場合があります。

#!/home/euphoria-4.0b2/bin/eui

integer a = 0

label "FIRST"
printf(1, "value of a %d\n", a)
a += 10

if a < 50 then
   goto "FIRST"
end if
printf(1, "Final value of a %d\n", a)

これは、次の結果を生成します-

value of a 0
value of a 10
value of a 20
value of a 30
value of a 40
Final value of a 50

陶酔-短絡評価

条件が and または or 演算子を使用して if、elsif、until 、または while によってテストされる場合、短絡評価が使用されます。 たとえば-

if a < 0 and b > 0 then
   -- block of code
end if

a <0が偽の場合、Euphoriaはbが0より大きいかどうかをテストしません。 それにもかかわらず、全体的な結果が偽であることを知っています。 同様に-

if a < 0 or b > 0 then
   -- block of code
end if

a <0が真の場合、Euphoriaは、bの値をテストせずに、結果が真であるとすぐに判断します。このテストの結果は無関係であるためです。

一般的には、次の形式の条件があるときはいつでも-

A and B

AとBが任意の2つの式である場合、Euphoriaは、Aが偽の場合にショートカットを使用し、式Bを見なくてもすぐに全体の結果を偽にします。

同様に、次の形式の条件があるときはいつでも-

A or  B

Aがtrueの場合、Euphoriaは式Bの評価をスキップし、結果がtrueであると宣言します。

if、elsif、until、およびwhile条件についてのみandおよびorの Short-circuit 評価が行われます。 他のコンテキストでは使用されません。 たとえば-

x = 1 or {1,2,3,4,5} -- x should be set to {1,1,1,1,1}

ここで短絡が使用された場合、xを1に設定し、\ {1,2,3,4,5}を見るだけでなく、間違っています。

したがって、if、elsif、until、またはwhile条件で短絡を使用できます。これは、結果がtrueまたはfalseであり、結果としてアトムを生成するために条件が必要かどうかだけを考慮する必要があるためです。

幸福感-シーケンス

シーケンスは、コンマで区切られた中括弧で囲まれたオブジェクトのリスト\ {}で表されます。 シーケンスには、アトムと他のシーケンスの両方を含めることができます。 たとえば-

{2, 3, 5, 7, 11, 13, 17, 19}
{1, 2, {3, 3, 3}, 4, {5, {6}}}
{{"Zara", "Ayan"}, 52389, 97.25}
{} -- the 0-element sequence

角括弧で要素番号を指定することにより、シーケンスの単一の要素を選択できます。 要素番号は1から始まります。

たとえば、xに\ {5、7.2、9、0.5、13}が含まれている場合、x [2]は7.2です。

x [2]に\ {11,22,33}が含まれているとします。x[2]を要求すると\ {11,22,33}を取得し、x [2] [3]を要求すると、アトム33。

#!/home/euphoria-4.0b2/bin/eui

sequence x
x = {1, 2, 3, 4}

for a = 1 to length(x) do
   printf(1, "value of x[%d] = %d\n", {a, x[a]})
end for

ここで、length()は、シーケンスの長さを返す組み込み関数です。 上記の例では、次の結果が生成されます-

value of x[1] = 1
value of x[2] = 2
value of x[3] = 3
value of x[4] = 4

文字列

文字列は、単なる*シーケンス*の文字です。 それは2つの方法のいずれかで入力することができます-

(a)二重引用符の使用-

"ABCDEFG"
  • (b)生の文字列表記法を使用する- *
-- Using back-quotes
`ABCDEFG`

or

-- Using three double-quotes
"""ABCDEFG"""

あなたは概念を理解するために次の例を試すことができます-

#!/home/euphoria-4.0b2/bin/eui

sequence x
x = "ABCD"

for a = 1 to length(x) do
   printf(1, "value of x[%d] = %s\n", {a, x[a]})
end for

これは、次の結果を生成します-

value of x[1] = A
value of x[2] = B
value of x[3] = C
value of x[4] = D

文字列配列

文字列の配列は、次のようにシーケンスを使用して実装することができます-

#!/home/euphoria-4.0b2/bin/eui

sequence x = {"Hello", "World", "Euphoria", "", "Last One"}

for a = 1 to length(x) do
   printf(1, "value of x[%d] = %s\n", {a, x[a]})
end for

これは、次の結果を生成します-

value of x[1] = Hello
value of x[2] = World
value of x[3] = Euphoria
value of x[4] =
value of x[5] = Last One

陶酔構造

構造は、次のようにシーケンスを使用して実装できます-

#!/home/euphoria-4.0b2/bin/eui

sequence employee = {
   {"John","Smith"},
      45000,
      27,
      185.5
}
printf(1, "First Name = %s, Last Name = %s\n", {employee[1][1],employee[1][2]} )

これは、次の結果を生成します-

First Name = John, Last Name = Smith

シーケンスに対して直接実行できるさまざまな操作があります。 それらを詳細に見てみましょう-

尿路手術

シーケンスに適用されると、単項演算子が実際にシーケンスの各要素に適用され、同じ長さの結果のシーケンスが生成されます。

#!/home/euphoria-4.0b2/bin/eui

sequence x
x = -{1, 2, 3, 4}

for a = 1 to length(x) do
   printf(1, "value of x[%d] = %d\n", {a, x[a]})
end for

これは、次の結果を生成します-

value of x[1] = -1
value of x[2] = -2
value of x[3] = -3
value of x[4] = -4

算術演算

ほとんどすべての算術演算は、次のようにシーケンスで実行できます-

#!/home/euphoria-4.0b2/bin/eui

sequence x, y, a, b, c
x = {1, 2, 3}
y = {10, 20, 30}

a = x + y
puts(1, "Value of a = {")

for i = 1 to length(a) do
   printf(1, "%d,", a[i])
end for
puts(1, "}\n")

b = x - y
puts(1, "Value of b = {")
for i = 1 to length(a) do
   printf(1, "%d,", b[i])
end for
puts(1, "}\n")

c = x* 3
puts(1, "Value of c = {")

for i = 1 to length(c) do
   printf(1, "%d,", c[i])
end for
puts(1, "}\n")

これは、次の結果を生成します-

Value of a = {11,22,33,}
Value of b = {-9,-18,-27,}
Value of c = {3,6,9,}

コマンドラインオプション

ユーザーはコマンドラインオプションをEuphoriaスクリプトに渡すことができ、次のように* command_line()*関数を使用してシーケンスとしてアクセスできます-

#!/home/euphoria-4.0b2/bin/eui

sequence x

x = command_line()

printf(1, "Interpeter Name: %s\n", {x[1]} )
printf(1, "Script Name: %s\n", {x[2]} )
printf(1, "First Argument: %s\n", {x[3]})
printf(1, "Second Argument: %s\n", {x[4]})

ここで* printf()*はEuphoriaの組み込み関数です。 このスクリプトを次のように実行すると-

$eui test.ex "one" "two"

これは、次の結果を生成します-

Interpeter Name:/home/euphoria-4.0b2/bin/eui
Script Name: test.ex
First Argument: one
Second Argument: two

ユーフォリア-日付と時刻

Euphoriaには、プログラムに日付と時刻を返すライブラリルーチンがあります。

_date()_メソッド

date()メソッドは、8つのアトム要素で構成されるシーケンス値を返します。 次の例では、詳細に説明します-

#!/home/euphoria-4.0b2/bin/eui

integer curr_year, curr_day, curr_day_of_year, curr_hour, curr_minute, curr_second
sequence system_date, word_week, word_month, notation,
   curr_day_of_week, curr_month
   word_week = {"Sunday",
      "Monday",
      "Tuesday",
      "Wednesday",
      "Thursday",
      "Friday",
      "Saturday"}
   word_month = {"January", "February",
      "March",
      "April",
      "May",
      "June",
      "July",
      "August",
      "September",
      "October",
      "November",
      "December"}
-- Get current system date.
system_date = date()

-- Now take individual elements
curr_year = system_date[1] + 1900
curr_month = word_month[system_date[2]]
curr_day = system_date[3]
curr_hour = system_date[4]
curr_minute = system_date[5]
curr_second = system_date[6]
curr_day_of_week = word_week[system_date[7]]
curr_day_of_year = system_date[8]

if curr_hour >= 12 then
   notation = "p.m."
else
   notation = "a.m."
end if

if curr_hour > 12 then
   curr_hour = curr_hour - 12
end if

if curr_hour = 0 then
   curr_hour = 12
end if

puts(1, "\nHello Euphoria!\n\n")
printf(1, "Today is %s, %s %d, %d.\n", {curr_day_of_week,
   curr_month, curr_day, curr_year})

printf(1, "The time is %.2d:%.2d:%.2d %s\n", {curr_hour,
   curr_minute, curr_second, notation})

printf(1, "It is %3d days into the current year.\n", {curr_day_of_year})

これにより、標準画面に次の結果が生成されます-

Hello Euphoria!

Today is Friday, January 22, 2010.
The time is 02:54:58 p.m.
It is  22 days into the current year.

_time()_メソッド

time()メソッドは、一定の時点から経過した秒数を表すアトム値を返します。 次の例では、詳細に説明します-

#!/home/euphoria-4.0b2/bin/eui

constant ITERATIONS = 100000000
integer p
atom t0, t1, loop_overhead

t0 = time()
for i = 1 to ITERATIONS do
   -- time an empty loop
end for

loop_overhead = time() - t0

printf(1, "Loop overhead:%d\n", loop_overhead)

t0 = time()
for i = 1 to ITERATIONS do
    p = power(2, 20)
end for

t1 = (time() - (t0 + loop_overhead))/ITERATIONS

printf(1, "Time (in seconds) for one call to power:%d\n", t1)

これは、次の結果を生成します-

Loop overhead:1
Time (in seconds) for one call to power:0

日時関連のメソッド

Euphoriaは、日付と時刻の操作に役立つメソッドのリストを提供します。 これらのメソッドは、link:/euphoria/euphoria_library_routines [Euphoria Library Routines]にリストされています。

陶酔-手順

プロシージャは、プログラム内のどこからでも呼び出すことができる再利用可能なコードのグループです。 これにより、同じコードを何度も記述する必要がなくなります。 これは、プログラマーがモジュラーコードを書くのに役立ちます。

他の高度なプログラミング言語と同様に、Euphoriaは、プロシージャを使用してモジュラーコードを記述するために必要なすべての機能もサポートしています。

前の章で_printf()_や_length()_などの手順を見たことがあるはずです。 これらの手順を何度も使用していますが、コアユーフォリアで記述されたのは一度だけです。

Euphoriaでは、独自のプロシージャを作成することもできます。 このセクションでは、Euphoriaで独自のプロシージャを作成する方法について説明します。

プロシージャ定義

プロシージャを使用する前に、定義する必要があります。 Euphoriaでプロシージャを定義する最も一般的な方法は、 procedure キーワードを使用し、その後に一意のプロシージャ名、パラメータのリスト(空の場合がある)、および end procedure 文で終わる文ブロックを使用することです。 基本的な構文は以下のとおりです-

procedure procedurename(parameter-list)

   statements
   ..........

end procedure

パラメータを取らないsayHelloと呼ばれる簡単な手順は、ここで定義されています-

procedure  sayHello()
   puts(1, "Hello there")
end procedure

プロシージャの呼び出し

スクリプトの後半のどこかでプロシージャを呼び出すには、単に次のようにそのプロシージャの名前を書く必要があります-

#!/home/euphoria-4.0b2/bin/eui

procedure  sayHello()
   puts(1, "Hello there")
end procedure

-- Call above defined procedure.
sayHello()

これは、次の結果を生成します-

Hello there

プロシージャパラメータ

これまで、パラメータなしの手順を見てきました。 ただし、プロシージャの呼び出し中に異なるパラメーターを渡す機能があります。 これらの渡されたパラメーターは、プロシージャー内でキャプチャーでき、これらのパラメーターに対して任意の操作を実行できます。

プロシージャは、コンマで区切られた複数のパラメーターを使用できます。

_sayHello_プロシージャを少し変更してみましょう。 今回は2つのパラメータがかかります-

#!/home/euphoria-4.0b2/bin/eui

procedure sayHello(sequence name,atom  age)
   printf(1, "%s is %d years old.", {name, age})
end procedure

-- Call above defined procedure.
sayHello("zara", 8)

これは、次の結果を生成します-

zara is 8 years old.

幸福感-機能

Euphoria関数はプロシージャに似ていますが、値を返し、式で使用できます。 この章では、Euphoriaで独自の関数を作成する方法について説明します。

関数定義

関数を使用する前に、定義する必要があります。 Euphoriaで関数を定義する最も一般的な方法は、 function キーワードを使用し、その後に一意の関数名、パラメーターのリスト(空の場合がある)、および end function ステートメントで終わるステートメントブロックを使用することです。 基本的な構文はここに示されています-

function functionname(parameter-list)

   statements
   ..........
   return [Euphoria Object]

end function

パラメータを取らないsayHelloと呼ばれる単純な関数はここで定義されています-

function sayHello()
   puts(1, "Hello there")
   return 1
end function

関数を呼び出す

スクリプトのどこかで関数を呼び出すには、次のようにその関数の名前を書く必要があります-

#!/home/euphoria-4.0b2/bin/eui

function sayHello()
   puts(1, "Hello there")
   return 1
end function

-- Call above defined function.
sayHello()

これは、次の結果を生成します-

Hello there

関数パラメーター

これまで、パラメータなしの関数を見てきました。 ただし、関数の呼び出し中に異なるパラメーターを渡す機能があります。 これらの渡されたパラメーターは関数内でキャプチャーでき、これらのパラメーターに対して任意の操作を実行できます。

関数は、コンマで区切られた複数のパラメーターを取ることができます。

_sayHello_関数を少し変更してみましょう。 今回は2つのパラメータがかかります-

#!/home/euphoria-4.0b2/bin/eui

function sayHello(sequence name,atom  age)
   printf(1, "%s is %d years old.", {name, age})
   return 1
end function

-- Call above defined function.
sayHello("zara", 8)

これは、次の結果を生成します-

zara is 8 years old.

_return_ステートメント

Euphoria関数には、ステートメント end function を閉じる前に_return_ステートメントが必要です。 任意のEuphoriaオブジェクトを返すことができます。 実際には、オブジェクトのシーケンスを返すことにより、複数の戻り値を持つことができます。 例えば

return {x_pos, y_pos}

返すものがない場合は、単に1または0を返します。 戻り値1は成功を示し、0は失敗を示します

Euphoria-ファイルI/O

Euphoriaプログラミング言語を使用すると、フロッピードライブまたはハードドライブのファイルデータを読み取って変更するプログラムを作成したり、出力形式として新しいファイルを作成したりできます。 プリンターやモデムなど、コンピューター上のデバイスにアクセスすることもできます。

この章では、Euphoriaで利用可能なすべての基本的なI/O機能について説明しました。 その他の機能については、標準のEuphoriaのドキュメントを参照してください。

画面に表示する

出力を生成する最も簡単な方法は、_puts()_ステートメントを使用することです。このステートメントでは、画面に表示する文字列を渡すことができます。 動的な値を使用して文字列をフォーマットする必要がある場合にも使用できる別のメソッド_printf()_があります。

これらのメソッドは、文字列に渡す式を変換し、次のように結果を標準出力に書き込みます-

#!/home/euphoria-4.0b2/bin/eui

puts(1, "Euphoria is really a great language, isn't it?" )

これにより、標準画面に次の結果が生成されます-

Euphoria is really a great language, isn't it?

ファイルの開閉

Euphoriaは、デフォルトでファイルを操作するために必要な基本的な方法を提供します。 次の方法を使用して、ファイル操作のほとんどを行うことができます-

  • 開いた()
  • 閉じる()
  • printf()
  • 取得()
  • getc()

_open_メソッド

ファイルを読み書きする前に、Euphoriaの組み込みopen() methodを使用してファイルを開く必要があります。 この関数は、関連する他のサポートメソッドを呼び出すために使用されるファイル記述子を作成します。

構文

integer file_num = open(file_name, access_mode)

上記のメソッドは、指定されたファイル名を開く際にエラーが発生した場合に-1を返します。 ここにパラメータがあります-

  • file_name -file_name引数は、アクセスするファイルの名前を含む文字列値です。
  • access_mode -access_modeは、ファイルを開く必要があるモードを決定します。 たとえば、読み取り、書き込み、追加など。 ファイルを開くモードの可能な値の完全なリストは、次の表に記載されています-
S.No Modes & Description
1

r

読み取り専用のテキストファイルを開きます。 ファイルポインタはファイルの先頭に置かれます。

2

rb

バイナリ形式でのみ読み取り用にファイルを開きます。 ファイルポインタはファイルの先頭に置かれます。

3

w

書き込み専用のテキストファイルを開きます。 ファイルが存在する場合はファイルを上書きします。 ファイルが存在しない場合は、書き込み用の新しいファイルを作成します。

4

wb

バイナリ形式でのみ書き込み用にファイルを開きます。 ファイルが存在する場合はファイルを上書きします。 ファイルが存在しない場合は、書き込み用の新しいファイルを作成します。

5

u

読み取りと書き込みの両方のためにファイルを開きます。 ファイルポインターは、ファイルの先頭に設定されます。

6

ub

読み取りと書き込みの両方のファイルをバイナリ形式で開きます。 ファイルポインタはファイルの先頭に置かれます。

7

a

追加するファイルを開きます。 ファイルが存在する場合、ファイルポインターはファイルの末尾にあります(追加モード)。 ファイルが存在しない場合は、書き込み用の新しいファイルを作成します。

8

ab

バイナリ形式で追加するためのファイルを開きます。 ファイルが存在する場合、ファイルポインターはファイルの末尾にあります(追加モード)。 ファイルが存在しない場合は、書き込み用の新しいファイルを作成します。

次の例では、Linuxシステムの現在のディレクトリに新しいテキストファイルを作成します-

#!/home/euphoria-4.0b2/bin/eui

integer file_num
constant ERROR = 2
constant STDOUT = 1

file_num = open("myfile,txt", "w")

if file_num = -1 then
   puts(ERROR, "couldn't open myfile\n")
else
   puts(STDOUT, "File opend successfully\n")
end if

ファイルが正常に開いた場合、「myfile.txt」が現在のディレクトリに作成され、次の結果が生成されます-

File opend successfully

_close()_メソッド

close()メソッドは、書き込まれていない情報をすべてフラッシュし、ファイルを閉じます。その後、ファイルに対する読み取りまたは書き込みができなくなります。

Euphoriaは、ファイルの参照オブジェクトが別のファイルに再割り当てされると、自動的にファイルを閉じます。 close()メソッドを使用してファイルを閉じることをお勧めします。

構文

close( file_num );

ここでは、ファイルを開いているときに受け取ったファイル記述子がパラメーターとして渡されます。

次の例では、上記のようにファイルを作成し、プログラムが存在する前にそれを閉じます-

#!/home/euphoria-4.0b2/bin/eui

integer file_num
constant ERROR = 2
constant STDOUT = 1

file_num = open("myfile.txt", "w")

if file_num = -1 then
   puts(ERROR, "couldn't open myfile\n")
else
   puts(STDOUT, "File opend successfully\n")
end if

if file_num = -1 then
   puts(ERROR, "No need to close the file\n")
else
   close( file_num )
   puts(STDOUT, "File closed successfully\n")
end if

これは、次の結果を生成します-

File opend successfully
File closed successfully

ファイルの読み書き

Euphoriaには、テキストモードまたはバイナリモードでファイルを読み書きする際の作業を楽にするアクセス方法のセットが用意されています。 _printf()_および_gets()_メソッドを使用してファイルを読み書きする方法を見てみましょう。

_printf()_メソッド

_printf()_メソッドは、開いているファイルに任意の文字列を書き込みます。

構文

printf(fn, st, x)

ここにパラメータがあります-

  • fn -open()メソッドから受け取ったファイル記述子。
  • st -10進数またはアトムが%dを使用してフォーマットされ、ストリングまたはシーケンスが%sを使用してフォーマットされるフォーマット文字列。
  • x -xがシーケンスの場合、stの書式指定子はxの対応する要素と一致します。 xがアトムの場合、通常stには1つのフォーマット指定子のみが含まれ、xに適用されます。 しかしながら; stに複数の書式指定子が含まれている場合、それぞれが同じ値xに適用されます。

次の例では、ファイルを開き、このファイルに人の名前と年齢を書き込みます-

#!/home/euphoria-4.0b2/bin/eui

integer file_num
constant ERROR = 2
constant STDOUT = 1

file_num = open("myfile.txt", "w")

if file_num = -1 then
   puts(ERROR, "couldn't open myfile\n")
else
   puts(STDOUT, "File opend successfully\n")
end if

printf(file_num, "My name is %s and age is %d\n", {"Zara", 8})

if file_num = -1 then
   puts(ERROR, "No need to close the file\n")
else
   close( file_num )
   puts(STDOUT, "File closed successfully\n")
end if

上記の例は、_myfile.txt_ファイルを作成します。 指定されたコンテンツをそのファイルに書き込み、最終的に閉じます。 このファイルを開くと、次の内容になります-

My name is Zara and age is 8

_gets()_メソッド

_gets()_メソッドは、開いているファイルから文字列を読み取ります。

構文

gets(file_num)

ここで渡されるパラメーターは、_opend()_メソッドによって返されるファイルの説明です。 このメソッドは、ファイルの先頭から1行ずつ読み取りを開始します。 文字の値は0〜255です。 アトム-1はファイルの終わりに返されます。

作成済みのファイル_myfile.txt_を取得してみましょう。

#!/home/euphoria-4.0b2/bin/eui

integer file_num
object line

constant ERROR = 2
constant STDOUT = 1

file_num = open("myfile.txt", "r")
if file_num = -1 then
   puts(ERROR, "couldn't open myfile\n")
else
   puts(STDOUT, "File opend successfully\n")
end if

line = gets(file_num)
printf( STDOUT, "Read content : %s\n", {line})

if file_num = -1 then
   puts(ERROR, "No need to close the file\n")
else
   close( file_num )
   puts(STDOUT, "File closed successfully\n")
end if

これは、次の結果を生成します-

File opend successfully
Read content : My name is Zara and age is 8

File closed successfully

Euphoriaは、ファイルの操作に役立つ多くのメソッドのリストを提供します。 これらのメソッドは、link:/euphoria/euphoria_library_routines [Euphoria Library Routines]にリストされています。

ユーフォリア-ライブラリルーチン

多数のライブラリルーチンが提供されています。 いくつかは、インタプリタ ex.exe、exw.exeまたはexu に直接組み込まれています。 その他はEuphoriaで記述されており、それらを使用するには euphoria \ include ディレクトリに.eファイルの1つを含める必要があります。

どのようなオブジェクトが渡され、返される可能性があるかを示すために、次のプレフィックスが使用されます-

S.No Prefix & Description
1

x

一般的なオブジェクト(原子またはシーケンス)

2

s

シーケンス

3

a

原子

4

i

整数

5

fn

ファイル番号として使用される整数

6

st

文字列シーケンス、または単一文字のアトム

定義済みのタイプ

これらの型で変数を宣言するだけでなく、値が特定の型であるかどうかをテストするために、通常の関数のように変数を呼び出すこともできます。

integer test if an object is an integer
atom test if an object is an atom
sequence test if an object is a sequence
object test if an object is an object (always true)

シーケンス操作

length return the length of a sequence
repeat repeat an object n times to form a sequence of length n
reverse reverse a sequence
append add a new element to the end of a sequence
prepend add a new element to the beginning of a sequence

検索と並べ替え

compare compare two objects
equal test if two objects are identical
find find an object in a sequence - start searching from element number 1
find_from find an object in a sequence - start searching from any element number
match find a sequence as a slice of another sequence - start searching from element number 1
match_from find a sequence as a slice of another sequence - start searching from any element number
sort sort the elements of a sequence into ascending order
custom_sort sort the elements of a sequence based on a compare function that you supply

パターンマッチング

lower convert an atom or sequence to lower case
upper convert an atom or sequence to upper case
wildcard_match match a pattern containing ? and * wildcards
wildcard_file match a file name against a wildcard specification

Math

これらのルーチンは、個々のアトムまたは値のシーケンスに適用できます。

sqrt calculate the square root of an object
rand generate random numbers
sin calculate the sine of an angle
arcsin calculate the angle with a given sine
cos calculate the cosine of an angle
arccos calculate the angle with a given cosine
tan calculate the tangent of an angle
arctan calculate the arc tangent of a number
log calculate the natural logarithm
floor round down to the nearest integer
remainder calculate the remainder when a number is divided by another
power calculate a number raised to a power
PI the mathematical value PI (3.14159…​)

ビットごとの論理演算

これらのルーチンは、数値をバイナリビットのコレクションとして扱い、数値のバイナリ表現の対応するビットに対して論理演算が実行されます。

ビットを左または右にシフトするルーチンはありませんが、2の累乗で乗算または除算することで同じ効果を実現できます。

and_bits perform logical AND on corresponding bits
or_bits perform logical OR on corresponding bits
xor_bits perform logical XOR on corresponding bits
not_bits perform logical NOT on all bits

ファイルおよびデバイスI/O

ファイルまたはデバイスで入力または出力を行うには、最初にファイルまたはデバイスを開き、次に以下のルーチンを使用して読み取りまたは書き込みを行ってから、ファイルまたはデバイスを閉じる必要があります。 http://www.rapideuphoria.com/lib_h_o#open [open()]は、他のI/Oルーチンの最初の引数として使用するファイル番号を提供します。 特定のファイル/デバイスが自動的に(テキストファイルとして)開かれます-

  • 0-標準入力
  • 1-標準出力
  • 2-標準エラー
open open a file or device
close close a file or device
flush flush out buffered data to a file or device
lock_file lock a file or device
unlock_file unlock a file or device
print ##print a Euphoria object on one line, with braces and commas \{,,} to show the structure
pretty_print ##print a Euphoria object in a nice readable form, using multiple lines and appropriate indentation
? x ##shorthand for print(1, x)
sprint return a printed Euphoria object as a string sequence
printf formatted print to a file or device
sprintf formatted print returned as a string sequence
puts output a string sequence to a file or device
getc read the next character from a file or device
gets ##read the next line from a file or device
get_bytes ##read the next n bytes from a file or device
prompt_string prompt the user to enter a string
get_key ##check for key pressed by the user, don’t wait
wait_key wait for user to press a key
get read the representation of any Euphoria object from a file
prompt_number prompt the user to enter a number
value read the representation of any Euphoria object from a string
seek move to any byte position within an open file
where report the current byte position in an open file
current_dir return the name of the current directory
chdir change to a new current directory
dir return complete info on all files in a directory
walk_dir recursively walk through all files in a directory
allow_break allow control-c/control-Break to terminate your program or not
check_break check if user has pressed control-c or control-Break

マウスのサポート(DOS32およびLinux)

Windows XPでは、DOSマウスを(非フルスクリーン)ウィンドウで動作させる場合は、DOSウィンドウのプロパティでクイック編集モードを無効にする必要があります。

get_mouse return mouse "events" (clicks, movements)
mouse_events select mouse events to watch for
mouse_pointer display or hide the mouse pointer

オペレーティング・システム

time number of seconds since a fixed point in the past
tick_rate set the number of clock ticks per second (DOS32)
date current year, month, day, hour, minute, second etc.
command_line command-line used to run this program
getenv get value of an environment variable
system execute an operating system command line
system_exec execute a program and get its exit code
abort terminate execution
sleep suspend execution for a period of time
platform find out which operating system are we running on

特別なマシン依存ルーチン

machine_func specialized internal operations with a return value
machine_proc specialized internal operations with no return value

デバッグ

trace dynamically turns tracing on or off
profile dynamically turns profiling on or off

グラフィックスとサウンド

次のルーチンを使用すると、画面に情報を表示できます。 DOSでは、PC画面を多くのグラフィックモードのいずれかに配置できます。

次のルーチンは、すべてのテキストモードとピクセルグラフィックモードで動作します。

clear_screen clear the screen
position set cursor line and column
get_position return cursor line and column
graphics_mode select a new pixel-graphics or text mode (DOS32)
video_config return parameters of current mode
scroll scroll text up or down
wrap control line wrap at right edge of screen
text_color set foreground text color
bk_color set background color
palette change color for one color number (DOS32)
all_palette change color for all color numbers (DOS32)
get_all_palette get the palette values for all colors (DOS32)
read_bitmap read a bitmap (.bmp) file and return a palette and a 2-d sequence of pixels
save_bitmap create a bitmap (.bmp) file, given a palette and a 2-d sequence of pixels
get_active_page return the page currently being written to (DOS32)
set_active_page change the page currently being written to (DOS32)
get_display_page return the page currently being displayed (DOS32)
set_display_page change the page currently being displayed (DOS32)
sound make a sound on the PC speaker (DOS32)

次のルーチンは、モードでのみテキストで動作します

cursor select cursor shape
text_rows set number of lines on text screen
get_screen_char get one character from the screen
put_screen_char put one or more characters on the screen
save_text_image save a rectangular region from a text screen
display_text_image display an image on the text screen

次のルーチンは、ピクセルグラフィックモードでのみ動作します(DOS32)

pixel set color of a pixel or set of pixels
get_pixel read color of a pixel or set of pixels
draw_line connect a series of graphics points with a line
polygon draw an n-sided figure
ellipse draw an ellipse or circle
save_screen save the screen to a bitmap (.bmp) file
save_image save a rectangular region from a pixel-graphics screen
display_image display an image on the pixel-graphics screen

マルチタスク

task_clock_start restart the scheduler’s clock
task_clock_stop stop the scheduler’s clock
task_create create a new task
task_list get a list of all tasks
task_schedule schedule a task for execution
task_self return the task id of the current task
task_status the current status (active, suspended, terminated) of a task
task_suspend Suspend a task.
task_yield Yield control, so the scheduler can pick a new task to run.