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

namespace Anonymus
{
	public static class LibraryLoader
	{
		enum IdentifierType
		{
			GlobalVariable,
			MemberVariable,
			Function,
			MemberFunction,
			ConstructorFunction,
			TupleType,
			ValueType,
			ClassType,
			RefType,
			PointerType,
			TypeDefType,
			EnumType,
			FlagType,
			FunctionType,
		}

		enum LibraryLoaderCommand
		{
			DeclareIds,
			DeclareMembers,
			Exit,
		}

		static Identifier GetIdByAsmName(BinaryReader Reader, List<Identifier> Ids)
		{
			var RAsmName = Reader.ReadString();
			foreach (var e in Ids)
				if (e.AsmName == RAsmName) return e;

			return null;
		}

		static Identifier ReadIdentifier(BinaryReader Reader, List<Identifier> Ids, IdContainer Container)
		{
			var Ret = (Identifier)null;
			var IdCode = Reader.ReadByte();
			if (IdCode == 255) return GetIdByAsmName(Reader, Ids);

			var IdType = (IdentifierType)IdCode;
			var Name = new PString(Reader.ReadString());
			var AsmName = Reader.ReadString();
			var IsStatic = Reader.ReadBoolean();
			var IsSealed = Reader.ReadBoolean();

			//-----------------------------------------------------------------------------------
			if (IdType == IdentifierType.GlobalVariable)
			{
				var Type = ReadIdentifier(Reader, Ids, Container) as Type;
				if (Type == null) return null;

				var V = new GlobalVariable(Container, Name, Type);
				V.ReadOnly = Reader.ReadBoolean();
				Ret = V;
			}

			else if (IdType == IdentifierType.MemberVariable)
			{
				var Type = ReadIdentifier(Reader, Ids, Container) as Type;
				if (Type == null) return null;

				var V = new MemberVariable(Container, Name, Type);
				V.ReadOnly = Reader.ReadBoolean();
				V.Offset = Reader.ReadInt32();
				Ret = V;
			}

			//-----------------------------------------------------------------------------------
			else if (IdType == IdentifierType.Function || IdType == IdentifierType.ConstructorFunction)
			{
				var FType = ReadIdentifier(Reader, Ids, Container) as FunctionType;
				if (FType == null) return null;

				if (IdType == IdentifierType.Function)
					Ret = new Function(Container, Name, FType, null);
				else if (IdType == IdentifierType.ConstructorFunction)
					Ret = new ConstructorFunction(Container, Name, FType, null);
				else throw new Exception("ERROR");
			}

			//-----------------------------------------------------------------------------------
			else if (IdType == IdentifierType.MemberFunction)
			{
				var FType = ReadIdentifier(Reader, Ids, Container) as FunctionType;
				if (FType == null) return null;

				var MRet = new MemberFunction(Container, Name, FType, null);
				MRet.Offset = Reader.ReadInt32();
				Ret = MRet;
			}

			//-----------------------------------------------------------------------------------
			else if (IdType == IdentifierType.RefType || IdType == IdentifierType.PointerType ||
				IdType == IdentifierType.TypeDefType)
			{
				var ChType = ReadIdentifier(Reader, Ids, Container) as Type;
				if (ChType == null) return null;

				if (IdType == IdentifierType.RefType)
					return ChType.CreateRefType(Container, Name);
				else if (IdType == IdentifierType.PointerType)
					return ChType.CreatePointerType(Container, Name);
				else if (IdType == IdentifierType.TypeDefType)
					return new TypeDefType(Container, Name, ChType);
				else throw new Exception("ERROR");
			}

			//-----------------------------------------------------------------------------------
			else if (IdType == IdentifierType.TupleType || IdType == IdentifierType.ClassType ||
				IdType == IdentifierType.ValueType)
			{
				var Members = ReadIdList(Reader, Ids, Container);
				var SRet = (StructuredType)null;

				if (IdType == IdentifierType.TupleType)
					SRet = new TupleType(Container, Name);
				else if (IdType == IdentifierType.ValueType)
					SRet = new ValueType(Container, Name);
				else if (IdType == IdentifierType.ValueType)
					SRet = new ClassType(Container, Name);
				else throw new Exception("ERROR");

				SRet.Members = Members;
				Ret = SRet;
			}

			//-----------------------------------------------------------------------------------
			else if (IdType == IdentifierType.FunctionType)
			{
				var Conv = (CallConv)Reader.ReadByte();
				var RetType = ReadIdentifier(Reader, Ids, Container) as Type;
				if (RetType == null) return null;

				var ParamCount = Reader.ReadUInt16();
				var Params = new VarDeclList();
				for (var i = 0; i < ParamCount; i++)
				{
					var Type = ReadIdentifier(Reader, Ids, Container) as Type;
					if (Type == null) return null;

					var PName = new PString(Reader.ReadString());
					var Decl = new VarDecl(null, null, Type, PName, null);
					Params.Add(Decl);
				}

				Ret = new FunctionType(Container, Name, Conv, RetType, Params);
			}

			//-----------------------------------------------------------------------------------
			else
			{
				throw new Exception("ERROR");
			}

			if (Ret == null) throw new Exception("ERROR");
			Ret.AsmName = AsmName;
			Ret.IsStatic = IsStatic;
			Ret.IsSealed = IsSealed;
			Ids.Add(Ret);
			return Ret;
		}

		static IdentifierList ReadIdList(BinaryReader Reader, List<Identifier> Ids, IdContainer Container)
		{
			var Ret = new IdentifierList();
			var Count = Reader.ReadUInt16();

			for (var i = 0; i < Count; i++)
			{
				var Id = ReadIdentifier(Reader, Ids, Container);
				if (Id == null) return null;
				Ret.Add(Id);
			}

			return Ret;
		}

		static bool LoadLibrary(Stream Stream, IdContainer Container)
		{
			var Reader = new BinaryReader(Stream);
			var Format = Reader.ReadString();
			if (Format != "AsLib") return false;

			var Ids = new List<Identifier>();
			while (true)
			{
				var Comm = (LibraryLoaderCommand)Reader.ReadByte();
				if (Comm == LibraryLoaderCommand.DeclareIds)
				{
					var Res = ReadIdList(Reader, Ids, Container);
					if (Res == null) return false;
					Ids.AddRange(Res);
				}
				else if (Comm == LibraryLoaderCommand.DeclareMembers)
				{
					var Id = GetIdByAsmName(Reader, Ids) as StructuredType;
					if (Id == null) return false;

					var Res = ReadIdList(Reader, Ids, Container);
					if (Res == null) return false;

					if (Id.Members == null) Id.Members = new IdentifierList();
					Id.Members.AddRange(Res);
				}
				else if (Comm == LibraryLoaderCommand.Exit)
				{
					break;
				}
				else
				{
					throw new Exception("ERROR");
				}
			}

			Reader.Close();
			return true;
		}

		static List<StructuredType> SaveIdentifier(BinaryWriter Writer, Identifier Id, bool Root = true)
		{
			var Type = Id as Type;
			if (Type != null && Type.DeclaredType)
			{
				Writer.Write((byte)255);
				Writer.Write(Id.AsmName);
				return null;
			}

			var IdType = GetIdType(Id);
			Writer.Write((byte)IdType);
			Writer.Write(Id.Name.String);
			Writer.Write(Id.AsmName);
			Writer.Write(Id.IsStatic);
			Writer.Write(Id.IsSealed);

			//-----------------------------------------------------------------------------------
			if (Id is Type)
			{
				if (Id is FunctionType)
				{
					var FType = Id as FunctionType;
					Writer.Write((int)FType.Conv);
					var Ret = SaveIdentifier(Writer, FType.RetType, false);

					Writer.Write((ushort)FType.Params.Count);
					foreach (var e in FType.Params)
					{
						if (Ret == null) Ret = SaveIdentifier(Writer, e.Type, false);
						else Ret.AddRange(SaveIdentifier(Writer, e.Type, false));
						Writer.Write(e.Name.String);
					}

					return Ret;
				}

				else if (Id is RefType || Id is PointerType || Id is TypeDefType)
				{
					var Ch = (Type)null;
					if (Id is RefType) Ch = (Id as RefType).Child;
					else if (Id is PointerType) Ch = (Id as PointerType).Child;
					else if (Id is TypeDefType) Ch = (Id as TypeDefType).Child;
					else throw new Exception("ERROR");

					return SaveIdentifier(Writer, Ch, false);
				}

				else if (Id is ChildType)
				{
					return SaveIdentifier(Writer, (Id as ChildType).Child, false);
				}

				else
				{
					throw new Exception("ERROR");
				}
			}

			//-----------------------------------------------------------------------------------
			else if (Id is Variable)
			{
				var Var = Id as Variable;
				SaveIdentifier(Writer, Var.Type, false);
				Writer.Write(Var.ReadOnly);

				if (Id is MemberVariable)
				{
					var MVar = Id as MemberVariable;
					Writer.Write(MVar.Offset);
				}
			}

			//-----------------------------------------------------------------------------------
			else if (Id is Function)
			{
				var Func = Id as Function;
				SaveIdentifier(Writer, Func.Type, false);

				if (Id is MemberFunction)
				{
					var MFunc = Id as MemberFunction;
					Writer.Write(MFunc.Offset);
				}
			}

			//-----------------------------------------------------------------------------------
			else
			{
				throw new Exception("ERROR");
			}

			return null;
		}

		static IdentifierType GetIdType(Identifier Id)
		{
			if (Id is GlobalVariable) return IdentifierType.GlobalVariable;
			else if (Id is MemberVariable) return IdentifierType.MemberVariable;
			else if (Id is Function) return IdentifierType.Function;
			else if (Id is MemberFunction) return IdentifierType.MemberFunction;
			else if (Id is ConstructorFunction) return IdentifierType.ConstructorFunction;
			else if (Id is RefType) return IdentifierType.RefType;
			else if (Id is PointerType) return IdentifierType.PointerType;
			else if (Id is TypeDefType) return IdentifierType.TypeDefType;
			else if (Id is TupleType) return IdentifierType.TupleType;
			else if (Id is ValueType) return IdentifierType.ValueType;
			else if (Id is ClassType) return IdentifierType.ClassType;
			else if (Id is FunctionType) return IdentifierType.FunctionType;
			else throw new Exception("ERROR");
		}

		static List<StructuredType> SaveTypes(BinaryWriter Writer, IdentifierList Ids)
		{
			var Count = 0;
			foreach (var e in Ids)
				if (e.DeclInThis && e is Type) Count++;

			var Ret = new List<StructuredType>();
			Writer.Write((ushort)Count);

			foreach (var e in Ids)
				if (e.DeclInThis && e is Type)
				{
					var Res = SaveIdentifier(Writer, e);
					if (Res == null) continue;

					foreach (var SType in Res)
					{
						Ret.Add(SType);
						Ret.AddRange(SaveTypes(Writer, SType.Members));
					}
				}

			return Ret;
		}

		public static void SaveNonTypes(BinaryWriter Writer, IdentifierList Ids)
		{
			var Count = 0;
			foreach (var e in Ids)
				if (e.DeclInThis && !(e is Type)) Count++;

			Writer.Write((ushort)Count);
			foreach (var e in Ids)
				if (e.DeclInThis && !(e is Type))
				{
					SaveIdentifier(Writer, e);
				}
		}

		public static void SaveLibrary(Stream Stream, IdContainer Container)
		{
			var Writer = new BinaryWriter(Stream);
			Writer.Write("AsLib");

			Writer.Write((byte)LibraryLoaderCommand.DeclareIds);
			var Types = SaveTypes(Writer, Container.Identifiers);
			Writer.Write((byte)LibraryLoaderCommand.DeclareIds);
			SaveNonTypes(Writer, Container.Identifiers);

			foreach (var e in Types)
			{
				Writer.Write((byte)LibraryLoaderCommand.DeclareMembers);
				Writer.Write((byte)255);
				Writer.Write(e.AsmName);
				SaveNonTypes(Writer, e.Members);
			}

			Writer.Close();
		}

		public static bool LoadLibrary(string FileName, IdContainer Container)
		{
			var Strm = File.OpenRead(FileName);
			if (Strm == null) return false;
			var Ret = true;

			try
			{
				Ret = LoadLibrary(Strm, Container);
			}
			catch (Exception e)
			{
				Ret = false;
			}

			Strm.Dispose();
			return Ret;
		}

		public static bool SaveLibrary(string FileName, IdContainer Container)
		{
			var Strm = File.OpenWrite(FileName);
			if (Strm == null) return false;

			SaveLibrary(Strm, Container);
			Strm.Dispose();
			return true;
		}
	}

	public class NewLibraryLoader
	{
		enum LibLoaderCommand
		{
			Typedef,
			Class,
			Struct,
			Members,
			Exit,
		}

		[Flags]
		enum IdFlags
		{
			IsStatic = 1,
			IsSealed = 2,
		}

		enum Types
		{
			ByAsmName,
			Pointer,
			Ref,
			Tuple,
		}

		class LibLoaderType
		{

		}

		public IdContainer Container;
		public IdentifierList Ids;
		public BinaryReader Reader;

		public NewLibraryLoader(IdContainer Container)
		{
			this.Container = Container;
		}

		Identifier GetIdByAsmName(BinaryReader Reader)
		{
			var RAsmName = Reader.ReadString();
			foreach (var e in Ids)
				if (e.AsmName == RAsmName) return e;

			return null;
		}

		Type ReadType()
		{
			var Type = (Types)Reader.ReadByte();
			if (Type == Types.ByAsmName)
			{
				var AsmName = Reader.ReadString();
				return GetIdByAsmName(Reader) as Type;
			}
			else if (Type == Types.Ref || Type == Types.Pointer)
			{
				var Ch = ReadType() as Type;
				if (Ch == null) return null;

				if (Type == Types.Pointer) return Ch.CreatePointerType();
				else return Ch.CreateRefType();
			}
			else if (Type == Types.Tuple)
			{
				var Count = Reader.ReadByte();
				var Members = new IdentifierList();

				for (var i = 0; i < Count; i++)
				{
					var MemberType = ReadType();
					var Name = new PString(Reader.ReadString());
					var V = new MemberVariable(Container, Name, MemberType);
					V.Offset = Reader.ReadInt32();
					Members.Add(V);
				}

				return new TupleType(Container, Members, false);
			}

			return null;
		}

		public bool LoadLibrary(Stream Stream)
		{
			Reader = new BinaryReader(Stream);
			Ids = new IdentifierList();

			try
			{
				var Format = Reader.ReadString();
				if (Format != "AsLib") throw new Exception("ERROR");

				while (true)
				{
					var Comm = (LibLoaderCommand)Reader.ReadByte();
					if (Comm == LibLoaderCommand.Typedef)
					{

					}
					else if (Comm == LibLoaderCommand.Exit)
					{
						break;
					}
				}
			}
			catch (Exception)
			{
				Reader.Close();
				return false;
			}

			Reader.Close();
			Reader = null;
			Ids = null;
			return true;
		}

		public bool LoadLibrary(string FileName)
		{
			var Strm = File.OpenRead(FileName);
			if (Strm == null) return false;
			var Ret = LoadLibrary(Strm);

			Strm.Dispose();
			return Ret;
		}

		public bool SaveLibrary(string FileName, IdContainer Container)
		{
			var Strm = File.OpenWrite(FileName);
			if (Strm == null) return false;

			//SaveLibrary(Strm, Container);
			Strm.Dispose();
			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)

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
Web04 | 2.8.140721.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