Click here to Skip to main content
11,645,499 members (74,514 online)
Click here to Skip to main content
Add your own
alternative version

Bird Programming Language: Part 1

, 1 Jan 2013 GPL3 217.9K 2.4K 153
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;

namespace Anonymus
{
	public class MacroArgPlugIn : ExprPlugIn
	{
		public List<string> Parameters;

		public MacroArgPlugIn(IdContainer Container, List<string> Parameters)
			: base(Container)
		{
			this.Parameters = Parameters;
		}

		public override PlugInRes NewNode(ref ExpressionNode Node)
		{
			if (Node is StrExpressionNode)
			{
				var StrNode = Node as StrExpressionNode;
				if (Parameters != null)
				{
					var Index = Parameters.IndexOf(Node.Code.String);
					if (Index != -1)
					{
						Node = Root.NewNode(new MacroArgNode(Index, Node.Code));
						return PlugInRes.Ready;
					}
				}
			}

			return PlugInRes.Succeeded;
		}
	}

	public class DefinePlugIn : MultiPlugIn
	{
		public DefinePlugIn(IdContainer Container, List<string> Parameters)
			: base(Container)
		{
			AddPlugIn(new MacroArgPlugIn(Container, Parameters));
			AddPlugIn(new PreProcPlugIn(Container, false));
			AddPlugIn(new CalcPlugIn(Container, false));
		}
	}

	public class PreProcPlugIn : ExprPlugIn
	{
		public bool IfDef;

		public PreProcPlugIn(IdContainer Container, bool IfDef)
			: base(Container)
		{
			this.IfDef = IfDef;
		}

		public static IEnumerable<ExpressionNode> GetParams(OpExpressionNode Node)
		{
			for (int i = 1; i < Node.Children.Count; i++)
				yield return Node.Children[i];
		}

		public bool ChkMacroNodes(ExpressionNode Node)
		{
			var RetValue = true;
			var ONode = Node as OpExpressionNode;

			IEnumerable<ExpressionNode> Children = null;
			if (ONode != null && ONode.Operator == Operator.Call) Children = GetParams(ONode);
			else Children = Node.EnumChildren;

			foreach (var e in Children)
				if (e is MacroExpressionNode)
				{
					var m = e as MacroExpressionNode;
					if (m.Macro.Parameters.Count != 0)
					{
						State.Messages.Add(MessageId.ParamCount, m.Code);
						RetValue = false;
					}
				}

			return RetValue;
		}

		public override PlugInRes NewNode(ref ExpressionNode Node)
		{
			if (Node is StrExpressionNode)
			{
				var IdNode = Node as StrExpressionNode;
				var Macro = PreState.GetMacro(IdNode.Code.String);

				if (IfDef)
				{
					Node = Root.NewNode(Expressions.GetBoolValue(Container, Macro != null, Node.Code));
					if (Node == null) return PlugInRes.Failed;
					return PlugInRes.Ready;
				}
				else if (Macro != null)
				{
					if (Macro.Value == null)
					{
						State.Messages.Add(MessageId.MacroWithoutValue, Node.Code);
						return PlugInRes.Failed;
					}

					Macro.Used = true;
					var Params = Macro.Parameters;
					if (Params == null || Params.Count == 0)
						Node = Macro.Value.Copy(State, PlugIn: Root, Begin: false, End: false);
					else Node = Root.NewNode(new MacroExpressionNode(Macro, Node.Code));

					if (Node == null) return PlugInRes.Failed;
					return PlugInRes.Ready;
				}
			}

			if (PreState == null) return PlugInRes.Succeeded;

			if (Node is OpExpressionNode)
			{
				var OpNode = Node as OpExpressionNode;
				var Ch = OpNode.Children;
				var Op = OpNode.Operator;

				if (Op == Operator.Call && Ch[0] is MacroExpressionNode)
				{
					var MFunc = Ch[0] as MacroExpressionNode;
					var Macro = MFunc.Macro;
					Macro.Used = true;

					if (Ch.Count != Macro.Parameters.Count + 1)
					{
						State.Messages.Add(MessageId.ParamCount, Node.Code);
						return PlugInRes.Failed;
					}

					var Nodes = new List<ExpressionNode>();
					var LnkNodes = (List<LinkedExprNode>)null;
					for (var i = 1; i < Ch.Count; i++)
					{
						if (Ch[i] is IdExpressionNode || Ch[i] is ConstExpressionNode)
						{
							Nodes.Add(Ch[i]);
						}
						else
						{
							if (LnkNodes == null)
								LnkNodes = new List<LinkedExprNode>();

							var N = new LinkedExprNode(Ch[i]);
							LnkNodes.Add(N);
							Nodes.Add(new LinkingNode(N, Node.Code));
						}
					}

					ReplaceNodeFunc RFunc = (ChNode) =>
					{
						if (ChNode is MacroArgNode)
						{
							var ArgIndex = (ChNode as MacroArgNode).Index;
							return Nodes[ArgIndex].Copy(State);
						}

						return ChNode;
					};

					Node = Macro.Value.Copy(State, Node.Code, null, RFunc, Begin: false, End: false);
					if (Node == null) return PlugInRes.Failed;
					Node.LinkedNodes = LnkNodes;
					Node = Node.CallNewNode(Root, false, false, false);
					return PlugInRes.Ready;
				}
			}

			if (!ChkMacroNodes(Node)) return PlugInRes.Failed;
			else return PlugInRes.Succeeded;
		}

		public override PlugInRes End(ref ExpressionNode Node)
		{
			if (Node != null && Node is MacroExpressionNode)
			{
				var MNode = Node as MacroExpressionNode;
				State.Messages.Add(MessageId.ParamCount, MNode.Code);
				return PlugInRes.Failed;
			}

			return PlugInRes.Succeeded;
		}
	}

    public class Macro
    {
		public PString Name;
        public ExpressionNode Value;
        public List<string> Parameters;
		public bool Used = false;

        public Macro(PString Name, ExpressionNode Value, List<string> Parameters)
        {
            this.Name = Name;
            this.Value = Value;
            this.Parameters = Parameters;
        }
    }

    public class PreprocessorState
    {
		public MessageCollector Messages;
        public List<Macro> Macroes = new List<Macro>();
        public List<bool> Conditions = new List<bool>();

		public PreprocessorState(MessageCollector Messages)
		{
			this.Messages = Messages;
		}

		public void ChkUnusedMacros()
		{
			foreach (var e in Macroes)
				if (!e.Used) Messages.Add(MessageId.UnusedId, e.Name);
		}

        public void Redefine(Macro m)
        {
            RemoveMacro(m.Name.String);
            Macroes.Add(m);
        }

        public bool IsDefined(string name)
        {
            foreach (var e in Macroes)
                if (e.Name.String == name) return true;

            return false;
        }

        public Macro GetMacro(string name)
        {
            foreach (var e in Macroes)
                if (e.Name.String == name) return e;

            return null;
        }

        public bool RemoveMacro(string name)
        {
            int Pos = -1;
            int Count = Macroes.Count;
            for (int i = 0; i < Count; i++)
            {
                if (Macroes[i].Name.String == name)
                {
                    Pos = i;
                    break;
                }
            }

            if (Pos != -1)
            {
                Macroes.RemoveAt(Pos);
                return true;
            }

            return false;
        }

        public bool IsInDefBlock()
        {
            foreach (var e in Conditions)
                if (!e) return false;

            return true;
        }

        public void AddCondition(bool Value)
        {
            Conditions.Add(Value);
        }

        public bool DoElse()
        {
            int l = Conditions.Count - 1;
            if (l < 0) return false;

            Conditions[l] = !Conditions[l];
            return true;
        }

        public bool DoEndif()
        {
            int l = Conditions.Count - 1;
            if (l < 0) return false;

            Conditions.RemoveAt(l);
            return true;
        }

        public PreprocessorState NewState()
        {
			var Ret = new PreprocessorState(Messages);
            foreach (var e in Macroes)
                Ret.Macroes.Add(e);

            return Ret;
        }

        public bool CheckConditions()
        {
            if (Conditions.Count > 0)
            {
				Messages.Add(MessageId.NoEndif);
                return false;
            }

            return true;
        }
    }

    public enum PreprocessorRes
    {
        Succeed,
        Failed,
        NotPreprocLine,
    }

    public class Preprocessor
    {
		public PreprocessorState PreState;
		public CompilerState State;
		public IdContainer Container;

		public Preprocessor(ScopeNode Container)
		{
			this.PreState = Container.PreState;
			this.State = Container.State;
			this.Container = Container;
		}

		Macro ProcMacro(PString MLine)
		{
			var MacroName = MLine.Word();
			if (!Helper.IsValidIdentifierName(MacroName.String))
			{
				PreState.Messages.Add(MessageId.NotValidName, MacroName);
				return null;
			}

			PString Params = null;
			var zp = BracketHelper.ZPos(MLine.String);
			if (zp > 0)
			{
				Params = MLine.Substring(1, zp - 1);
				MLine = MLine.Substring(zp + 1).Trim();
			}

			List<string> ParamList = null;
			if (Params != null)
			{
				var PStrList = new List<PString>();
				var Recognizer = State.Language.ArgRecognizer;
				if (!Recognizer.SplitArgs(State, Params, PStrList))
					return null;

				ParamList = new List<string>();
				foreach (var e in PStrList)
				{
					if (!Helper.IsValidIdentifierName(e.String))
					{
						PreState.Messages.Add(MessageId.WrongParamList, e);
						return null;
					}

					if (ParamList.Contains(e.String))
					{
						PreState.Messages.Add(MessageId.IdAlreadyDefined, e);
						return null;
					}

					ParamList.Add(e.String);
				}
			}

			MLine.TrimThis();
			if (MLine.StrLen > 0)
			{
				var PlugIn = new DefinePlugIn(Container, ParamList);
				var Node = Expressions.NewExpressionTree(MLine, PlugIn, true);

				if (Node != null)
					return new Macro(MacroName, Node, ParamList);
				else return null;
			}
			
			return new Macro(MacroName, null, ParamList);
		}

		private PreprocessorRes ProcCommands(string Order, PString Line, PString MLine)
		{
			PString Name;
			var ReDef = false;

            switch (Order)
            {
                case "error":
                    PreState.Messages.Add(MessageId.PreprocError, MLine);
                    return PreprocessorRes.Failed;

                case "warning":
                    PreState.Messages.Add(MessageId.PreprocWarning, MLine);
                    return PreprocessorRes.Succeed;

                case "info":
                    PreState.Messages.Add(MessageId.PreprocInfo, MLine);
                    return PreprocessorRes.Succeed;

                case "redef":
                    ReDef = true;
                    goto case "define";

                case "define":
					var Macro = ProcMacro(MLine);
					if (Macro != null)
					{
						Name = Macro.Name;

						if (!ReDef && PreState.IsDefined(Name.String))
						{
							PreState.Messages.Add(MessageId.MacroAlreadyDefined, Name);
							return PreprocessorRes.Failed;
						}

						PreState.Redefine(Macro);
						return PreprocessorRes.Succeed;
					}
					else
					{
						return PreprocessorRes.Failed;
					}

                case "undef":
                    Name = MLine.Word();
                    if (!Helper.IsValidIdentifierName(Name.String))
                    {
                        PreState.Messages.Add(MessageId.NotValidName, Name);
                        return PreprocessorRes.Failed;
                    }

                    if (!PreState.RemoveMacro(Name.String))
                    {
                        PreState.Messages.Add(MessageId.NoMacro, Name);
                        return PreprocessorRes.Failed;
                    }

                    if (MLine.String != "") PreState.Messages.Add(MessageId.ParamCount, MLine);
                    return PreprocessorRes.Succeed;

				default:
					PreState.Messages.Add(MessageId.UnknownCommand, Line);
					return PreprocessorRes.Failed;
			}
		}

        public PreprocessorRes PreprocessLine(PString Line)
        {
            ExpressionNode Node;
            var InDefBlock = PreState.IsInDefBlock();

            Line = Line.Trim();
            if (Line.String[0] == '#')
            {
				var MLine = Line.TrimmedSubstring(1);
				var Order = MLine.Word();

                bool IfDef = false;

				switch (Order.String)
				{
                    case "ifdef":
                        IfDef = true;
                        goto case "if";

                    case "if":
                        if (InDefBlock)
                        {
							var PlugIn = new GlobalPlugIn(Container);
							PlugIn.GetPlugIn<TypeMgrPlugIn>().RetType = Container.GlobalScope.BoolType;
							PlugIn.GetPlugIn<PreProcPlugIn>().IfDef = IfDef;
							PlugIn.GetPlugIn<CalcPlugIn>().Const = true;

							Node = Expressions.NewExpressionTree(MLine, PlugIn, true);
                            if (Node != null)
                            {
                                var CNode = Node as ConstExpressionNode;
								PreState.AddCondition(CNode.Bool);
								return PreprocessorRes.Succeed;
                            }
                            else
                            {
                                PreState.AddCondition(true);
                                return PreprocessorRes.Failed;
                            }
                        }
                        
						PreState.AddCondition(true);
                        break;

                    case "elif":
                        if (!PreState.DoElse())
                        {
                            PreState.Messages.Add(MessageId.NoMatchingIf, Line);
                            return PreprocessorRes.Failed;
                        }

                        goto case "if";

                    case "else":
                        if (!PreState.DoElse())
                        {
                            PreState.Messages.Add(MessageId.NoMatchingIf, Line);
                            return PreprocessorRes.Failed;
                        }

                        break;

                    case "endif":
                        if (!PreState.DoEndif())
                        {
                            PreState.Messages.Add(MessageId.NoMatchingIf, Line);
                            return PreprocessorRes.Failed;
                        }

                        break;

                    default:
						if (InDefBlock) return ProcCommands(Order.String, Line, MLine);

						State.Messages.Add(MessageId.UnknownCommand, Line);
						return PreprocessorRes.Failed;
                }

                return PreprocessorRes.Succeed;
            }

            return InDefBlock ? PreprocessorRes.NotPreprocLine : PreprocessorRes.Succeed;
        }
    }
}

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.

You may also be interested in...

| Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.150731.1 | Last Updated 1 Jan 2013
Article Copyright 2011 by Dávid Kocsis
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid