SharpC: A C Interpreter In C# - 0010

类型

程序支持以下类型:void, char, short, int, float, 指针。
关联到表达式的操作数,就有:

  • Operand:操作数基类。
  • Value:值类型。
  • ValueOfPointer:指针类型。
  • ValueOfPointerIndiction:指针内容类型。
  • ValueOfVariableReference:变量引用类型。
  • ValueOfFunctionCalling:函数调用类型。不用奇怪,这里将函数调用归结到类型系统以简化操作。

类型定义

public enum PrimitiveDataType
{
  VoidType = 0x0001,
  CharType = 0x0002,
  ShortType = 0x0004,
  IntType = 0x0008,
  FloatType = 0x0010,

  SignedType = 0x0100,
  UnsignedType = 0x0200,

  ConstType = 0x1000,
  PointerType = 0x2000,
  StructureType = 0x4000,

  BaseTypeMask = 0x00FF,
  PointerMask = 0xDFFF
};

类型信息

public struct DataTypeInfo
{
  public PrimitiveDataType Type;
  public int PointerCount;

  public PrimitiveDataType BaseType

  public bool IsPointer

  public bool IsUnsigned

  ...

变量信息

public class Variable : Context
{
  public DataTypeInfo TypeInfo;
  public int Address;
  public int PointerAddress;
  public int ReferenceCount = 0;

        ...

函数信息

public class FunctionDefine : Context
{
  private Stack> m_parameterStack;

  public DataTypeInfo ReturnType;
  public Expression.Operand.Operand ReturnValue;

  public bool IsVariableArgument;
  public int ReferenceCount;
  public int IteratorCount = 0;

  public List ArgumentDefinitions;
  public Block Body;

...

表达式信息

public class ExpressionNode
{
  public Expression.ExpressionToken Token;
  public ExpressionNode LeftNode;
  public ExpressionNode RightNode;

  public override string ToString()
  {
      ...

  }

  public Operand.Operand Evaluate(Context ctx)
  {
      ...        

  }

  public DataTypeInfo ResultType(Context ctx)
  {
      // 表达式结果类型推导

      ...        

  } // func ResultType
}

类型系统粗就,下一节从运行过程来深入了解SharpC。

你可能感兴趣的:(c#)