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

📄 syntax.cs

📁 compiler programming, code something
💻 CS
📖 第 1 页 / 共 3 页
字号:
/*
Sharp Compiler
Copyright (C) 2003  Michael Bebenita

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
*/
using System;
using System.Reflection;
using System.Reflection.Emit;
using System.Collections;

namespace Core
{
	public class SyntaxStack
	{
		private Stack m_Stack = new Stack(100);
		public Stack Stack{get{return m_Stack;}}
		public object Pop(){return m_Stack.Pop();}
		public object Peek(){return m_Stack.Peek();}
		public void Push(object value){m_Stack.Push(value);}
		public void Pop(int count)
		{
			while(count-- > 0)
				m_Stack.Pop();
		}

		/// <summary>
		/// Remove item at specified depth off the stack. 0 means top of stack.
		/// </summary>
		public void Remove(int depth)
		{
			Stack temp = new Stack();
			for(;depth > 0;depth --)
				temp.Push(m_Stack.Pop());
			m_Stack.Pop();
			while(temp.Count > 0)
				m_Stack.Push(temp.Pop());
		}

		public string PopString(){return (string)m_Stack.Pop();}
		public Type PopType(){return (Type)m_Stack.Pop();}
		public Body PopBody(){return (Body)m_Stack.Pop();}
		public Expression PopExpression(){return (Expression)m_Stack.Pop();}

		public CollectionBase PeekCollection(){return (CollectionBase)m_Stack.Peek();}

		public Statement PopStatement(){return (Statement)m_Stack.Pop();}
		public Assignment PopAssignment(){return (Assignment)m_Stack.Pop();}
	}


	public enum PassMethod
	{
		ByValue,
		ByReference
	}

	public enum VariableType
	{
		Primitive,
		PrimitiveArray,
		Structure,
		StructureArray
	}

	public enum PrimitiveType
	{
		Boolean,
		Integer,
		Real,
		Character,
		Void
	}

	public enum BinaryOperatorType
	{
		None,
		Add,
		Subtract,
		Multiply,
		Divide,
		Modulo,
		GreaterThen,
		LessThen,
		GraterOrEqualTo,
		LessOrEqualTo,
		Equal,
		NotEqual,
		And,
		Or
	}

	public enum UnaryOperatorType
	{
		None,
		Positive,
		Negative,
		Not,
		Indexer
	}


	/// <summary>
	/// Describes a type.
	/// </summary>
	public class Type
	{
		/// <summary>
		/// Name of type, generally name of structure if VariableType is Structure.
		/// </summary>
		public string Name;
		/// <summary>
		/// Variable type.
		/// </summary>
		public VariableType VariableType = VariableType.Primitive;
		/// <summary>
		/// Primitive type if VariableType is Primitive.
		/// </summary>
		public PrimitiveType PrimitiveType = PrimitiveType.Void;

		public bool IsRef = false;

		/// <summary>
		/// Create a primitive type.
		/// </summary>
		/// <param name="primitiveType"></param>
		public Type(PrimitiveType primitiveType)
		{
			VariableType = VariableType.Primitive;
			PrimitiveType = primitiveType;
		}

		/// <summary>
		/// Create a structure type.
		/// </summary>
		/// <param name="name"></param>
		public Type(string name)
		{
			VariableType = VariableType.Structure;
			Name = name;
		}

		/// <summary>
		/// Creates a .NET system type from type.
		/// </summary>
		/// <returns></returns>
		public System.Type ToSystemType()
		{
			if(!IsRef)
			{
				if(VariableType == VariableType.Primitive)
				{
					if(PrimitiveType == PrimitiveType.Integer)
						return typeof(int);
					else if(PrimitiveType == PrimitiveType.Boolean)
						return typeof(bool);
					else if(PrimitiveType == PrimitiveType.Character)
						return typeof(char);
					else if(PrimitiveType == PrimitiveType.Real)
						return typeof(float);
					else if(PrimitiveType == PrimitiveType.Void)
						return typeof(void);
				}
				else if(VariableType == VariableType.PrimitiveArray)
				{
					if(PrimitiveType == PrimitiveType.Integer)
						return typeof(int []);
					else if(PrimitiveType == PrimitiveType.Boolean)
						return typeof(bool []);
					else if(PrimitiveType == PrimitiveType.Character)
						return typeof(char []);
					else if(PrimitiveType == PrimitiveType.Real)
						return typeof(float []);
				}
			}
			else
			{
				if(VariableType == VariableType.Primitive)
				{
					if(PrimitiveType == PrimitiveType.Integer)
						return System.Type.GetType("System.Int32&");
					else
						return typeof(void);
				}
				else if(VariableType == VariableType.PrimitiveArray)
				{
					if(PrimitiveType == PrimitiveType.Integer)
						return System.Type.GetType("System.Int32[]&");
					else
						return typeof(void);
				}
			}
			return null;
		}

		private Type() {}	// Hidden default constructor.

		/// <summary>
		/// Create a primitive array type from a non array type.
		/// </summary>
		public static Type CreateArrayFromType(Type type)
		{
			if(type.VariableType == VariableType.Primitive)
				type.VariableType = VariableType.PrimitiveArray;
			else if(type.VariableType == VariableType.Structure)
				type.VariableType = VariableType.StructureArray;
			return type;
		}

		/// <summary>
		/// Create a primitive array type.
		/// </summary>
		public static Type CreateArrayType(PrimitiveType primitiveType)
		{
			Type newType = new Type();
			newType.VariableType = VariableType.PrimitiveArray;
			newType.PrimitiveType = primitiveType;
			return newType;
		}

		/// <summary>
		/// Create a structure array type.
		/// </summary>
		public static Type CreateArrayType(string name)
		{
			Type newType = new Type();
			newType.VariableType = VariableType.StructureArray;
			newType.Name = name;
			return newType;
		}

	}


	public enum LiteralType
	{
		Boolean,
		Integer,
		Real,
		Character,
		String
	}

	/// <summary>
	/// Describes a literal found in text
	/// </summary>
	public class Literal : Expression
	{
		/// <summary>
		/// Textual value of the literal, string and character literals include quotes.
		/// </summary>
		public string Value;

		/// <summary>
		/// Type of literal.
		/// </summary>
		public LiteralType LiteralType;

		/// <summary>
		/// Create a literal object.
		/// </summary>
		/// <param name="value">Value of the literal.</param>
		/// <param name="literalType">Literal type.</param>
		public Literal(string value, LiteralType literalType)
		{
			Value = value;	LiteralType = literalType;
		}
	}

	/// <summary>
	/// Describes a compilation unit.
	/// </summary>
	public class Module
	{
		/// <summary>
		/// Name of module.
		/// </summary>
		public string Name;

		/// <summary>
		/// Module body.
		/// </summary>
		public Body Body;

		/// <summary>
		/// Create a module object.
		/// </summary>
		public Module(Body body, string name)
		{
			Body = body;	Name = name;
		}

	}

	/// <summary>
	/// Describes a code body.
	/// </summary>
	public class Body
	{
		public FunctionCollection Functions	= null;
		public StructureCollection Structures = null;
		public StatementCollection Statements = null;

		public Compilation.SymbolTable SymbolTable = null;

		/// <summary>
		/// Create a body from a collection of statements. 
		/// Functions, variables, structures and regular statements will be separated.
		/// </summary>
		public Body(StatementCollection statements)
		{			
			if(statements == null)
				return;

			Functions = new FunctionCollection();
			Structures = new StructureCollection();
			Statements = new StatementCollection();

			foreach(Statement statement in statements)
			{
				if(statement.GetType() == typeof(Function))
					Functions.Add((Function)statement);
				//else if(statement.GetType() == typeof(Variable))
				//	Variables.Add((Variable)statement);
				else if(statement.GetType() == typeof(Structure))
					Structures.Add((Structure)statement);
				else 
					Statements.Add(statement);
			}
		}
	}

	/// <summary>
	/// Describes a function.
	/// </summary>
	public class Function : Statement
	{
		/// <summary>
		/// Function name.
		/// </summary>
		public string Name;
		/// <summary>
		/// Function body.
		/// </summary>
		public Body Body;
		/// <summary>
		/// Function type.
		/// </summary>
		public Type Type;
		/// <summary>
		/// Function parameters.
		/// </summary>
		public ParameterCollection Parameters;

		/// <summary>
		/// IL method builder.
		/// </summary>
		public MethodBuilder Builder;

⌨️ 快捷键说明

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