Ejemplo n.º 1
0
 void ConditionalAddShort([NotNull] IWordBuilder wb, string word, CompileConstantDelegate compileConstant,
                          [CanBeNull] ZilObject value)
 {
     if (value == null)
     {
         wb.AddShort(0);
     }
     else
     {
         var operand = compileConstant(value);
         if (operand == null)
         {
             ctx.HandleError(new CompilerError(
                                 CompilerMessages.Nonconstant_Initializer_For_0_1_2,
                                 "vocab word",
                                 word,
                                 value.ToString()));
             wb.AddShort(0);
         }
         else
         {
             wb.AddShort(operand);
         }
     }
 }
Ejemplo n.º 2
0
        public void WriteToBuilder(IWord word, IWordBuilder wb, WriteToBuilderHelpers helpers)
        {
            var  zversion          = ctx.ZEnvironment.ZVersion;
            var  nw                = (NewParserWord)word;
            bool needSemanticStuff = false;

            if (zversion >= 4)
            {
                if (nw.HasClass(dirClass))
                {
                    ConditionalAddByte(wb, word.Atom.Text, helpers.CompileConstant, nw.DirId);
                    wb.AddByte(0);
                }
                else if (!nw.HasClass(verbClass))
                {
                    ConditionalAddShort(wb, word.Atom.Text, helpers.CompileConstant, nw.SemanticStuff);
                }
                else
                {
                    needSemanticStuff = true;
                }
            }
            else
            {
                bool adj = nw.HasClass(adjClass), dir = nw.HasClass(dirClass);
                if (adj || dir)
                {
                    if (adj)
                    {
                        ConditionalAddByte(wb, word.Atom.Text, helpers.CompileConstant, nw.AdjId);
                    }
                    else
                    {
                        wb.AddByte(0);
                    }

                    if (dir)
                    {
                        ConditionalAddByte(wb, word.Atom.Text, helpers.CompileConstant, nw.DirId);
                    }
                    else
                    {
                        wb.AddByte(0);
                    }
                }
                else
                {
                    ConditionalAddShort(wb, word.Atom.Text, helpers.CompileConstant, nw.SemanticStuff);
                }
            }

            bool verbed = false;

            if (nw.HasClass(verbClass))
            {
                var       verbStuff = nw.VerbStuff;
                ZilObject verbStuffId;

                if ((IsVerbPointer(verbStuff) && (verbStuffId = verbStuff.GetPrimitive(ctx)) != null) ||
                    TryGetVerbStuffId(verbStuff, out verbStuffId))
                {
                    if (verbStuffId.StdTypeAtom == StdAtom.VWORD)
                    {
                        verbStuffId = NewParserWord.FromVword(ctx, (ZilHash)verbStuffId).Atom;
                    }
                    var actTableAtom = ZilAtom.Parse("ACT?" + ((ZilAtom)verbStuffId).Text, ctx);
                    var actConstant  = helpers.CompileConstant(actTableAtom);
                    Debug.Assert(actConstant != null);
                    wb.AddShort(actConstant);
                    verbed = true;
                }
            }

            if (!verbed)
            {
                if (zversion == 3)
                {
                    wb.AddShort(0);
                }
                else if (needSemanticStuff)
                {
                    ConditionalAddShort(wb, word.Atom.Text, helpers.CompileConstant, nw.SemanticStuff);
                }
            }

            if (!ctx.GetCompilationFlagOption(StdAtom.WORD_FLAGS_IN_TABLE))
            {
                wb.AddShort((short)nw.Flags);
            }

            if (ctx.GetCompilationFlagOption(StdAtom.ONE_BYTE_PARTS_OF_SPEECH))
            {
                var lowByte  = (byte)(nw.Classification & 0x7f);
                var highByte = (byte)((nw.Classification >> 7) & 0x7f);
                if (lowByte != 0 && highByte != 0)
                {
                    ctx.HandleError(new CompilerError(CompilerMessages.ONEBYTEPARTSOFSPEECH_Loses_Data_For_0, word.Atom.Text));
                }

                if (highByte != 0)
                {
                    wb.AddByte((byte)(highByte | 0x80));
                }
                else
                {
                    wb.AddByte(lowByte);
                }
            }
            else
            {
                wb.AddShort((short)nw.Classification);
            }
        }