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

namespace Anonymus
{
	public class CodeFile
	{
		public string FilePath;
		public string[] Lines;

        public CodeFile(string FilePath)
		{
            FilePath = Path.GetFullPath(FilePath);

            this.FilePath = FilePath;
            Lines = File.ReadAllLines(FilePath);
		}
	}
	
	public class Settings
	{
        public string Architecture;
        public string Debug;
        public string Format;
        public string Entry;

		public Settings(BinaryReader br)
		{
            Architecture = br.ReadString();
            Debug = br.ReadString();
		}

        public Settings()
        {
            Architecture = "x86";
            Debug = "false";
            Format = "console";
            Entry = "Main";
        }

		public void Save(BinaryWriter bw)
		{
            bw.Write(Architecture);
			bw.Write(Debug);
		}
	}

	public class Project
	{
		public static Encoding Encoding = new UnicodeEncoding();

		public string FileName;
		public List<CodeFile> CodeFiles;
		public Settings Settings;

		public string ProjDir
		{
			get { return Path.GetDirectoryName(FileName); }
		}

        public string OutName
        {
            get
            {
                var Ret = Path.GetFileNameWithoutExtension(FileName);
                if (Settings.Debug == "true") Ret += ".debug";
                else if (Settings.Debug == "false") Ret += ".release";
                Ret += "." + Settings.Architecture;
                return Ret;
            }
        }

		public Project()
		{
			FileName = "";
			CodeFiles = new List<CodeFile>();
            Settings = new Settings();
		}

		public Project(string FileName)
		{
			this.FileName = FileName;

			using (var br = new BinaryReader(File.OpenRead(FileName), Encoding))
			{
				var Count = br.ReadInt32();
				CodeFiles = new List<CodeFile>();

				for (int i = 0; i < Count; i++)
					CodeFiles.Add(new CodeFile(br.ReadString()));

				Settings = new Settings(br);
			}
		}

		public void Save(string Path = null)
		{
            if (Path != null) FileName = Path;

            if (File.Exists(FileName))
                File.Delete(FileName);

            using (var bw = new BinaryWriter(File.OpenWrite(FileName), Encoding))
			{
				bw.Write(CodeFiles.Count);
				foreach (var e in CodeFiles)
                    bw.Write(e.FilePath);

				Settings.Save(bw);
			}
		}

		public void SetSetting(string Name, string Value)
		{
            Name = Name.ToLower();
			var Type = typeof(Settings);
			var Ret = Type.GetField(Name);

            if (Ret == null)
            {
                Console.WriteLine("Unknown settings");
            }
            else
            {
                if (Name != "entry") Value = Value.ToLower();
                Ret.SetValue(Settings, Value);
            }
		}

		public PString[] GetLines()
		{
			if (CodeFiles.Count == 0)
				return null;

			var Ret = (IEnumerable<PString>)null;
			foreach (var e in CodeFiles)
			{
                var Ps = Helper.StrToPStrArr(e.FilePath, e.Lines);
				if (Ret == null) Ret = Ps;
				else Ret = Ret.Union(Ps);
			}

			return Ret != null ? Ret.ToArray() : null;
		}

        public void Assemble(CompilerState State, string AsmCode)
        {
            var OutDir = ProjDir;
            var s = Path.DirectorySeparatorChar;

            if (!(OutDir.Length > 0 && OutDir[OutDir.Length - 1] == s))
                OutDir += Path.DirectorySeparatorChar;

            OutDir += ".as";
            if (!Directory.Exists(OutDir))
                Directory.CreateDirectory(OutDir);

            var oName = OutDir + Path.DirectorySeparatorChar + OutName;
            var AsmFile = oName + ".s";
            File.WriteAllText(AsmFile, AsmCode);
            
            var StartInfo = new ProcessStartInfo("fasm.exe", AsmFile);
            StartInfo.UseShellExecute = false;
            var Proc = Process.Start(StartInfo);

            /*
            StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
            StartInfo.RedirectStandardError = true;
            StartInfo.RedirectStandardOutput = true;

            var Proc = Process.Start(StartInfo);

            while (!Proc.HasExited)
            {
                Thread.Sleep(10);
                Console.WriteLine(Proc.StandardOutput.ReadToEnd());
                Console.WriteLine(Proc.StandardError.ReadToEnd());
            }
            */
        }

        public bool ApplySettings(CompilerState State)
        {
			var RetValue = true;
            var SErrs = new List<string>();

			if (Settings.Architecture == "x86") State.Arch = new x86.x86Architecture(false);
			else if (Settings.Architecture == "x64") State.Arch = new x86.x86Architecture(true);
			else SErrs.Add("Architecture = " + Settings.Architecture);

			if (Settings.Format == "gui") State.Format = ImageFormat.GUI;
			else if (Settings.Format == "console") State.Format = ImageFormat.Console;
            else if (Settings.Format == "dll") State.Format = ImageFormat.DLL;
            else if (Settings.Format == "asdll") State.Format = ImageFormat.AsDLL;
            else SErrs.Add("Format = " + Settings.Format);

			if (Settings.Entry == "" && (State.Format == ImageFormat.GUI ||
				State.Format == ImageFormat.Console))
			{
				State.Messages.Add(MessageId.EntryNotSpecified, (PString)null);
				RetValue = false;
			}
			else
			{
				State.Entry = Settings.Entry;
			}

            if (SErrs.Count > 0)
            {
                foreach (var e in SErrs)
                    State.Messages.Add(MessageId.WrongSettings, (PString)null, e);

                return false;
            }

            return RetValue;
        }

		public void Compile(CompilerState State = null, bool Asm = true)
		{
			if (State == null) State = new CompilerState(null, null);
			else State.Messages.Messages = new List<Message>();

            if (ApplySettings(State))
            {
                var Lines = GetLines();
                if (Lines != null)
                {
					var Compiler = State.Arch.CreateCompiler(State);
                    Compiler.Compile(State, Lines);
					var AsmCode = Compiler.GetAssembly().Replace("\n", "\r\n");
                    if (AsmCode != null && Asm) Assemble(State, AsmCode.ToString());
                }
                else
                {
                    Console.WriteLine("Nothing to compile");
                    return;
                }
            }

			State.Messages.WriteToConsole();
		}
	}

	public class Program
	{
        public static void MakeFTest()
        {
            if (File.Exists("Txt\\FTest.txt"))
                File.Delete("Txt\\FTest.txt");

            var sw = new StreamWriter("Txt\\FTest.txt");
            sw.WriteLine("#define Func(x, y, z) (x + y) * z");
            sw.WriteLine();

            var LineCount = 2;
            var i = 0;

            while (LineCount < 10000)
            {
				var c = i > 0 ? i.ToString() : "";
                sw.WriteLine("void Main" + c + "()");
                sw.WriteLine("\tint x = Func(1, 2, 3),");
                sw.WriteLine("\t    y = Func(2, 3, 4),");
                sw.WriteLine("\t    z = Func(5, 6, 7)");
                sw.WriteLine();
                sw.WriteLine("\tint w = x + y + z");
                sw.WriteLine();
                sw.WriteLine("\tfor int i = 0 until x");
				sw.WriteLine("\t\tfor int j = 0 until y");
				sw.WriteLine("\t\t\tfor int k = 0 until z");
                sw.WriteLine("\t\t\t\tif i > j and i > k and j > k");
				sw.WriteLine("\t\t\t\t\ti = 0");
                sw.WriteLine();

                LineCount += 13;
                i++;
            }

            sw.Close();
        }

        public static void Pipe()
        {
            var Projects = new List<Project>();
            var CurrProject = (Project)null;
            var NewProjCount = 0;

            var Str = (PString)null;
            var Exit = false;

            while (!Exit)
            {
                try
                {
                    Str = new PString(Console.ReadLine().ToLower());
                    var Word = Str.Word();
                    Str.TrimThis();

                    if (Word.String == "new")
                    {
                        if (!Directory.Exists("tmp"))
                            Directory.CreateDirectory("tmp");

                        var Proj = new Project();
                        var c = (++NewProjCount).ToString();
                        Proj.FileName = Path.GetFullPath("tmp\\untitled" + c + ".asproj");
                        CurrProject = Proj;
                    }

                    else if (Word.String == "load")
                    {
                        CurrProject = new Project(Str.String);
                    }

                    else if (Word.String == "setproj")
                    {
                        var ok = false;
                        foreach (var e in Projects)
                            if (e.FileName == Str.String)
                            {
                                CurrProject = e;
                                ok = true;
                                break;
                            }

                        if (!ok) Console.WriteLine("Unloaded project");
                    }

                    else if (Word.String == "setsetting")
                    {
                        if (CurrProject == null)
                        {
                            Console.WriteLine("Unknown project");
                        }
                        else
                        {
                            var Name = Str.Word().String.Trim();
                            var Value = Str.Word().String.Trim();
                            CurrProject.SetSetting(Name, Value);
                        }
                    }

                    else if (Word.String == "save")
                    {
                        if (CurrProject == null) Console.WriteLine("Unknown project");
                        else CurrProject.Save();
                    }

                    else if (Word.String == "saveas")
                    {
                        if (CurrProject == null) Console.WriteLine("Unknown project");
                        else CurrProject.Save(Str.String);
                    }

                    else if (Word.String == "compile")
                    {
                        if (CurrProject == null) Console.WriteLine("Unknown project");
                        else CurrProject.Compile();
                    }

                    else if (Word.String == "addfile")
                    {
                        if (CurrProject == null) Console.WriteLine("Unknown project");
                        else CurrProject.CodeFiles.Add(new CodeFile(Str.String));
                    }

                    else if (Word.String == "getcurrent")
                    {
                        if (CurrProject == null) Console.WriteLine("Unknown project");
                        else Console.WriteLine(CurrProject.FileName);
                    }

                    else if (Word.String == "getprojects")
                    {
                        foreach (var e in Projects)
                            Console.WriteLine(e.FileName);
                    }

                    else if (Word.String == "exit")
                    {
                        Exit = true;
                    }

                    else
                    {
                        Console.WriteLine("Unknown command");
                    }
                }
                catch (Exception e)
                {
                    Console.WriteLine(e.Message);
                    Console.WriteLine(e.StackTrace);
                }
            }
        }

        public static void Main(string[] Args)
		{
			if (Args.Length < 2)
			{
				Console.WriteLine("Command line error");
				return;
			}

			var LinesLst = new List<PString>(1024);
			for (var i = 0; i < Args.Length - 1; i++)
			{
				var fLines = File.ReadAllLines(Args[i]);
				for (var lIndex = 0; lIndex < fLines.Length; lIndex++)
					LinesLst.Add(new PString(Args[i], lIndex, 0, fLines[lIndex]));
			}

			var Lines = LinesLst.ToArray();
			LinesLst = null;

			var Arch = new x86.x86Architecture(false);
			var Lang = new AsLang.AsLanguage();
			var State = new CompilerState(Arch, Lang);
			State.Entry = "AsEntry";
			State.Format = ImageFormat.MSCoff;

			var Compiler = Arch.CreateCompiler(State);
			if (Compiler.Compile(State, Lines))
			{
				Console.WriteLine("A fordítás sikeres.");
				State.Messages.WriteToConsole();
				File.WriteAllText(Args[Args.Length - 1], Compiler.GetAssembly());
			}
			else
			{
				Console.WriteLine("A fordítás sikertelen.");
				State.Messages.WriteToConsole();
			}


			/*
            if (Args[0] == "-pipe")
            {
                if (Args.Length == 1) Pipe();
                else Console.WriteLine("Commandline error");
            }
            else
            {
                if (Args.Length == 1)
                {
                    try
                    {
                        var Proj = new Project(Args[1]);
                        Proj.Compile(null, true);
                    }
                    catch (Exception e)
                    {
                        Console.WriteLine(e.Message);
                    }
                }
                else
                {
                    Console.WriteLine("Commandline error");
                }
            }
			*/
		}
	}
}

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.140814.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