C++トランスレーター(試作・テスト版)

 投稿者:しばっち  投稿日:2014年 7月16日(水)20時46分47秒
  このプログラムは、BASICファイルをC++ファイルに変換(補助?)します。
但し、このプログラムは「未完成」であり、試作テスト版です。

サポートが少なく、このプログラムのバグ(変換エラー)や未完成なため対応していないなど、コンパイル通すにはまだまだ「手直し」が必要です。
予めご了承ください。(私自身の勘違いや思い込み、知識不足などもあります)
そのため、テキストウィンドゥに書き出しています。(あ~ぁテストする度にエラー(変換ミス)が出てくる!!)

まだアクセレーション目的に使用できる段階ではありません。その際はBASIC Accを使用してください。

変数、関数等は基本的にdouble型に変換しています。
内部関数、内部サブルーチンには対応していません。外部関数、外部サブルーチンに定義しなおしてください。
グラフィック関係は全て未対応です。
行番号付きリストには対応しておりません。(GOTO,GOSUB-RETURN文にも対応していません)
書式設定、READ-DATA文、例外処理、Microsoft Basic互換モードや十進BASIC独自のもの等は未対応です。
(SUB COMMAND_???と記述されているコマンドのみです)

また、言語仕様の違いによるものなども対応していません。(●その他)

このプログラムでは構文解析などは行っておりません。
単純な置換処理を行っているだけです。

FOR I=1 TO 10   →   for (i=1;i<=10;i+=1)

このプログラムはC++変換のみです。コンパイル、実行(EXEファイル)は手動です。
全て自己責任でお願いします。(無限ループ等にご注意ください)

標準入出力を使用しています。表示数が多い時はリダイレクト「>」をしてください。
但し、入力メッセージも標準出力しています。

INPUT PROMPT "A,B=":A,B   →  cout << "A,B="; 標準出力
                              cin >> a >> b;  標準入力(スペースで区切る)
検証(●その他)、テスト(C++変換,コンパイル、EXE実行)がまだまだ不十分です。(ゴメンナサイ!! 検証とC++変換までで精一杯です)

● C/C++コンパイラー
フリーで使用できるC/C++コンパイラー(登録不要)に、GCC(「g++ --version」で4.8.1)を使用しました。
その他のC/C++コンパイラー(VC++等)でも使用できると思いますが、他のコンパイラーについては全く
検証・テストしておりませんので、ご了承ください。

○ MinGW(Minimalist GNU for Windows) GNUコンパイラコレクション
http://sourceforge.jp/projects/mingw/

○ MinGw インストール
http://rei-farms.jp/blog/webmaking/2565/


○ コンパイル方法(sample.cppの場合)
「g++.exe」コマンドを使用します。

g++ -O2 sample.cpp -o sample.exe
「-O2」は最適化オプション 「-o」は出力ファイル名。(「g++ --help」「g++ --target-help」でヘルプ。「gcc --help」でも同じ)

又は
gcc -O2 sample.cpp -o sample.exe -l stdc++

○ gccオプション
http://www.ysr.net.it-chiba.ac.jp/data/cc.html

● Boostライブラリー
1000桁モード、有理数モードはBoostライブラリーを使用します。
下記よりダウンロードして解凍するだけです。(ビルドは必要ありません)


○ 1000桁モードを浮動小数1000桁型で定義しています。(14368桁まで?)

○ 有理数モードを任意精度有理数型で定義しています。

   任意精度整数型

   有理数型(多倍長ではない)

(他にもgmp,mpfr,mpirライブラリーなどがあるのだが...浮動小数100000000桁!!!!)
http://www.mpfr.org/

○ コンパイル方法
boost.png
index.html
LICENSE_1_0.txt
bootstrap.bat
boostフォルダ
docフォルダ
などが入っているフォルダを「-I」オプションで指定します。

g++ -I I:\MinGW\boost_1_55_0\boost_1_55_0 sample.cpp -o sample.exe

● その他(備考)
○long double型での計算がおかしい。原因不明。

#include <iostream>
using namespace std;
#include <cmath>
int main() {
long double x;
double y;
x=2.0;
y=2.0;
cout << sizeof(long double) << endl;
cout << sizeof(double) << endl;
cout << sqrt(x) << endl;
cout << sqrt(y) << endl;
}
実行結果
12
8
-2.7341e-053
1.41421

○ 想定外の書き方をしたプログラムは誤変換(誤動作)します。
DIM SQR(10)
PRINT """A"""
など
スペース「" "」1個の違いで誤変換したりします。

○ 変数宣言ルーチンで探索対象としているのは、LET文の左辺、FOR文の制御変数、DIM文、INPUT文で使用している変数、
CALL文での受け渡し、及びPUBLIC文でのグローバル変数宣言だけです。
それら以外で現れる変数は探索対象になっていません。

○ 配列の添字に使用する変数がint型とするため、このプログラムでは
for文で使用する変数をint型としていましたが、添え字へのアクセスを
強制的にint型へキャストさせることでdouble型で宣言することにしました。(仕様変更)

a[5]        → a[static_cast<int>(5)]                       定数をキャストしてもコンパイルエラーにならない。
a[sqrt(n)]  → a[static_cast<int>(sqrt(n))]                 関数でもキャストすればエラーにならない。
a[a[n]]     → a[static_cast<int>(a[static_cast<int>(n)])]  キャストしないとエラーになる。
a[z]        → a[static_cast<int>(abs(z))]                  変数zが複素数型の場合。(zがdouble型でも問題ない)

○ 配列の宣言においてC/C++では静的配列と動的配列は区別され、
C++では動的配列にnew演算子を使用して

double *b;
b = new double[n];

として確保し、delete[] 演算子で(確保時に失敗したときのエラー処理も必要です)

delete[] b;
として適切に開放する必要があります。これにはメモリーリークという問題と関連します。

このプログラムではそれらを区別していません。

DIM A(10),B(N)  →  double a[10+1]={},b[n+1];  (C99規格 可変長配列)

添字の下限指定は、定数かつ負数でない場合です。
DIM A(-10 TO 10)  未対応
DIM A(N TO M)     未対応
DIM A(5 TO 20) → DIM A(20)として変換処理する

なお、可変長配列の初期化はfor文によるループで行っています。

○ 割り算においてint型同士の場合、計算結果もint型になる
int型/int型        → int型        8  / 5  → 1
double型/int型     → double型    8.0 / 5  → 1.6
   int型/double型  → double型
このため、どちらも定数の場合、型変換(キャスト)しています。 8 / 5 →  8 /(double)5

○ SUB文では呼び出しが参照渡しで、実引数を書き換えることができる。
このため、仮引数に「&」をつけて参照渡しにしています。

SUB SAMPLE(A,B) → void sample(double &a,double &b)

ところが、実引数に定数や計算式を使って呼び出すようにしていると、
引数が戻り値を受け取ることができないため、コンパイルエラーになります。
このため、ダミー変数を用意した。
CALL SAMPLE(A+B,C) → double dummy1=a+b;
                       sample(dummy1,c);
○ SUBやFUNCTIONで配列引数に多次元配列での受け渡しができません。

  SUB A(N,M,ARRAY(,))
で2次元配列の受け渡しができますが、これを

void a(double &n,double &m,double array[][])
と変換できません。

○ 配列の値渡しができません。関数定義(FUNCTION 文)内で配列要素を書き換えても
実引数は影響を受けませんが、C/C++では配列引数は参照渡しになるため、
書き換えを行っていると実引数は影響を受けます。
(構造体にして渡す方法があるようだが...)

○ SUBやFUNCTION名、変数名がC/C++の予約語と重なっていた場合、"_"アンダーバーを付加しています。

CALL TRY(A)  → _try(a);
LET SWITCH=1 → _switch=1;

○ 複素数モードでは変数を複素数型で宣言していますが、

IF A<B THEN
これを

if (a<b) {
のようにしても大小比較ができません。
これは

if (real(a)<real(b)) {

のように修正する必要があります。

但し、変数a,bが複素数型ではない時(double型の時)は、real関数は
使用できません。
○ ファイル入出力モード(ACCESS OUTIN,又は省略)

OPEN #1:NAME FILENAME$
PRINT #1:A
CLOSE #1
これを

fstream fs1(filename,ios::in | ios::out);
fs1 << a << endl;
fs1.close();
としてもファイルが生成されず

fstream fs1(filename,ios::out);
fs1 << a << endl;
fs1.close();
としないとファイルが生成されないようだ。

○「LET A=1,B=2,B=3」の書き方に対応しました。(仕様追加)

なお、これらの詳細については、ネット上で検索してください。
(大量書き込みにつきましては、ご容赦ください)
 

Re: C++トランスレーター(試作・テスト版)

 投稿者:しばっち  投稿日:2014年 7月16日(水)20時49分32秒
  > No.3430[元記事へ]

OPTION ARITHMETIC NATIVE
LET MAXLINE=1000
LET MAXSIZE=100
DIM VARIABLE_STRING$(MAXSIZE),VARIABLE_PUBLIC$(MAXSIZE)
DIM C$(MAXSIZE),C2$(4),A$(MAXLINE),VARIABLE_ARGUMENT$(MAXSIZE),VARIABLE_ARRAY$(MAXSIZE)
DIM EXPRESSION$(MAXSIZE,3),VA_DIM(MAXSIZE),VA_INIT$(MAXSIZE),VARIABLE_DIM$(MAXSIZE)
DIM VARIABLE_FOR$(MAXSIZE)
PUBLIC STRING FUNCNAME$(100),FUNCNAMENOARG$(100),VARIABLE$(100),TYPE$
PUBLIC NUMERIC FC,VA_COUNT
FILE GETOPENNAME F$,"BASファイル|*.BAS"
IF F$="" THEN STOP
OPEN #1:NAME F$
CALL CPRINT("// Converted file  "&F$)
CALL CPRINT("")
CALL CPRINT("#include <iostream>")
CALL CPRINT("using namespace std;")
LET SETPREC$=" setprecision(15) << "                                            !'マニピュレータ 15桁表示
LET TYPE$="double "                                                             !'10進、2進モード(double型)
FOR I=1 TO MAXLINE                                                              !'インクルードファイル設定
   LINE INPUT #1, IF MISSING THEN EXIT FOR:A$(I)
   CALL SETUP(A$(I))                                                            !'コマンド等を大文字化
   IF A$(I)(1:1)="&" THEN                                                       !'行継続
      LET A$(I)(1:1)=""
      FOR J=LEN(A$(I-1)) TO 1 STEP -1
         IF A$(I-1)(J:J)="&" THEN
            LET A$(I-1)(J:J)=TRIM$(A$(I))
            LET A$(I)=""
            EXIT FOR
         END IF
      NEXT J
   END IF
   IF POS(A$(I),"OPTION ARITHMETIC COMPLEX")>0 AND OPT=0 THEN
      CALL CPRINT("#include <complex>")
      LET TYPE$="complex <double> "                                             !'複素数型
      LET OPT=1
   END IF
   IF POS(A$(I),"OPTION ARITHMETIC DECIMAL_HIGH")>0 AND OPT=0 THEN
      CALL CPRINT("#include <boost/multiprecision/cpp_dec_float.hpp>")
      !'CALL CPRINT("#include <boost/math/constants/constants.hpp>")            !'PI値
      CALL CPRINT("using namespace boost::multiprecision;")
      CALL CPRINT("typedef number<cpp_dec_float<1000> > big_float;")            !'浮動小数1000桁型
      LET SETPREC$=" setprecision(1001) << "                                    !'マニピュレータ 1001桁表示
      LET TYPE$="big_float "
      LET OPT=1
   END IF
   IF POS(A$(I),"OPTION ARITHMETIC RATIONAL")>0 AND OPT=0 THEN
      CALL CPRINT("#include <boost/multiprecision/cpp_int.hpp>")
      CALL CPRINT("using namespace boost::multiprecision;")
      !'LET TYPE$="cpp_int "                                                    !'任意精度整数型
      LET TYPE$="cpp_rational "                                                 !'任意精度有理数型
      !' CALL CPRINT("#include <boost/rational.hpp>")
      !' LET TYPE$="rational <int> "                                            !'有理数型
      LET OPT=1
   END IF
   IF POS(A$(I),"OPEN")>0 AND LITERAL(A$(I),"OPEN")=0 AND INCLUDEOPEN=0 THEN
      LET INCLUDEOPEN=1
      CALL CPRINT("#include <fstream>")
   END IF
   RESTORE
   DO
      READ IF MISSING THEN EXIT DO:X$
      LET S$=X$(1:LEN(X$)-1)&" ("
      IF (POS(A$(I),X$)>0 AND LITERAL(A$(I),X$)=0 OR POS(A$(I),S$)>0 AND LITERAL(A$(I),S$)=0) AND INCLUDEMATH=0 THEN
         LET INCLUDEMATH=1
         CALL CPRINT("#include <cmath>")
         EXIT DO
      END IF
   LOOP
   DATA "ABS(","ACOS(","ASIN(","ATN(","ANGLE(","CSC(","COS(","COSH(","COT(","CEIL(","EXP(","FP(","IP(","INT("
   DATA "LOG(","LOG2(","LOG10(","MOD(","REMAINDER(","SIN(","SINH(","SEC(","SQR("
   DATA "TAN(","TANH(","TRUNCATE(","^"
   DATA "ACSC(","ASEC(","ACOT(","ATANH(","ASINH(","ACOSH(","ASECH(","ACSCH(","ACOTH(","CSCH(","COTH(","CBRT(","ERF(","ERFC("
   DATA "LGAMMA(","GAMMA(","HYPOT(","J0(","J1(","JN(","SECH(","Y0(","Y1(","YN("
   IF POS(A$(I),"$")>0 AND INCLUDESTR=0 THEN
      LET INCLUDESTR=1
      CALL CPRINT("#include <string>")
      CALL CPRINT("#include <sstream>")
      IF INCLUDERND=0 THEN CALL CPRINT("#include <cstdlib>")
   END IF
   IF ((POS(A$(I),"RANDOMIZE")>0 AND LITERAL(A$(I),"RANDOMIZE")=0) OR (POS(A$(I),"TIME$")=0 AND POS(A$(I),"TIME")>0 AND LITERAL(A$(I),"TIME")=0)) AND INCLUDERND=0 THEN
      LET INCLUDERND=1
      IF INCLUDESTR=0 THEN CALL CPRINT("#include <cstdlib>")
      CALL CPRINT("#include <ctime>")
   END IF
NEXT I
LET MAXLINE=I
CLOSE #1
IF TYPE$="double " OR TYPE$="complex <double> " THEN CALL CPRINT("#include <iomanip>")
IF OPT=1 OR INCLUDEOPEN=1 OR INCLUDEMATH=1 OR INCLUDESTR=1 OR INCLUDERND=1 THEN CALL CPRINT("")
LET PROTOTYPE=1
FOR I=1 TO MAXLINE                                                              !'プロトタイプ宣言
   LET X$=A$(I)
   IF POS(X$,"DECLARE")=0 AND POS(X$,"END")=0 AND POS(X$,"EXIT")=0 AND POS(X$,"(,)")=0 AND POS(X$,"(,,)")=0 AND POS(X$,"EXTERNAL")=1 THEN
      IF POS(X$,"FUNCTION ")>0 AND LITERAL(X$,"FUNCTION ")=0 THEN
         CALL COMMAND_FUNCTION(X$)
         LET FL=1
      END IF
      IF POS(X$,"SUB ")>0 AND LITERAL(X$,"SUB ")=0 THEN
         CALL COMMAND_SUB(X$)
         LET FL=1
      END IF
   END IF
NEXT I
IF FL=1 THEN CALL CPRINT("")
LET PROTOTYPE=0
FOR I=1 TO MAXLINE                                                              !'以下、関数定義(インライン関数)
   IF POS(A$(I),"ROUND(")>0 AND FL_ROUND=0 THEN
      CALL CPRINT("inline double round(double x,double n) {")                   !'関数オーバーロード
      CALL CPRINT("return floor(x*pow(10,n)+.5)/pow(10,n);")
      CALL CPRINT("}")
      CALL CPRINT("inline double round(double x) {")                            !'関数オーバーロード
      CALL CPRINT("return floor(x+.5);")
      CALL CPRINT("}")
      LET FL_ROUND=1
   END IF
   IF (POS(A$(I),"SGN(")>0 OR POS(A$(I),"IP(")>0 OR POS(A$(I),"FP(")>0 OR POS(A$(I),"TRUNCATE(")>0) AND FL_SGN=0 THEN
      CALL CPRINT("inline double sgn(double x) {")
      CALL CPRINT("if (x>0) return 1.0;")
      CALL CPRINT("if (x<0) return -1.0;")
      CALL CPRINT("return 0;")
      CALL CPRINT("}")
      CALL CPRINT("inline double ip(double x) {")
      CALL CPRINT("return sgn(x)*floor(abs(x));")
      CALL CPRINT("}")
      CALL CPRINT("inline double fp(double x) {")
      CALL CPRINT("return x-ip(x);")
      CALL CPRINT("}")
      CALL CPRINT("inline double truncate(double x,double n) {")
      CALL CPRINT("return ip(x*pow(10.0,(int)n))/pow(10.0,(int)n);")
      CALL CPRINT("}")
      !'CALL CPRINT("inline double remainder(double x,double y) {")
      !'CALL CPRINT("return x-y*ip(x/y);")
      !'CALL CPRINT("}")
      LET FL_SGN=1
   END IF
   IF POS(A$(I),"ANGLE(")>0 AND FL_ANGLE=0 THEN
      CALL CPRINT("inline double angle(double x,double y) {")
      CALL CPRINT("return atan2(y,x);")
      CALL CPRINT("}")
      LET FL_ANGLE=1
   END IF
   IF POS(A$(I),"ASECH(")>0 AND FL_ASECH=0 THEN
      CALL CPRINT("inline double asech(double x) {")
      CALL CPRINT("return log((sqrt(1.0-x*x)+1.0)/x);")
      CALL CPRINT("}")
      LET FL_ASECH=1
   END IF
   IF POS(A$(I),"ACSCH(")>0 AND FL_ACSCH=0 THEN
      CALL CPRINT("inline double acsch(double x) {")
      CALL CPRINT("return log((sgn(x)*sqrt(x*x+1.0)+1.0)/x);")
      CALL CPRINT("}")
      LET FL_ACSCH=1
   END IF
   IF POS(A$(I),"ACOTH(")>0 AND FL_ACOTH=0 THEN
      CALL CPRINT("inline double acoth(double x) {")
      CALL CPRINT("return log((x+1.0)/(x-1.0))/2.0;")
      CALL CPRINT("}")
      LET FL_ACOTH=1
   END IF
   IF POS(A$(I),"FACT(")>0 AND FL_FACT=0 THEN
      CALL CPRINT("inline double fact(double x) {")
      CALL CPRINT("if (x<=1.0) return 1.0;")
      CALL CPRINT("return fact(x-1)*x;")
      CALL CPRINT("}")
      LET FL_FACT=1
   END IF
   IF POS(A$(I),"PERM(")>0 AND FL_PERM=0 THEN
      CALL CPRINT("inline double perm(double n,double r) {")
      !'CALL CPRINT("return fact(n)/fact(n-r);")
      CALL CPRINT("double p=1.0;")
      CALL CPRINT("for (int i=1;i<=r;i++)")
      CALL CPRINT("p*=n-i+1.0;")
      CALL CPRINT("return p;")
      CALL CPRINT("}")
      LET FL_PERM=1
   END IF
   IF POS(A$(I),"COMB(")>0 AND FL_COMB=0 THEN
      CALL CPRINT("inline double comb(double n,double r) {")
      !' CALL CPRINT("return perm(n,r)/fact(r);")
      CALL CPRINT("double p=1.0;")
      CALL CPRINT("for(int i=1;i<=r;i++)")
      CALL CPRINT("p*=(n-i+1.0)/(double)i;")
      CALL CPRINT("return p;")
      CALL CPRINT("}")
      LET FL_COMB=1
   END IF
   IF (POS(A$(I),"GCD(")>0 OR POS(A$(I),"LCM(")>0) AND FL_GCD=0 THEN
      CALL CPRINT("inline double gcd(double m,double n) {")
      CALL CPRINT("if (n==0) return m;")
      CALL CPRINT("return gcd(n,fmod(m,n));")
      CALL CPRINT("}")
      CALL CPRINT("inline double lcm(double m,double n) {")
      CALL CPRINT("return m*n/gcd(m,n);")
      CALL CPRINT("}")
      LET FL_GCD=1
   END IF
   !'IF POS(A$(I),"MAX(")>0 AND FL_MAX=0 THEN
   !'   CALL CPRINT("template <typename T>")
   !'   CALL CPRINT("inline T tmax(T x , T y) {")
   !'   CALL CPRINT("if(x>y) return x;")
   !'   CALL CPRINT("return y;")
   !'   CALL CPRINT("}")
   !'   LET FL_MAX=1
   !'END IF
   !'IF POS(A$(I),"MIN(")>0 AND FL_MIN=0 THEN
   !'   CALL CPRINT("template <typename T>")
   !'   CALL CPRINT("inline T tmin(T x , T y) {")
   !'   CALL CPRINT("if(x<y) return x;")
   !'   CALL CPRINT("return y;")
   !'   CALL CPRINT("}")
   !'   LET FL_MIN=1
   !'END IF
   IF POS(A$(I),"BITOR(")>0 AND FL_BITOR=0 THEN
      CALL CPRINT("inline int bit_or(double x,double y) {")
      CALL CPRINT("return (int)x|(int)y;")
      CALL CPRINT("}")
      LET FL_BITOR=1
   END IF
   IF POS(A$(I),"BITAND(")>0 AND FL_BITAND=0 THEN
      CALL CPRINT("inline int bit_and(double x,double y) {")
      CALL CPRINT("return (int)x&(int)y;")
      CALL CPRINT("}")
      LET FL_BITAND=1
   END IF
   IF POS(A$(I),"BITXOR(")>0 AND FL_BITXOR=0 THEN
      CALL CPRINT("inline int bit_xor(double x,double y) {")
      CALL CPRINT("return (int)x^(int)y;")
      CALL CPRINT("}")
      LET FL_BITXOR=1
   END IF
   IF POS(A$(I),"BITNOT(")>0 AND FL_BITNOT=0 THEN
      CALL CPRINT("inline int bit_not(double x) {")
      CALL CPRINT("return ~(int)x;")
      CALL CPRINT("}")
      LET FL_BITNOT=1
   END IF
   IF POS(A$(I),"BITIMP(")>0 AND FL_BITIMP=0 THEN
      CALL CPRINT("inline int bit_imp(double x,double y) {")
      CALL CPRINT("return ~(int)x|(int)y;")
      CALL CPRINT("}")
      LET FL_BITIMP=1
   END IF
   IF POS(A$(I),"BVAL(")>0 AND FL_BVAL=0 THEN
   !'  CALL CPRINT("inline int bval(string x,int n) {")
   !'  CALL CPRINT("if (n==16) x="&CHR$(34)&"0x"&CHR$(34)&"+x;")
   !'  CALL CPRINT("if (n==8) x="&CHR$(34)&"0"&CHR$(34)&"+x;")
   !'  CALL CPRINT("return atoi(x.c_str());")
   !'  CALL CPRINT("}")
      CALL CPRINT("inline int bval(string x,int n) {")
      CALL CPRINT("int y=0,a;")
      CALL CPRINT("string s="&CHR$(34)&"0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"&CHR$(34)&";")
      CALL CPRINT("for (int i=0;i<x.size();i++) {")
      CALL CPRINT("x[i]=toupper(x[i]);")
      CALL CPRINT("a=s.find(x[i],0);")
      CALL CPRINT("y=y*n+a;")
      CALL CPRINT("}")
      CALL CPRINT("return y;")
      CALL CPRINT("}")
      LET FL_BVAL=1
   END IF
   IF POS(A$(I),"VAL(")>0 AND POS(A$(I),"BVAL(")=0 AND FL_VAL=0 THEN
      CALL CPRINT("inline double val(string x) {")
      CALL CPRINT("return atof(x.c_str());")
      CALL CPRINT("}")
      LET FL_VAL=1
   END IF
   IF POS(A$(I),"UBOUND(")>0 AND FL_UBOUND=0 THEN
      CALL CPRINT("template <typename TYPE, size_t N>")
      CALL CPRINT("inline size_t ubound(const TYPE (&)[N]) {")
      CALL CPRINT("return N;")
      CALL CPRINT("}")
      LET FL_UBOUND=1
   END IF
   IF POS(A$(I),"SUBSTR$(")>0 AND FL_SUBSTR=0 THEN
      CALL CPRINT("inline string substr(string x,double m,double n) {")
      CALL CPRINT("return x.substr((int)m-1,(int)n);")
      CALL CPRINT("}")
      LET FL_SUBSTR=1
   END IF
   IF POS(A$(I),"BSTR$(")>0 AND POS(A$(I),"SUBSTR$(")=0 AND FL_BSTR=0 THEN
   !'  CALL CPRINT("inline string bstr(double x,int n) {")
   !'  CALL CPRINT("ostringstream stream;")
   !'  CALL CPRINT("if (n==16) stream << hex << (int)x;")
   !'  CALL CPRINT("else if (n==8) stream << oct << (int)x;")
   !'  CALL CPRINT("else  stream << dec << (int)x;")
   !'  CALL CPRINT("cout << dec;")                                              !'マニピュレータ設定を10進法に戻す
   !'  CALL CPRINT("return stream.str();")
   !'  CALL CPRINT("}")
      CALL CPRINT("inline string bstr(double x,int n) {")
      CALL CPRINT("string a="&CHR$(34)&"0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"&CHR$(34)&",b;")
      CALL CPRINT("int i;")
      CALL CPRINT("while (x!=0) {")
      CALL CPRINT("i=fmod((int)x,n);")
      CALL CPRINT("b=a[i]+b;")
      CALL CPRINT("x=floor(x/n);")
      CALL CPRINT("}")
      CALL CPRINT("return b;")
      CALL CPRINT("}")
      LET FL_BSTR=1
   END IF
   IF POS(A$(I),"STR$(")>0 AND POS(A$(I),"SUBSTR$(")=0 AND POS(A$(I),"BSTR$(")=0 AND FL_STR=0 THEN
      CALL CPRINT("inline string str(double x) {")
      CALL CPRINT("ostringstream stream;")
      CALL CPRINT("stream << x;")
      CALL CPRINT("return stream.str();")
      CALL CPRINT("}")
      LET FL_STR=1
   END IF
   IF POS(A$(I),"POS(")>0 AND FL_POS=0 THEN
      CALL CPRINT("inline int pos(string x,string y) {")                        !'関数オーバーロード
      CALL CPRINT("return x.find(y,0)+1;")
      CALL CPRINT("}")
      CALL CPRINT("inline int pos(string x,string y,double n) {")               !'関数オーバーロード
      CALL CPRINT("return x.find(y,(int)n)+1;")
      CALL CPRINT("}")
      LET FL_POS=1
   END IF
   IF POS(A$(I),"REPEAT$(")>0 AND FL_REPEAT=0 THEN
      CALL CPRINT("inline string repeat(string a,double n) {")
      CALL CPRINT("string x;")
      CALL CPRINT("for(int i=0;i<n;i++)")
      CALL CPRINT("x+=a;")
      CALL CPRINT("return x;")
      CALL CPRINT("}")
      LET FL_REPEAT=1
   END IF
   IF POS(A$(I),"SPC(")>0 AND FL_SPC=0 THEN
      CALL CPRINT("inline string spc(int n) {")
      CALL CPRINT("string x;")
      CALL CPRINT("for (int i=0;i<n;i++)")
      CALL CPRINT("x+="&CHR$(34)&" "&CHR$(34)&";")
      CALL CPRINT("return x;")
      CALL CPRINT("}")
      LET FL_SPC=1
   END IF
   !'IF POS(A$(I),"TAB(")>0 AND FL_TAB=0 THEN
   !'   CALL CPRINT("inline string tab(int n) {")
   !'   CALL CPRINT("string x;")
   !'   CALL CPRINT("for (int i=0;i<n;i++)")
   !'   CALL CPRINT("x+="&CHR$(34)&" "&CHR$(34)&";")
   !'   CALL CPRINT("return x;")
   !'   CALL CPRINT("}")
   !'   LET FL_TAB=1
   !'END IF
   IF POS(A$(I),"LTRIM$(")>0 AND FL_LTRIM=0 THEN
      CALL CPRINT("inline string ltrim(string x) {")
      CALL CPRINT("int i;")
      CALL CPRINT("for (i=0;i<x.size();i++)")
      CALL CPRINT("if(x.substr(i,1)!="&CHR$(34)&" "&CHR$(34)&") break;")
      CALL CPRINT("return x.substr(i);")
      CALL CPRINT("}")
      LET FL_LTRIM=1
   END IF
   IF POS(A$(I),"RTRIM$(")>0 AND FL_RTRIM=0 THEN
      CALL CPRINT("inline string rtrim(string x) {")
      CALL CPRINT("int i;")
      CALL CPRINT("for (i=x.size();i>0;i--)")
      CALL CPRINT("if(x.substr(i,1)!="&CHR$(34)&" "&CHR$(34)&") break;")
      CALL CPRINT("return x.substr(0,i);")
      CALL CPRINT("}")
      LET FL_RTRIM=1
   END IF
   IF POS(A$(I),"ORD(")>0 AND FL_ORD=0 THEN
      CALL CPRINT("inline int ord(string x) {")
      CALL CPRINT("return (int)x[0];")
      CALL CPRINT("}")
      LET FL_ORD=1
   END IF
   IF POS(A$(I),"CHR$(")>0 AND FL_CHR=0 THEN
      CALL CPRINT("inline string chr(char x) {")
      CALL CPRINT("return string(1,x);")
      CALL CPRINT("}")
      LET FL_CHR=1
   END IF
   IF POS(A$(I),"UCASE$(")>0 AND FL_UCASE=0 THEN
      CALL CPRINT("inlne string ucase(string x) {")
      CALL CPRINT("string s;")
      CALL CPRINT("for(int i=0;i<x.size();i++)")
      CALL CPRINT("s+=toupper(x[i]);")
      CALL CPRINT("return s;")
      CALL CPRINT("}")
      LET FL_UCASE=1
   END IF
   IF POS(A$(I),"LCASE$(")>0 AND FL_LCASE=0 THEN
      CALL CPRINT("inline string lcase(string x) {")
      CALL CPRINT("string s;")
      CALL CPRINT("for(int i=0;i<x.size();i++)")
      CALL CPRINT("s+=tolower(x[i]);")
      CALL CPRINT("return s;")
      CALL CPRINT("}")
      LET FL_LCASE=1
   END IF
   IF POS(A$(I),"LEFT$(")>0 AND FL_LEFT=0 THEN
      CALL CPRINT("inline string left(string x,double n) {")
      CALL CPRINT("return x.substr(x.size()-(int)n,(int)n);")
      CALL CPRINT("}")
      LET FL_LEFT=1
   END IF
   IF POS(A$(I),"RIGHT$(")>0 AND FL_RIGHT=0 THEN
      CALL CPRINT("inline string right(string x,double n) {")
      CALL CPRINT("return x.substr(0,(int)n);")
      CALL CPRINT("}")
      LET FL_RIGHT=1
   END IF
   IF POS(A$(I),"MID$(")>0 AND FL_MID=0 THEN
      CALL CPRINT("inline string mid(string x,double m,double n) {")
      CALL CPRINT("return x.substr((int)m-1,(int)m+(int)n-1);")
      CALL CPRINT("}")
      LET FL_MID=1
   END IF
   !'IF POS(A$(I),"SWAP ")>0 AND FL_SWAP=0 THEN
   !'   CALL CPRINT("template <typename T>")
   !'   CALL CPRINT("inline void cswap(T &x,T &y) {")
   !'   CALL CPRINT("T z;")
   !'   CALL CPRINT("z=x;x=y;y=z;")
   !'   CALL CPRINT("}")
   !'   LET FL_SWAP=1
   !'END IF
   IF POS(A$(I),"EPS(")>0 AND FL_EPS=0 THEN
      CALL CPRINT("inline double eps(x) {")
      CALL CPRINT("if (x==0) return 1e-99;")
      CALL CPRINT("if (x>0 && x<10) return 1e-14;")
      CALL CPRINT("return fmax(pow(10,floor(log10(abs(x))-14)),1e-99);")
      CALL CPRINT("}")
      LET FL_EPS=1
   END IF
   IF POS(A$(I),"DEF ")>0 AND LITERAL(A$(I),"DEF ")=0 THEN
      LET X$=A$(I)
      IF POS(X$,"!")>0 AND LITERAL(X$,"!")=0 THEN LET X$=TRIM$(FRONTSTRING$(X$,"!"))
      IF POS(X$,"REM ")>0 AND LITERAL(X$,"REM ")=0 THEN LET X$=TRIM$(FRONTSTRING$(X$,"REM "))
      IF X$<>"" THEN
         IF POS(X$,"(")=0 THEN
            LET FNAME$=TRIM$(BETWEENSTRING$(X$,"DEF ","="))
         ELSE
            LET FNAME$=TRIM$(BETWEENSTRING$(X$,"DEF ","("))
         END IF
         LET FNAME$=LCASE$(TRANSRESERVED$(FNAME$))
         IF POS(FNAME$,"$")>0 THEN LET FNAME$=TRANSFORM$(FNAME$,"$","")
         LET FC=FC+1
         LET FUNCNAME$(FC)=FNAME$
         IF POS(X$,"(")>0 THEN
            LET BETWEEN$=BETWEENSTRING$(X$,"(",")")
            LET BETWEEN$=TRIM$(LCASE$(TRANSRESERVED$(BETWEEN$)))
         ELSE
            LET BETWEEN$=""
            LET FUNCNAMENOARG$(FC)=FNAME$&"()"
         END IF
         LET BEHIND$=BEHINDSTRING$(X$,"=")
         LET BEHIND$=TRANSRESERVED$(BEHIND$)
         LET BEHIND$=TRIM$(LCASE$(TRANSFUNC$(BEHIND$)))
         IF POS(BETWEEN$,",")=0 THEN
            IF BETWEEN$="" THEN
               CALL CPRINT("inline "&TYPE$&FNAME$&"(void) {")
            ELSE
               CALL CPRINT("inline "&TYPE$&FNAME$&"("&TYPE$&BETWEEN$&") {")
            END IF
         ELSE
            LET AA$=""
            CALL TOKUN(BETWEEN$,C$,N)
            FOR J=1 TO N
               LET AA$=AA$&TYPE$&C$(J)&","
            NEXT J
            CALL CPRINT("inline "&TYPE$&FNAME$&"("&AA$(1:LEN(AA$)-1)&") {")
         END IF
         CALL CPRINT("return "&BEHIND$&";")
         CALL CPRINT("}")
      END IF
   END IF
NEXT I
LET CTAB=0
FOR I=1 TO MAXLINE
!' IF POS(A$(I),"EPS")>0 AND FL_EPS=0 THEN
!'    IF TYPE$="bigfloat " THEN
!'       CALL CPRINT("bigfloat eps;")
!'       CALL CPRINT("eps.assign("&CHR$(34)&"1e-1000"&CHR$(34)&");")
!'    ELSE
!'       CALL CPRINT("const double eps=1e-14;")
!'    END IF
!'    LET FL_EPS=1
!' END IF
   IF POS(A$(I),"PI")>0 AND LITERAL(A$(I),"PI")=0 AND FL_PI=0 THEN
   !'IF TYPE$="big_float " THEN CALL CPRINT("const big_float pi=boost::math::constants::pi<big_float>();")
      IF TYPE$="double " OR TYPE$="complex <double> " THEN
      !'CALL CPRINT("const double pi=M_PI;")
         CALL CPRINT("const double pi=3.1415926535897932384626;")
      END IF
      LET FL_PI=1
   END IF
   IF (POS(A$(I),"PUBLIC NUMERIC")>0 OR POS(A$(I),"PUBLIC STRING")>0) AND LITERAL(A$(I),"PUBLIC")=0 THEN   !'グローバル変数宣言
      LET X$=A$(I)
      CALL COMMAND_PUBLIC(X$)
      LET FL_PUBLIC=1
   END IF
NEXT I
 

Re: C++トランスレーター(試作・テスト版)

 投稿者:しばっち  投稿日:2014年 7月16日(水)20時50分46秒
  続き 2

IF FL_EPS=1 OR FL_PI=1 OR FL_PUBLIC=1 THEN CALL CPRINT("")
CALL CPRINT("int main(int argc, char* argv[]) {")
MAT VARIABLE_ARGUMENT$=NUL$
LET VA_ARGUMENT_COUNT=0
CALL SCAN$(A$,1)                                                                !'ローカル変数宣言
!'CALL CPRINT("cout <<"&SETPREC$(1:POS(SETPREC$,"<<")-1)&";")                   !'マニピュレータ設定
!'CALL CPRINT("cout << uppercase << dec << right << fixed;")
FOR II=1 TO MAXLINE                                                             !'メイン処理
   CALL MAIN(A$(II))
NEXT II
STOP

SUB MAIN(X$)                                                                    !'分岐処理ルーチン
   LET X$=TRIM$(X$)
   IF X$="" THEN EXIT SUB
   IF POS(X$,"!")>0 OR POS(X$,"REM ")>0 THEN                                    !'「REM・AINDER」
      IF LITERAL(X$,"!")=0 AND LITERAL(X$,"REM")=0 THEN
         CALL COMMAND_REM(X$)                                                   !'「IF A$="!" THEN 」
         EXIT SUB
      END IF
   END IF
   CALL CPRINT("// "&X$)
   IF POS(X$,"IF ")=1 OR POS(X$,"IF(")=1 OR POS(X$,"ELSEIF")=1 THEN
      IF (POS(X$,"END")=0 OR LITERAL(X$,"END")<>0) AND (POS(X$,"READ")=0 OR LITERAL(X$,"READ")<>0) AND (POS(X$,"INPUT")=0 OR LITERAL(X$,"INPUT")<>0) THEN
         CALL COMMAND_IF(X$)
         EXIT SUB                                                               !'「READ IF MISSING THEN」「INPUT #1,IF MISSING THEN」「END IF」
      END IF
   END IF
   IF POS(X$,"LET ")=1 THEN
      CALL COMMAND_LET(X$)
      EXIT SUB
   END IF
   IF POS(X$,"PRINT")=1 AND (POS(X$,"USING")=0 OR LITERAL(X$,"USING")<>0) THEN
      CALL COMMAND_PRINT(X$)                                                    !'「PRINT "USING"」
      EXIT SUB
   END IF
   IF POS(X$,"CASE ")>0 AND LITERAL(X$,"CASE")=0 THEN
      CALL COMMAND_CASE(X$)                                                     !'「SELECT CASE」「CASE ELSE」
      EXIT SUB
   END IF
   IF POS(X$,"CALL ")=1 THEN
      CALL COMMAND_CALL(X$)
      EXIT SUB
   END IF
   IF POS(X$,"ELSE")=1 AND POS(X$,"CASE")=0 AND POS(X$,"ELSEIF")=0 AND LITERAL(X$,"ELSE")=0 AND LITERAL(X$,"CASE")=0 THEN
      CALL COMMAND_ELSE(X$)
      EXIT SUB
   END IF
   IF POS(X$,"DIM ")=1 THEN
      CALL COMMAND_DIM(X$)
      EXIT SUB
   END IF
   IF POS(X$,"FUNCTION ")>0 AND POS(X$,"DECLARE")=0 AND POS(X$,"(,)")=0 AND POS(X$,"(,,)")=0 AND LITERAL(X$,"FUNCTION ")=0 AND POS(X$,"EXTERNAL")=1 THEN
      CALL COMMAND_FUNCTION(X$)
      EXIT SUB
   END IF
   IF POS(X$,"SUB ")>0 AND POS(X$,"DECLARE")=0 AND POS(X$,"(,)")=0 AND POS(X$,"(,,)")=0 AND LITERAL(X$,"SUB ")=0 AND POS(X$,"EXTERNAL")=1 THEN
      CALL COMMAND_SUB(X$)
      EXIT SUB
   END IF
   IF POS(X$,"FOR ")=1 THEN
      CALL COMMAND_FOR(X$)
      EXIT SUB
   END IF
   IF POS(X$,"NEXT ")=1 THEN
      CALL COMMAND_NEXT(X$)
      EXIT SUB
   END IF
   IF POS(X$,"DO")=1 THEN
      CALL COMMAND_DO(X$)
      EXIT SUB
   END IF
   IF POS(X$,"LOOP")=1 THEN
      CALL COMMAND_LOOP(X$)
      EXIT SUB
   END IF
   IF POS(X$,"INPUT ")=1 AND POS(X$,"IF")=0 AND POS(X$,"CHARACTER")=0 AND POS(X$,"ELAPSED")=0 AND POS(X$,"TIMEOUT")=0 OR POS(X$,"LINE INPUT")=1 THEN
      CALL COMMAND_INPUT(X$)                                                    !'「CHARACTER INPUT」「INPUT ELAPSED」「INPUT TIMEOUT」「INPUT #1,IF MISSING THEN 」
      EXIT SUB
   END IF
   IF POS(X$,"END")=1 AND POS(X$,"PICTURE")=0 THEN
      CALL COMMAND_END(X$)                                                      !'「END」「END IF」「END SUB」「END FUNCTION」「END SELECT」
      EXIT SUB
   END IF
   IF POS(X$,"EXIT")=1 AND POS(X$,"PICTURE")=0 THEN
      CALL COMMAND_EXIT(X$)
      EXIT SUB
   END IF
   IF POS(X$,"RANDOMIZE")=1 THEN
      CALL COMMAND_RANDOMIZE(X$)
      EXIT SUB
   END IF
   IF POS(X$,"STOP")=1 THEN
      CALL COMMAND_STOP(X$)
      EXIT SUB
   END IF
   IF POS(X$,"OPEN ")=1 THEN
      CALL COMMAND_OPEN(X$)
      EXIT SUB
   END IF
   IF POS(X$,"CLOSE")=1 THEN
      CALL COMMAND_CLOSE(X$)
      EXIT SUB
   END IF
   IF POS(X$,"SWAP ")=1 THEN
      CALL COMMAND_SWAP(X$)
      EXIT SUB
   END IF
   IF POS(X$,"OPTION ARITHMETIC")>0 THEN EXIT SUB
   IF POS(X$,"OPTION ANGLE RADIANS")>0 THEN EXIT SUB
   IF POS(X$,"OPTION BASE")>0 THEN EXIT SUB
   IF POS(X$,"OPTION CHARACTER BYTE")>0 THEN EXIT SUB
   IF POS(X$,"PUBLIC")>0 THEN EXIT SUB
   IF POS(X$,"DEF ")>0 THEN EXIT SUB
   IF POS(X$,"DECLARE EXTERNAL")>0 THEN EXIT SUB
   IF POS(X$,"SET ECHO")>0 THEN EXIT SUB
   CALL CPRINT(X$&" // ******* 未対応です *******")
END SUB

SUB COMMAND_PUBLIC(XX$)                                                         !'以下、各コマンド処理ルーチン
   LOCAL I,K,J,N,L
   LET X$=XX$
   IF POS(X$,"!")>0 THEN LET X$=TRIM$(FRONTSTRING$(X$,"!"))
   IF POS(X$,"REM ")>0 THEN LET X$=TRIM$(FRONTSTRING$(X$,"REM "))
   LET X$=TRANSRESERVED$(X$)                                                    !'変数名がC/C++予約語なら変換
   LET X$=TRANSFORM$(X$,"(","[")                                                !'配列変数なら変換
   LET X$=TRANSFORM$(X$,")","+1]")
   LET I=1
   DO
      IF X$(I:I)="[" THEN
         LET J=I+1
         DO
            IF X$(J:J)="," THEN                                                 !'多次元配列時の処理
               LET X$(J:J)="+1]["
               LET J=J+4
            END IF
            LET J=J+1
         LOOP UNTIL X$(J:J)="]"
      END IF
      LET I=I+1
   LOOP UNTIL LEN(X$)<=I
   IF POS(X$,"NUMERIC")>0 THEN
      LET BEHIND$=BEHINDSTRING$(X$,"NUMERIC ")
   ELSEIF POS(X$,"STRING")>0 THEN
      LET BEHIND$=BEHINDSTRING$(X$,"STRING ")
   END IF
   CALL TOKUN(BEHIND$,C$,K)
   FOR I=1 TO K
      IF POS(C$(I),"TO")>0 THEN                                                 !'下限指定 TO句の処理
         IF POS(C$(I),"][")=0 THEN                                              !'1次元配列
            LET AA$=TRIM$(BETWEENSTRING$(C$(I),"[","TO"))
            LET BB$=TRIM$(BETWEENSTRING$(C$(I),"TO","+1]"))
            IF POS("0123456789",AA$(1:1))=0 OR POS("0123456789",BB$(1:1))=0 THEN   !'定数でないなら
               CALL CPRINT(XX$&" // ******* 未対応です *******")
               EXIT SUB
            ELSE
               LET C$(I)=C$(I)(1:POS(C$(I),"["))&BB$&"+1]"
            END IF
         ELSE                                                                   !'多次元配列時の処理
            LET CC$=""
            LET J=0
            LET L=0
            LET N=0
            DO
               LET J=POS(C$(I),"[",J+1)
               LET L=POS(C$(I),"+1]",L+1)
               LET N=N+1
               LET C2$(N)=C$(I)(J+1:L-1)
            LOOP UNTIL L+2>=LEN(C$(I))
            FOR J=1 TO N
               LET AA$=TRIM$(FRONTSTRING$(C2$(J),"TO"))
               LET BB$=TRIM$(BEHINDSTRING$(C2$(J),"TO"))
               IF POS("0123456789",AA$(1:1))=0 OR POS("0123456789",BB$(1:1))=0 THEN
                  CALL CPRINT(XX$&" // ******* 未対応です *******")
                  EXIT SUB
               ELSE
                  LET CC$=CC$&BB$&"+1]["
               END IF
            NEXT J
            LET C$(I)=C$(I)(1:POS(C$(I),"["))&CC$(1:LEN(CC$)-1)
         END IF
      END IF
      IF POS(C$(I),"[")=0 THEN
         LET VA_PUBLIC_COUNT=VA_PUBLIC_COUNT+1
         LET VARIABLE_PUBLIC$(VA_PUBLIC_COUNT)=LCASE$(TRIM$(C$(I)))             !'配列変数名登録
      END IF
   NEXT I
   IF POS(X$,"NUMERIC")>0 THEN
      IF POS(BEHIND$,"]")>0 THEN
         LET BEHIND$=""
         FOR I=1 TO K
            LET BEHIND$=BEHIND$&C$(I)
            IF POS(C$(I),"]")>0 THEN LET BEHIND$=BEHIND$&"={}," ELSE LET BEHIND$=BEHIND$&","   !'配列初期化設定
         NEXT I
         LET BEHIND$=BEHIND$(1:LEN(BEHIND$)-1)
      ELSE
         LET I=1
         DO
            IF BEHIND$(I:I)="," AND BEHIND$(I-3:I)<>"={}," THEN                 !'配列でないなら初期値0
               LET BEHIND$(I:I)="=0,"
               LET I=I+2
            END IF
            LET I=I+1
         LOOP UNTIL LEN(X$)<=I
         IF BEHIND$(LEN(BEHIND$)-2:LEN(BEHIND$))<>"={}" THEN
            LET BEHIND$=BEHIND$&"=0"
         END IF
      END IF
      CALL CPRINT(TYPE$&LCASE$(BEHIND$)&";")
   ELSE
      LET BEHIND$=TRANSFORM$(BEHIND$,"$","_string")
      IF POS(BEHIND$,"]")>0 THEN
         LET BEHIND$=TRANSFORM$(BEHIND$,"]",")={"&CHR$(34)&CHR$(34)&"}")        !'string配列 NULL初期化
         LET BEHIND$=TRANSFORM$(BEHIND$,")","]")
      END IF
      CALL CPRINT("string "&LCASE$(BEHIND$)&";")
   END IF
END SUB

SUB COMMAND_DIM(XX$)
   LOCAL I,N,J,K
   LET X$=XX$
   LET X$=TRANSRESERVED$(X$)
   LET BEHIND$=TRIM$(BEHINDSTRING$(X$,"DIM "))
   LET INI$=""
   CALL SPLIT2(BEHIND$,C$,N)
   LET CC$=""
   LET SS$=""
   MAT EXPRESSION$=NUL$
   MAT VA_DIM=ZER
   MAT VA_INIT$=NUL$
   MAT VARIABLE_DIM$=NUL$
   FOR I=1 TO N
      LET FL=0
      LET C$(I)=TRIM$(C$(I))
      IF POS(C$(I),"TO")>0 THEN                                                 !'下限指定時の処理
         IF POS(C$(I),",")=0 THEN                                               !'1次元配列時
            LET AA$=TRIM$(BETWEENSTRING$(C$(I),"(","TO"))
            LET BB$=TRIM$(BETWEENSTRING$(C$(I),"TO",")"))
            IF POS("0123456789",AA$(1:1))=0 OR POS("0123456789",BB$(1:1))=0 THEN
               CALL CPRINT(XX$&" // ******* 未対応です *******")
               EXIT SUB
            ELSE
               LET C$(I)=C$(I)(1:POS(C$(I),"("))&BB$&")"
            END IF
         ELSE                                                                   !'多次元配列時の処理
            LET J=POS(C$(I),"(")
            LET K=POS(C$(I),")")
            LET AA$=C$(I)(J+1:K-1)
            CALL TOKUN(AA$,C2$,K)
            FOR J=1 TO K
               LET AA$=TRIM$(FRONTSTRING$(C2$(J),"TO"))
               LET BB$=TRIM$(BEHINDSTRING$(C2$(J),"TO"))
               IF POS("0123456789",AA$(1:1))=0 OR POS("0123456789",BB$(1:1))=0 THEN
                  CALL CPRINT(XX$&" // ******* 未対応です *******")
                  EXIT SUB
               ELSE
                  LET CC$=CC$&BB$&","
               END IF
            NEXT J
            LET C$(I)=C$(I)(1:POS(C$(I),"("))&CC$(1:LEN(CC$)-1)&")"
            LET CC$=""
         END IF
      END IF
      LET AA$=""
      LET BB$=""
      IF POS(C$(I),"$")>0 THEN LET C$(I)=TRANSFORM$(C$(I),"$","_string")
      LET B$=TRANSARRAYNAME$(C$(I))
      IF TYPE$="complex <double> " THEN                                         !'強制キャストとりあえず解除
         LET INI$="static_cast<int>(abs("
         LET B$=TRANSFORM$(B$,INI$,"")
         LET B$=TRANSFORM$(B$,"))","")
         LET AA$="abs("
         LET BB$=")"
      ELSE
         LET INI$="static_cast<int>("
         LET B$=TRANSFORM$(B$,INI$,"")
         LET B$=TRANSFORM$(B$,")","")
      END IF
      LET C$(I)=TRANSFUNC$(B$)
      IF B$=C$(I) THEN
         LET K=POS(B$,"[")
         FOR J=K+1 TO LEN(B$)
            IF POS("abcdefghijklmnopqrstuvwxyz",LCASE$(B$(J:J)))>0 THEN LET FL=1   !'変数指定なら
         NEXT J
      ELSE
         LET FL=1
      END IF
      IF FL=1 THEN
         FOR J=1 TO LEN(C$(I))
            IF C$(I)(J:J)="[" THEN EXIT FOR
         NEXT J
         LET VARIABLE_DIM$(I)=TRIM$(LCASE$(C$(I)(1:J-1)))
      END IF
      LET K=POS(C$(I),"[")
      LET J=K
      LET SP=0
      DO
         LET J=J+1
         IF C$(I)(J:J)="(" THEN LET SP=SP+1
         IF C$(I)(J:J)=")" THEN LET SP=SP-1
         IF SP=0 AND C$(I)(J:J+1)="][" THEN
            LET VA_DIM(I)=VA_DIM(I)+1
            LET EXPRESSION$(I,VA_DIM(I))=LCASE$(C$(I)(K+1:J-1))
            LET C$(I)(J:J+1)="+1]["
            LET J=J+3
            LET K=J
         END IF
      LOOP UNTIL C$(I)(J:J)="]" OR LEN(C$(I))=J
      LET VA_DIM(I)=VA_DIM(I)+1
      LET EXPRESSION$(I,VA_DIM(I))=LCASE$(C$(I)(K+1:J-1))
      LET C$(I)(J:J)="+1]"
      IF FL=1 THEN
         LET C$(I)=TRANSFORM$(C$(I),"[","\"&INI$)
         LET C$(I)=TRANSFORM$(C$(I),"\","[")
         IF TYPE$="complex <double> " THEN
            LET C$(I)=TRANSFORM$(C$(I),"]","))\")
            LET C$(I)=TRANSFORM$(C$(I),"\","]")
         ELSE
            LET C$(I)=TRANSFORM$(C$(I),"]",")\")
            LET C$(I)=TRANSFORM$(C$(I),"\","]")
         END IF
      END IF
      IF POS(C$(I),"_string")>0 THEN
         IF FL=0 THEN
            LET CC$=CC$&C$(I)&"={"&CHR$(34)&CHR$(34)&"},"
         ELSE
            LET CC$=CC$&C$(I)&","
            LET VA_INIT$(I)=CHR$(34)&CHR$(34)&";"                               !'初期値"" NULL
         END IF
      ELSE
         IF FL=0 THEN
            LET SS$=SS$&C$(I)&"={},"
         ELSE
            LET SS$=SS$&C$(I)&","
            LET VA_INIT$(I)="0;"                                                !'初期値0
         END IF
      END IF
   NEXT I
   IF CC$<>"" THEN
      CALL CPRINT("string "&LCASE$(LEFT$(CC$,LEN(CC$)-1))&";")
   END IF
   IF SS$<>"" THEN
      CALL CPRINT(TYPE$&LCASE$(LEFT$(SS$,LEN(SS$)-1))&";")
   END IF
   FOR I=1 TO N                                                                 !'可変長配列の初期化
      IF VARIABLE_DIM$(I)<>"" THEN
         SELECT CASE VA_DIM(I)
         CASE 1
            CALL CPRINT("for (int i=0;i<="&AA$&EXPRESSION$(I,1)&BB$&";i++)")
            CALL CPRINT(VARIABLE_DIM$(I)&"[i]="&VA_INIT$(I))
         CASE 2
            CALL CPRINT("for (int i=0;i<="&AA$&EXPRESSION$(I,1)&BB$&";i++)")
            CALL CPRINT("for (int j=0;j<="&AA$&EXPRESSION$(I,2)&BB$&";j++)")
            CALL CPRINT(VARIABLE_DIM$(I)&"[i][j]="&VA_INIT$(I))
         CASE 3
            CALL CPRINT("for (int i=0;i<="&AA$&EXPRESSION$(I,1)&BB$&";i++)")
            CALL CPRINT("for (int j=0;j<="&AA$&EXPRESSION$(I,2)&BB$&";j++)")
            CALL CPRINT("for (int k=0;k<="&AA$&EXPRESSION$(I,3)&BB$&";k++)")
            CALL CPRINT(VARIABLE_DIM$(I)&"[i][j][k]="&VA_INIT$(I))
         CASE ELSE
         END SELECT
      END IF
   NEXT I
END SUB

SUB COMMAND_FOR(X$)
   LOCAL I
   LET X$=TRANSRESERVED$(X$)
   LET P$=TRIM$(LCASE$(BETWEENSTRING$(X$,"FOR","=")))                           !'制御変数名
   LET NUM$=TRIM$(BETWEENSTRING$(X$,"=","TO"))                                  !'初期値
   IF POS(X$,"STEP")>0 THEN
      LET ST$=BEHINDSTRING$(X$,"STEP")                                          !'増分
      LET ST$=TRANSARRAYNAME$(ST$)
      LET ST$=TRANSFUNC$(ST$)
      LET I=POS(ST$,"-")
      IF I>0 THEN
         LET SI$=">="
         LET SIGN$="-"
         LET ST$(I:I)=""
      ELSE
         LET SI$="<="
         LET SIGN$="+"
      END IF
      LET T$=TRIM$(BETWEENSTRING$(X$,"TO","STEP"))                              !'終了値
   ELSE
      LET T$=TRIM$(BEHINDSTRING$(X$,"TO"))
      LET ST$="1"
      LET SI$="<="
      LET SIGN$="+"
   END IF
   LET T$=TRANSARRAYNAME$(T$)                                                   !'配列変数名変換
   LET T$=TRANSFUNC$(T$)                                                        !'関数名変換
   LET NUM$=TRANSARRAYNAME$(NUM$)
   LET NUM$=TRANSFUNC$(NUM$)
   !!!CALL CPRINT("#pragma omp parallel for")                                   !!! openMP マルチスレッド (#include<omp.h>)
   CALL CPRINT("for("&P$&"="&LCASE$(NUM$)&";"&P$&SI$&LCASE$(T$)&";"&P$&SIGN$&"="&LCASE$(TRIM$(ST$))&") {")
END SUB

SUB COMMAND_NEXT(X$)
   CALL CPRINT("}")
END SUB

SUB COMMAND_FUNCTION(XX$)
   LOCAL N,K
   LET X$=XX$
   IF POS(X$,"!")>0 THEN LET X$=TRIM$(FRONTSTRING$(X$,"!"))
   IF POS(X$,"REM ")>0 THEN LET X$=TRIM$(FRONTSTRING$(X$,"REM "))
   LET X$=TRANSRESERVED$(X$)
   IF POS(X$,"(")=0 THEN                                                        !'仮引数なしの場合
      LET FNAME$=LCASE$(TRIM$(BEHINDSTRING$(X$,"FUNCTION")))                    !'関数名
      IF POS(FNAME$,"$")>0 THEN LET FNAME$=TRANSFORM$(FNAME$,"$","")
      IF PROTOTYPE<>0 THEN                                                      !'プロトタイプ宣言
         CALL CPRINT(TYPE$&FNAME$&"(void);")
         LET FC=FC+1
         LET FUNCNAME$(FC)=FNAME$                                               !'関数名登録
         EXIT SUB
      ELSE
         CALL CPRINT(TYPE$&FNAME$&"(void)")
         CALL CPRINT("{")
         EXIT SUB
      END IF
   END IF
   IF POS(X$,"()")>0 THEN LET X$=TRANSFORM$(X$,"()","[]")                       !'1次元配列 A() → A[]
   LET BEHIND$=BEHINDSTRING$(X$,"(")
   LET BEHIND$=BEHIND$(1:LEN(BEHIND$)-1)                                        !'仮引数列
   LET FNAME$=LCASE$(TRIM$(BETWEENSTRING$(X$,"FUNCTION","(")))
   IF PROTOTYPE<>0 THEN
      LET FC=FC+1
      LET FUNCNAME$(FC)=FNAME$
   END IF
   CALL TOKUN(BEHIND$,C$,K)
   LET AA$=""
   MAT VARIABLE_ARGUMENT$=NUL$
   LET VA_ARGUMENT_COUNT=0
   FOR N=1 TO K
      LET C$(N)=LCASE$(TRIM$(C$(N)))
      LET B$=C$(N)
      LET B$=TRANSFORM$(B$,"[]","")
      LET VA_ARGUMENT_COUNT=VA_ARGUMENT_COUNT+1
      LET VARIABLE_ARGUMENT$(VA_ARGUMENT_COUNT)=B$                              !'仮引数変数名登録
      IF POS(C$(N),"$")>0 THEN
         LET C$(N)="string "&TRANSFORM$(C$(N),"$","_string")
      ELSE
         LET C$(N)=TYPE$&C$(N)
      END IF
      LET AA$=AA$&C$(N)&","
   NEXT N
   LET AA$=AA$(1:LEN(AA$)-1)
   IF PROTOTYPE<>0 THEN LET AA$=AA$&");" ELSE LET AA$=AA$&")"
   CALL CPRINT(TYPE$&LCASE$(FNAME$&"("&AA$))
   IF PROTOTYPE=0 THEN                                                          !'プロトタイプ宣言でないなら使用変数名のスキャン、変数宣言
      CALL CPRINT("{")
      CALL SCAN$(A$,II)
   END IF
END SUB
 

Re: C++トランスレーター(試作・テスト版)

 投稿者:しばっち  投稿日:2014年 7月16日(水)20時52分15秒
  続き 3

SUB COMMAND_SUB(XX$)
   LOCAL N,K
   LET X$=XX$
   LET VA_CALL_COUNT=0
   IF POS(X$,"!")>0 THEN LET X$=TRIM$(FRONTSTRING$(X$,"!"))
   IF POS(X$,"REM ")>0 THEN LET X$=TRIM$(FRONTSTRING$(X$,"REM "))
   LET X$=TRANSRESERVED$(X$)
   IF POS(X$,"(")=0 THEN                                                        !'仮引数なしの場合
      LET BEHIND$=TRIM$(BEHINDSTRING$(X$,"SUB "))
      IF PROTOTYPE<>0 THEN
         CALL CPRINT("void "&LCASE$(BEHIND$)&"(void);")
         EXIT SUB
      ELSE
         CALL CPRINT("void "&LCASE$(BEHIND$)&"(void)")
         CALL CPRINT("{")
         EXIT SUB
      END IF
   END IF
   IF POS(X$,"()")>0 THEN LET X$=TRANSFORM$(X$,"()","[]")
   LET BEHIND$=BEHINDSTRING$(X$,"(")
   LET BEHIND$=BEHIND$(1:LEN(BEHIND$)-1)
   LET SNAME$=TRIM$(LCASE$(BETWEENSTRING$(X$,"SUB","(")))                       !'サブルーチン名
   CALL TOKUN(BEHIND$,C$,K)
   LET AA$=""
   MAT VARIABLE_ARGUMENT$=NUL$
   LET VA_ARGUMENT_COUNT=0
   FOR N=1 TO K
      LET C$(N)=LCASE$(TRIM$(C$(N)))
      LET B$=C$(N)
      LET B$=TRANSFORM$(B$,"[]","")
      LET VA_ARGUMENT_COUNT=VA_ARGUMENT_COUNT+1
      LET VARIABLE_ARGUMENT$(VA_ARGUMENT_COUNT)=B$
      IF POS(C$(N),"[")=0 THEN
         LET B$="&"
      ELSE
         LET B$=""
      END IF
      IF POS(C$(N),"$")>0 THEN
         LET C$(N)="string "&B$&TRANSFORM$(C$(N),"$","_string")
      ELSE
         LET C$(N)=TYPE$&B$&C$(N)
      END IF
      LET AA$=AA$&C$(N)&","
   NEXT N
   LET AA$=AA$(1:LEN(AA$)-1)
   IF PROTOTYPE<>0 THEN LET AA$=AA$&");" ELSE LET AA$=AA$&")"
   CALL CPRINT("void "&SNAME$&"("&AA$)
   IF PROTOTYPE=0 THEN
      CALL CPRINT("{")
      CALL SCAN$(A$,II)
   END IF
END SUB

SUB COMMAND_CASE(X$)
   LOCAL I,N
   LET X$=TRANSRESERVED$(X$)
   IF POS(X$,"SELECT CASE")>0 THEN
      LET SWITCH$=BEHINDSTRING$(X$,"SELECT CASE")                               !'SELECT CASE区の変数名
      LET SWITCH$=TRANSARRAYNAME$(SWITCH$)
      LET SWITCH$=LCASE$(TRANSFUNC$(SWITCH$))
      IF POS(SWITCH$,"$")>0 AND LITERAL(SWITCH$,"$")=0 THEN  LET SWITCH$=TRANSFORM$(SWITCH$,"$","_string")
      LET NUMCASE=0
      EXIT SUB
   END IF
   IF POS(X$,"CASE ELSE")>0 THEN
      CALL CPRINT("}")
      CALL CPRINT("else")
      CALL CPRINT("{")
   ELSEIF POS(X$,"CASE")>0 THEN                                                 !'CASE区の処理
      LET BEHIND$=TRIM$(BEHINDSTRING$(X$,"CASE"))
      LET AA$=""
      CALL TOKUN(BEHIND$,C$,N)
      FOR I=1 TO N
         IF POS(C$(I),"TO")>0 THEN                                              !'範囲指定時「CASE 1 TO 5」
            LET FRONT$=FRONTSTRING$(C$(I),"TO")
            LET BEHIND$=BEHINDSTRING$(C$(I),"TO")
            LET AA$=AA$&TRIM$(FRONT$)&"<="&SWITCH$&" && "&SWITCH$&" <= "&TRIM$(BEHIND$)&" || "
         ELSEIF POS(C$(I),"IS")>0 THEN                                          !' IS区指定時「CASE IS>5,IS<0」
            LET AA$=AA$&SWITCH$&TRIM$(BEHINDSTRING$(C$(I),"IS"))&" || "
         ELSE
            LET AA$=AA$&SWITCH$&"=="&C$(I)&" || "                               !'「CASE 1,3,5」
         END IF
      NEXT I
      LET AA$=AA$(1:LEN(AA$)-4)
      LET NUMCASE=NUMCASE+1
      IF NUMCASE=1 THEN                                                         !'最初のCASE区
         CALL CPRINT("if("&AA$&") { ")
      ELSE                                                                      !'2番目以降のCASE区
         CALL CPRINT("}")
         CALL CPRINT("else if("&AA$&") { ")
      END IF
   END IF
END SUB

SUB COMMAND_DO(XX$)
   LET X$=XX$
   LET X$=TRANSRESERVED$(X$)
   LET X$=TRANSCO$(X$)
   LET X$=TRANSARRAYNAME$(X$)
   LET X$=TRANSFUNC$(X$)
   IF POS(X$,"DO WHILE")>0 THEN                                                 !'DO WHILE処理
      LET BEHIND$=BEHINDSTRING$(X$,"WHILE")
      CALL CPRINT("while ("&LLCASE$(TRIM$(BEHIND$))&") {")
      EXIT SUB
   END IF
   IF POS(X$,"DO UNTIL")>0 THEN
      LET BEHIND$=BEHINDSTRING$(X$,"UNTIL")
      CALL CPRINT("do {")                                                       !'DO UNTIL処理
      CALL CPRINT( "if ("&LLCASE$(TRIM$(BEHIND$))&") break;")
      EXIT SUB
   END IF
   CALL CPRINT("do {")
END SUB

SUB COMMAND_LOOP(XX$)
   LET X$=XX$
   LET X$=TRANSRESERVED$(X$)
   LET X$=TRANSCO$(X$)
   LET X$=TRANSARRAYNAME$(X$)
   LET X$=TRANSFUNC$(X$)
   IF POS(X$,"LOOP UNTIL")>0 THEN                                               !'LOOP UNTIL処理
      LET BEHIND$=TRIM$(BEHINDSTRING$(X$,"UNTIL"))
      CALL CPRINT("if ("&LLCASE$(BEHIND$)&") break;")
   END IF
   IF POS(X$,"LOOP WHILE")>0 THEN                                               !'LOOP WHILE処理
      LET BEHIND$=TRIM$(BEHINDSTRING$(X$,"WHILE"))
      CALL CPRINT("} while ("&LLCASE$(BEHIND$)&");")
      EXIT SUB
   END IF
   CALL CPRINT("}")
END SUB

SUB COMMAND_LET(XX$)
   LOCAL I,N,K
   LET N=0
   LET K=0
   LET X$=XX$
   LET X$=TRANSRESERVED$(X$)
   FOR I=1 TO LEN(X$)
      IF X$(I:I)="=" THEN LET K=K+1
   NEXT I
   IF K=1 THEN LET BETWEEN$=LCASE$(TRIM$(BETWEENSTRING$(X$,"LET","=")))
   LET AA$=""
   IF K=1 AND BETWEEN$=FNAME$ THEN                                              !'FUNCTION内で関数名と同じ場合
      LET AA$="return "
      LET X$=BEHINDSTRING$(X$,"=")
   END IF
   LET X$=TRANSFORM$(X$,"LET ","")
   IF K=1 AND POS(BETWEEN$,",")=0 THEN
      LET X$=TRANSARRAYNAME$(X$)
      LET X$=LLCASE$(TRANSFUNC$(X$))
      CALL CPRINT(AA$&X$&";")
      EXIT SUB
   END IF
   IF K=1 THEN                                                                  !'複数、多次元配列指定「LET A,B,C(1,1)=5」
      LET X$=BEHINDSTRING$(X$,"=")
      CALL SPLIT2(BETWEEN$,C$,N)
      FOR I=1 TO N
         IF POS(C$(I),"(")>0 THEN LET C$(I)=TRANSARRAYNAME$(C$(I))
         LET X$=TRANSARRAYNAME$(X$)
         LET X$=LLCASE$(TRANSFUNC$(X$))
         CALL CPRINT(C$(I)&"="&X$&";")
      NEXT I
   ELSE                                                                         !'「LET A=1,B=2,C=3」
      CALL SPLIT2(X$,C$,N)
      FOR I=1 TO N
         LET BEHIND$=BEHINDSTRING$(C$(I),"=")
         LET BEHIND$=TRANSARRAYNAME$(BEHIND$)
         LET BEHIND$=LLCASE$(TRANSFUNC$(BEHIND$))
         LET FRONT$=FRONTSTRING$(C$(I),"=")
         IF POS(FRONT$,"(")>0 THEN LET FRONT$=TRANSARRAYNAME$(FRONT$)
         LET FRONT$=TRANSFORM$(FRONT$,"$","_string")
         CALL CPRINT(LCASE$(FRONT$)&"="&BEHIND$&";")
      NEXT I
   END IF
END SUB

SUB COMMAND_INPUT(XX$)
   LOCAL I,J,K
   LET X$=XX$
   LET X$=TRANSRESERVED$(X$)
   LET NUM$=""
   LET X$=TRANSFORM$(X$,"$","_string")
   IF POS(X$,"#")>0 THEN                                                        !'ファイル読込指定時
      LET NUM$=TRIM$(BETWEENSTRING$(X$,"#",":"))                                !'経路番号
      LET X$=TRANSFORM$(X$,"#"&NUM$&":","")
   END IF
   LET K=POS(X$,"PROMPT")
   IF K>0 THEN                                                                  !'INPUT PROMPT文「INPUT PROMPT "男:1 女:2":N」
      IF POS(X$,CHR$(34))>0 THEN                                                !'文字定数取出
         FOR I=K+6 TO LEN(X$)
            IF X$(I:I)=CHR$(34) THEN
               FOR J=I+1 TO LEN(X$)
                  IF X$(J:J)=CHR$(34) THEN
                     LET S$=TRIM$(X$(I:J))
                     LET I=J+1
                     EXIT FOR
                  END IF
               NEXT J
            END IF
         NEXT I
         FOR I=J+1 TO LEN(X$)
            IF X$(I:I)=":" THEN EXIT FOR
         NEXT I
         LET ST$=TRIM$(X$(I+1:LEN(X$)))
      ELSE
         LET S$=TRIM$(BETWEENSTRING$(X$,"PROMPT",":"))                          !'出力文(文字定数取出)
         LET ST$=TRIM$(BEHINDSTRING$(X$,":"))                                   !'変数列取出
      END IF
      LET ST$=TRANSFORM$(ST$,","," >> ")
      LET ST$=LCASE$(TRANSARRAYNAME$(ST$))
      CALL CPRINT("cout << "&S$&";")
      !' CALL CPRINT("cerr << "&S$&";")
      IF POS(X$,"LINE INPUT")>0 THEN
         CALL CPRINT("getline(cin,"&ST$&");")                                   !'「LINE INPUT PROMPT "A=":A$」
      ELSE
         CALL CPRINT("cin >> "&ST$&";")                                         !'「INPUT PROMPT "A,B,C=":A$,B$,C$」
      END IF
   ELSE
      IF POS(X$,"LINE INPUT")>0 THEN
         IF NUM$<>"" THEN
            LET X$=TRANSARRAYNAME$(X$)
            LET X$=TRANSFORM$(X$,"LINE INPUT","getline(fs"&NUM$&",")            !'「LINE INPUT #1:A$」
            CALL CPRINT(LCASE$(X$)&");")
         ELSE
            LET X$=TRANSFORM$(X$,"LINE INPUT","getline(cin,")                   !'「LINE INPUT A$」
            CALL CPRINT(LCASE$(X$)&");")
         END IF
      ELSE
         IF NUM$<>"" THEN
            LET X$=TRANSFORM$(X$,"INPUT","fs"&NUM$&" >> ")                      !'「INPUT #1:A,B,C」
         ELSE
            LET X$=TRANSFORM$(X$,"INPUT","cin >> ")                             !'「INPUT A,B,C」
            LET X$=TRANSFORM$(X$,","," >> ")
            CALL CPRINT(LCASE$(X$)&";")
         END IF
      END IF
   END IF
END SUB

SUB COMMAND_PRINT(XX$)
   LET X$=XX$
   LET X$=TRANSRESERVED$(X$)
   LOCAL I,J,N
   LET NUM$=""
   IF TRIM$(BEHINDSTRING$(X$,"PRINT"))="" THEN                                  !'改行指定
      CALL CPRINT("cout << endl;")
      !'CALL CPRINT("cout << '\n';")
      EXIT SUB
   END IF
   LET ST$=""
   IF RIGHT$(X$,1)<>";" AND RIGHT$(X$,1)<>"," THEN                              !'文末に";"又は","がないなら改行指定
      LET ST$=" << endl"
      !'LET ST$=" << '\n'"
   ELSE
      IF RIGHT$(X$,1)=";" THEN LET X$(LEN(X$):LEN(X$))=" <<"&CHR$(39)&" "&CHR$(39)   !'スペース文字に置き換え
      IF RIGHT$(X$,1)="," THEN LET X$(LEN(X$):LEN(X$))=" <<"&CHR$(39)&REPEAT$(CHR$(32),20)&CHR$(39)
   END IF
   LET SP=0
   LET I=0
   DO
      LET I=I+1
      IF X$(I:I)=CHR$(34) THEN                                                  !'文字定数内の「,」「;」を除く「"A,B=";A,B」
         FOR J=I+1 TO LEN(X$)
            IF X$(J:J)=CHR$(34) THEN EXIT FOR
         NEXT J
         LET I=J
      END IF
      IF X$(I:I)="(" OR X$(I:I)="[" THEN                                        !'多次元配列変数内の「,」を除く「A,B;C(1,1),D」
         LET SP=SP+1
         FOR J=I+1 TO LEN(X$)
            IF X$(J:J)="(" OR X$(J:J)="[" THEN LET SP=SP+1
            IF X$(J:J)=")" OR X$(J:J)="]" THEN LET SP=SP-1
            IF SP=0 THEN EXIT FOR
         NEXT J
         LET I=J
      END IF
      IF X$(I:I)="," THEN LET X$(I:I)=" <<"&CHR$(39)&REPEAT$(CHR$(32),20)&CHR$(39)&"<< "   !'「,」「;」を" "スペースに置換
      IF X$(I:I)=";" THEN LET X$(I:I)=" <<"&CHR$(39)&" "&CHR$(39)&"<< "
   LOOP UNTIL LEN(X$)<=I
   IF POS(X$,"#")>0 THEN
      LET NUM$=TRIM$(BETWEENSTRING$(X$,"#",":"))                                !'経路番号
      LET X$=TRANSFORM$(X$,"#"&NUM$&":","")
      IF TRIM$(X$)="PRINT" THEN
         CALL CPRINT("fs"&NUM$&" << endl;")
         EXIT SUB
      END IF
   END IF
   IF POS(X$,CHR$(34))>0 THEN                                                   !'文字定数があるなら
      LET BEHIND$=TRIM$(BEHINDSTRING$(X$,"PRINT"))
      CALL SPLIT(BEHIND$,C$,N)                                                  !'PRINT文以降を分割
      FOR I=1 TO N STEP 2
         IF C$(I)<>"" THEN
            LET C$(I)=TRANSARRAYNAME$(C$(I))
            LET C$(I)=LCASE$(TRANSFUNC$(C$(I)))
         END IF
      NEXT I
      IF NUM$<>"" THEN LET B$="fs"&NUM$&" << "&SETPREC$ ELSE LET B$="cout << "&SETPREC$
      FOR I=1 TO N
         LET B$=B$&TRIM$(C$(I))
      NEXT I
      LET B$=TRANSFORM$(B$,CHR$(39),CHR$(34))
      CALL CPRINT(B$&ST$&";")
   ELSE
      LET X$=TRANSARRAYNAME$(X$)
      LET X$=TRANSFUNC$(X$)
      IF NUM$<>"" THEN
         LET X$=TRANSFORM$(X$,"PRINT","fs"&NUM$&" <<"&SETPREC$)
      ELSE
         LET X$=TRANSFORM$(X$,"PRINT","cout <<"&SETPREC$)
      END IF
      LET X$=TRANSFORM$(X$,CHR$(39),CHR$(34))
      CALL CPRINT(LCASE$(TRIM$(X$))&ST$&";")
   END IF
END SUB

SUB COMMAND_IF(XX$)
   LOCAL FRONT$,BEHIND$,I
   LET SW=0
   LET X$=XX$
   LET X$=TRANSRESERVED$(X$)
   IF POS(X$,CHR$(34))>0 THEN                                                   !'「IF A$="THEN" THEN」
      FOR I=1 TO LEN(X$)
         IF X$(I:I)=CHR$(34) THEN LET SW=1-SW
         IF SW=0 AND X$(I:I+3)="THEN" THEN
            LET FRONT$=TRIM$(X$(1:I-1))
            LET BEHIND$=TRIM$(X$(I+4:LEN(X$)))
            EXIT FOR
         END IF
      NEXT I
   ELSE
      LET FRONT$=TRIM$(FRONTSTRING$(X$,"THEN"))
      LET BEHIND$=TRIM$(BEHINDSTRING$(X$,"THEN"))
   END IF
   LET FRONT$=TRANSCO$(FRONT$)
   LET FRONT$=TRANSARRAYNAME$(FRONT$)
   LET FRONT$=TRANSFUNC$(FRONT$)
   IF POS(FRONT$,"ELSEIF")>0 THEN
      CALL CPRINT("}")
      LET FRONT$=TRANSFORM$(FRONT$,"ELSEIF ","else if (")
      LET FRONT$=TRANSFORM$(FRONT$,"ELSEIF(","else if ((")
   ELSE
      LET FRONT$=TRANSFORM$(FRONT$,"IF ","if (")
      LET FRONT$=TRANSFORM$(FRONT$,"IF(","if ((")
   END IF
   CALL CPRINT(LLCASE$(FRONT$)&")")
   CALL CPRINT("{")
   IF BEHIND$<>"" THEN                                                          !'THEN区以降
      IF POS(BEHIND$,"ELSE")>0 THEN                                             !'ELSE区があるなら
         LET FRONT$=BETWEENSTRING$(X$,"THEN","ELSE")                            !'THEN区とELSE区の間
         LET BEHIND$=BEHINDSTRING$(X$,"ELSE")                                   !'ELSE区以降
         CALL MAIN(FRONT$)                                                      !'再帰処理
         CALL CPRINT("}")
         CALL CPRINT("else")
         CALL CPRINT("{")
         CALL MAIN(BEHIND$)
         CALL CPRINT("}")
      ELSE
         CALL MAIN(BEHIND$)
         CALL CPRINT("}")
      END IF
   END IF
END SUB

SUB COMMAND_ELSE(X$)
   IF POS(X$,"ELSE")>0 THEN
      CALL CPRINT("}")
      CALL CPRINT("else")
      CALL CPRINT("{")
   END IF
END SUB

SUB COMMAND_OPEN(X$)
   LET X$=TRANSRESERVED$(X$)
   LET NAME$=TRIM$(BEHINDSTRING$(X$,"NAME"))
   IF POS(X$,",ACCESS")>0 THEN
      LET NAME$=TRIM$(BETWEENSTRING$(X$,"NAME",",ACCESS"))
      IF POS(X$,"INPUT")>0 THEN
         LET IO$="if"                                                           !'読み込みストリーム
         !' OPENMODE$=",ios::in"
      END IF
      IF POS(X$,"OUTPUT")>0 THEN
         LET IO$="of"                                                           !'書き込みストリーム
         !' OPENMODE$=",ios::out"
      END IF
      IF POS(X$,"OUTIN")>0 THEN
         LET IO$="f"                                                            !'読み書きストリーム
         LET OPENMODE$=",ios::in | ios::out"                                    !'●その他
      END IF
   ELSE
      LET IO$="f"
      LET OPENMODE$=",ios::in | ios::out"                                       !'●その他
   END IF
   LET NUM$=TRIM$(BETWEENSTRING$(X$,"#",":"))                                   !'経路番号
   IF POS(NAME$,"$")>0 THEN LET NAME$=TRANSFORM$(NAME$,"$","_string.c_str()")
   CALL CPRINT(IO$&"stream fs"&NUM$&"("&LLCASE$(NAME$)&OPENMODE$&");")
   !' CALL CPRINT(IO$&"stream fs"&NUM$&";")
   !' CALL CPRINT("fs&NUM$&".open("&LLCASE$(NAME$)&OPENMODE$&");")
   CALL CPRINT("if (fs"&NUM$&".fail()){")                                       !'エラー処理
   CALL CPRINT("cout << "&CHR$(34)&"ファイルをオープンできません"&CHR$(34)&" << endl;")
   CALL CPRINT("exit(1);")
   CALL CPRINT("}")
END SUB

SUB COMMAND_CLOSE(X$)
   CALL CPRINT("fs"&NUM$&".close();")
   LET NUM$=""
END SUB

SUB COMMAND_EXIT(X$)
   IF POS(X$,"EXIT SUB")>0 THEN
      CALL CPRINT("return;")
      EXIT SUB
   END IF
   IF POS(X$,"EXIT FUNCTION")>0 THEN
   !' CALL CPRINT("return;")
      EXIT SUB
   END IF
   IF POS(X$,"EXIT DO")>0 OR POS(X$,"EXIT FOR")>0 THEN
      CALL CPRINT("break;")
      EXIT SUB
   END IF
END SUB

SUB COMMAND_END(X$)
!' IF X$="END" THEN
!'    CALL CPRINT("cout << "&CHR$(34)&"Hit Enter Key"&CHR$(34)&" << endl;")
!'    CALL CPRINT("cin.get();")                                                 !'エンターキー入力待ち(ウィンドゥが閉じてしまうのを防ぐ)
!' END IF
   CALL CPRINT("}")
END SUB

SUB COMMAND_RANDOMIZE(X$)
   CALL CPRINT("srand((unsigned)time(NULL));")
END SUB

SUB COMMAND_CALL(XX$)
   LET X$=XX$
   LET X$=TRANSRESERVED$(X$)
   LOCAL I,N,J,K
   LET AA$=""
   LET X$=TRIM$(TRANSFORM$(X$,"CALL",""))
   IF LITERAL(X$,"$")=0 THEN LET X$=TRANSFORM$(X$,"$","_string")
   IF POS(X$,"(")=0 THEN                                                        !'引数なし
      CALL CPRINT(LCASE$(X$)&"();")
      EXIT SUB
   END IF
   LET FRONT$=TRIM$(FRONTSTRING$(X$,"("))
   LET BEHIND$=TRIM$(BEHINDSTRING$(X$,"("))
   CALL SPLIT2(BEHIND$(1:LEN(BEHIND$)-1),C$,N)
   FOR I=1 TO N
      IF C$(I)<>"" THEN
         LET FL=0
         FOR J=1 TO LEN(C$(I))
            IF POS("+-*/^",C$(I)(J:J))>0 OR POS("0123456789",C$(I)(1:1))>0 THEN LET FL=1   !'引数が数式、定数なら
         NEXT J
         LET C$(I)=TRANSARRAYNAME$(C$(I))
         IF POS(C$(I),CHR$(34))=0 AND POS(C$(I),"&")=0 THEN
            LET BB$=C$(I)
            LET C$(I)=TRANSFUNC$(C$(I))                                         !'関数名変換で変換されたか
            IF BB$<>C$(I) OR FL=1 THEN                                          !'引数が関数なら
               LET VA_CALL_COUNT=VA_CALL_COUNT+1
               LET BB$=LCASE$(C$(I))
               LET C$(I)="dummy"&STR$(VA_CALL_COUNT)                            !'ダミー変数宣言
               CALL CPRINT(TYPE$&C$(I)&"="&BB$&";")
            END IF
         ELSE                                                                   !'文字定数指定なら
            LET BB$=C$(I)
            LET VA_CALL_COUNT=VA_CALL_COUNT+1
            LET C$(I)="dummy"&STR$(VA_CALL_COUNT)                               !'ダミー変数宣言
            CALL CPRINT("string "&C$(I)&"="&LLCASE$(BB$)&";")
         END IF
      END IF
      LET AA$=AA$&C$(I)&","
   NEXT I
   CALL CPRINT(LLCASE$(FRONT$&"("&AA$(1:LEN(AA$)-1)&");"))
END SUB

SUB COMMAND_REM(XX$)
   LOCAL I,FRONT$,BEHIND$
   LET X$=XX$
   LET I=POS(X$,"!")
   LET X$=TRANSFORM$(X$,"!","//")
   IF I=0 THEN
      LET I=POS(X$,"REM ")
      LET X$=TRANSFORM$(X$,"REM ","//")
   END IF
   IF I>1 THEN
      LET FRONT$=TRIM$(X$(1:I-1))                                               !'注釈文字以前
      !'  LET BEHIND$=TRIM$(X$(I:LEN(X$)))                                      !'注釈文字以降
      CALL MAIN(FRONT$)                                                         !'再帰処理
      !'  CALL CPRINT(BEHIND$)
   END IF
END SUB
 

Re: C++トランスレーター(試作・テスト版)

 投稿者:しばっち  投稿日:2014年 7月16日(水)20時52分56秒
  > No.3433[元記事へ]

続き 4

SUB COMMAND_STOP(X$)
   CALL CPRINT("exit(0);")
   !'CALL CPRINT("exit(1);")
END SUB

SUB COMMAND_SWAP(X$)
   LET X$=TRANSRESERVED$(X$)
   LET BEHIND$=TRIM$(BEHINDSTRING$(X$,"SWAP "))
   LET BEHIND$=TRANSARRAYNAME$(BEHIND$)
   LET BEHIND$=TRANSFORM$(BEHIND$,"$","_string")
   CALL CPRINT("swap("&LCASE$(BEHIND$)&");")
   !'CALL CPRINT("cswap("&LCASE$(BEHIND$)&");")
END SUB

SUB SCAN$(A$(),NN)                                                              !'使用変数名の探索、宣言
   LOCAL I,J,K,N
   LET J=NN
   LET AA$=""
   LET BB$=""
   LET B$=""
   MAT VARIABLE$=NUL$
   MAT VARIABLE_FOR$=NUL$
   MAT VARIABLE_STRING$=NUL$
   MAT VARIABLE_ARRAY$=NUL$
   LET VA_COUNT=0
   LET VA_FOR_COUNT=0
   LET VA_STRING_COUNT=0
   LET VA_ARRAY_COUNT=0
   DO
      LET X$=A$(J)
      IF POS(X$,"!")>0 AND LITERAL(X$,"!")=0 THEN LET X$=FRONTSTRING$(X$,"!")
      IF POS(X$,"REM ")>0 AND LITERAL(X$,"REM ")=0 THEN LET X$=FRONTSTRING$(X$,"REM ")
      IF POS(X$,"DIM ")>0 AND LITERAL(X$,"DIM ")=0 THEN                         !'DIM文より探索
         LET X$=TRANSRESERVED$(X$)
         LET BEHIND$=TRIM$(BEHINDSTRING$(X$,"DIM "))
         CALL SPLIT2(BEHIND$,C$,N)
         FOR K=1 TO N
            LET AA$=TRIM$(LCASE$(FRONTSTRING$(C$(K),"(")))
            LET SAME=0
            FOR I=1 TO VA_ARRAY_COUNT
               IF AA$=VARIABLE_ARRAY$(I) THEN LET SAME=1
            NEXT I
            IF SAME=0 THEN
               LET VA_ARRAY_COUNT=VA_ARRAY_COUNT+1
               LET VARIABLE_ARRAY$(VA_ARRAY_COUNT)=AA$
            END IF
         NEXT K
      END IF
      LET X$=A$(J)
      LET SAME=0
      IF POS(X$,"!")>0 AND LITERAL(X$,"!")=0 THEN LET X$=FRONTSTRING$(X$,"!")
      IF POS(X$,"REM ")>0 AND LITERAL(X$,"REM ")=0 THEN LET X$=FRONTSTRING$(X$,"REM ")
      IF POS(X$,"LET ")>0 AND LITERAL(X$,"LET ")=0 THEN                         !'LET文より探索
         LET K=0
         FOR I=1 TO LEN(X$)
            IF X$(I:I)="=" THEN LET K=K+1
         NEXT I
         IF K=1 THEN
            LET X$=TRIM$(BETWEENSTRING$(X$,"LET ","="))
         ELSE
            LET X$=TRANSFORM$(X$,"LET ","")
         END IF
         IF POS(X$,",")=0 THEN                                                  !'単指定「LET A=5」
            LET X$=LCASE$(TRANSRESERVED$(X$))
            IF POS(X$,"(")=0 THEN
               FOR I=1 TO MAXSIZE
                  IF VARIABLE_STRING$(I)=X$ OR VARIABLE$(I)=X$ OR VARIABLE_FOR$(I)=X$ OR FUNCNAME$(I)=X$ OR VARIABLE_PUBLIC$(I)=X$ OR VARIABLE_ARGUMENT$(I)=X$ OR VARIABLE_ARRAY$(I)=X$ THEN
                     LET SAME=1
                     EXIT FOR
                  END IF
               NEXT  I
               IF SAME=0 THEN
                  IF POS(X$,"$")>0 THEN
                     LET VA_STRING_COUNT=VA_STRING_COUNT+1
                     LET VARIABLE_STRING$(VA_STRING_COUNT)=X$
                  ELSE
                     LET VA_COUNT=VA_COUNT+1
                     LET VARIABLE$(VA_COUNT)=X$
                  END IF
               END IF
            ELSE
               LET X$=FRONTSTRING$(X$,"(")
               FOR I=1 TO VA_ARRAY_COUNT
                  IF VARIABLE_ARRAY$(I)=X$ THEN
                     LET SAME=1
                     EXIT FOR
                  END IF
               NEXT I
               IF SAME=0 THEN
                  LET VA_ARRAY_COUNT=VA_ARRAY_COUNT+1
                  LET VARIABLE_ARRAY$(VA_ARRAY_COUNT)=X$                        !'配列変数名登録
               END IF
            END IF
         ELSE                                                                   !'複数指定「LET A(1,1),B,C=5」
            CALL SPLIT2(X$,C$,N)
            FOR K=1 TO N
               IF POS(C$(K),"=")>0 THEN LET C$(K)=TRIM$(FRONTSTRING$(C$(K),"="))!'複数指定「LET A=1,B=2,C=3」
               IF POS(C$(K),"(")=0 THEN
                  FOR I=1 TO MAXSIZE
                     IF VARIABLE_STRING$(I)=C$(K) OR VARIABLE$(I)=C$(K) OR VARIABLE_FOR$(I)=C$(K) OR FUNCNAME$(I)=C$(K) OR VARIABLE_PUBLIC$(I)=C$(K) OR VARIABLE_ARGUMENT$(I)=C$(K) OR VARIABLE_ARRAY$(I)=X$ THEN
                        LET SAME=1
                        EXIT FOR
                     END IF
                  NEXT  I
                  IF SAME=0 THEN
                     IF POS(C$(K),"$")>0 THEN
                        LET VA_STRING_COUNT=VA_STRING_COUNT+1
                        LET VARIABLE_STRING$(VA_STRING_COUNT)=C$(K)
                     ELSE
                        LET VA_COUNT=VA_COUNT+1
                        LET VARIABLE$(VA_COUNT)=C$(K)
                     END IF
                  END IF
               ELSE
                  LET C$(K)=FRONTSTRING$(C$(K),"(")
                  FOR I=1 TO VA_ARRAY_COUNT
                     IF VARIABLE_ARRAY$(I)=C$(K) THEN
                        LET SAME=1
                        EXIT FOR
                     END IF
                  NEXT I
                  IF SAME=0 THEN
                     LET VA_ARRAY_COUNT=VA_ARRAY_COUNT+1
                     LET VARIABLE_ARRAY$(VA_ARRAY_COUNT)=C$(K)                  !'配列変数名登録
                  END IF
               END IF
            NEXT K
         END IF
      END IF
      LET SAME=0
      LET X$=A$(J)
      IF POS(X$,"!")>0 AND LITERAL(X$,"!")=0 THEN LET X$=FRONTSTRING$(X$,"!")
      IF POS(X$,"REM ")>0 AND LITERAL(X$,"REM ")=0 THEN LET X$=FRONTSTRING$(X$,"REM ")
      IF POS(X$,"FOR ")>0 AND LITERAL(X$,"FOR ")=0 THEN                         !'FOR文より探索
         LET X$=TRIM$(BETWEENSTRING$(X$,"FOR ","="))
         LET X$=LCASE$(TRANSRESERVED$(X$))
         FOR I=1 TO MAXSIZE
            IF VARIABLE_FOR$(I)=X$ OR VARIABLE_STRING$(I)=X$ OR VARIABLE$(I)=X$ OR VARIABLE_PUBLIC$(I)=X$ OR VARIABLE_ARGUMENT$(I)=X$ THEN
               LET SAME=1
               EXIT FOR
            END IF
         NEXT I
         IF SAME=0 THEN
            LET VA_FOR_COUNT=VA_FOR_COUNT+1
            LET VARIABLE_FOR$(VA_FOR_COUNT)=X$
         END IF
      END IF
      LET X$=A$(J)
      IF POS(X$,"!")>0 AND LITERAL(X$,"!")=0 THEN LET X$=FRONTSTRING$(X$,"!")
      IF POS(X$,"REM ")>0 AND LITERAL(X$,"REM ")=0 THEN LET X$=FRONTSTRING$(X$,"REM ")
      IF POS(X$,"INPUT ")>0 AND LITERAL(X$,"INPUT ")=0 THEN                     !'INPUT文より探索
         IF POS(X$,":")>0 THEN
            LET SW=0
            FOR I=1 TO LEN(X$)
               IF X$(I:I)=CHR$(34) THEN LET SW=1-SW
               IF SW=0 AND X$(I:I)=":" THEN EXIT FOR
            NEXT I
            LET X$=X$(I+1:LEN(X$))
         ELSE
            LET X$=TRIM$(BEHINDSTRING$(X$,"INPUT"))
         END IF
         LET X$=LCASE$(TRANSRESERVED$(X$))
         LET SP=0
         FOR I=1 TO LEN(X$)
            IF X$(I:I)="(" THEN
               LET SP=SP+1
               FOR K=I+1 TO LEN(X$)
                  IF X$(K:K)="(" THEN LET SP=SP+1
                  IF SP>0 AND X$(K:K)="," THEN LET X$(K:K)="\"                  !'多次元配列内の「,」置換
                  IF X$(K:K)=")" THEN LET SP=SP-1
                  IF SP=0 THEN EXIT FOR
               NEXT K
            END IF
         NEXT I
         CALL TOKUN(X$,C$,N)                                                    !'区切り「,」で分割
         FOR K=1 TO N
            IF C$(K)<>"" THEN
               LET SAME=0
               IF POS(C$(K),"(")=0 THEN
                  FOR I=1 TO MAXSIZE
                     IF VARIABLE_FOR$(I)=C$(K) OR VARIABLE_STRING$(I)=C$(K) OR VARIABLE$(I)=C$(K) OR VARIABLE_PUBLIC$(I)=C$(K) OR VARIABLE_ARGUMENT$(I)=C$(K) OR VARIABLE_ARRAY$(I)=C$(K) THEN
                        LET SAME=1
                        EXIT FOR
                     END IF
                  NEXT  I
                  IF SAME=0 THEN
                     IF POS(C$(K),"$")>0 THEN
                        LET VA_STRING_COUNT=VA_STRING_COUNT+1
                        LET VARIABLE_STRING$(VA_STRING_COUNT)=C$(K)
                     ELSE
                        LET VA_COUNT=VA_COUNT+1
                        LET VARIABLE$(VA_COUNT)=C$(K)
                     END IF
                  END IF
               END IF
            END IF
         NEXT K
      END IF
      LET X$=A$(J)
      IF POS(X$,"!")>0 AND LITERAL(X$,"!")=0 THEN LET X$=FRONTSTRING$(X$,"!")
      IF POS(X$,"REM ")>0 AND LITERAL(X$,"REM ")=0 THEN LET X$=FRONTSTRING$(X$,"REM ")
      IF POS(X$,"CALL ")>0 AND POS(X$,"(")>0 AND LITERAL(X$,"CALL ")=0 THEN     !'CALL文より探索
         LET SP=0
         FOR I=1 TO LEN(X$)
            IF X$(I:I)="(" THEN
               LET K=I
               FOR N=K+1 TO LEN(X$)
                  IF X$(N:N)="(" THEN LET SP=SP+1
                  IF X$(N:N)=")" THEN LET SP=SP-1
                  IF SP>0 AND X$(N:N)="," THEN LET X$(N:N)="\"
                  IF SP=0 THEN EXIT FOR
               NEXT N
               LET BETWEEN$=X$(K+1:N-1)
               EXIT FOR
            END IF
         NEXT I
         CALL TOKUN(BETWEEN$,C$,N)
         FOR K=1 TO N
            IF C$(K)<>"" THEN
               IF POS(C$(K),"(")>0 THEN LET C$(K)=TRIM$(FRONTSTRING$(C$(K),"("))
               LET SAME=0
               LET FL=0
               IF C$(K)<>"" AND POS("0123456789",C$(K)(1:1))=0 THEN
                  FOR I=1 TO LEN(C$(K))
                     IF POS("+-*/^&"&CHR$(34),C$(K)(I:I))>0 THEN  LET FL=1
                  NEXT I
                  IF FL=0 THEN
                     LET B$=TRANSFUNC$(C$(K))
                     IF B$<>C$(K) THEN LET FL=1
                  END IF
                  IF FL=0 THEN
                     LET C$(K)=LCASE$(TRANSRESERVED$(C$(K)))
                     FOR I=1 TO MAXSIZE
                        IF VARIABLE_FOR$(I)=C$(K) OR VARIABLE_STRING$(I)=C$(K) OR VARIABLE$(I)=C$(K) OR VARIABLE_PUBLIC$(I)=C$(K) OR VARIABLE_ARRAY$(I)=C$(K) OR VARIABLE_ARGUMENT$(I)=C$(K) THEN
                           LET SAME=1
                           EXIT FOR
                        END IF
                     NEXT  I
                     IF SAME=0 THEN
                        IF POS(C$(K),"$")>0 THEN
                           LET VA_STRING_COUNT=VA_STRING_COUNT+1
                           LET VARIABLE_STRING$(VA_STRING_COUNT)=C$(K)
                        ELSE
                           LET VA_COUNT=VA_COUNT+1
                           LET VARIABLE$(VA_COUNT)=C$(K)
                        END IF
                     END IF
                  END IF
               END IF
            END IF
         NEXT K
      END IF
      LET J=J+1
   LOOP UNTIL POS(X$,"END")>0 AND POS(X$,"END IF")=0 AND POS(X$,"END SELECT")=0 AND POS(X$,"END WHEN")=0 AND LITERAL(X$,"END")=0
   LET B$=""
   LET AA$=""
   LET BB$=""
   IF VA_COUNT>0 THEN                                                           !'変数名宣言、初期化
      LET B$=TYPE$
      FOR I=1 TO VA_COUNT-1
         IF VARIABLE$(I)<>"" THEN  LET B$=B$&LCASE$(VARIABLE$(I))&"=0,"
      NEXT I
      IF VARIABLE$(VA_COUNT)<>"" THEN LET B$=B$&LCASE$(VARIABLE$(VA_COUNT))&"=0;" ELSE LET B$=B$(1:LEN(B$)-1)&";"
   END IF
   IF VA_FOR_COUNT>0 THEN                                                       !'FOR文での使用変数名宣言、初期化
      LET BB$="double "                                                         !'仕様変更
      !'  LET BB$="int "
      FOR I=1 TO VA_FOR_COUNT-1
         IF VARIABLE_FOR$(I)<>"" THEN LET BB$=BB$&LCASE$(VARIABLE_FOR$(I))&"=0,"
      NEXT I
      IF VARIABLE_FOR$(VA_FOR_COUNT)<>"" THEN LET BB$=BB$&LCASE$(VARIABLE_FOR$(VA_FOR_COUNT))&"=0;" ELSE LET BB$=BB$(1:LEN(BB$)-1)&";"
   END IF
   IF VA_STRING_COUNT>0 THEN                                                    !'文字列変数名宣言、初期化
      LET AA$="string "
      FOR I=1 TO VA_STRING_COUNT-1
         LET AA$=AA$&LCASE$(VARIABLE_STRING$(I))&"="&CHR$(34)&CHR$(34)&","
      NEXT I
      LET AA$=AA$&LCASE$(VARIABLE_STRING$(VA_STRING_COUNT))&"="&CHR$(34)&CHR$(34)&";"
   END IF
   IF B$<>"" THEN CALL CPRINT(B$)
   IF BB$<>"" THEN CALL CPRINT(BB$)
   IF AA$<>"" THEN
      LET AA$=TRANSFORM$(AA$,"$","_string")
      CALL CPRINT(AA$)
   END IF
   IF B$<>"" OR BB$<>"" OR AA$<>"" THEN CALL CPRINT("")
END SUB

SUB CPRINT(X$)                                                                  !'簡易整形&表示
   IF POS(X$,"{")>0 AND POS(X$,"{}")=0 AND POS(X$,"{"&CHR$(34)&CHR$(34)&"}")=0 AND LITERAL(X$,"{")=0 THEN
      PRINT REPEAT$(" ",CTAB*4);
      LET CTAB=CTAB+1
   ELSEIF POS(X$,"}")>0 AND POS(X$,"{}")=0 AND POS(X$,"{"&CHR$(34)&CHR$(34)&"}")=0 AND LITERAL(X$,"}")=0 THEN
      LET CTAB=CTAB-1
      IF CTAB<0 THEN LET CTAB=0
      PRINT REPEAT$(" ",CTAB*4);
   ELSE
      PRINT REPEAT$(" ",CTAB*4);
   END IF
   IF POS(X$,"null")>0 AND LITERAL(X$,"null")=0 THEN LET X$=TRANSFORM$(X$,"null","NULL")
   PRINT TRIM$(X$)
END SUB
END

EXTERNAL  SUB SPLIT(X$,C$(),N)                                                  !'PRINT文抽出ルーチン(偶数番目が文字定数)
OPTION ARITHMETIC NATIVE                                                        !'A;"ABC";B;"DEF" → C$(1)=A; C$(2)="ABC" C$(3)=;B;  C$(4)="DEF"
MAT C$=NUL$                                                                     !'"ABC";B;C;"DEF" → C$(1)="" C$(2)="ABC" C$(3)=;B;C; C$(4)="DEF"
LET N=1
FOR I=1 TO LEN(X$)
   IF X$(I:I)=CHR$(34) THEN
      IF MOD(N,2)=0 THEN
         LET C$(N)=C$(N)&X$(I:I)
         LET N=N+1
      ELSE
         LET N=N+1
         LET C$(N)=C$(N)&X$(I:I)
      END IF
   ELSE
      LET C$(N)=C$(N)&X$(I:I)
   END IF
NEXT I
END SUB

EXTERNAL  SUB SPLIT2(X$,C$(),N)                                                 !'抽出ルーチン「,」区切り
OPTION ARITHMETIC NATIVE                                                        !'A,B(1,1),"A,B" → A
LET SP=0                                                                        !'                  B(1,1)
MAT C$=NUL$                                                                     !'                  "A,B"
LET N=0
LET SW=0
FOR I=1 TO LEN(X$)
   IF I=1 OR X$(I:I)="," THEN
      IF I=1 THEN LET K=1 ELSE LET K=I+1
      FOR J=I+1 TO LEN(X$)
         IF X$(J:J)="(" THEN LET SP=SP+1
         IF X$(J:J)=")" THEN LET SP=SP-1
         IF X$(J:J)=CHR$(34) THEN LET SW=1-SW
         IF SW=0 AND SP=0 AND X$(J:J)="," OR J=LEN(X$) THEN EXIT FOR
      NEXT  J
      LET N=N+1
      IF J=LEN(X$) THEN LET J=J+1
      LET C$(N)=TRIM$(X$(K:J-1))
      LET I=J-1
   END IF
NEXT I
END SUB

EXTERNAL FUNCTION TRANSFORM$(A$,B$,C$)                                          !'置換ルーチン TRANSFORM$("AAABAB","A","a")="aaaBaB"
OPTION ARITHMETIC NATIVE
DO                                                                              !'複数個に対応するためループ
   LET N=POS(A$,B$)
   IF N>0 THEN
      LET L$=LEFT$(A$,N-1)
      LET R$=RIGHT$(A$,LEN(A$)-LEN(B$)-N+1)
      LET A$=L$&C$&R$
   END IF
LOOP UNTIL N=0
LET TRANSFORM$=A$
END FUNCTION

EXTERNAL FUNCTION TRANSFORM2$(A$,B$,C$)                                         !'置換ルーチン
OPTION ARITHMETIC NATIVE
LET I=0
DO
   LET I=I+1
   IF A$(I:I)=CHR$(34) THEN LET SW=1-SW
   IF SW=0 AND A$(I:I+LEN(B$)-1)=B$ THEN
      LET A$(I:I+LEN(B$)-1)=""
      LET A$(I:I-1)=C$
   END IF
LOOP UNTIL LEN(A$)<=I
LET TRANSFORM2$=A$
END FUNCTION

EXTERNAL FUNCTION FRONTSTRING$(A$,B$)                                           !'前方取り出し FRONTSTRING$("ABCDEFG","DE")="ABC"
OPTION ARITHMETIC NATIVE
LET N=POS(A$,B$)
IF N=0 THEN
   LET FRONTSTRING$=A$
ELSE
   LET FRONTSTRING$=A$(1:N-1)
END IF
END FUNCTION

EXTERNAL FUNCTION BEHINDSTRING$(A$,B$)                                          !'後方取り出し BEHINDSTRING$("ABCDEFG","DE")="FG"
OPTION ARITHMETIC NATIVE
LET N=POS(A$,B$)
IF N=0 THEN
   LET BEHINDSTRING$=A$
ELSE
   LET BEHINDSTRING$=A$(N+LEN(B$):LEN(A$))
END IF
END FUNCTION

EXTERNAL  FUNCTION BETWEENSTRING$(X$,B$,A$)                                     !'指定間取り出し BETWEENSTRING$("ABCDEFG","B","F")="CDE"
OPTION ARITHMETIC NATIVE
LET K$=BEHINDSTRING$(X$,B$)
LET BETWEENSTRING$=FRONTSTRING$(K$,A$)
END FUNCTION

EXTERNAL SUB TOKUN(A$,X$(),K)                                                   !'抽出ルーチン「,」区切り
OPTION ARITHMETIC NATIVE
LET B$=A$
MAT X$=NUL$
LET K=0
DO
   LET N=POS(B$,",")
   IF N>0 THEN
      LET K=K+1
      LET X$(K)=TRIM$(FRONTSTRING$(B$,","))
      LET B$=BEHINDSTRING$(B$,",")
   END IF
LOOP UNTIL N=0
LET K=K+1
IF RIGHT$(B$,1)="," THEN
   LET X$(K)=TRIM$(LEFT$(B$,LEN(B$)-1))
ELSE
   LET X$(K)=TRIM$(B$)
END IF
END SUB
 

Re: C++トランスレーター(試作・テスト版)

 投稿者:しばっち  投稿日:2014年 7月16日(水)20時53分25秒
  > No.3434[元記事へ]

続き 5

EXTERNAL  FUNCTION TRANSFUNC$(X$)                                               !'関数名変換ルーチン SQR(X) → sqrt(x)
OPTION ARITHMETIC NATIVE
RESTORE
DO
   READ IF MISSING THEN EXIT DO:S$,T$
   LET FL=0
   FOR I=1 TO FC
      IF POS(LCASE$(X$),FUNCNAME$(I))>0 THEN                                    !'FUNCTION定義で同一関数名ではない
         LET FL=1
         IF FUNCNAMENOARG$(I)<>"" THEN                                          !'DEF文定義の引数なしの場合「()」をつける DEF F=X^2 , LET Y=Y+F → =y=y+f();
            LET X$=TRANSFORM$(X$,UCASE$(FUNCNAMENOARG$(I)(1:LEN(FUNCNAMENOARG$(I))-2)),"#")
            LET X$=TRANSFORM$(X$,"#",FUNCNAMENOARG$(I))
         END IF
      END IF
   NEXT I
   IF FL=0 THEN
      DO WHILE POS(X$,S$)>0 AND LITERAL(X$,S$)=0
         LET X$=TRANSFORM$(X$,S$,T$)
      LOOP
      LET SS$=S$(1:LEN(S$)-1)&" ("
      DO WHILE POS(X$,SS$)>0 AND LITERAL(X$,SS$)=0
         LET X$=TRANSFORM$(X$,SS$,T$)
      LOOP
   END IF
LOOP
DO
   LET FL=0
   !'IF POS(X$,"POS(")>0 THEN
   !'   LET I=POS(X$,"POS(")
   !'   LET X$=TRANSFORM$(X$,"POS(","")
   !'   LET X$=TRANSFORM$(X$,",",".find(")
   !'   LET I=POS(X$,")",I)
   !'   LET X$(I:I)=",0)"
   !'   LET FL=1
   !'END IF
   !' IF POS(X$,"VAL(")>0 AND POS(X$,"BVAL(")=0 THEN
   !'    LET X$=TRANSFORM$(X$,"VAL(","atod(")
   !'    LET X$=TRANSFORM$(X$,")",".c_str(}}")
   !'    LET X$=TRANSFORM$(X$,"}",")")
   !'    LET FL=1
   !' END IF
   IF POS(X$,"^")>0 AND LITERAL(X$,"^")=0 THEN
      DO WHILE POS(X$,"^")>0
         LET N=POS(X$,"^")
         LET SP=0
         FOR I=N-1 TO 1 STEP -1
            IF X$(I:I)=")" OR X$(I:I)="]" THEN LET SP=SP+1
            IF SP>0 AND (X$(I:I)="(" OR X$(I:I)="[") THEN LET SP=SP-1
            IF POS("=,+-*/(<>[ ",X$(I:I))>0 AND SP=0 AND (I<>N-1 OR X$(I:I)<>" ") THEN
               IF POS("(",X$(I:I))>0 AND POS("+-*/=,",X$(I-1:I-1))>0 OR POS("<>=,+-*/ ",X$(I:I))>0 OR POS("abcdefghijklmnopqrstuvwxyz",LCASE$(X$(I-1:I-1)))=0 OR POS("([",X$(I:I))>0 AND POS("abcdefghijklmnopqrstuvwxyz",LCASE$(X$(I-1:I-1)))>0 AND (X$(N-1:N)<>")^" AND X$(N-1:N)<>"]^") THEN
                  IF X$(N-1:N)=")^" OR X$(N-1:N)="]^" THEN
                     IF (POS("+-*/ ",X$(I-1:I-1))>0 OR X$(I-1:I)="((") AND POS("abcdefghijklmnopqrstuvwxyz",LCASE$(X$(I-1:I-1)))=0 THEN
                        LET X$(I:I-1)="pow("
                     ELSE
                        LET X$(I+1:I)="pow("
                     END IF
                  ELSE
                     LET X$(I+1:I)="pow("
                  END IF
                  EXIT FOR
               END IF
            ELSEIF SP=0 AND I=1 AND POS(",(=+-*/[ ",X$(I:I))=0 THEN
               LET X$="pow("&X$
               EXIT FOR
            END IF
         NEXT I
         LET N=POS(X$,"^")
         LET SP=0
         FOR I=N+1 TO LEN(X$)
            IF X$(I:I)="(" OR X$(I:I)="[" THEN LET SP=SP+1
            IF SP>0 AND (X$(I:I)=")" OR X$(I:I)="]") THEN LET SP=SP-1
            IF POS(",)+-*/]<> ",X$(I:I))>0 AND SP=0 AND (I<>N+1 OR X$(I:I)<>" ") THEN
               LET X$(I:I-1)=")"
               EXIT FOR
            ELSEIF SP=0 AND I=LEN(X$) AND POS(",)+-*/] ",X$(I:I))=0 THEN
               LET X$=X$&")"
            END IF
         NEXT I
         LET X$(N:N)=","
      LOOP
      LET FL=1
   END IF
   IF POS(X$,"LEN(")>0 THEN
      LET I=POS(X$,"LEN(")
      LET X$=TRANSFORM$(X$,"BLEN(","")
      LET X$=TRANSFORM$(X$,"LEN(","")
      LET I=POS(X$,")",I)
      LET X$(I:I)=""
      LET X$(I:I)=".size()"
      LET FL=1
   END IF
   !' IF POS(X$,"ANGLE(")>0 THEN
   !'    LET I=POS(X$,"ANGLE(")
   !'    LET J=POS(X$,",",I)
   !'    LET AA$=X$(I+6:J-1)
   !'    LET K=POS(X$,")",J)
   !'    LET BB$=X$(J+1:K-1)
   !'    LET X$=TRANSFORM$(X$,"ANGLE(","atan2(")
   !'    LET X$=TRANSFORM$(X$,AA$,REPEAT$("#",LEN(AA$)))
   !'    LET X$=TRANSFORM$(X$,BB$,REPEAT$("&",LEN(BB$)))
   !'    LET X$=TRANSFORM$(X$,REPEAT$("#",LEN(AA$)),BB$)
   !'    LET X$=TRANSFORM$(X$,REPEAT$("&",LEN(BB$)),AA$)
   !'    LET FL=1
   !' END IF
   !' IF POS(X$,"LEFT$(")>0 THEN
   !'    LET I=POS(X$,"LEFT$(")
   !'    LET X$=TRANSFORM$(X$,"LEFT$(","")
   !'    LET I=POS(X$,",",I)
   !'    LET X$(I:I)=".substr(0,"
   !'    LET FL=1
   !' END IF
   !' IF POS(X$,"RIGHT$(")>0 THEN
   !'    LET I=POS(X$,"RIGHT$(")
   !'    LET BETWEEN$=BETWEENSTRING$(X$,"RIGHT$(",",")
   !'    LET NUM$=BETWEENSTRING$(X$,",",")")
   !'    LET X$=TRANSFORM$(X$,"RIGHT$(","")
   !'    LET I=POS(X$,",",I)
   !'    LET X$(I:I)=".substr("&BETWEEN$&".size()-"&NUM$&","
   !'    LET FL=1
   !' END IF
   !' IF POS(X$,"MID$(")>0 THEN
   !'    DIM C$(3)
   !'    LET J=POS(X$,"MID$(")
   !'    LET SP=0
   !'    FOR I=J+5 TO LEN(X$)
   !'       IF X$(I:I)="," THEN LET SP=SP+1
   !'       IF SP=2 AND X$(I:I)=")" THEN EXIT FOR
   !'    NEXT I
   !'    CALL TOKUN(X$(J:I),C$)
   !'    LET X$=TRANSFORM$(X$,"MID$("&C$(1),C$(1)&".subst("&C$(2)&"-1,"&C$(3)&")")
   !'    LET FL=1
   !' END IF
   !' IF POS(X$,"BITAND(")>0 THEN
   !'    LET I=POS(X$,"BITAND(")
   !'    LET I=POS(X$,",",I)
   !'    LET X$(I:I)="&"
   !'    LET X$=TRANSFORM$(X$,"BITAND","")
   !'    LET FL=1
   !' END IF
   !' IF POS(X$,"BITOR(")>0 THEN
   !'    LET I=POS(X$,"BITOR(")
   !'    LET I=POS(X$,",",I)
   !'    LET X$(I:I)="|"
   !'    LET X$=TRANSFORM$(X$,"BITOR","")
   !'    LET FL=1
   !' END IF
   !' IF POS(X$,"BITXOR(")>0 THEN
   !'    LET I=POS(X$,"BITXOR(")
   !'    LET I=POS(X$,",",I)
   !'    LET X$(I:I)="^"
   !'    LET X$=TRANSFORM$(X$,"BITXOR","")
   !'    LET FL=1
   !' END IF
   !' IF POS(X$,"BITNOT(")>0 THEN
   !'    LET X$=TRANSFORM$(X$,"BITNOT","~")
   !'    LET FL=1
   !' END IF
   IF POS(X$,"&&")=0 AND POS(X$,"&")>0 AND LITERAL(X$,"&")=0 THEN
      LET X$=TRANSFORM$(X$,"&","+")
      LET FL=1
   END IF
   LET I=POS(X$,"/")
   IF I>0 THEN
      LET SW=0
      LET I=0
      DO
         LET I=I+1
         IF X$(I:I)=CHR$(34) THEN LET SW=1-SW
         IF SW=0 AND X$(I:I)="/" AND X$(I:I+8)<>"/(double)" THEN
            LET K=0
            LET L=0
            FOR J=I-1 TO 1 STEP -1                                              !'被除数
               IF POS("0123456789",X$(J:J))>0 THEN                              !'定数か
                  LET K=1
               END IF
               IF POS("abcdefghijklmnopqrstuvwxyz",LCASE$(X$(J:J)))>0 THEN      !'変数、関数なら
                  LET K=0                                                       !'変数名 A123など
                  EXIT FOR
               END IF
               IF POS("= ;,+-*/<>()[]",X$(J:J))>0 AND (J<>I-1 OR X$(J:J)<>" ") THEN EXIT FOR
            NEXT J
            FOR J=I+1 TO LEN(X$)                                                !'除数
               IF POS("0123456789",X$(J:J))>0 THEN
                  LET L=1
               END IF
               IF POS("abcdefghijklmnopqrstuvwxyz",LCASE$(X$(J:J)))>0 THEN      !'変数、関数なら
                  LET L=0
                  EXIT FOR
               END IF
               IF POS(" ;,+-*/<>()[]",X$(J:J))>0 AND (J<>I+1 OR X$(J:J)<>" ") THEN EXIT FOR
            NEXT J
            IF K=1 AND L=1 THEN                                                 !'共に定数なら
               LET X$(I:I)="/(double)"                                          !'double型へキャスト
               LET I=I+8
               LET FL=1
            END IF
         END IF
      LOOP UNTIL I>=LEN(X$)
   END IF
   IF POS(X$,"TIME")>0 AND LITERAL(X$,"TIME")=0 AND POS(X$,"TIME$")=0 THEN
      LET X$=TRANSFORM$(X$,"TIME","(unsigned)time(NULL)")
      LET FL=1
   END IF
   IF POS(X$,CHR$(34)&CHR$(34)&CHR$(34)&CHR$(34))>0 THEN                        !'「PRINT """";A$;""""」
      LET X$=TRANSFORM$(X$,CHR$(34)&CHR$(34)&CHR$(34)&CHR$(34),CHR$(39)&CHR$(34)&CHR$(39))
      LET FL=1
   END IF
LOOP UNTIL FL=0
IF LITERAL(X$,"$")=0 THEN LET X$=TRANSFORM$(X$,"$","_string")
LET TRANSFUNC$=X$
DATA "ABS(","abs("                                                              !'関数名置換リスト BASIC → C/C++
DATA "ARG(","arg("
DATA "ANGLE(","angle("
DATA "ASIN(","asin("
DATA "ATN(","atan("
DATA "ACOS(","acos("
DATA "ACSC(","asin(1.0/"                                                        !'「"A・CSC("」
DATA "ASEC(","acos(1.0/"
DATA "ACOT(","atan(1.0/"
DATA "ATANH(","atanh("
DATA "ASINH(","asinh("
DATA "ACOSH(","acosh("
DATA "ASECH(","asech("
DATA "ACSCH(","acsch("
DATA "ACOTH(","acoth("
DATA "SUBSTR$(","substr("                                                       !'「"SUB・STR$("」「"SU・BSTR$("」
DATA "BSTR$(","bstr("
DATA "BVAL(","bval("
DATA "BITAND(","bit_and("
DATA "BITOR(","bit_or("
DATA "BITXOR(","bit_xor("
DATA "BITNOT(","bit_not("                                                       !'「"BIT・NOT("」
DATA "BITIMP(","bit_imp("
DATA "CEIL(","ceil("
DATA "CHR$(","chr("
DATA "COMB(","comb("
DATA "CONJ(","conj("
DATA "COMPLEX(","complex<double>("
DATA "COS(","cos("
DATA "COT(","1.0/tan("
DATA "CSC(","1.0/sin("
DATA "CSCH(","1.0/sinh("
DATA "COSH(","cosh("
DATA "COTH(","1.0/tanh("
DATA "CBRT(","cbrt("                                                            !'立方根 X^(1/3)
DATA "DEG(","(180.0/pi*"
DATA "DENOM(","denominator("
DATA "EXP(","exp("
DATA "ERF(","erf("                                                              !'誤差関数
DATA "ERFC(","erfc("                                                            !'1-erf(x)
DATA "EPS(","eps("
DATA "FACT(","fact("
DATA "FP(","fp("
DATA "LGAMMA(","lgamma("                                                        !'対数ガンマ関数「"L・GAMMA("」
DATA "GAMMA(","tgamma("                                                         !'ガンマ関数
DATA "GCD(","gcd("
DATA "HYPOT(","hypot("                                                          !'HYPOT(X,Y)=SQR(X*X+Y*Y)
DATA "IP(","ip("
DATA "INT(","floor("
DATA "IM(","imag("
DATA "J0(","j0("                                                                !'ベッセル関数 J0(X)
DATA "J1(","j1("                                                                !'ベッセル関数 J1(X)
DATA "JN(","jn("                                                                !'ベッセル関数 JN(N,X)
DATA "LCM(","lcm("
DATA "LOG(","log("
DATA "LOG2(","log2("
DATA "LOG10(","log10("
DATA "LTRIM$(","ltrim("
DATA "LCASE$(","lcase("
DATA "LEFT$(","left("
DATA "MOD(","fmod("
DATA "MID$(","mid("
DATA "MAX(","fmax("
DATA "MIN(","fmin("
!'DATA "MAX(","tmax("
!'DATA "MIN(","tmin("
DATA "NOT(","(! "
DATA "NUMER(","numerator("
DATA "ORD(","ord("
DATA "PERM(","perm("
DATA "POS(","pos("
DATA "RND","rand()/32768.0"
DATA "ROUND(","round("
DATA "REMAINDER(","fmod("
DATA "RAD(","(pi/180.0*"
DATA "RE(","real("
DATA "RTRIM$(","rtrim("
DATA "RIGHT$(","right("
DATA "REPEAT$(","repeat("
DATA "SPC(","spc("
DATA "SGN(","sgn("
DATA "SQR(","sqrt("
DATA "SIN(","sin("
DATA "SEC(","1.0/cos("
DATA "SINH(","sinh("
DATA "SECH(","1.0/cosh("
!'DATA "STR$(","toString<double>("
DATA "STR$(","str("
DATA "UBOUND(","ubound("
DATA "TRUNCATE(","truncate("
DATA "TAN(","tan("
DATA "TANH(","tanh("
DATA "TAB(","tab("
DATA "UCASE$(","ucase("
DATA "VAL(","val("
DATA "Y0(","y0("                                                                !'ベッセル関数 Y0(X)
DATA "Y1(","y1("                                                                !'ベッセル関数 Y1(X)
DATA "YN(","yn("                                                                !'ベッセル関数 YN(N,X)
END FUNCTION

EXTERNAL  FUNCTION TRANSCO$(A$)                                                 !'条件式変換
OPTION ARITHMETIC NATIVE
LET S$="_0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"
LET I=POS(A$,"AND")
IF I>0 AND POS(S$,A$(I-1:I-1))=0 AND POS(S$,A$(I+3:I+3))=0 THEN LET A$=TRANSFORM2$(A$,"AND"," && ")
LET I=POS(A$,"OR")
IF I>0 AND POS(S$,A$(I-1:I-1))=0 AND POS(S$,A$(I+2:I+2))=0 THEN LET A$=TRANSFORM2$(A$,"OR"," || ")
IF POS(A$,"<=")>0 THEN LET A$=TRANSFORM2$(A$,"=<","<=")
IF POS(A$,"=>")>0 THEN LET A$=TRANSFORM2$(A$,"=>",">=")
IF POS(A$,"<=")=0 AND POS(A$,">=")=0 AND POS(A$,"=")>0 THEN
   LET A$=TRANSFORM2$(A$,"=","\")                                               !'無限ループ抑制
   LET A$=TRANSFORM2$(A$,"\","==")
END IF
IF POS(A$,"<>")>0 THEN LET A$=TRANSFORM2$(A$,"<>","!=")
IF POS(A$,"><")>0 THEN LET A$=TRANSFORM2$(A$,"><","!=")
LET TRANSCO$=A$
END FUNCTION

EXTERNAL  FUNCTION TRANSRESERVED$(X$)                                           !'C/C++予約語名変換("_"アンダーバー付加)
OPTION ARITHMETIC NATIVE
RESTORE
DO
   READ IF MISSING THEN EXIT DO:A$
   LET I=POS(X$,A$)
   IF LITERAL(X$,A$)=0 AND (I>0 AND POS(" (),+-*/=<>",X$(I-1:I-1))>0) OR I=1 THEN
      LET X$=TRANSFORM2$(X$,A$,"_"&LCASE$(A$))
   END IF
LOOP
LET TRANSRESERVED$=X$
DATA ALIGNOF,ALIGNAS,ASM,AUTO,BOOL,BREAK,CATCH,CHAR,CLASS,CONST,CONSTEXPR,CONTINUE,DECLTYPE,DEFAULT,DELETE,DOUBLE
DATA ENUM,EXPLICIT,EXPORT,FALSE,FINAL,FLOAT,FRIEND,INLINE,LONG,MUTABLE,NAMESPACE,NEW,OPERATOR,OVERRIDE
DATA PRIVATE,PROTECTED,REGISTER,SHORT,SIGNED,SIZEOF,STATIC,STRUCT,SWITCH
DATA TEMPLATE,THIS,THROW,TRUE,TRY,TYPEDEF,TYPEID,TYPENAME,NOEXCEPT,NULLPTR,USING,UNION,UNSIGNED,VIRTUAL,VOID,VOLATILE
DATA XOR,RESTRICT
!'DATA COMPL,EXTERN
END FUNCTION

EXTERNAL  FUNCTION TRANSARRAYNAME$(X$)                                          !'配列変数名変換 SQR(A(10)) → SQR(a[10])
OPTION ARITHMETIC NATIVE
LET X$=TRIM$(X$)
LET I=0
DO
   LET I=POS(X$,"(",I+1)
   IF I=0 THEN EXIT DO
   RESTORE
   LET FL=0
   DO
      READ IF MISSING THEN EXIT DO:FUNC$
      IF LEN(FUNC$)<=I AND X$(I-LEN(FUNC$)+1:I)=FUNC$ THEN
         LET FL=1
         IF FUNC$=" (" AND POS("abcdefghijklmnopqrstuvwxyz",LCASE$(X$(I-2:I-2)))>0 THEN   !' A (N) → a[n]
            LET X$(I-1:I-1)=""
            LET FL=0
         ELSE
            EXIT DO
         END IF
      END IF
      IF FUNC$(LEN(FUNC$):LEN(FUNC$))="(" THEN
         LET S$=FUNC$(1:LEN(FUNC$)-1)&" ("
         IF LEN(S$)<=I AND X$(I-LEN(S$)+1:I)=S$ THEN
            LET FL=1
            EXIT DO
         END IF
      END IF
   LOOP
   IF FL=0 THEN
      FOR J=1 TO FC
         IF LEN(FUNCNAME$(J))<=I AND LCASE$(X$(I-LEN(FUNCNAME$(J)):I))=FUNCNAME$(J)&"(" THEN
            LET FL=1
            EXIT FOR
         END IF
      NEXT J
   END IF
   IF I>1 AND (X$(I-1:I)="=("  OR X$(I-1:I)="^(") OR X$(1:1)="(" THEN LET FL=1
   LET SP=0
   IF FL=0 THEN
      IF X$(I:I)="(" THEN LET SP=SP+1
      LET X$(I:I)="["
      LET J=I
      DO
         LET J=J+1
         IF X$(J:J)="(" THEN LET SP=SP+1
         IF X$(J:J)=")" THEN  LET SP=SP-1
         IF SP=0 THEN
            LET X$(J:J)="]"
            EXIT DO
         END IF
         IF SP=1 AND X$(J:J)="," THEN LET X$(J:J)="]["
      LOOP UNTIL LEN(X$)<=J
   END IF
LOOP
IF TYPE$="complex <double> " THEN
   LET X$=TRANSFORM$(X$,"[","\static_cast<int>(abs(")                           !'int型へ強制キャスト(複素数型)
   LET X$=TRANSFORM$(X$,"\","[")
   LET X$=TRANSFORM$(X$,"]","))\")
   LET X$=TRANSFORM$(X$,"\","]")
ELSE
   LET X$=TRANSFORM$(X$,"[","\static_cast<int>(")                               !'int型へ強制キャスト
   LET X$=TRANSFORM$(X$,"\","[")
   LET X$=TRANSFORM$(X$,"]",")\")
   LET X$=TRANSFORM$(X$,"\","]")
END IF
LET TRANSARRAYNAME$=X$
DATA "ABS(","ACOS(","ANGLE(","ARG(","ASIN(","ATN(","ACSC(","ASEC(","ACOT(","ASECH(","ACSCH(","ACOTH("
DATA "BITAND(","BITNOT(","BITOR(","BITXOR(","BLEN(","SUBSTR$(","BSTR$(","BVAL("
DATA "CEIL(","CHR$(","CONJ(","COS(","COSH(","COT(","COTH(","CON(","CSC(","CSCH(","COMPLEX(","COMB("
DATA "DEG(","DENOM(","DET(","DWORD$(","DOT("
DATA "EXP(","EPS(","FACT(","FP(","GCD("
DATA "IM(","INT(","IP(","LBOUND(","LCASE$(","LEFT$(","LEN(","LOG(","LOG10(","LOG2(","LTRIM$("
DATA "MAX(","MID$(","MIN(","MOD(","NOT(","NUMER(","ORD("
DATA "PACKDBL$(","PERM(","POS(","PIXLX(","PIXELY(","RAD(","RE(","REMAINDER(","REPEAT$(","RIGHT$(","RTRIM$(","ROUND("
DATA "SEC(","SECH(","SGN(","SIN(","SINH(","SQR(","STR$(","SIZE(","SPC("
DATA "TAB(","TAN(","TANH(","TRUNCATE("
DATA "UBOUND(","UCASE$(","USING$(","UNPACKDBL(","VAL(","WORD$(","WORLDX(","WORLDY("
DATA "LGAMMA(","GAMMA(","ERF(","ERFC(","HYPOT(","CBRT(","J0(","J1(","JN(","Y0(","Y1(","YN("
DATA "+(","-(","*(","/("," (","((","IF(","OR(","AND(","||(","&&(","pow("
END FUNCTION

EXTERNAL  FUNCTION TRIM$(X$)                                                    !'空白削除
OPTION ARITHMETIC NATIVE
LET TRIM$=RTRIM$(LTRIM$(X$))
END FUNCTION

EXTERNAL  FUNCTION LITERAL(X$,Y$)                                               !'文字定数か
OPTION ARITHMETIC NATIVE
LET K=POS(X$,Y$)
LET S=0
IF K>0 THEN
   FOR I=K+1 TO LEN(X$)
      IF X$(I:I)=CHR$(34) THEN
         LET S=S+1
         EXIT FOR
      END IF
   NEXT I
   FOR I=K-1 TO 1 STEP -1
      IF X$(I:I)=CHR$(34) THEN
         LET S=S+1
         EXIT FOR
      END IF
   NEXT I
END IF
IF S=2 THEN LET LITERAL=1 ELSE LET LITERAL=0
END FUNCTION

EXTERNAL  FUNCTION LLCASE$(X$)                                                  !'文字定数でないなら小文字化
OPTION ARITHMETIC NATIVE
LET S$=""
LET SW=0
FOR I=1 TO LEN(X$)
   IF X$(I:I)=CHR$(34) THEN LET SW=1-SW
   IF SW=0 THEN
      LET S$=S$&LCASE$(X$(I:I))
   ELSE
      LET S$=S$&X$(I:I)
   END IF
NEXT I
LET LLCASE$=S$
END FUNCTION

EXTERNAL  FUNCTION UUCASE$(X$)
OPTION ARITHMETIC NATIVE
LET S$=""
LET SW=0
FOR I=1 TO LEN(X$)
   IF X$(I:I)=CHR$(34) THEN LET SW=1-SW
   IF SW=0 THEN
      LET S$=S$&UCASE$(X$(I:I))
   ELSE
      LET S$=S$&X$(I:I)
   END IF
NEXT I
LET UUCASE$=S$
END FUNCTION

EXTERNAL  SUB SETUP(X$)                                                         !'手始めに大文字にする 大文字→小文字化(無限ループを抑える)
OPTION ARITHMETIC NATIVE
LET X$=TRIM$(X$)
LET I=POS(X$,"!")
IF I=0 THEN LET I=POS(X$,"REM ")
IF I>0 AND LITERAL(X$,"!")=0 AND LITERAL(X$,"REM ")=0 THEN
   LET BEHIND$=X$(I:LEN(X$))
   LET X$=X$(1:I-1)
END IF
LET X$=UUCASE$(X$)&BEHIND$
END SUB
 

戻る