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

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.Text;
using System.Linq;

namespace Bird
{
	public class WithScopeNode : CodeScopeNode
	{
		public Dictionary<Identifier, PackedMemberId> Ids = new Dictionary<Identifier, PackedMemberId>();
		public ExpressionNode WithNode;

		public WithScopeNode(IdContainer Parent, CodeString Code, ExpressionNode Node)
			: base(Parent, Code)
		{
			WithNode = Node;
		}

		public override bool GetContainerId(string Name, List<IdentifierFound> Out, Predicate<Identifier> Func = null)
		{
			var RetValue = base.GetContainerId(Name, Out, Func);
			var Type = WithNode.Type as StructuredType;

			foreach (var e in Identifiers.SearchMember(this, Type, Name, Func))
			{
				PackedMemberId Id;
				if (!Ids.TryGetValue(e.Identifier, out Id))
				{
					Id = new PackedMemberId(this, new CodeString(),
						e.Identifier.TypeOfSelf, WithNode, e.Identifier);

					Ids.Add(e.Identifier, Id);
				}

				if (Func(Id))
				{
					Out.Add(new IdentifierFound(this, Id));
					RetValue = true;
				}
			}

			return RetValue;
		}
	}
	
	public class CodeScopeNode : ScopeNode
	{
		public bool DisableLastChild;
		public IFinishableCommRecognizer LastCommRecognizer;
		public IdContainer LastAddedContainer;
		public CheckingMode CheckingMode;

		public override bool IsAlreadyDefined(string Name, Predicate<Identifier> Func = null)
		{
			Predicate<IdContainer> ContainerFunc = x =>
				Identifiers.Search(x, x.IdentifierList, Name, Func).Count == 0;

			return !TrueForAllParent<IdContainer>(ContainerFunc, FunctionScope.Parent);
		}

		public bool AddFinishableCommand(IdContainer Container, IFinishableCommRecognizer Recognizer)
		{
			if (!FinishLastCommand())
				return false;

			LastAddedContainer = Container;
			LastCommRecognizer = Recognizer;

			DisableLastChild = false;
			Children.Add(Container);
			return true;
		}

		public bool AddCommand(IdContainer Container)
		{
			if (!FinishLastCommand())
				return false;

			DisableLastChild = false;
			Children.Add(Container);
			return true;
		}

		public bool FinishLastCommand()
		{
			if (LastCommRecognizer != null)
			{
				LastCommRecognizer.Finish(LastAddedContainer);
				MarkFinished();
			}

			return true;
		}

		public void MarkFinished()
		{
			LastAddedContainer = null;
			LastCommRecognizer = null;
		}

		public bool BasicConstructorCommands()
		{
			if (FunctionScope.ConstructorCall != null)
			{
				Children.Add(new Command(this, FunctionScope.ConstructorCall.Code, CommandType.Expression)
				{
					Expressions = new List<ExpressionNode>() { FunctionScope.ConstructorCall },
				});
			}

			return AssignInitValues();
		}

		public bool AssignInitValues()
		{
			var Plugin = GetPlugin();
			var RetValue = true;
			var Scope = FunctionScope.Parent;

			for (var i = 0; i < Scope.IdentifierList.Count; i++)
			{
				var Var = Scope.IdentifierList[i] as MemberVariable;
				if (Var != null)
				{
					if (!Var.InitString.IsValid) continue;

					Plugin.Reset();
					if (!Var.CalcValue(Plugin, BeginEndMode.Both, true))
						RetValue = false;

					Children.Add(new Command(this, Var.InitString, CommandType.Expression)
					{
						Expressions = new List<ExpressionNode>() { Var.InitValue }
					});
				}
			}

			return RetValue;
		}

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

		public override PluginRoot GetPlugin()
		{
			return new PluginForCodeScope(this);
		}

		public CodeScopeNode(IdContainer Parent, CodeString Code)
			: base(Parent, Code)
		{
			var CodeScopeParent = GetParent<CodeScopeNode>(FunctionScope);
			if (CodeScopeParent != null) CheckingMode = CodeScopeParent.CheckingMode;
		}

		public bool Return(ExpressionNode Node, CodeString Code)
		{
			var FS = FunctionScope;
			var VoidType = GlobalContainer.CommonIds.Void;
			var RetType = FS.Type.RetType;

			if (Node != null)
			{
				var Func = FunctionScope.Function;
				if (Func is Constructor || Func is Destructor)
				{
					State.Messages.Add(MessageId.CannotReturn, Code);
					return false;
				}

				if (RetType.RealId is VoidType)
				{
					var TypeStrs = new[] { Node.Type.Name.ToString(), RetType.Name.ToString() };
					State.Messages.Add(MessageId.CannotConvert, Code, TypeStrs);
					return false;
				}

				Children.Add(new Command(this, Code, CommandType.Return)
				{
					Expressions = new List<ExpressionNode>() { Node },
					Label = FS.ReturnLabel,
				});
			}
			else
			{
				if (FS.NeedsReturnVal)
				{
					var TypeStrs = new[] { VoidType.Name.ToString(), RetType.Name.ToString() };
					State.Messages.Add(MessageId.CannotConvert, Code, TypeStrs);
					return false;
				}

				if (!CopyRetVal(Code)) 
					return false;

				Children.Add(new Command(this, Code, CommandType.Return)
				{
					Label = FS.ReturnLabel,
				});
			}

			return true;
		}

		public IdContainer LastChild
		{
			get
			{
				var CommCount = Children.Count;
				if (CommCount > 0 && !DisableLastChild)
					return Children[CommCount - 1];
				else return null;
			}
		}

		public bool RecognizeCommand(CodeString Code)
		{
			var Recs = State.Language.CommRecognizers;
            for (var i = 0; i < Recs.Length; i++)
			{
				var Res = Recs[i].Recognize(this, Code);
				if (Res != SimpleRecResult.Unknown)
					return Res != SimpleRecResult.Failed;
			}

			State.Messages.Add(MessageId.NotExpected, Code);
			return false;
		}

		public bool CopyRetVal(CodeString Code, Identifier Id)
		{
			var Plugin = GetPlugin();
			if (!Plugin.Begin()) return false;

			var Node = Plugin.NewNode(new IdExpressionNode(Id, Code));
			if (Node == null || Plugin.End(ref Node) == PluginResult.Failed) 
				return false;

			Children.Add(new Command(this, Code, CommandType.Return)
			{
				Expressions = new List<ExpressionNode>() { Node },
				Label = FunctionScope.ReturnLabel,
			});

			return true;
		}

		public bool CopyRetVal(CodeString Code, ExpressionNode SrcVar)
		{
			var Plugin = GetPlugin();
			var Node = SrcVar.Copy(Plugin, Code: Code);
			if (Node == null) return false;

			Children.Add(new Command(this, Code, CommandType.Return)
			{
				Expressions = new List<ExpressionNode>() { Node },
				Label = FunctionScope.ReturnLabel,
			});

			return true;
		}

		public bool CopyRetVal(CodeString Code)
		{
			var FS = FunctionScope;
			if (!(FS.Type.RetType is VoidType))
			{
				if (FS.Function is Constructor) return CopyRetVal(Code, FS.SelfVariable);
			}

			return true;
		}

		public virtual bool ProcessCode()
		{
			if (!State.Language.CodeProcessor.Process(this))
				return false;

			return FinishLastCommand();
		}

		public override void GetAssembly(CodeGenerator CG, GetAssemblyMode Mode = GetAssemblyMode.Code)
		{
			CG.Container = this;
			for (var i = 0; i < Children.Count; i++)
				Children[i].GetAssembly(CG);
		}

		public override bool DeclareVariables(Variable[] Variables, GetIdMode IdMode = GetIdMode.Everywhere)
		{
			var Ret = base.DeclareVariables(Variables, IdMode);
            for (var i = 0; i < Variables.Length; i++)
			{
				var e = Variables[i];
				if (e != null && e.InitValue != null)
				{
					Children.Add(new Command(this, e.InitString, CommandType.Expression)
					{
						Expressions = new List<ExpressionNode>() { e.InitValue },
					});
				}
			}

			return Ret;
		}
	}
}

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.140905.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