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.IO;
using System.Collections.Generic;
using System.Runtime.InteropServices;
using System.Numerics;
using System.Linq;

namespace Bird
{
	public static class DataStoring
	{
		public static BigInteger GetAlignmentMask(int Size, int Align)
		{
			var ZeroBits = Helper.Pow2Sqrt(Align);
			var SizeMask = GetSizeMask(Size);
			return (SizeMask << ZeroBits) & SizeMask;
		}

		public static int AlignWithDecrease(int Value, int Align)
		{
			return Value - Value % Align;
		}

		public static int AlignWithIncrease(int Value, int Align)
		{
			return AlignWithDecrease(Value + Align - 1, Align);
		}

		public static BigInteger AlignWithDecrease(BigInteger Value, int Align)
		{
			return Value - Value % Align;
		}

		public static BigInteger AlignWithIncrease(BigInteger Value, int Align)
		{
			return AlignWithDecrease(Value + Align - 1, Align);
		}

		public static ExpressionNode AlignWithDecrease(PluginRoot Plugin, ExpressionNode Node, int Align, CodeString Code)
		{
			var RType = Node.Type.RealId as Type;
			var Mask = GetAlignmentMask(RType.Size, Align);
			var AndCh1Value = new IntegerValue(Mask);

			var AndCh1 = Plugin.NewNode(new ConstExpressionNode(Node.Type, AndCh1Value, Code));
			if (AndCh1 == null) return null;

			var AndCh = new ExpressionNode[] { Node, AndCh1 };
			return Plugin.NewNode(new OpExpressionNode(Operator.BitwiseAnd, AndCh, Code));
		}

		public static ExpressionNode AlignWithIncrease(PluginRoot Plugin, ExpressionNode Node, int Align, CodeString Code)
		{
			var RType = Node.Type.RealId as Type;
			var AddCh1Value = new IntegerValue(Align - 1);

			var AddCh1 = Plugin.NewNode(new ConstExpressionNode(Node.Type, AddCh1Value, Code));
			if (AddCh1 == null) return null;

			var AddCh = new ExpressionNode[] { Node, AddCh1 };
			var AddNode = Plugin.NewNode(new OpExpressionNode(Operator.Add, AddCh, Code));
			if (AddNode == null) return null;

			return AlignWithDecrease(Plugin, AddNode, Align, Code);
		}

		public static bool VerifyAlign(int Align)
		{
			return Align == 1 || Align == 2 || Align == 4 || Align == 8 || Align == 16 || Align == 32;
		}

		public static string[] Str_SizeMask = new string[] { "", "FF", "FFFF", "FFFFFF",
			"FFFFFFFF", "FFFFFFFFFF", "FFFFFFFFFFFF", "FFFFFFFFFFFFFF", "FFFFFFFFFFFFFFFF" };

		public static ulong[] Ulong_SizeMask = new ulong[] { 0x0, 0xFF, 0xFFFF, 0xFFFFFF,
			0xFFFFFFFF, 0xFFFFFFFFFF, 0xFFFFFFFFFFFF, 0xFFFFFFFFFFFFFF, 0xFFFFFFFFFFFFFFFF };

		public static BigInteger WrapToType(BigInteger x, Identifier Type)
		{
			var RType = Type.RealId as Type;
			var Bytes = x.ToByteArray();
			var Ret = new byte[RType.Size];

			var MinLength = Math.Min(Bytes.Length, Ret.Length);
			for (var i = 0; i < MinLength; i++)
				Ret[i] = Bytes[i];

			if (x < 0)
			{
				for (var i = Bytes.Length; i < Ret.Length; i++)
					Ret[i] = byte.MaxValue;
			}

			if ((Ret[Ret.Length - 1] & 0x80) != 0 && RType is UnsignedType)
			{
				var NRet = new byte[Ret.Length + 1];
				for (var i = 0; i < Ret.Length; i++)
					NRet[i] = Ret[i];

				Ret = NRet;
			}

			return new BigInteger(Ret);
		}

		public static BigInteger GetSizeMask(int Size)
		{
			if (Size <= 8) return Ulong_SizeMask[Size];

			var Ret = new BigInteger();
			for (var i = 0; i < Size; i++)
				Ret = (Ret << 8) + 0xFF;

			return Ret;
		}
	}

	public static class LEB128Helper
	{
		public static ulong DecodeULong(Func<byte> GetByte)
		{
			var Result = 0UL;
			var Shift = (byte)0;

			while (true)
			{
				var Byte = GetByte();
				Result |= (ulong)(Byte & 0x7F) << Shift;
				if ((Byte & 0x80) == 0) break;
				Shift += 7;
			}

			return Result;
		}

		public static long DecodeLong(Func<byte> GetByte)
		{
			var Result = 0L;
			var Shift = (byte)0;
			var Byte = (byte)0;

			while (true)
			{
				Byte = GetByte();
				Result |= (long)(Byte & 0x7F) << Shift;
				Shift += 7;
				if ((Byte & 0x80) == 0) break;
			}

			if (Shift < sizeof(long) * 8 && (Byte & 0x40) != 0)
				Result |= -1L << Shift;

			return Result;
		}

		public static uint DecodeUInt(Func<byte> GetByte)
		{
			var Result = 0U;
			var Shift = (byte)0;

			while (true)
			{
				var Byte = GetByte();
				Result |= (uint)(Byte & 0x7F) << Shift;
				if ((Byte & 0x80) == 0) break;
				Shift += 7;
			}

			return Result;
		}

		public static int DecodeInt(Func<byte> GetByte)
		{
			var Result = 0;
			var Shift = (byte)0;
			var Byte = (byte)0;

			while (true)
			{
				Byte = GetByte();
				Result |= (int)(Byte & 0x7F) << Shift;
				Shift += 7;
				if ((Byte & 0x80) == 0) break;
			}

			if (Shift < sizeof(int) * 8 && (Byte & 0x40) != 0)
				Result |= -1 << Shift;

			return Result;
		}

		public static BigInteger Decode(Func<byte> GetByte)
		{
			var Result = (BigInteger)0;
			var Shift = 0;
			var Byte = (byte)0;

			while (true)
			{
				Byte = GetByte();
				Result |= ((BigInteger)(Byte & 0x7F) << Shift);
				Shift += 7;
				if ((Byte & 0x80) == 0) break;
			}

			if ((Byte & 0x40) != 0)
				Result |= -((BigInteger)1 << Shift);

			return Result;
		}

		public static void Encode(int Value, Action<byte> EmitByte)
		{
			var More = true;
			while (More)
			{
				var Byte = (byte)(Value & 0x7F);
				var Signed = (Byte & 0x40) != 0;
				Value >>= 7;

				if ((Value == 0 && !Signed) || (Value == -1 && Signed)) More = false;
				else Byte |= 0x80;
				EmitByte(Byte);
			}
		}

		public static void Encode(uint Value, Action<byte> EmitByte)
		{
			do
			{
				var Byte = (byte)(Value & 0x7F);
				if ((Value >>= 7) != 0)
					Byte |= 0x80;

				EmitByte(Byte);
			} while (Value != 0);
		}

		public static void Encode(long Value, Action<byte> EmitByte)
		{
			var More = true;
			while (More)
			{
				var Byte = (byte)(Value & 0x7F);
				var Signed = (Byte & 0x40) != 0;
				Value >>= 7;

				if ((Value == 0 && !Signed) || (Value == -1 && Signed)) More = false;
				else Byte |= 0x80;
				EmitByte(Byte);
			}
		}

		public static void Encode(ulong Value, Action<byte> EmitByte)
		{
			do
			{
				var Byte = (byte)(Value & 0x7F);
				if ((Value >>= 7) != 0)
					Byte |= 0x80;

				EmitByte(Byte);
			} while (Value != 0);
		}

		public static void Encode(BigInteger Value, Action<byte> EmitByte)
		{
			var More = true;
			while (More)
			{
				var Byte = (byte)(Value & 0x7F);
				var Signed = (Byte & 0x40) != 0;
				Value >>= 7;

				if ((Value == 0 && !Signed) || (Value == -1 && Signed)) More = false;
				else Byte |= 0x80;
				EmitByte(Byte);
			}
		}
	}
}

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