Click here to Skip to main content
12,504,462 members (55,125 online)
Click here to Skip to main content

Tagged as

Stats

239.8K views
2.5K downloads
154 bookmarked
Posted

Bird Programming Language: Part 1

, 1 Jan 2013 GPL3
A new general purpose language that aims to be fast, high level and simple to use.
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
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
As.exe
AsLibs.a
PerfTest
Launcher.bmx
Squares
.bmx
Launcher.bmx
Template
Launcher.bmx
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 class EnumTypeScope : ProcessableScope
	{
		public EnumType Type;
		public List<EnumValue> Values;
		public List<ulong> UsedVals;

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

		public override ExpressionNode GetExprRec(PString Name,
			ExprPlugIn PlugIn = null, GetIdMode Mode = GetIdMode.Everywhere)
		{
			foreach (var e in Values)
				if (e.Name == Name.String)
				{
					return e.GetNode(State, Name, PlugIn, Type: Type.TypeOfValues);
				}

			return base.GetExprRec(Name, PlugIn, Mode);
		}

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

		public override bool ProcessScope()
		{
			if (!GetEnumValues(Source, Type))
				return false;

			Type.Values = Values.ToArray();
			return true;
		}

		private bool GetEnumValues(PString[] Inner, EnumType EType)
		{
			Values = new List<EnumValue>();
			UsedVals = new List<ulong>();

			var RetValue = true;
			var End = false;

			foreach (var e in Inner)
				if (e != null)
				{
					var Trimmed = e.Trim();
					if (Trimmed.StrLen == 0) continue;

					if (End)
					{
						State.Messages.Add(MessageId.NotExpected, e);
						continue;
					}

					foreach (var f in Trimmed.Split(",", InZ: true))
					{
						f.TrimThis();
						var Val = GetEnumValue(EType, f);

						if (Val == null) RetValue = false;
						else Values.Add(Val);
					}

					End = true;
				}

			return RetValue;
		}

		private EnumValue GetEnumValue(EnumType EType, PString e)
		{
			var RetValue = true;
			var p = e.IndexOf('=', InZ: true);
			if (p == -1) p = e.StrLen;

			var Name = e.TrimmedSubstring(0, p);

			if (!Helper.IsValidIdentifierName(Name.String))
			{
				State.Messages.Add(MessageId.NotValidName, e);
				RetValue = false;
			}

			ConstExpressionNode RetNode = null;
			if (p < e.StrLen)
			{
				var Str = e.TrimmedSubstring(p + 1);
				if (Str.StrLen == 0)
				{
					State.Messages.Add(MessageId.DeficientExpr, e);
					RetValue = false;
				}
				else
				{
					var PlugIn = GetPlugIn();
					PlugIn.GetPlugIn<TypeMgrPlugIn>().RetType = EType.TypeOfValues;
					var ValueNode = Expressions.NewExpressionTree(Str, PlugIn);
					var ValCNode = ValueNode as ConstExpressionNode;

					var Index = UsedVals.IndexOf(ValCNode.Unsigned);
					if (Index != -1)
					{
						State.Messages.Add(MessageId.SameEnumValue, e, Values[Index].Name);
						RetValue = false;
					}

					if (ValueNode == null || !RetValue)
					{
						RetValue = false;
					}
					else
					{
						RetNode = ValCNode.ConvertTo(State, EType);
						if (RetNode == null) throw new Exception("ERROR");
					}
				}
			}
			else
			{
				var Flag = Type is FlagType;
				var Index = Flag ? 1UL : 0UL;

				while (UsedVals.Contains(Index))
				{
					if (Flag) Index *= 2;
					else Index++;

					if (Index == 0)
					{
						State.Messages.Add(MessageId.EnumValOverflow, e);
						RetValue = false;
						break;
					}
				}

				if (RetValue) RetNode = new ConstExpressionNode(EType, Index, e);
			}

			if (RetValue)
			{
				UsedVals.Add(RetNode.Unsigned);
				return new EnumValue(Name.String, RetNode);
			}

			return null;
		}

	}

	public class StructuredTypeScope : ProcessableScope
	{
		public StructuredType Type;

		public bool ChkCycleInStructs(Identifier Id)
		{
			var Member = Id as MemberVariable;
			if (Member == null) return true;
			var Type = Member.Type as StructuredType;
			if (Type == null) return true;

			if (Type == this.Type)
			{
				State.Messages.Add(MessageId.CycleInStructs, Id.Name);
				return false;
			}

			foreach (var e in Type.Members)
				if (!ChkCycleInStructs(e)) return false;

			return true;
		}

		public bool ChkCycleInStructs()
		{
			foreach (var e in Type.Members)
				if (!ChkCycleInStructs(e)) return false;

			return true;
		}

		public StructuredTypeScope(IdContainer Parent, PString[] Source, StructuredType Type)
			: base(Parent, Source)
		{
			this.Type = Type;
			Type.Members = Identifiers;
		}

		public override bool CanIdDeclared(Identifier Id)
		{
			if (!(Id is ConstructorFunction) && Id.Name.String == Type.Name.String)
			{
				State.Messages.Add(MessageId.CantDeclare, Id.Name);
				return false;
			}

			return base.CanIdDeclared(Id);
		}

		public string GetAsmName()
		{
			var Ret = "_" + Type.Name.String;
			var Structured = Type.Container as StructuredTypeScope;
			if (Structured != null) Ret = Structured.GetAsmName() + Ret;
			return Ret;
		}

		public override bool OnUnTypedFuncDecl(ModifierList Mods,
			PString Name, VarDeclList Params, PString[] Inner)
		{
			if (Name.String == this.Type.Name.String)
				return CreateConstructor(Mods, Name, Params, Inner) != null;

			return base.OnUnTypedFuncDecl(Mods, Name, Params, Inner);
		}

		private Function CreateConstructor(ModifierList Mods, PString Name, VarDeclList Params, PString[] Inner)
		{
			Function Func = new ConstructorFunction(this, Name, null, null);
			Func = DeclareFunction(GlobalScope.VoidType, Name, Params, Inner, Mods, Func);
			if (Func != null) State.Arch.OnNewIdentifier(Func);

			var CallingLine = (PString)null;
			var ConstructType = (Type)null;
			var BaseConstructor = (ConstructorFunction)null;
			var ConstructorParams = (PString)null;
			var FirstLine = Inner != null && Inner.Length > 0 ? Inner[0].Trim() : null;

			if (FirstLine != null && FirstLine.StrLen > 0 && FirstLine[0] == ':')
			{
				Inner[0] = null;
				CallingLine = FirstLine.TrimmedSubstring(0);
				var Pos = Expressions.AsFindParamPosition(State, CallingLine.String);

				if (Pos == -1)
				{
					State.Messages.Add(MessageId.NotExpected, FirstLine);
					return null;
				}

				var BName = CallingLine.TrimmedSubstring(1, Pos);
				ConstructorParams = BracketHelper.TrimOneBracket(CallingLine.TrimmedSubstring(Pos));

				if (BName.String == "base")
				{
					if (Type.Base == null)
					{
						State.Messages.Add(MessageId.UnknownId, Type.Str_Base);
						return null;
					}

					ConstructType = Type.Base;
					BaseConstructor = Type.Base.Constructor;
					if (BaseConstructor == null && ConstructorParams.StrLen > 0)
					{
						State.Messages.Add(MessageId.ParamCount, ConstructorParams);
						return null;
					}
				}
				else if (BName.String == "this")
				{
					ConstructType = Type;
					throw new NotImplementedException();
				}
				else
				{
					State.Messages.Add(MessageId.NotExpected, CallingLine);
					return null;
				}
			}
			else if (Type.Base != null)
			{
				BaseConstructor = Type.Base.Constructor;
				if (BaseConstructor != null && BaseConstructor.Type.Params.Count > 0)
				{
					State.Messages.Add(MessageId.NoParamLessConstructor, Type.Str_Base);
					return null;
				}
			}

			if (BaseConstructor != null)
			{
				if (!CreateBaseCallNode(Type, Func, CallingLine, BaseConstructor, ConstructorParams))
					return null;
			}

			return Func;
		}

		public bool CreateBaseCallNode(Type ConstructType, Function Func, PString CallingLine,
			ConstructorFunction BaseConstructor, PString ConstructorParams)
		{
			var FuncScope = Func.FunctionScope;
			var PlugIn = FuncScope.GetPlugIn();

			PlugIn.Begin();
			var This = PlugIn.NewNode(new IdExpressionNode(FuncScope.Self, CallingLine));
			if (This == null) return false;

			var MConstructor = PlugIn.NewNode(new IdExpressionNode(BaseConstructor, CallingLine));
			var SelfNode = PlugIn.NewNode(new IdExpressionNode(FuncScope.Self, CallingLine));
			if (SelfNode == null || MConstructor == null) return false;

			var MemberNodeCh = new List<ExpressionNode>() { SelfNode, MConstructor };
			var MemberNode = PlugIn.NewNode(new OpExpressionNode(Operator.Member, MemberNodeCh, CallingLine));
			if (MemberNode == null) return false;

			var Ch = (List<ExpressionNode>)null;
			if (ConstructorParams != null)
			{
				Ch = Expressions.CreateParamNodes(PlugIn, CallingLine, null, ConstructorParams);
				if (Ch == null) return false;
			}
			else
			{
				Ch = new List<ExpressionNode>();
			}

			Ch.Insert(0, MemberNode);
			var Expr = Expressions.CreateCallIndexExpr(CallingLine, PlugIn, Operator.Call, Ch);
			if (Expr == null || (Expr = PlugIn.End(Expr)) == null) return false;
			if (!FuncScope.AddExpressionCommand(CallingLine, Expr)) return false;
			return true;
		}

		public override bool ProcessScope()
		{
			if (Type.Str_Base != null)
			{
				var BType = Parent.GetId<Type>(Type.Str_Base);
				if (BType == null) return false;

				Type.Base = BType as StructuredType;
				if (Type.Base == null || !Type.GetType().IsEquivalentTo(BType.GetType()))
				{
					State.Messages.Add(MessageId.CannotInherit, Type.Str_Base);
					return false;
				}
				else if (Type.Base.IsStatic)
				{
					State.Messages.Add(MessageId.CannotInheritStatic, Type.Str_Base);
					return false;
				}
				else if (Type.Base.IsSealed)
				{
					State.Messages.Add(MessageId.CannotInheritSealed, Type.Str_Base);
					return false;
				}

				if (Type.IsSubclassOf(Type))
				{
					State.Messages.Add(MessageId.CycleInStructs, Type.Str_Base);
					return false;
				}
			}

			//--------------------------------------------------------------------------------------
			if (!base.ProcessScope() || !ChkCycleInStructs())
				return false;

			if (Type.Members.GetIdRec<ConstructorFunction>(null) == null && Type.Base != null)
			{
				var BaseConstructor = Type.Base.Constructor;
				if (BaseConstructor.Type.Params.Count > 0)
				{
					State.Messages.Add(MessageId.NoParamLessConstructor, Type.Str_Base);
					return false;
				}
			}

			Type.CalcPositions();
			return true;
		}

		public override void GetMTProcIds(List<Identifier> Ids)
		{
			Ids.AddRange(Identifiers);
			base.GetMTProcIds(Ids);
		}

		public override Variable CreateVariable(PString Name, Type Type, ModifierList Mods = null)
		{
			var Ret = (Variable)null;
			if (Mods != null && Mods.Contains(ModifierType.Static))
				Ret = new GlobalVariable(this, Name, Type);
			else Ret = new MemberVariable(this, Name, Type);

			if (Mods != null && !Mods.Apply(this, Ret)) return null;
			State.Arch.OnNewIdentifier(Ret);
			return Ret;
		}

		public override Function CreateFunction(PString Name, FuncScopeNode Scope, FunctionType FuncType, ModifierList Mods = null)
		{
			var Ret = (Function)null;
			if (Mods != null && Mods.Contains(ModifierType.Static))
				Ret = new Function(this, Name, FuncType, Scope);
			else Ret = new MemberFunction(this, Name, FuncType, Scope);

			if (!Mods.Apply(this, Ret)) return null;
			State.Arch.OnNewIdentifier(Ret);
			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
Software Developer
Hungary Hungary
No Biography provided

You may also be interested in...

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