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

namespace Anonymus
{
	public abstract class InnerScopeRecognizer
	{
		public abstract PString[] GetInnerScope(ScopeNode Scope, PString Line, int Index, bool Mod = true, object Data = null);
	}

	public abstract class DeclarationRecognizer
	{
		public abstract bool Recognize(ProcessableScope Scope);
	}

	public delegate bool OnVarCreatedFunc(Variable Var);

	public abstract class ScopeNode : IdContainer
	{
		public PString[] Source;

		public ScopeNode(IdContainer Parent, PString[] Source)
			: base(Parent)
		{
			this.Source = Source;
		}

		public int CalcSourceLength()
		{
			if (Source == null) return 0;

			var Ret = 0;
			foreach (var e in Source)
				if (e != null) Ret += e.StrLen;

			return Ret;
		}

		public virtual void GetMTProcIds(List<Identifier> Ids)
		{
		}

		public override IEnumerable<IdContainer> EnumChildren
		{
			get
			{
				foreach (var Id in Identifiers)
				{
					var Func = Id as Function;
					if (Func != null && Func.FunctionScope != null)
						yield return Func.FunctionScope;
				}

				foreach (var e in base.EnumChildren)
					yield return e;
			}
		}

		public PString[] InnerScope(PString Line, int Index, bool Mod = true)
		{
			return State.Language.InnerScopeRecognizer.GetInnerScope(this, Line, Index, Mod);
		}

		public PString[] FInnerScope(PString Command, PString Line, int Index, bool Mod = true, bool Warning = true)
		{
			var Inner = InnerScope(Line, Index, Mod);
            if (Inner == null) return null;

            if (Inner.Length == 0 && Warning)
			{
				State.Messages.Add(MessageId.EmptyScope, Command);
				return null;
			}

			return Inner;
		}

		public virtual void GetAsmCode(Compiler Comp, GetAsmCodeMode Mode)
		{
			foreach (var Id in Identifiers)
				if (Id.DeclInThis) Id.GetAsmCode(Comp, Mode);
		}

	}

	public abstract class ProcessableScope : ScopeNode
	{
		public TypeDeclList TypeDeclarations = new TypeDeclList();

		public bool DeclareTypes()
		{
			var RetValue = TypeDeclarations.AddFromString(this) && TypeDeclarations.Declare();
			foreach (var e in TypeDeclarations.DeclaredScopes())
				if (!e.DeclareTypes()) RetValue = false;

			return RetValue;
		}

		public virtual void GetTypeDeclarations(TypeDeclList List, int Depth = 0)
		{
			List.AddRange(TypeDeclarations);

			var NDepth = Depth + 1;
			foreach (var e in TypeDeclarations.DeclaredScopes())
				e.GetTypeDeclarations(List, NDepth);
		}

		public virtual TypeDeclList GetTypeDeclarations()
		{
			var Ret = new TypeDeclList();
			GetTypeDeclarations(Ret);
			return Ret;
		}

		public virtual bool ProcessScope()
		{
			return State.Language.DeclarationRecognizer.Recognize(this);
		}

		public ProcessableScope(IdContainer Parent, PString[] Source)
			: base(Parent, Source)
		{
		}

		public virtual bool OnUnTypedFuncDecl(ModifierList Mods, PString Name,
			VarDeclList Params, PString[] Inner)
		{
			State.Messages.Add(MessageId.Unnamed, Name);
			return false;
		}

		public virtual bool OnTypedFuncDecl(ModifierList Mods, PString TypeName,
			PString Name, VarDeclList Params, PString[] Inner)
		{
			return DeclareFunction(TypeName, Name, Params, Inner, Mods) != null;
		}
	}

	public class ImportScopeNode : ExternScopeNode
	{
		public string Library;
		public string Name;

		public ImportScopeNode(IdContainer Parent, string Library, PString[] Source)
			: base(Parent, Source)
		{
			this.Library = Library;
		}
	}

	public class ExternScopeNode : ProcessableScope
	{
		public ExternScopeNode(IdContainer Parent, PString[] Source)
			: base(Parent, Source)
		{
		}

		public override Variable CreateVariable(PString Name, Type Type, ModifierList Mods = null)
		{
			throw new NotImplementedException();
		}

		public override Function DeclareFunction(Type RetType, PString Name, VarDeclList Params,
			PString[] Inner, ModifierList Mods = null, Function Func = null)
		{
			var Type = new FunctionType(this, Name, CallConv.AsCall, RetType, Params);
			Type.SetUsed();

			if (Inner.Length > 0)
			{
				State.Messages.Add(MessageId.FuncCannotHaveInnerScope, Name);
				return null;
			}

			return DeclareFunction(Type, Name, null, Mods, Func);
		}
	}

	public abstract class GlobalScopeProcessor
	{
		public abstract bool Process(GlobalScopeNode Scope);
	}

	public class GlobalScopeNode : ProcessableScope
	{
		CompilerState _State;
		PreprocessorState _PreState;

		public override CompilerState State
		{
			get { return _State; }
		}

		public override PreprocessorState PreState
		{
			get { return _PreState; }
		}

		public List<ImportScopeNode> ImportScopes;
		public List<ExternScopeNode> ExternScopes;
		public List<GlobalVariable> ExprConsts = new List<GlobalVariable>();
		public List<Type> BasicTypes = new List<Type>();
		public List<NumberType> NumTypes = new List<NumberType>();
		public BoolType BoolType;
		public VoidType VoidType;
		public AutomaticType AutoType;
		public ConstStringType CStrType;
		public TypeOfType TypeOfType;

		public override IEnumerable<IdContainer> EnumChildren
		{
			get
			{
				if (ImportScopes != null)
					foreach (var e in ImportScopes)
						yield return e;

				if (ExternScopes != null)
					foreach (var e in ExternScopes)
						yield return e;

				foreach (var e in base.EnumChildren)
					yield return e;
			}
		}

		public override T GetIdRec<T>(PString Name, GetIdMode Mode = GetIdMode.Everywhere,
			bool SearchedInBasicTypes = false)
		{
			if (ImportScopes != null)
				foreach (var e in ImportScopes)
				{
					var Ret = e.Identifiers.GetIdRec<T>(Name.String);
					if (Ret != null) return Ret;
				}

			if (ExternScopes != null)
				foreach (var e in ExternScopes)
				{
					var Ret = e.Identifiers.GetIdRec<T>(Name.String);
					if (Ret != null) return Ret;
				}

			return base.GetIdRec<T>(Name, Mode, SearchedInBasicTypes);
		}

		public override void GetMTProcIds(List<Identifier> Ids)
		{
			Func<Identifier, bool> Func = x => x is Function || x is Variable;
			Ids.AddRange(Identifiers.Where(Func));
		}

		public GlobalVariable CreateExprConst(ConstData Value, Type Type)
		{
			foreach (var e in ExprConsts)
				if (e.Type == Type && Value.IsEqual(e.GlbInitVal))
					return e;

			var Name = new PString(State.NextLabelIndex.ToString());
			var Ret = CreateVariable(Name, Type) as GlobalVariable;
			Ret.GlbInitVal = Value;
			ExprConsts.Add(Ret);
			return Ret;
		}

		public GlobalVariable CreateExprConst(ConstExpressionNode Node)
		{
			return CreateExprConst(Node.Value, Node.Type);
		}

		public ExpressionNode CreateConstRId(ConstExpressionNode ConstNode, ExprPlugIn PlugIn)
		{
			var Glb = CreateExprConst(ConstNode);
			var RIdNode = new IdExpressionNode(Glb, ConstNode.Code);
			return PlugIn.NewNode(RIdNode);
		}

		public Function GetEntry()
		{
			if (!State.HasMainFunc) return null;

			var Fun = GlobalScope.GetId<Function>(new PString(State.Entry), EnableMessages: false);
			if (Fun == null)
			{
				State.Messages.Add(MessageId.EntryNotFound, (PString)null, State.Entry);
				return null;
			}

			return Fun;
		}

		public override Variable CreateVariable(PString Name, Type Type, ModifierList Mods = null)
		{
			var Ret = new GlobalVariable(this, Name, Type);
			if (Mods != null && !Mods.Apply(this, Ret)) return null;
			State.Arch.OnNewIdentifier(Ret);
			return Ret;
		}

		public bool ProcessTypes(MTCompiler C)
		{
			var RetValue = DeclareTypes();
			var Decls = GetTypeDeclarations();
			if (!Decls.ResolveTypedefs()) RetValue = false;

			if (!ProcessScope()) RetValue = false;
			else GetMTProcIds(C.Identifiers);

			foreach (var e in Decls.DeclaredScopes())
			{
				if (!e.ProcessScope()) RetValue = false;
				else e.GetMTProcIds(C.Identifiers);
			}

			return RetValue;
		}

		public bool Process()
		{
			if (!State.Language.GlobalScopeProcessor.Process(this))
				return false;

			var Compiler = new MTCompiler(State, this);
			if (!ProcessTypes(Compiler)) return false;
			if (!Compiler.Run()) return false;

			ChkUnusedIds();
			PreState.ChkUnusedMacros();
			return true;
		}

		public GlobalScopeNode(PreprocessorState PreState, CompilerState State, PString[] Source)
			: base(null, Source)
		{
			this._PreState = PreState;
			this._State = State;

			// ----------------------------------------------------------------------------------------------------
			NumTypes.Add(new SignedType(this, new PString("sbyte"), 1, "sb") { DeclInThis = false, DeclaredType = true });
			NumTypes.Add(new SignedType(this, new PString("short"), 2, "s") { DeclInThis = false, DeclaredType = true });
			NumTypes.Add(new SignedType(this, new PString("int"), 4, "i") { DeclInThis = false, DeclaredType = true });
			NumTypes.Add(new SignedType(this, new PString("long"), 8, "l") { DeclInThis = false, DeclaredType = true });

			NumTypes.Add(new UnsignedType(this, new PString("byte"), 1, "b") { DeclInThis = false, DeclaredType = true });
			NumTypes.Add(new UnsignedType(this, new PString("ushort"), 2, "us") { DeclInThis = false, DeclaredType = true });
			NumTypes.Add(new UnsignedType(this, new PString("uint"), 4, "u") { DeclInThis = false, DeclaredType = true });
			NumTypes.Add(new UnsignedType(this, new PString("ulong"), 8, "ul") { DeclInThis = false, DeclaredType = true });

			NumTypes.Add(new FloatType(this, new PString("float"), 4, "f") { DeclInThis = false, DeclaredType = true });
			NumTypes.Add(new FloatType(this, new PString("double"), 8, "d") { DeclInThis = false, DeclaredType = true });

			BoolType = new BoolType(this, new PString("bool"), 1, 1UL, 0UL) { DeclInThis = false, DeclaredType = true };
			VoidType = new VoidType(this, new PString("void")) { DeclInThis = false, DeclaredType = true };
			CStrType = new ConstStringType(this, new PString("string")) { DeclInThis = false, DeclaredType = true };
			AutoType = new AutomaticType(this, new PString("var")) { DeclInThis = false, DeclaredType = true };
			TypeOfType = new TypeOfType(this, new PString("type")) { DeclInThis = false, DeclaredType = true };

			// ----------------------------------------------------------------------------------------------------
			BasicTypes.AddRange(NumTypes);
			BasicTypes.Add(BoolType);
			BasicTypes.Add(VoidType);
			BasicTypes.Add(AutoType);
			//BasicTypes.Add(TypeOfType);

			State.Arch.OnNewContainer(this);
		}

		public override void GetAsmCode(Compiler Comp, GetAsmCodeMode Mode)
		{
			base.GetAsmCode(Comp, Mode);

			if (Mode == GetAsmCodeMode.Data)
			{
				foreach (var e in ExprConsts)
				{
					Comp.Label(e.AsmName);
					Comp.Declare(e.Type, e.GlbInitVal);
				}

				foreach (var Scope in TypeDeclarations.DeclaredScopes())
					Scope.GetAsmCode(Comp, Mode);
			}
		}

		public StringBuilder GetExternCode()
		{
			var Ret = new StringBuilder();
			if (ExternScopes != null)
			{
				foreach (var Scope in ExternScopes)
					foreach (var Id in Scope.Identifiers)
						if (Id.Used) Ret.Append("\textrn " + Id.AsmName + "\n");
			}

			return Ret;
		}

		public StringBuilder GetImportCode()
		{
			var Ret = new StringBuilder();
			var Index = 0;

			var Lst = new StringBuilder();
			if (ImportScopes != null)
			{
				foreach (var Scope in ImportScopes)
				{
					Index++;
					Scope.Name = "dll" + Index.ToString();

					if (Index > 1) Lst.Append(", \\\n\t        ");
					Lst.Append(Scope.Name + ", \"" + Scope.Library + "\"");
				}
			}

			if (Lst.Length > 0)
			{
				Ret.Append("\tlibrary ");
				Ret.Append(Lst);
				Ret.Append("\n\n");

				foreach (var Scope in ImportScopes)
				{
					Lst = new StringBuilder();
					Index = 0;

					foreach (var Id in Scope.Identifiers)
					{
						Index++;
						if (Index > 1) Lst.Append(", \\\n\t       ");
						Lst.Append(Id.AsmName + ", \"" + Id.Name + "\"");
					}

					if (Lst.Length > 0)
					{
						Ret.Append("\timport " + Scope.Name);
						Ret.Append(", \\\n\t       ");
						Ret.Append(Lst);
						Ret.Append("\n\n");
					}
				}
			}

			return Ret;
		}
	}

}

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