Exemple #1
0
        /// <summary>
        /// Creates and initializes a new <see cref="MicrocodeCompiler"/>.
        /// </summary>
        public MicrocodeCompiler()
        {
            Aliases = new MicrocodeAlias[]
            {
            };

            KnownInputModes = new char[] { '$', '#', '?' };

            CommentHead   = String("//");
            Operator      = OneOf(Aliases.Select(o => Try(String(o.Alias)).ThenReturn(o)));
            ExceptEndLine = AnyCharExcept('\r', '\n');

            Comment       = CommentHead.Then(ExceptEndLine.SkipMany()).Labelled("comment");
            Documentation =
                from name in LetterOrDigit.AtLeastOnceString().Labelled("name")
                from inputs in OneOf(KnownInputModes).Labelled("input-mode").Many().Labelled("input-modes")
                from colon in Char(':').Before(SkipWhitespaces)
                from desc in ExceptEndLine.ManyString().Labelled("description")
                select new MicrocodeDoc()
            {
                CommandName = name,
                InputModes  = inputs.Select(i => i.GetValueType()).ToArray(),
                Description = desc
            };

            SignalCall = OneOf(
                Operator
                .Select(o => new MicrocodeCall(o.OperationName))
                .Labelled("control alias"),
                Letter.AtLeastOnceString()
                .Select(s => new MicrocodeCall(s))
                .Labelled("control signal"));

            Command =
                from n in Documentation
                from lb in EndOfLine
                from cs in Char(' ').SkipMany().Then(SignalCall.Or(Comment.ThenReturn(new MicrocodeCall()))).SeparatedAndOptionallyTerminatedAtLeastOnce(EndOfLine)
                select new MicrocodeCommand()
            {
                Documentation = n, Calls = cs
            };

            Microcode =
                Comment.ThenReturn(new Maybe <MicrocodeCommand>()).Or(
                    Command.Select(c => new Maybe <MicrocodeCommand>(c)))
                .SeparatedAtLeastOnce(SkipWhitespaces)
                .Select(c => c.Where(m => m.HasValue).Select(m => m.Value));
        }
Exemple #2
0
        /// <summary>
        /// Creates and initializes a new <see cref="CommandCompiler"/>.
        /// </summary>
        public CommandCompiler()
        {
            CommentHead      = String("//");
            LiteralMarker    = Char('#');
            CharMarker       = Char('\'');
            StringMarker     = Char('\"');
            AddressMarker    = Char('$');
            DirectiveMarker  = Char('.');
            ReferenceMarker  = Char('?');
            AnyCommandMarker = OneOf(LiteralMarker, AddressMarker, ReferenceMarker);
            EnumStart        = Char('{');
            EnumEnd          = Char('}');
            ExceptEndLine    = AnyCharExcept('\r', '\n');

            TrueString  = String("true");
            FalseString = String("false");
            NullString  = String("null");

            ReservedCommandNames = new string[]
            {
                "Define",
                "Var",
                "Constant"
            };

            NumLiteral =
                from l in CurrentPos
                from n in LiteralMarker.Then(Digit.Or(Char('.')).Or(Char('-')).ManyString())
                select new ValueToken()
            {
                Value = n, Type = ValueType.Immediate, Position = l
            };

            CharLiteral =
                from l in CurrentPos
                from c in Any.Between(CharMarker)
                select new ValueToken()
            {
                Value = c.ToString(), Type = ValueType.Immediate, Position = l
            };

            StringLiteral =
                from l in CurrentPos
                from c in AnyCharExcept('\"').ManyString().Between(StringMarker)
                select new ValueToken()
            {
                Value = c.ToString(), Type = ValueType.Immediate, Position = l
            };

            BoolLiteral =
                from l in CurrentPos
                from b in OneOf(Try(TrueString), FalseString)
                select new ValueToken()
            {
                Value = b, Type = ValueType.Immediate, Position = l
            };

            EnumLiteral =
                from l in CurrentPos
                from e in LetterOrDigit.ManyString().Between(EnumStart, EnumEnd)
                select new ValueToken()
            {
                Value = e, Type = ValueType.Immediate, Position = l
            };

            NullLiteral =
                from l in CurrentPos
                from n in NullString
                select new ValueToken()
            {
                Value = string.Empty, Type = ValueType.Immediate, Position = l
            };

            Address =
                from l in CurrentPos
                from a in AddressMarker
                from n in Num
                select new ValueToken()
            {
                Value = n.ToString(), Type = ValueType.Address, Position = l
            };

            Directive =
                from l in CurrentPos
                from d in DirectiveMarker
                from n in LetterOrDigit.Or(DirectiveMarker).ManyString()
                select new ValueToken()
            {
                Value = n, Type = ValueType.Directive, Position = l
            };

            Reference =
                from l in CurrentPos
                from r in ReferenceMarker
                from n in LetterOrDigit.Or(DirectiveMarker).ManyString()
                select new ValueToken()
            {
                Value = n, Type = ValueType.Reference, Position = l
            };

            Value = OneOf(
                NumLiteral.Labelled("number"),
                CharLiteral.Labelled("char"),
                StringLiteral.Labelled("string"),
                EnumLiteral.Labelled("enum"),
                Try(BoolLiteral).Labelled("bool"),
                Try(NullLiteral).Labelled("null"),
                Address.Labelled("address"),
                Directive.Labelled("directive"),
                Reference.Labelled("reference"));

            CompilerCommand =
                from l in CurrentPos
                from c in LetterOrDigit.AtLeastOnceString().Where(n => ReservedCommandNames.Contains(n)).Labelled("reserved command")
                from v in Try(SkipWhitespaces.Then(Value)).Many()
                select new CommandCall()
            {
                Name     = c,
                Inputs   = v.ToArray(),
                Type     = CallType.Compiler,
                Position = l
            };

            CallCommand =
                from l in CurrentPos
                from c in LetterOrDigit.Or(AnyCommandMarker).AtLeastOnceString().Where(n => KnownCommandNames.Contains(n)).Labelled("known command")
                from v in Try(SkipWhitespaces.Then(Value)).Many()
                select new CommandCall()
            {
                Name     = c,
                Inputs   = v.ToArray(),
                Type     = CallType.Command,
                Position = l
            };

            Command = OneOf(Try(CompilerCommand).Labelled("compiler call"), CallCommand.Labelled("command call"));
            Code    = OneOf(
                Try(CommentHead.Then(ExceptEndLine.SkipMany())).ThenReturn <CommandCall?>(null).Labelled("comment"),
                Command.Select <CommandCall?>(c => c))
                      .SeparatedAndOptionallyTerminatedAtLeastOnce(SkipWhitespaces);
        }