StringT Object::to_string(Frame * frame, Object * object) { if (object) { StringStreamT buffer; object->to_code(frame, buffer); return buffer.str(); } else { return "nil"; } }
InternalError::InternalError(const char * expression, const char * func, const char * file, unsigned line) throw () : _function(func), _expression (expression), _file (file), _line (line) { try { StringStreamT buffer; buffer << "Internal Error [" << _file << ":" << _line << "] [" << _function << "]: " << _expression; _what = buffer.str(); } catch (...) { _what = "Undefined Internal Error"; } }
Ref<Object> Tracer::dump(Frame * frame) { Tracer * tracer = NULL; frame->extract()(tracer, "self"); StringStreamT buffer; tracer->dump(frame, buffer); return new(frame) String(buffer.str()); }
Value * CompiledFunction::code(Frame * frame) { CompiledFunction * function = NULL; frame->extract()(function); StringStreamT buffer; llvm::raw_os_ostream llvmBuffer(buffer); function->compiledValue(frame)->print(llvmBuffer); llvmBuffer.flush(); return new String(buffer.str()); }
StringT unescape_string (const StringT & value) { StringStreamT buffer; StringT::const_iterator i = value.begin(), end = value.end(); // Skip enclosing quotes ++i; --end; for (; i < end; ++i) { if (*i == '\\') { ++i; switch (*i) { case 't': buffer << '\t'; continue; case 'r': buffer << '\r'; continue; case 'n': buffer << '\n'; continue; case '\\': buffer << '\\'; continue; case '"': buffer << '"'; continue; case '\'': buffer << '\''; continue; case 'x': if ((end - i) >= 2) { StringT::value_type value = Math::convert_to_digit(*(++i)) << 4; value |= Math::convert_to_digit(*(++i)); buffer << (StringT::value_type)value; continue; } else { break; } case '.': continue; } throw std::runtime_error("Could not parse string escape!"); } else { buffer << *i; } } return buffer.str(); }
int main (int argc, const char * argv[]) { Time start; using namespace Kai; GC_init(); int result = 0; Terminal console(STDIN_FILENO); Table * context = buildContext(); BasicEditor editor(context); if (argc == 2) { SourceCode code(argv[1]); runCode(context, code, result); } else if (console.isTTY()) { // Running interactively TerminalEditor terminalEditor("kai> "); StringStreamT buffer; std::cerr << "Startup time = " << (Time() - start) << std::endl; while (terminalEditor.readInput(buffer, editor)) { Value * value; SourceCode currentLine("<stdin>", buffer.str()); value = runCode(context, currentLine, result); std::cout << Value::toString(value) << std::endl; buffer.str(""); } } else { StringStreamT buffer; buffer << std::cin.rdbuf(); SourceCode code ("<stdin>", buffer.str()); runCode(context, code, result); } // Dump trace statistics. std::cout << std::endl; Tracer::globalTracer()->dump(std::cout); return result; }
StringT escape_string (const StringT & value) { StringStreamT buffer; StringT::const_iterator i = value.begin(), end = value.end(); buffer << '"'; for (; i != end; ++i) { if (*i == '"') { buffer << "\\\""; } else { buffer << *i; } } buffer << '"'; return buffer.str(); }
int main (int argc, const char * argv[]) { Time start; using namespace Kai; signal(SIGSEGV, signal_hang); int result = 0; Ref<Frame> context = build_context(); // Used later for setting up system arguments: Ref<System> system = context->lookup(context->sym("system")); Ref<Terminal> terminal = context->lookup(context->sym("terminal")); Ref<SourceCode> code; if (argc >= 2) { // First argument is a file path system->set_arguments(argc - 1, argv + 1); code = new(context) SourceCode(argv[1]); } else { // Source code expected from stdin system->set_arguments(argc, argv); StringStreamT buffer; buffer << std::cin.rdbuf(); code = new(context) SourceCode("<stdin>", buffer.str()); } try { run_code(context, code, result, terminal); } catch (std::exception & error) { std::cerr << "Fatal Error: " << error.what() << std::endl; } #ifdef KAI_TRACE // Dump trace statistics. std::cout << std::endl; Tracer::global_tracer()->dump(std::cout); #endif return result; }
void Tracer::dump(Frame * frame, std::ostream & buffer) { buffer << "Tracer Statistics" << std::endl; for (StatisticsMapT::const_iterator i = _statistics.begin(); i != _statistics.end(); ++i) { const Object * function = i->first; const Statistics & stats = i->second; buffer << "\t"; StringStreamT code; function->to_code(frame, code); buffer << code.str(); buffer << "\t\t" << stats.total_time; buffer << "\t" << stats.count; buffer << std::endl; } }
Ref<Object> String::join(Frame * frame) { StringStreamT buffer; String * self; Cell * next = frame->extract()(self, "self"); buffer << self->value(); while (next != NULL) { String * head = next->head().as<String>(); if (head) { buffer << head->value(); } else { buffer << Object::to_string(frame, next->head()); } next = next->tail().as<Cell>(); } return new(frame) String(buffer.str()); }
AudioError::AudioError(ErrorNumberT error_number, StringT error_description, StringT error_target) : _error_number(error_number) { StringStreamT buffer; buffer << "Audio Error #" << error_number << ": " << error_description << "(" << error_target << ")"; _message = buffer.str(); }