Executable
using CLanguage.Types;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Text;
namespace CLanguage.Interpreter
{
[System.Runtime.CompilerServices.NullableContext(1)]
[System.Runtime.CompilerServices.Nullable(0)]
public class Executable
{
private readonly List<CompiledVariable> globals = new List<CompiledVariable>();
public MachineInfo MachineInfo { get; set; }
public List<BaseFunction> Functions { get; set; }
public IReadOnlyList<CompiledVariable> Globals => globals;
public Executable(MachineInfo machineInfo)
{
MachineInfo = machineInfo;
Functions = new List<BaseFunction>();
Functions.AddRange(machineInfo.InternalFunctions.Cast<BaseFunction>());
}
public CompiledVariable AddGlobal(string name, CType type)
{
CompiledVariable compiledVariable = Globals.LastOrDefault();
int offset = (compiledVariable != null) ? (compiledVariable.StackOffset + compiledVariable.VariableType.NumValues) : 0;
CompiledVariable compiledVariable2 = new CompiledVariable(name, offset, type);
globals.Add(compiledVariable2);
return compiledVariable2;
}
public Value GetConstantMemory(string stringConstant)
{
int count = Globals.Count;
byte[] bytes = Encoding.UTF8.GetBytes(stringConstant);
int value = bytes.Length + 1;
CArrayType type = new CArrayType(CBasicType.SignedChar, value);
CompiledVariable compiledVariable = AddGlobal("__c" + Globals.Count.ToString(), type);
compiledVariable.InitialValue = (from x in bytes.Concat(new byte[1])
select (x)).ToArray();
return Value.Pointer(compiledVariable.StackOffset);
}
}
}