@Override public Void visit(Bag b, Void _) { boolean allCells = true; for (Term t : b.getContents()) { if (t instanceof Cell || t instanceof TermComment) { continue; } else if (t instanceof Variable) { Variable v = (Variable)t; if (v.getSort().equals("Bag")) { continue; } } else if (t instanceof Bracket) { Bracket r = (Bracket)t; if (r.getContent() instanceof Rewrite && r.getContent().getSort().equals("Bag")) { continue; } } allCells = false; break; } if (allCells) { for (Term t : b.getContents()) { this.visitNode(t); } } else { assert false; } return null; }
@Override public void visit(Variable node) { if (node.isFresh()) { if (current == right && !inCondition) { Integer i = fresh.get(node); if (i == null) i = new Integer(1); else i = new Integer(i.intValue()); fresh.put(node, i); return; } // nodes are ok to be found in rhs GlobalSettings.kem.register( new KException( KException.ExceptionType.ERROR, KException.KExceptionGroup.COMPILER, "Fresh variable \"" + node + "\" is bound in the " + "rule pattern.", getName(), node.getFilename(), node.getLocation())); } // System.out.println("Variable: " + node); Integer i = current.remove(node); if (i == null) { i = new Integer(1); } else { i = new Integer(i.intValue() + 1); } current.put(node, i); }
public static Variable freshVar(Set<Variable> vars, String sort) { String prefix = "?"; int i = 0; Variable v = new Variable(prefix + i, sort); while (vars.contains(v)) { v.setName(prefix + (++i)); } return v; }
@Override public Void visit(Variable v, Void _) { builder.append(v.getName()); if (v.isUserTyped()) { builder.append(':'); builder.append(v.getSort()); } return null; }
public void printLabel(Term t) { if (t instanceof Variable) { Variable v = (Variable) t; builder.append(v.getName()); } else { KLabelConstant l = (KLabelConstant)t; builder.append('`'); builder.append(l.getLabel()); builder.append('`'); } }
@Override public void visit(TermCons node) { if (!node.getCons().equals(MetaK.Constants.freshCons)) { super.visit(node); return; } if (!inCondition) { GlobalSettings.kem.register( new KException( KException.ExceptionType.ERROR, KException.KExceptionGroup.COMPILER, "Fresh can only be used in conditions.", getName(), node.getFilename(), node.getLocation())); } final Term term = node.getContents().get(0); if (!(term instanceof Variable)) { GlobalSettings.kem.register( new KException( KException.ExceptionType.ERROR, KException.KExceptionGroup.COMPILER, "Fresh can only be applied to variables, but was applied to\n\t\t" + term, getName(), term.getFilename(), term.getLocation())); } Variable v = (Variable) term; if (left.containsKey(v)) { for (Variable v1 : left.keySet()) { if (v1.equals(v)) { GlobalSettings.kem.register( new KException( KException.ExceptionType.ERROR, KException.KExceptionGroup.COMPILER, "Fresh variable \"" + v1 + "\" is bound in the rule pattern.", getName(), v1.getFilename(), v1.getLocation())); } } } left.put(v, new Integer(1)); }
public static Term getTerm(Production prod, org.kframework.kil.loader.Context context) { if (prod.isSubsort()) { final Variable freshVar = Variable.getFreshVar(prod.getItems().get(0).toString()); if (prod.containsAttribute("klabel")) { return KApp.of(KLabelConstant.of(prod.getKLabel(), context), freshVar); } return freshVar; } if (prod.isConstant()) { String terminal = ((Terminal) prod.getItems().get(0)).getTerminal(); if (prod.getSort().equals(KSorts.KLABEL)) { return KLabelConstant.of(terminal, context); } else if (prod.getSort().equals(BoolBuiltin.SORT_NAME)) { return BoolBuiltin.kAppOf(terminal); } else if (prod.getSort().equals(IntBuiltin.SORT_NAME)) { return IntBuiltin.kAppOf(terminal); } else if (prod.getSort().equals(StringBuiltin.SORT_NAME)) { return StringBuiltin.kAppOf(terminal); } else { return GenericToken.kAppOf(prod.getSort(), terminal); } } if (prod.isLexical()) { return KApp.of( KLabelConstant.of("#token", context), StringBuiltin.kAppOf(prod.getSort()), Variable.getFreshVar("String")); } TermCons t = new TermCons(prod.getSort(), prod.getCons(), context); if (prod.isListDecl()) { t.getContents().add(Variable.getFreshVar(((UserList) prod.getItems().get(0)).getSort())); t.getContents().add(Variable.getFreshVar(prod.getSort())); return t; } for (ProductionItem item : prod.getItems()) { if (item instanceof Sort) { t.getContents().add(Variable.getFreshVar(((Sort) item).getName())); } } return t; }
@Override public ASTNode transform(org.kframework.kil.Variable node) throws TransformerException { if (node.getSort().equals("Bag")) return new Variable(node.getName(), Kind.CELL_COLLECTION.toString()); DataStructureSort dataStructureSort = context.dataStructureSortOf(node.getSort()); if (dataStructureSort != null) { if (dataStructureSort.type().equals(org.kframework.kil.KSorts.LIST)) return new Variable(node.getName(), KSorts.LIST); if (dataStructureSort.type().equals(org.kframework.kil.KSorts.MAP)) return new Variable(node.getName(), KSorts.MAP); if (dataStructureSort.type().equals(org.kframework.kil.KSorts.SET)) return new Variable(node.getName(), KSorts.SET); } return new Variable(node.getName(), node.getSort()); }
@Override public Void visit(KApp app, Void _) { if (app.getLabel() instanceof Token) { assert ((KList)app.getChild()).isEmpty(); this.visitNode(app.getLabel()); } else { printLabel(app.getLabel()); Term child = app.getChild(); builder.append('('); if (child instanceof KList) { visitNestedKLists((KList)child); } else if (child instanceof Variable) { Variable klistVar = (Variable)child; assert (klistVar.getSort().equals("KList")); builder.append(klistVar.getName()); builder.append(":KList"); } else { assert false; } builder.append(')'); } return null; }
@Override public ASTNode visit(org.kframework.kil.Variable node, Void _void) { String name = node.fullName(); if (node.getSort().equals(org.kframework.kil.Sort.BAG) || node.getSort().equals(org.kframework.kil.Sort.BAG_ITEM)) { return new Variable(name, Kind.CELL_COLLECTION.asSort()); } if (node.getSort().equals(org.kframework.kil.Sort.K)) { return new Variable(name, Sort.KSEQUENCE); } if (node.getSort().equals(org.kframework.kil.Sort.KLIST)) { return new Variable(name, Sort.KLIST); } DataStructureSort dataStructureSort = context.dataStructureSortOf(node.getSort()); if (dataStructureSort != null) { Sort sort = null; if (dataStructureSort.type().equals(org.kframework.kil.Sort.LIST)) { sort = Sort.LIST; } else if (dataStructureSort.type().equals(org.kframework.kil.Sort.MAP)) { sort = Sort.MAP; } else if (dataStructureSort.type().equals(org.kframework.kil.Sort.SET)) { sort = Sort.SET; } else { assert false : "unexpected data structure " + dataStructureSort.type(); } if (concreteCollectionSize.containsKey(node)) { return new ConcreteCollectionVariable(name, sort, concreteCollectionSize.get(node)); } else { return new Variable(name, sort); } } return new Variable(name, Sort.of(node.getSort())); }
public static boolean isAnonVar(Variable node) { return node.getName().startsWith(Constants.anyVarSymbol); }
@Override public void visit(Sentence node) { inCondition = false; left.clear(); right.clear(); fresh.clear(); current = left; node.getBody().accept(this); if (node.getRequires() != null) { current = right; inCondition = true; node.getRequires().accept(this); } // TODO: add checks for Ensures, too. for (Variable v : right.keySet()) { if (MetaK.isAnonVar(v) && !v.isFresh()) { GlobalSettings.kem.register( new KException( KException.ExceptionType.ERROR, KException.KExceptionGroup.COMPILER, "Anonymous variable found in the right hand side of a rewrite.", getName(), v.getFilename(), v.getLocation())); } if (!left.containsKey(v)) { node.addAttribute(UNBOUNDED_VARS, ""); /* matching logic relaxes this restriction */ if (!GlobalSettings.javaBackend) { GlobalSettings.kem.register( new KException( KException.ExceptionType.ERROR, KException.KExceptionGroup.COMPILER, "Unbounded Variable " + v.toString() + ".", getName(), v.getFilename(), v.getLocation())); } else { GlobalSettings.kem.register( new KException( KException.ExceptionType.WARNING, KException.KExceptionGroup.COMPILER, "Unbounded Variable " + v.toString() + ".", getName(), v.getFilename(), v.getLocation())); } } } for (Map.Entry<Variable, Integer> e : left.entrySet()) { final Variable key = e.getKey(); if (fresh.containsKey(key)) { GlobalSettings.kem.register( new KException( KException.ExceptionType.ERROR, KException.KExceptionGroup.COMPILER, "Variable " + key + " has the same name as a fresh " + "variable.", getName(), key.getFilename(), key.getLocation())); } if (MetaK.isAnonVar(key)) continue; if (e.getValue().intValue() > 1) continue; if (!right.containsKey(key)) { GlobalSettings.kem.register( new KException( KException.ExceptionType.HIDDENWARNING, KException.KExceptionGroup.COMPILER, "Singleton variable " + key.toString() + ".\n" + " If this is not a spelling mistake, please consider using anonymous variables.", getName(), key.getFilename(), key.getLocation())); } } }