/** * * 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; //using TUG.Mogentes.Codegen; using Antlr.Runtime; namespace TUG.Mogentes { public enum IdentifierKind { TypeIdentifier, // some named type EnumIdentifier, // identifier of enumerated type LandmarkIdentifier, // landmark AttributeIdentifier, // global attribute LocalVariableIdentifier,// local variable ParameterIdentifier, // method/named action argument ExpressionVariableIdentifier, // var in an expression MethodIdentifier, // a method NamedActionIdentifier, // a named action Module, // the main module - actually this has no name... List, Constant } /* ABSTRACT class Identifier. * */ public abstract class Identifier : UlyssesBasicClass, IAst, IComparable { protected int m_line; protected int m_column; protected IScope m_definingScope; protected IdentifierKind m_identifierKind; protected string m_tokenText; protected UlyssesType m_Type; // basic attributes of an Identifier public IdentifierKind kind { get { return m_identifierKind; } } // kind of identifier public UlyssesType type { get { return m_Type; } } // type of identifier public IScope definingScope { get { return m_definingScope; } } // scope where it's defined // from lexer public string tokenText { get { return m_tokenText; } } public int line { get { return m_line; } } public int column { get { return m_column; } } public AstNodeTypeEnum nodeType { get { return AstNodeTypeEnum.identifier; } } public Identifier(IToken aToken, UlyssesType aType, IdentifierKind aKind, IScope aDefiningScope) { m_tokenText = aToken.Text; m_line = aToken.Line; m_column = aToken.CharPositionInLine; m_Type = aType; m_identifierKind = aKind; m_definingScope = aDefiningScope; } public Identifier(string name, UlyssesType aType, IdentifierKind aKind, IScope aDefiningScope) { m_tokenText = name; m_line = 0; m_column = 0; m_Type = aType; m_identifierKind = aKind; m_definingScope = aDefiningScope; } public Identifier(Identifier toCopy) { m_tokenText = toCopy.m_tokenText; m_line = toCopy.m_line; m_column = toCopy.m_column; m_Type = toCopy.m_Type; m_identifierKind = toCopy.m_identifierKind; m_definingScope = toCopy.m_definingScope; } public virtual Identifier Clone() { throw new NotImplementedException(); } public virtual void Accept(IAstVisitor visitor) { throw new NotImplementedException(); } public void SetType(UlyssesType aType) { if (aType == null) throw new ArgumentException("New type can not be null."); m_Type = aType; } public void SetTokenText(string aText) { m_tokenText = aText; } public override string ToString() { return tokenText; } public int CompareTo(object obj) { if (obj is Identifier) return tokenText.CompareTo(obj.ToString()); else throw new InvalidCastException(); } } /////////////////////////////////////////////// /// Value Identifiers /// public abstract class ValueIdentifier : Identifier { public ValueIdentifier(IToken aToken, UlyssesType aType, IdentifierKind aKind, IScope aDefiningScope) : base(aToken, aType, aKind, aDefiningScope) { } public ValueIdentifier(ValueIdentifier toCopy) : base(toCopy) { } } public sealed class ConstantIdentifier : ValueIdentifier { private Expression m_value; public Expression Value { get { return m_value; } } public ConstantIdentifier(IToken aToken, Expression value, IScope aDefiningScope) : base(aToken, null, IdentifierKind.Constant, aDefiningScope) { m_value = value; } public ConstantIdentifier(ConstantIdentifier toCopy) : base(toCopy) { m_value = toCopy.m_value; } public void SetValue(Expression aValue) { m_value = aValue; } public override Identifier Clone() { return this; } public override void Accept(IAstVisitor visitor) { visitor.visit(this); } } public sealed class EnumIdentifier : ValueIdentifier { private int value; private bool haveValue; public int Value { get { return value; } } public bool HaveValue { get { return haveValue; } } public EnumIdentifier(IToken aToken, EnumType aType, IScope aDefiningScope) : base(aToken, aType, IdentifierKind.EnumIdentifier, aDefiningScope) { haveValue = false; } public EnumIdentifier(IToken aToken, int aValue, EnumType aType, IScope aDefiningScope) : base(aToken, aType, IdentifierKind.EnumIdentifier, aDefiningScope) { haveValue = true; value = aValue; } public EnumIdentifier(EnumIdentifier toCopy) : base(toCopy) { value = toCopy.value; haveValue = toCopy.haveValue; } public override Identifier Clone() { return this; } public override void Accept(IAstVisitor visitor) { visitor.visit(this); } } public sealed class LandmarkIdentifier : ValueIdentifier { public LandmarkIdentifier(IToken aToken, QrType aType, IScope aDefiningScope) : base(aToken, aType, IdentifierKind.LandmarkIdentifier, aDefiningScope) { } public LandmarkIdentifier(LandmarkIdentifier toCopy) : base(toCopy) { } public override Identifier Clone() { return this; } public override void Accept(IAstVisitor visitor) { visitor.visit(this); } } /////////////////////////////////////////////// /// Variable Identifiers /// public abstract class ValueReferenceIdentifier : Identifier { public ValueReferenceIdentifier(IToken aToken, UlyssesType aType, IdentifierKind aKind, IScope aDefiningScope) : base(aToken, aType, aKind, aDefiningScope) { } public ValueReferenceIdentifier(ValueReferenceIdentifier toCopy) : base(toCopy) { } public ValueReferenceIdentifier(string name, UlyssesType aType, IdentifierKind aKind, IScope aDefiningScope) : base(name, aType, aKind, aDefiningScope) { } } public sealed class AttributeIdentifier : ValueReferenceIdentifier { private Expression m_initializer; private bool m_isStatic; private bool m_isObservable; // used for qualitative values private bool m_isControllable; // used for qualitative values public Expression initializer { get { return m_initializer; } } public bool isStatic { get { return m_isStatic; } } public bool isObservable { get { return m_isObservable; } } public bool isControllable { get { return m_isControllable; } } public AttributeIdentifier(IToken aToken, UlyssesType aType, IScope aDefiningScope, Expression anInitializer, bool isStatic, bool isObservable, bool isControllable) : base(aToken, aType, IdentifierKind.AttributeIdentifier, aDefiningScope) { m_initializer = anInitializer; m_isStatic = isStatic; m_isControllable = isControllable; m_isObservable = isObservable; } public AttributeIdentifier(AttributeIdentifier toCopy) : base(toCopy) { m_initializer = toCopy.initializer; m_isStatic = toCopy.m_isStatic; m_isControllable = toCopy.m_isControllable; m_isObservable = toCopy.m_isObservable; } public override Identifier Clone() { return new AttributeIdentifier(this); } public override void Accept(IAstVisitor visitor) { visitor.visit(this); } public void SetInitializer(Expression newExpr) { if (newExpr == null) throw new ArgumentException(); m_initializer = newExpr; } } public class LocalVariableIdentifier : ValueReferenceIdentifier { public LocalVariableIdentifier(IToken aToken, UlyssesType aType, IScope aDefiningScope) : base(aToken, aType, IdentifierKind.LocalVariableIdentifier, aDefiningScope) { } public LocalVariableIdentifier(LocalVariableIdentifier toCopy) : base(toCopy) { } public override Identifier Clone() { return new LocalVariableIdentifier(this); } public LocalVariableIdentifier(string name, UlyssesType aType, IdentifierKind aKind, IScope aDefiningScope) : base(name, aType, aKind, aDefiningScope) { } public override void Accept(IAstVisitor visitor) { visitor.visit(this); } } public sealed class ParameterIdentifier : LocalVariableIdentifier { public ParameterIdentifier(IToken aToken, UlyssesType aType, IScope aDefiningScope) : base(aToken, aType, aDefiningScope) { m_identifierKind = IdentifierKind.ParameterIdentifier; } public ParameterIdentifier(string name, UlyssesType aType, IScope aDefiningScope) : base(name, aType, IdentifierKind.ParameterIdentifier, aDefiningScope) { } public ParameterIdentifier(ParameterIdentifier toCopy) : base(toCopy) { } public override Identifier Clone() { return new ParameterIdentifier(this); } public override void Accept(IAstVisitor visitor) { visitor.visit(this); } } public sealed class ExpressionVariableIdentifier : LocalVariableIdentifier { private bool m_isInitialized; public bool initialized { get { return m_isInitialized; } } public ExpressionVariableIdentifier(IToken aToken, UlyssesType aType, IScope aDefiningScope) : base(aToken, aType, aDefiningScope) { m_identifierKind = IdentifierKind.ExpressionVariableIdentifier; } public ExpressionVariableIdentifier(string name, int line, int col) : base(name, null, IdentifierKind.ExpressionVariableIdentifier, null) { m_line = line; m_column = col; m_isInitialized = false; } public ExpressionVariableIdentifier(ExpressionVariableIdentifier toCopy) : base(toCopy) { m_line = toCopy.m_line; m_column = toCopy.m_column; m_isInitialized = toCopy.m_isInitialized; } public override Identifier Clone() { return new ExpressionVariableIdentifier(this); } public override void Accept(IAstVisitor visitor) { visitor.visit(this); } public void SetInitialized(bool newValue) { m_isInitialized = newValue; } } /////////////////////////////////////////////// /// Type Identifier /// public class TypeIdentifier : Identifier { public string prefix; protected TypeIdentifier(IToken aToken, UlyssesType aType, IdentifierKind someKind, IScope aDefiningScope) : base(aToken, aType, someKind, aDefiningScope) { } protected TypeIdentifier(string aToken, UlyssesType aType, IdentifierKind someKind, IScope aDefiningScope) : base(aToken, aType, someKind, aDefiningScope) { } public TypeIdentifier(IToken aToken, UlyssesType aType, IScope aDefiningScope) : base(aToken, aType, IdentifierKind.TypeIdentifier, aDefiningScope) { } public TypeIdentifier(string aName, UlyssesType aType, IScope aDefiningScope) : base(aName, aType, IdentifierKind.TypeIdentifier, aDefiningScope) { } public TypeIdentifier(TypeIdentifier toCopy) : base(toCopy) { this.prefix = toCopy.prefix; } public override Identifier Clone() { return new TypeIdentifier(this); } public override void Accept(IAstVisitor visitor) { visitor.visit(this); } } public sealed class SelfTypeIdentifier : TypeIdentifier { public SelfTypeIdentifier(string aName, UlyssesType aType, IScope aDefiningScope) : base(aName, aType, aDefiningScope) { } public override void Accept(IAstVisitor visitor) { visitor.visit(this); } } /////////////////////////////////////////////// /// Scope Identifiers /// public abstract class Scope : TypeIdentifier, IScope { protected SymbolTable m_symbolTable; public Scope(IToken aToken, UlyssesType aType, IdentifierKind aKind, IScope aDefiningScope) : base(aToken, aType, aKind, aDefiningScope) { m_symbolTable = new SymbolTable(); } public Scope(string aName, UlyssesType aType, IdentifierKind aKind, IScope aDefiningScope) : base(aName, aType, aKind, aDefiningScope) { m_symbolTable = new SymbolTable(); } public Scope(Scope toCopy) : base(toCopy) { m_symbolTable = toCopy.symbolTable; } public SymbolTable symbolTable { get { return m_symbolTable; } } public IScope GetParentScope() { return m_definingScope; } public void SetParentScope(IScope parentScope) { m_definingScope = parentScope; } public virtual void AddIdentifier(Identifier anIdentifier, object tag) { m_symbolTable.AddIdentifier(anIdentifier); } public Identifier ResolveIdentifier(string aName) { if (m_symbolTable.Defined(aName)) return m_symbolTable.Get(aName); else return null; } } public abstract class FunctionIdentifier : Scope { private Block m_body; private List m_parameter = new List(); public Block body { get { return m_body; } } public List parameter { get { return m_parameter; } } public FunctionIdentifier(IToken aToken, UlyssesType aType, IdentifierKind aKind, IScope aDefiningScope) : base(aToken, aType, aKind, aDefiningScope) { } public FunctionIdentifier(FunctionIdentifier toCopy) : base(toCopy) { m_body = toCopy.m_body; m_parameter = new List(toCopy.m_parameter); } public void SetBody(Block body) { m_body = body; } public void AddParameter(ParameterIdentifier aParameter) { m_parameter.Add(aParameter); AddIdentifier(aParameter, null); } } public sealed class MethodIdentifier : FunctionIdentifier { public MethodIdentifier(IToken aToken, UlyssesType aType, IScope aDefiningScope) : base(aToken, aType, IdentifierKind.MethodIdentifier, aDefiningScope) { } public MethodIdentifier(MethodIdentifier toCopy) : base(toCopy) { } public override Identifier Clone() { return new MethodIdentifier(this); } public override void Accept(IAstVisitor visitor) { visitor.visit(this); } } public sealed class NamedActionIdentifier : FunctionIdentifier { public NamedActionIdentifier(IToken aToken, UlyssesType aType, IScope aDefiningScope) : base(aToken, aType, IdentifierKind.NamedActionIdentifier, aDefiningScope) { } public NamedActionIdentifier(NamedActionIdentifier toCopy) : base(toCopy) { } public override Identifier Clone() { return new NamedActionIdentifier(this); } public override void Accept(IAstVisitor visitor) { visitor.visit(this); } } public class Module : Scope { //protected List m_typesToFixUp; //protected List m_referencesToResolve; //public List typesToFixUp { get { return m_typesToFixUp; } } //public List referencesToResolve { get { return m_referencesToResolve; } } public Module() : base("@Module", null, IdentifierKind.Module, null) { //m_typesToFixUp = new List(); // m_referencesToResolve = new List(); } public override void Accept(IAstVisitor visitor) { visitor.visit(this); } } public sealed class MainModule : Module { private IdentifierList m_systemDescription; private ActionSystem m_instance; public IdentifierList systemDescription { get { return m_systemDescription; } } public ActionSystem instance { get { return m_instance; } } public MainModule() : base() { m_systemDescription = null; } public void SetSystemDescription(IdentifierList aDescr) { m_systemDescription = aDescr; } public void SetInstance(ActionSystem anObject) { m_instance = anObject; } public override void Accept(IAstVisitor visitor) { visitor.visit(this); } } /////////////////////////////////////////////// /// Identifier List /// public enum IdentifierListType { normal, nondeterministic, seqential, prioritized } public abstract class IdentifierList : Identifier { private LinkedList m_list; private IdentifierListType m_type; public LinkedList identifiers { get { return m_list; } } public IdentifierListType listType { get { return m_type; } } public IdentifierList(IdentifierListType aConcatType) : base("list", null, IdentifierKind.List, null) { m_list = new LinkedList(); m_type = aConcatType; } public void AddElement(Identifier toAdd) { m_list.AddLast(toAdd); } } public sealed class NondetIdentifierList : IdentifierList { public NondetIdentifierList() : base(IdentifierListType.nondeterministic) { } public NondetIdentifierList(IdentifierList aParent) : base(IdentifierListType.nondeterministic) { if (aParent != null) aParent.AddElement(this); } public override void Accept(IAstVisitor visitor) { visitor.visit(this); } } public sealed class SeqIdentifierList : IdentifierList { public SeqIdentifierList() : base(IdentifierListType.seqential) { } public SeqIdentifierList(IdentifierList aParent) : base(IdentifierListType.seqential) { if (aParent != null) aParent.AddElement(this); } public override void Accept(IAstVisitor visitor) { visitor.visit(this); } } public sealed class PrioIdentifierList : IdentifierList { public PrioIdentifierList() : base(IdentifierListType.prioritized) { } public PrioIdentifierList(IdentifierList aParent) : base(IdentifierListType.prioritized) { if (aParent != null) aParent.AddElement(this); } public override void Accept(IAstVisitor visitor) { visitor.visit(this); } } public sealed class UnspecIdentifierList : IdentifierList { public UnspecIdentifierList() : base(IdentifierListType.normal) { } public UnspecIdentifierList(IdentifierList aParent) : base(IdentifierListType.normal) { if (aParent != null) aParent.AddElement(this); } public override void Accept(IAstVisitor visitor) { visitor.visit(this); } } }