2016-11-21 3 views
0

私たちは、チームプロジェクトとしてJava言語(IFJ16言語と呼ばれる)の解釈を書いています。C- Interpret/Compilatorで必要なときに構造体の中の別の型のポインタを使用する

我々がコーディングされている言語は、純粋なC(我々はすべてのOOPを使用するカント)

あり、これは問題です:私たちは

typedef struct Instr { 
    BTSNode *Id1; // Adress of first operand 
    BTSNode *Id2; // Adresss of second operand 
    BTSNode *Id3; // Adress where the result is added 
    InstrType type; // Type of instruction. InstrType is enum struct (like insDiv, insMux, ...) 
}Instr; 

INSTR

タイプの構造を読み取る必要がある解釈で

BTSNodeは、すべての変数(クラスと関数が格納されている)のバイナリツリー内のノードです。構造:

typedef struct tBTSNode { 
     tableName key; // Key, variable/func,class name 
     NodeType nodeType; // Type (class, func, var) 
     union { 
      struct tBTSNode *functions; // Pointer on functions of class 
      int argNo; // Argument number if its function argument 
    } tBTSNode; 

     tabSymbol data; // Data 
     int inc; // Was var initialized? 

     struct tBTSNode *variables; // Pointer on function or class variables 

     struct tBTSNode *lptr; // Pointer on left subtree 
     struct tBTSNode *rptr; // Pointer on right subtree 
    } BTSNode, *tBTSNodePtr; 


    typedef struct tableSymbolVariable { 
      varType type; //In var case: type of var | in function case= return var type | In class case: NULL 
      varValue value; //In case of var: var value | In case of class and func: null 
     } tabSymbol, *tabSymbolPtr; 
    typedef enum { 
     var_int, 
     var_double, 
     var_string, 
     var_void, 
    } varType; 


    union { 
     int intValue; 
     double doubleValue; 
     char *stringValue; 
    } varValue; 

のでInterprerは(で、...最初に実行されますので、最初の命令)命令(タイプINSTRのInstructionfによって満たされたシンプルなスタック)、注文OF STACKを得ました。彼がスタックの最後のアイテムを実行している間、サイクリングによって意志を解釈する(その時間にプログラムが終了する)。

ここに問題があります。私たちが持っているところの例

a = b + c; 

とても簡単です。だから私はちょうどタイプがCOMPTであれば(もちろん私がチェックするしまった私のdoMath関数を呼び出します

typedef struct Instr { 
    BTSNode *Id1 = adress of node with variable b 
    BTSNode *Id2 = adress of node with variable c 
    BTSNode *Id3 = adress of node with variable a (there will by the b+c result storaged) 
    InstrType type = insPlus 
}Instr; 

:構造は(厥だけでなく、いただきました!問題をあなたを示すための実際のコード、ノート)のようになります。 )

Id3->data.value = Id1->data.value Id2->data.value; 

ここに問題があります。この例を変更するとどうなりますか

a = b + 30; 

どうすればよいですか? aとbは問題ありません。私はバイナリツリーにvaribalesを見つけ、それらをId1とId3に追加します。 しかし30番はどうすればいいですか?

Id1-Td3が無効になるようにInstrの構造を変更しようと思っていました*ポインタと優先順位分析では、BTSNodeポインタ(実際にはポインタの場合)と数値(または文字列、 )この場合、int * pointerにretypeするだけです。だから、構造が

 typedef struct Instr { 
    BTSNode *Id1 = adress of node with variable b 
    Int *Id2 = adress of node with variable c 
    BTSNode *Id3 = adress of node with variable a (there will by the b+c result storaged) 
    InstrType type = insPlus 
}Instr; 

になりそう、私の質問は:が、これは「正しい」ソリューションですか?あるいは私はそれをより良く、より簡単に行うことができますか?

ありがとうございました。

+0

あなたはすでにBTSNodeの種類が異なっていると思います。それで、追加の "数値定数"を作成するのはなぜですか? – zwol

+0

さて、私はその考えを持っています。しかし、より多くの型が存在し、解釈の型はほとんど完了しており、再作業の時間はありません:/ – John

+2

...これでほとんど終わりましたが、数値定数のサポートは既にありませんか? – zwol

答えて

1

ダミーの匿名変数を定数値で作成し、通常の変数のような命令ノードから参照することができます。

関連する問題