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;

namespace Bird.Recognizers
{
	public interface IBuiltinFuncRecognizer
	{
		ExprRecResult Recognize(CodeString Code, CodeString Function, CodeString[] Params,
			CodeString[] GenericParams, PluginRoot Plugin, ref ExpressionNode Out);
	}

	public class StackAllocRecognizer : IBuiltinFuncRecognizer
	{
		public ExprRecResult Recognize(CodeString Code, CodeString Function, CodeString[] Params,
			CodeString[] GenericParams, PluginRoot Plugin, ref ExpressionNode Out)
		{
			if (Function.IsEqual("stackalloc"))
			{
				var State = Plugin.State;

				if (GenericParams != null && GenericParams.Length != 0)
				{
					State.Messages.Add(MessageId.NonGenericIdentifier, Function);
					return ExprRecResult.Failed;
				}

				if (Params.Length != 1)
				{
					State.Messages.Add(MessageId.ParamCount, Code);
					return ExprRecResult.Failed;
				}

				var Bytes = Expressions.Recognize(Params[0], Plugin, true);
				if (Bytes == null) return ExprRecResult.Failed;

				var Ch = new ExpressionNode[] { Bytes };
				Out = new OpExpressionNode(Operator.StackAlloc, Ch, Code);
				return ExprRecResult.Succeeded;
			}

			return ExprRecResult.Unknown;
		}
	}

	public class IncBinRecognizer : IBuiltinFuncRecognizer
	{
		private IncludedBinary GetIncBin(CodeString Code, PluginRoot Plugin)
		{
			string String;
			if (!Constants.RecognizeString(Code, Plugin, out String))
				return null;

			var Global = Plugin.Container.GlobalContainer;
			var IncBin = Global.GetIncludedBinary(String);

			if (IncBin == null)
			{
				Plugin.State.Messages.Add(MessageId.UnknownId, Code);
				return null;
			}

			return IncBin;
		}

		public ExprRecResult Recognize(CodeString Code, CodeString Function, CodeString[] Params,
			CodeString[] GenericParams, PluginRoot Plugin, ref ExpressionNode Out)
		{
			var State = Plugin.State;
			var Container = Plugin.Container;

			int Func;
			if (Function.IsEqual("incbin_ptr")) Func = 0;
			else if (Function.IsEqual("incbin_size")) Func = 1;
			else return ExprRecResult.Unknown;

			if (GenericParams != null && GenericParams.Length != 0)
			{
				State.Messages.Add(MessageId.NonGenericIdentifier, Function);
				return ExprRecResult.Failed;
			}

			if (Params.Length != 1)
			{
				State.Messages.Add(MessageId.ParamCount, Code);
				return ExprRecResult.Failed;
			}

			var IncBin = GetIncBin(Params[0], Plugin);
			if (IncBin == null) return ExprRecResult.Failed;

			if (Func == 0)
			{
				Out = new DataPointerNode(Code, IncBin);
			}
			else if (Func == 1)
			{
                var Type = Container.GlobalContainer.CommonIds.UIntPtr;
				Out = new ConstExpressionNode(Type, new IntegerValue(IncBin.Length), Code);
			}
			else
			{
				throw new ApplicationException();
			}

			return ExprRecResult.Succeeded;
		}
	}

	public class DataPointerRecognizer : IBuiltinFuncRecognizer
	{
		public ExprRecResult Recognize(CodeString Code, CodeString Function, CodeString[] Params,
			CodeString[] GenericParams, PluginRoot Plugin, ref ExpressionNode Out)
		{
			var State = Plugin.State;
			if (Function.IsEqual("id_desc_ptr"))
			{
				if (GenericParams != null && GenericParams.Length != 0)
				{
					State.Messages.Add(MessageId.NonGenericIdentifier, Function);
					return ExprRecResult.Failed;
				}

				if (Params.Length != 1)
				{
					State.Messages.Add(MessageId.ParamCount, Code);
					return ExprRecResult.Failed;
				}

				var Container = Plugin.Container;
				var Id = Container.RecognizeIdentifier(Params[0]);
				if (Id == null) return ExprRecResult.Failed;

				Out = new DataPointerNode(Code, Id);
				return ExprRecResult.Succeeded;
			}

			if (Function.IsEqual("assembly_desc_ptr"))
			{
				var Global = Plugin.Container.GlobalContainer;
				if (GenericParams != null && GenericParams.Length != 0)
				{
					State.Messages.Add(MessageId.NonGenericIdentifier, Function);
					return ExprRecResult.Failed;
				}

				if (Params.Length == 0)
				{
					Out = new DataPointerNode(Code, Global.OutputAssembly);
					return ExprRecResult.Succeeded;
				}
				else
				{
					if (Params.Length != 1)
					{
						State.Messages.Add(MessageId.ParamCount, Code);
						return ExprRecResult.Failed;
					}

					string String;
					if (!Constants.RecognizeString(Params[0], Plugin, out String))
						return ExprRecResult.Failed;

					var Assembly = Global.GetAssembly(String);
					if (Assembly == null)
					{
						State.Messages.Add(MessageId.UnknownId, Params[0]);
						return ExprRecResult.Failed;
					}

					Out = new DataPointerNode(Code, Assembly);
					return ExprRecResult.Succeeded;
				}
			}

			return ExprRecResult.Unknown;
		}
	}

	public class DefaultRecognizer : IBuiltinFuncRecognizer
	{
		public ExprRecResult Recognize(CodeString Code, CodeString Function, CodeString[] Params,
			CodeString[] GenericParams, PluginRoot Plugin, ref ExpressionNode Out)
		{
			if (Function.IsEqual("default"))
			{
				var State = Plugin.State;
				if (GenericParams != null && GenericParams.Length != 0)
				{
					State.Messages.Add(MessageId.NonGenericIdentifier, Function);
					return ExprRecResult.Failed;
				}

				if (Params.Length != 1)
				{
					State.Messages.Add(MessageId.ParamCount, Code);
					return ExprRecResult.Failed;
				}

				var Container = Plugin.Container;
				var Type = Container.RecognizeIdentifier(Params[0], GetIdOptions.DefaultForType);
				if (Type == null) return ExprRecResult.Failed;

				Out = Constants.GetDefaultValue(Plugin, Type, Code);
				return Out == null ? ExprRecResult.Failed : ExprRecResult.Ready;
			}

			return ExprRecResult.Unknown;
		}
	}

	public class ReinterpretCastRecognizer : IBuiltinFuncRecognizer
	{
		public ExprRecResult Recognize(CodeString Code, CodeString Function, CodeString[] Params,
			CodeString[] GenericParams, PluginRoot Plugin, ref ExpressionNode Out)
		{
			if (Function.IsEqual("reinterpret_cast"))
			{
				var State = Plugin.State;
				var Container = Plugin.Container;

				if (GenericParams == null || GenericParams.Length != 1)
				{
					State.Messages.Add(MessageId.GenericParamCount, Code);
					return ExprRecResult.Failed;
				}

				if (Params.Length != 1)
				{
					State.Messages.Add(MessageId.ParamCount, Code);
					return ExprRecResult.Failed;
				}

                var Options = GetIdOptions.DefaultForType;
                var Type = Identifiers.Recognize(Container, GenericParams[0], Options);
                if (Type == null) return ExprRecResult.Failed;

				var Child = Expressions.Recognize(Params[0], Plugin, true);
                var TypeNode = Plugin.NewNode(new IdExpressionNode(Type, GenericParams[0]));
                if (Child == null || TypeNode == null) return ExprRecResult.Failed;

				Out = new OpExpressionNode(Operator.Reinterpret, Code);
                Out.Children = new ExpressionNode[] { Child, TypeNode };
				return ExprRecResult.Succeeded;
			}

			return ExprRecResult.Unknown;
		}
	}

	public class IsDefinedRecognizer : IBuiltinFuncRecognizer
	{
		public ExprRecResult Recognize(CodeString Code, CodeString Function, CodeString[] Params,
			CodeString[] GenericParams, PluginRoot Plugin, ref ExpressionNode Out)
		{
			if (Function.IsEqual("defined"))
			{
				var State = Plugin.State;
				if (GenericParams != null && GenericParams.Length != 0)
				{
					State.Messages.Add(MessageId.NonGenericIdentifier, Function);
					return ExprRecResult.Failed;
				}

				if (Params.Length != 1)
				{
					State.Messages.Add(MessageId.ParamCount, Code);
					return ExprRecResult.Failed;
				}

                var Preprocessor = State.GlobalContainer.Preprocessor;
                var Defined = Preprocessor.GetMacro(Params[0].ToString()) != null;
				Out = Constants.GetBoolValue(Plugin.Container, Defined, Code);
				if (Out == null) return ExprRecResult.Failed;
				return ExprRecResult.Succeeded;
			}

			return ExprRecResult.Unknown;
		}
	}

	public class SizeOfRecognizer : IBuiltinFuncRecognizer
	{
		public bool AllowVariables = false;

		public ExprRecResult Recognize(CodeString Code, CodeString Function, CodeString[] Params,
			CodeString[] GenericParams, PluginRoot Plugin, ref ExpressionNode Out)
		{
			if (Function.IsEqual("sizeof"))
			{
				var State = Plugin.State;
				if (GenericParams != null && GenericParams.Length != 0)
				{
					State.Messages.Add(MessageId.NonGenericIdentifier, Function);
					return ExprRecResult.Failed;
				}

				if (Params.Length != 1)
				{
					State.Messages.Add(MessageId.ParamCount, Code);
					return ExprRecResult.Failed;
				}

				var Options = GetIdOptions.Default;
				Options.Func = x => x.RealId is Type || (x.RealId is Variable && AllowVariables);

				var Id = Plugin.Container.RecognizeIdentifier(Params[0], Options);
				if (Id == null) return ExprRecResult.Failed;

                Type Type = null;
                if (Id.RealId is Variable && AllowVariables)
                    Type = Id.TypeOfSelf.RealId as Type;
                else Type = Id.RealId as Type;

				if (Type == null)
				{
					State.Messages.Add(MessageId.CannotGetSize, Params[0]);
					return ExprRecResult.Failed;
				}

				Out = Constants.GetIntValue(Plugin.Container, Type.Size, Code, true);
				return ExprRecResult.Succeeded;
			}

			return ExprRecResult.Unknown;
		}
	}
}

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