The StaDyn Programming Language
Source code documentation of StaDyn, a hybrid static and dynamic typing language.
Block.cs
Go to the documentation of this file.
1 // -------------------------------------------------------------------------- //
3 // Project rROTOR //
4 // -------------------------------------------------------------------------- //
5 // File: Block.cs //
6 // Author: Cristina Gonzalez Muņoz - cristi.gm@gmail.com //
7 // Description: //
8 // Encapsulates a block of statements. //
9 // Inheritance: Statement. //
10 // Implements Composite pattern [Composite]. //
11 // Implements Visitor pattern [Concrete Element]. //
12 // -------------------------------------------------------------------------- //
13 // Create date: 11-12-2006 //
14 // Modification date: 12-12-2006 //
16 
17 using System;
18 using System.Collections.Generic;
19 using System.Text;
20 
21 using Tools;
22 using ErrorManagement;
23 
24 namespace AST
25 {
34  public class Block : Statement
35  {
36  #region Fields
37 
41  private List<Statement> statements;
42 
43  #endregion
44 
45  #region Properties
46 
50  public int StatementCount
51  {
52  get { return this.statements.Count; }
53  }
54 
55  #endregion
56 
57  #region Constructor
58 
65  public Block(Location location):base(location)
66  {
67  this.statements = new List<Statement>();
68  }
69 
77  public Block(List<Statement> stats, Location location):base(location)
78  {
79  this.statements = stats;
80  }
81 
82  #endregion
83 
84  #region AddStatement()
85 
90  public void AddStatement(Statement statement) {
91  if (statement != null)
92  this.statements.Add(statement);
93  }
94 
95  #endregion
96 
97  #region AddStatementToTheBeginning()
98 
103  public void AddStatementToTheBeginning(Statement statement)
104  {
105  this.statements.Insert(0, statement);
106  }
107 
108  #endregion
109 
110  #region AddStatementAtIndex()
111 
117  public void AddStatementAtIndex(Statement statement, int index)
118  {
119  this.statements.Insert(index, statement);
120  }
121 
122  #endregion
123 
124  #region SearchPosition()
125 
131  public int SearchPosition(string id)
132  {
133  for (int i = 0; i < this.statements.Count; i++)
134  {
135  if (this.statements[i] is IdDeclaration)
136  {
137  if (((IdDeclaration)this.statements[i]).Identifier.Equals(id))
138  return i;
139  }
140 
141  if (this.statements[i] is DeclarationSet)
142  {
143  if (((DeclarationSet)this.statements[i]).ContainsId(id))
144  return i;
145  }
146  }
147  return -1;
148  }
149 
150  #endregion
151 
152  #region GetStatementElement()
153 
159  public Statement GetStatementElement(int index)
160  {
161  return this.statements[index];
162  }
163 
164  #endregion
165 
166  #region Accept()
167 
174  public override Object Accept(Visitor v, Object o)
175  {
176  return v.Visit(this, o);
177  }
178 
179  #endregion
180  }
181 }
void AddStatementAtIndex(Statement statement, int index)
Add a new statement at the specified index.
Definition: Block.cs:117
Encapsulates a block of statements.
Definition: Block.cs:34
Abstract class to define different visits over the abstract syntax tree.
Definition: Visitor.cs:29
This class encapsulates a location in a specific file. Implements an Inmutable pattern. So it can be used in any context, that is his internal fields never change.
Definition: Location.cs:24
Abstract class represents a programming language statement.
Definition: Statement.cs:30
Block(Location location)
Constructor of Block
Definition: Block.cs:65
int StatementCount
Gets the number of statements.
Definition: Block.cs:51
void AddStatement(Statement statement)
Add a new statement to the end of the block.
Definition: Block.cs:90
int SearchPosition(string id)
Searches the identifier and returns its position.
Definition: Block.cs:131
Encapsulates a statement with several declarations.
Encapsulates a declaration.
Location location
Location: Encapsulates in one object the line, column and filename
Definition: AstNode.cs:39
void AddStatementToTheBeginning(Statement statement)
Add a new statement.
Definition: Block.cs:103
Block(List< Statement > stats, Location location)
Constructor of Block
Definition: Block.cs:77
override Object Accept(Visitor v, Object o)
Accept method of a concrete visitor.
Definition: Block.cs:174
Statement GetStatementElement(int index)
Gets the element stored in the specified index.
Definition: Block.cs:159