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.Threading;
using System.Threading.Tasks;

namespace Bird
{
	public abstract class TypeScope : IdentifierScope
	{
		public TypeScope(IdContainer Parent, CodeString Code)
			: base(Parent, Code)
		{
		}

		public override IdentifierAccess DefaultAccess
		{
			get { return IdentifierAccess.Private; }
		}
	}

	public class EnumScope : TypeScope
	{
		public EnumType EnumType;

		public override Identifier Identifier
		{
			get { return EnumType; }
		}

		public override IdentifierAccess DefaultAccess
		{
			get { return IdentifierAccess.Public; }
		}

		public EnumScope(IdContainer Parent, CodeString Code, EnumType Type)
			: base(Parent, Code)
		{
			this.EnumType = Type;
		}

		public override Variable OnCreateVariable(CodeString Name, Identifier Type, List<Modifier> Mods = null)
		{
			return new ConstVariable(this, Name, Type, null);
		}

		public override bool ProcessScope()
		{
			var TypeOfValues = EnumType.Children[0];
			if (TypeOfValues == null)
			{
				if (EnumType.Str_TypeOfValues.IsValid)
				{
					TypeOfValues = RecognizeIdentifier(EnumType.Str_TypeOfValues, GetIdOptions.DefaultForType);
					if (TypeOfValues == null) return false;

					if (!(TypeOfValues.RealId is NonFloatType))
					{
						State.Messages.Add(MessageId.EnumTypeError, EnumType.Str_TypeOfValues);
						return false;
					}

					if (Identifiers.IsLessAccessable(TypeOfValues, EnumType))
					{
						State.Messages.Add(MessageId.LessAccessable, EnumType.Name, TypeOfValues.Name.ToString());
						return false;
					}
				}
				else
				{
					TypeOfValues = GlobalContainer.CommonIds.Int32;
				}

				EnumType.Children[0] = TypeOfValues;
				EnumType.Update();
			}

			var LastValue = (ConstValue)null;
			for (var i = 0; i < IdentifierList.Count; i++)
			{
				var Const = IdentifierList[i] as ConstVariable;
				if (Const == null || Const.TypeOfSelf != EnumType)
					throw new ApplicationException();

				if (Const.ConstInitValue == null)
				{
					if (LastValue == null)
					{
						if (TypeOfValues is NonFloatType)
							Const.ConstInitValue = new IntegerValue(0);
						else throw new ApplicationException();
					}
					else
					{
						var NewValue = LastValue.Copy() as IntegerValue;
						Const.ConstInitValue = NewValue;
						NewValue.Value++;
					}
				}

				LastValue = Const.ConstInitValue;
				if (!LastValue.CheckBounds(State, TypeOfValues, Const.Name))
					return false;
			}

			return true;
		}

		public override PluginRoot GetPlugin()
		{
			var Plugin = base.GetPlugin();
			Plugin.GetPlugin<TypeMngrPlugin>().RetType = EnumType.TypeOfValues;
			Plugin.GetPlugin<IdRecognizerPlugin>().ConvertEnums = EnumType;
			return Plugin;
		}
	}

	public class StructuredScope : TypeScope
	{
		public StructuredType StructuredType;
		public FunctionOverloads ConstructorOverloads;

		public override Identifier Identifier
		{
			get { return StructuredType; }
		}

		public bool CheckCycleInStructs(Identifier Id)
		{
			var Member = Id as MemberVariable;
			if (Member == null) return true;

			var Type = Member.TypeOfSelf.RealId as StructuredType;
			if (Type == null) return true;

			if (Type == this.StructuredType)
			{
				State.Messages.Add(MessageId.CycleInStructured, Id.Name);
				return false;
			}

			var Members = Type.StructuredScope.IdentifierList;
			for (var i = 0; i < Members.Count; i++)
			{
				if (Members[i] is MemberVariable && !CheckCycleInStructs(Members[i]))
					return false;
			}

			return true;
		}

		public bool CheckCycleInStructs()
		{
			if (StructuredType is StructType)
			{
				for (var i = 0; i < IdentifierList.Count; i++)
					if (!CheckCycleInStructs(IdentifierList[i])) return false;
			}

			return true;
		}

		public StructuredScope(IdContainer Parent, CodeString Code, StructuredType Type)
			: base(Parent, Code)
		{
			this.StructuredType = Type;
		}
		
		public bool ProcessIdentifiers()
		{
			var Result = true;
			for (var i = 0; i < IdentifierList.Count; i++)
			{
				if (!ProcessId(IdentifierList[i]))
					Result = false;
			}

            if (!Result) return false;

            var Abstracts = new List<Identifier>();
            for (var i = 0; i < StructuredType.BaseStructures.Length; i++)
            {
                var Base = StructuredType.BaseStructures[i].Base;
                var StructuredBase = Base.UnderlyingStructureOrRealId as StructuredType;
                for (var j = 0; j < StructuredBase.StructuredScope.IdentifierList.Count; j++)
                {
                    var Id = StructuredBase.StructuredScope.IdentifierList[j];
                    if ((Id.Flags & IdentifierFlags.Abstract) != 0) Abstracts.Add(Id);
                }
            }

            for (var i = 0; i < StructuredType.StructuredScope.IdentifierList.Count; i++)
            {
                var Id = StructuredType.StructuredScope.IdentifierList[i];
                if ((Id.Flags & IdentifierFlags.Override) != 0)
                    Abstracts.Remove(Id.OverriddenId);
            }

            for (var i = 0; i < Abstracts.Count; i++)
            {
                var Name = Identifiers.GetFullName(Abstracts[i]);
                State.Messages.Add(MessageId.BaseIdNotImplemented, StructuredType.Name, Name);
            }

            if (Abstracts.Count > 0) Result = false;
			return Result;
		}

		bool ProcessId(Identifier Id)
		{
            if ((Id.Flags & IdentifierFlags.Override) != 0)
            {
                var OverriddenId = (Identifier)null;
                if (Id is MemberFunction)
                {
                    var MemberFunc = Id as MemberFunction;
					var List = Identifiers.SearchBaseMember(null, StructuredType, Id.Name.ToString(),
                        x =>
                        {
                            if (!(x is MemberFunction)) return false;
                            if (x.Access != MemberFunc.Access) return false;
                            return x.TypeOfSelf.IsEquivalent(Id.TypeOfSelf);
                        });

                    if (List.Count == 1)
                    {
                        OverriddenId = List[0].Identifier;
                        MemberFunc.OverriddenId = OverriddenId;
                    }
                    else if (List.Count != 0)
                    {
                        throw new ApplicationException();
                    }
                }
                else if (Id is Property)
                {
                    var Property = Id as Property;
                    var PropScope = Property.PropertyScope;

					var List = Identifiers.SearchBaseMember(null, StructuredType, Id.Name.ToString(), 
						x =>
						{
							var xProperty = x as Property;
							if (xProperty == null) return false;

							var xScope = xProperty.PropertyScope;
							var xGetter = xScope.Getter;
							var xSetter = xScope.Setter;

							if ((x.Flags & IdentifierFlags.Static) != 0) return false;
							if (x.Access != Property.Access) return false;
							if ((PropScope.Getter == null) != (xGetter == null)) return false;
							if ((PropScope.Setter == null) != (xSetter == null)) return false;

							if (PropScope.Getter != null)
							{
								if (PropScope.Getter.Access != xGetter.Access) return false;
								if (!PropScope.Getter.TypeOfSelf.IsEquivalent(xGetter.TypeOfSelf)) return false;
							}

							if (PropScope.Setter != null)
							{
								if (PropScope.Setter.Access != xSetter.Access) return false;
								if (!PropScope.Setter.TypeOfSelf.IsEquivalent(xSetter.TypeOfSelf)) return false;
							}

							return true;
						});

                    if (List.Count == 1)
                    {
                        var OverriddenProp = List[0].Identifier as Property;
                        var OverriddenScope = OverriddenProp.PropertyScope;
                        Property.OverriddenId = OverriddenProp;

                        if (PropScope.Getter != null)
                        {
                            var OverriddenFunc = OverriddenScope.Getter as MemberFunction;
                            var MemberGetter = PropScope.Getter as MemberFunction;
                            MemberGetter.OverriddenId = OverriddenFunc;
                        }

                        if (PropScope.Setter != null)
                        {
                            var OverriddenFunc = OverriddenScope.Setter as MemberFunction;
                            var MemberSetter = PropScope.Setter as MemberFunction;
                            MemberSetter.OverriddenId = OverriddenFunc;
                        }

                        OverriddenId = OverriddenProp;
                    }
                    else if (List.Count != 0)
                    {
                        throw new ApplicationException();
                    }
                }
                else
                {
                    throw new ApplicationException();
                }

                if (OverriddenId == null)
                {
                    State.Messages.Add(MessageId.NoOverridable, Id.Declaration);
                    return false;
                }
                else if ((OverriddenId.Flags & IdentifierFlags.Virtual) == 0)
                {
                    State.Messages.Add(MessageId.OverrideNonvirtual, Id.Declaration);
                    return false;
                }
                else if ((OverriddenId.Flags & IdentifierFlags.Sealed) != 0)
                {
                    State.Messages.Add(MessageId.OverrideSealed, Id.Declaration);
                    return false;
                }
            }
            else if (!(Id is Constructor || Id is Destructor) && Id.Name.IsValid)
            {
                var List = new List<IdentifierFound>();
                for (var i = 0; i < StructuredType.BaseStructures.Length; i++)
                {
                    var Base = StructuredType.BaseStructures[i].Base;
					List.AddRange(Identifiers.SearchMember(null, Base, Id.Name.ToString(), 
						x =>
						{
							if (x.DeclaredIdType == DeclaredIdType.Function &&
								Id.DeclaredIdType == DeclaredIdType.Function)
							{
								if (!Identifiers.AreParametersSame(x, Id))
									return false;
							}

							return x.Name.IsValid;
						}
					));
                }

                if (List.Count > 0)
                {
                    if ((Id.Flags & IdentifierFlags.HideBaseId) == 0)
                        State.Messages.Add(MessageId.HidingRequired, Id.Name);
                }
                else
                {
                    if ((Id.Flags & IdentifierFlags.HideBaseId) != 0)
                        State.Messages.Add(MessageId.HidingUnnecessary, Id.Name);
                }
            }

			return true;
		}

		public override bool CanIdDeclared(Identifier Id)
		{
			if (!(Id is Constructor) && Id.Name.IsEqual(StructuredType.Name))
			{
				State.Messages.Add(MessageId.CantDeclare, Id.Name);
				return false;
			}

			if ((Id is Variable || Id is Function) && !(Id is ConstVariable))
			{
				if ((Id.Flags & IdentifierFlags.Static) == 0 && (StructuredType.Flags & IdentifierFlags.Static) != 0)
				{
					State.Messages.Add(MessageId.NonStaticInStaticClass, Id.Declaration);
					return false;
				}
			}

			if (Id is Constructor && StructuredType is StructType)
			{
				var Ctor = Id as Constructor;
				var Type = Ctor.TypeOfSelf.RealId as TypeOfFunction;
				if (Type.Children.Length == 1)
				{
					State.Messages.Add(MessageId.StructParamLessCtor, Id.Declaration);
					return false;
				}
			}

			return base.CanIdDeclared(Id);
		}

		public Constructor CreateDeclaredConstructor(CodeString Declaration, FunctionParameter[] Params, List<Modifier> Mods = null)
		{
			if (ConstructorOverloads == null)
				ConstructorOverloads = new FunctionOverloads(null);

			var RetType = (Type)GlobalContainer.CommonIds.Void;
			if (StructuredType is ClassType)
			{
				if ((Mods == null || (Modifiers.GetFlags(Mods) & IdentifierFlags.Static) == 0) &&
					(StructuredType.Flags & (IdentifierFlags.Static | IdentifierFlags.Abstract)) == 0)
				{
					RetType = StructuredType;
				}
			}

			var FuncType = new TypeOfFunction(this, DefaultCallConv, RetType, Params);
			var Func = new Constructor(this, FuncType, ConstructorOverloads, Declaration);

			if (!AdjustAndDeclareFunction(Func, Mods)) return null;
			return Func;
		}

		public Constructor CreateDeclaredConstructorAndScope(CodeString Declaration, 
			FunctionParameter[] Params, CodeString Inner, List<Modifier> Mods = null)
		{
			var Func = CreateDeclaredConstructor(Declaration, Params, Mods);
			if (Func == null || CreateScopeForFunction(Func, Inner) == null) return null;
			return Func;
		}

		public bool ProcessConstructors()
		{
			var HasConstructor = StructuredType.HasNonstaticConstructor;

			if (!HasConstructor)
			{
				for (var i = 0; i < StructuredType.BaseStructures.Length; i++)
				{
					var Base = StructuredType.BaseStructures[i].Base;
					var SBase = Base.UnderlyingStructureOrRealId as StructuredType;

					if (!SBase.HasParameterLessCtor)
					{
						State.Messages.Add(MessageId.NoParamLessConstructor, 
							StructuredType.BaseStructures[i].Name);

						return false;
					}
				}
			}

			if (!HasConstructor && NeedsConstructor)
			{
				var Name = new CodeString(StructuredType.Name.ToString());
				var Func = CreateDeclaredConstructorAndScope(Name, null, new CodeString());
				if (Func == null) return false;

				Func.Access = IdentifierAccess.Public;
			}

			return true;
		}

		public bool ProcessBase()
		{
            var Type = StructuredType;
            for (var i = 0; i < Type.BaseStructures.Length; i++)
			{
                var BaseData = Type.BaseStructures[i];
                if ((Type.TypeFlags & TypeFlags.NoDefaultBase) != 0)
				{
					State.Messages.Add(MessageId.NobaseClassbase, BaseData.Name);
					return false;
				}

				BaseData.Base = Parent.RecognizeIdentifier(BaseData.Name, GetIdOptions.DefaultForType);
				if (BaseData.Base == null) return false;

				var RealBase = BaseData.Base.RealId;
                if (!Type.GetType().IsEquivalentTo(RealBase.GetType()))
				{
					State.Messages.Add(MessageId.CannotInherit, BaseData.Name);
					return false;
				}
				else if ((RealBase.Flags & IdentifierFlags.Static) != 0)
				{
					State.Messages.Add(MessageId.CannotInheritStatic, BaseData.Name);
					return false;
				}
				else if ((RealBase.Flags & IdentifierFlags.Sealed) != 0)
				{
					State.Messages.Add(MessageId.CannotInheritSealed, BaseData.Name);
					return false;
				}

                Type.BaseStructures[i] = BaseData;
			}

            if (Type.BaseStructures.Length == 0 &&  (Type.Flags & IdentifierFlags.Static) == 0 &&
                Type is ClassType && (Type.TypeFlags & TypeFlags.NoDefaultBase) == 0)
			{
				var NewBase = Identifiers.GetByFullNameFast<ClassType>(State, "System.Object");
                if (NewBase == null) return false;

                Type.BaseStructures = new StructureBase[1];
                Type.BaseStructures[0] = new StructureBase(NewBase);
			}

            for (var i = 0; i < Type.BaseStructures.Length; i++)
			{
                var BaseData = Type.BaseStructures[i];
                if (Identifiers.IsLessAccessable(BaseData.Base, Type))
				{
                    State.Messages.Add(MessageId.LessAccessable, Type.Name, BaseData.Name.ToString());
					return false;
				}
			}

            Type.Update();
			return true;
		}

		public override bool ProcessScope()
		{
			for (var i = 0; i < StructuredType.BaseStructures.Length; i++)
			{
				var BaseData = StructuredType.BaseStructures[i];
				if (Identifiers.IsSubtypeOf(BaseData.Base, StructuredType))
				{
					State.Messages.Add(MessageId.CycleInStructured, BaseData.Name);
					return false;
				}
			}

			if (!base.ProcessScope()) return false;
			if (!CheckCycleInStructs()) return false;
			if (!ProcessConstructors()) return false;
			return true;
		}

		bool NeedsConstructor
		{
			get
			{
				if (StructuredType is ClassType)
				{
					if ((StructuredType.Flags & IdentifierFlags.Abstract) == 0 &&
						(StructuredType.Flags & IdentifierFlags.Static) == 0)
					{
						return true;
					}
				}

				for (var i = 0; i < IdentifierList.Count; i++)
				{
					var V = IdentifierList[i] as MemberVariable;
					if (V != null && V.InitString.IsValid)
						return true;
				}

				return false;
			}
		}

		public override Variable OnCreateVariable(CodeString Name, Identifier Type, List<Modifier> Mods = null)
		{
			var Ret = CreateVariableHelper(Name, Type, Mods);
			if (Ret == null) Ret = new MemberVariable(this, Name, Type);
			return Ret;
		}

		public override Function OnCreateFunction(CodeString Name, TypeOfFunction FuncType,
			FunctionOverloads Overload, List<Modifier> Mods = null)
		{
			if (Mods != null && (Modifiers.GetFlags(Mods) & IdentifierFlags.Static) != 0)
				return new Function(this, Name, FuncType, Overload);
			else return new MemberFunction(this, Name, FuncType, Overload);
		}
	}
}

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
Web02 | 2.8.140827.1 | Last Updated 1 Jan 2013
Article Copyright 2011 by Dávid Kocsis
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid