summaryrefslogtreecommitdiffstats
path: root/kalyptus/Ast.pm
blob: 0fb4bd0d4e691ed8f087918e954252a704d64647 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
package Ast;
use strict;

use vars qw/ $this $pack @endCodes /;

#-----------------------------------------------------------------------------
# This package is used  to create a simple Abstract Syntax tree. Each node
# in the AST is an associative array and supports two kinds of properties -
# scalars and lists of scalars.
# See SchemParser.pm for an example of usage.
#                                                               ... Sriram
#-----------------------------------------------------------------------------

# Constructor 
# e.g AST::New ("personnel")
# Stores the argument in a property called astNodeName whose sole purpose
# is to support Print()

sub New {
	my ($this) = {"astNodeName" => $_[0]};
	bless ($this);
	return $this;
}

# Add a property to this object
# $astNode->AddProp("className", "Employee");

sub AddProp {
	my ($this) = $_[0];
	$this->{$_[1]} = $_[2];
}

# Equivalent to AddProp, except the property name is associated
# with a list of values
# $classAstNode->AddProp("attrList", $attrAstNode);

sub AddPropList {
	my ($this) = $_[0];
	if (! exists $this->{$_[1]}) {
		$this->{$_[1]} = [];
	}
	push (@{$this->{$_[1]}}, $_[2]);
}

# Returns a list of all the property names of this object
sub GetProps {
	my ($this) = $_[0];
	return keys %{$this};
}

sub Visit {
    # Converts each of this AstNode's properties into global variables.
    # The global variables are introduced into package "main"
    # At the same time, a piece of code is  formed to undo this work above -
    # $endCode essentially contains the values of these global variables
    # before  they are mangled. endCode gets pushed into a stack (endCodes),
    # which is unwound by UnVisit().

    local ($this, $pack) = @_;


    my $code = ""; 
    my $endCode = "";


    foreach my $k (keys %{$this}) {

	my $glob = $pack."::".$k;

	if ( defined $$glob ) {

		if ( ${$glob} ne "" ) {
			$$glob =~ s/\'/\\\'/g; 
		}

	    $endCode .= '$'.$pack.'::'.$k. " = '".$$glob."';";
	} else {
	    $endCode .= '$'.$pack . "::". $k . ' = "";';
	}
	$code .= '$'.$pack . "::" . $k . "= \$this->{\"$k\"};";
    }
    push (@endCodes, $endCode);
    eval($code) if $code;
}

sub UnVisit {
    my $code = pop(@endCodes);
    eval($code) if ($code);
}

1;