#!/usr/bin/env python3

from scanner import scan
from parser  import parse
import ast
import sys

# This is the function that knows how to print a node
# with its parts indented beneath it:

INDENT_VALUE = 5

def print_node(node, indent=0):
    spaces = ' ' * indent
    if isinstance( node, ast.DrawingNode ):
        print( spaces + "Drawing" )
        for row in node.value():
            print_node(row, indent+INDENT_VALUE)
    elif isinstance( node, ast.RowNode ):
        print( spaces + "Row" )
        repeat, chunks = node.value()
        print_node(repeat, indent+INDENT_VALUE)
        for chunk in chunks:
            print_node(chunk, indent+INDENT_VALUE)
    elif isinstance( node, ast.RepeatNode ):
        print( spaces + "Repeat", node )
    elif isinstance( node, ast.ChunkNode ):
        count, char = node.value()
        print( spaces + "Chunk ", count, char )
    else:
        print('Illegal AST node:', node)

# The main script:

source_file    = open(sys.argv[1])
source_program = source_file.read()
token_list     = scan(source_program)
result         = parse(token_list)

if result:
    print_node(result)

