|
/**
|
|
*
|
|
* OOAS Compiler (Deprecated)
|
|
*
|
|
* Copyright 2015, Institute for Software Technology, Graz University of
|
|
* Technology. Portions are copyright 2015 by the AIT Austrian Institute
|
|
* of Technology. All rights reserved.
|
|
*
|
|
* SEE THE "LICENSE" FILE FOR THE TERMS UNDER WHICH THIS FILE IS PROVIDED.
|
|
*
|
|
* Please notice that this version of the OOAS compiler is considered de-
|
|
* precated. Only the Java version is maintained.
|
|
*
|
|
* Contributors:
|
|
* Willibald Krenn (TU Graz/AIT)
|
|
* Stefan Tiran (TU Graz/AIT)
|
|
*/
|
|
|
|
using System;
|
|
using System.Collections.Generic;
|
|
|
|
|
|
namespace TUG.Mogentes
|
|
{
|
|
public enum StatementKind
|
|
{
|
|
GuardedCommand,
|
|
MethodCall,
|
|
Assignment,
|
|
SeqBlock,
|
|
NondetBlock,
|
|
PrioBlock,
|
|
Skip, Abort, Kill,
|
|
QualConstraint
|
|
}
|
|
|
|
|
|
///////////////////////////////////////////////
|
|
/// basic Statement
|
|
///
|
|
public abstract class Statement : UlyssesBasicClass, IAst
|
|
{
|
|
private StatementKind m_kind;
|
|
private int m_line;
|
|
private int m_pos;
|
|
|
|
public StatementKind kind { get { return m_kind; } }
|
|
public int line { get { return m_line; } }
|
|
public int pos { get { return m_pos; } }
|
|
|
|
public Statement(StatementKind aKind, int aline, int apos)
|
|
{
|
|
m_kind = aKind;
|
|
m_line = aline;
|
|
m_pos = apos;
|
|
}
|
|
|
|
public Statement(Statement toCopy)
|
|
{
|
|
m_kind = toCopy.m_kind;
|
|
m_line = toCopy.m_line;
|
|
m_pos = toCopy.m_pos;
|
|
}
|
|
|
|
public virtual Statement Clone()
|
|
{
|
|
throw new NotImplementedException();
|
|
}
|
|
|
|
public AstNodeTypeEnum nodeType { get { return AstNodeTypeEnum.statement; } }
|
|
|
|
public virtual void Accept(IAstVisitor visitor)
|
|
{
|
|
throw new NotImplementedException();
|
|
}
|
|
}
|
|
|
|
|
|
///////////////////////////////////////////////
|
|
/// Qualitative Constraints
|
|
///
|
|
public enum QualitativeConstraintOperation { Equal, Deriv, Sum, Prod, Diff }
|
|
public sealed class QualitativeConstraintStatement : Statement
|
|
{
|
|
private Identifier m_variable0;
|
|
private Identifier m_variable1;
|
|
private Identifier m_variable2;
|
|
private QualitativeConstraintOperation m_operation;
|
|
public bool tag = false; // says that this is an obs assignment (important for prolog code target)
|
|
|
|
public Identifier variable0 { get { return m_variable0; } }
|
|
public Identifier variable1 { get { return m_variable1; } }
|
|
public Identifier variable2 { get { return m_variable2; } }
|
|
public QualitativeConstraintOperation operation { get { return m_operation; } }
|
|
|
|
public QualitativeConstraintStatement(Identifier var0, Identifier var1, QualitativeConstraintOperation op, int aline, int apos)
|
|
: base(StatementKind.QualConstraint, aline, apos)
|
|
{
|
|
if (op == QualitativeConstraintOperation.Diff
|
|
|| op == QualitativeConstraintOperation.Prod
|
|
|| op == QualitativeConstraintOperation.Sum)
|
|
throw new ArgumentException();
|
|
m_variable0 = var0;
|
|
m_variable1 = var1;
|
|
m_variable2 = null;
|
|
m_operation = op;
|
|
}
|
|
|
|
public QualitativeConstraintStatement(QualitativeConstraintStatement toCopy)
|
|
: base(toCopy)
|
|
{
|
|
m_variable0 = toCopy.m_variable0;
|
|
m_variable1 = toCopy.m_variable1;
|
|
m_variable2 = toCopy.m_variable2;
|
|
m_operation = toCopy.m_operation;
|
|
tag = toCopy.tag;
|
|
}
|
|
|
|
|
|
public QualitativeConstraintStatement(Identifier var0, Identifier var1, Identifier var2, QualitativeConstraintOperation op, int aline, int apos)
|
|
: base(StatementKind.QualConstraint, aline, apos)
|
|
{
|
|
if (op == QualitativeConstraintOperation.Deriv
|
|
|| op == QualitativeConstraintOperation.Equal)
|
|
throw new ArgumentException();
|
|
m_variable0 = var0;
|
|
m_variable1 = var1;
|
|
m_variable2 = var2;
|
|
m_operation = op;
|
|
}
|
|
|
|
public override void Accept(IAstVisitor visitor)
|
|
{
|
|
visitor.visit(this);
|
|
}
|
|
|
|
public override Statement Clone()
|
|
{
|
|
return new QualitativeConstraintStatement(this);
|
|
}
|
|
|
|
public void SetVariable0(Identifier newVal)
|
|
{
|
|
m_variable0 = newVal;
|
|
}
|
|
|
|
public void SetVariable1(Identifier newVal)
|
|
{
|
|
m_variable1 = newVal;
|
|
}
|
|
|
|
public void SetVariable2(Identifier newVal)
|
|
{
|
|
m_variable2 = newVal;
|
|
}
|
|
}
|
|
|
|
///////////////////////////////////////////////
|
|
/// Blocks
|
|
///
|
|
public abstract class Block : Statement, IScope
|
|
{
|
|
private SymbolTable m_symbols;
|
|
private LinkedList<Statement> m_statements;
|
|
private Expression m_filter; /*gets transformed to guarded command in resolvevisitor!*/
|
|
private IScope m_parentScope;
|
|
|
|
public SymbolTable symbols { get { return m_symbols; } }
|
|
public LinkedList<Statement> statements { get { return m_statements; } }
|
|
public Expression filter { get { return m_filter; } }
|
|
|
|
public Block(StatementKind aKind, int aline, int apos)
|
|
: base(aKind, aline, apos)
|
|
{
|
|
m_symbols = new SymbolTable();
|
|
m_statements = new LinkedList<Statement>();
|
|
}
|
|
|
|
public Block(Block toCopy)
|
|
: base(toCopy)
|
|
{
|
|
m_symbols = new SymbolTable(toCopy.m_symbols);
|
|
m_statements = new LinkedList<Statement>(toCopy.m_statements);
|
|
m_parentScope = toCopy.m_parentScope;
|
|
m_filter = toCopy.m_filter;
|
|
}
|
|
|
|
public override Statement Clone()
|
|
{
|
|
throw new NotImplementedException();
|
|
}
|
|
|
|
public void AddStatement(Statement toAdd)
|
|
{
|
|
m_statements.AddLast(toAdd);
|
|
}
|
|
|
|
public void SetStatements(LinkedList<Statement> newStatements)
|
|
{
|
|
m_statements = newStatements;
|
|
}
|
|
|
|
public void AddIdentifier(Identifier anIdentifier)
|
|
{
|
|
m_symbols.AddIdentifier(anIdentifier);
|
|
}
|
|
|
|
public Identifier ResolveIdentifier(string aName)
|
|
{
|
|
if (m_symbols.Defined(aName))
|
|
return m_symbols.Get(aName);
|
|
else
|
|
return null;
|
|
}
|
|
|
|
public IScope GetParentScope()
|
|
{
|
|
return m_parentScope;
|
|
}
|
|
|
|
public void SetParentScope(IScope parentScope)
|
|
{
|
|
m_parentScope = parentScope;
|
|
}
|
|
|
|
public void AddIdentifier(Identifier anIdentifier, object tag)
|
|
{
|
|
m_symbols.AddIdentifier(anIdentifier);
|
|
}
|
|
|
|
|
|
internal void SetFilter(Expression sexpr)
|
|
{
|
|
m_filter = sexpr;
|
|
}
|
|
}
|
|
|
|
public sealed class NondetBlock : Block
|
|
{
|
|
public NondetBlock(int aline, int apos)
|
|
: base(StatementKind.NondetBlock, aline, apos)
|
|
{ }
|
|
|
|
public NondetBlock(Block aParent, int aline, int apos)
|
|
: base(StatementKind.NondetBlock, aline, apos)
|
|
{
|
|
if (aParent != null)
|
|
aParent.AddStatement(this);
|
|
}
|
|
|
|
public NondetBlock(NondetBlock toCopy)
|
|
: base(toCopy)
|
|
{ }
|
|
|
|
public override Statement Clone()
|
|
{
|
|
return new NondetBlock(this);
|
|
}
|
|
|
|
public override void Accept(IAstVisitor visitor)
|
|
{
|
|
visitor.visit(this);
|
|
}
|
|
}
|
|
|
|
public sealed class SeqBlock : Block
|
|
{
|
|
public SeqBlock(int aline, int apos)
|
|
: base(StatementKind.SeqBlock, aline, apos)
|
|
{ }
|
|
|
|
public SeqBlock(Block aParent, int aline, int apos)
|
|
: base(StatementKind.SeqBlock, aline, apos)
|
|
{
|
|
if (aParent != null)
|
|
aParent.AddStatement(this);
|
|
}
|
|
|
|
public SeqBlock(SeqBlock toCopy)
|
|
: base(toCopy)
|
|
{ }
|
|
|
|
public override Statement Clone()
|
|
{
|
|
return new SeqBlock(this);
|
|
}
|
|
|
|
public override void Accept(IAstVisitor visitor)
|
|
{
|
|
visitor.visit(this);
|
|
}
|
|
}
|
|
|
|
public sealed class PrioBlock : Block
|
|
{
|
|
public PrioBlock(int aline, int apos)
|
|
: base(StatementKind.PrioBlock, aline, apos)
|
|
{ }
|
|
|
|
public PrioBlock(Block aParent, int aline, int apos)
|
|
: base(StatementKind.PrioBlock, aline, apos)
|
|
{
|
|
if (aParent != null)
|
|
aParent.AddStatement(this);
|
|
}
|
|
|
|
public PrioBlock(PrioBlock toCopy)
|
|
: base(toCopy)
|
|
{ }
|
|
|
|
public override Statement Clone()
|
|
{
|
|
return new PrioBlock(this);
|
|
}
|
|
|
|
public override void Accept(IAstVisitor visitor)
|
|
{
|
|
visitor.visit(this);
|
|
}
|
|
}
|
|
|
|
|
|
|
|
///////////////////////////////////////////////
|
|
/// Guarded Command
|
|
///
|
|
public sealed class GuardedCommand : Statement
|
|
{
|
|
private Expression m_guard;
|
|
private Block m_body;
|
|
private bool m_isQualitative;
|
|
|
|
public Expression guard { get { return m_guard; } }
|
|
public Block body { get { return m_body; } }
|
|
public bool isQualitative { get { return m_isQualitative; } }
|
|
|
|
public GuardedCommand(Expression aGuard, Block aBlock, int aline, int apos)
|
|
: base(StatementKind.GuardedCommand, aline, apos)
|
|
{
|
|
m_guard = aGuard;
|
|
m_body = aBlock;
|
|
m_isQualitative = false;
|
|
}
|
|
|
|
public GuardedCommand(GuardedCommand toCopy)
|
|
: base(toCopy)
|
|
{
|
|
m_guard = toCopy.m_guard;
|
|
m_body = toCopy.m_body;
|
|
m_isQualitative = toCopy.m_isQualitative;
|
|
}
|
|
|
|
public override Statement Clone()
|
|
{
|
|
return new GuardedCommand(this);
|
|
}
|
|
|
|
public override void Accept(IAstVisitor visitor)
|
|
{
|
|
visitor.visit(this);
|
|
}
|
|
|
|
public void SetGuard(Expression newGuard)
|
|
{
|
|
if (newGuard == null)
|
|
throw new ArgumentException();
|
|
m_guard = newGuard;
|
|
}
|
|
|
|
public void SetBody(Block newBody)
|
|
{
|
|
if (newBody == null)
|
|
throw new ArgumentException();
|
|
m_body = newBody;
|
|
}
|
|
|
|
public void SetIsQualitative(bool newVal)
|
|
{
|
|
m_isQualitative = newVal;
|
|
}
|
|
}
|
|
|
|
|
|
///////////////////////////////////////////////
|
|
/// Assignment
|
|
///
|
|
public sealed class Assignment : Statement, IScope
|
|
{
|
|
private LinkedList<Expression> m_places;
|
|
private LinkedList<Expression> m_values;
|
|
private Expression m_nondetExpression;
|
|
private IScope m_prarentScope;
|
|
private SymbolTable m_symbols; // needed for nondeterminsitic assignment (we most likely will have new variables)
|
|
|
|
public LinkedList<Expression> places { get { return m_places; } }
|
|
public LinkedList<Expression> values { get { return m_values; } }
|
|
public Expression nondetExpression { get { return m_nondetExpression; } }
|
|
public SymbolTable symbols { get { return m_symbols; } }
|
|
|
|
public Assignment(Expression place, Expression value, Expression nondetExpression, int aline, int apos)
|
|
: base(StatementKind.Assignment, aline, apos)
|
|
{
|
|
m_places = new LinkedList<Expression>();
|
|
m_values = new LinkedList<Expression>();
|
|
if (place != null)
|
|
m_places.AddFirst(place);
|
|
if (value != null)
|
|
m_values.AddFirst(value);
|
|
m_nondetExpression = nondetExpression;
|
|
m_symbols = new SymbolTable();
|
|
}
|
|
|
|
public Assignment(Assignment toCopy)
|
|
: base(toCopy)
|
|
{
|
|
m_places = new LinkedList<Expression>(toCopy.m_places);
|
|
m_values = new LinkedList<Expression>(toCopy.m_values);
|
|
m_nondetExpression = toCopy.m_nondetExpression;
|
|
m_symbols = new SymbolTable(toCopy.symbols);
|
|
}
|
|
|
|
public override Statement Clone()
|
|
{
|
|
return new Assignment(this);
|
|
}
|
|
|
|
public void AddPlace(Expression aPlace)
|
|
{
|
|
m_places.AddLast(aPlace);
|
|
}
|
|
|
|
public void AddValue(Expression aValue)
|
|
{
|
|
m_values.AddLast(aValue);
|
|
}
|
|
|
|
public void SetNondetExpression(Expression anExpr)
|
|
{
|
|
m_nondetExpression = anExpr;
|
|
}
|
|
|
|
public override void Accept(IAstVisitor visitor)
|
|
{
|
|
visitor.visit(this);
|
|
}
|
|
|
|
#region IScope Member
|
|
|
|
public Identifier ResolveIdentifier(string aName)
|
|
{
|
|
if (m_symbols.Defined(aName))
|
|
return m_symbols.Get(aName);
|
|
else
|
|
return null;
|
|
}
|
|
|
|
public IScope GetParentScope()
|
|
{
|
|
return m_prarentScope;
|
|
}
|
|
|
|
public void SetParentScope(IScope parentScope)
|
|
{
|
|
m_prarentScope = parentScope;
|
|
}
|
|
|
|
public void AddIdentifier(Identifier anIdentifier, object tag)
|
|
{
|
|
m_symbols.AddIdentifier(anIdentifier);
|
|
}
|
|
|
|
#endregion
|
|
}
|
|
|
|
|
|
///////////////////////////////////////////////
|
|
/// Methodcall
|
|
///
|
|
public sealed class Call : Statement
|
|
{
|
|
private Expression m_callExpression;
|
|
|
|
public Expression callExpression { get { return m_callExpression; } }
|
|
|
|
public Call(Expression callExpression, int aline, int apos)
|
|
: base(StatementKind.MethodCall, aline, apos)
|
|
{
|
|
m_callExpression = callExpression;
|
|
}
|
|
|
|
public Call(Call toCopy)
|
|
: base(toCopy)
|
|
{
|
|
m_callExpression = toCopy.m_callExpression;
|
|
}
|
|
|
|
public override Statement Clone()
|
|
{
|
|
return new Call(this);
|
|
}
|
|
|
|
public override void Accept(IAstVisitor visitor)
|
|
{
|
|
visitor.visit(this);
|
|
}
|
|
|
|
public void SetCallExpression(Expression newExpression)
|
|
{
|
|
//if (newExpression == null)
|
|
// throw new ArgumentException();
|
|
m_callExpression = newExpression;
|
|
}
|
|
}
|
|
|
|
|
|
///////////////////////////////////////////////
|
|
/// Simple Statements
|
|
///
|
|
public sealed class SkipStatement : Statement
|
|
{
|
|
public SkipStatement(int aline, int apos)
|
|
: base(StatementKind.Skip, aline, apos)
|
|
{ }
|
|
|
|
public SkipStatement(SkipStatement copy)
|
|
: base(copy)
|
|
{ }
|
|
|
|
public override Statement Clone()
|
|
{
|
|
return new SkipStatement(this);
|
|
}
|
|
|
|
public override void Accept(IAstVisitor visitor)
|
|
{
|
|
visitor.visit(this);
|
|
}
|
|
}
|
|
|
|
public sealed class AbortStatement : Statement
|
|
{
|
|
public AbortStatement(int aline, int apos)
|
|
: base(StatementKind.Abort, aline, apos)
|
|
{ }
|
|
|
|
public AbortStatement(AbortStatement toCopy)
|
|
: base(toCopy)
|
|
{ }
|
|
|
|
public override Statement Clone()
|
|
{
|
|
return new AbortStatement(this);
|
|
}
|
|
|
|
public override void Accept(IAstVisitor visitor)
|
|
{
|
|
visitor.visit(this);
|
|
}
|
|
}
|
|
|
|
public sealed class KillStatement : Statement
|
|
{
|
|
public Identifier someOne;
|
|
|
|
public KillStatement(Identifier toKill, int aline, int apos)
|
|
: base(StatementKind.Kill, aline, apos)
|
|
{
|
|
someOne = toKill;
|
|
}
|
|
|
|
public KillStatement(KillStatement toCopy)
|
|
: base(toCopy)
|
|
{
|
|
someOne = toCopy.someOne;
|
|
}
|
|
|
|
public override Statement Clone()
|
|
{
|
|
return new KillStatement(this);
|
|
}
|
|
|
|
public override void Accept(IAstVisitor visitor)
|
|
{
|
|
visitor.visit(this);
|
|
}
|
|
|
|
public void SetIdentifier(Identifier target)
|
|
{
|
|
someOne = target;
|
|
}
|
|
}
|
|
}
|