mirror of
https://github.com/zrax/pycdc.git
synced 2026-06-23 11:34:07 +00:00
pycdc did not reconstruct `match` statements: class patterns hit the
unhandled MATCH_CLASS opcode and bailed to "# WARNING: Decompyle incomplete",
and value patterns were mis-rendered as an if/elif chain.
This adds reconstruction of the common, statically-recognizable shapes:
* Class patterns - `case Cls(a, b):` - including positional captures and
`_` wildcard sub-patterns (`case Cls(_):`).
* Value patterns - `case 0:` / `case 'x':` - compiled as a COPY-threaded
COMPARE_OP chain rather than MATCH_CLASS.
* The `case _:` wildcard (which carries no test opcode).
A pre-scan over the code object recognizes the simple, guard-free shape of
each case and records it; the MATCH_CLASS handler and a small guarded hook in
COMPARE_OP open ASTMatchBlock/ASTCaseBlock and skip the pattern-test
machinery, and the block-close logic closes each case at its fail-target and
the match at its merge. Anything outside the recognized shape (kwarg patterns,
guards, sequence/mapping patterns) is left unregistered and still bails
honestly, so no incorrect output is produced.
New AST nodes: ASTMatchBlock (subject) and ASTCaseBlock (pattern), rendered
via the existing block-header machinery (type_str + print_src). PycBuffer
gains setPos() so the reconstructor can skip to a case body.
All hooks are guarded by per-offset match tables that are empty for code
without a match statement, so non-match input is unaffected.
Test: tests/input/match_statement.py exercises class patterns, a wildcard
sub-pattern, value patterns and `case _`. Full existing suite still passes.
Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
67 lines
1.6 KiB
C++
67 lines
1.6 KiB
C++
#ifndef _PYC_FILE_H
|
|
#define _PYC_FILE_H
|
|
|
|
#include <cstdio>
|
|
#include <ostream>
|
|
|
|
#ifdef WIN32
|
|
typedef __int64 Pyc_INT64;
|
|
#else
|
|
typedef long long Pyc_INT64;
|
|
#endif
|
|
|
|
class PycData {
|
|
public:
|
|
PycData() { }
|
|
virtual ~PycData() { }
|
|
|
|
virtual bool isOpen() const = 0;
|
|
virtual bool atEof() const = 0;
|
|
|
|
virtual int getByte() = 0;
|
|
virtual void getBuffer(int bytes, void* buffer) = 0;
|
|
int get16();
|
|
int get32();
|
|
Pyc_INT64 get64();
|
|
};
|
|
|
|
class PycFile : public PycData {
|
|
public:
|
|
PycFile(const char* filename);
|
|
~PycFile() { if (m_stream) fclose(m_stream); }
|
|
|
|
bool isOpen() const override { return (m_stream != 0); }
|
|
bool atEof() const override;
|
|
|
|
int getByte() override;
|
|
void getBuffer(int bytes, void* buffer) override;
|
|
|
|
private:
|
|
FILE* m_stream;
|
|
};
|
|
|
|
class PycBuffer : public PycData {
|
|
public:
|
|
PycBuffer(const void* buffer, int size)
|
|
: m_buffer((const unsigned char*)buffer), m_size(size), m_pos(0) { }
|
|
~PycBuffer() { }
|
|
|
|
bool isOpen() const override { return (m_buffer != 0); }
|
|
bool atEof() const override { return (m_pos == m_size); }
|
|
|
|
int getByte() override;
|
|
void getBuffer(int bytes, void* buffer) override;
|
|
|
|
// Reposition the read cursor (used by 3.11 match/case reconstruction to
|
|
// skip past pattern-test machinery to a case body).
|
|
void setPos(int pos) { m_pos = pos; }
|
|
|
|
private:
|
|
const unsigned char* m_buffer;
|
|
int m_size, m_pos;
|
|
};
|
|
|
|
int formatted_print(std::ostream& stream, const char* format, ...);
|
|
int formatted_printv(std::ostream& stream, const char* format, va_list args);
|
|
|
|
#endif
|