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

Bird Programming Language: Part 3

, 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
using System;
using System.Collections.Generic;
using System.Linq;
using Bird.Recognizers;

namespace Bird
{
	public enum PluginResult
	{
		Failed,
		Succeeded,
		Ready,
		Interrupt,
	}

	[Flags]
	public enum BeginEndMode
	{
		None = 0,
		Begin = 1,
		End = 2,
		Both = Begin | End,
	}

	public interface IPluginDeclarationHandler
	{
		bool OnIdentifierDeclared(Identifier Id);
	}

	public interface IExpressionPlugin
	{
		bool Begin();
		PluginResult NewNode(ref ExpressionNode Node);
		PluginResult End(ref ExpressionNode Node);
		PluginResult ForceContinue(ref ExpressionNode Node);
	}

	public class ExpressionPlugin : IExpressionPlugin
	{
		public PluginRoot Parent;
		public IdContainer Container;
		public CompilerState State;

		public virtual PluginResult ForceContinue(ref ExpressionNode Node)
		{
			return PluginResult.Succeeded;
		}

		public virtual PluginResult NewNode(ref ExpressionNode Node)
		{
			return PluginResult.Succeeded;
		}

		public virtual PluginResult End(ref ExpressionNode Node)
		{
			return PluginResult.Succeeded;
		}

		public virtual bool Begin()
		{
			Container = Parent.Container;
			return true;
		}

		public ExpressionPlugin(PluginRoot Parent)
		{
			this.Parent = Parent;
			this.Container = Parent.Container;
			this.State = Parent.State;
		}

		protected PluginResult ResolveNode(ref ExpressionNode Node)
		{
			if (Node.InterrupterPlugin != -1)
			{
				if (Node.InterrupterPlugin != Array.IndexOf(Parent.Plugins, this))
					return PluginResult.Succeeded;

				var Res = ForceContinue(ref Node);
				if (Res != PluginResult.Succeeded) return Res;

				if (Parent.FinishNode(ref Node, false) == PluginResult.Failed)
					return PluginResult.Failed;

				return PluginResult.Ready;
			}

			return PluginResult.Succeeded;
		}

		protected ExpressionNode ResolveNode(ExpressionNode Node)
		{
			var Res = ResolveNode(ref Node);
			return Res == PluginResult.Failed ? null : Node;
		}

		protected bool ResolveChildren(ExpressionNode Node)
		{
			return Node.ReplaceChildren(x => ResolveNode(x));
		}
	}

	public class EmptyPluginRoot : PluginRoot
	{
		public EmptyPluginRoot(IdContainer Container)
			: base(Container)
		{
			Plugins = new IExpressionPlugin[0];
		}
	}
	
	public class PluginRoot
	{
		public CompilerState State;
		public IdContainer Container;
		public IExpressionPlugin[] Plugins;
		public int CallNewNodeFrom = 0;
		public bool CurrentlyUsing = false;

		public Variable CreateVariable(Identifier Type, CodeString Name)
		{
			return Container.CreateVariable(Name, Type);
		}

		public bool DeclareIdentifier(Identifier Id)
		{
			if (Id.Container.FunctionScope != null)
			{
                if (!Id.Container.CanIdDeclared(Id))
					return false;

				for (var i = 0; i < Plugins.Length; i++)
				{
					var Plugin = Plugins[i] as IPluginDeclarationHandler;
					if (Plugin != null && !Plugin.OnIdentifierDeclared(Id))
						return false;
				}

				return true;
			}
			else
			{
				State.Messages.Add(MessageId.CannotDeclVar, Id.Name);
				return false;
			}
		}

		public Variable CreateAndDeclareVar(Identifier Type, CodeString Name)
		{
			var Ret = CreateVariable(Type, Name);
			if (Ret == null || !DeclareIdentifier(Ret)) return null;
			return Ret;
		}

		public ExpressionNode DeclareVarAndCreateIdNode(CodeString Code, Identifier Type, CodeString Name)
		{
			var Var = CreateAndDeclareVar(Type, Name);
			if (Var == null) return null;

			return NewNode(new IdExpressionNode(Var, Code, ExpressionFlags.IdMustBeAssigned));
		}

		public ExpressionNode DeclareVarAndCreateIdNode(CodeString Code, CodeString TypeName, CodeString Name)
		{
			var Type = Container.RecognizeIdentifier(TypeName, GetIdOptions.DefaultForType);
			if (Type == null) return null;

			return DeclareVarAndCreateIdNode(Code, Type, Name);
		}

		public ExpressionNode FinishNode(ExpressionNode Node, bool CallInterrupter = true)
		{
			var Res = FinishNode(ref Node, CallInterrupter);
			return Res == PluginResult.Failed ? null : Node;
		}

		public ExpressionNode NewNode(ExpressionNode Node)
		{
			if (NewNode(ref Node) == PluginResult.Failed)
				return null;
			else return Node;
		}

		public ExpressionNode End(ExpressionNode Node)
		{
			if (End(ref Node) == PluginResult.Failed)
				return null;
			else return Node;
		}

		public void Reset()
		{
			CurrentlyUsing = false;
		}

		public ExpressionNode Continue(ExpressionNode Node, bool CallInterrupter = true)
		{
			if (Continue(ref Node, CallInterrupter) == PluginResult.Failed)
				return null;

			return Node;
		}

		public PluginResult FinishNode(ref ExpressionNode Node, bool CallInterrupter = true)
		{
			while (true)
			{
				var Res = Continue(ref Node, CallInterrupter);
				if (Res != PluginResult.Ready && Res != PluginResult.Interrupt) 
					return Res;

				if (Node.InterrupterPlugin == -1) return Res;
				CallInterrupter = true;
			}
		}

		public PluginResult FinishNode(ref ExpressionNode Node, ExpressionPlugin Until, bool CallInterrupter = true)
		{
			var Index = Array.IndexOf(Plugins, Until);
			if (Index == -1) throw new ApplicationException();

			while (true)
			{
				var Res = Continue(ref Node, CallInterrupter);
				if (Res != PluginResult.Ready && Res != PluginResult.Interrupt)
					return Res;

				if (Node.InterrupterPlugin == -1 || Node.InterrupterPlugin > Index) 
					return Res;

				CallInterrupter = true;
			}
		}

		public PluginResult Continue(ref ExpressionNode Node, bool CallInterrupter = true)
		{
			if (Node.InterrupterPlugin == -1) return PluginResult.Ready;
			//throw new ApplicationException("Node hasn't been interrupted");

			var From = Node.InterrupterPlugin;
			if (CallInterrupter)
			{
				var Res = Plugins[From].ForceContinue(ref Node);
				if (Res != PluginResult.Succeeded)
				{
					if (Res == PluginResult.Interrupt)
						throw new ApplicationException("Cannot interrupt");

					return Res;
				}
			}

			Node.InterrupterPlugin = -1;
			return NewNode(ref Node, From + 1);
		}

		public PluginRoot(IdContainer Container)
		{
			this.Container = Container;
			this.State = Container.State;
		}

		internal PluginResult NewNode(ref ExpressionNode Node, int From)
		{
			if (!CurrentlyUsing)
				throw new InvalidOperationException("Begin hasn't been called");

			for (var i = From; i < Plugins.Length; i++)
			{
				var Res = Plugins[i].NewNode(ref Node);

				if (Res != PluginResult.Succeeded)
				{
					if (Res == PluginResult.Interrupt)
					{
						if (Node.InterrupterPlugin != -1)
							throw new InvalidOperationException("Already interrupted");

						Node.InterrupterPlugin = i;
					}

					return Res;
				}
			}

			if (!Node.CheckChildren(x => x.InterrupterPlugin == -1))
				throw new ApplicationException("Unfinished node");

			return PluginResult.Succeeded;
		}

		public PluginResult NewNodeDontCallAll(ref ExpressionNode Node)
		{
			Node.InterrupterPlugin = -1;
			return NewNode(ref Node, CallNewNodeFrom);
		}

		public PluginResult NewNode(ref ExpressionNode Node)
		{
			Node.InterrupterPlugin = -1;
			return NewNode(ref Node, 0);
		}

		public bool Begin()
		{
			if (CurrentlyUsing)
				throw new InvalidOperationException("End hasn't been called");

			CurrentlyUsing = true;
			for (var i = 0; i < Plugins.Length; i++)
				if (!Plugins[i].Begin()) return false;

			return true;
		}

		public PluginResult End(ref ExpressionNode Node)
		{
			if (!CurrentlyUsing)
				throw new InvalidOperationException("Begin hasn't been called");

			for (var i = 0; i < Plugins.Length; i++)
			{
				var Res = Plugins[i].End(ref Node);
				if (Res != PluginResult.Succeeded)
				{
					if (Res == PluginResult.Interrupt)
						throw new ApplicationException("Can't interrupt");

					return Res;
				}
			}

#if DEBUG
			if (!Expressions.CheckLinkedNodes(Node))
				throw new ApplicationException();
#endif
			CurrentlyUsing = false;
			return PluginResult.Succeeded;
		}

		public T GetPlugin<T>() where T : ExpressionPlugin
		{
			for (var i = 0; i < Plugins.Length; i++)
				if (Plugins[i] is T) return Plugins[i] as T;

			return null;
		}
	}

	public class PluginForGlobals : PluginRoot
	{
		public PluginForGlobals(IdContainer Container)
			: base(Container)
		{
			Plugins = new IExpressionPlugin[]
			{
				new PreProcPlugin(this, false),
				new IdRecognizerPlugin(this),
				new TypeMngrPlugin(this, null),
				new EvaluatorPlugin(this, true),
			};
		}
	}

	public class PluginForCodeScope : PluginRoot
	{
		public PluginForCodeScope(IdContainer Container)
			: base(Container)
		{
			Plugins = new IExpressionPlugin[]
			{
				new PreProcPlugin(this, false),
				new IdRecognizerPlugin(this),
				new TypeMngrPlugin(this, null, TypeMngrPluginFlags.CalculateLayouts),
				new EvaluatorPlugin(this, false),
				new CompilerPlugin(this),
			};
		}
	}

	public class PluginForConstants : PluginRoot
	{
		public PluginForConstants(IdContainer Container, bool DoNotFail = false)
			: base(Container)
		{
			Plugins = new IExpressionPlugin[]
			{
				new PreProcPlugin(this, false),
				new IdRecognizerPlugin(this, DoNotFail),
				new TypeMngrPlugin(this, null, TypeMngrPluginFlags.EnableUntypedNodes),
				new EvaluatorPlugin(this, false),
			};
		}
	}
}

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.1411023.1 | Last Updated 1 Jan 2013
Article Copyright 2012 by Dávid Kocsis
Everything else Copyright © CodeProject, 1999-2014
Layout: fixed | fluid