|
| 1 | +#include <cstddef> |
| 2 | +#include <iostream> |
| 3 | +#include <fstream> |
| 4 | +#include <vector> |
| 5 | +#include <string> |
| 6 | +#include <system_error> |
| 7 | + |
| 8 | +#include "../compiler/parsing/tokenizer.h" |
| 9 | +#include "../compiler/parsing/parser.h" |
| 10 | +#include "../compiler/parsing/node.h" |
| 11 | +#include "../include/engine.h" |
| 12 | + |
| 13 | + |
| 14 | +namespace olcl |
| 15 | +{ |
| 16 | + namespace |
| 17 | + { |
| 18 | + void dump_ast(olcl::node & node, std::uint32_t depth, bool escape_next = true) |
| 19 | + { |
| 20 | + using nt = olcl::node_type; |
| 21 | + using nvt = olcl::symbol_type; |
| 22 | + |
| 23 | + switch (node.type) |
| 24 | + { |
| 25 | + case nt::t_module: |
| 26 | + for (auto binding : *node.module.bindings) |
| 27 | + { |
| 28 | + dump_ast(*binding, depth); |
| 29 | + std::cout << "\n"; |
| 30 | + } |
| 31 | + break; |
| 32 | + |
| 33 | + case nt::t_binding: |
| 34 | + std::cout << *node.binding.name << " : "; |
| 35 | + dump_ast(*node.binding.type, depth, false); |
| 36 | + std::cout << " = "; |
| 37 | + dump_ast(*node.binding.value, depth, false); |
| 38 | + std::cout << ";"; |
| 39 | + break; |
| 40 | + |
| 41 | + case nt::t_type: |
| 42 | + std::cout << *node.typeinfo.name; |
| 43 | + break; |
| 44 | + |
| 45 | + case nt::t_value: |
| 46 | + switch (node.value.type) |
| 47 | + { |
| 48 | + case nvt::t_character: |
| 49 | + std::cout << '\'' << (char) node.value.character; |
| 50 | + break; |
| 51 | + case nvt::t_number: |
| 52 | + std::cout << *node.value.number; |
| 53 | + break; |
| 54 | + case nvt::t_string: |
| 55 | + std::cout << *node.value.string; |
| 56 | + break; |
| 57 | + case nvt::t_variable: |
| 58 | + std::cout << *node.value.variable; |
| 59 | + break; |
| 60 | + case nvt::t_function: |
| 61 | + if (escape_next) std::cout << '('; |
| 62 | + dump_ast(*node.value.function.parameters, depth); |
| 63 | + std::cout << " -> "; |
| 64 | + dump_ast(*node.value.function.return_type, depth); |
| 65 | + std::cout << ' '; |
| 66 | + |
| 67 | + if (node.value.function.code) |
| 68 | + dump_ast(*node.value.function.code, depth); |
| 69 | + |
| 70 | + if (escape_next) std::cout << ')'; |
| 71 | + break; |
| 72 | + } |
| 73 | + break; |
| 74 | + |
| 75 | + case nt::t_binary_operation: |
| 76 | + if (escape_next) std::cout << '('; |
| 77 | + dump_ast(*node.binary_operation.left, depth); |
| 78 | + std::cout << ' ' << node.binary_operation.operation->representation << ' '; |
| 79 | + dump_ast(*node.binary_operation.right, depth); |
| 80 | + if (escape_next) std::cout << ')'; |
| 81 | + break; |
| 82 | + |
| 83 | + case nt::t_unary_operation: |
| 84 | + std::cout << '(' << node.unary_operation.operation->representation; |
| 85 | + dump_ast(*node.unary_operation.expression, depth); |
| 86 | + std::cout << ')'; |
| 87 | + break; |
| 88 | + |
| 89 | + case nt::t_function_call: |
| 90 | + dump_ast(*node.call.expression, depth); |
| 91 | + std::cout << '('; |
| 92 | + dump_ast(*node.call.arguments, depth, false); |
| 93 | + std::cout << ')'; |
| 94 | + break; |
| 95 | + |
| 96 | + case nt::t_block: |
| 97 | + std::cout << "{\n"; |
| 98 | + |
| 99 | + for (auto e : *node.block.code) |
| 100 | + { |
| 101 | + for (std::uint32_t i = 0; i < depth + 1; ++i) |
| 102 | + std::cout << " "; |
| 103 | + dump_ast(*e, depth + 1u, false); |
| 104 | + std::cout << ";\n"; |
| 105 | + } |
| 106 | + |
| 107 | + for (std::uint32_t i = 0; i < depth; ++i) |
| 108 | + std::cout << " "; |
| 109 | + std::cout << '}'; |
| 110 | + break; |
| 111 | + |
| 112 | + case nt::t_list: |
| 113 | + if (escape_next) std::cout << '('; |
| 114 | + for (auto e : *node.list.values) |
| 115 | + { |
| 116 | + dump_ast(*e, depth); |
| 117 | + std::cout << ", "; |
| 118 | + } |
| 119 | + if (escape_next) std::cout << ')'; |
| 120 | + break; |
| 121 | + |
| 122 | + case nt::t_stmt_return: |
| 123 | + std::cout << "return "; |
| 124 | + dump_ast(*node.stmt_return.expression, depth, false); |
| 125 | + break; |
| 126 | + |
| 127 | + case nt::t_stmt_if: |
| 128 | + std::cout << "if "; |
| 129 | + dump_ast(*node.stmt_if.condition, depth); |
| 130 | + std::cout << " then "; |
| 131 | + dump_ast(*node.stmt_if.on_true, depth); |
| 132 | + |
| 133 | + if (node.stmt_if.on_false) |
| 134 | + { |
| 135 | + std::cout << " else "; |
| 136 | + dump_ast(*node.stmt_if.on_false, depth); |
| 137 | + } |
| 138 | + |
| 139 | + break; |
| 140 | + } |
| 141 | + } |
| 142 | + } |
| 143 | + |
| 144 | + script * engine::loadScript(std::string filename) |
| 145 | + { |
| 146 | + using isb_char_iter = std::istreambuf_iterator<char>; |
| 147 | + |
| 148 | + std::string data; |
| 149 | + try |
| 150 | + { |
| 151 | + std::ifstream source; |
| 152 | + source.exceptions(std::ifstream::failbit | std::ifstream::badbit); |
| 153 | + |
| 154 | + source.open(filename); |
| 155 | + data = std::string(isb_char_iter(source), isb_char_iter()); |
| 156 | + } |
| 157 | + catch (std::exception &e) |
| 158 | + { |
| 159 | + std::cout << "Error #LC001:\n" |
| 160 | + << "File " << filename << " can not be opened" << std::endl; |
| 161 | + |
| 162 | + return nullptr; |
| 163 | + } |
| 164 | + |
| 165 | + try |
| 166 | + { |
| 167 | + auto tokens = olcl::parsing::tokenize(data); |
| 168 | + auto ast = olcl::parsing::parse(tokens); |
| 169 | + |
| 170 | + dump_ast(*ast, 0); |
| 171 | + auto s = new script(ast); |
| 172 | + |
| 173 | + std::cout << std::flush; |
| 174 | + return s; |
| 175 | + } |
| 176 | + catch (std::runtime_error &e) |
| 177 | + { |
| 178 | + // fail |
| 179 | + return nullptr; |
| 180 | + } |
| 181 | + } |
| 182 | +} |
0 commit comments