|
| 1 | +using Microsoft.Extensions.Logging; |
| 2 | + |
| 3 | +using Semgus.Model.Smt; |
| 4 | +using Semgus.Parser.Reader; |
| 5 | + |
| 6 | +using System; |
| 7 | +using System.Collections.Generic; |
| 8 | +using System.Linq; |
| 9 | +using System.Text; |
| 10 | +using System.Threading.Tasks; |
| 11 | + |
| 12 | +namespace Semgus.Parser.Commands |
| 13 | +{ |
| 14 | + /// <summary> |
| 15 | + /// Intrinsic commands. These are an extension to declare "intrinsic" functions, |
| 16 | + /// constants, and sorts. These only are added to the SMT context for parsing, but |
| 17 | + /// are not emitted as a part of the problem file. They are assumed to be handled by |
| 18 | + /// another tool down the line, for example, theory extensions in an SMT solver. |
| 19 | + /// </summary> |
| 20 | + internal class IntrinsicCommands |
| 21 | + { |
| 22 | + private readonly ISmtContextProvider _smtCtxProvider; |
| 23 | + private readonly ISourceMap _sourceMap; |
| 24 | + private readonly ISourceContextProvider _sourceContextProvider; |
| 25 | + private readonly ILogger<IntrinsicCommands> _logger; |
| 26 | + |
| 27 | + /// <summary> |
| 28 | + /// Creates a new IntrinsicCommands instance |
| 29 | + /// </summary> |
| 30 | + /// <param name="smtCtxProvider">The SMT context provider</param> |
| 31 | + /// <param name="sourceMap">The source map</param> |
| 32 | + /// <param name="sourceContextProvider">The source context provider</param> |
| 33 | + /// <param name="handler">Not used, but required for DI</param> |
| 34 | + /// <param name="logger">The logger</param> |
| 35 | + public IntrinsicCommands(ISmtContextProvider smtCtxProvider, |
| 36 | + ISourceMap sourceMap, |
| 37 | + ISourceContextProvider sourceContextProvider, |
| 38 | + ISemgusProblemHandler handler, |
| 39 | + ILogger<IntrinsicCommands> logger) |
| 40 | + { |
| 41 | + _smtCtxProvider = smtCtxProvider; |
| 42 | + _sourceMap = sourceMap; |
| 43 | + _sourceContextProvider = sourceContextProvider; |
| 44 | + _logger = logger; |
| 45 | + } |
| 46 | + |
| 47 | + /// <summary> |
| 48 | + /// Declares an "intrinsic" constant |
| 49 | + /// </summary> |
| 50 | + /// <param name="name">Function name</param> |
| 51 | + /// <param name="argIds">Function argument sorts</param> |
| 52 | + /// <param name="returnSortId">Function return sort</param> |
| 53 | + [Command("declare-intrinsic-fun")] |
| 54 | + public void DeclareIntrinsicFun(SmtIdentifier name, IList<SmtSortIdentifier> argIds, SmtSortIdentifier returnSortId) |
| 55 | + { |
| 56 | + using var logScope = _logger.BeginScope($"while processing `declare-intrinsic-fun` for {name}:"); |
| 57 | + |
| 58 | + var returnSort = _smtCtxProvider.Context.GetSortOrDie(returnSortId, _sourceMap, _logger); |
| 59 | + var args = argIds.Select(argId => _smtCtxProvider.Context.GetSortOrDie(argId, _sourceMap, _logger)); |
| 60 | + var rank = new SmtFunctionRank(returnSort, args.ToArray()); |
| 61 | + var decl = new SmtFunction(name, _sourceContextProvider.CurrentSmtSource, rank); |
| 62 | + |
| 63 | + _smtCtxProvider.Context.AddFunctionDeclaration(decl); |
| 64 | + } |
| 65 | + |
| 66 | + /// <summary> |
| 67 | + /// Declares an "intrinsic" constant |
| 68 | + /// </summary> |
| 69 | + /// <param name="name">Constant name</param> |
| 70 | + /// <param name="returnSortId">Sort of constant</param> |
| 71 | + [Command("declare-intrinsic-const")] |
| 72 | + public void DeclareIntrinsicConst(SmtIdentifier name, SmtSortIdentifier returnSortId) |
| 73 | + { |
| 74 | + using var logScope = _logger.BeginScope($"while processing `declare-intrinsic-const` for {name}:"); |
| 75 | + |
| 76 | + var returnSort = _smtCtxProvider.Context.GetSortOrDie(returnSortId, _sourceMap, _logger); |
| 77 | + var rank = new SmtFunctionRank(returnSort, Array.Empty<SmtSort>()); |
| 78 | + var decl = new SmtFunction(name, _sourceContextProvider.CurrentSmtSource, rank); |
| 79 | + |
| 80 | + _smtCtxProvider.Context.AddFunctionDeclaration(decl); |
| 81 | + } |
| 82 | + |
| 83 | + /// <summary> |
| 84 | + /// Declares an "intrinsic" sort |
| 85 | + /// </summary> |
| 86 | + /// <param name="name">Sort name to declare</param> |
| 87 | + [Command("declare-intrinsic-sort")] |
| 88 | + public void DeclareIntrinsicSort(SmtSortIdentifier name) |
| 89 | + { |
| 90 | + using var logScope = _logger.BeginScope($"while processing `declare-intrinsic-sort` for {name}:"); |
| 91 | + |
| 92 | + var sort = new SmtSort.GenericSort(name); |
| 93 | + |
| 94 | + _smtCtxProvider.Context.AddSortDeclaration(sort); |
| 95 | + } |
| 96 | + } |
| 97 | +} |
0 commit comments