Block
using CLanguage.Interpreter;
using CLanguage.Types;
using System;
using System.Collections.Generic;
using System.Linq;
namespace CLanguage.Syntax
{
public class Block : Statement
{
public Location StartLocation { get; set; } = Location.Null;
public List<Statement> Statements { get; set; } = new List<Statement>();
public Location EndLocation { get; set; } = Location.Null;
public List<CompiledVariable> Variables { get; set; } = new List<CompiledVariable>();
public List<CompiledFunction> Functions { get; set; } = new List<CompiledFunction>();
public Dictionary<string, CType> Typedefs { get; set; } = new Dictionary<string, CType>();
public List<Statement> InitStatements { get; set; } = new List<Statement>();
public override bool AlwaysReturns => Statements.Any((Statement s) => s.AlwaysReturns);
public Block()
{
}
public Block(Location startLoc, List<Statement> statements, Location endLoc)
{
StartLocation = startLoc;
Statements.AddRange(statements);
EndLocation = endLoc;
}
public Block(Location startLoc, Location endLoc)
{
StartLocation = startLoc;
EndLocation = endLoc;
}
public Block(Block parent, Location startLoc)
{
StartLocation = startLoc;
EndLocation = Location.Null;
}
public void AddStatement(Statement stmt)
{
Statements.Add(stmt);
}
protected override void DoEmit(EmitContext ec)
{
ec.BeginBlock(this);
List<Statement>.Enumerator enumerator = InitStatements.GetEnumerator();
try {
while (enumerator.MoveNext()) {
enumerator.Current.Emit(ec);
}
} finally {
((IDisposable)enumerator).Dispose();
}
enumerator = Statements.GetEnumerator();
try {
while (enumerator.MoveNext()) {
enumerator.Current.Emit(ec);
}
} finally {
((IDisposable)enumerator).Dispose();
}
ec.EndBlock();
}
}
}