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

namespace Bird
{
	public delegate bool OnVarCreatedFunc(Variable Var);

	public abstract class IdentifierScope : NonCodeScope
	{
		public IdentifierScope(IdContainer Parent, CodeString Code)
			: base(Parent, Code)
		{
		}

		public abstract Identifier Identifier { get; }
        /*
		public override bool IsAlreadyDefined(string Name, Predicate<Identifier> Func = null)
		{
			return Identifier.SearchScopesId(this, Name, Func).Count > 0;
		}
        */
		public override bool GetContainerId(string Name, List<IdentifierFound> Out, Predicate<Identifier> Func = null)
		{
			return Identifiers.SearchMember(this, Identifier, Name, Out, Func);
		}

		protected override string CalculateAssemblyName()
		{
			var Id = Identifier;
			if (!Id.Name.IsValid) return base.CalculateAssemblyName();
			else return base.CalculateAssemblyName() + "_" + Id.Name;
		}
	}

	public class NamespaceScope : IdentifierScope
	{
		public Namespace Namespace;
		public AutoAllocatedList<Namespace> UsedNamespaces;

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

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

		public NamespaceScope(IdContainer Parent, CodeString Code, Namespace Namespace)
			: base(Parent, Code)
		{
			this.Namespace = Namespace;
		}

		public override bool GetContainerId(string Name, List<IdentifierFound> Out, Predicate<Identifier> Func = null)
		{
			var RetValue = base.GetContainerId(Name, Out, Func);
			for (var i = 0; i < UsedNamespaces.Count; i++)
			{
				if (Identifiers.SearchMember(this, UsedNamespaces[i], Name, Out, Func))
					RetValue = true;
			}

			return RetValue;
		}
	}

	public abstract class ScopeNode : IdContainer
	{
		public CodeString Code;

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

		public int SourceLength
		{
			get { return Code.IsValid ? Code.Length : 0; }
		}
	}

	public abstract class NonCodeScope : ScopeNode
	{
		public virtual void GetMTProcIds(List<Identifier> Out)
		{
			for (var i = 0; i < IdentifierList.Count; i++)
			{
				var Id = IdentifierList[i];
				if (Id is Function || Id is IdentifierAlias || Id is Variable)
					Out.Add(Id);
			}
		}

		public virtual bool ProcessScope()
		{
			return State.Language.DeclarationRecognizer.Recognize(this);
		}

		public NonCodeScope(IdContainer Parent, CodeString Code)
			: base(Parent, Code)
		{
		}
	}

	public class AssemblyScope : NamespaceScope
	{
		public Assembly Assembly;

		public AssemblyScope(GlobalContainer Parent, Assembly Assembly, CodeFile CodeFile = null)
			: base(Parent, CodeFile == null ? new CodeString() : new CodeString(CodeFile), Parent.GlobalNamespace)
		{
			this.Assembly = Assembly;
		}

		public override void GetAssembly(CodeGenerator CG, GetAssemblyMode Mode = GetAssemblyMode.Code)
		{
			if (Assembly == GlobalContainer.OutputAssembly)
				base.GetAssembly(CG, Mode);
		}

		public override void GetGlobalPointers(List<string> Out)
		{
			if (Assembly == GlobalContainer.OutputAssembly)
				base.GetGlobalPointers(Out);
		}
	}

	public class PropertyScope : IdentifierScope
	{
		public Property Property;
		public int GetterIndex = -1;
		public int SetterIndex = -1;

		protected override string CalculateAssemblyName()
		{
			if (!Property.Name.IsValid) return base.CalculateAssemblyName() + "_%Indexer";
			else return base.CalculateAssemblyName();
		}

		public Function Getter
		{
			get
			{
				if (GetterIndex == -1) return null;
				return IdentifierList[GetterIndex] as Function;
			}

			set
			{
				if (value == null)
				{
					if (GetterIndex != -1)
					{
						IdentifierList.RemoveAt(GetterIndex);
						GetterIndex = -1;
					}

					return;
				}

				if (GetterIndex == -1)
				{
					GetterIndex = IdentifierList.Count;
					IdentifierList.Add(value);
					return;
				}

				IdentifierList[GetterIndex] = value;
			}
		}

		public Function Setter
		{
			get
			{
				if (SetterIndex == -1) return null;
				return IdentifierList[SetterIndex] as Function;
			}

			set
			{
				if (value == null)
				{
					if (SetterIndex != -1)
					{
						IdentifierList.RemoveAt(SetterIndex);
						SetterIndex = -1;
					}

					return;
				}

				if (SetterIndex == -1)
				{
					SetterIndex = IdentifierList.Count;
					IdentifierList.Add(value);
					return;
				}

				IdentifierList[SetterIndex] = value;
			}
		}

		public PropertyScope(IdContainer Parent, CodeString Code, Property Property)
			: base(Parent, Code)
		{
			this.Property = Property;
		}

		public override IdContainer RealContainer
		{
			get { return Parent.RealContainer; }
		}

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

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

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

		public override Function OnCreateFunction(CodeString Name, TypeOfFunction FuncType,
			FunctionOverloads Overload, List<Modifier> Mods = null)
		{
			throw new ApplicationException();
		}

		bool CreateScopeForAccessor(Function Func, CodeString Code)
		{
			if (Func.HasCode)
			{
				Func.FunctionScope = new FunctionScope(this, Func, Code);
				if (!Func.FunctionScope.Initialize()) return false;
				
				var Parameters = Func.FunctionScope.Parameters;
				for (var i = 0; i < Parameters.Count; i++)
					Parameters[i].SetUsed();
			}

			return true;
		}

		Function CreateFunctionForAccessor(string Name, CodeString Declaration, TypeOfFunction Type, IdentifierAccess Access)
		{
			if (Identifiers.IsLessRestrictive(Access, Property.Access))
			{
				State.Messages.Add(MessageId.PropertyAccessLevel, Name);
				return null;
			}

			Function Ret;
			if (!(Parent is StructuredScope) || (Property.Flags & IdentifierFlags.Static) != 0)
				Ret = new Function(this, new CodeString(Name), Type, null);
			else Ret = new MemberFunction(this, new CodeString(Name), Type, null);

			Ret.Flags = Property.Flags;
			Ret.Access = Access;
			Ret.Declaration = Declaration;
			return Ret;
		}

		public Function CreateGetter(CodeString Declaration, CodeString Code, IdentifierAccess Access = IdentifierAccess.Public)
		{
			var Children = new Identifier[Property.Children.Length];
			for (var i = 0; i < Property.Children.Length; i++)
				Children[i] = Property.Children[i];

			var Type = new TypeOfFunction(this, DefaultCallConv, Children);
			var Ret = CreateFunctionForAccessor("get", Declaration, Type, Access);
			if (Ret == null || !CreateScopeForAccessor(Ret, Code)) return null;

			Getter = Ret;
			return Ret;
		}

		public Function CreateSetter(CodeString Declaration, CodeString ValueName, CodeString Code,
			IdentifierAccess Access = IdentifierAccess.Public)
		{
			var PChildren = Property.Children.Length;
			var Children = new Identifier[PChildren + 1];
			Children[0] = GlobalContainer.CommonIds.Void;
			Children[PChildren] = new FunctionParameter(this, ValueName, Property.TypeOfSelf);

			for (var i = 1; i < PChildren; i++)
				Children[i] = Property.Children[i];

			var Type = new TypeOfFunction(this, DefaultCallConv, Children);
			var Ret = CreateFunctionForAccessor("set", Declaration, Type, Access);
			if (Ret == null || !CreateScopeForAccessor(Ret, Code)) return null;

			Setter = Ret;
			return Ret;
		}

		public override bool ProcessScope()
		{
			return true;
		}

		public bool AutoImplementGetter(Identifier Id)
		{
			var Getter = this.Getter;
			var FScope = Getter.FunctionScope;
			FScope.Flags |= FunctionScopeFlags.DisableParsing;
			
			var Plugin = FScope.GetPlugin();
			if (!Plugin.Begin()) return false;

			var Value = Expressions.CreateReference(FScope, Id, Plugin, Getter.Name);
			if (Value == null || Plugin.End(ref Value) == PluginResult.Failed) 
				return false;

			var Comm = new Command(FScope, Getter.Name, CommandType.Return);
			Comm.Expressions = new List<ExpressionNode>() { Value };
			Comm.Label = FScope.ReturnLabel;
			FScope.Children.Add(Comm);
			return true;
		}

		public bool AutoImplementSetter(Identifier Id)
		{
			var Setter = this.Setter;
			var FScope = Setter.FunctionScope;
			FScope.Flags |= FunctionScopeFlags.DisableParsing;

			var Plugin = FScope.GetPlugin();
			if (!Plugin.Begin()) return false;

			var Dst = Expressions.CreateReference(FScope, Id, Plugin, Getter.Name);
			var Value = Plugin.NewNode(new IdExpressionNode(FScope.Parameters[0], Setter.Name));
			if (Dst == null || Value == null) return false;

			var Expr = Expressions.SetValue(Dst, Value, Plugin, Setter.Name, true);
			if (Expr == null) return false;

			var Comm = new Command(FScope, Setter.Name, CommandType.Expression);
			Comm.Expressions = new List<ExpressionNode>() { Expr };
			FScope.Children.Add(Comm);
			return true;
		}

		public bool AutoImplement()
		{
			if (Property.Children.Length != 1)
				throw new ApplicationException();

			var Type = Property.Children[0];
			var Name = new CodeString(Property.Name.ToString() + "_%Value");

			Identifier Id;
			if ((Property.Flags & IdentifierFlags.Static) != 0)
			{
				Id = new GlobalVariable(StructuredScope, Name, Type);
				Id.Flags |= IdentifierFlags.Static;
			}
			else
			{
				Id = new MemberVariable(StructuredScope, Name, Type);
			}

			if (!StructuredScope.DeclareIdentifier(Id))
				return false;

			if (!AutoImplementGetter(Id)) return false;
			if (!AutoImplementSetter(Id)) return false;
			return true;
		}

		public bool ProcessAutoImplementation()
		{
			var Getter = this.Getter;
			var Setter = this.Setter;

			var GScope = Getter != null ? Getter.FunctionScope : null;
			var SScope = Setter != null ? Setter.FunctionScope : null;

			if ((Property.Flags & IdentifierFlags.Abstract) != 0 || (Property.Flags & IdentifierFlags.Extern) != 0)
				return true;

			if (Getter != null && Setter != null && !GScope.Code.IsValid && !SScope.Code.IsValid)
			{
				if (Property.Children.Length > 1)
				{
					State.Messages.Add(MessageId.UnimplementedWithIndices, Property.Declaration);
					return false;
				}

				return AutoImplement();
			}
			else
			{
				if (Getter != null && !GScope.Code.IsValid)
					State.Messages.Add(MessageId.EmptyScope, Getter.Declaration);

				if (Setter != null && !SScope.Code.IsValid)
					State.Messages.Add(MessageId.EmptyScope, Setter.Declaration);

				return true;
			}
		}

	}
}

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
Web04 | 2.8.141029.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