Click here to Skip to main content
Click here to Skip to main content
Add your own
alternative version

Bird Programming Language: Part 1

, 1 Jan 2013
A new general purpose language that aims to be fast, high level and simple to use.
Bird-noexe.zip
Bird
Archives
crt2.o
crtbegin.o
crtend.o
libadvapi32.a
libcomctl32.a
libcomdlg32.a
libgcc.a
libgdi32.a
libglu32.a
libkernel32.a
libmingw32.a
libmingwex.a
libmoldname.a
libmsvcrt.a
libopengl32.a
libshell32.a
libstdc++.a
libuser32.a
libwinmm.a
libwsock32.a
Libraries
BirdCore
Array.bird
BigInteger.bird
BinaryRW.bird
BirdCore.a
BirdCore.blib
CategoryData.dat
Console.bird
Convert.bird
Debug.bird
Entry.bird
Environment.bird
Exception.bird
Float.bird
LEB128.bird
Math.bird
Memory.bird
Object.bird
Random.bird
Reflection.bird
StandardC.bird
Stream.bird
String.bird
ToLowerData.dat
ToUpperData.dat
Types.bird
ValueType.bird
Win32.bird
x86Helper.bird
BlitzMax
.bmx
Launcher.bmx.gui.release.win32.x86.o
appstub.release.win32.x86.a
bank.release.win32.x86.a
bankstream.release.win32.x86.a
blitz.release.win32.x86.a
BlitzMax.a
BlitzMax.bird
BlitzMax.blib
d3d7max2d.release.win32.x86.a
d3d9max2d.release.win32.x86.a
directx.release.win32.x86.a
dxgraphics.release.win32.x86.a
event.release.win32.x86.a
filesystem.release.win32.x86.a
font.release.win32.x86.a
glgraphics.release.win32.x86.a
glmax2d.release.win32.x86.a
graphics.release.win32.x86.a
hook.release.win32.x86.a
keycodes.release.win32.x86.a
Launcher.bmx
libpng.release.win32.x86.a
linkedlist.release.win32.x86.a
math.release.win32.x86.a
max2d.release.win32.x86.a
opengl.release.win32.x86.a
pixmap.release.win32.x86.a
pngloader.release.win32.x86.a
polledinput.release.win32.x86.a
standardio.release.win32.x86.a
stdc.release.win32.x86.a
stream.release.win32.x86.a
system.release.win32.x86.a
textstream.release.win32.x86.a
win32.release.win32.x86.a
zlib.release.win32.x86.a
PrgLinec.bmx
Samples
Circles
Circles.bird
CppMain.bird
Fire
Fire.bird
Higher Order Functions
C#
Higher Order Functions.v11.suo
Properties
Test.bird
msvcrt.lib
Reflection
Reflection.bird
Squares
Squares.bird
Template
Template.bird
Source
Base
Bird.v11.suo
Expressions
Identifiers
Languages
NativeCode
Properties
Recognizers
Expressions
Scopes
x86
Thumbs.db
Bird.zip
crt2.o
crtbegin.o
crtend.o
libadvapi32.a
libcomctl32.a
libcomdlg32.a
libgcc.a
libgdi32.a
libglu32.a
libkernel32.a
libmingw32.a
libmingwex.a
libmoldname.a
libmsvcrt.a
libopengl32.a
libshell32.a
libstdc++.a
libuser32.a
libwinmm.a
libwsock32.a
Binaries
ar.exe
Bird.exe
fasm.exe
ld.exe
libiconv-2.dll
libintl-8.dll
Array.bird
BigInteger.bird
BinaryRW.bird
BirdCore.a
BirdCore.blib
CategoryData.dat
Console.bird
Convert.bird
Debug.bird
Entry.bird
Environment.bird
Exception.bird
Float.bird
LEB128.bird
Math.bird
Memory.bird
Object.bird
Random.bird
Reflection.bird
StandardC.bird
Stream.bird
String.bird
ToLowerData.dat
ToUpperData.dat
Types.bird
ValueType.bird
Win32.bird
x86Helper.bird
Launcher.bmx.gui.release.win32.x86.o
appstub.release.win32.x86.a
bank.release.win32.x86.a
bankstream.release.win32.x86.a
blitz.release.win32.x86.a
BlitzMax.a
BlitzMax.bird
BlitzMax.blib
d3d7max2d.release.win32.x86.a
d3d9max2d.release.win32.x86.a
directx.release.win32.x86.a
dxgraphics.release.win32.x86.a
event.release.win32.x86.a
filesystem.release.win32.x86.a
font.release.win32.x86.a
glgraphics.release.win32.x86.a
glmax2d.release.win32.x86.a
graphics.release.win32.x86.a
hook.release.win32.x86.a
keycodes.release.win32.x86.a
Launcher.bmx
libpng.release.win32.x86.a
linkedlist.release.win32.x86.a
math.release.win32.x86.a
max2d.release.win32.x86.a
opengl.release.win32.x86.a
pixmap.release.win32.x86.a
pngloader.release.win32.x86.a
polledinput.release.win32.x86.a
standardio.release.win32.x86.a
stdc.release.win32.x86.a
stream.release.win32.x86.a
system.release.win32.x86.a
textstream.release.win32.x86.a
win32.release.win32.x86.a
zlib.release.win32.x86.a
PrgLinec.bmx
PrgLinec.exe
Circles.bird
CppMain.bird
Fire.bird
Higher Order Functions.v11.suo
Test.bird
msvcrt.lib
Reflection.bird
Squares.bird
Template.bird
Bird.v11.suo
Thumbs.db
examples.zip
Examples
As.exe
AsLibs.a
PerfTest
Launcher.bmx
Squares
.bmx
Launcher.bmx
Template
Launcher.bmx
source.zip
Anonymus 7_1
Anonymus.csproj.user
Anonymus.idc
Anonymus.pidb
Anonymus.suo
Anonymus.userprefs
Base
Expressions
Lib
Long
LongDivTest.bmx
ULongConv.bmx
Math
MISZ
AsLibs.a
PerfTest
Launcher.bmx
Squares
Launcher.bmx
Template
Launcher.bmx
PrgLinec.bmx
PrgLinec.exe
Properties
Scopes
Txt
Asm.docx
Code.docx
Lib.aslib
~$Code.docx
x86
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Numerics;

namespace Anonymus
{
	public enum TypeConversion
	{
		Automatic,
		Convertable,
		Nonconvertable,
	}

	public abstract class Type : Identifier
	{
		public int _Align = 0;
		public bool DeclaredType = false;

		public abstract int Size { get; }
		public abstract Type Copy(IdContainer Container, PString Name);
		public abstract bool IsEqual(Type Tp);

		public virtual bool CanCreateRefType { get { return true; } }
		public virtual bool CanCreatePtrType { get { return true; } }

		public virtual void Declare(Compiler Comp, ConstData Data)
		{
			throw new Exception("ERROR");
		}

		public Type(IdContainer Container, PString Name)
			: base(Container, Name)
		{
			this.Name = Name;
		}

		public virtual bool CanOpApplied(Operator Op, Type SrcType)
		{
			return Op == Operator.Assignment;
		}

		public virtual TypeConversion CanConvert(Type To)
		{
            if (IsEqual(To)) return TypeConversion.Automatic;
			return TypeConversion.Nonconvertable;
		}

		public virtual PointerType CreatePointerType(IdContainer Container = null, PString Name = null)
		{
			if (!RealType.CanCreatePtrType) return null;
			if (Name == null) Name = new PString(this.Name.String + "*");
			if (Container == null) Container = this.Container;

			var Ret = new PointerType(Container, Name, this);
			Ret.DeclaredType = false;
			return Ret;
		}

		public virtual RefType CreateRefType(IdContainer Container = null, PString Name = null)
		{
			if (!RealType.CanCreateRefType) return null;
			if (Name == null) Name = new PString("ref " + this.Name.String);
			if (Container == null) Container = this.Container;

			var Ret = new RefType(Container, Name, this);
			Ret.DeclaredType = false;
			return Ret;
		}

		public TypeDefType CreateTypeDefType(IdContainer Container, PString Name)
		{
			var Ret = new TypeDefType(Container, Name, this);
			Ret.DeclaredType = false;
			return Ret;
		}

		public virtual Type RealType
		{
			get { return this; }
		}
	}

	public class TypeOfType : Type
	{
		public TypeOfType(IdContainer Container, PString Name)
			: base(Container, Name)
		{
		}

		public override int Size
		{
			get { throw new NotImplementedException(); }
		}

		public override Type Copy(IdContainer Container, PString Name)
		{
			return new TypeOfType(Container, Name);
		}

		public override bool IsEqual(Type Tp)
		{
			return Tp.RealType is TypeOfType;
		}
	}

	public class AutomaticType : Type
	{
		public override bool CanCreatePtrType { get { return false; } }
		public override bool CanCreateRefType { get { return false; } }

		public AutomaticType(IdContainer Container, PString Name)
			: base(Container, Name)
		{
		}

		public override int Size
		{
			get { throw new NotImplementedException(); }
		}

		public override Type Copy(IdContainer Container, PString Name)
		{
			return new AutomaticType(Container, Name);
		}

		public override bool IsEqual(Type Tp)
		{
			return Tp.RealType is AutomaticType;
		}

		public override bool CanOpApplied(Operator Op, Type SrcType)
		{
			if (Op == Operator.Assignment) return true;
			return base.CanOpApplied(Op, SrcType);
		}

		public override TypeConversion CanConvert(Type To)
		{
			return TypeConversion.Automatic;
		}
	}

	public class PointerType : Type
	{
		public Type Child;

		public PointerType(IdContainer Container, PString Name, Type Child)
			: base(Container, Name)
		{
			this.Child = Child;
		}

		public override int Size
		{
			get { return Arch.RegSize; }
		}

		public override Type Copy(IdContainer Container, PString Name)
		{
			return new PointerType(Container, Name, Child);
		}

		public override bool CanOpApplied(Operator Op, Type SrcType)
		{
			if (SrcType != null) SrcType = SrcType.RealType;

			if (Op == Operator.Indirection) return true;
			if (Operators.IsRelEqualityOp(Op) && SrcType is PointerType) return true;

			if (!(Child is VoidType))
			{
				if ((Op == Operator.Add || Op == Operator.Subract) && SrcType is NonFltNumType) return true;
				if (Op == Operator.Index) return true;
			}

			return base.CanOpApplied(Op, SrcType);
		}

		public override void SetUsed()
		{
			Child.SetUsed();
			base.SetUsed();
		}

		public override bool IsEqual(Type Tp)
		{
			var PType = Tp.RealType as PointerType;
			return PType != null && Child.IsEqual(PType.Child);
		}

		public override TypeConversion CanConvert(Type To)
		{
			var PTo = To as PointerType;
			if (PTo != null)
			{
				if (PTo.Child is VoidType)
					return TypeConversion.Automatic;
				else return TypeConversion.Convertable;
			}

			return base.CanConvert(To);
		}

		public override void Declare(Compiler Comp, ConstData Data)
		{
			Comp.Declare(this, Data);
		}
	}

	public class ConstStringType : Type
	{
		public override bool CanCreatePtrType { get { return false; } }
		public override bool CanCreateRefType { get { return false; } }

		public ConstStringType(IdContainer Container, PString Name)
			: base(Container, Name)
		{
		}

		public override int Size
		{
			get { throw new Exception("ERROR"); }
		}

		public override void GetAsmCode(Compiler Comp, GetAsmCodeMode Mode)
		{
			throw new Exception("ERROR");
		}

		public override Type Copy(IdContainer Container, PString Name)
		{
			return new ConstStringType(this.Container, Name);
		}

		public override bool CanOpApplied(Operator Op, Type SrcType)
		{
			if (SrcType != null) SrcType = SrcType.RealType;
			return base.CanOpApplied(Op, SrcType) || ((Op == Operator.Add ||
				Op == Operator.Equal || Op == Operator.Nonequal) && SrcType is ConstStringType);
		}

		public override bool IsEqual(Type Tp)
		{
			return Tp.RealType is ConstStringType;
		}
	}

	public class VoidType : Type
	{
		public override bool CanCreateRefType { get { return false; } }

		public VoidType(IdContainer Container, PString Name)
			: base(Container, Name)
		{
		}

		public override Type Copy(IdContainer Container, PString Name)
		{
			return new VoidType(Container, Name);
		}

		public override int Size
		{
			get { return 0; }
		}

		public override bool IsEqual(Type Tp)
		{
			return Tp.RealType is VoidType;
		}

		public override TypeConversion CanConvert(Type To)
		{
			return TypeConversion.Nonconvertable;
		}

		public override bool CanOpApplied(Operator Op, Type SrcType)
		{
			return false;
		}
	}

	public abstract class NumberType : Type
	{
		public int _Size;
		public string Code;

		public NumberType(IdContainer Container, PString Name, int Size, string Code)
			: base(Container, Name)
		{
			this._Size = Size;
			this.Code = Code;
		}

		public override int Size
		{
			get { return _Size; }
		}

		public override bool CanOpApplied(Operator Op, Type SrcType)
		{
			if (SrcType != null) if (SrcType != null) SrcType = SrcType.RealType;

			if (Operators.IsMathsOp(Op) && Op != Operator.Abs && (SrcType == null || SrcType is NumberType)) return true;
			if ((Operators.IsRelEqualityOp(Op) || Operators.IsArithmeticalOp(Op)) && SrcType is NumberType) return true;
			if (base.CanOpApplied(Op, SrcType)) return true;
			return false;
		}

		public override bool IsEqual(Type Tp)
		{
			var NType = Tp.RealType as NumberType;
			if (NType == null || !GetType().IsEquivalentTo(NType.GetType()))
				return false;

			return _Size == NType._Size;
		}

		public override TypeConversion CanConvert(Type To)
		{
			var NTo = To.RealType as NumberType;
			if (NTo != null) return TypeConversion.Convertable;
			return base.CanConvert(To);
		}

		public override void Declare(Compiler Comp, ConstData Data)
		{
			Comp.Declare(this, Data);
		}
	}

	public abstract class NonFltNumType : NumberType
	{
		public BigInteger MinValue;
		public BigInteger MaxValue;

		public NonFltNumType(IdContainer Container, PString Name, int Size, string Code)
			: base(Container, Name, Size, Code)
		{
		}

		public override bool CanOpApplied(Operator Op, Type SrcType)
		{
			if (SrcType != null) SrcType = SrcType.RealType;

			if ((Op == Operator.Add || Op == Operator.Subract) && SrcType is PointerType &&
				!((SrcType as PointerType).Child is VoidType)) return true;

			if (base.CanOpApplied(Op, SrcType) || Op == Operator.Complement) return true;
			if ((Operators.IsShiftOp(Op) || Operators.IsBitwiseOp(Op)) && SrcType is NonFltNumType) return true;
			return false;
		}

		public override TypeConversion CanConvert(Type To)
		{
			var FTo = To as FloatType;
			if (FTo != null) return TypeConversion.Automatic;
			return base.CanConvert(To);
		}
	}

	public class SignedType : NonFltNumType
	{
		public SignedType(IdContainer Container, PString Name, int Size, string Code)
			: base(Container, Name, Size, Code)
		{
			MaxValue = Helper.Pow(new BigInteger(2), new BigInteger(Size * 8 - 1)) - 1;
			MinValue = -Helper.Pow(new BigInteger(2), new BigInteger(Size * 8 - 1));
		}

		public override Type Copy(IdContainer Container, PString Name)
		{
			return new SignedType(Container, Name, Size, null);
		}

		public override bool CanOpApplied(Operator Op, Type SrcType)
		{
			return Op == Operator.Neg || Op == Operator.Abs || base.CanOpApplied(Op, SrcType);
		}

		public override TypeConversion CanConvert(Type To)
		{
			var STo = To.RealType as SignedType;
			if (STo != null && STo.Size >= Size)
				return TypeConversion.Automatic;

			return base.CanConvert(To);
		}
	}

	public class UnsignedType : NonFltNumType
	{
		public UnsignedType(IdContainer Container, PString Name, int Size, string Code)
			: base(Container, Name, Size, Code)
		{
			MaxValue = Helper.Pow(new BigInteger(2), new BigInteger(Size * 8)) - 1;
			MinValue = new BigInteger(0);
		}

		public override Type Copy(IdContainer Container, PString Name)
		{
			return new UnsignedType(Container, Name, Size, null);
		}

		public override TypeConversion CanConvert(Type To)
		{
			var UTo = To as SignedType;
			if (UTo != null && UTo.Size >= Size)
				return TypeConversion.Automatic;

			return base.CanConvert(To);
		}
	}

	public class FloatType : NumberType
	{
		public FloatType(IdContainer Container, PString Name, int Size, string Code)
			: base(Container, Name, Size, Code)
		{
		}

		public override Type Copy(IdContainer Container, PString Name)
		{
			return new FloatType(Container, Name, Size, null);
		}

		public override bool CanOpApplied(Operator Op, Type SrcType)
		{
			if (Op == Operator.Modolus) return false;
			return base.CanOpApplied(Op, SrcType) || Op == Operator.Neg;
		}

		public override TypeConversion CanConvert(Type To)
		{
			var FTo = To.RealType as FloatType;
			if (FTo != null && FTo.Size >= Size)
				return TypeConversion.Automatic;

			return base.CanConvert(To);
		}
	}

	public class BoolType : Type
	{
		public int _Size;
		public ulong TrueVal;
		public ulong FalseVal;

		public ConstData TrueData;
		public ConstData FalseData;

		public BoolType(IdContainer Container, PString Name, int Size, ulong TrueVal, ulong FalseVal)
			: base(Container, Name)
		{
			this._Size = Size;
			this.TrueVal = TrueVal;
			this.FalseVal = FalseVal;

			TrueData = new ConstData(TrueVal);
			FalseData = new ConstData(FalseVal);
		}

		public override Type Copy(IdContainer Container, PString Name)
		{
			return new BoolType(Container, Name, _Size, TrueVal, FalseVal);
		}

		public override int Size
		{
			get { return _Size; }
		}

		public override bool CanOpApplied(Operator Op, Type SrcType)
		{
			if (SrcType != null) SrcType = SrcType.RealType;
			if (Op == Operator.Not) return true;
			if (SrcType is BoolType && (Operators.IsLogicalOp(Op) || Op == Operator.Equal || Op == Operator.Nonequal))
				return true;

			return base.CanOpApplied(Op, SrcType);
		}

		public override bool IsEqual(Type Tp)
		{
			var BType = Tp.RealType as BoolType;
			return BType != null && _Size == BType._Size &&
				TrueVal == BType.TrueVal && FalseVal == BType.FalseVal;
		}

		public override void Declare(Compiler Comp, ConstData Data)
		{
			Comp.Declare(this, Data);
		}
	}

	public class EnumValue
	{
		public string Name;
		public ConstExpressionNode Value;

		public EnumValue(string Name, ConstExpressionNode Value)
		{
			this.Name = Name;
			this.Value = Value;
		}

		public ExpressionNode GetNode(CompilerState State, PString Code, ExprPlugIn PlugIn = null, Type Type = null)
		{
			var Node = Value.Copy(State, Code, x => x);
			if (Node == null) return null;
			if (Type != null) Node.Type = Type;
			if (PlugIn != null) Node = PlugIn.NewNode(Node);
			return Node;
		}
	}

	public class FlagType : EnumType
	{
		public FlagType(IdContainer Container, PString Name, Type TypeOfValues, EnumValue[] Values)
			: base(Container, Name, TypeOfValues, Values)
		{
		}

		public override bool CanOpApplied(Operator Op, Type SrcType)
		{
			return base.CanOpApplied(Op, SrcType) || Operators.IsBitwiseOp(Op);
		}
	}

	public class EnumType : Type
	{
		public Type TypeOfValues;
		public EnumValue[] Values;

		public EnumType(IdContainer Container, PString Name, Type TypeOfValues, EnumValue[] Values)
			: base(Container, Name)
		{
			this.TypeOfValues = TypeOfValues;
			this.Values = Values;
		}

		public EnumValue GetValue(String Name)
		{
			foreach (var e in Values)
				if (e.Name == Name) return e;

			return null;
		}

		public EnumValue GetValue(ulong Value)
		{
			foreach (var e in Values)
				if (e.Value.Unsigned == Value) return e;

			return null;
		}

		public override Type Copy(IdContainer Container, PString Name)
		{
			return new EnumType(Container, Name, TypeOfValues, Values);
		}

		public override int Size
		{
			get { return TypeOfValues.Size; }
		}

		public override bool CanOpApplied(Operator Op, Type SrcType)
		{
			return base.CanOpApplied(Op, SrcType) ||
				Op == Operator.Equal || Op == Operator.Nonequal;
		}

		public override bool IsEqual(Type Tp)
		{
			return Tp.RealType == this;
		}

		public override TypeConversion CanConvert(Type To)
		{
			return TypeOfValues.CanConvert(To);
		}

		public override void Declare(Compiler Comp, ConstData Data)
		{
			Comp.Declare(this, Data);
		}
	}

	public abstract class StructuredType : Type
	{
		public StructuredType Base;
		public PString Str_Base;

        public IdentifierList Members;
        public int Pow2Size;

        public bool Calced = false;
		public int VarSize = 0;
		public int FuncSize = 0;

		public StructuredType(IdContainer Container, PString Name)
			: base(Container, Name)
		{
		}

        public void CalcPositions()
        {
            if (Calced|| !DeclInThis) return;
            var State = Container.State;
            VarSize = 0;
            FuncSize = 0;

            if (Base != null)
            {
                Base.CalcPositions();
                VarSize = Base.VarSize;
                FuncSize = Base.FuncSize;
            }

            foreach (var Id in Members)
                if (Id is MemberFunction && !Id.IsStatic)
                {
                    var MemFunc = Id as MemberFunction;
                    MemFunc.Offset = Helper.AdjustVarPos(FuncSize, MemFunc.Type);

                    var Size = MemFunc.Type.Size;
                    if (FuncSize == 0) VarSize += Size;
                    FuncSize = MemFunc.Offset + Size;
                }

            foreach (var Id in Members)
                if (Id is MemberVariable && !Id.IsStatic)
                {
                    var MemVar = Id as MemberVariable;
                    MemVar.Offset = Helper.AdjustVarPos(VarSize, MemVar.Type);
                    VarSize = MemVar.Offset + MemVar.Type.Size;
                }

            Pow2Size = VarSize;
            var M = State.Arch.MaxStructPow2Size;

            if (VarSize < M) Pow2Size = (int)Helper.Pow2((uint)VarSize);
            else if (VarSize % M != 0) Pow2Size = VarSize + (M - VarSize % M);
        }

		public ConstructorFunction Constructor
		{
			get
			{
				var Ret = Members.GetIdRec<ConstructorFunction>(null);
				if (Ret != null) return Ret;
				else if (Base != null) return Base.Constructor;
				else return null;
			}
		}

        public bool IsSubclassOf(StructuredType Type)
        {
            if (Base == Type) return true;
            else if (Base != null) return Base.IsSubclassOf(Type);
            else return false;
        }

		public override bool IsEqual(Type Tp)
		{
			return Tp.RealType == this;
		}

		public override bool CanOpApplied(Operator Op, Type SrcType)
		{
			if (Op == Operator.Member) return true;
			if (Op == Operator.New) return true;
			return base.CanOpApplied(Op, SrcType);
		}

		public virtual T GetId<T>(string Name) where T : Identifier
		{
			var Ret = Members.GetIdRec<T>(Name);
			if (Ret != null) return Ret;
			else if (Base != null) return Base.GetId<T>(Name);
			else return Ret;
		}

		public void DeclareFuncPtrs(Compiler Comp)
		{
			if (Base != null) Base.DeclareFuncPtrs(Comp);

			foreach (var e in Members)
				if (e is MemberFunction)
					Comp.DeclareLabelPtr(e.AsmName);
		}

		public void DeclareInitValues(Compiler Comp)
		{
			if (Base != null) Base.DeclareInitValues(Comp);

			foreach (var e in Members)
				if (e is MemberVariable)
				{
					var Var = e as MemberVariable;
					if (Var.Type is ValueType)
					{
						var Structured = Var.Type as StructuredType;
						Structured.DeclareInitValues(Comp);
					}
					else
					{
						var InitVal = Var.InitValue;
						var Const = InitVal as ConstExpressionNode;

						if (Const == null) Comp.Declare(Var.Type, null);
						else Comp.Declare(Const.Type, Const.Value);
					}
				}

			var C = Pow2Size - VarSize;
			if (C > 0) Comp.DeclareUnknownBytes(C);
		}

		public override void GetAsmCode(Compiler Comp, GetAsmCodeMode Mode)
		{
			foreach (var e in Members)
				e.GetAsmCode(Comp, Mode);

			if (Mode == GetAsmCodeMode.Data && !IsStatic)
			{
				if (FuncSize > 0)
				{
					var FuncPtr = Comp.State.NextLabelIndex;
					Comp.Label(FuncPtr, false);
					DeclareFuncPtrs(Comp);
					Comp.Append("\n");

					Comp.Label(AsmName + "_%InitVal");
					Comp.DeclareLabelPtr(FuncPtr);
				}
				else
				{
					Comp.Label(AsmName + "_%InitVal");
				}

				DeclareInitValues(Comp);
				Comp.Append("\n");
			}
		}
	}

	public class ClassType : StructuredType
	{
		public ClassType(IdContainer Container, PString Name)
			: base(Container, Name)
		{
		}

		public override Type Copy(IdContainer Container, PString Name)
		{
			var Ret = new ClassType(Container, Name);
			Ret.Members = Members;
			return Ret;
		}

		public override int Size
		{
			get { return Arch.RegSize; }
		}

		public override void Declare(Compiler Comp, ConstData Data)
		{
			Comp.Declare(this, Data);
		}

        public override TypeConversion CanConvert(Type To)
        {
            var Structured = To.RealType as StructuredType;
            if (Structured != null && IsSubclassOf(Structured))
                return TypeConversion.Automatic;

            return base.CanConvert(To);
        }
	}

	public class ValueType : StructuredType
	{
		public ValueType(IdContainer Container, PString Name)
			: base(Container, Name)
		{
		}

		public override Type Copy(IdContainer Container, PString Name)
		{
			var Ret = new ValueType(Container, Name);
			Ret.Members = Members;
			return Ret;
		}

		public override int Size
		{
			get { return Pow2Size; }
		}

		public override void Declare(Compiler Comp, ConstData Data)
		{
			if (Data != null) throw new Exception("ERROR");

			foreach (var e in Members)
				if (e is MemberVariable)
				{
					var Var = e as MemberVariable;
					Var.Type.Declare(Comp, null);
				}

			var C = Pow2Size - VarSize;
			if (C > 0) Comp.DeclareUnknownBytes(C);
		}

	}

    public class TupleType : ValueType
    {
		public bool Named = false;

        public TupleType(IdContainer Container, PString Name)
			: base(Container, Name)
		{
		}

		public TupleType(IdContainer Container, IdentifierList Members, bool Calc = true)
			: base(Container, null)
		{
			this.Members = Members;
			CalcName();
			if (Calc) CalcPositions();
		}

		public void CalcName()
		{
			var Name = ""; // "{"
			for (var i = 0; i < Members.Count; i++)
			{
				Name += (Members[i] as MemberVariable).Type.Name.String;
				if (i < Members.Count - 1) Name += ", ";
				//else Name += "}";
			}

			this.Name = new PString(Name);
		}

        public override T GetId<T>(string Name)
        {
			if (Name.IsNum())
			{
				var i = Convert.ToInt32(Name);
				if (i >= Members.Count || i < 0) return null;
				else return Members[i] as T;
			}

            return base.GetId<T>(Name);
        }

        public override bool IsEqual(Type Tp)
        {
            var V = Tp.RealType as TupleType;
            if (V != null)
            {
                if (V.Members.Count != Members.Count) return false;
                for (var i = 0; i < Members.Count; i++)
				{
					var Var1 = V.Members[i] as MemberVariable;
					var Var2 = Members[i] as MemberVariable;
					if (Var1 == null || Var2 == null) return false;

                    if (!Var2.Type.IsEqual(Var1.Type)) return false;
                }

                return true;
            }

            return base.IsEqual(Tp);
        }

        public override TypeConversion CanConvert(Type To)
        {
            var V = To.RealType as TupleType;
            if (V != null)
            {
                if (V.Members.Count != Members.Count)
                    return TypeConversion.Nonconvertable;

                var Automatic = true;
                for (var i = 0; i < Members.Count; i++)
				{
					var Var1 = V.Members[i] as MemberVariable;
					var Var2 = Members[i] as MemberVariable;
					if (Var1 == null || Var2 == null) return TypeConversion.Nonconvertable;

                    var Res = Var2.Type.CanConvert(Var1.Type);
                    if (Res == TypeConversion.Convertable) Automatic = false;
                    else if (Res == TypeConversion.Nonconvertable) return Res;
                }

                if (Automatic) return TypeConversion.Automatic;
                else return TypeConversion.Convertable;
            }

            return base.CanConvert(To);
        }
		
		public override bool CanOpApplied(Operator Op, Type SrcType)
		{
            var V = SrcType.RealType as TupleType;
            if (V != null && Operators.IsBitArithmOp(Op))
            {
				if (V.Members.Count != Members.Count) return false;
				for (var i = 0; i < Members.Count; i++)
				{
					var Var1 = V.Members[i] as MemberVariable;
					var Var2 = Members[i] as MemberVariable;
					if (Var1 == null || Var2 == null) return false;

					if (!Var2.Type.CanOpApplied(Op, Var1.Type))
						return false;
				}

				return true;
			}

			if (Op == Operator.Neg || Op == Operator.Complement)
			{
				for (var i = 0; i < Members.Count; i++)
				{
					var Var = Members[i] as MemberVariable;
					if (Var == null || !Var.Type.CanOpApplied(Op, null))
						return false;
				}

				return true;
			}

			return base.CanOpApplied(Op, SrcType);
		}

    }

	public class RefType : Type
	{
		public Type Child;

		public override bool CanCreatePtrType { get { return false; } }
		public override bool CanCreateRefType { get { return false; } }

		public RefType(IdContainer Container, PString Name, Type Child)
			: base(Container, Name)
		{
			this.Child = Child;
		}

		public override Type Copy(IdContainer Container, PString Name)
		{
			return new RefType(Container, Name, Child);
		}

        public override int Size
        {
            get { return Arch.RegSize; }
        }

		public override void SetUsed()
		{
			Child.SetUsed();
			base.SetUsed();
		}

		public override bool IsEqual(Type Tp)
		{
			var RType = Tp.RealType as RefType;
			if (RType == null) return false;
			else return Child.IsEqual(RType.Child);
		}
	}

	public class FunctionType : Type
	{
		public CallConv Conv;
		public Type RetType;
		public VarDeclList Params;

		public FunctionType(IdContainer Container, PString Name, CallConv Conv, Type RetType, VarDeclList Params)
			: base(Container, Name)
		{
			this.Conv = Conv;
			this.RetType = RetType;
			this.Params = Params;
		}

		public override Type Copy(IdContainer Container, PString Name)
		{
			return new FunctionType(Container, Name, Conv, RetType, Params);
		}

		public override int Size
		{
			get { return Arch.RegSize; }
		}

		public override bool IsEqual(Type Tp)
		{
			var FType = Tp.RealType as FunctionType;
			if (FType == null) return false;

			if (Conv != FType.Conv) return false;
			if (!RetType.IsEqual(FType.RetType)) return false;

			var C = Params.Count;
			if (C != FType.Params.Count) return false;

			for (var i = 0; i < C; i++)
				if (Params[i].Type.IsEqual(FType.Params[i].Type))
					return false;

			return true;
		}

		public override void SetUsed()
		{
			base.SetUsed();
			RetType.SetUsed();

			foreach (var e in Params)
				e.Type.SetUsed();
		}

		public override bool CanOpApplied(Operator Op, Type SrcType)
		{
			if (Op == Operator.Call) return true;
			return base.CanOpApplied(Op, SrcType);
		}
	}

    public abstract class ChildType : Type
    {
        public Type Child;

        public ChildType(IdContainer Container, PString Name, Type Child)
            : base(Container, Name)
        {
            this.Child = Child;
        }

        public override int Size
        {
            get { return Child.Size; }
        }

        public override Type RealType
        {
            get { return Child.RealType; }
        }

        public override TypeConversion CanConvert(Type To)
        {
            return Child.CanConvert(To);
        }

        public override bool IsEqual(Type Tp)
        {
            return Child.IsEqual(Tp);
        }

        public override void SetUsed()
        {
            Child.SetUsed();
            base.SetUsed();
        }
    }

    public class TypeDefType : ChildType
	{
		public TypeDefType(IdContainer Container, PString Name, Type Child)
			: base(Container, Name, Child)
		{
			this.Child = Child;
		}

		public override Type Copy(IdContainer Container, PString Name)
		{
			return new TypeDefType(Container, Name, Child);
		}
	}

    public class NamedFuncRetType : ChildType
    {
        public NamedFuncRetType(IdContainer Container, PString Name, Type Child)
			: base(Container, Name, Child)
		{
			this.Child = Child;
		}

		public override Type Copy(IdContainer Container, PString Name)
		{
			return new TypeDefType(Container, Name, Child);
		}
    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The GNU General Public License (GPLv3)

Share

About the Author

Dávid Kocsis
Student
Hungary Hungary
I've been programming for 8 years. My first big project was a remake of a nice bomberman game called Dyna Blaster. When i was little i played a lot with it. Now i'm working on a new programming language and code generator.
I would like to work with someone, so feel free to contact me about it.

| Advertise | Privacy | Mobile
Web03 | 2.8.140821.2 | Last Updated 1 Jan 2013
Article Copyright 2011 by Dávid Kocsis
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid