class SVG:
    def __init__(self, path, w, h, scale):
        self.path = path
        self.w = w
        self.h = h
        self.scale = scale

    def __enter__(self):
        vw, vh, pw, ph = self.w, self.h, self.w * self.scale, self.h * self.scale
        self.fp = open(self.path, "w")
        self.fp.write(f'<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 {vw} {vh}" width="{pw}" height="{ph}" stroke-width="0.25">\n')
        self.fp.write('<style>text { font: 4px sans-serif; }</style>\n')
        return self

    def __exit__(self, exc_type, exc_value, traceback):
        self.fp.write("</svg>\n")
        self.fp.close()

    def line(self, x1, y1, x2, y2, stroke="black"):
        self.fp.write(f'<line x1="{x1}" y1="{y1}" x2="{x2}" y2="{y2}" stroke="{stroke}"></line>\n')

    def rect(self, x, y, w, h, *, stroke="black", fill="#00000000"):
        self.fp.write(f'<rect x="{x}" y="{y}" width="{w}" height="{h}" stroke="{stroke}" fill="{fill}" />\n')

    def text(self, x, y, text, stroke="#00000000", fill="black"):
        self.fp.write(f'<text x="{x}" y="{y}" stroke="{stroke}" fill="{fill}" text-anchor="middle" dominant-baseline="middle">{text}</text>\n')

program = """
72 73 + P
"#004B87" f "#00000000" s
0 0 100 100 R
"#00000000" f "#2C6E91" s 12
10 0 10 100 L 17
20 0 20 100 L
30 0 30 100 L
40 0 40 100 L
50 0 50 100 L *
60 0 60 100 L
70 0 70 100 L
80 0 80 100 L
90 0 90 100 L 18
0 10 100 10 L
0 20 100 20 L
0 30 100 30 L
0 40 100 40 L +
0 50 100 50 L
0 60 100 60 L
0 70 100 70 L
0 80 100 80 L 19
0 90 100 90 L
"#A1C6EA" s
25 60 55 65 Lc 75 40 Lc 45 35 Lc 25 60 Lc
25 61 Lc 55 66 Lc 55 65 Lc
55 66 75 41 Lc 75 40 L
"#D1E6FF" f
0 52 65 50 50 T
* * * P
"""

svg = SVG("dec01.svg", 100, 100, 8)
# ... autres globales ...

def execute(program):
    # coder ici
    # utiliser print(v, end="") pour afficher une valeur v
    # utilier svg.xyz() pour dessiner dans le SVG
    pass

with svg:
    execute(program)
    print()
