void ConditionalAddByte([NotNull] IWordBuilder wb, string word, CompileConstantDelegate compileConstant, [CanBeNull] ZilObject value) { if (value == null) { wb.AddByte(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.AddByte(0); } else { wb.AddByte(operand); } } }
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); } }
public void WriteToBuilder([NotNull] Context ctx, [NotNull] IWordBuilder wb, [NotNull] DirIndexToPropertyOperandDelegate dirIndexToPropertyOperand) { // discard excess parts of speech if needed CheckTooMany(ctx); var pos = PartOfSpeech; var partsToWrite = new List <PartOfSpeech>(2); var compactVocab = IsCompactVocab(ctx); // expand parts of speech, observing the First flags and a few special cases if ((pos & PartOfSpeech.Adjective) != 0) { // in V4+, don't write a value for Adjective if (ctx.ZEnvironment.ZVersion < 4) { if ((pos & PartOfSpeech.FirstMask) == PartOfSpeech.AdjectiveFirst) { partsToWrite.Insert(0, PartOfSpeech.Adjective); } else { partsToWrite.Add(PartOfSpeech.Adjective); } } } if ((pos & PartOfSpeech.Direction) != 0) { if ((pos & PartOfSpeech.FirstMask) == PartOfSpeech.DirectionFirst) { partsToWrite.Insert(0, PartOfSpeech.Direction); } else { partsToWrite.Add(PartOfSpeech.Direction); } } if ((pos & PartOfSpeech.Verb) != 0) { if ((pos & PartOfSpeech.FirstMask) == PartOfSpeech.VerbFirst) { partsToWrite.Insert(0, PartOfSpeech.Verb); } else { partsToWrite.Add(PartOfSpeech.Verb); } } if ((pos & PartOfSpeech.Object) != 0) { // for CompactVocab and NewVoc, don't write a value for Object if (!compactVocab && !IsNewVoc(ctx)) { // there is no ObjectFirst, so keep it first if all other First flags are clear if ((pos & PartOfSpeech.FirstMask) == 0) { partsToWrite.Insert(0, PartOfSpeech.Object); } else { partsToWrite.Add(PartOfSpeech.Object); } } } if ((pos & PartOfSpeech.Buzzword) != 0) { // for CompactVocab, don't write a value for Buzzword if (!compactVocab) { // there is no BuzzwordFirst: Buzzword comes before everything but Preposition partsToWrite.Insert(0, PartOfSpeech.Buzzword); } } if ((pos & PartOfSpeech.Preposition) != 0) { // for CompactVocab, don't write a value for Preposition if (!compactVocab) { partsToWrite.Insert(0, PartOfSpeech.Preposition); } } // write part of speech flags wb.AddByte((byte)pos); // write values int limit = compactVocab ? 1 : 2; for (int i = 0; i < limit; i++) { if (i < partsToWrite.Count) { var p = partsToWrite[i]; var value = GetValue(p); if (p == PartOfSpeech.Direction) { wb.AddByte(dirIndexToPropertyOperand(value)); } else { wb.AddByte(value); } } else { wb.AddByte(0); } } }