ast_statement.h 3.67 KB
Newer Older
victor's avatar
 
victor committed
1 2 3 4 5
#ifndef AST_STATEMENT_H
#define AST_STATEMENT_H

#include <QList>

victor's avatar
 
victor committed
6
#undef ABSTRACTSYNTAXTREE_EXPORT
victor's avatar
 
victor committed
7
#ifdef DATAFORMATS_LIBRARY
victor's avatar
 
victor committed
8 9 10 11
#define ABSTRACTSYNTAXTREE_EXPORT Q_DECL_EXPORT
#else
#define ABSTRACTSYNTAXTREE_EXPORT Q_DECL_IMPORT
#endif
victor's avatar
 
victor committed
12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28

namespace AST {

/** Defines statement (in terminology of Kumir 1.x: ProgaValue) type */
enum StatementType {

    /** No actual statement, but error code */
    StError,

    /** Assignment of function call
      * NOTE: function call is "assignment", where left part is void */
    StAssign,

    /** Assert, pre-condition of post-condition,
      * depending on context */
    StAssert,

victor's avatar
 
victor committed
29 30 31
    /** Array-variable initialization */
    StVarInitialize,

victor's avatar
 
victor committed
32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47
    /** Terminal input */
    StInput,

    /** Terminal output */
    StOutput,

    /** Loop block */
    StLoop,

    /** One or two if-then-else sub-blocks */
    StIfThenElse,

    /** At lease one switch-case-else sub-block */
    StSwitchCaseElse,

    /** Loop break of function return statement */
victor's avatar
 
victor committed
48 49 50 51 52 53 54
    StBreak,

    /** Pause evaluation until key pressed */
    StPause,

    /** Terminate evaluation unconditionally */
    StHalt
victor's avatar
 
victor committed
55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79
};

/** Loop clarification */
enum LoopType {

    /** For loop (russian: "nc dlja") */
    LoopFor,

    /** While or forever loop (russian: "nc" or "nc poka") */
    LoopWhile,

    /** Counter loop (russian "nc ? raz") */
    LoopTimes,
};

/** Loop-specific part of statement */
struct LoopSpec {

    /** Loop type */
    enum LoopType type;

    /** Referece to "for" loop variable */
    struct Variable * forVariable;

    /** "for" loop "from" value */
victor's avatar
 
victor committed
80
    struct Expression * fromValue;
victor's avatar
 
victor committed
81 82

    /** "for" loop "to" value */
victor's avatar
 
victor committed
83
    struct Expression * toValue;
victor's avatar
 
victor committed
84 85

    /** "for" loop "step" value */
victor's avatar
 
victor committed
86
    struct Expression * stepValue;
victor's avatar
 
victor committed
87 88

    /** "while" loop condition value */
victor's avatar
 
victor committed
89
    struct Expression * whileCondition;
victor's avatar
 
victor committed
90 91

    /** "times" loop value */
victor's avatar
 
victor committed
92
    struct Expression * timesValue;
victor's avatar
 
victor committed
93 94

    /** Optional condition for loop end */
victor's avatar
 
victor committed
95
    struct Expression * endCondition;
victor's avatar
 
victor committed
96 97

    /** Loop body */
victor's avatar
 
victor committed
98
    QList<struct Statement * > body;
victor's avatar
 
victor committed
99

victor's avatar
 
victor committed
100 101
    /** Loop end lexems */
    QList<struct Lexem*> endLexems;
victor's avatar
 
victor committed
102

victor's avatar
 
victor committed
103 104 105 106 107 108
};

/** "if-then", "case" or "else" sub-block of statement */
struct ConditionSpec {

    /** Condition; empty for "else" block */
victor's avatar
 
victor committed
109
    struct Expression * condition;
victor's avatar
 
victor committed
110 111

    /** Conditional body */
victor's avatar
 
victor committed
112
    QList<struct Statement * > body;
victor's avatar
 
victor committed
113

victor's avatar
 
victor committed
114 115
    /** Condition source lexems */
    QList<struct Lexem*> lexems;
victor's avatar
 
victor committed
116 117

    QString conditionError;
victor's avatar
 
victor committed
118 119 120
};

/** Evaluable statement */
victor's avatar
 
victor committed
121
struct ABSTRACTSYNTAXTREE_EXPORT Statement {
victor's avatar
 
victor committed
122 123 124 125

    /** Statement kind */
    StatementType type;

victor's avatar
 
victor committed
126
    /** Statement source lexems */
victor's avatar
 
victor committed
127 128
    QList<struct Lexem*> lexems;
    QList<struct Lexem*> endBlockLexems;
victor's avatar
 
victor committed
129

victor's avatar
 
victor committed
130 131
    /** Compile-time error */
    QString error;
victor's avatar
 
victor committed
132

133 134
    QString headerError;
    int headerErrorLine;
victor's avatar
 
victor committed
135 136 137
    QString beginBlockError;
    QString endBlockError;

victor's avatar
 
victor committed
138 139 140
    /** If error: add to generated code or not */
    bool skipErrorEvaluation;

victor's avatar
 
victor committed
141 142
    /** List of top-level expressions for all
      * statement kinds except StLoop, StIfThenElse and StSwitchCaseElse */
victor's avatar
 
victor committed
143 144 145 146
    QList<struct Expression * > expressions;

    /** Variable references list, defined only if type==StVarInitialize */
    QList<struct Variable * > variables;
victor's avatar
 
victor committed
147 148 149 150 151 152

    /** Loop specific-part, defined only if type==StLoop */
    struct LoopSpec loop;

    /** Conditional blocks, defined only if type==StIfThenElse or type==StSwitchCaseElse */
    QList<struct ConditionSpec> conditionals;
victor's avatar
 
victor committed
153 154 155 156 157 158

    explicit Statement();
    explicit Statement(const struct Statement * src);
    void updateReferences(const struct Statement * src,
                          const struct Data * srcData,
                          const struct Data * data);
victor's avatar
 
victor committed
159
    QString dump() const;
victor's avatar
 
victor committed
160
    ~Statement();
victor's avatar
 
victor committed
161 162 163 164 165 166
};


}

#endif // AST_STATEMENT_H