summaryrefslogtreecommitdiffstats
path: root/source4/scripting/python/examples
Commit message (Expand)AuthorAgeFilesLines
* s4-python: python is not always in /usr/binAndrew Tridgell2010-06-243-3/+3
* python: Cope with the dom_sid2 alias in pidl's python generating code.Jelmer Vernooij2009-07-301-2/+2
* DCE/RPC(Python): Rename py_talloc_import to py_talloc_steal.Jelmer Vernooij2009-07-301-1/+1
* Display lsa string contents.Jelmer Vernooij2009-01-071-2/+5
* Add working Python bindings for NBT.Jelmer Vernooij2008-06-111-0/+28
* Fix import.Jelmer Vernooij2008-05-261-1/+1
* Move some scripts to examples directory since they're not really generically ...Jelmer Vernooij2008-05-262-0/+201
='n110' href='#n110'>110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307
#/usr/bin/ruby

# $Id$
# vim: syntax=ruby

# the AST tree

# the parent class for all of our syntactical objects
module Puppet
    module Parser
        class ASTError < RuntimeError; end
        #---------------------------------------------------------------
        class AST
            Puppet.setdefault(:typecheck, true)
            Puppet.setdefault(:paramcheck, true)
            attr_accessor :line, :file, :parent

            @@pink = ""
            @@green = ""
            @@yellow = ""
            @@slate = ""
            @@reset = ""

            @@indent = " " * 4
            @@indline = @@pink + ("-" * 4) + @@reset
            @@midline = @@slate + ("-" * 4) + @@reset

            @@settypes = Hash.new { |hash,key|
                hash[key] = Hash.new(0)
            }

            def AST.indention
                return @@indent * @@indention
            end

            def AST.midline
                return @@midline
            end

            def evaluate(scope)
                #Puppet.debug("Evaluating ast %s" % @name)
                value = self.collect { |obj|
                    obj.safeevaluate(scope)
                }.reject { |obj|
                    obj.nil?
                }
            end

            def safeevaluate(*args)
                begin
                    self.evaluate(*args)
                rescue Puppet::DevError
                    raise
                rescue => detail
                    if Puppet[:debug]
                        puts caller
                    end
                    error = Puppet::DevError.new(
                        "Child of type %s failed: %s" %
                            [self.class, detail.to_s]
                    )
                    error.stack = caller
                    raise error
                end
            end

            def typewrap(string)
                #return self.class.to_s.sub(/.+::/,'') +
                    #"(" + @@green + string.to_s + @@reset + ")"
                return @@green + string.to_s + @@reset +
                    "(" + self.class.to_s.sub(/.+::/,'') + ")"
            end

            def initialize(args)
                # this has to wait until all of the objects are defined
                unless defined? @@klassorder
                    @@klassorder = [
                        AST::VarDef, AST::TypeDefaults,
                        AST::ObjectDef, AST::StatementArray
                    ]
                end

                args.each { |param,value|
                    method = param.to_s + "="
                    unless self.respond_to?(method)
                        error = Puppet::ParseError.new(
                            "Invalid parameter %s to object class %s" %
                                [method,self.class.to_s]
                        )
                        error.line = self.line
                        error.file = self.file
                        error.stack = caller
                        raise error
                    end

                    begin
                        #Puppet.debug("sending %s to %s" % [method, self.class])
                        self.send(method,value)
                    rescue => detail
                        error = Puppet::DevError.new(
                            "Could not set parameter %s on class %s: %s" %
                                [method,self.class.to_s,detail]
                        )
                        error.stack = caller
                        raise error
                    end
                }
            end

            #---------------------------------------------------------------
            # this differentiation is used by the interpreter
            # these objects have children
            class Branch < AST
                include Enumerable
                attr_accessor :pin, :children

                def each
                    @children.each { |child|
                        yield child
                    }
                end

                def evaluate(scope)
                    self.collect { |item|
                        item.safeevaluate(scope)
                    }.reject { |obj|
                        obj.nil
                    }
                end

                def initialize(arghash)
                    super(arghash)

                    unless defined? @children
                        @children = []
                    end

                    #puts "children is '%s'" % [@children]

                    self.each { |child|
                        if child.class == Array
                            error = Puppet::DevError.new(
                                "child for %s(%s) is array" % [self.class,self.parent]
                            )
                            error.stack = caller
                            raise error
                        end
                        unless child.nil?
                            child.parent = self
                        end
                    }
                end

                def tree(indent = 0)
                    return ((@@indline * indent) +
                        self.typewrap(self.pin)) + "\n" + self.collect { |child|
                            child.tree(indent + 1)
                    }.join("\n")
                end
            end
            #---------------------------------------------------------------

            #---------------------------------------------------------------
            class ASTArray < AST::Branch
                include Enumerable

                def [](index)
                    @children[index]
                end

                def evaluate(scope)
                    rets = nil
                    if scope.declarative
                        test = [
                            AST::VarDef, AST::TypeDefaults
                        ]

                        # if we're operating declaratively, then we want to get
                        # all of our 'setting' operations done first
                        settors = []
                        others = []
                        @children.each { |child|
                            if test.include?(child.class)
                                settors.push child
                            else
                                others.push child
                            end
                        }
                        rets = [settors,others].flatten.collect { |child|
                            child.safeevaluate(scope)
                        }
                    else
                        rets = @children.collect { |item|
                            item.safeevaluate(scope)
                        }
                    end
                    rets = rets.reject { |obj| obj.nil? }
                end

                def initialize(hash)
                    super(hash)

                    @children.each { |child|
                        unless child.is_a?(AST)
                            Puppet.err("child %s is not an ast" % child)
                            exit
                        end
                    }
                    return self
                end

                def push(*ary)
                    ary.each { |child|
                        #Puppet.debug "adding %s(%s) of type %s to %s" %
                        #    [child, child.object_id, child.class.to_s.sub(/.+::/,''),
                        #    self.object_id]
                        @children.push(child)
                    }

                    return self
                end

                def to_s
                    return "[" + @children.collect { |child|
                        child.to_s
                    }.join(", ") + "]"
                end

                def tree(indent = 0)
                    #puts((AST.indent * indent) + self.pin)
                    self.collect { |child|
                        if child.class == Array
                            Puppet.debug "child is array for %s" % self.class
                        end
                        child.tree(indent)
                    }.join("\n" + (AST.midline * (indent+1)) + "\n")
                end
            end
            #---------------------------------------------------------------

            #---------------------------------------------------------------
            class StatementArray < ASTArray
                def evaluate(scope)
                    rets = nil
                    if scope.declarative
                        # if we're operating declaratively, then we want to get
                        # all of our 'setting' operations done first
                        rets = @children.sort { |a,b|
                            [a,b].each { |i|
                                unless  @@klassorder.include?(i.class)
                                    error = Puppet::DevError.new(
                                        "Order not defined for %s" % i.class
                                    )
                                    error.stack = caller
                                    raise error
                                end
                            }
                            @@klassorder.index(a.class) <=> @@klassorder.index(b.class)
                        }.collect { |item|
                            Puppet.debug "Decl evaluating %s" % item.class
                            item.safeevaluate(scope)
                        }.reject { |obj| obj.nil? }
                    else
                        rets = @children.collect { |item|
                            item.safeevaluate(scope)
                        }.reject { |obj| obj.nil? }
                    end

                    return rets
                end
            end
            #---------------------------------------------------------------

            #---------------------------------------------------------------
            class ObjectInst < ASTArray; end
            #---------------------------------------------------------------

            #---------------------------------------------------------------
            # and these ones don't
            class Leaf < AST
                attr_accessor :value, :type

                # this only works if @value has already been evaluated
                # otherwise you get AST objects, which you don't likely want...
                def evaluate(scope)
                    return @value
                end

                def tree(indent = 0)
                    return ((@@indent * indent) + self.typewrap(self.value))
                end

                def to_s
                    return @value
                end
            end
            #---------------------------------------------------------------

            #---------------------------------------------------------------
            class Boolean < AST::Leaf
                def initialize(hash)
                    super

                    unless @value == 'true' or @value == 'false'
                        error = Puppet::DevError.new(
                            "'%s' is not a boolean" % @value
                        )
                        error.stack = caller
                        raise error
                    end
                    if @value == 'true'
                        @value = true
                    else
                        @value = false
                    end
                end

                def evaluate(scope)
                    return @value
                end

                def to_s
                    return @value
                end
            end
            #---------------------------------------------------------------

            #---------------------------------------------------------------
            class String < AST::Leaf
                def evaluate(scope)
                    return scope.strinterp(@value)
                end
            end
            #---------------------------------------------------------------

            #---------------------------------------------------------------
            class Word < AST::Leaf; end
            #---------------------------------------------------------------

            #---------------------------------------------------------------
            class Default < AST::Leaf; end
            #---------------------------------------------------------------

            #---------------------------------------------------------------
            class Type < AST::Leaf; end
            #---------------------------------------------------------------

            #---------------------------------------------------------------
            class Name < AST::Leaf; end
            #---------------------------------------------------------------

            #---------------------------------------------------------------
            class Variable < Word
                def evaluate(scope)
                    # look up the variable value in the symbol table
                    begin
                        return scope.lookupvar(@value)
                    rescue Puppet::ParseError => except
                        except.line = self.line
                        except.file = self.file
                        raise except
                    rescue => detail
                        error = Puppet::DevError.new(detail)
                        error.line = self.line
                        error.file = self.file
                        error.stack = caller
                        raise error
                    end
                end
            end
            #---------------------------------------------------------------

            #---------------------------------------------------------------
            class ObjectDef < AST::Branch
                attr_accessor :name, :type
                attr_reader :params

                def []=(index,obj)
                    @params[index] = obj
                end

                def [](index)
                    return @params[index]
                end

                def each
                    #Puppet.debug("each called on %s" % self)
                    [@type,@name,@params].flatten.each { |param|
                        #Puppet.debug("yielding param %s" % param)
                        yield param
                    }
                end

                def evaluate(scope)
                    hash = {}

                    objtype = @type.safeevaluate(scope)
                    objnames = @name.safeevaluate(scope)

                    # first, retrieve the defaults
                    begin
                        defaults = scope.lookupdefaults(objtype)
                    rescue => detail
                        error = Puppet::DevError.new(
                            "Could not lookup defaults for %s: %s" %
                                [objtype, detail.to_s]
                        )
                    end
                    defaults.each { |var,value|
                        Puppet.debug "Found default %s for %s" %
                            [var,objtype]

                        hash[var] = value
                    }

                    # then set all of the specified params
                    @params.each { |param|
                        ary = param.safeevaluate(scope)
                        hash[ary[0]] = ary[1]
                    }

                    # it's easier to always use an array, even for only one name
                    unless objnames.is_a?(Array)
                        objnames = [objnames]
                    end

                    begin
                        object = scope.lookuptype(objtype)
                    rescue Puppet::ParseError => except
                        except.line = self.line
                        except.file = self.file
                        raise except
                    rescue => detail
                        error = Puppet::ParseError.new(detail)
                        error.line = self.line
                        error.file = self.file
                        error.stack = caller
                        raise error
                    end

                    # this is where our implicit iteration takes place;
                    # if someone passed an array as the name, then we act
                    # just like the called us many times
                    objnames.collect { |objname|
                        # if the type is not defined in our scope, we assume
                        # that it's a type that the client will understand, so we
                        # just store it in our objectable
                        if object.nil?
                            begin
                                Puppet.debug("Setting object '%s' with arguments %s" %
                                    [objname, hash.inspect])
                                obj = scope.setobject(
                                    objtype,
                                    objname,
                                    hash,
                                    @file,
                                    @line
                                )
                            rescue Puppet::ParseError => except
                                except.line = self.line
                                except.file = self.file
                                raise except
                            rescue => detail
                                error = Puppet::ParseError.new(detail)
                                error.line = self.line
                                error.file = self.file
                                error.stack = caller
                                raise error
                            end
                        else
                            # but things like components create a new type; if we find
                            # one of those, evaluate that with our arguments
                            Puppet.debug("Calling object '%s' with arguments %s" %
                                [object.name, hash.inspect])
                            object.safeevaluate(scope,hash,objtype,objname)
                        end
                    }.reject { |obj| obj.nil? }
                end

                def initialize(hash)
                    super

                    # we don't have to evaluate because we require bare words
                    # for types
                    objtype = @type.value

                    # This will basically always be on, but I wanted to make it at
                    # least simple to turn off if it came to that
                    if Puppet[:typecheck]
                        builtin = false
                        begin
                            builtin = Puppet::Type.type(objtype)
                        rescue TypeError
                            # nothing; we've already set builtin to false
                        end
                        if builtin
                            # we're a builtin type
                            #Puppet.debug "%s is a builtin type" % objtype
                            # like :typecheck, this always defaults to on, but
                            # at least it's easy to turn off if necessary
                            if Puppet[:paramcheck]
                                @params.each { |param|
                                    #p self.name
                                    #p @params
                                    unless param.is_a?(AST::ObjectParam)
                                        raise Puppet::DevError,
                                            "Got something other than param"
                                    end
                                    begin
                                        pname = param.param.value
                                    rescue => detail
                                        raise Puppet::DevError, detail.to_s
                                    end
                                    next if pname == "name" # always allow these
                                    unless builtin.validarg?(pname)
                                        error = Puppet::ParseError.new(
                                            "Invalid parameter '%s' for type '%s'" %
                                                [pname,objtype]
                                        )
                                        error.stack = caller
                                        error.line = self.line
                                        error.file = self.file
                                        raise error
                                    end
                                }
                            end
                        # FIXME this should use scoping rules to find the set type,
                        # not a global list
                        elsif @@settypes.include?(objtype) 
                            # we've defined it locally
                            Puppet.debug "%s is a defined type" % objtype
                            hash = @@settypes[objtype]
                            @params.each { |param|
                                # FIXME we might need to do more here eventually...
                                if Puppet::Type.metaparam?(param.param.value.intern)
                                    next
                                end

                                begin
                                    pname = param.param.value
                                rescue => detail
                                    raise Puppet::DevError, detail.to_s
                                end
                                unless hash.include?(pname)
                                    error = Puppet::ParseError.new(
                                        "Invalid parameter '%s' for type '%s'" %
                                            [pname,objtype]
                                    )
                                    error.stack = caller
                                    error.line = self.line
                                    error.file = self.file
                                    raise error
                                end
                            }
                        else
                            # we don't know anything about it
                            error = Puppet::ParseError.new(
                                "Unknown type '%s'" % objtype
                            )
                            error.line = self.line
                            error.file = self.file
                            error.stack = caller
                            raise error
                        end
                    end
                end

                def params=(params)
                    if params.is_a?(AST::ASTArray)
                        @params = params
                    else
                        @params = AST::ASTArray.new(
                            :line => params.line,
                            :file => params.file,
                            :children => [params]
                        )
                    end
                end

                def tree(indent = 0)
                    return [
                        @type.tree(indent + 1),
                        @name.tree(indent + 1),
                        ((@@indline * indent) + self.typewrap(self.pin)),
                        @params.collect { |param|
                            begin
                                param.tree(indent + 1)
                            rescue NoMethodError => detail
                                Puppet.err @params.inspect
                                error = Puppet::DevError.new(
                                    "failed to tree a %s" % self.class
                                )
                                error.stack = caller
                                raise error
                            end
                        }.join("\n")
                    ].join("\n")
                end

                def to_s
                    return "%s => { %s }" % [@name,
                        @params.collect { |param|
                            param.to_s
                        }.join("\n")
                    ]
                end
            end
            #---------------------------------------------------------------

            #---------------------------------------------------------------
            class ObjectRef < AST::Branch
                attr_accessor :name, :type

                def each
                    #Puppet.debug("each called on %s" % self)
                    [@type,@name].flatten.each { |param|
                        #Puppet.debug("yielding param %s" % param)
                        yield param
                    }
                end

                def evaluate(scope)
                    objtype = @type.safeevaluate(scope)
                    objnames = @name.safeevaluate(scope)

                    # it's easier to always use an array, even for only one name
                    unless objnames.is_a?(Array)
                        objnames = [objnames]
                    end

                    begin
                        object = scope.lookuptype(objtype)
                    rescue Puppet::ParseError => except
                        except.line = self.line
                        except.file = self.file
                        raise except
                    rescue => detail
                        error = Puppet::ParseError.new(detail)
                        error.line = self.line
                        error.file = self.file
                        error.stack = caller
                        raise error
                    end
                    Puppet.debug "ObjectRef returned type %s" % object

                    # should we implicitly iterate here?
                    # yes, i believe that we essentially have to...
                    objnames.collect { |objname|
                        if object.is_a?(Component)
                            objname = "%s[%s]" % [objtype,objname]
                            objtype = "component"
                        end
                        [objtype,objname]
                    }.reject { |obj| obj.nil? }
                end

                def tree(indent = 0)
                    return [
                        @type.tree(indent + 1),
                        @name.tree(indent + 1),
                        ((@@indline * indent) + self.typewrap(self.pin))
                    ].join("\n")
                end

                def to_s
                    return "%s[%s]" % [@name,@type]
                end
            end
            #---------------------------------------------------------------

            #---------------------------------------------------------------
            class ObjectParam < AST::Branch
                attr_accessor :value, :param

                def each
                    [@param,@value].each { |child| yield child }
                end

                def evaluate(scope)
                    param = @param.safeevaluate(scope)
                    value = @value.safeevaluate(scope)
                    return [param, value]
                end

                def tree(indent = 0)
                    return [
                        @param.tree(indent + 1),
                        ((@@indline * indent) + self.typewrap(self.pin)),
                        @value.tree(indent + 1)
                    ].join("\n")
                end

                def to_s
                    return "%s => %s" % [@param,@value]
                end
            end
            #---------------------------------------------------------------

            #---------------------------------------------------------------
            class CaseStatement < AST::Branch
                attr_accessor :test, :options, :default

                # 'if' is a bit special, since we don't want to continue
                # evaluating if a test turns up true
                def evaluate(scope)
                    value = @test.safeevaluate(scope)

                    retvalue = nil
                    found = false
                    default = nil
                    @options.each { |option|
                        if option.eachvalue { |opval|
                            if opval == value
                                break true
                            end
                        }
                            # we found a matching option
                            retvalue = option.safeevaluate(scope)
                            break
                        end
                    }

                    unless found
                        if defined? @default
                            retvalue = @default.safeevaluate(scope)
                        else
                            Puppet.debug "No true answers and no default"
                        end
                    end
                    return retvalue
                end

                def initialize(hash)
                    values = {}

                    super
                    # this won't work if we move away from only allowing constants
                    # here
                    # but for now, it's fine and useful
                    @options.each { |option|
                        if option.default?
                            @default = option
                        end
                        option.eachvalue { |val|
                            if values.include?(val)
                                raise Puppet::ParseError,
                                    "Value %s appears twice in case statement" %
                                        val
                            else
                                values[val] = true
                            end
                        }
                    }
                end

                def tree(indent = 0)
                    rettree = [
                        @test.tree(indent + 1),
                        ((@@indline * indent) + self.typewrap(self.pin)),
                        @options.tree(indent + 1)
                    ]

                    return rettree.flatten.join("\n")
                end

                def each
                    [@test,@options].each { |child| yield child }
                end
            end
            #---------------------------------------------------------------

            #---------------------------------------------------------------
            class CaseOpt < AST::Branch
                attr_accessor :value, :statements

                # CaseOpt is a bit special -- we just want the value first,
                # so that CaseStatement can compare, and then it will selectively
                # decide whether to fully evaluate this option

                def default?
                    if defined? @default
                        return @default
                    end

                    if @value.is_a?(AST::ASTArray)
                        @value.each { |subval|
                            if subval.is_a?(AST::Default)
                                @default = true
                                break
                            end
                        }
                    else
                        if @value.is_a?(AST::Default)
                            @default = true
                        end
                    end

                    unless defined? @default
                        @default = false
                    end

                    return @default
                end

                def eachvalue
                    if @value.is_a?(AST::ASTArray)
                        @value.each { |subval|
                            yield subval.value
                        }
                    else
                        yield @value.value
                    end
                end

                def evaluate(scope)
                    return @statements.safeevaluate(scope.newscope)
                end

                def tree(indent = 0)
                    rettree = [
                        @value.tree(indent + 1),
                        ((@@indline * indent) + self.typewrap(self.pin)),
                        @statements.tree(indent + 1)
                    ]
                    return rettree.flatten.join("\n")
                end

                def each
                    [@value,@statements].each { |child| yield child }
                end
            end
            #---------------------------------------------------------------

            #---------------------------------------------------------------
            class Selector < AST::Branch
                attr_accessor :param, :value

                # okay, here's a decision point...
                def evaluate(scope)
                    # retrieve our values and make them a touch easier to manage
                    hash = Hash[*(@value.safeevaluate(scope).flatten)]

                    retvalue = nil

                    paramvalue = @param.safeevaluate(scope)

                    retvalue = hash.detect { |test,value|
                        # FIXME this will return variables named 'default'...
                        if paramvalue == test
                            break value
                        end
                    }
                    if retvalue.nil?
                        if hash.include?("default")
                            return hash["default"]
                        else
                            error = Puppet::ParseError.new(
                                "No value for selector param '%s'" % paramvalue
                            )
                            error.line = self.line
                            error.file = self.file
                            error.stack = self.stack
                            raise error
                        end
                    end

                    return retvalue
                end

                def tree(indent = 0)
                    return [
                        @param.tree(indent + 1),
                        ((@@indline * indent) + self.typewrap(self.pin)),
                        @value.tree(indent + 1)
                    ].join("\n")
                end

                def each
                    [@param,@value].each { |child| yield child }
                end
            end
            #---------------------------------------------------------------

            #---------------------------------------------------------------
            class VarDef < AST::Branch
                attr_accessor :name, :value

                def evaluate(scope)
                    name = @name.safeevaluate(scope)
                    value = @value.safeevaluate(scope)

                    Puppet.debug "setting %s to %s" % [name,value]
                    begin
                        scope.setvar(name,value)
                    rescue Puppet::ParseError => except
                        except.line = self.line
                        except.file = self.file
                        raise except
                    rescue => detail
                        error = Puppet::ParseError.new(detail)
                        error.line = self.line
                        error.file = self.file
                        error.stack = caller
                        raise error
                    end
                end

                def each
                    [@name,@value].each { |child| yield child }
                end

                def tree(indent = 0)
                    return [
                        @name.tree(indent + 1),
                        ((@@indline * 4 * indent) + self.typewrap(self.pin)),
                        @value.tree(indent + 1)
                    ].join("\n")
                end

                def to_s
                    return "%s => %s" % [@name,@value]
                end
            end
            #---------------------------------------------------------------

            #---------------------------------------------------------------
            class TypeDefaults < AST::Branch
                attr_accessor :type, :params

                def each
                    [@type,@params].each { |child| yield child }
                end

                def evaluate(scope)
                    type = @type.safeevaluate(scope)
                    params = @params.safeevaluate(scope)

                    #Puppet.info "Params are %s" % params.inspect
                    #Puppet.debug("evaluating '%s.%s' with values [%s]" %
                    #    [type,name,values])
                    # okay, now i need the interpreter's client object thing...
                    begin
                        scope.setdefaults(type.downcase,params)
                    rescue Puppet::ParseError => except
                        except.line = self.line
                        except.file = self.file
                        raise except
                    rescue => detail
                        error = Puppet::ParseError.new(detail)
                        error.line = self.line
                        error.file = self.file
                        error.stack = caller
                        raise error
                    end
                end

                def tree(indent = 0)
                    return [
                        @type.tree(indent + 1),
                        ((@@indline * 4 * indent) + self.typewrap(self.pin)),
                        @params.tree(indent + 1)
                    ].join("\n")
                end

                def to_s
                    return "%s { %s }" % [@type,@params]
                end
            end
            #---------------------------------------------------------------

            #---------------------------------------------------------------
            # these are analogous to defining new object types
            class CompDef < AST::Branch
                attr_accessor :name, :args, :code

                def each
                    [@name,@args,@code].each { |child| yield child }
                end

                def evaluate(scope)
                    name = @name.safeevaluate(scope)
                    args = @args.safeevaluate(scope)

                    #Puppet.debug("defining '%s' with arguments [%s]" %
                    #    [name,args])
                    #p @args
                    #p args
                    # okay, now i need to evaluate all of the statements
                    # within a component and a new lexical scope...

                    begin
                        scope.settype(name,
                            Component.new(
                                :name => name,
                                :args => args,
                                :code => @code
                            )
                        )
                    rescue Puppet::ParseError => except
                        except.line = self.line
                        except.file = self.file
                        raise except
                    rescue => detail
                        error = Puppet::ParseError.new(detail)
                        error.line = self.line
                        error.file = self.file
                        error.stack = caller
                        raise error
                    end
                end

                def initialize(hash)
                    super

                    Puppet.debug "Defining type %s" % @name.value

                    # we need to both mark that a given argument is valid,
                    # and we need to also store any provided default arguments
                    hash = @@settypes[@name.value]
                    if @args.is_a?(AST::ASTArray)
                        @args.each { |ary|
                            if ary.is_a?(AST::ASTArray)
                                arg = ary[0]
                                hash[arg.value] += 1
                            else
                                hash[ary.value] += 1
                            end
                        }
                    else
                        #Puppet.warning "got arg %s" % @args.inspect
                        hash[@args.value] += 1
                    end
                end

                def tree(indent = 0)
                    return [
                        @name.tree(indent + 1),
                        ((@@indline * 4 * indent) + self.typewrap("define")),
                        @args.tree(indent + 1),
                        @code.tree(indent + 1),
                    ].join("\n")
                end

                def to_s
                    return "define %s(%s) {\n%s }" % [@name, @args, @code]
                end
            end
            #---------------------------------------------------------------

            #---------------------------------------------------------------
            # these are analogous to defining new object types
            class ClassDef < AST::CompDef
                attr_accessor :parentclass

                def each
                    [@name,@args,@parentclass,@code].each { |child| yield child }
                end

                def evaluate(scope)
                    name = @name.safeevaluate(scope)
                    args = @args.safeevaluate(scope)

                    #Puppet.debug "evaluating parent %s of type %s" %
                    #    [@parent.name, @parent.class]
                    parent = @parentclass.safeevaluate(scope)

                    Puppet.debug("defining hostclass '%s' with arguments [%s]" %
                        [name,args])

                    begin
                        scope.settype(name,
                            HostClass.new(
                                :name => name,
                                :args => args,
                                :parent => parent,
                                :code => @code
                            )
                        )
                    rescue Puppet::ParseError => except
                        except.line = self.line
                        except.file = self.file
                        raise except
                    rescue => detail
                        error = Puppet::ParseError.new(detail)
                        error.line = self.line
                        error.file = self.file
                        error.stack = caller
                        raise error
                    end
                end

                def tree(indent = 0)
                    return [
                        @name.tree(indent + 1),
                        ((@@indline * 4 * indent) + self.typewrap("class")),
                        @args.tree(indent + 1),
                        @parentclass.tree(indent + 1),
                        @code.tree(indent + 1),
                    ].join("\n")
                end

                def to_s
                    return "class %s(%s) inherits %s {\n%s }" %
                        [@name, @args, @parentclass, @code]
                end
            end
            #---------------------------------------------------------------

            #---------------------------------------------------------------
            # host definitions are special, because they get called when a host
            # whose name matches connects
            class NodeDef < AST::Branch
                attr_accessor :names, :code

                def each
                    [@names,@code].each { |child| yield child }
                end

                def evaluate(scope)
                    names = @names.safeevaluate(scope)

                    unless names.is_a?(Array)
                        names = [names]
                    end
                    Puppet.debug("defining hosts '%s'" % [names.join(", ")])

                    names.each { |name|
                        begin
                            scope.sethost(name,
                                Host.new(
                                    :name => name,
                                    :code => @code
                                )
                            )
                        rescue Puppet::ParseError => except
                            except.line = self.line
                            except.file = self.file
                            raise except
                        rescue => detail
                            error = Puppet::ParseError.new(detail)
                            error.line = self.line
                            error.file = self.file
                            error.stack = caller
                            raise error
                        end
                    }
                end

                def tree(indent = 0)
                    return [
                        @names.tree(indent + 1),
                        ((@@indline * 4 * indent) + self.typewrap("host")),
                        @code.tree(indent + 1),
                    ].join("\n")
                end

                def to_s
                    return "host %s {\n%s }" % [@name, @code]
                end
            end
            #---------------------------------------------------------------

            #---------------------------------------------------------------
            # this is not really an AST node; it's just a placeholder
            # for a bunch of AST code to evaluate later
            class Component < AST::Branch
                attr_accessor :name, :args, :code

                def evaluate(scope,hash,objtype,objname)
                    scope = scope.newscope
                    scope.type = objtype
                    scope.name = objname

                    # define all of the arguments in our local scope
                    if self.args
                        Puppet.debug "args are %s" % self.args.inspect
                        self.args.each { |arg, default|
                            unless hash.include?(arg)
                                # FIXME this needs to test for 'defined?'
                                if defined? default
                                    hash[arg] = default
                                else
                                    error = Puppet::ParseError.new(
                                        "Must pass %s to %s of type %s" %
                                            [arg.inspect,name,objtype]
                                    )
                                    error.line = self.line
                                    error.file = self.file
                                    error.stack = caller
                                    raise error
                                end
                            end
                        }
                    end

                    hash["name"] = objname
                    hash.each { |arg,value|
                        begin
                            scope.setvar(arg,hash[arg])
                        rescue Puppet::ParseError => except
                            except.line = self.line
                            except.file = self.file
                            raise except
                        rescue Puppet::ParseError => except
                            except.line = self.line
                            except.file = self.file
                            raise except
                        rescue => except
                            error = Puppet::ParseError.new(except.message)
                            error.line = self.line
                            error.file = self.file
                            error.stack = caller
                            raise error
                        end
                    }

                    # now just evaluate the code with our new bindings
                    self.code.safeevaluate(scope)
                end
            end
            #---------------------------------------------------------------

            #---------------------------------------------------------------
            # this is not really an AST node; it's just a placeholder
            # for a bunch of AST code to evaluate later
            class HostClass < AST::Component
                attr_accessor :parentclass

                def evaluate(scope,hash,objtype,objname)
                    if @parentclass
                        begin
                            parentobj = scope.lookuptype(@parentclass)
                        rescue Puppet::ParseError => except
                            except.line = self.line
                            except.file = self.file
                            raise except
                        rescue => detail
                            error = Puppet::ParseError.new(detail)
                            error.line = self.line
                            error.file = self.file
                            raise error
                        end
                        unless parentobj
                            error = Puppet::ParseError.new( 
                                "Could not find parent '%s' of '%s'" % [@parentclass,@name])
                            error.line = self.line
                            error.file = self.file
                            raise error
                        end
                        parentobj.safeevaluate(scope,hash,objtype,objname)
                    end

                    # just use the Component evaluate method, but change the type
                    # to our own type
                    super(scope,hash,@name,objname)
                end

                def initialize(hash)
                    @parentclass = nil
                    super
                    if self.parent.is_a?(Array)
                        self.parent = nil
                    end
                end

            end
            #---------------------------------------------------------------

            #---------------------------------------------------------------
            # this is not really an AST node; it's just a placeholder
            # for a bunch of AST code to evaluate later
            class Host < AST::Component
                attr_accessor :name, :args, :code, :parentclass

                def evaluate(scope,hash,objtype,objname)
                    if @parentclass
                        begin
                            parentobj = scope.lookuptype(@parentclass)
                        rescue Puppet::ParseError => except
                            except.line = self.line
                            except.file = self.file
                            raise except
                        rescue => detail
                            error = Puppet::ParseError.new(detail)
                            error.line = self.line
                            error.file = self.file
                            raise error
                        end
                        unless parentobj
                            error = Puppet::ParseError.new( 
                                "Could not find parent '%s' of '%s'" % [@parentclass,@name])
                            error.line = self.line
                            error.file = self.file
                            raise error
                        end
                        parentobj.safeevaluate(scope,hash,objtype,objname)
                    end

                    # just use the Component evaluate method, but change the type
                    # to our own type
                    super(scope,hash,@name,objname)
                end
            end
            #---------------------------------------------------------------
        end
    end
end