/* * This is a quick'n dirty hack to see whether the internal data structures are sane enough to emit some * prolog code.. * * */ using System; using System.Collections.Generic; using System.Text; namespace TUG.Mogentes.Codegen { class prologCodeGenerator : SyntaxTreeVisitor { protected int indentLevel = 0; protected int indentTabWidth = 4; protected bool writeTypes = true; protected StringBuilder output = new StringBuilder(); protected Dictionary definedTypes = new Dictionary(); public void Append(string text) { output.Append(text); } public void Indent() { for (int i = 0; i < indentLevel * indentTabWidth; i++) output.Append(" "); } public void AppendLine(string text) { output.AppendLine(text); Indent(); } public override string ToString() { return output.ToString(); } public void visit(CompositionList compositionList, int stage) where T : SyntaxTree { } public void visit(OoaType anObject, int stage) { if (stage != 0) return; // VARIABLES List varlist = new List(); List init = new List(); AppendLine(""); AppendLine("% variable defs"); foreach (var sym in anObject.symbols.Values) { if (sym.GetType() == typeof(VariableSymbol)) { init.Add(((VariableSymbol)sym).initializer.ToString()); Append("var(["); Append(sym.tokenText); Append("],"); varlist.Add(sym.tokenText); if (sym.type is OpaqueType) { Append(((OpaqueType)sym.type).referencedType.tokenText); AppendLine(")."); } else { AppendLine("). % anonymous types not supported"); } } } /*define state vector*/ AppendLine(""); Append("state_def(["); int cntr = varlist.Count; varlist.ForEach(i => { cntr--; Append(i); if (cntr > 0) Append(","); }); AppendLine("])."); /*define start values (just a copy/paste of the initializer expressions)*/ AppendLine(""); Append("init(["); cntr = init.Count; init.ForEach(i => { cntr--; Append(i); if (cntr > 0) Append(","); }); AppendLine("])."); // ACTIONS AppendLine(""); List actions = new List(); foreach (var sym in anObject.symbols.Values) { if (sym.GetType() == typeof(NamedActionSymbol)) actions.Add((NamedActionSymbol)sym); } if (actions.Count > 0) { Append("as :- "); cntr = actions.Count; foreach (var na in actions) { cntr--; Append(na.tokenText); Append(":"); foreach (UlyssesStatement s in ((FunctionType)na.type).body) { if (s is GuardedCommandStatement) { Append("("); Append(((GuardedCommandStatement)s).guard.ToString()); Append(") => ("); bool first = true; foreach (UlyssesStatement sb in ((GuardedCommandStatement)s).body) { if (first) first = false; else Append(","); Append(sb.ToString()); } Append(")"); } } if (cntr > 0) AppendLine(","); } AppendLine("."); } } public void visit(BoolType anObject, int stage) { } public void visit(IntType intType, int p) { } public void visit(FloatType floatType, int p) { } public void visit(CharType charType, int p) { } public void visit(EnumType enumType, int p) { } public void visit(ListType listType, int p) { } public void visit(MapType mapType, int p) { } public void visit(TupleType tupleType, int p) { } public void visit(OoaSystem ooaSystem, int stage) { if (stage == 0) { AppendLine(":- public(as/0)."); AppendLine(":- public(asM/0)."); AppendLine(""); AppendLine(":- dynamic(qstate_init/1)."); AppendLine(":- dynamic(qstate_constraints/1)."); AppendLine(":- public(qstate_constraints/1)."); AppendLine(":- public(qstate_init/1)."); AppendLine(""); AppendLine("% defintion of maximal search depth"); AppendLine("searchDepth(15)."); AppendLine(""); } if (stage == 2) writeTypes = false; } public bool visit(NamedTypeSymbol namedTypeSymbol, int p) { if (p != 0) return writeTypes; if (!definedTypes.ContainsKey(namedTypeSymbol)) { definedTypes.Add(namedTypeSymbol, namedTypeSymbol.tokenText); int i; Type aType = namedTypeSymbol.type.GetType(); string atypedef = "type(" + namedTypeSymbol.tokenText + ","; if (aType == typeof(IntType)) { IntType intType = (IntType)namedTypeSymbol.type; Append(atypedef); Append("X) :- "); Append("fd_domain(X,"); Append(intType.low.ToString()); Append(","); Append(intType.high.ToString()); Append("),"); AppendLine("fd_labeling(X)."); } else if (aType == typeof(EnumType)) { EnumType enumType = (EnumType)namedTypeSymbol.type; i = enumType.enumSymbols.Count; Append(atypedef); Append("["); foreach (var sym in enumType.enumSymbols) { i--; Append("'"); Append(sym.tokenText); Append("'"); if (i > 0) Append(","); } AppendLine("])."); } else if (aType == typeof(BoolType)) { AppendLine(" % bool not supported yet. "); } else if (aType == typeof(FloatType)) { AppendLine(" % float not supported yet. "); } else if (aType == typeof(CharType)) { AppendLine(" % char not supported yet. "); } else if (aType == typeof(ListType)) { AppendLine(" % list not supported yet. "); } else if (aType == typeof(MapType)) { AppendLine(" % map not supported yet. "); } else if (aType == typeof(TupleType)) { AppendLine(" % tuple not supported yet. "); } } return writeTypes; } public bool visit(ParameterSymbol parameterSymbol, int p) { return true; } public bool visit(VariableSymbol variableSymbol, int p) { return true; } public bool visit(MethodSymbol methodSymbol, int p) { return true; } public void visit(EnumIdSymbol enumIdSymbol, int p) { } public void visit(AbortStatement abortStatement, int p) { } public void visit(SkipStatement skipStatement, int p) { } public void visit(KillStatement killStatement, int p) { } public void visit(GuardedCommandStatement guardedCommandStatement, int p) { } public void visit(NamedActionInclusionStatement namedActionInclusionStatement, int p) { } public void visit(MethodCallStatement methodCallStatement, int p) { } public void visit(AssignmentStatement assignmentStatement, int p) { } public void visit(LocalVarStatement localVarStatement, int p) { } public void visit(ConstantChar constantChar, int p) { } public void visit(ConstantInt constantInt, int p) { } public void visit(ConstantFloat constantFloat, int p) { } public void visit(ConstantNil constantNil, int p) { } public void visit(ConstantBool constantBool, int p) { } public void visit(ConstantList constantList, int p) { } public void visit(ConstantSet constantSet, int p) { } public void visit(ConstantMap constantMap, int p) { } public void visit(UnaryOperator unaryOperator, int p) { } public void visit(SetListUnaryOperator setListUnaryOperator, int p) { } public void visit(MapUnaryOperator mapUnaryOperator, int p) { } public void visit(BinaryOperator binaryOperator, int p) { } public void visit(BoolBinaryOperator boolBinaryOperator, int p) { } public void visit(NumericBinaryOperator numericBinaryOperator, int p) { } public void visit(SetListBinaryOperator setListBinaryOperator, int p) { } public void visit(MapBinaryOperator mapBinaryOperator, int p) { } public void visit(QualifiedIdentifier qualifiedIdentifier, int p) { } public void visit(Reference reference, int p) { } public void visit(AccessExpression accessExpression, int p) { } public void visit(ConditionalOperator conditionalOperator, int p) { } public void visit(OpaqueType namedType, int p) { } public bool visit(NamedActionSymbol namedActionSymbol, int p) { return true; } public void visit(ConstantTuple constantTuple, int p) { } #region SyntaxTreeVisitor Member public bool visit(FunctionType functionType, int p) { return true; } #endregion } }