34 lines
1.1 KiB
C#
34 lines
1.1 KiB
C#
|
using System.Collections.Generic;
|
||
|
using TLang.Common;
|
||
|
|
||
|
namespace TLang.Common
|
||
|
{
|
||
|
public class BytecodeChunk
|
||
|
{
|
||
|
public int CurrentByteLength {get;set;}=0;
|
||
|
public void Add(Instruction instruction)
|
||
|
{
|
||
|
var i = instruction as LabelInstruction;
|
||
|
if(i != null) Offsets.Add(i.Key,CurrentByteLength);
|
||
|
else {
|
||
|
CurrentByteLength += instruction.CalculateLength();
|
||
|
Instructions.Add(instruction);
|
||
|
}
|
||
|
}
|
||
|
public void SetLabelable()
|
||
|
{
|
||
|
foreach(var item in Instructions)
|
||
|
{
|
||
|
var item2 = item as ILabelable;
|
||
|
if(item2 != null)
|
||
|
{
|
||
|
if(Offsets.ContainsKey(item2.Key)) item2.Value = Offsets[item2.Key];
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
public Dictionary<string,int> Offsets {get;set;}=new Dictionary<string, int>();
|
||
|
public List<string> Arguments {get;set;}=new List<string>();
|
||
|
public List<Instruction> Instructions {get;set;}=new List<Instruction>();
|
||
|
}
|
||
|
}
|