⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 syntax.cs

📁 compiler programming, code something
💻 CS
📖 第 1 页 / 共 3 页
字号:
		/// <summary>
		/// Creates a function object.
		/// </summary>
		public Function(Body body, ParameterCollection parameters, string name, Type type)
		{
			Body = body;	Parameters = parameters;	Name = name;	Type = type;
		}
	}

	/// <summary>
	/// Describes a function call statement.
	/// </summary>
	public class CallStatement : Statement
	{
		/// <summary>
		/// Function to call.
		/// </summary>
		public string Name;

		/// <summary>
		/// Function arguments to pass.
		/// </summary>
		public ArgumentCollection Arguments;

		/// <summary>
		/// Creates a function call object.
		/// </summary>
		public CallStatement(ArgumentCollection arguments, string name)
		{
			Arguments = arguments;	Name = name;
		}
	}

	/// <summary>
	/// Describes a function call.
	/// </summary>
	public class Call : Expression
	{
		/// <summary>
		/// Function to call.
		/// </summary>
		public string Name;

		/// <summary>
		/// Function arguments to pass.
		/// </summary>
		public ArgumentCollection Arguments;

		/// <summary>
		/// Creates a function call object.
		/// </summary>
		public Call(ArgumentCollection arguments, string name)
		{
			Arguments = arguments;	Name = name;
		}
	}

	/// <summary>
	/// Describes a structure.
	/// </summary>
	public class Structure : Statement
	{
		/// <summary>
		/// Structure name.
		/// </summary>
		public string Name;

		/// <summary>
		/// Structure variables.
		/// </summary>
		public VariableCollection Variables = null;

		/// <summary>
		/// Create a structure object.
		/// </summary>
		public Structure(VariableCollection variables, string name)
		{
			Variables = variables;	Name = name;
		}

	}

	/// <summary>
	/// Describes a variable.
	/// </summary>
	public class Variable : Statement
	{
		/// <summary>
		/// Variable name.
		/// </summary>
		public string Name;
		/// <summary>
		/// Variable type.
		/// </summary>
		public Type Type;

		/// <summary>
		/// Variable initial value;
		/// </summary>
		public object Value;

		/// <summary>
		/// Create a variable object.
		/// </summary>
		public Variable(object value, string name, Type type)
		{
			Name = name;	Type = type;	Value = value;
		}
	}

	/// <summary>
	/// Describes a parameter.
	/// </summary>
	public class Parameter
	{
		/// <summary>
		/// Parameter name.
		/// </summary>
		public string Name;
		/// <summary>
		/// Parameter type.
		/// </summary>
		public Type Type;
		/// <summary>
		/// Parameter pass method.
		/// </summary>
		public PassMethod PassMethod = PassMethod.ByValue;

		/// <summary>
		/// Create parameter object.
		/// </summary>
		public Parameter(string name, Type type, PassMethod passMethod)
		{
			Name = name;	Type = type;	PassMethod = passMethod;
		}
	}

	/// <summary>
	/// Describes an argument.
	/// </summary>
	public class Argument
	{
		/// <summary>
		/// Argument expression.
		/// </summary>
		public Expression Value;

		/// <summary>
		/// Argument pass method.
		/// </summary>
		public PassMethod PassMethod = PassMethod.ByValue;

		/// <summary>
		/// Create argument object.
		/// </summary>
		public Argument(Expression value, PassMethod passMethod)
		{
			Value = value;	PassMethod = passMethod;
		}
	}

	/// <summary>
	/// Describes a statement.
	/// </summary>
	public class Statement
	{

	}

	public enum ElementType
	{
		Collection,
		Expression
	}

	/// <summary>
	/// Describes an array or structure initialization element.
	/// </summary>
	public class Element
	{
		public ElementType ElementType = ElementType.Expression;
		public ElementCollection Elements = null;
		public Expression Expression = null;

		/// <summary>
		/// Creates an element collection element object.
		/// </summary>
		public Element(ElementCollection elements)
		{
			ElementType = ElementType.Collection;
			Elements = elements;
		}

		/// <summary>
		/// Creates a expression element object.
		/// </summary>
		public Element(Expression expression)
		{
			ElementType = ElementType.Expression;
			Expression = expression;
		}
	}

	public abstract class Expression
	{
		public Type Type;
	}

	public class BinaryExpression : Expression
	{
		public Expression Right = null;
		public Expression Left = null;
		public BinaryOperatorType BinaryOperatorType = BinaryOperatorType.None;

		public BinaryExpression(Expression right, Expression left, BinaryOperatorType binaryOperatorType)
		{
			Right = right;	Left = left;	BinaryOperatorType = binaryOperatorType;
		}
	}

	public class UnaryExpression : Expression
	{
		public Expression Value = null;
		public Expression Indexer = null;
		public UnaryOperatorType UnaryOperatorType = UnaryOperatorType.None;

		public UnaryExpression(Expression indexer, Expression value, UnaryOperatorType unaryOperatorType)
		{
			Value = value;	Indexer = indexer;	UnaryOperatorType = unaryOperatorType;
		}
	}

	public class Name : Expression
	{
		public string Value;
		public Name(string value)
		{
			Value = value;
		}
	}

	/// <summary>
	/// Describes a return statement.
	/// </summary>
	public class Return : Statement
	{
		/// <summary>
		/// Return value;
		/// </summary>
		public Expression Value;
		/// <summary>
		/// Create return object.
		/// </summary>
		/// <param name="value"></param>
		public Return(Expression value)
		{
			Value = value;
		}
	}

	public class If : Statement
	{
		public Expression Condition = null;
		public Body IfBody = null;
		public Body ElseBody = null;

		public If(Body elseBody, Body ifBody, Expression condition)
		{
			ElseBody = elseBody;	IfBody = ifBody;	Condition = condition;
		}
	}

	public class While : Statement
	{
		public Expression Condition = null;
		public Body Body = null;

		public While(Body body, Expression condition)
		{
			Body = body;
			Condition = condition;
		}
	}

	public class Do : Statement
	{
		public Expression Condition = null;
		public Body Body = null;

		public Do(Expression condition,Body body)
		{
			Body = body;
			Condition = condition;
		}
	}

	public class For : Statement
	{
		public Assignment Initializer = null;
		public Expression Condition = null;
		public Assignment Counter = null;
		public Body Body = null;

		public For(Body body, Assignment counter, Expression condition, Assignment initializer)
		{
			Body = body;	Counter = counter;	Condition = condition;	Initializer = initializer;
		}
	}

	public class Assignment : Statement
	{
		public string Name;
		public Expression Value;
		public Expression Index;
		public Assignment(Expression value, Expression index, string name)
		{
			Value = value;	Name = name;	Index = index;
		}
	}

	public class Semantics
	{
		public static void Apply(Production production, SyntaxStack stack)
		{
			switch(production.m_ID)
			{
				case 0: // <Literal> ::= boolean_literal
					// Create boolean literal object.
					stack.Push(new Literal(stack.PopString(),LiteralType.Boolean));
					break;
				case 1: // <Literal> ::= integer_literal
					// Create integer literal object.
					stack.Push(new Literal(stack.PopString(),LiteralType.Integer));
					break;
				case 2: // <Literal> ::= real_literal
					// Create real literal object.
					stack.Push(new Literal(stack.PopString(),LiteralType.Real));
					break;
				case 3: // <Literal> ::= character_literal
					// Create character literal object.
					stack.Push(new Literal(stack.PopString(),LiteralType.Character));
					break;
				case 4: // <Literal> ::= string_literal
					// Create string literal object.
					stack.Push(new Literal(stack.PopString(),LiteralType.String));
					break;
				case 5: // <Type> ::= <Structure_Type>
					// !!! DO NOTHING !!!
					break;
				case 6: // <Type> ::= <Primitive_Type>
					// !!! DO NOTHING !!!
					break;
				case 7: // <Type> ::= <Array_Type>
					// !!! DO NOTHING !!!
					break;
				case 8: // <Structure_Type> ::= identifier
					// Create structure type object.
					stack.Push(new Type(stack.PopString()));
					break;
				case 9: // <Primitive_Type> ::= bool
					// Create a boolean type object.
					stack.Pop(1);
					stack.Push(new Type(PrimitiveType.Boolean));
					break;
				case 10: // <Primitive_Type> ::= int
					// Create an integer type object.
					stack.Pop(1);
					stack.Push(new Type(PrimitiveType.Integer));
					break;
				case 11: // <Primitive_Type> ::= real
					// Create an real type object.

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -