simplification
Download
81 lines 2.5 KiBNomsuModified 2026-11-08executable raw
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
#!/usr/bin/env nomsu -V8.0.0
###
    Tool to print out a parse tree of files in an easy-to-read format. Usage:
    nomsu tools/parse.nom file1 file2 directory1 ...        
    
use "filesystem"
use "commandline"

(print tree $t at indent $indent) means:
    when $t.type is:
        "Action":
            say "\($indent)Action (\($t.stub)):"
            for $arg in $t:
                if ($arg is syntax tree):
                    print tree $arg at indent "\$indent  "
        
        "MethodCall":
            say "\($indent)MethodCall on:"
            print tree $t.1 at indent "\$indent    "
            print tree $t.2 at indent "\$indent    "
        
        "Number":
            say "\$indent\($t.1)"
        
        "Var":
            say "\($indent)$\($t.1)"
        
        else:
            say "\$indent\($t.type):"
            for $arg in $t:
                when:
                    ($arg is syntax tree):
                        print tree $arg at indent "\$indent  "
                    
                    else:
                        say "\$indent  \(quote $arg)"

($ as xml) means:
    when (type of $) is:
        "a Syntax Tree":
            $body = ((($bit as xml) for $bit in $) joined with " ")
            if ($.type == "Action"):
                return "<Action name=\"\(($, get stub) as xml)\">\($body)</Action>"
            ..else:
                return "<\($.type)>\($body)</\($.type)>"
        
        "Text":
            return
                $, with "&" -> "&amp;", with "\"" -> "&quot;", with "'" -> "&apos;"
                .., with "<" -> "&lt;", with ">" -> "&gt;"
        
        else:
            return "\$"

($ as lisp) means:
    when (type of $) is:
        "a Syntax Tree":
            $body = (({$.type} concatenated with (($bit as lisp) for $bit in $)) joined with " ")
            return "(\$body)"
        
        "Text":
            return "\"\($, with "\\" -> "\\\\", with "\"" -> "\\\"", with "\n" -> "\\n")\""
        
        else:
            return "\$"

command line program with $args:
    for $filename in $args.extras:
        $file = (read file $filename)
        unless $file:
            fail "File does not exist: \$filename"
        $nomsu = (NomsuCode from (Source $filename 1 #$file) $file)
        $tree = ($nomsu parsed)
        when:
            ($args.x or $args.xml):
                say ($tree as xml)
            
            ($args.l or $args.lisp):
                say ($tree as lisp)
            
            else:
                print tree $tree at indent ""