// yaks_main.mycpp.cc: translated from Python by mycpp // #include "_gen/yaks/yaks_main.mycpp.h" #include "yaks/preamble.h" // BEGIN mycpp output #include "mycpp/runtime.h" GLOBAL_STR(str0, "("); GLOBAL_STR(str1, ")"); GLOBAL_STR(str2, "_"); GLOBAL_STR(str3, "T"); GLOBAL_STR(str4, "F"); GLOBAL_STR(str5, "\n\n \n oil AST\n \n \n \n
\n");
GLOBAL_STR(str6, "\n    
\n \n\n "); GLOBAL_STR(str7, "n"); GLOBAL_STR(str8, "s"); GLOBAL_STR(str9, "o"); GLOBAL_STR(str10, "o"); GLOBAL_STR(str11, "o"); GLOBAL_STR(str12, ""); GLOBAL_STR(str13, ""); GLOBAL_STR(str14, " "); GLOBAL_STR(str15, "\n"); GLOBAL_STR(str16, " "); GLOBAL_STR(str17, "]"); GLOBAL_STR(str18, " "); GLOBAL_STR(str19, " "); GLOBAL_STR(str20, "\n"); GLOBAL_STR(str21, "\n"); GLOBAL_STR(str22, " "); GLOBAL_STR(str23, "%s%s: ["); GLOBAL_STR(str24, "\n"); GLOBAL_STR(str25, "\n"); GLOBAL_STR(str26, "%s]"); GLOBAL_STR(str27, "%s%s: "); GLOBAL_STR(str28, "\n"); GLOBAL_STR(str29, "\n"); GLOBAL_STR(str30, " "); GLOBAL_STR(str31, "UNTYPED any"); GLOBAL_STR(str32, "...0x%s"); GLOBAL_STR(str33, " "); GLOBAL_STR(str34, " "); GLOBAL_STR(str35, " %s:"); GLOBAL_STR(str36, "UNTYPED any"); GLOBAL_STR(str37, "["); GLOBAL_STR(str38, " "); GLOBAL_STR(str39, "]"); GLOBAL_STR(str40, "...0x%s"); GLOBAL_STR(str41, "\u001b[0;0m"); GLOBAL_STR(str42, "\u001b[1m"); GLOBAL_STR(str43, "\u001b[4m"); GLOBAL_STR(str44, "\u001b[7m"); GLOBAL_STR(str45, "\u001b[31m"); GLOBAL_STR(str46, "\u001b[32m"); GLOBAL_STR(str47, "\u001b[33m"); GLOBAL_STR(str48, "\u001b[34m"); GLOBAL_STR(str49, "\u001b[35m"); GLOBAL_STR(str50, "\u001b[36m"); GLOBAL_STR(str51, "\u001b[37m"); GLOBAL_STR(str52, "<%s %r>"); GLOBAL_STR(str53, "code"); GLOBAL_STR(str54, "message"); GLOBAL_STR(str55, "%s, got %s"); GLOBAL_STR(str56, " (line %d, offset %d-%d: %r)"); GLOBAL_STR(str57, ""); GLOBAL_STR(str58, " 0x%s"); GLOBAL_STR(str59, ""); GLOBAL_STR(str60, "\n"); GLOBAL_STR(str61, "\n"); GLOBAL_STR(str62, " "); GLOBAL_STR(str63, "[]"); GLOBAL_STR(str64, "["); GLOBAL_STR(str65, ","); GLOBAL_STR(str66, "]"); GLOBAL_STR(str67, "{}"); GLOBAL_STR(str68, "{"); GLOBAL_STR(str69, ","); GLOBAL_STR(str70, ":"); GLOBAL_STR(str71, "}"); GLOBAL_STR(str72, "{"); GLOBAL_STR(str73, "\"type\":"); GLOBAL_STR(str74, "\"value\":"); GLOBAL_STR(str75, "}"); GLOBAL_STR(str76, "\"BashArray\","); GLOBAL_STR(str77, "{}"); GLOBAL_STR(str78, "{"); GLOBAL_STR(str79, ","); GLOBAL_STR(str80, ":"); GLOBAL_STR(str81, "}"); GLOBAL_STR(str82, "\"BashAssoc\","); GLOBAL_STR(str83, "{}"); GLOBAL_STR(str84, "{"); GLOBAL_STR(str85, ","); GLOBAL_STR(str86, ":"); GLOBAL_STR(str87, "}"); GLOBAL_STR(str88, "null"); GLOBAL_STR(str89, "true"); GLOBAL_STR(str90, "false"); GLOBAL_STR(str91, "null"); GLOBAL_STR(str92, "INFINITY"); GLOBAL_STR(str93, "-"); GLOBAL_STR(str94, "null"); GLOBAL_STR(str95, "NAN"); GLOBAL_STR(str96, "[ -->%s ]"); GLOBAL_STR(str97, "Can't encode List%s in object cycle"); GLOBAL_STR(str98, "{ -->%s }"); GLOBAL_STR(str99, "Can't encode Dict%s in object cycle"); GLOBAL_STR(str100, "<%s%s>"); GLOBAL_STR(str101, "Can't serialize object of type %s"); GLOBAL_STR(str102, "Single quotes aren't part of JSON; you may want 'json8 read'"); GLOBAL_STR(str103, "Comments aren't part of JSON; you may want 'json8 read'"); GLOBAL_STR(str104, "Pure JSON does not accept j\"\" prefix"); GLOBAL_STR(str105, "Invalid UTF-8 in %s string literal"); GLOBAL_STR(str106, "J8 Lines can't have unescaped ASCII control chars"); GLOBAL_STR(str107, "Unexpected EOF while lexing %s string"); GLOBAL_STR(str108, "Bad backslash escape in %s string"); GLOBAL_STR(str109, "%s strings can't have unescaped ASCII control chars"); GLOBAL_STR(str110, "Invalid UTF-8 in %s string literal"); GLOBAL_STR(str111, "Code point can't be greater than U+10ffff"); GLOBAL_STR(str112, "\\u{%s} escape is illegal because it's in the surrogate range"); GLOBAL_STR(str113, "\\y%s escapes not allowed in u'' strings"); GLOBAL_STR(str114, "J8"); GLOBAL_STR(str115, "JSON"); GLOBAL_STR(str116, ""); GLOBAL_STR(str117, "Expected %s, got %s"); GLOBAL_STR(str118, "t"); GLOBAL_STR(str119, "Integer is too big"); GLOBAL_STR(str120, "Unexpected EOF while parsing %s"); GLOBAL_STR(str121, "Invalid token while parsing %s: %s"); GLOBAL_STR(str122, "Got %d bytes of unexpected trailing input"); GLOBAL_STR(str123, "t"); GLOBAL_STR(str124, "Unexpected EOF while parsing %s"); GLOBAL_STR(str125, "Invalid token while parsing %s: %s"); GLOBAL_STR(str126, "Unexpected trailing input"); GLOBAL_STR(str127, "%s tok_id %s %d-%d"); GLOBAL_STR(str128, "Unexpected text after J8 Line (%s)"); GLOBAL_STR(str129, "Unexpected trailing input in J8 Lines"); GLOBAL_STR(str130, "&"); GLOBAL_STR(str131, "&"); GLOBAL_STR(str132, "<"); GLOBAL_STR(str133, "<"); GLOBAL_STR(str134, ">"); GLOBAL_STR(str135, ">"); GLOBAL_STR(str136, "int"); GLOBAL_STR(str137, "BigStr*"); GLOBAL_STR(str138, "List<"); GLOBAL_STR(str139, ">*"); GLOBAL_STR(str140, " %s"); GLOBAL_STR(str141, "%d"); GLOBAL_STR(str142, " return "); GLOBAL_STR(str143, ";\n"); GLOBAL_STR(str144, " %s("); GLOBAL_STR(str145, ", "); GLOBAL_STR(str146, ") {\n"); GLOBAL_STR(str147, "}\n"); GLOBAL_STR(str148, "#include \"mycpp/runtime.h\"\n"); GLOBAL_STR(str149, "namespace %s {\n"); GLOBAL_STR(str150, "} // namespace %s\n"); GLOBAL_STR(str151, "Expected Symbol, got %s"); GLOBAL_STR(str152, "Expected List, got %s"); GLOBAL_STR(str153, "path"); GLOBAL_STR(str154, "chunk"); GLOBAL_STR(str155, "Bool"); GLOBAL_STR(str156, "Int"); GLOBAL_STR(str157, "Str"); GLOBAL_STR(str158, "List"); GLOBAL_STR(str159, "Dict"); GLOBAL_STR(str160, "Signature should have 3 items: => params return"); GLOBAL_STR(str161, "=>"); GLOBAL_STR(str162, "Signature should start with =>"); GLOBAL_STR(str163, "Statement shouldn't be empty"); GLOBAL_STR(str164, "var"); GLOBAL_STR(str165, "setvar"); GLOBAL_STR(str166, "call"); GLOBAL_STR(str167, "return"); GLOBAL_STR(str168, "break"); GLOBAL_STR(str169, "continue"); GLOBAL_STR(str170, "Invalid statment %r"); GLOBAL_STR(str171, "Module should have at least 2 items, got %d"); GLOBAL_STR(str172, "Module shouldn't be empty"); GLOBAL_STR(str173, "global"); GLOBAL_STR(str174, "func"); GLOBAL_STR(str175, "class"); GLOBAL_STR(str176, "Invalid module def %r"); GLOBAL_STR(str177, "foo"); GLOBAL_STR(str178, "Action required"); GLOBAL_STR(str179, "cpp"); GLOBAL_STR(str180, ""); GLOBAL_STR(str181, "\n"); GLOBAL_STR(str182, "\n"); GLOBAL_STR(str183, "check"); GLOBAL_STR(str184, "hi"); GLOBAL_STR(str185, "\n"); GLOBAL_STR(str186, "Invalid action %r"); namespace runtime { // forward declare class TraversalState; } // forward declare namespace runtime namespace format { // forward declare class ColorOutput; class TextOutput; class HtmlOutput; class AnsiOutput; class _PrettyPrinter; } // forward declare namespace format namespace ansi { // forward declare } // forward declare namespace ansi namespace error { // forward declare class _ErrorWithLocation; class Usage; class Parse; class FailGlob; class RedirectEval; class FatalRuntime; class Strict; class ErrExit; class Expr; class Structured; class AssertionErr; class TypeErrVerbose; class TypeErr; class Runtime; class Decode; class Encode; } // forward declare namespace error namespace num { // forward declare } // forward declare namespace num namespace j8 { // forward declare class InstancePrinter; class PrettyPrinter; class LexerDecoder; class _Parser; class Parser; class Nil8Parser; class J8LinesParser; } // forward declare namespace j8 namespace j8_lite { // forward declare } // forward declare namespace j8_lite namespace cgi { // forward declare } // forward declare namespace cgi namespace gen_cpp { // forward declare } // forward declare namespace gen_cpp namespace transform { // forward declare } // forward declare namespace transform namespace yaks_main { // forward declare } // forward declare namespace yaks_main namespace runtime { // declare using hnode_asdl::hnode; extern int NO_SPID; hnode::Record* NewRecord(BigStr* node_type); hnode::Leaf* NewLeaf(BigStr* s, hnode_asdl::color_t e_color); class TraversalState { public: TraversalState(); Dict* seen; Dict* ref_count; static constexpr ObjHeader obj_header() { return ObjHeader::ClassScanned(2, sizeof(TraversalState)); } DISALLOW_COPY_AND_ASSIGN(TraversalState) }; extern BigStr* TRUE_STR; extern BigStr* FALSE_STR; } // declare namespace runtime namespace format { // declare using hnode_asdl::hnode; format::ColorOutput* DetectConsoleOutput(mylib::Writer* f); class ColorOutput { public: ColorOutput(mylib::Writer* f); virtual format::ColorOutput* NewTempBuffer(); virtual void FileHeader(); virtual void FileFooter(); virtual void PushColor(hnode_asdl::color_t e_color); virtual void PopColor(); virtual void write(BigStr* s); void WriteRaw(Tuple2* raw); int NumChars(); Tuple2 GetRaw(); mylib::Writer* f; int num_chars; static constexpr uint32_t field_mask() { return maskbit(offsetof(ColorOutput, f)); } static constexpr ObjHeader obj_header() { return ObjHeader::ClassFixed(field_mask(), sizeof(ColorOutput)); } DISALLOW_COPY_AND_ASSIGN(ColorOutput) }; class TextOutput : public ::format::ColorOutput { public: TextOutput(mylib::Writer* f); virtual format::TextOutput* NewTempBuffer(); virtual void PushColor(hnode_asdl::color_t e_color); virtual void PopColor(); static constexpr uint32_t field_mask() { return ::format::ColorOutput::field_mask(); } static constexpr ObjHeader obj_header() { return ObjHeader::ClassFixed(field_mask(), sizeof(TextOutput)); } DISALLOW_COPY_AND_ASSIGN(TextOutput) }; class HtmlOutput : public ::format::ColorOutput { public: HtmlOutput(mylib::Writer* f); virtual format::HtmlOutput* NewTempBuffer(); virtual void FileHeader(); virtual void FileFooter(); virtual void PushColor(hnode_asdl::color_t e_color); virtual void PopColor(); virtual void write(BigStr* s); static constexpr uint32_t field_mask() { return ::format::ColorOutput::field_mask(); } static constexpr ObjHeader obj_header() { return ObjHeader::ClassFixed(field_mask(), sizeof(HtmlOutput)); } DISALLOW_COPY_AND_ASSIGN(HtmlOutput) }; class AnsiOutput : public ::format::ColorOutput { public: AnsiOutput(mylib::Writer* f); virtual format::AnsiOutput* NewTempBuffer(); virtual void PushColor(hnode_asdl::color_t e_color); virtual void PopColor(); static constexpr uint32_t field_mask() { return ::format::ColorOutput::field_mask(); } static constexpr ObjHeader obj_header() { return ObjHeader::ClassFixed(field_mask(), sizeof(AnsiOutput)); } DISALLOW_COPY_AND_ASSIGN(AnsiOutput) }; extern int INDENT; class _PrettyPrinter { public: _PrettyPrinter(int max_col); bool _PrintWrappedArray(List* array, int prefix_len, format::ColorOutput* f, int indent); bool _PrintWholeArray(List* array, int prefix_len, format::ColorOutput* f, int indent); void _PrintRecord(hnode::Record* node, format::ColorOutput* f, int indent); void PrintNode(hnode_asdl::hnode_t* node, format::ColorOutput* f, int indent); int max_col; static constexpr ObjHeader obj_header() { return ObjHeader::ClassScanned(0, sizeof(_PrettyPrinter)); } DISALLOW_COPY_AND_ASSIGN(_PrettyPrinter) }; bool _TrySingleLineObj(hnode::Record* node, format::ColorOutput* f, int max_chars); bool _TrySingleLine(hnode_asdl::hnode_t* node, format::ColorOutput* f, int max_chars); void PrintTree(hnode_asdl::hnode_t* node, format::ColorOutput* f); } // declare namespace format namespace ansi { // declare extern BigStr* RESET; extern BigStr* BOLD; extern BigStr* UNDERLINE; extern BigStr* REVERSE; extern BigStr* RED; extern BigStr* GREEN; extern BigStr* YELLOW; extern BigStr* BLUE; extern BigStr* MAGENTA; extern BigStr* CYAN; extern BigStr* WHITE; } // declare namespace ansi namespace error { // declare using syntax_asdl::loc; BigStr* _ValType(value_asdl::value_t* val); class _ErrorWithLocation { public: _ErrorWithLocation(BigStr* msg, syntax_asdl::loc_t* location); bool HasLocation(); BigStr* UserErrorString(); syntax_asdl::loc_t* location; BigStr* msg; static constexpr uint32_t field_mask() { return maskbit(offsetof(_ErrorWithLocation, location)) | maskbit(offsetof(_ErrorWithLocation, msg)); } static constexpr ObjHeader obj_header() { return ObjHeader::ClassFixed(field_mask(), sizeof(_ErrorWithLocation)); } DISALLOW_COPY_AND_ASSIGN(_ErrorWithLocation) }; class Usage : public ::error::_ErrorWithLocation { public: Usage(BigStr* msg, syntax_asdl::loc_t* location); static constexpr uint32_t field_mask() { return ::error::_ErrorWithLocation::field_mask(); } static constexpr ObjHeader obj_header() { return ObjHeader::ClassFixed(field_mask(), sizeof(Usage)); } DISALLOW_COPY_AND_ASSIGN(Usage) }; class Parse : public ::error::_ErrorWithLocation { public: Parse(BigStr* msg, syntax_asdl::loc_t* location); static constexpr uint32_t field_mask() { return ::error::_ErrorWithLocation::field_mask(); } static constexpr ObjHeader obj_header() { return ObjHeader::ClassFixed(field_mask(), sizeof(Parse)); } DISALLOW_COPY_AND_ASSIGN(Parse) }; class FailGlob : public ::error::_ErrorWithLocation { public: FailGlob(BigStr* msg, syntax_asdl::loc_t* location); static constexpr uint32_t field_mask() { return ::error::_ErrorWithLocation::field_mask(); } static constexpr ObjHeader obj_header() { return ObjHeader::ClassFixed(field_mask(), sizeof(FailGlob)); } DISALLOW_COPY_AND_ASSIGN(FailGlob) }; class RedirectEval : public ::error::_ErrorWithLocation { public: RedirectEval(BigStr* msg, syntax_asdl::loc_t* location); static constexpr uint32_t field_mask() { return ::error::_ErrorWithLocation::field_mask(); } static constexpr ObjHeader obj_header() { return ObjHeader::ClassFixed(field_mask(), sizeof(RedirectEval)); } DISALLOW_COPY_AND_ASSIGN(RedirectEval) }; class FatalRuntime : public ::error::_ErrorWithLocation { public: FatalRuntime(int exit_status, BigStr* msg, syntax_asdl::loc_t* location); int ExitStatus(); int exit_status; static constexpr uint32_t field_mask() { return ::error::_ErrorWithLocation::field_mask(); } static constexpr ObjHeader obj_header() { return ObjHeader::ClassFixed(field_mask(), sizeof(FatalRuntime)); } DISALLOW_COPY_AND_ASSIGN(FatalRuntime) }; class Strict : public ::error::FatalRuntime { public: Strict(BigStr* msg, syntax_asdl::loc_t* location); static constexpr uint32_t field_mask() { return ::error::FatalRuntime::field_mask(); } static constexpr ObjHeader obj_header() { return ObjHeader::ClassFixed(field_mask(), sizeof(Strict)); } DISALLOW_COPY_AND_ASSIGN(Strict) }; class ErrExit : public ::error::FatalRuntime { public: ErrExit(int exit_status, BigStr* msg, syntax_asdl::loc_t* location, bool show_code = false); bool show_code; static constexpr uint32_t field_mask() { return ::error::FatalRuntime::field_mask(); } static constexpr ObjHeader obj_header() { return ObjHeader::ClassFixed(field_mask(), sizeof(ErrExit)); } DISALLOW_COPY_AND_ASSIGN(ErrExit) }; class Expr : public ::error::FatalRuntime { public: Expr(BigStr* msg, syntax_asdl::loc_t* location); static constexpr uint32_t field_mask() { return ::error::FatalRuntime::field_mask(); } static constexpr ObjHeader obj_header() { return ObjHeader::ClassFixed(field_mask(), sizeof(Expr)); } DISALLOW_COPY_AND_ASSIGN(Expr) }; class Structured : public ::error::FatalRuntime { public: Structured(int status, BigStr* msg, syntax_asdl::loc_t* location, Dict* properties = nullptr); value::Dict* ToDict(); Dict* properties; static constexpr uint32_t field_mask() { return ::error::FatalRuntime::field_mask() | maskbit(offsetof(Structured, properties)); } static constexpr ObjHeader obj_header() { return ObjHeader::ClassFixed(field_mask(), sizeof(Structured)); } DISALLOW_COPY_AND_ASSIGN(Structured) }; class AssertionErr : public ::error::Expr { public: AssertionErr(BigStr* msg, syntax_asdl::loc_t* location); static constexpr uint32_t field_mask() { return ::error::Expr::field_mask(); } static constexpr ObjHeader obj_header() { return ObjHeader::ClassFixed(field_mask(), sizeof(AssertionErr)); } DISALLOW_COPY_AND_ASSIGN(AssertionErr) }; class TypeErrVerbose : public ::error::Expr { public: TypeErrVerbose(BigStr* msg, syntax_asdl::loc_t* location); static constexpr uint32_t field_mask() { return ::error::Expr::field_mask(); } static constexpr ObjHeader obj_header() { return ObjHeader::ClassFixed(field_mask(), sizeof(TypeErrVerbose)); } DISALLOW_COPY_AND_ASSIGN(TypeErrVerbose) }; class TypeErr : public ::error::TypeErrVerbose { public: TypeErr(value_asdl::value_t* actual_val, BigStr* msg, syntax_asdl::loc_t* location); static constexpr uint32_t field_mask() { return ::error::TypeErrVerbose::field_mask(); } static constexpr ObjHeader obj_header() { return ObjHeader::ClassFixed(field_mask(), sizeof(TypeErr)); } DISALLOW_COPY_AND_ASSIGN(TypeErr) }; class Runtime { public: Runtime(BigStr* msg); BigStr* UserErrorString(); BigStr* msg; static constexpr ObjHeader obj_header() { return ObjHeader::ClassScanned(1, sizeof(Runtime)); } DISALLOW_COPY_AND_ASSIGN(Runtime) }; class Decode { public: Decode(BigStr* msg, BigStr* s, int start_pos, int end_pos, int line_num); BigStr* Message(); BigStr* __str__(); BigStr* msg; BigStr* s; int start_pos; int end_pos; int line_num; static constexpr ObjHeader obj_header() { return ObjHeader::ClassScanned(2, sizeof(Decode)); } DISALLOW_COPY_AND_ASSIGN(Decode) }; class Encode { public: Encode(BigStr* msg); BigStr* Message(); BigStr* msg; static constexpr ObjHeader obj_header() { return ObjHeader::ClassScanned(1, sizeof(Encode)); } DISALLOW_COPY_AND_ASSIGN(Encode) }; [[noreturn]] void e_usage(BigStr* msg, syntax_asdl::loc_t* location); [[noreturn]] void e_strict(BigStr* msg, syntax_asdl::loc_t* location); [[noreturn]] void p_die(BigStr* msg, syntax_asdl::loc_t* location); [[noreturn]] void e_die(BigStr* msg, syntax_asdl::loc_t* location = nullptr); [[noreturn]] void e_die_status(int status, BigStr* msg, syntax_asdl::loc_t* location = nullptr); } // declare namespace error namespace num { // declare value::Int* ToBig(int i); mops::BigInt Exponent(mops::BigInt x, mops::BigInt y); int Exponent2(int x, int y); mops::BigInt IntDivide(mops::BigInt x, mops::BigInt y); int IntDivide2(int x, int y); mops::BigInt IntRemainder(mops::BigInt x, mops::BigInt y); int IntRemainder2(int x, int y); } // declare namespace num namespace j8 { // declare using id_kind_asdl::Id; BigStr* ValType(value_asdl::value_t* val); int ValueId(value_asdl::value_t* val); BigStr* ValueIdString(value_asdl::value_t* val); BigStr* Utf8Encode(int code); extern int SHOW_CYCLES; extern int SHOW_NON_DATA; extern int LOSSY_JSON; extern int INF_NAN_ARE_NULL; void _Print(value_asdl::value_t* val, mylib::BufWriter* buf, int indent, int options = 0); void PrintMessage(value_asdl::value_t* val, mylib::BufWriter* buf, int indent); void PrintJsonMessage(value_asdl::value_t* val, mylib::BufWriter* buf, int indent); void PrintLine(value_asdl::value_t* val, mylib::Writer* f); void EncodeString(BigStr* s, mylib::BufWriter* buf, bool unquoted_ok = false); BigStr* MaybeEncodeString(BigStr* s); BigStr* MaybeEncodeJsonString(BigStr* s); extern int UNSEEN; extern int EXPLORING; extern int FINISHED; class InstancePrinter { public: InstancePrinter(mylib::BufWriter* buf, int indent, int options); void _ItemIndent(int level); void _BracketIndent(int level); void _MaybeNewline(); void _MaybeSpace(); void _PrintList(value::List* val, int level); void _PrintDict(value::Dict* val, int level); void _PrintBashPrefix(BigStr* type_str, int level); void _PrintBashSuffix(int level); void _PrintBashArray(value::BashArray* val, int level); void _PrintBashAssoc(value::BashAssoc* val, int level); void Print(value_asdl::value_t* val, int level = 0); mylib::BufWriter* buf; Dict* visited; int indent; int options; static constexpr ObjHeader obj_header() { return ObjHeader::ClassScanned(2, sizeof(InstancePrinter)); } DISALLOW_COPY_AND_ASSIGN(InstancePrinter) }; class PrettyPrinter { public: PrettyPrinter(int max_col); void PrettyTree(value_asdl::value_t* val, format::ColorOutput* f); void Print(value_asdl::value_t* val, mylib::BufWriter* buf); Dict* ref_count; int max_col; static constexpr ObjHeader obj_header() { return ObjHeader::ClassScanned(1, sizeof(PrettyPrinter)); } DISALLOW_COPY_AND_ASSIGN(PrettyPrinter) }; class LexerDecoder { public: LexerDecoder(BigStr* s, bool is_j8, BigStr* lang_str); error::Decode* _Error(BigStr* msg, int end_pos); Tuple3 Next(); Tuple3 NextForLines(); Tuple3 _DecodeString(int left_id, int str_pos); BigStr* s; BigStr* lang_str; mylib::BufWriter* decoded; bool is_j8; int pos; int cur_line_num; static constexpr ObjHeader obj_header() { return ObjHeader::ClassScanned(3, sizeof(LexerDecoder)); } DISALLOW_COPY_AND_ASSIGN(LexerDecoder) }; class _Parser { public: _Parser(BigStr* s, bool is_j8); void _Next(); void _Eat(int tok_id); void _NextForLines(); error::Decode* _ParseError(BigStr* msg); BigStr* decoded; int end_pos; bool is_j8; BigStr* lang_str; j8::LexerDecoder* lexer; BigStr* s; int start_pos; int tok_id; static constexpr uint32_t field_mask() { return maskbit(offsetof(_Parser, decoded)) | maskbit(offsetof(_Parser, lang_str)) | maskbit(offsetof(_Parser, lexer)) | maskbit(offsetof(_Parser, s)); } static constexpr ObjHeader obj_header() { return ObjHeader::ClassFixed(field_mask(), sizeof(_Parser)); } DISALLOW_COPY_AND_ASSIGN(_Parser) }; class Parser : public ::j8::_Parser { public: Parser(BigStr* s, bool is_j8); Tuple2 _ParsePair(); value_asdl::value_t* _ParseDict(); value_asdl::value_t* _ParseList(); value_asdl::value_t* _ParseValue(); value_asdl::value_t* ParseValue(); static constexpr uint32_t field_mask() { return ::j8::_Parser::field_mask(); } static constexpr ObjHeader obj_header() { return ObjHeader::ClassFixed(field_mask(), sizeof(Parser)); } DISALLOW_COPY_AND_ASSIGN(Parser) }; class Nil8Parser : public ::j8::_Parser { public: Nil8Parser(BigStr* s, bool is_j8); nil8_asdl::nvalue_t* _ParseRecord(); nil8_asdl::nvalue_t* _ParseList8(); nil8_asdl::nvalue_t* _ParseNil8(); nil8_asdl::nvalue_t* ParseNil8(); static constexpr uint32_t field_mask() { return ::j8::_Parser::field_mask(); } static constexpr ObjHeader obj_header() { return ObjHeader::ClassFixed(field_mask(), sizeof(Nil8Parser)); } DISALLOW_COPY_AND_ASSIGN(Nil8Parser) }; class J8LinesParser : public ::j8::_Parser { public: J8LinesParser(BigStr* s); void _Show(BigStr* s); void _ParseLine(List* out); List* Parse(); static constexpr uint32_t field_mask() { return ::j8::_Parser::field_mask(); } static constexpr ObjHeader obj_header() { return ObjHeader::ClassFixed(field_mask(), sizeof(J8LinesParser)); } DISALLOW_COPY_AND_ASSIGN(J8LinesParser) }; List* SplitJ8Lines(BigStr* s); } // declare namespace j8 namespace j8_lite { // declare BigStr* EncodeString(BigStr* s, bool unquoted_ok = false); BigStr* YshEncodeString(BigStr* s); BigStr* MaybeShellEncode(BigStr* s); BigStr* ShellEncode(BigStr* s); BigStr* YshEncode(BigStr* s, bool unquoted_ok = false); } // declare namespace j8_lite namespace cgi { // declare BigStr* escape(BigStr* s); } // declare namespace cgi namespace gen_cpp { // declare void GenType(yaks_asdl::ktype_t* typ, mylib::Writer* f); void GenParam(yaks_asdl::NameType* p, mylib::Writer* f); void GenExpr(yaks_asdl::kexpr_t* expr, mylib::Writer* f); void GenStatement(yaks_asdl::stmt_t* st, mylib::Writer* f); void GenFunction(mod_def::Func* func, mylib::Writer* f); void GenCpp(yaks_asdl::Program* prog, mylib::Writer* f); } // declare namespace gen_cpp namespace transform { // declare BigStr* MustBeSymbol(nil8_asdl::nvalue_t* nval); List* MustBeList(nil8_asdl::nvalue_t* nval); yaks_asdl::kexpr_t* TransformExpr(nil8_asdl::nvalue_t* nval); yaks_asdl::ktype_t* TransformType(nil8_asdl::nvalue_t* nval); yaks_asdl::NameType* TransformParam(List* param_n); List* TransformParams(List* params_n); yaks_asdl::Signature* TransformSignature(nil8_asdl::nvalue_t* nval); mod_def::Func* TransformFunc(List* func_n); yaks_asdl::Module* TransformModule(List* mod_n); yaks_asdl::Program* Transform(nil8_asdl::nvalue_t* nval); } // declare namespace transform namespace yaks_main { // declare int main(List* argv); } // declare namespace yaks_main namespace runtime { // define using hnode_asdl::hnode; using hnode_asdl::color_t; using hnode_asdl::color_e; int NO_SPID = -1; hnode::Record* NewRecord(BigStr* node_type) { StackRoot _root0(&node_type); return Alloc(node_type, Alloc>(), false, str0, str1, Alloc>()); } hnode::Leaf* NewLeaf(BigStr* s, hnode_asdl::color_t e_color) { StackRoot _root0(&s); if (s == nullptr) { return Alloc(str2, color_e::OtherConst); } else { return Alloc(s, e_color); } } TraversalState::TraversalState() { this->seen = Alloc>(); this->ref_count = Alloc>(); } BigStr* TRUE_STR = str3; BigStr* FALSE_STR = str4; } // define namespace runtime namespace format { // define using hnode_asdl::hnode; using hnode_asdl::hnode_e; using hnode_asdl::hnode_t; using hnode_asdl::color_e; using hnode_asdl::color_t; format::ColorOutput* DetectConsoleOutput(mylib::Writer* f) { StackRoot _root0(&f); if (f->isatty()) { return Alloc(f); } else { return Alloc(f); } } ColorOutput::ColorOutput(mylib::Writer* f) { this->f = f; this->num_chars = 0; } format::ColorOutput* ColorOutput::NewTempBuffer() { FAIL(kNotImplemented); // Python NotImplementedError } void ColorOutput::FileHeader() { ; // pass } void ColorOutput::FileFooter() { ; // pass } void ColorOutput::PushColor(hnode_asdl::color_t e_color) { FAIL(kNotImplemented); // Python NotImplementedError } void ColorOutput::PopColor() { FAIL(kNotImplemented); // Python NotImplementedError } void ColorOutput::write(BigStr* s) { StackRoot _root0(&s); this->f->write(s); this->num_chars += len(s); } void ColorOutput::WriteRaw(Tuple2* raw) { BigStr* s = nullptr; int num_chars; StackRoot _root0(&raw); StackRoot _root1(&s); Tuple2* tup0 = raw; s = tup0->at0(); num_chars = tup0->at1(); this->f->write(s); this->num_chars += num_chars; } int ColorOutput::NumChars() { return this->num_chars; } Tuple2 ColorOutput::GetRaw() { mylib::BufWriter* f = nullptr; StackRoot _root0(&f); f = static_cast(this->f); return Tuple2(f->getvalue(), this->num_chars); } TextOutput::TextOutput(mylib::Writer* f) : ::format::ColorOutput(f) { } format::TextOutput* TextOutput::NewTempBuffer() { return Alloc(Alloc()); } void TextOutput::PushColor(hnode_asdl::color_t e_color) { ; // pass } void TextOutput::PopColor() { ; // pass } HtmlOutput::HtmlOutput(mylib::Writer* f) : ::format::ColorOutput(f) { } format::HtmlOutput* HtmlOutput::NewTempBuffer() { return Alloc(Alloc()); } void HtmlOutput::FileHeader() { this->f->write(str5); } void HtmlOutput::FileFooter() { this->f->write(str6); } void HtmlOutput::PushColor(hnode_asdl::color_t e_color) { BigStr* css_class = nullptr; StackRoot _root0(&css_class); if (e_color == color_e::TypeName) { css_class = str7; } else { if (e_color == color_e::StringConst) { css_class = str8; } else { if (e_color == color_e::OtherConst) { css_class = str9; } else { if (e_color == color_e::External) { css_class = str10; } else { if (e_color == color_e::UserType) { css_class = str11; } else { assert(0); // AssertionError } } } } } this->f->write(StrFormat("", css_class)); } void HtmlOutput::PopColor() { this->f->write(str13); } void HtmlOutput::write(BigStr* s) { StackRoot _root0(&s); this->f->write(cgi::escape(s)); this->num_chars += len(s); } AnsiOutput::AnsiOutput(mylib::Writer* f) : ::format::ColorOutput(f) { } format::AnsiOutput* AnsiOutput::NewTempBuffer() { return Alloc(Alloc()); } void AnsiOutput::PushColor(hnode_asdl::color_t e_color) { if (e_color == color_e::TypeName) { this->f->write(ansi::YELLOW); } else { if (e_color == color_e::StringConst) { this->f->write(ansi::BOLD); } else { if (e_color == color_e::OtherConst) { this->f->write(ansi::GREEN); } else { if (e_color == color_e::External) { this->f->write(str_concat(ansi::BOLD, ansi::BLUE)); } else { if (e_color == color_e::UserType) { this->f->write(ansi::GREEN); } else { assert(0); // AssertionError } } } } } } void AnsiOutput::PopColor() { this->f->write(ansi::RESET); } int INDENT = 2; _PrettyPrinter::_PrettyPrinter(int max_col) { this->max_col = max_col; } bool _PrettyPrinter::_PrintWrappedArray(List* array, int prefix_len, format::ColorOutput* f, int indent) { bool all_fit; int chars_so_far; int i; format::ColorOutput* single_f = nullptr; BigStr* s = nullptr; int num_chars; StackRoot _root0(&array); StackRoot _root1(&f); StackRoot _root2(&single_f); StackRoot _root3(&s); all_fit = true; chars_so_far = prefix_len; i = 0; for (ListIter it(array); !it.Done(); it.Next(), ++i) { hnode_asdl::hnode_t* val = it.Value(); StackRoot _for(&val ); if (i != 0) { f->write(str14); } single_f = f->NewTempBuffer(); if (_TrySingleLine(val, single_f, (this->max_col - chars_so_far))) { Tuple2 tup1 = single_f->GetRaw(); s = tup1.at0(); num_chars = tup1.at1(); f->WriteRaw((Alloc>(s, num_chars))); chars_so_far += single_f->NumChars(); } else { f->write(str15); this->PrintNode(val, f, (indent + INDENT)); chars_so_far = 0; all_fit = false; } } return all_fit; } bool _PrettyPrinter::_PrintWholeArray(List* array, int prefix_len, format::ColorOutput* f, int indent) { bool all_fit; List*>* pieces = nullptr; int chars_so_far; format::ColorOutput* single_f = nullptr; BigStr* s = nullptr; int num_chars; int i; StackRoot _root0(&array); StackRoot _root1(&f); StackRoot _root2(&pieces); StackRoot _root3(&single_f); StackRoot _root4(&s); all_fit = true; pieces = Alloc*>>(); chars_so_far = prefix_len; for (ListIter it(array); !it.Done(); it.Next()) { hnode_asdl::hnode_t* item = it.Value(); StackRoot _for(&item ); single_f = f->NewTempBuffer(); if (_TrySingleLine(item, single_f, (this->max_col - chars_so_far))) { Tuple2 tup2 = single_f->GetRaw(); s = tup2.at0(); num_chars = tup2.at1(); pieces->append((Alloc>(s, num_chars))); chars_so_far += single_f->NumChars(); } else { all_fit = false; break; } } if (all_fit) { i = 0; for (ListIter*> it(pieces); !it.Done(); it.Next(), ++i) { Tuple2* p = it.Value(); StackRoot _for(&p ); if (i != 0) { f->write(str16); } f->WriteRaw(p); } f->write(str17); } return all_fit; } void _PrettyPrinter::_PrintRecord(hnode::Record* node, format::ColorOutput* f, int indent) { BigStr* ind = nullptr; BigStr* prefix = nullptr; int prefix_len; bool all_fit; BigStr* name = nullptr; hnode_asdl::hnode_t* val = nullptr; BigStr* ind1 = nullptr; hnode_asdl::hnode_t* UP_val = nullptr; int tag; BigStr* name_str = nullptr; format::ColorOutput* single_f = nullptr; BigStr* s = nullptr; int num_chars; StackRoot _root0(&node); StackRoot _root1(&f); StackRoot _root2(&ind); StackRoot _root3(&prefix); StackRoot _root4(&name); StackRoot _root5(&val); StackRoot _root6(&ind1); StackRoot _root7(&UP_val); StackRoot _root8(&name_str); StackRoot _root9(&single_f); StackRoot _root10(&s); ind = str_repeat(str18, indent); if (node->abbrev) { prefix = str_concat(ind, node->left); f->write(prefix); if (len(node->node_type)) { f->PushColor(color_e::TypeName); f->write(node->node_type); f->PopColor(); f->write(str19); } prefix_len = ((len(prefix) + len(node->node_type)) + 1); all_fit = this->_PrintWrappedArray(node->unnamed_fields, prefix_len, f, indent); if (!all_fit) { f->write(str20); f->write(ind); } f->write(node->right); } else { f->write(str_concat(ind, node->left)); f->PushColor(color_e::TypeName); f->write(node->node_type); f->PopColor(); f->write(str21); for (ListIter it(node->fields); !it.Done(); it.Next()) { hnode_asdl::Field* field = it.Value(); StackRoot _for(&field ); name = field->name; val = field->val; ind1 = str_repeat(str22, (indent + INDENT)); UP_val = val; tag = val->tag(); if (tag == hnode_e::Array) { hnode::Array* val = static_cast(UP_val); name_str = StrFormat("%s%s: [", ind1, name); f->write(name_str); prefix_len = len(name_str); if (!this->_PrintWholeArray(val->children, prefix_len, f, indent)) { f->write(str24); for (ListIter it(val->children); !it.Done(); it.Next()) { hnode_asdl::hnode_t* child = it.Value(); StackRoot _for(&child ); this->PrintNode(child, f, ((indent + INDENT) + INDENT)); f->write(str25); } f->write(StrFormat("%s]", ind1)); } } else { name_str = StrFormat("%s%s: ", ind1, name); f->write(name_str); prefix_len = len(name_str); single_f = f->NewTempBuffer(); if (_TrySingleLine(val, single_f, (this->max_col - prefix_len))) { Tuple2 tup3 = single_f->GetRaw(); s = tup3.at0(); num_chars = tup3.at1(); f->WriteRaw((Alloc>(s, num_chars))); } else { f->write(str28); this->PrintNode(val, f, ((indent + INDENT) + INDENT)); } } f->write(str29); } f->write(str_concat(ind, node->right)); } } void _PrettyPrinter::PrintNode(hnode_asdl::hnode_t* node, format::ColorOutput* f, int indent) { BigStr* ind = nullptr; format::ColorOutput* single_f = nullptr; BigStr* s = nullptr; int num_chars; hnode_asdl::hnode_t* UP_node = nullptr; int tag; StackRoot _root0(&node); StackRoot _root1(&f); StackRoot _root2(&ind); StackRoot _root3(&single_f); StackRoot _root4(&s); StackRoot _root5(&UP_node); ind = str_repeat(str30, indent); single_f = f->NewTempBuffer(); single_f->write(ind); if (_TrySingleLine(node, single_f, (this->max_col - indent))) { Tuple2 tup4 = single_f->GetRaw(); s = tup4.at0(); num_chars = tup4.at1(); f->WriteRaw((Alloc>(s, num_chars))); return ; } UP_node = node; tag = node->tag(); if (tag == hnode_e::Leaf) { hnode::Leaf* node = static_cast(UP_node); f->PushColor(node->color); f->write(j8_lite::EncodeString(node->s, true)); f->PopColor(); } else { if (tag == hnode_e::External) { hnode::External* node = static_cast(UP_node); f->PushColor(color_e::External); // if not PYTHON { f->write(str31); } // endif MYCPP f->PopColor(); } else { if (tag == hnode_e::Record) { hnode::Record* node = static_cast(UP_node); this->_PrintRecord(node, f, indent); } else { if (tag == hnode_e::AlreadySeen) { hnode::AlreadySeen* node = static_cast(UP_node); f->write(StrFormat("...0x%s", mylib::hex_lower(node->heap_id))); } else { assert(0); // AssertionError } } } } } bool _TrySingleLineObj(hnode::Record* node, format::ColorOutput* f, int max_chars) { int i; StackRoot _root0(&node); StackRoot _root1(&f); f->write(node->left); if (node->abbrev) { if (len(node->node_type)) { f->PushColor(color_e::TypeName); f->write(node->node_type); f->PopColor(); f->write(str33); } i = 0; for (ListIter it(node->unnamed_fields); !it.Done(); it.Next(), ++i) { hnode_asdl::hnode_t* val = it.Value(); StackRoot _for(&val ); if (i != 0) { f->write(str34); } if (!_TrySingleLine(val, f, max_chars)) { return false; } } } else { f->PushColor(color_e::TypeName); f->write(node->node_type); f->PopColor(); for (ListIter it(node->fields); !it.Done(); it.Next()) { hnode_asdl::Field* field = it.Value(); StackRoot _for(&field ); f->write(StrFormat(" %s:", field->name)); if (!_TrySingleLine(field->val, f, max_chars)) { return false; } } } f->write(node->right); return true; } bool _TrySingleLine(hnode_asdl::hnode_t* node, format::ColorOutput* f, int max_chars) { hnode_asdl::hnode_t* UP_node = nullptr; int tag; int i; int num_chars_so_far; StackRoot _root0(&node); StackRoot _root1(&f); StackRoot _root2(&UP_node); UP_node = node; tag = node->tag(); if (tag == hnode_e::Leaf) { hnode::Leaf* node = static_cast(UP_node); f->PushColor(node->color); f->write(j8_lite::EncodeString(node->s, true)); f->PopColor(); } else { if (tag == hnode_e::External) { hnode::External* node = static_cast(UP_node); f->PushColor(color_e::External); // if not PYTHON { f->write(str36); } // endif MYCPP f->PopColor(); } else { if (tag == hnode_e::Array) { hnode::Array* node = static_cast(UP_node); f->write(str37); i = 0; for (ListIter it(node->children); !it.Done(); it.Next(), ++i) { hnode_asdl::hnode_t* item = it.Value(); StackRoot _for(&item ); if (i != 0) { f->write(str38); } if (!_TrySingleLine(item, f, max_chars)) { return false; } } f->write(str39); } else { if (tag == hnode_e::Record) { hnode::Record* node = static_cast(UP_node); return _TrySingleLineObj(node, f, max_chars); } else { if (tag == hnode_e::AlreadySeen) { hnode::AlreadySeen* node = static_cast(UP_node); f->write(StrFormat("...0x%s", mylib::hex_lower(node->heap_id))); } else { assert(0); // AssertionError } } } } } num_chars_so_far = f->NumChars(); if (num_chars_so_far > max_chars) { return false; } return true; } void PrintTree(hnode_asdl::hnode_t* node, format::ColorOutput* f) { format::_PrettyPrinter* pp = nullptr; StackRoot _root0(&node); StackRoot _root1(&f); StackRoot _root2(&pp); pp = Alloc<_PrettyPrinter>(100); pp->PrintNode(node, f, 0); } } // define namespace format namespace ansi { // define BigStr* RESET = str41; BigStr* BOLD = str42; BigStr* UNDERLINE = str43; BigStr* REVERSE = str44; BigStr* RED = str45; BigStr* GREEN = str46; BigStr* YELLOW = str47; BigStr* BLUE = str48; BigStr* MAGENTA = str49; BigStr* CYAN = str50; BigStr* WHITE = str51; } // define namespace ansi namespace error { // define using syntax_asdl::loc_e; using syntax_asdl::loc_t; using syntax_asdl::loc; using value_asdl::value; using value_asdl::value_t; using value_asdl::value_str; BigStr* _ValType(value_asdl::value_t* val) { StackRoot _root0(&val); return value_str(val->tag(), false); } _ErrorWithLocation::_ErrorWithLocation(BigStr* msg, syntax_asdl::loc_t* location) { this->msg = msg; if (location == nullptr) { this->location = loc::Missing; } else { this->location = location; } } bool _ErrorWithLocation::HasLocation() { return this->location->tag() != loc_e::Missing; } BigStr* _ErrorWithLocation::UserErrorString() { return this->msg; } Usage::Usage(BigStr* msg, syntax_asdl::loc_t* location) : ::error::_ErrorWithLocation(msg, location) { } Parse::Parse(BigStr* msg, syntax_asdl::loc_t* location) : ::error::_ErrorWithLocation(msg, location) { } FailGlob::FailGlob(BigStr* msg, syntax_asdl::loc_t* location) : ::error::_ErrorWithLocation(msg, location) { } RedirectEval::RedirectEval(BigStr* msg, syntax_asdl::loc_t* location) : ::error::_ErrorWithLocation(msg, location) { } FatalRuntime::FatalRuntime(int exit_status, BigStr* msg, syntax_asdl::loc_t* location) : ::error::_ErrorWithLocation(msg, location) { this->exit_status = exit_status; } int FatalRuntime::ExitStatus() { return this->exit_status; } Strict::Strict(BigStr* msg, syntax_asdl::loc_t* location) : ::error::FatalRuntime(1, msg, location) { } ErrExit::ErrExit(int exit_status, BigStr* msg, syntax_asdl::loc_t* location, bool show_code) : ::error::FatalRuntime(exit_status, msg, location) { this->show_code = show_code; } Expr::Expr(BigStr* msg, syntax_asdl::loc_t* location) : ::error::FatalRuntime(3, msg, location) { } Structured::Structured(int status, BigStr* msg, syntax_asdl::loc_t* location, Dict* properties) : ::error::FatalRuntime(status, msg, location) { this->properties = properties; } value::Dict* Structured::ToDict() { if (this->properties == nullptr) { this->properties = Alloc>(); } this->properties->set(str53, num::ToBig(this->ExitStatus())); this->properties->set(str54, Alloc(this->msg)); return Alloc(this->properties); } AssertionErr::AssertionErr(BigStr* msg, syntax_asdl::loc_t* location) : ::error::Expr(msg, location) { } TypeErrVerbose::TypeErrVerbose(BigStr* msg, syntax_asdl::loc_t* location) : ::error::Expr(msg, location) { } TypeErr::TypeErr(value_asdl::value_t* actual_val, BigStr* msg, syntax_asdl::loc_t* location) : ::error::TypeErrVerbose(StrFormat("%s, got %s", msg, _ValType(actual_val)), location) { } Runtime::Runtime(BigStr* msg) { this->msg = msg; } BigStr* Runtime::UserErrorString() { return this->msg; } Decode::Decode(BigStr* msg, BigStr* s, int start_pos, int end_pos, int line_num) { this->msg = msg; this->s = s; this->start_pos = start_pos; this->end_pos = end_pos; this->line_num = line_num; } BigStr* Decode::Message() { int start; int end; BigStr* part = nullptr; StackRoot _root0(&part); start = max(0, (this->start_pos - 4)); end = min(len(this->s), (this->end_pos + 4)); part = this->s->slice(start, end); return str_concat(this->msg, StrFormat(" (line %d, offset %d-%d: %r)", this->line_num, this->start_pos, this->end_pos, part)); } BigStr* Decode::__str__() { return this->Message(); } Encode::Encode(BigStr* msg) { this->msg = msg; } BigStr* Encode::Message() { return this->msg; } [[noreturn]] void e_usage(BigStr* msg, syntax_asdl::loc_t* location) { StackRoot _root0(&msg); StackRoot _root1(&location); throw Alloc(msg, location); } [[noreturn]] void e_strict(BigStr* msg, syntax_asdl::loc_t* location) { StackRoot _root0(&msg); StackRoot _root1(&location); throw Alloc(msg, location); } [[noreturn]] void p_die(BigStr* msg, syntax_asdl::loc_t* location) { StackRoot _root0(&msg); StackRoot _root1(&location); throw Alloc(msg, location); } [[noreturn]] void e_die(BigStr* msg, syntax_asdl::loc_t* location) { StackRoot _root0(&msg); StackRoot _root1(&location); throw Alloc(1, msg, location); } [[noreturn]] void e_die_status(int status, BigStr* msg, syntax_asdl::loc_t* location) { StackRoot _root0(&msg); StackRoot _root1(&location); throw Alloc(status, msg, location); } } // define namespace error namespace num { // define using value_asdl::value; value::Int* ToBig(int i) { return Alloc(mops::IntWiden(i)); } mops::BigInt Exponent(mops::BigInt x, mops::BigInt y) { int y_int; mops::BigInt result; y_int = mops::BigTruncate(y); result = mops::BigInt(1); for (int i = 0; i < y_int; ++i) { result = mops::Mul(result, x); } return result; } int Exponent2(int x, int y) { return mops::BigTruncate(Exponent(mops::IntWiden(x), mops::IntWiden(y))); } mops::BigInt IntDivide(mops::BigInt x, mops::BigInt y) { mops::BigInt ZERO; int sign; mops::BigInt ax; mops::BigInt ay; ZERO = mops::BigInt(0); sign = 1; if (mops::Greater(ZERO, x)) { ax = mops::Negate(x); sign = -1; } else { ax = x; } if (mops::Greater(ZERO, y)) { ay = mops::Negate(y); sign = -sign; } else { ay = y; } return mops::Mul(mops::IntWiden(sign), mops::Div(ax, ay)); } int IntDivide2(int x, int y) { return mops::BigTruncate(IntDivide(mops::IntWiden(x), mops::IntWiden(y))); } mops::BigInt IntRemainder(mops::BigInt x, mops::BigInt y) { mops::BigInt ZERO; mops::BigInt ax; int sign; mops::BigInt ay; ZERO = mops::BigInt(0); if (mops::Greater(ZERO, x)) { ax = mops::Negate(x); sign = -1; } else { ax = x; sign = 1; } if (mops::Greater(ZERO, y)) { ay = mops::Negate(y); } else { ay = y; } return mops::Mul(mops::IntWiden(sign), mops::Rem(ax, ay)); } int IntRemainder2(int x, int y) { return mops::BigTruncate(IntRemainder(mops::IntWiden(x), mops::IntWiden(y))); } } // define namespace num namespace j8 { // define using id_kind_asdl::Id; using id_kind_asdl::Id_t; using id_kind_asdl::Id_str; using value_asdl::value; using value_asdl::value_e; using value_asdl::value_t; using value_asdl::value_str; using nil8_asdl::nvalue; using nil8_asdl::nvalue_t; namespace fmt = format; BigStr* ValType(value_asdl::value_t* val) { StackRoot _root0(&val); return value_str(val->tag(), false); } int ValueId(value_asdl::value_t* val) { StackRoot _root0(&val); switch (val->tag()) { case value_e::Null: case value_e::Bool: case value_e::Int: case value_e::Float: case value_e::Str: { return -1; } break; default: { return HeapValueId(val); } } } BigStr* ValueIdString(value_asdl::value_t* val) { int heap_id; StackRoot _root0(&val); heap_id = ValueId(val); if (heap_id == -1) { return str57; } else { return StrFormat(" 0x%s", mylib::hex_lower(heap_id)); } } BigStr* Utf8Encode(int code) { int num_cont_bytes; List* bytes_ = nullptr; int b; List* tmp = nullptr; StackRoot _root0(&bytes_); StackRoot _root1(&tmp); num_cont_bytes = 0; if (code <= 127) { return chr((code & 127)); } else { if (code <= 2047) { num_cont_bytes = 1; } else { if (code <= 65535) { num_cont_bytes = 2; } else { num_cont_bytes = 3; } } } bytes_ = Alloc>(); for (int _ = 0; _ < num_cont_bytes; ++_) { bytes_->append((128 | (code & 63))); code >>= 6; } b = ((30 << (6 - num_cont_bytes)) | (code & (63 >> num_cont_bytes))); bytes_->append(b); bytes_->reverse(); tmp = Alloc>(); for (ListIter it(bytes_); !it.Done(); it.Next()) { int b = it.Value(); tmp->append(chr((b & 255))); } return str59->join(tmp); } int SHOW_CYCLES = (1 << 1); int SHOW_NON_DATA = (1 << 2); int LOSSY_JSON = (1 << 3); int INF_NAN_ARE_NULL = (1 << 4); void _Print(value_asdl::value_t* val, mylib::BufWriter* buf, int indent, int options) { j8::InstancePrinter* p = nullptr; StackRoot _root0(&val); StackRoot _root1(&buf); StackRoot _root2(&p); p = Alloc(buf, indent, options); p->Print(val); } void PrintMessage(value_asdl::value_t* val, mylib::BufWriter* buf, int indent) { StackRoot _root0(&val); StackRoot _root1(&buf); _Print(val, buf, indent); } void PrintJsonMessage(value_asdl::value_t* val, mylib::BufWriter* buf, int indent) { StackRoot _root0(&val); StackRoot _root1(&buf); _Print(val, buf, indent, (LOSSY_JSON | INF_NAN_ARE_NULL)); } void PrintLine(value_asdl::value_t* val, mylib::Writer* f) { mylib::BufWriter* buf = nullptr; StackRoot _root0(&val); StackRoot _root1(&f); StackRoot _root2(&buf); buf = Alloc(); _Print(val, buf, -1, (SHOW_CYCLES | SHOW_NON_DATA)); f->write(buf->getvalue()); f->write(str60); } void EncodeString(BigStr* s, mylib::BufWriter* buf, bool unquoted_ok) { StackRoot _root0(&s); StackRoot _root1(&buf); if ((unquoted_ok and fastfunc::CanOmitQuotes(s))) { buf->write(s); return ; } _Print(Alloc(s), buf, -1); } BigStr* MaybeEncodeString(BigStr* s) { mylib::BufWriter* buf = nullptr; StackRoot _root0(&s); StackRoot _root1(&buf); buf = Alloc(); _Print(Alloc(s), buf, -1); return buf->getvalue(); } BigStr* MaybeEncodeJsonString(BigStr* s) { mylib::BufWriter* buf = nullptr; StackRoot _root0(&s); StackRoot _root1(&buf); buf = Alloc(); _Print(Alloc(s), buf, -1, LOSSY_JSON); return buf->getvalue(); } int UNSEEN = 0; int EXPLORING = 1; int FINISHED = 2; InstancePrinter::InstancePrinter(mylib::BufWriter* buf, int indent, int options) { this->buf = buf; this->indent = indent; this->options = options; this->visited = Alloc>(); } void InstancePrinter::_ItemIndent(int level) { if (this->indent == -1) { return ; } this->buf->write_spaces(((level + 1) * this->indent)); } void InstancePrinter::_BracketIndent(int level) { if (this->indent == -1) { return ; } this->buf->write_spaces((level * this->indent)); } void InstancePrinter::_MaybeNewline() { if (this->indent == -1) { return ; } this->buf->write(str61); } void InstancePrinter::_MaybeSpace() { if (this->indent == -1) { return ; } this->buf->write(str62); } void InstancePrinter::_PrintList(value::List* val, int level) { int i; StackRoot _root0(&val); if (len(val->items) == 0) { this->buf->write(str63); } else { this->buf->write(str64); this->_MaybeNewline(); i = 0; for (ListIter it(val->items); !it.Done(); it.Next(), ++i) { value_asdl::value_t* item = it.Value(); StackRoot _for(&item ); if (i != 0) { this->buf->write(str65); this->_MaybeNewline(); } this->_ItemIndent(level); this->Print(item, (level + 1)); } this->_MaybeNewline(); this->_BracketIndent(level); this->buf->write(str66); } } void InstancePrinter::_PrintDict(value::Dict* val, int level) { int i; StackRoot _root0(&val); if (len(val->d) == 0) { this->buf->write(str67); } else { this->buf->write(str68); this->_MaybeNewline(); i = 0; for (DictIter it(val->d); !it.Done(); it.Next()) { BigStr* k = it.Key(); value_asdl::value_t* v = it.Value(); if (i != 0) { this->buf->write(str69); this->_MaybeNewline(); } this->_ItemIndent(level); pyj8::WriteString(k, this->options, this->buf); this->buf->write(str70); this->_MaybeSpace(); this->Print(v, (level + 1)); i += 1; } this->_MaybeNewline(); this->_BracketIndent(level); this->buf->write(str71); } } void InstancePrinter::_PrintBashPrefix(BigStr* type_str, int level) { StackRoot _root0(&type_str); this->buf->write(str72); this->_MaybeNewline(); this->_ItemIndent(level); this->buf->write(str73); this->_MaybeSpace(); this->buf->write(type_str); this->_MaybeNewline(); this->_ItemIndent(level); this->buf->write(str74); this->_MaybeSpace(); } void InstancePrinter::_PrintBashSuffix(int level) { level -= 1; this->_MaybeNewline(); this->_BracketIndent(level); this->buf->write(str75); } void InstancePrinter::_PrintBashArray(value::BashArray* val, int level) { bool first; int i; StackRoot _root0(&val); this->_PrintBashPrefix(str76, level); if (len(val->strs) == 0) { this->buf->write(str77); } else { this->buf->write(str78); this->_MaybeNewline(); level += 1; first = true; i = 0; for (ListIter it(val->strs); !it.Done(); it.Next(), ++i) { BigStr* s = it.Value(); StackRoot _for(&s ); if (s == nullptr) { continue; } if (!first) { this->buf->write(str79); this->_MaybeNewline(); } this->_ItemIndent(level); pyj8::WriteString(str(i), this->options, this->buf); this->buf->write(str80); this->_MaybeSpace(); pyj8::WriteString(s, this->options, this->buf); first = false; } this->_MaybeNewline(); this->_BracketIndent(level); this->buf->write(str81); } this->_PrintBashSuffix(level); } void InstancePrinter::_PrintBashAssoc(value::BashAssoc* val, int level) { int i; StackRoot _root0(&val); this->_PrintBashPrefix(str82, level); if (len(val->d) == 0) { this->buf->write(str83); } else { this->buf->write(str84); this->_MaybeNewline(); level += 1; i = 0; for (DictIter it(val->d); !it.Done(); it.Next()) { BigStr* k2 = it.Key(); BigStr* v2 = it.Value(); if (i != 0) { this->buf->write(str85); this->_MaybeNewline(); } this->_ItemIndent(level); pyj8::WriteString(k2, this->options, this->buf); this->buf->write(str86); this->_MaybeSpace(); pyj8::WriteString(v2, this->options, this->buf); i += 1; } this->_MaybeNewline(); this->_BracketIndent(level); this->buf->write(str87); } this->_PrintBashSuffix(level); } void InstancePrinter::Print(value_asdl::value_t* val, int level) { value_asdl::value_t* UP_val = nullptr; double fl; BigStr* s = nullptr; int heap_id; int node_state; BigStr* ysh_type = nullptr; BigStr* id_str = nullptr; StackRoot _root0(&val); StackRoot _root1(&UP_val); StackRoot _root2(&s); StackRoot _root3(&ysh_type); StackRoot _root4(&id_str); UP_val = val; switch (val->tag()) { case value_e::Null: { this->buf->write(str88); } break; case value_e::Bool: { value::Bool* val = static_cast(UP_val); this->buf->write(val->b ? str89 : str90); } break; case value_e::Int: { value::Int* val = static_cast(UP_val); this->buf->write(mops::ToStr(val->i)); } break; case value_e::Float: { value::Float* val = static_cast(UP_val); fl = val->f; if (math::isinf(fl)) { if ((this->options & INF_NAN_ARE_NULL)) { s = str91; } else { s = str92; if (fl < 0) { s = str_concat(str93, s); } } } else { if (math::isnan(fl)) { if ((this->options & INF_NAN_ARE_NULL)) { s = str94; } else { s = str95; } } else { s = str(fl); } } this->buf->write(s); } break; case value_e::Str: { value::Str* val = static_cast(UP_val); pyj8::WriteString(val->s, this->options, this->buf); } break; case value_e::List: { value::List* val = static_cast(UP_val); heap_id = HeapValueId(val); node_state = this->visited->get(heap_id, UNSEEN); if (node_state == FINISHED) { this->_PrintList(val, level); return ; } if (node_state == EXPLORING) { if ((this->options & SHOW_CYCLES)) { this->buf->write(StrFormat("[ -->%s ]", ValueIdString(val))); return ; } else { throw Alloc(StrFormat("Can't encode List%s in object cycle", ValueIdString(val))); } } this->visited->set(heap_id, EXPLORING); this->_PrintList(val, level); this->visited->set(heap_id, FINISHED); } break; case value_e::Dict: { value::Dict* val = static_cast(UP_val); heap_id = HeapValueId(val); node_state = this->visited->get(heap_id, UNSEEN); if (node_state == FINISHED) { this->_PrintDict(val, level); return ; } if (node_state == EXPLORING) { if ((this->options & SHOW_CYCLES)) { this->buf->write(StrFormat("{ -->%s }", ValueIdString(val))); return ; } else { throw Alloc(StrFormat("Can't encode Dict%s in object cycle", ValueIdString(val))); } } this->visited->set(heap_id, EXPLORING); this->_PrintDict(val, level); this->visited->set(heap_id, FINISHED); } break; case value_e::BashArray: { value::BashArray* val = static_cast(UP_val); this->_PrintBashArray(val, level); } break; case value_e::BashAssoc: { value::BashAssoc* val = static_cast(UP_val); this->_PrintBashAssoc(val, level); } break; default: { ; // pass if ((this->options & SHOW_NON_DATA)) { ysh_type = ValType(val); id_str = ValueIdString(val); this->buf->write(StrFormat("<%s%s>", ysh_type, id_str)); } else { throw Alloc(StrFormat("Can't serialize object of type %s", ValType(val))); } } } } PrettyPrinter::PrettyPrinter(int max_col) { this->max_col = max_col; this->ref_count = Alloc>(); } void PrettyPrinter::PrettyTree(value_asdl::value_t* val, format::ColorOutput* f) { StackRoot _root0(&val); StackRoot _root1(&f); ; // pass } void PrettyPrinter::Print(value_asdl::value_t* val, mylib::BufWriter* buf) { format::ColorOutput* f = nullptr; StackRoot _root0(&val); StackRoot _root1(&buf); StackRoot _root2(&f); f = fmt::DetectConsoleOutput(mylib::Stdout()); this->PrettyTree(val, f); ; // pass } LexerDecoder::LexerDecoder(BigStr* s, bool is_j8, BigStr* lang_str) { this->s = s; this->is_j8 = is_j8; this->lang_str = lang_str; this->pos = 0; this->cur_line_num = 1; this->decoded = Alloc(); } error::Decode* LexerDecoder::_Error(BigStr* msg, int end_pos) { StackRoot _root0(&msg); return Alloc(msg, this->s, this->pos, end_pos, this->cur_line_num); } Tuple3 LexerDecoder::Next() { int tok_id; int end_pos; Tuple2 tup0 = match::MatchJ8Token(this->s, this->pos); tok_id = tup0.at0(); end_pos = tup0.at1(); if (!this->is_j8) { if ((tok_id == Id::Left_BSingleQuote || tok_id == Id::Left_USingleQuote)) { throw this->_Error(str102, end_pos); } if (tok_id == Id::Ignored_Comment) { throw this->_Error(str103, end_pos); } } if ((tok_id == Id::Left_DoubleQuote || tok_id == Id::Left_BSingleQuote || tok_id == Id::Left_USingleQuote)) { return this->_DecodeString(tok_id, end_pos); } if (tok_id == Id::Left_JDoubleQuote) { if (this->is_j8) { return this->_DecodeString(tok_id, end_pos); } else { throw this->_Error(str104, end_pos); } } if (tok_id == Id::Ignored_Newline) { this->cur_line_num += 1; } this->pos = end_pos; return Tuple3(tok_id, end_pos, nullptr); } Tuple3 LexerDecoder::NextForLines() { int tok_id; int end_pos; Tuple2 tup1 = match::MatchJ8LinesToken(this->s, this->pos); tok_id = tup1.at0(); end_pos = tup1.at1(); if ((tok_id == Id::Left_DoubleQuote || tok_id == Id::Left_JDoubleQuote || tok_id == Id::Left_BSingleQuote || tok_id == Id::Left_USingleQuote)) { return this->_DecodeString(tok_id, end_pos); } if ((tok_id == Id::Lit_Chars and !pyj8::PartIsUtf8(this->s, this->pos, end_pos))) { throw this->_Error(StrFormat("Invalid UTF-8 in %s string literal", this->lang_str), end_pos); } if (tok_id == Id::Char_AsciiControl) { throw this->_Error(str106, end_pos); } if (tok_id == Id::J8_Newline) { this->cur_line_num += 1; } this->pos = end_pos; return Tuple3(tok_id, end_pos, nullptr); } Tuple3 LexerDecoder::_DecodeString(int left_id, int str_pos) { int tok_id; int str_end; BigStr* s = nullptr; BigStr* part = nullptr; BigStr* ch = nullptr; BigStr* h = nullptr; int i; BigStr* h1 = nullptr; BigStr* h2 = nullptr; int i1; int i2; int code_point; StackRoot _root0(&s); StackRoot _root1(&part); StackRoot _root2(&ch); StackRoot _root3(&h); StackRoot _root4(&h1); StackRoot _root5(&h2); while (true) { if ((left_id == Id::Left_DoubleQuote || left_id == Id::Left_JDoubleQuote)) { Tuple2 tup2 = match::MatchJsonStrToken(this->s, str_pos); tok_id = tup2.at0(); str_end = tup2.at1(); } else { Tuple2 tup3 = match::MatchJ8StrToken(this->s, str_pos); tok_id = tup3.at0(); str_end = tup3.at1(); } if (tok_id == Id::Eol_Tok) { throw this->_Error(StrFormat("Unexpected EOF while lexing %s string", this->lang_str), str_end); } if (tok_id == Id::Unknown_Backslash) { throw this->_Error(StrFormat("Bad backslash escape in %s string", this->lang_str), str_end); } if (tok_id == Id::Char_AsciiControl) { throw this->_Error(StrFormat("%s strings can't have unescaped ASCII control chars", this->lang_str), str_end); } if ((tok_id == Id::Right_SingleQuote || tok_id == Id::Right_DoubleQuote)) { this->pos = str_end; s = this->decoded->getvalue(); this->decoded->clear(); return Tuple3(Id::J8_String, str_end, s); } if (tok_id == Id::Lit_Chars) { part = this->s->slice(str_pos, str_end); if (!pyj8::PartIsUtf8(this->s, str_pos, str_end)) { throw this->_Error(StrFormat("Invalid UTF-8 in %s string literal", this->lang_str), str_end); } } else { if (tok_id == Id::Char_OneChar) { ch = this->s->at((str_pos + 1)); part = consts::LookupCharC(ch); } else { if (tok_id == Id::Char_UBraced) { h = this->s->slice((str_pos + 3), (str_end - 1)); i = to_int(h, 16); if (i > 1114111) { throw this->_Error(str111, str_end); } if ((55296 <= i and i < 57344)) { throw this->_Error(StrFormat("\\u{%s} escape is illegal because it's in the surrogate range", h), str_end); } part = Utf8Encode(i); } else { if (tok_id == Id::Char_YHex) { h = this->s->slice((str_pos + 2), str_end); if (left_id != Id::Left_BSingleQuote) { throw this->_Error(StrFormat("\\y%s escapes not allowed in u'' strings", h), str_end); } i = to_int(h, 16); part = chr(i); } else { if (tok_id == Id::Char_SurrogatePair) { h1 = this->s->slice((str_pos + 2), (str_pos + 6)); h2 = this->s->slice((str_pos + 8), (str_pos + 12)); i1 = (to_int(h1, 16) - 55296); i2 = (to_int(h2, 16) - 56320); code_point = ((65536 + (i1 << 10)) + i2); part = Utf8Encode(code_point); } else { if (tok_id == Id::Char_Unicode4) { h = this->s->slice((str_pos + 2), str_end); i = to_int(h, 16); part = Utf8Encode(i); } else { assert(0); // AssertionError } } } } } } this->decoded->write(part); str_pos = str_end; } } _Parser::_Parser(BigStr* s, bool is_j8) { this->s = s; this->is_j8 = is_j8; this->lang_str = is_j8 ? str114 : str115; this->lexer = Alloc(s, is_j8, this->lang_str); this->tok_id = Id::Undefined_Tok; this->start_pos = 0; this->end_pos = 0; this->decoded = str116; } void _Parser::_Next() { while (true) { this->start_pos = this->end_pos; Tuple3 tup4 = this->lexer->Next(); this->tok_id = tup4.at0(); this->end_pos = tup4.at1(); this->decoded = tup4.at2(); if ((this->tok_id != Id::Ignored_Space && this->tok_id != Id::Ignored_Newline && this->tok_id != Id::Ignored_Comment)) { break; } } } void _Parser::_Eat(int tok_id) { if (this->tok_id != tok_id) { throw this->_ParseError(StrFormat("Expected %s, got %s", Id_str(tok_id), Id_str(this->tok_id))); } this->_Next(); } void _Parser::_NextForLines() { this->start_pos = this->end_pos; Tuple3 tup5 = this->lexer->NextForLines(); this->tok_id = tup5.at0(); this->end_pos = tup5.at1(); this->decoded = tup5.at2(); } error::Decode* _Parser::_ParseError(BigStr* msg) { StackRoot _root0(&msg); return Alloc(msg, this->s, this->start_pos, this->end_pos, this->lexer->cur_line_num); } Parser::Parser(BigStr* s, bool is_j8) : ::j8::_Parser(s, is_j8) { } Tuple2 Parser::_ParsePair() { BigStr* k = nullptr; value_asdl::value_t* v = nullptr; StackRoot _root0(&k); StackRoot _root1(&v); k = this->decoded; this->_Eat(Id::J8_String); this->_Eat(Id::J8_Colon); v = this->_ParseValue(); return Tuple2(k, v); } value_asdl::value_t* Parser::_ParseDict() { Dict* d = nullptr; BigStr* k = nullptr; value_asdl::value_t* v = nullptr; StackRoot _root0(&d); StackRoot _root1(&k); StackRoot _root2(&v); d = Alloc>(); this->_Next(); if (this->tok_id == Id::J8_RBrace) { this->_Next(); return Alloc(d); } Tuple2 tup6 = this->_ParsePair(); k = tup6.at0(); v = tup6.at1(); d->set(k, v); while (this->tok_id == Id::J8_Comma) { this->_Next(); Tuple2 tup7 = this->_ParsePair(); k = tup7.at0(); v = tup7.at1(); d->set(k, v); } this->_Eat(Id::J8_RBrace); return Alloc(d); } value_asdl::value_t* Parser::_ParseList() { List* items = nullptr; StackRoot _root0(&items); items = Alloc>(); this->_Next(); if (this->tok_id == Id::J8_RBracket) { this->_Next(); return Alloc(items); } items->append(this->_ParseValue()); while (this->tok_id == Id::J8_Comma) { this->_Next(); items->append(this->_ParseValue()); } this->_Eat(Id::J8_RBracket); return Alloc(items); } value_asdl::value_t* Parser::_ParseValue() { value::Bool* b = nullptr; BigStr* part = nullptr; mops::BigInt big; value::Str* str_val = nullptr; StackRoot _root0(&b); StackRoot _root1(&part); StackRoot _root2(&str_val); if (this->tok_id == Id::J8_LBrace) { return this->_ParseDict(); } else { if (this->tok_id == Id::J8_LBracket) { return this->_ParseList(); } else { if (this->tok_id == Id::J8_Null) { this->_Next(); return value::Null; } else { if (this->tok_id == Id::J8_Bool) { b = Alloc(str_equals(this->s->at(this->start_pos), str118)); this->_Next(); return b; } else { if (this->tok_id == Id::J8_Int) { part = this->s->slice(this->start_pos, this->end_pos); this->_Next(); try { big = mops::FromStr(part); } catch (ValueError*) { throw this->_ParseError(str119); } return Alloc(big); } else { if (this->tok_id == Id::J8_Float) { part = this->s->slice(this->start_pos, this->end_pos); this->_Next(); return Alloc(to_float(part)); } else { if (this->tok_id == Id::J8_String) { str_val = Alloc(this->decoded); this->_Next(); return str_val; } else { if (this->tok_id == Id::Eol_Tok) { throw this->_ParseError(StrFormat("Unexpected EOF while parsing %s", this->lang_str)); } else { throw this->_ParseError(StrFormat("Invalid token while parsing %s: %s", this->lang_str, Id_str(this->tok_id))); } } } } } } } } } value_asdl::value_t* Parser::ParseValue() { value_asdl::value_t* obj = nullptr; int n; int extra; StackRoot _root0(&obj); this->_Next(); obj = this->_ParseValue(); n = len(this->s); if (this->start_pos != n) { extra = (n - this->start_pos); throw this->_ParseError(StrFormat("Got %d bytes of unexpected trailing input", extra)); } return obj; } Nil8Parser::Nil8Parser(BigStr* s, bool is_j8) : ::j8::_Parser(s, is_j8) { } nil8_asdl::nvalue_t* Nil8Parser::_ParseRecord() { List* items = nullptr; StackRoot _root0(&items); items = Alloc>(); this->_Next(); if (this->tok_id == Id::J8_RParen) { this->_Next(); return Alloc(items); } while (this->tok_id != Id::J8_RParen) { items->append(this->_ParseNil8()); } this->_Eat(Id::J8_RParen); return Alloc(items); } nil8_asdl::nvalue_t* Nil8Parser::_ParseList8() { List* items = nullptr; StackRoot _root0(&items); items = Alloc>(); this->_Next(); if (this->tok_id == Id::J8_RBracket) { this->_Next(); return Alloc(items); } while (this->tok_id != Id::J8_RBracket) { items->append(this->_ParseNil8()); } this->_Eat(Id::J8_RBracket); return Alloc(items); } nil8_asdl::nvalue_t* Nil8Parser::_ParseNil8() { nil8_asdl::nvalue_t* obj = nullptr; nvalue::Bool* b = nullptr; BigStr* part = nullptr; nvalue::Str* str_val = nullptr; nvalue::Symbol* op = nullptr; nil8_asdl::nvalue_t* operand2 = nullptr; nil8_asdl::nvalue_t* infix = nullptr; StackRoot _root0(&obj); StackRoot _root1(&b); StackRoot _root2(&part); StackRoot _root3(&str_val); StackRoot _root4(&op); StackRoot _root5(&operand2); StackRoot _root6(&infix); if (this->tok_id == Id::J8_LParen) { obj = this->_ParseRecord(); } else { if (this->tok_id == Id::J8_LBracket) { obj = this->_ParseList8(); } else { if (this->tok_id == Id::J8_Null) { this->_Next(); obj = nvalue::Null; } else { if (this->tok_id == Id::J8_Bool) { b = Alloc(str_equals(this->s->at(this->start_pos), str123)); this->_Next(); obj = b; } else { if (this->tok_id == Id::J8_Int) { part = this->s->slice(this->start_pos, this->end_pos); this->_Next(); obj = Alloc(to_int(part)); } else { if (this->tok_id == Id::J8_Float) { part = this->s->slice(this->start_pos, this->end_pos); this->_Next(); obj = Alloc(to_float(part)); } else { if (this->tok_id == Id::J8_String) { str_val = Alloc(this->decoded); this->_Next(); obj = str_val; } else { if ((this->tok_id == Id::J8_Identifier || this->tok_id == Id::J8_Operator || this->tok_id == Id::J8_Colon || this->tok_id == Id::J8_Comma)) { part = this->s->slice(this->start_pos, this->end_pos); this->_Next(); obj = Alloc(part); } else { if (this->tok_id == Id::Eol_Tok) { throw this->_ParseError(StrFormat("Unexpected EOF while parsing %s", this->lang_str)); } else { throw this->_ParseError(StrFormat("Invalid token while parsing %s: %s", this->lang_str, Id_str(this->tok_id))); } } } } } } } } } if ((this->tok_id == Id::J8_Operator || this->tok_id == Id::J8_Colon || this->tok_id == Id::J8_Comma)) { part = this->s->slice(this->start_pos, this->end_pos); op = Alloc(part); this->_Next(); operand2 = this->_ParseNil8(); infix = Alloc(NewList(std::initializer_list{op, obj, operand2})); return infix; } return obj; } nil8_asdl::nvalue_t* Nil8Parser::ParseNil8() { nil8_asdl::nvalue_t* obj = nullptr; StackRoot _root0(&obj); this->_Next(); obj = this->_ParseNil8(); if (this->tok_id != Id::Eol_Tok) { throw this->_ParseError(str126); } return obj; } J8LinesParser::J8LinesParser(BigStr* s) : ::j8::_Parser(s, true) { } void J8LinesParser::_Show(BigStr* s) { StackRoot _root0(&s); mylib::print_stderr(StrFormat("%s tok_id %s %d-%d", s, Id_str(this->tok_id), this->start_pos, this->end_pos)); } void J8LinesParser::_ParseLine(List* out) { int string_start; int prev_id; int prev_start; int string_end; StackRoot _root0(&out); if (this->tok_id == Id::WS_Space) { this->_NextForLines(); } if ((this->tok_id == Id::J8_Newline || this->tok_id == Id::Eol_Tok)) { this->_NextForLines(); return ; } if (this->tok_id == Id::J8_String) { out->append(this->decoded); this->_NextForLines(); if (this->tok_id == Id::WS_Space) { this->_NextForLines(); } if ((this->tok_id != Id::J8_Newline && this->tok_id != Id::Eol_Tok)) { throw this->_ParseError(StrFormat("Unexpected text after J8 Line (%s)", Id_str(this->tok_id))); } this->_NextForLines(); return ; } if (this->tok_id == Id::Lit_Chars) { string_start = this->start_pos; while (true) { prev_id = this->tok_id; prev_start = this->start_pos; this->_NextForLines(); if ((this->tok_id == Id::J8_Newline || this->tok_id == Id::Eol_Tok)) { break; } } if (prev_id == Id::WS_Space) { string_end = prev_start; } else { string_end = this->start_pos; } out->append(this->s->slice(string_start, string_end)); this->_NextForLines(); return ; } assert(0); // AssertionError } List* J8LinesParser::Parse() { List* lines = nullptr; StackRoot _root0(&lines); this->_NextForLines(); lines = Alloc>(); while (this->tok_id != Id::Eol_Tok) { this->_ParseLine(lines); } if (this->tok_id != Id::Eol_Tok) { throw this->_ParseError(str129); } return lines; } List* SplitJ8Lines(BigStr* s) { j8::J8LinesParser* p = nullptr; StackRoot _root0(&s); StackRoot _root1(&p); p = Alloc(s); return p->Parse(); } } // define namespace j8 namespace j8_lite { // define BigStr* EncodeString(BigStr* s, bool unquoted_ok) { StackRoot _root0(&s); if ((unquoted_ok and fastfunc::CanOmitQuotes(s))) { return s; } return fastfunc::J8EncodeString(s, 1); } BigStr* YshEncodeString(BigStr* s) { StackRoot _root0(&s); return fastfunc::ShellEncodeString(s, 1); } BigStr* MaybeShellEncode(BigStr* s) { StackRoot _root0(&s); if (fastfunc::CanOmitQuotes(s)) { return s; } return fastfunc::ShellEncodeString(s, 0); } BigStr* ShellEncode(BigStr* s) { StackRoot _root0(&s); return fastfunc::ShellEncodeString(s, 0); } BigStr* YshEncode(BigStr* s, bool unquoted_ok) { StackRoot _root0(&s); if ((unquoted_ok and fastfunc::CanOmitQuotes(s))) { return s; } return fastfunc::ShellEncodeString(s, 1); } } // define namespace j8_lite namespace cgi { // define BigStr* escape(BigStr* s) { StackRoot _root0(&s); s = s->replace(str130, str131); s = s->replace(str132, str133); s = s->replace(str134, str135); return s; } } // define namespace cgi namespace gen_cpp { // define using yaks_asdl::Program; using yaks_asdl::mod_def; using yaks_asdl::mod_def_e; using yaks_asdl::ktype; using yaks_asdl::ktype_e; using yaks_asdl::ktype_t; using yaks_asdl::NameType; using yaks_asdl::stmt; using yaks_asdl::stmt_e; using yaks_asdl::stmt_t; using yaks_asdl::Int; using yaks_asdl::kexpr_e; using yaks_asdl::kexpr_t; using yaks_asdl::Int; void GenType(yaks_asdl::ktype_t* typ, mylib::Writer* f) { yaks_asdl::ktype_t* UP_typ = nullptr; StackRoot _root0(&typ); StackRoot _root1(&f); StackRoot _root2(&UP_typ); UP_typ = typ; switch (typ->tag()) { case ktype_e::Int: { f->write(str136); } break; case ktype_e::Str: { f->write(str137); } break; case ktype_e::List: { ktype::List* typ = static_cast(UP_typ); f->write(str138); GenType(typ->T, f); f->write(str139); } break; default: { assert(0); // AssertionError } } } void GenParam(yaks_asdl::NameType* p, mylib::Writer* f) { StackRoot _root0(&p); StackRoot _root1(&f); GenType(p->typ, f); f->write(StrFormat(" %s", p->name)); } void GenExpr(yaks_asdl::kexpr_t* expr, mylib::Writer* f) { yaks_asdl::kexpr_t* UP_expr = nullptr; StackRoot _root0(&expr); StackRoot _root1(&f); StackRoot _root2(&UP_expr); UP_expr = expr; switch (expr->tag()) { case kexpr_e::Int: { Int* expr = static_cast(UP_expr); f->write(StrFormat("%d", expr->i)); } break; default: { assert(0); // AssertionError } } } void GenStatement(yaks_asdl::stmt_t* st, mylib::Writer* f) { yaks_asdl::stmt_t* UP_st = nullptr; StackRoot _root0(&st); StackRoot _root1(&f); StackRoot _root2(&UP_st); UP_st = st; switch (st->tag()) { case stmt_e::Return: { stmt::Return* st = static_cast(UP_st); f->write(str142); GenExpr(st->e, f); f->write(str143); } break; } } void GenFunction(mod_def::Func* func, mylib::Writer* f) { int i; StackRoot _root0(&func); StackRoot _root1(&f); GenType(func->sig->return_type, f); f->write(StrFormat(" %s(", func->name)); i = 0; for (ListIter it(func->sig->params); !it.Done(); it.Next(), ++i) { yaks_asdl::NameType* p = it.Value(); StackRoot _for(&p ); if (i != 0) { f->write(str145); } GenParam(p, f); } f->write(str146); for (ListIter it(func->statements); !it.Done(); it.Next()) { yaks_asdl::stmt_t* st = it.Value(); StackRoot _for(&st ); GenStatement(st, f); } f->write(str147); } void GenCpp(yaks_asdl::Program* prog, mylib::Writer* f) { yaks_asdl::mod_def_t* UP_d = nullptr; StackRoot _root0(&prog); StackRoot _root1(&f); StackRoot _root2(&UP_d); f->write(str148); for (ListIter it(prog->modules); !it.Done(); it.Next()) { yaks_asdl::Module* module = it.Value(); StackRoot _for(&module ); f->write(StrFormat("namespace %s {\n", module->name)); for (ListIter it(module->defs); !it.Done(); it.Next()) { yaks_asdl::mod_def_t* d = it.Value(); StackRoot _for(&d ); UP_d = d; switch (d->tag()) { case mod_def_e::Func: { mod_def::Func* d = static_cast(UP_d); GenFunction(d, f); } break; } } f->write(StrFormat("} // namespace %s\n", module->name)); } } } // define namespace gen_cpp namespace transform { // define using nil8_asdl::nvalue; using nil8_asdl::nvalue_e; using nil8_asdl::nvalue_t; using yaks_asdl::Module; using yaks_asdl::Program; using yaks_asdl::mod_def; using yaks_asdl::mod_def_t; using yaks_asdl::ktype; using yaks_asdl::ktype_t; using yaks_asdl::stmt; using yaks_asdl::stmt_t; using yaks_asdl::kexpr_t; using yaks_asdl::Int; using yaks_asdl::Token; using yaks_asdl::Signature; using yaks_asdl::NameType; BigStr* MustBeSymbol(nil8_asdl::nvalue_t* nval) { StackRoot _root0(&nval); if (nval->tag() != nvalue_e::Symbol) { assert(0); // AssertionError } return static_cast(nval)->s; } List* MustBeList(nil8_asdl::nvalue_t* nval) { StackRoot _root0(&nval); if (nval->tag() != nvalue_e::List) { assert(0); // AssertionError } return static_cast(nval)->items; } yaks_asdl::kexpr_t* TransformExpr(nil8_asdl::nvalue_t* nval) { nil8_asdl::nvalue_t* UP_nval = nullptr; yaks_asdl::Token* loc = nullptr; StackRoot _root0(&nval); StackRoot _root1(&UP_nval); StackRoot _root2(&loc); UP_nval = nval; switch (nval->tag()) { case nvalue_e::Int: { nvalue::Int* nval = static_cast(UP_nval); loc = Alloc(str153, str154, 0, 3); return Alloc(nval->i, loc); } break; default: { assert(0); // AssertionError } } } yaks_asdl::ktype_t* TransformType(nil8_asdl::nvalue_t* nval) { nil8_asdl::nvalue_t* UP_nval = nullptr; BigStr* first = nullptr; StackRoot _root0(&nval); StackRoot _root1(&UP_nval); StackRoot _root2(&first); UP_nval = nval; switch (nval->tag()) { case nvalue_e::Symbol: { nvalue::Symbol* nval = static_cast(UP_nval); if (str_equals(nval->s, str155)) { return ktype::Bool; } else { if (str_equals(nval->s, str156)) { return ktype::Int; } else { if (str_equals(nval->s, str157)) { return ktype::Str; } else { assert(0); // AssertionError } } } } break; case nvalue_e::List: { nvalue::List* nval = static_cast(UP_nval); first = MustBeSymbol(nval->items->at(0)); if (str_equals(first, str158)) { return Alloc(TransformType(nval->items->at(1))); } else { if (str_equals(first, str159)) { return Alloc(TransformType(nval->items->at(1)), TransformType(nval->items->at(2))); } else { assert(0); // AssertionError } } } break; default: { assert(0); // AssertionError } } } yaks_asdl::NameType* TransformParam(List* param_n) { BigStr* name = nullptr; yaks_asdl::ktype_t* typ = nullptr; StackRoot _root0(¶m_n); StackRoot _root1(&name); StackRoot _root2(&typ); if (len(param_n) != 2) { assert(0); // AssertionError } name = MustBeSymbol(param_n->at(0)); typ = TransformType(param_n->at(1)); return Alloc(name, typ); } List* TransformParams(List* params_n) { List* result = nullptr; List* param_n = nullptr; StackRoot _root0(¶ms_n); StackRoot _root1(&result); StackRoot _root2(¶m_n); result = Alloc>(); for (ListIter it(params_n); !it.Done(); it.Next()) { nil8_asdl::nvalue_t* p = it.Value(); StackRoot _for(&p ); param_n = MustBeList(p); result->append(TransformParam(param_n)); } return result; } yaks_asdl::Signature* TransformSignature(nil8_asdl::nvalue_t* nval) { List* sig_n = nullptr; BigStr* first = nullptr; List* params_n = nullptr; StackRoot _root0(&nval); StackRoot _root1(&sig_n); StackRoot _root2(&first); StackRoot _root3(¶ms_n); sig_n = MustBeList(nval); if (len(sig_n) != 3) { assert(0); // AssertionError } first = MustBeSymbol(sig_n->at(0)); if (!(str_equals(first, str161))) { assert(0); // AssertionError } params_n = MustBeList(sig_n->at(1)); return Alloc(TransformParams(params_n), TransformType(sig_n->at(2))); } mod_def::Func* TransformFunc(List* func_n) { BigStr* func_name = nullptr; List* out_stmts = nullptr; yaks_asdl::Signature* sig = nullptr; mod_def::Func* func = nullptr; List* stmts = nullptr; List* stmt_n = nullptr; BigStr* first = nullptr; yaks_asdl::kexpr_t* expr = nullptr; StackRoot _root0(&func_n); StackRoot _root1(&func_name); StackRoot _root2(&out_stmts); StackRoot _root3(&sig); StackRoot _root4(&func); StackRoot _root5(&stmts); StackRoot _root6(&stmt_n); StackRoot _root7(&first); StackRoot _root8(&expr); func_name = MustBeSymbol(func_n->at(1)); out_stmts = Alloc>(); sig = TransformSignature(func_n->at(2)); func = Alloc(func_name, sig, out_stmts); stmts = func_n->slice(3); for (ListIter it(stmts); !it.Done(); it.Next()) { nil8_asdl::nvalue_t* st = it.Value(); StackRoot _for(&st ); stmt_n = MustBeList(st); if (len(stmt_n) == 0) { assert(0); // AssertionError } first = MustBeSymbol(stmt_n->at(0)); if (str_equals(first, str164)) { ; // pass } else { if (str_equals(first, str165)) { ; // pass } else { if (str_equals(first, str166)) { ; // pass } else { if (str_equals(first, str167)) { expr = TransformExpr(stmt_n->at(1)); out_stmts->append(Alloc(expr)); } else { if (str_equals(first, str168)) { ; // pass } else { if (str_equals(first, str169)) { ; // pass } else { assert(0); // AssertionError } } } } } } } return func; } yaks_asdl::Module* TransformModule(List* mod_n) { BigStr* mod_name = nullptr; List* out_defs = nullptr; yaks_asdl::Module* module = nullptr; List* defs = nullptr; List* def_n = nullptr; BigStr* first = nullptr; StackRoot _root0(&mod_n); StackRoot _root1(&mod_name); StackRoot _root2(&out_defs); StackRoot _root3(&module); StackRoot _root4(&defs); StackRoot _root5(&def_n); StackRoot _root6(&first); if (len(mod_n) < 2) { assert(0); // AssertionError } mod_name = MustBeSymbol(mod_n->at(1)); out_defs = Alloc>(); module = Alloc(mod_name, out_defs); defs = mod_n->slice(2); for (ListIter it(defs); !it.Done(); it.Next()) { nil8_asdl::nvalue_t* d = it.Value(); StackRoot _for(&d ); def_n = MustBeList(d); if (len(def_n) == 0) { assert(0); // AssertionError } first = MustBeSymbol(def_n->at(0)); if (str_equals(first, str173)) { ; // pass } else { if (str_equals(first, str174)) { out_defs->append(TransformFunc(def_n)); } else { if (str_equals(first, str175)) { ; // pass } else { assert(0); // AssertionError } } } } return module; } yaks_asdl::Program* Transform(nil8_asdl::nvalue_t* nval) { List* mod_n = nullptr; yaks_asdl::Module* module = nullptr; yaks_asdl::Program* prog = nullptr; StackRoot _root0(&nval); StackRoot _root1(&mod_n); StackRoot _root2(&module); StackRoot _root3(&prog); mod_n = MustBeList(nval); module = TransformModule(mod_n); prog = Alloc(str177, NewList(std::initializer_list{module})); return prog; } } // define namespace transform namespace yaks_main { // define namespace fmt = format; int main(List* argv) { mylib::Writer* stderr_ = nullptr; BigStr* action = nullptr; BigStr* path = nullptr; List* lines = nullptr; mylib::LineReader* f = nullptr; BigStr* line = nullptr; BigStr* contents = nullptr; j8::Nil8Parser* p = nullptr; nil8_asdl::nvalue_t* nval = nullptr; format::ColorOutput* pretty_f = nullptr; yaks_asdl::Program* prog = nullptr; yaks_asdl::Module* m = nullptr; StackRoot _root0(&argv); StackRoot _root1(&stderr_); StackRoot _root2(&action); StackRoot _root3(&path); StackRoot _root4(&lines); StackRoot _root5(&f); StackRoot _root6(&line); StackRoot _root7(&contents); StackRoot _root8(&p); StackRoot _root9(&nval); StackRoot _root10(&pretty_f); StackRoot _root11(&prog); StackRoot _root12(&m); stderr_ = mylib::Stderr(); try { action = argv->at(1); } catch (IndexError*) { throw Alloc(str178); } if (str_equals(action, str179)) { path = argv->at(2); lines = Alloc>(); f = mylib::open(path); while (true) { line = f->readline(); if (len(line) == 0) { break; } lines->append(line); } contents = str180->join(lines); p = Alloc(contents, true); try { nval = p->ParseNil8(); } catch (error::Decode* e) { return 1; } pretty_f = fmt::DetectConsoleOutput(stderr_); fmt::PrintTree(nval->PrettyTree(), pretty_f); stderr_->write(str181); prog = transform::Transform(nval); fmt::PrintTree(prog->PrettyTree(), pretty_f); stderr_->write(str182); gen_cpp::GenCpp(prog, mylib::Stdout()); } else { if (str_equals(action, str183)) { path = argv->at(2); m = Alloc(str184, Alloc>()); pretty_f = fmt::DetectConsoleOutput(stderr_); fmt::PrintTree(m->PrettyTree(), pretty_f); stderr_->write(str185); } else { throw Alloc(StrFormat("Invalid action %r", action)); } } return 0; } } // define namespace yaks_main int main(int argc, char **argv) { mylib::InitCppOnly(); // Initializes gHeap auto* args = Alloc>(); for (int i = 0; i < argc; ++i) { args->append(StrFromC(argv[i])); } int status = yaks_main::main(args); gHeap.ProcessExit(); return status; }