| 110 | } |
| 111 | |
| 112 | void Extensions::generateFunctionCode(int keyword, std::vector<Interpreter::Type_Code>& code, Literals& literals, |
| 113 | const std::string& id, int optionalArguments) const |
| 114 | { |
| 115 | assert(optionalArguments >= 0); |
| 116 | |
| 117 | auto iter = mFunctions.find(keyword); |
| 118 | if (iter == mFunctions.end()) |
| 119 | throw std::logic_error("unknown custom function keyword"); |
| 120 | |
| 121 | if (optionalArguments && iter->second.mSegment != 3) |
| 122 | throw std::logic_error("functions with optional arguments must be placed into segment 3"); |
| 123 | |
| 124 | if (!id.empty()) |
| 125 | { |
| 126 | if (iter->second.mCodeExplicit == -1) |
| 127 | throw std::logic_error("explicit references not supported"); |
| 128 | |
| 129 | int index = literals.addString(id); |
| 130 | Generator::pushInt(code, literals, index); |
| 131 | } |
| 132 | |
| 133 | switch (iter->second.mSegment) |
| 134 | { |
| 135 | case 3: |
| 136 | |
| 137 | if (optionalArguments >= 256) |
| 138 | throw std::logic_error("number of optional arguments is too large for segment 3"); |
| 139 | |
| 140 | code.push_back(Generator::segment3( |
| 141 | id.empty() ? iter->second.mCode : iter->second.mCodeExplicit, optionalArguments)); |
| 142 | |
| 143 | break; |
| 144 | |
| 145 | case 5: |
| 146 | |
| 147 | code.push_back(Generator::segment5(id.empty() ? iter->second.mCode : iter->second.mCodeExplicit)); |
| 148 | |
| 149 | break; |
| 150 | |
| 151 | default: |
| 152 | |
| 153 | throw std::logic_error("unsupported code segment"); |
| 154 | } |
| 155 | } |
| 156 | |
| 157 | void Extensions::generateInstructionCode(int keyword, std::vector<Interpreter::Type_Code>& code, Literals& literals, |
| 158 | const std::string& id, int optionalArguments) const |