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
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.Runtime.InteropServices;

namespace Anonymus
{
	public enum Operator
	{
		Nothing,
		Assignment,
		Condition,
		CreateTuple,
		New,
		Ref,
		Indirection,

		Call,
		Index,
		Member,

		Neg,
		Not,
		Address,
		Complement,

		And,
		Or,
		Xor,

		Less,
		Greater,
		Equal,
		Nonequal,
		LessEqual,
		GreaterEqual,

		Add,
		Subract,

		Multiply,
		Divide,
		Modolus,

		ShiftLeft,
		ShiftRight,

		BitwiseAnd,
		BitwiseOr,
		BitwiseXor,

		Abs,
		Sqrt,

		Sin,
		Cos,
		Tan,
		ATan,
		ATan2,
		ASin,
		ACos,
	}

	public static class Operators
	{
		public static bool IsStdOp(Operator Op)
		{
			return Operators.IsBoolRetBitArithmOp(Op) || Operators.IsMathsOp(Op) ||
				Op == Operator.Neg || Op == Operator.Not || Op == Operator.Complement || Op == Operator.Condition;
		}

		public static bool IsMathsOp(Operator Op)
		{
			return Op == Operator.Abs || Op == Operator.Sqrt || Op == Operator.Sin || Op == Operator.ASin ||
				Op == Operator.ACos || Op == Operator.Cos || Op == Operator.Tan || Op == Operator.ATan || Op == Operator.ATan2;
		}

		public static bool IsInstructionOp(Operator Op)
		{
			return IsRelEqualityOp(Op) || IsBitArithmOp(Op) || Op == Operator.Assignment;
		}

		public static bool IsArithmeticalOp(Operator Op)
		{
			return Op == Operator.Add || Op == Operator.Subract || Op == Operator.Multiply ||
				   Op == Operator.Divide || Op == Operator.Modolus;
		}

		public static bool IsRelEqualityOp(Operator Op)
		{
			return Op == Operator.Equal || Op == Operator.Nonequal || Op == Operator.Less ||
				   Op == Operator.LessEqual || Op == Operator.Greater || Op == Operator.GreaterEqual;
		}

		public static bool IsShiftOp(Operator Op)
		{
			return Op == Operator.ShiftLeft || Op == Operator.ShiftRight;
		}

		public static bool IsBitwiseOp(Operator Op)
		{
			return Op == Operator.BitwiseAnd || Op == Operator.BitwiseOr || Op == Operator.BitwiseXor;
		}

		public static bool IsLogicalOp(Operator Op)
		{
			return Op == Operator.And || Op == Operator.Or || Op == Operator.And;
		}

		public static bool IsBitArithmOp(Operator Op)
		{
			return IsArithmeticalOp(Op) || IsShiftOp(Op) || IsBitwiseOp(Op);
		}

		public static bool IsBoolRetBitArithmOp(Operator Op)
		{
			return IsBitArithmOp(Op) || IsBoolRetOp(Op);
		}

		public static bool IsBoolRetOp(Operator Op)
		{
			return IsLogicalOp(Op) || IsRelEqualityOp(Op);
		}

		public static Operator NegateOp(Operator Op)
		{
			switch (Op)
			{
                case Operator.And: Op = Operator.Or; break;
                case Operator.Or: Op = Operator.And; break;

				case Operator.Equal: Op = Operator.Nonequal; break;
				case Operator.Nonequal: Op = Operator.Equal; break;

				case Operator.Less: Op = Operator.GreaterEqual; break;
				case Operator.LessEqual: Op = Operator.Greater; break;
				case Operator.Greater: Op = Operator.LessEqual; break;
				case Operator.GreaterEqual: Op = Operator.Less; break;
				default: throw new Exception("ERROR");
			}

			return Op;
		}
	}

	public delegate ExpressionNode ReplaceVarsFunc(string Name);
	public delegate ExpressionNode ReplaceNodeFunc(ExpressionNode Node);

	public class MacroExpressionNode : ExpressionNode
	{
		public Macro Macro;

		public MacroExpressionNode(Macro Macro, PString Code)
			: base(Code)
		{
			this.Macro = Macro;
		}

		public override ExpressionNode Copy(CompilerState State, PString Code, ReplaceNodeFunc Func)
		{
			if (Code == null) Code = this.Code;
			return Func(new MacroExpressionNode(Macro, Code));
		}
	}

	public class MacroArgNode : ExpressionNode
	{
		public int Index;

		public MacroArgNode(int Index, PString Code)
			: base(Code)
		{
			this.Index = Index;
		}

		public override ExpressionNode Copy(CompilerState State, PString Code, ReplaceNodeFunc Func)
		{
			if (Code == null) Code = this.Code;
			return Func(new MacroArgNode(Index, Code));
		}
	}

	public class StrExpressionNode : ExpressionNode
	{
		public StrExpressionNode(PString Code)
			: base(Code)
		{
		}

		public override ExpressionNode Copy(CompilerState State, PString Code, ReplaceNodeFunc Func)
		{
			if (Code == null) Code = this.Code;
			return Func(new StrExpressionNode(Code));
		}
	}

	public class IdExpressionNode : ExpressionNode
	{
		public Identifier Id;
        public bool MustBeAssigned = false;

		public IdExpressionNode(Identifier Id, PString Code)
			: base(Code)
		{
			this.Id = Id;
		}

		public override ExpressionNode Copy(CompilerState State, PString Code, ReplaceNodeFunc Func)
		{
			if (Code == null) Code = this.Code;
			return Func(new IdExpressionNode(Id, Code));
		}
	}

	public class OpExpressionNode : ExpressionNode
	{
		public List<ExpressionNode> Children;
		public Operator Operator = Operator.Nothing;
		public bool Reverse = false;

		public OpExpressionNode(Operator Operator, List<ExpressionNode> Children, PString Code)
			: base(Code)
		{
			this.Operator = Operator;
			this.Children = Children;
		}

		public OpExpressionNode(PString Code)
			: base(Code)
		{
			this.Operator = Operator.Nothing;
			this.Children = null;
		}

		protected override void CallChNewNode(ExprPlugIn Settings, bool CallToLinkedNodes)
		{
			for (var i = 0; i < Children.Count; i++)
				Children[i] = Children[i].CallNewNode(Settings, false, false, CallToLinkedNodes);

			base.CallChNewNode(Settings, CallToLinkedNodes);
		}

		public override IEnumerable<ExpressionNode> EnumChildrenF(NodeEnumMode Mode)
		{
			foreach (var e in base.EnumChildrenF(Mode))
				yield return e;

			foreach (var e in Children)
			{
				if (!(e is LinkingNode))
				{
					if (Mode.HasFlag(NodeEnumMode.Nodes))
						yield return e;
				}
				else
				{
					if (Mode.HasFlag(NodeEnumMode.Linking))
						yield return e;
				}
			}
		}

		public override ExpressionNode Copy(CompilerState State, PString Code, ReplaceNodeFunc Func)
		{
			var Ch = new List<ExpressionNode>();
			foreach (var e in Children)
			{
				var p = e.Copy(State, Code, Func);
				if (p == null) return null;
				Ch.Add(p);
			}

			if (Code == null) Code = this.Code;
			return Func(new OpExpressionNode(Operator, Ch, Code));
		}

		public override void ReplaceNodes(ExpressionNode From, ExpressionNode To)
		{
			base.ReplaceNodes(From, To);
			for (int i = 0; i < Children.Count; i++)
				if (Children[i] == From) Children[i] = To;
		}

		public override bool GetAssignVar(ref Variable AssignVar)
		{
			if (Operator == Operator.Assignment)
			{
				var N = Children[0] as IdExpressionNode;
				if (N != null) AssignVar = N.Id as Variable;
				else AssignVar = null;
				return true;
			}

			return false;
		}

		public void Swap()
		{
			var tmp = Children[0];
			Children[0] = Children[1];
			Children[1] = tmp;

			if (Operator == Operator.Subract || Operator == Operator.Divide)
			{
				Reverse = !Reverse;
			}
			else if (Operators.IsRelEqualityOp(Operator))
			{
				if (Operator == Operator.Less) Operator = Operator.Greater;
				else if (Operator == Operator.LessEqual) Operator = Operator.GreaterEqual;
				else if (Operator == Operator.Greater) Operator = Operator.Less;
				else if (Operator == Operator.GreaterEqual) Operator = Operator.LessEqual;
			}
		}
	}

	public class CastExpressionNode : ExpressionNode
	{
		public ExpressionNode Child;

		protected override void CallChNewNode(ExprPlugIn Settings, bool CallToLinkedNodes)
		{
			Child = Child.CallNewNode(Settings, false, false, CallToLinkedNodes);
			base.CallChNewNode(Settings, CallToLinkedNodes);
		}

		public override void ReplaceNodes(ExpressionNode From, ExpressionNode To)
		{
			if (Child == From) Child = To;
			base.ReplaceNodes(From, To);
		}

		public CastExpressionNode(Type Type, ExpressionNode Child, PString Code)
			: base(Code)
		{
			this.Type = Type;
			this.Child = Child;
		}

		public override IEnumerable<ExpressionNode> EnumChildrenF(NodeEnumMode Mode)
		{
			foreach (var e in base.EnumChildrenF(Mode))
				yield return e;

			if (!(Child is LinkingNode))
			{
				if (Mode.HasFlag(NodeEnumMode.Nodes))
					yield return Child;
			}
			else
			{
				if (Mode.HasFlag(NodeEnumMode.Linking))
					yield return Child;
			}
		}

		public override ExpressionNode Copy(CompilerState State, PString Code, ReplaceNodeFunc Func)
		{
			var Ch = Child.Copy(State, Code, Func);
			if (Code == null) Code = this.Code;
			return Func(new CastExpressionNode(Type, Ch, Code));
		}
	}

	public class LinkedExprNode
	{
		public ExpressionNode Node;
		public object ArchData;
		public int LinkingCount = 0;

		public LinkedExprNode(ExpressionNode Node)
		{
			this.Node = Node;
		}
	}

	[Flags]
	public enum NodeEnumMode
	{
		Nodes = 1,
		Linking = 2,
		Linked = 4,
		NodesnLinked = Nodes | Linked,
	}

	public class LinkingNode : ExpressionNode
	{
		public LinkedExprNode LinkedNode;

		public LinkingNode(LinkedExprNode Node, PString Code)
			: base(Code)
		{
			this.LinkedNode = Node;
		}
		
		public override IEnumerable<ExpressionNode> EnumChildrenF(NodeEnumMode Mode)
		{
			foreach (var e in base.EnumChildrenF(Mode))
				yield return e;

			if (Mode.HasFlag(NodeEnumMode.Linked))
				yield return LinkedNode.Node;
		}
		
		public override ExpressionNode Copy(CompilerState State, PString Code, ReplaceNodeFunc Func)
		{
			if (Code == null) Code = this.Code;
			return Func(new LinkingNode(LinkedNode, Code));
		}

		public override ExpressionNode RealNode
		{
			get { return LinkedNode.Node; }
		}
	}

	public abstract class ExpressionNode
	{
		public List<LinkedExprNode> LinkedNodes;
		public Type Type;
		public object ArchData;
		public PString Code;

		// RootNode Data
		public NodeVariables Vars;

		public virtual ExpressionNode RealNode
		{
			get { return this; }
		}
		
		public virtual IEnumerable<ExpressionNode> EnumChildrenF(NodeEnumMode Mode)
		{
			if (LinkedNodes != null && Mode.HasFlag(NodeEnumMode.Linked))
			{
				foreach (var e in LinkedNodes)
					yield return e.Node;
			}
		}

		public IEnumerable<ExpressionNode> EnumChildren
		{
			get { return EnumChildrenF(NodeEnumMode.NodesnLinked); }
		}

		public IEnumerable<ExpressionNode> EnumChWithoutLinkedNodes
		{
			get { return EnumChildrenF(NodeEnumMode.Nodes); }
		}

		public virtual void ReplaceNodes(ExpressionNode From, ExpressionNode To)
		{
			if (LinkedNodes != null)
			{
				foreach (var e in LinkedNodes)
					if (e.Node == From) e.Node = To;
			}
		}

		public virtual ConditionRes ConditionResult
		{ get { return ConditionRes.Unknown; } }

		protected virtual void CallChNewNode(ExprPlugIn Settings, bool CallToLinkedNodes)
		{
		}

		public ExpressionNode CallNewNode(ExprPlugIn Settings, bool Begin = true, bool End = true,
										  bool CallToLinkedNodes = true)
		{
			if (Begin) Settings.Begin();

			if (LinkedNodes != null && CallToLinkedNodes)
			{
				for (var i = 0; i < LinkedNodes.Count; i++)
				{
					var N = LinkedNodes[i].Node;
					N = N.CallNewNode(Settings, false, false);
					LinkedNodes[i].Node = N;
				}
			}

			CallChNewNode(Settings, CallToLinkedNodes);

			var Node = Settings.NewNode(this);
			if (End) Node = Settings.End(Node);
			return Node;
		}

		public ExpressionNode Copy(CompilerState State, PString Code = null, ExprPlugIn PlugIn = null,
			ReplaceNodeFunc Func = null, bool Begin = true, bool End = true)
		{
			if (Begin && PlugIn != null) PlugIn.Begin();

			var Ret = Copy(State, Code, (Node) =>
			{
				if (Func != null) Node = Func(Node);
				if (PlugIn != null && Node != null) return PlugIn.NewNode(Node);
				else return Node;
			});

			if (End && Ret != null && PlugIn != null) Ret = PlugIn.End(Ret);
			return Ret;
		}

		public abstract ExpressionNode Copy(CompilerState State, PString Code, ReplaceNodeFunc Func);

		public ExpressionNode(PString Code)
		{
			this.Code = Code;
		}

		public ExpressionNode Indirection(CompilerState State, IdContainer Container,
			PString Code, ExprPlugIn PlugIn = null)
		{
			var Ch = new List<ExpressionNode>() { this };
			var Ret = new OpExpressionNode(Operator.Indirection, Ch, Code);

			if (PlugIn == null) return Ret;
			else return PlugIn.NewNode(Ret);
		}

		public ExpressionNode CreateAssignExpr(ExpressionNode Child, PString Code,
			ExprPlugIn PlugIn = null, bool End = false)
		{
			var Children = new List<ExpressionNode>() { Child, this };
			var Ret = (ExpressionNode)new OpExpressionNode(Operator.Assignment, Children, Code);
			if (PlugIn == null) return Ret;
			
			Ret = PlugIn.NewNode(Ret);
			if (Ret == null) return null;

			if (End) Ret = PlugIn.End(Ret);
			return Ret;
		}

		public ExpressionNode CreateAssignExpr(Identifier Id, PString Code,
			ExprPlugIn PlugIn = null, bool End = false)
		{
			var Child = (ExpressionNode)new IdExpressionNode(Id, Code);
			if (PlugIn != null)
			{
				Child = PlugIn.NewNode(Child);
				if (Child == null) return null;
			}

			return CreateAssignExpr(Child, Code, PlugIn, End);
		}

		public ExpressionNode CreateCastExpr(Type Type, PString Code = null, ExprPlugIn PlugIn = null)
		{
			if (Code == null) Code = this.Code;
			var Ret = new CastExpressionNode(Type, this, Code);
			if (PlugIn == null) return Ret;
			else return PlugIn.NewNode(Ret);
		}

		public bool IdUsed(Identifier Id)
		{
			if (this is IdExpressionNode)
			{
				var RId = this as IdExpressionNode;
				return RId.Id == Id;
			}

			foreach (var Ch in EnumChWithoutLinkedNodes)
				if (Ch.IdUsed(Id)) return true;

			return false;
		}

		public bool IdUsed()
		{
			if (this is IdExpressionNode || this is StrExpressionNode)
				return true;

			foreach (var e in EnumChildren)
				if (e.IdUsed()) return true;

			return false;
		}

		public virtual bool GetAssignVar(ref Variable AssignVar)
		{
			return false;
		}
	}

	public class NodeVariables
	{
		public List<IdExpressionNode> AssignedIds = new List<IdExpressionNode>();
		public List<IdExpressionNode> UsedBeforeAssignIds = new List<IdExpressionNode>();

		public NodeVariables()
		{
		}

		public bool UsedBeforeAssign(Identifier Id)
		{
			foreach (var e in UsedBeforeAssignIds)
				if (e.Id == Id) return true;

			return false;
		}

		public bool Assigned(Identifier Id)
		{
			foreach (var e in AssignedIds)
				if (e.Id == Id) return true;

			return false;
		}

		public bool Assigned(IEnumerable<Identifier> Ids)
		{
			foreach (var e in Ids)
				if (!Assigned(e)) return false;

			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
Web03 | 2.8.140827.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