Exemplo n.º 1
0
        void MakePart(IWord word, ISourceLine location, PartOfSpeech part, string description,
                      Func <OldParserWord, Action <Context, ISourceLine, byte> > getSetter, ref byte next, bool onlyNumberedInV3 = false)
        {
            var result = (OldParserWord)word;

            if ((result.PartOfSpeech & part) != 0)
            {
                return;
            }

            byte value;

            if (!onlyNumberedInV3 || ctx.ZEnvironment.ZVersion == 3)
            {
                if (next == 0)
                {
                    throw new InterpreterError(
                              InterpreterMessages.Too_Many_0_Only_1_Allowed_In_This_Vocab_Format,
                              description,
                              255);
                }

                value = next--;
            }
            else
            {
                value = 0;
            }

            getSetter(result)(ctx, location, value);
        }
Exemplo n.º 2
0
        /// <exception cref="InvalidOperationException">Too many adjectives.</exception>
        public void MakeAdjective(IWord word, ISourceLine location)
        {
            var nw = (NewParserWord)word;

            if (!nw.HasClass(adjClass))
            {
                ZilFix value;
                if (ctx.ZEnvironment.ZVersion < 4)
                {
                    if (nextAdjective == 0)
                    {
                        throw new InterpreterError(
                                  InterpreterMessages.Too_Many_0_Only_1_Allowed_In_This_Vocab_Format,
                                  "adjectives",
                                  255);
                    }

                    value = new ZilFix(nextAdjective--);
                }
                else
                {
                    value = null;
                }

                NewAddWord(
                    nw.Atom,
                    ctx.GetStdAtom(StdAtom.TADJ),
                    value,
                    ZilFix.Zero);
            }
        }
Exemplo n.º 3
0
        public Syntax(ISourceLine src, [NotNull] IWord verb, int numObjects, [CanBeNull] IWord prep1, [CanBeNull] IWord prep2,
                      byte options1, byte options2, [CanBeNull] ZilAtom findFlag1, [CanBeNull] ZilAtom findFlag2,
                      [NotNull] ZilAtom action, [CanBeNull] ZilAtom preaction, [NotNull] ZilAtom actionName,
                      [ItemNotNull][CanBeNull] IEnumerable <ZilAtom> synonyms = null)
        {
            SourceLine = src;

            Verb         = verb;
            NumObjects   = numObjects;
            Preposition1 = prep1;
            Preposition2 = prep2;
            Options1     = options1;
            Options2     = options2;
            FindFlag1    = findFlag1;
            FindFlag2    = findFlag2;
            Action       = action;
            Preaction    = preaction;
            ActionName   = actionName;

            if (synonyms == null)
            {
                Synonyms = EmptySynonyms;
            }
            else
            {
                Synonyms = new List <ZilAtom>(synonyms).AsReadOnly();
            }
        }
Exemplo n.º 4
0
 public LocalBindingRecord(LocalBindingType type, [NotNull] ISourceLine definition, [NotNull] string boundName,
                           [NotNull] ILocalBuilder storage)
 {
     Type         = type;
     Definition   = definition;
     LocalBuilder = storage;
     BoundName    = boundName;
 }
Exemplo n.º 5
0
        public void SetObject(ISourceLine location)
        {
            if ((PartOfSpeech & PartOfSpeech.Object) == 0)
            {
                // there is no PartOfSpeech.ObjectFirst, so don't change the First flags

                PartOfSpeech |= PartOfSpeech.Object;
                speechValues[PartOfSpeech.Object] = 1;
                definitions[PartOfSpeech.Object]  = location;
            }
        }
Exemplo n.º 6
0
        public void MakeObject(IWord word, ISourceLine location)
        {
            var nw = (NewParserWord)word;

            if (!nw.HasClass(objectClass))
            {
                NewAddWord(
                    nw.Atom,
                    ctx.GetStdAtom(StdAtom.TOBJECT),
                    null,
                    ZilFix.Zero);
            }
        }
Exemplo n.º 7
0
        public void MakeSyntaxPreposition(IWord word, ISourceLine location)
        {
            var nw = (NewParserWord)word;

            if (!nw.HasClass(particleClass))
            {
                NewAddWord(
                    nw.Atom,
                    ctx.GetStdAtom(StdAtom.PARTICLE),
                    null,
                    ZilFix.Zero);
            }
        }
Exemplo n.º 8
0
        public void MakePreposition(IWord word, ISourceLine location)
        {
            var nw = (NewParserWord)word;

            if (!nw.HasClass(prepClass))
            {
                NewAddWord(
                    nw.Atom,
                    ctx.GetStdAtom(StdAtom.TPREP),
                    null,
                    ZilFix.Zero);
            }
        }
Exemplo n.º 9
0
        public void MakeBuzzword(IWord word, ISourceLine location)
        {
            var nw = (NewParserWord)word;

            if (!nw.HasClass(buzzClass))        // always true since this translates to 0
            {
                NewAddWord(
                    nw.Atom,
                    ctx.GetStdAtom(StdAtom.TBUZZ),
                    null,
                    ZilFix.Zero);
            }
        }
Exemplo n.º 10
0
 public Diagnostic([NotNull] ISourceLine location, Severity severity,
                   [NotNull] string codePrefix, int codeNumber,
                   [NotNull] string messageFormat, [ItemNotNull][CanBeNull] object[] messageArgs,
                   [CanBeNull] string stackTrace, [ItemNotNull][CanBeNull] IReadOnlyList <Diagnostic> subDiagnostics)
 {
     Location       = location;
     Severity       = severity;
     CodePrefix     = codePrefix;
     CodeNumber     = codeNumber;
     MessageFormat  = messageFormat;
     MessageArgs    = messageArgs ?? NoArguments;
     StackTrace     = stackTrace;
     SubDiagnostics = subDiagnostics ?? NoDiagnostics;
 }
Exemplo n.º 11
0
        public void SetDirection([NotNull] Context ctx, ISourceLine location, byte value)
        {
            if ((PartOfSpeech & PartOfSpeech.Direction) == 0)
            {
                if (ShouldSetFirst(ctx))
                {
                    PartOfSpeech |= PartOfSpeech.DirectionFirst;
                }

                PartOfSpeech |= PartOfSpeech.Direction;
                speechValues[PartOfSpeech.Direction] = value;
                definitions[PartOfSpeech.Direction]  = location;
            }
        }
Exemplo n.º 12
0
        public void SetAdjective([NotNull] Context ctx, ISourceLine location, byte value)
        {
            if ((PartOfSpeech & PartOfSpeech.Adjective) == 0)
            {
                if (ctx.ZEnvironment.ZVersion < 4 && ShouldSetFirst(ctx))
                {
                    PartOfSpeech |= PartOfSpeech.AdjectiveFirst;
                }

                PartOfSpeech |= PartOfSpeech.Adjective;
                speechValues[PartOfSpeech.Adjective] = value;
                definitions[PartOfSpeech.Adjective]  = location;
            }
        }
Exemplo n.º 13
0
        public void SetBuzzword([NotNull] Context ctx, ISourceLine location, byte value)
        {
            if ((PartOfSpeech & PartOfSpeech.Buzzword) == 0)
            {
                // buzzword value comes before everything but preposition, except in CompactVocab
                if (!IsCompactVocab(ctx))
                {
                    PartOfSpeech &= ~PartOfSpeech.FirstMask;
                }

                PartOfSpeech |= PartOfSpeech.Buzzword;
                speechValues[PartOfSpeech.Buzzword] = value;
                definitions[PartOfSpeech.Buzzword]  = location;
            }
        }
Exemplo n.º 14
0
        public void MakeDirection(IWord word, ISourceLine location)
        {
            var result = (OldParserWord)word;

            if ((result.PartOfSpeech & PartOfSpeech.Direction) == 0)
            {
                var index = ctx.ZEnvironment.Directions.IndexOf(result.Atom);
                if (index == -1)
                {
                    throw new ArgumentException("Not a direction");
                }

                result.SetDirection(ctx, location, (byte)index);
            }
        }
Exemplo n.º 15
0
        public Diagnostic GetDiagnostic(ISourceLine location, int code, object[] messageArgs,
                                        string stackTrace, Diagnostic[] subDiagnostics)
        {
            var attr = messages[code];

            return(new Diagnostic(
                       location,
                       attr.Severity,
                       prefix,
                       code,
                       attr.Format,
                       messageArgs,
                       stackTrace,
                       subDiagnostics));
        }
Exemplo n.º 16
0
        public void SetPreposition([NotNull] Context ctx, ISourceLine location, byte value)
        {
            if ((PartOfSpeech & PartOfSpeech.Preposition) == 0)
            {
                // preposition value is always first, except in CompactVocab
                if (!IsCompactVocab(ctx))
                {
                    PartOfSpeech &= ~PartOfSpeech.FirstMask;
                }

                PartOfSpeech |= PartOfSpeech.Preposition;
                speechValues[PartOfSpeech.Preposition] = value;
                definitions[PartOfSpeech.Preposition]  = location;
            }
        }
Exemplo n.º 17
0
        public ILocalBuilder PushInnerLocal([NotNull] IRoutineBuilder rb, [NotNull] ZilAtom atom,
                                            LocalBindingType reason, ISourceLine src)
        {
            if (Locals.TryGetValue(atom, out var prev))
            {
                // save the old binding
                if (OuterLocals.TryGetValue(atom, out var stk) == false)
                {
                    stk = new Stack <LocalBindingRecord>();
                    OuterLocals.Add(atom, stk);
                }
                stk.Push(prev);
            }

            ILocalBuilder result;

            if (SpareLocals.Count > 0)
            {
                // reuse a spare variable
                result = SpareLocals.Pop();
            }
            else
            {
                // allocate a new variable with a unique name
                var tempName = MakeUniqueVariableName(atom);

                try
                {
                    result = rb.DefineLocal(tempName.Text);
                }
                catch (InvalidOperationException)
                {
                    throw new CompilerError(CompilerMessages.Expression_Needs_Temporary_Variables_Not_Allowed_Here);
                }

                TempLocalNames.Add(tempName);
            }

            var lbr = new LocalBindingRecord(reason, src, atom.Text, result);

            Locals[atom] = lbr;
            AllLocalBindingRecords.Add(lbr);
            return(result);
        }
Exemplo n.º 18
0
        /// <exception cref="ArgumentException"><paramref name="word"/> is not a direction.</exception>
        public void MakeDirection(IWord word, ISourceLine location)
        {
            var nw = (NewParserWord)word;

            if (!nw.HasClass(dirClass))
            {
                var index = ctx.ZEnvironment.Directions.IndexOf(nw.Atom);
                if (index == -1)
                {
                    throw new ArgumentException("Not a direction");
                }

                NewAddWord(
                    nw.Atom,
                    ctx.GetStdAtom(StdAtom.TDIR),
                    ZilAtom.Parse("P?" + nw.Atom.Text, ctx),
                    ZilFix.Zero);
            }
        }
Exemplo n.º 19
0
        public void MakeVerb(IWord word, ISourceLine location)
        {
            var nw = (NewParserWord)word;

            if (!nw.HasClass(verbClass))
            {
                var form     = new ZilForm(new ZilObject[] { ctx.GetStdAtom(StdAtom.MAKE_VERB_DATA) });
                var verbData = (ZilObject)form.Eval(ctx);

                nw.VerbStuff = verbData;
                ctx.PutProp(verbData, ctx.GetStdAtom(StdAtom.VERB_STUFF_ID), nw.Inner);

                NewAddWord(
                    nw.Atom,
                    ctx.GetStdAtom(StdAtom.TVERB),
                    verbData,
                    ZilFix.Zero);
            }
        }
Exemplo n.º 20
0
        public static Syntax Parse(ISourceLine src, [NotNull] IEnumerable <ZilObject> definition, [NotNull] Context ctx)
        {
            int     numObjects = 0;
            ZilAtom verb = null, prep1 = null, prep2 = null;
            ZilAtom action = null, preaction = null, actionName = null;
            ZilList bits1 = null, find1 = null, bits2 = null, find2 = null, syns = null;
            bool    rightSide = false;
            int     rhsCount  = 0;

            // main parsing
            foreach (var obj in definition)
            {
                if (verb == null)
                {
                    if (obj is ZilAtom atom && atom.StdAtom != StdAtom.Eq)
                    {
                        verb = atom;
                    }
                    else
                    {
                        throw new InterpreterError(InterpreterMessages.Missing_0_In_1, "verb", "syntax definition");
                    }
                }
Exemplo n.º 21
0
        /// <exception cref="InterpreterError">Syntax error.</exception>
        public static IEnumerable <ZilObject> Parse([NotNull] Context ctx, ISourceLine src, [NotNull] IEnumerable <char> chars, params ZilObject[] templateParams)
        {
            var parser = new Parser(ctx, src, templateParams);

            foreach (var po in parser.Parse(chars))
            {
                if (po.IsIgnorable)
                {
                    continue;
                }

                switch (po.Type)
                {
                case ParserOutputType.Object:
                    yield return(po.Object);

                    break;

                case ParserOutputType.EndOfInput:
                    yield break;

                case ParserOutputType.SyntaxError:
                    throw new InterpreterError(
                              src ?? new FileSourceLine(ctx.CurrentFile.Path, parser.Line),
                              InterpreterMessages.Syntax_Error_0, po.Exception.Message);

                case ParserOutputType.Terminator:
                    throw new InterpreterError(
                              src ?? new FileSourceLine(ctx.CurrentFile.Path, parser.Line),
                              InterpreterMessages.Syntax_Error_0, "misplaced terminator");

                default:
                    throw new UnhandledCaseException("parser output type");
                }
            }
        }
Exemplo n.º 22
0
 protected AssemblerError(ISourceLine node, string message)
     : base(message)
 {
     Node = node;
 }
Exemplo n.º 23
0
 public Token(ISourceLine line)
 {
     Line = line;
 }
Exemplo n.º 24
0
 void ReportErrorAndSkipExpr(ISourceLine node, [NotNull] string message)
 {
     ReportError(node, message);
     SkipExpr();
 }
Exemplo n.º 25
0
 void ReportErrorAndSkipExpr(ISourceLine node, [NotNull] string format, [NotNull] params object[] args)
 {
     ReportError(node, format, args);
     SkipExpr();
 }
Exemplo n.º 26
0
 public static Diagnostic GetDiagnostic([NotNull] this IDiagnosticFactory fac, [NotNull] ISourceLine location, int code, object[] messageArgs, string stackTrace)
 {
     return(fac.GetDiagnostic(location, code, messageArgs, stackTrace, null));
 }
Exemplo n.º 27
0
        public static ZilResult ToZilListResult([NotNull] this IEnumerable <ZilResult> inputs, ISourceLine sourceLine)
        {
            var array = inputs.Trim().ToArray();

            if (array.Length > 0 && array[array.Length - 1].ShouldPass())
            {
                return(array[array.Length - 1]);
            }

            return(new ZilList(array.Select(i => (ZilObject)i))
            {
                SourceLine = sourceLine
            });
        }
Exemplo n.º 28
0
 public Warning(ISourceLine node, string message)
     : base(node, message)
 {
 }
Exemplo n.º 29
0
 public FatalError(ISourceLine node, string message)
     : base(node, message)
 {
 }
Exemplo n.º 30
0
 public CompilerFatal(ISourceLine sourceLine, int code, [ItemNotNull] params object[] messageArgs)
     : base(MakeDiagnostic(sourceLine, code, messageArgs))
 {
 }
Exemplo n.º 31
0
 public CompilerFatal(ISourceLine sourceLine, int code)
     : this(sourceLine, code, null)
 {
 }