kaleidoscope_ast.cpp (1127B)
1 2 #include "kaleidoscope_ast.hpp" 3 4 #include <sstream> 5 6 namespace kal { 7 8 std::string 9 to_string(const Number& n) 10 { 11 std::ostringstream oss; 12 13 oss << "(number " << n.value << ')'; 14 return oss.str(); 15 } 16 17 std::string 18 to_string(const Variable& v) 19 { 20 std::ostringstream oss; 21 22 oss << "(variable " << v.name << ')'; 23 return oss.str(); 24 } 25 26 std::string 27 to_string(const BinaryOp& op) 28 { 29 std::ostringstream oss; 30 31 oss << "(binop '" << op.op << "' " << to_string(op.lhs) << ' ' 32 << to_string(op.rhs) << ')'; 33 return oss.str(); 34 } 35 36 std::string 37 to_string(const Call& c) 38 { 39 std::ostringstream oss; 40 41 oss << "(call " << c.callee; 42 for (const auto& arg : c.args) 43 oss << ' ' << to_string(arg); 44 oss << ')'; 45 46 return oss.str(); 47 } 48 49 std::string 50 to_string(const Prototype& p) 51 { 52 std::ostringstream oss; 53 54 oss << "(prototype " << p.name; 55 for (const auto& param : p.params) 56 oss << ' ' << param; 57 oss << ')'; 58 59 return oss.str(); 60 } 61 62 std::string 63 to_string(const Function& f) 64 { 65 std::ostringstream oss; 66 67 oss << "(function " << to_string(f.proto) << ' ' << to_string(f.body) << ')'; 68 return oss.str(); 69 } 70 71 } // namespace kal