tlang-runtime-compiler/TLang.Common/PushStringInstruction.cs

31 lines
825 B
C#
Raw Normal View History

2023-07-29 00:11:09 +00:00
using System;
using System.IO;
using TLang.Common;
namespace TLang.BytecodeCompiler
{
public class PushStringInstruction : Instruction
{
public string Value {get;set;}
public PushStringInstruction(string value)
{
Value = value;
}
public override int CalculateLength()
{
return System.Text.Encoding.UTF8.GetByteCount(Value) + 5;
}
public override void WriteData(Stream strm)
{
strm.WriteByte(PUSH_STRING);
byte[] bytes = System.Text.Encoding.UTF8.GetBytes(Value);
var data=BitConverter.GetBytes(bytes.Length);
if(BitConverter.IsLittleEndian) Array.Reverse(data);
strm.Write(data,0,data.Length);
strm.Write(bytes,0,bytes.Length);
}
}
}