Click here to Skip to main content
11,580,580 members (72,174 online)
Click here to Skip to main content
Add your own
alternative version

Bird Programming Language: Part 1

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

namespace Anonymus
{
	public delegate void CodeCondBranchFunc(Compiler Compiler);

	public abstract class CondBranch
	{
	}

	public class JumpCodeBranch : CondBranch
	{
		public int Label;

		public JumpCodeBranch(int Label)
		{
			this.Label = Label;
		}
	}

	public class CodeCondBranch : CondBranch
	{
		public CodeCondBranchFunc GetCode;

		public CodeCondBranch(CodeCondBranchFunc GetCode)
		{
			this.GetCode = GetCode;
		}
	}

	public abstract class Instruction
	{
		public bool Skip = false;
		public abstract string GetCode(Compiler Comp);
	}

	public class LabelInstruction : Instruction
	{
		public int Label;

        public LabelInstruction(int Label)
		{
			this.Label = Label;
		}

		public override string GetCode(Compiler Comp)
		{
			return Comp.LabelStr("_" + Label);
		}
	}

	public abstract class JumpInstruction : Instruction
	{
		public int Label;

		public JumpInstruction(int Label)
		{
			this.Label = Label;
		}

		public override string GetCode(Compiler Comp)
		{
			return Comp.JumpStr("_" + Label);
		}
	}

    public class ReplaceableJumpInstruction : JumpInstruction
    {
        public ReplaceableJumpInstruction(int Label)
            : base(Label)
        {
        }
    }

	public class StrInstruction : Instruction
	{
		public string Instruction;

		public StrInstruction(string Instruction)
		{
			this.Instruction = Instruction;
		}

		public override string GetCode(Compiler Comp)
		{
			return Instruction;
		}
	}

    public delegate void ReplaceJumpsFunc(Compiler Compiler);

	public abstract class Compiler
	{
		public CompilerState State;
        public IdContainer Container;

		public List<Instruction> Instructions = new List<Instruction>();
		public Dictionary<int, int> LabelPositions;
        public Dictionary<int, ReplaceJumpsFunc> ReplaceJumps;

		public abstract string JumpStr(string Label);
		public abstract string LabelStr(string Label);
		public abstract void GetExprCode(ExpressionNode Node);

		public abstract void GetConditionCode(ExpressionNode Condition, CondBranch Then,
			CondBranch Else, bool ThenAllPathJumpAway = false, bool LinkedNodesCalced = false,
			int NextLabel = -1);

		public abstract void CalcCondAsmCode(ExpressionNode Node, int Then, int Else,
			Operator PrewOp, bool Last, bool LinkedNodesCalced = false);

		public abstract void DeclareLabelPtr(string Label);
		public abstract void DeclareLabelPtr(int Label);
		public abstract void Declare(Type Type, ConstData Data);
		public abstract void DeclareUnknownBytes(int Count);
		public abstract bool Compile(CompilerState State, PString[] Lines);

		public Compiler(CompilerState State)
		{
			this.State = State;
		}

		public void Jump(int Label)
		{
			Instructions.Add(new ReplaceableJumpInstruction(Label));
		}

		public void Label(int Label, bool Chk = true)
		{
            if (LabelPositions == null) LabelPositions = new Dictionary<int, int>();
			LabelPositions.Add(Label, Instructions.Count);

			var Ins = new LabelInstruction(Label);
			if (!Chk) Ins.Skip = false;
			Instructions.Add(Ins);
		}

        public void SetJumpReplacing(int Label, ReplaceJumpsFunc Compiler)
        {
            if (ReplaceJumps == null) ReplaceJumps = new Dictionary<int, ReplaceJumpsFunc>();
            ReplaceJumps.Add(Label, Compiler);
        }

		public void Jump(string Label)
		{
			Instructions.Add(new StrInstruction(JumpStr(Label)));
		}

		public void Label(string Label)
		{
			Instructions.Add(new StrInstruction(LabelStr(Label)));
		}

        public virtual void InitJumpOptimization()
        {
            foreach (var e in Instructions)
                e.Skip = e is LabelInstruction;
        }

		public virtual void OptimizeJumps()
		{
            InitJumpOptimization();
            for (var i = 0; i < Instructions.Count; i++)
            {
                var JumpIns = Instructions[i] as JumpInstruction;
                if (JumpIns != null && !JumpIns.Skip)
                {
                    JumpIns.Label = JumpsTo(JumpIns.Label);

                    var SkipPos = i + 1;
                    for (; SkipPos < Instructions.Count; SkipPos++)
                    {
                        var SIns = Instructions[SkipPos];
                        var SLabelIns = SIns as LabelInstruction;
                        if (SLabelIns != null && SLabelIns.Label != JumpIns.Label)
                            continue;

                        var SJumpIns = SIns as JumpInstruction;
                        if (SJumpIns == null || JumpIns.Label != SJumpIns.Label) break;
                        else SIns.Skip = true;
                    }

                    var LblIndex = LabelPositions[JumpIns.Label];
                    if (SkipPos == LblIndex) JumpIns.Skip = true;
                    else Instructions[LblIndex].Skip = false;
                }
            }
		}

        public virtual void Optimize()
        {
            OptimizeJumps();

            if (ReplaceJumps != null)
            {
                var New = State.Arch.CreateCompiler(State, Container);
                var Count = 0;

                for (var i = 0; i < Instructions.Count; i++)
                {
                    var Ins = Instructions[i];
                    var JumpIns = Ins as ReplaceableJumpInstruction;
                    if (!Ins.Skip && JumpIns != null && ReplaceJumps.ContainsKey(JumpIns.Label))
                    {
                        ReplaceJumps[JumpIns.Label](New);
                        Count++;
                        continue;
                    }

                    New.Append(Ins);
                }

                if (Count > 0)
                {
                    Set(New);
                    Optimize();
                }
            }
        }

        void Set(Compiler Compiler)
        {
            Instructions = Compiler.Instructions;
            LabelPositions = Compiler.LabelPositions;
        }

        public void Insert(int Index, Compiler Comp)
        {
            var P = Comp != null ? Comp.Instructions.Count - 1 : 0;
            if (LabelPositions != null)
            {
                foreach (var e in LabelPositions.Keys.ToArray())
                    if (LabelPositions[e] >= Index) LabelPositions[e] += P;
            }

            if (P > 0)
            {
                Instructions.InsertRange(Index, Comp.Instructions);
                if (Comp.LabelPositions != null)
                {
                    foreach (var e in Comp.LabelPositions)
                        LabelPositions.Add(e.Key, e.Value + Index);
                }
            }
        }

		public int JumpsTo(int Label)
		{
			var InsIndex = LabelPositions[Label];
			for (var i = InsIndex; i < Instructions.Count; i++)
			{
				var Ins = Instructions[i];
				if (Ins is JumpInstruction)
				{
					var JumpIns = Ins as JumpInstruction;
					return JumpsTo(JumpIns.Label);
				}
				else if (Ins is LabelInstruction)
					continue;
				else break;
			}

			return Label;
		}

		public string GetAssembly()
		{
			var Ret = new StringBuilder();
			foreach (var Ins in Instructions)
				if (!Ins.Skip) Ret.Append(Ins.GetCode(this));

			return Ret.ToString();
		}

		public void Append(Compiler Comp)
		{
			var Count = Instructions.Count;
			Instructions.AddRange(Comp.Instructions);

            if (Comp.LabelPositions != null)
            {
                foreach (var e in Comp.LabelPositions)
                    LabelPositions.Add(e.Key, e.Value + Count);
            }
		}

		public void Append(Instruction Ins)
		{
			if (Ins is LabelInstruction)
			{
				var LblIns = Ins as LabelInstruction;
                if (LabelPositions == null) LabelPositions = new Dictionary<int, int>();
				LabelPositions.Add(LblIns.Label, Instructions.Count);
			}

			Instructions.Add(Ins);
		}

		public void Append(string Str)
		{
			Instructions.Add(new StrInstruction(Str));
		}

		public void Append(StringBuilder Str)
		{
			Instructions.Add(new StrInstruction(Str.ToString()));
		}
	}

	public abstract class Architecture
	{
		public abstract ExprPlugIn CreatePlugIn(IdContainer Container);
		public abstract Compiler CreateCompiler(CompilerState State);
		public abstract FuncScopeNode CreateFuncScope(IdContainer Parent, PString Name, FunctionType Type, bool IsStatic, PString[] Source);
		public abstract void OnNewContainer(IdContainer Container);
		public abstract void OnNewIdentifier(Identifier Id);
		public abstract void CalcExprDataPos(CompilerState State, IdContainer Container, ExpressionNode Node);
		public abstract int GetAlign(Type Type);
		public abstract CondBranch[] GetBraches(Compiler Compiler, GlobalScopeNode Global,
			Command Then, Command Else, ref ExpressionNode Condition);

		public abstract int RegSize { get; }
		public abstract int RegCount { get; }
		public abstract int MaxStructPow2Size { get; }

		public Compiler CreateCompiler(CompilerState State, IdContainer Container)
		{
			var Ret = CreateCompiler(State);
            Ret.Container = Container;
			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.

You may also be interested in...

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