From 159717f51cb8ed2b273a4fd3fbfb8801358693a1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Patrick=20L=C3=BChne?= Date: Thu, 19 Apr 2018 16:11:05 +0200 Subject: [PATCH] Support declaring functions as integer This adds a new syntax for declaring functions integer: #external integer((().” supported"); + throw LogicException(statement.location, "only #external declarations of the form “#external ().” or “#external integer(()).” supported"); }; if (!external.body.empty()) @@ -204,6 +204,47 @@ struct StatementVisitor if (predicate.arguments.size() != 1) fail(); + const auto handleIntegerDeclaration = + [&]() + { + // Integer function declarations are treated separately if applicable + if (strcmp(predicate.name, "integer") != 0) + return false; + + if (predicate.arguments.size() != 1) + return false; + + const auto &functionArgument = predicate.arguments.front(); + + if (!functionArgument.data.is()) + return false; + + const auto &function = functionArgument.data.get(); + + if (function.arguments.size() != 1) + return false; + + const auto &arityArgument = function.arguments.front(); + + if (!arityArgument.data.is()) + return false; + + const auto &aritySymbol = arityArgument.data.get(); + + if (aritySymbol.type() != Clingo::SymbolType::Number) + return false; + + const size_t arity = aritySymbol.number(); + + auto functionDeclaration = context.findOrCreateFunctionDeclaration(function.name, arity); + functionDeclaration->domain = ast::Domain::Integer; + + return true; + }; + + if (handleIntegerDeclaration()) + return; + const auto &arityArgument = predicate.arguments.front(); if (!arityArgument.data.is()) diff --git a/src/anthem/IntegerVariableDetection.cpp b/src/anthem/IntegerVariableDetection.cpp index 758adc9..425e3d0 100644 --- a/src/anthem/IntegerVariableDetection.cpp +++ b/src/anthem/IntegerVariableDetection.cpp @@ -49,12 +49,9 @@ struct TermDomainVisitor return ast::Domain::General; } - static ast::Domain visit(ast::Function &) + static ast::Domain visit(ast::Function &function) { - // Functions may return values of any type - - // TODO: implement explicit integer specifications - return ast::Domain::General; + return function.declaration->domain; } static ast::Domain visit(ast::Integer &)