[51b7345] | 1 | #include "translate.h" |
---|
| 2 | #include "../Parser/ParseNode.h" |
---|
| 3 | |
---|
| 4 | |
---|
| 5 | #include "Statement.h" |
---|
| 6 | #include "Expression.h" |
---|
| 7 | #include "Constant.h" |
---|
| 8 | #include "Type.h" |
---|
| 9 | |
---|
| 10 | int translate_driver(ParseNode *pn){ |
---|
| 11 | // in effect, this function is a visitor of sorts |
---|
| 12 | |
---|
| 13 | if (pn == 0) |
---|
| 14 | exit(1); |
---|
| 15 | |
---|
| 16 | /* Type switching considered harmful */ |
---|
| 17 | |
---|
| 18 | switch(pn->what_kind()){ |
---|
| 19 | case ParseNode::Statement: |
---|
| 20 | translate((StatementNode *)pn); |
---|
| 21 | break; |
---|
| 22 | case ParseNode::Expression: |
---|
| 23 | translate((ExpressionNode *)pn); |
---|
| 24 | break; |
---|
| 25 | default: |
---|
| 26 | translate(pn); |
---|
| 27 | break; |
---|
| 28 | } |
---|
| 29 | |
---|
| 30 | return 0; |
---|
| 31 | } |
---|
| 32 | |
---|
| 33 | Expression *translate(ExpressionNode *en){ |
---|
| 34 | Expression *expr; |
---|
| 35 | |
---|
| 36 | switch(en->what_kind()){ |
---|
| 37 | case ParseNode::CompositeExpression: |
---|
| 38 | break; |
---|
| 39 | case ParseNode::Constant: |
---|
| 40 | expr = translate((ConstantNode *)en); |
---|
| 41 | break; |
---|
| 42 | case ParseNode::VarRef: |
---|
| 43 | cout << "It's a varref" << endl; |
---|
| 44 | break; |
---|
| 45 | case ParseNode::Operator: |
---|
| 46 | cout << "It's an operator" << endl; |
---|
| 47 | break; |
---|
| 48 | default: |
---|
| 49 | break; |
---|
| 50 | } |
---|
| 51 | |
---|
| 52 | return expr; |
---|
| 53 | } |
---|
| 54 | |
---|
| 55 | Statement *translate(StatementNode *sn){ |
---|
| 56 | Statement *stmt = 0; |
---|
| 57 | |
---|
| 58 | switch(sn->get_type()){ |
---|
| 59 | case StatementNode::Exp: |
---|
| 60 | translate(sn->get_control()); |
---|
| 61 | break; |
---|
| 62 | case StatementNode::If: |
---|
| 63 | cout << "if" << endl; |
---|
| 64 | translate(sn->get_control()); |
---|
| 65 | break; |
---|
| 66 | case StatementNode::Switch: |
---|
| 67 | cout << "switch" << endl; |
---|
| 68 | break; |
---|
| 69 | case StatementNode::Case: |
---|
| 70 | cout << "case" << endl; |
---|
| 71 | break; |
---|
| 72 | case StatementNode::Default: |
---|
| 73 | cout << "default" << endl; |
---|
| 74 | break; |
---|
| 75 | case StatementNode::Choose: |
---|
| 76 | cout << "choose" << endl; |
---|
| 77 | break; |
---|
| 78 | case StatementNode::Fallthru: |
---|
| 79 | cout << "fallthru" << endl; |
---|
| 80 | break; |
---|
| 81 | case StatementNode::While: |
---|
| 82 | cout << "while" << endl; |
---|
| 83 | break; |
---|
| 84 | case StatementNode::Do: |
---|
| 85 | cout << "do" << endl; |
---|
| 86 | break; |
---|
| 87 | case StatementNode::For: |
---|
| 88 | cout << "for" << endl; |
---|
| 89 | break; |
---|
| 90 | case StatementNode::Goto: |
---|
| 91 | cout << "goto" << endl; |
---|
| 92 | break; |
---|
| 93 | case StatementNode::Continue: |
---|
| 94 | cout << "continue" << endl; |
---|
| 95 | break; |
---|
| 96 | case StatementNode::Break: |
---|
| 97 | cout << "break" << endl; |
---|
| 98 | break; |
---|
| 99 | case StatementNode::Return: |
---|
| 100 | cout << "return" << endl; |
---|
| 101 | break; |
---|
| 102 | case StatementNode::Throw: |
---|
| 103 | cout << "throw" << endl; |
---|
| 104 | break; |
---|
| 105 | case StatementNode::Try: |
---|
| 106 | cout << "try" << endl; |
---|
| 107 | break; |
---|
| 108 | case StatementNode::Catch: |
---|
| 109 | cout << "catch" << endl; |
---|
| 110 | break; |
---|
| 111 | case StatementNode::Asm: |
---|
| 112 | cout << "asm" << endl; |
---|
| 113 | break; |
---|
| 114 | default: |
---|
| 115 | break; |
---|
| 116 | } |
---|
| 117 | |
---|
| 118 | return stmt; |
---|
| 119 | } |
---|
| 120 | |
---|
| 121 | ConstantExpr *translate(ConstantNode *cn){ |
---|
| 122 | ConstantExpr *cnst; |
---|
| 123 | BasicType::Kind knd; |
---|
| 124 | |
---|
| 125 | struct Type::Qualifiers tq = {0,0,0}; |
---|
| 126 | struct BasicType::Modifiers tm = {0,0,0,0}; |
---|
| 127 | |
---|
| 128 | switch(cn->get_type()){ |
---|
| 129 | case ConstantNode::Integer: |
---|
| 130 | knd = BasicType::Int; |
---|
| 131 | break; |
---|
| 132 | case ConstantNode::Float: |
---|
| 133 | knd = BasicType::Float; |
---|
| 134 | break; |
---|
| 135 | case ConstantNode::Character: |
---|
| 136 | knd = BasicType::Char; |
---|
| 137 | break; |
---|
| 138 | case ConstantNode::String: |
---|
| 139 | break; |
---|
| 140 | default: |
---|
| 141 | break; |
---|
| 142 | } |
---|
| 143 | |
---|
| 144 | Constant *c = new Constant(new BasicType(tq,tm,knd), cn->get_name()); |
---|
| 145 | cnst = new ConstantExpr(*c); |
---|
| 146 | |
---|
| 147 | return cnst; |
---|
| 148 | } |
---|
| 149 | |
---|
| 150 | Expression *translate(CompositeExprNode *ce){ |
---|
| 151 | return 0; |
---|
| 152 | } |
---|
| 153 | |
---|
| 154 | int translate(ParseNode *pn){ |
---|
| 155 | cout << "In translate - parsenode" << endl; |
---|
| 156 | |
---|
| 157 | return 0; |
---|
| 158 | } |
---|