Click here to Skip to main content
11,482,402 members (75,160 online)
Click here to Skip to main content
Add your own
alternative version

Bird Programming Language: Part 1

, 1 Jan 2013 GPL3 214.6K 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 abstract class TypeDeclRecognizer
	{
		public abstract List<TypeDecl> Recognize(ScopeNode Scope);
	}

	public enum TypeDeclType
	{
		Enum,
		Flag,
		Class,
		Struct,
		TypeDef,
	}

	public enum TypeDeclRes
	{
		Failed,
		UnkownTypeDef,
		Succeeded,
	}

	public class TypeDecl
	{
		public ModifierList Mods;
		public TypeDeclType Type;
		public ScopeNode Scope;
		public PString Name;
		public PString Base;

		public PString[] Inner;
		public ProcessableScope NewScope;

		public TypeDecl(ScopeNode Scope, PString Name, TypeDeclType Type,
			PString Base, PString[] Inner, ModifierList Mods)
		{
			this.Scope = Scope;
			this.Name = Name;
			this.Type = Type;
			this.Base = Base;
			this.Inner = Inner;
			this.Mods = Mods;
		}

		public TypeDeclRes Declare()
		{
			if (Scope.IsIdDefined<Identifier>(Name.String, GetIdMode.Scope))
			{
				Scope.State.Messages.Add(MessageId.IdAlreadyDefined, Name);
				return TypeDeclRes.Failed;
			}

			//------------------------------------------------------------------
			else if (Type == TypeDeclType.TypeDef)
			{
				var NewType = Scope.GetId<Type>(Base, EnableMessages: false);
				if (NewType != null)
				{
					NewType = NewType.CreateTypeDefType(Scope, Name);
					if (!Scope.CanIdDeclared(NewType)) return TypeDeclRes.Failed;
					if (!Mods.Apply(Scope, NewType)) return TypeDeclRes.Failed;

					Scope.State.Arch.OnNewIdentifier(NewType);
					Scope.Identifiers.Add(NewType);
					NewType.DeclaredType = true;
					return TypeDeclRes.Succeeded;
				}

				return TypeDeclRes.UnkownTypeDef;
			}

			//------------------------------------------------------------------
			else if (Type == TypeDeclType.Struct || Type == TypeDeclType.Class)
			{
				var NewType = (StructuredType)null;
				if (Type == TypeDeclType.Struct) NewType = new ValueType(Scope, Name);
				else NewType = new ClassType(Scope, Name);

				NewType.Str_Base = Base;
				if (!Scope.CanIdDeclared(NewType)) return TypeDeclRes.Failed;
				if (!Mods.Apply(Scope, NewType)) return TypeDeclRes.Failed;

				NewScope = Scope.CreateStructuredTypeScope(Inner, NewType);
				Scope.State.Arch.OnNewIdentifier(NewType);
				Scope.Identifiers.Add(NewType);
				return TypeDeclRes.Succeeded;
			}

			//------------------------------------------------------------------
			else if (Type == TypeDeclType.Enum || Type == TypeDeclType.Flag)
			{
				if (Base != null)
				{
					Scope.State.Messages.Add(MessageId.CannotInherit, Base);
					return TypeDeclRes.Failed;
				}

				var IntType = Scope.GetType(typeof(SignedType), 4);
				var NewType = (EnumType)null;
				if (Type == TypeDeclType.Flag) NewType = new FlagType(Scope, Name, IntType, null);
				else NewType = new EnumType(Scope, Name, IntType, null);

				if (!Scope.CanIdDeclared(NewType)) return TypeDeclRes.Failed;
				if (!Mods.Apply(Scope, NewType)) return TypeDeclRes.Failed;

				NewScope = Scope.CreateEnumTypeScope(Inner, NewType);
				Scope.State.Arch.OnNewIdentifier(NewType);
				Scope.Identifiers.Add(NewType);
				return TypeDeclRes.Succeeded;
			}

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

	public class TypeDeclList : List<TypeDecl>
	{
		public TypeDeclList()
		{
		}

		public bool IsDefined(string Name, ScopeNode Scope)
		{
			foreach (var e in this)
			{
				if (e.Name.String == Name && e.Scope == Scope)
					return true;
			}

			return false;
		}

		public bool AddFromString(ScopeNode Scope)
		{
			var State = Scope.State;
			var Rec = State.Language.TypeDeclRecognizer;

			var RetValue = true;
			var Res = Rec.Recognize(Scope);
			if (Res == null) return false;

			foreach (var e in Res)
			{
				if (IsDefined(e.Name.String, Scope))
				{
					State.Messages.Add(MessageId.IdAlreadyDefined, e.Name);
					RetValue = false;
				}
				else
				{
					Add(e);
				}
			}

			return RetValue;
		}

		public bool ResolveTypedefs()
		{
			var RetValue = true;
			var Count = 1;

			var List = new List<TypeDecl>();
			foreach (var e in this)
				if (e.Type == TypeDeclType.TypeDef)
					List.Add(e);

			while (Count > 0)
			{
				Count = 0;
				for (var i = 0; i < List.Count; i++)
				{
					var Res = List[i].Declare();
					if (Res == TypeDeclRes.Succeeded) Count++;
					if (Res != TypeDeclRes.UnkownTypeDef)
					{
						List.RemoveAt(i);
						i--;
					}
				}
			}

			foreach (var e in List)
			{
				if (e.Scope.GetId<Type>(e.Base) != null)
					throw new Exception("ERROR");

				RetValue = false;
			}

			return RetValue;
		}

		public IEnumerable<ProcessableScope> DeclaredScopes()
		{
			foreach (var e in this)
				if (e.NewScope != null) yield return e.NewScope;
		}

		public IEnumerable<TypeDecl> EnumDeclarations(TypeDeclType Type)
		{
			foreach (var e in this)
				if (e.Type == Type) yield return e;
		}

		public bool Declare()
		{
			var RetValue = true;
			foreach (var e in this)
			{
				if (e.Type != TypeDeclType.TypeDef && e.Declare() != TypeDeclRes.Succeeded)
					RetValue = false;
			}

			return RetValue;
		}
	}

	public abstract class VarDeclRecognizer
	{
		public abstract List<VarDecl> Recognize(IdContainer Container, PString Code, bool EnableMessages);
	}

	public enum VarConvMode
	{
		Nothing,
		Normal,
		Assignment,
	}

	public class VarDecl
	{
		public PString Declaration;
		public PString Name;
		public PString InitString;

		public PString TypeName;
		public Type Type;

		public VarDecl(PString Declaration, PString TypeName, Type Type, PString Name, PString InitString)
		{
			this.Declaration = Declaration;
			this.TypeName = TypeName;
			this.Type = Type;
			this.Name = Name;
			this.InitString = InitString;
		}

		public ExpressionNode GetVal(ExprPlugIn PlugIn, bool End = true)
		{
			return Expressions.NewExpressionTree(InitString, PlugIn, End);
		}

		public Variable ConvToVar(ExprPlugIn PlugIn, ModifierList Mods = null,
			VarConvMode Mode = VarConvMode.Nothing)
		{
			var Container = PlugIn.Container;
			var Ret = Container.CreateVariable(Name, Type, Mods);
			if (Ret == null) return null;
			Ret.InitString = InitString;

			if (Mode != VarConvMode.Nothing)
			{
				if (!Ret.CalcValue(PlugIn, Mode == VarConvMode.Assignment))
					return null;
			}

			return Ret;
		}
	}

	public class VarDeclList : List<VarDecl>
	{
		public VarDeclList()
		{
		}

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

			return false;
		}

		public bool AddFromString(IdContainer Container, PString String,
			bool EnableUnnamed = false, bool EnableAutoType = true, bool EnableMessages = true)
		{
			var RetValue = true;
			var State = Container.State;
			var Rec = State.Language.VarDeclRecognizer;
			var Res = Rec.Recognize(Container, String, EnableMessages);
			if (Res == null) return false;

			var Type = (Type)null;
			foreach (var e in Res)
			{
				if (e.Name != null)
				{
					if (!Helper.IsValidIdentifierName(e.Name.String))
					{
						if (EnableMessages) State.Messages.Add(MessageId.NotValidName, e.Name);
						RetValue = false;
					}

					if (IsDefined(e.Name.String))
					{
						if (EnableMessages) State.Messages.Add(MessageId.IdAlreadyDefined, e.Name.String);
						RetValue = false;
					}
				}
				else if (!EnableUnnamed)
				{
					if (EnableMessages) State.Messages.Add(MessageId.MustBeNamed, e.Declaration);
					RetValue = false;
				}

				if (e.TypeName != null)
				{
					Type = e.Type;
					Type.SetUsed();

					if (Type is VoidType)
					{
						if (EnableMessages) State.Messages.Add(MessageId.CannotBeThisType, e.TypeName);
						RetValue = false;
					}
					else if (Type is AutomaticType && !EnableAutoType)
					{
						if (EnableMessages) State.Messages.Add(MessageId.Untyped, e.TypeName);
						RetValue = false;
					}
				}
				else
				{
					if (Type != e.Type) throw new Exception("ERROR");
				}

				Add(e);
			}

			return RetValue;
		}

		public List<Variable> ConvToVariables(ExprPlugIn PlugIn, ModifierList Mods = null,
			VarConvMode Mode = VarConvMode.Nothing)
		{
			var Ret = new List<Variable>();
			foreach (var e in this)
				Ret.Add(e.ConvToVar(PlugIn, Mods, Mode));

			return Ret;
		}

		public Type[] GetTypes()
		{
			var Ret = new Type[Count];
			for (var i = 0; i < Count; i++)
				Ret[i] = this[i].Type;

			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 | Terms of Use | Mobile
Web01 | 2.8.150520.1 | Last Updated 1 Jan 2013
Article Copyright 2011 by Dávid Kocsis
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid