Click here to Skip to main content
Click here to Skip to main content
Add your own
alternative version
Go to top

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

namespace Anonymus
{
	public enum ConstDataType
	{
		Signed,
		Unsigned,
		Double,
		String,
	}

	public unsafe class ConstData
	{
		public const int BuffSize = 8;

		public ConstDataType Type;
		public byte[] Bytes;
		public string String;

		public ConstData(long Data)
		{
			Bytes = new byte[BuffSize + 8];
			SignedData = Data;
			Type = ConstDataType.Signed;
		}

		public ConstData(ulong Data)
		{
			Bytes = new byte[BuffSize + 8];
			UnsignedData = Data;
			Type = ConstDataType.Unsigned;
		}

		public ConstData(double Data)
		{
			Bytes = new byte[BuffSize + 8];
			DoubleData = Data;
			Type = ConstDataType.Double;
		}

		public ConstData(string String)
		{
			this.String = String;
			this.Type = ConstDataType.String;
		}

		public byte* GetPtr(int Offset, int Size)
		{
			if (Offset + Size > BuffSize)
				throw new Exception("ERROR");

			fixed (byte* pBytes = Bytes)
				return pBytes + Offset;
		}

		public ulong GetUnsigned(int Offset, int Size)
		{
			var Ptr = GetPtr(Offset, Size);
			if (Size == 1) return (byte)(*(long*)Ptr);
			else if (Size == 2) return (ushort)(*(long*)Ptr);
			else if (Size == 4) return (uint)(*(long*)Ptr);
			else if (Size == 8) return (*(ulong*)Ptr);
			else throw new Exception("ERROR");
		}

		public long GetSigned(int Offset, int Size)
		{
			var Ptr = GetPtr(Offset, Size);
			if (Size == 1) return (sbyte)(*(long*)Ptr);
			else if (Size == 2) return(short)(*(long*)Ptr);
			else if (Size == 4) return (int)(*(long*)Ptr);
			else if (Size == 8) return (*(long*)Ptr);
			else throw new Exception("ERROR");
		}

		public ulong UnsignedData
		{
			get { return *(ulong*)GetPtr(0, sizeof(ulong)); }
			set { *(ulong*)GetPtr(0, sizeof(ulong)) = value; }
		}

		public string GetDefString(int Offset = 0, int Size = 8)
		{
			if (Type == ConstDataType.Signed)
				return GetSigned(Offset, Size).ToString();
			else return GetUnsigned(Offset, Size).ToString();
		}

		public long SignedData
		{
			get { return *(long*)GetPtr(0, sizeof(long)); }
			set { *(long*)GetPtr(0, sizeof(long)) = value; }
		}

		public double DoubleData
		{
			get { return * (double*)GetPtr(0, sizeof(double)); }
			set { *(double*)GetPtr(0, sizeof(double)) = value; }
		}

		public double CDoubleData
		{
			get
			{
				if (Type == ConstDataType.Signed) return (double)SignedData;
				else if (Type == ConstDataType.Unsigned) return (double)UnsignedData;
				else if (Type == ConstDataType.String) return Convert.ToInt64(String);
				else return *(double*)GetPtr(0, sizeof(double));
			}

			set
			{
				if (Type == ConstDataType.Signed) SignedData = (long)value;
				else if (Type == ConstDataType.Unsigned) UnsignedData = (ulong)value;
				else if (Type == ConstDataType.String) String = value.ToString();
				else *(double*)GetPtr(0, sizeof(double)) = value;
			}
		}

		public override string ToString()
		{
			if (Type == ConstDataType.Unsigned) return UnsignedData.ToString();
			else if (Type == ConstDataType.Signed) return SignedData.ToString();
			else if (Type == ConstDataType.Double) return DoubleData.ToString();
			else if (Type == ConstDataType.String) return String;
			else throw new Exception("ERROR");
		}

		public bool IsEqual(ConstData Data)
		{
			if (Type != Data.Type) return false;

			if (Type == ConstDataType.String)
			{
				return Data.String == String;
			}
			else
			{
				if (Data.Bytes.Length != Bytes.Length)
					throw new Exception("ERROR");

				for (var i = 0; i < Bytes.Length; i++)
					if (Data.Bytes[i] != Bytes[i]) return false;

				return true;
			}
		}
	}

	public class ConstExpressionNode : ExpressionNode
	{
		public ConstData Value;
		public ulong Unsigned { get { return Value.UnsignedData; } }
		public long Signed { get { return Value.SignedData; } }
		public double Double { get { return Value.DoubleData; } }
		public double CDouble { get { return Value.CDoubleData; } }
		public string String { get { return Value.String; } }

		public override ConditionRes ConditionResult
		{ get { return Bool ? ConditionRes.True : ConditionRes.False; } }

		public bool Bool
		{
			get
			{
				if (Type is BoolType) return Unsigned != (Type as BoolType).FalseVal;
				else throw new Exception("Not boolean type");
			}
		}

		public ConstExpressionNode(Type Type, ConstData Value, PString Code)
			: base(Code)
		{
			this.Type = Type;
			this.Value = Value;
		}

		public ConstExpressionNode(Type Type, ulong Value, PString Code)
			: base(Code)
		{
			this.Type = Type;
			this.Value = new ConstData(Value);
		}

		public ConstExpressionNode(Type Type, long Value, PString Code)
			: base(Code)
		{
			this.Type = Type;
			this.Value = new ConstData(Value);
		}

		public ConstExpressionNode(Type Type, double Value, PString Code)
			: base(Code)
		{
			this.Type = Type;
			this.Value = new ConstData(Value);
		}

		public ConstExpressionNode(Type Type, string Value, PString Code)
			: base(Code)
		{
			this.Type = Type;
			this.Value = new ConstData(Value);
		}

		public ConstExpressionNode(Type Type, bool Value, PString Code)
			: base(Code)
		{
			if (Type is BoolType)
			{
				var bType = Type as BoolType;
				this.Type = Type;
				this.Value = Value ? bType.TrueData : bType.FalseData;
			}
			else
			{
				throw new Exception("Not boolean type");
			}
		}

		public override ExpressionNode Copy(CompilerState State, PString Code, ReplaceNodeFunc Func)
		{
			if (Code == null) Code = this.Code;
			if (Value != null) return Func(new ConstExpressionNode(Type, Value, Code));
			else if (String != null) return Func(new ConstExpressionNode(Type, String, Code));
			else throw new Exception("Unknown constant type");
		}

		public override string ToString()
		{
			if (Type is SignedType) return Signed.ToString();
			else if (Type is UnsignedType) return Unsigned.ToString();
			else if (Type is FloatType) return Double.ToString();
			else if (Type is BoolType) return ((Type as BoolType).FalseVal != Unsigned).ToString();
			else if (Type is EnumType) return (Type as EnumType).GetValue(Unsigned).Name;
			else if (Type is ConstStringType) return String;
			else if (Type is AutomaticType) return Unsigned == 0 ? "null" : Unsigned.ToString();
			else if (Type is PointerType) return Unsigned.ToString();
			else throw new NotImplementedException();
		}

		public ConstExpressionNode ConvertTo(CompilerState State, Type To)
		{
#warning WARNING
			// pl -1 -> Unsinged, Enum

			ConstExpressionNode Ret;
			if (Type is EnumType) Ret = ConvertEnum(To);
			else if (Type is SignedType) Ret = ConvertSigned(To);
			else if (Type is UnsignedType) Ret = ConvertUnsigned(To);
			else if (Type is FloatType) Ret = ConvertFloat(To);
			else if (Type is BoolType) Ret = ConvertBool(To);
			else if (Type is ConstStringType) Ret = ConvertConstStr(State, To);
			else if (Type is AutomaticType) Ret = ConvertAuto(To);
			else return this;

			if (Ret == null)
			{
				State.Messages.Add(MessageId.CannotConvertConst,
					Code, ToString(), To.Name.String);
			}

			return Ret;
		}

		private ConstExpressionNode ConvertAuto(Type To)
		{
			return new ConstExpressionNode(To, Unsigned, Code);
		}

		private ConstExpressionNode ConvertConstStr(CompilerState State, Type To)
		{
			if (To is BoolType)
			{
				var BoolTo = To as BoolType;
				if (String == "true") return Expressions.GetBoolValue(BoolTo, true, Code);
				else if (String == "false") return Expressions.GetBoolValue(BoolTo, true, Code);
			}
			else if (String.Length > 0)
			{
				var Str = String;
				var Neg = Str[0] == '-';
				if (Neg) Str = Str.Substring(1);
				return Expressions.ConvStrToConst(State, null, Str, To, Negative: Neg);
			}

			return null;
		}

		private ConstExpressionNode ConvertBool(Type To)
		{
			if (To is BoolType) return new ConstExpressionNode(To, Bool, Code);
			else if (To is ConstStringType) return new ConstExpressionNode(To, Bool.ToString(), Code);

			return null;
		}

		private ConstExpressionNode ConvertFloat(Type To)
		{
			if (To is SignedType) return new ConstExpressionNode(To, (long)Double, Code);
			else if (To is UnsignedType) return new ConstExpressionNode(To, (ulong)Double, Code);
			else if (To is FloatType)
			{
				var FType = To as FloatType;
				if (FType.Size == 8) return new ConstExpressionNode(To, Double, Code);
				else if (FType.Size == 4) return new ConstExpressionNode(To, (float)Double, Code);
				else throw new NotImplementedException();
			}
			else if (To is ConstStringType) return new ConstExpressionNode(To, Double.ToString(), Code);

			return null;
		}

		private ConstExpressionNode ConvertUnsigned(Type To)
		{
			if (To is SignedType) return new ConstExpressionNode(To, (long)Unsigned, Code);
			else if (To is UnsignedType) return new ConstExpressionNode(To, (ulong)Unsigned, Code);
			else if (To is FloatType)
			{
				var FType = To as FloatType;
				if (FType.Size == 8) return new ConstExpressionNode(To, (double)Unsigned, Code);
				else if (FType.Size == 4) return new ConstExpressionNode(To, (float)Unsigned, Code);
				else throw new NotImplementedException();
			}
			else if (To is ConstStringType) return new ConstExpressionNode(To, Unsigned.ToString(), Code);
			else if (To is EnumType)
			{
				var ValType = (To as EnumType).TypeOfValues;
				if (ValType is SignedType) return new ConstExpressionNode(To, (long)Unsigned, Code);
				else if (ValType is UnsignedType) return new ConstExpressionNode(To, Unsigned, Code);
			}

			return null;
		}

		private ConstExpressionNode ConvertSigned(Type To)
		{
			if (To is SignedType) return new ConstExpressionNode(To, (long)Signed, Code);
			else if (To is UnsignedType) return new ConstExpressionNode(To, (ulong)Signed, Code);
			else if (To is FloatType)
			{
				var FType = To as FloatType;
				if (FType.Size == 8) return new ConstExpressionNode(To, (double)Signed, Code);
				else if (FType.Size == 4) return new ConstExpressionNode(To, (float)Signed, Code);
				else throw new NotImplementedException();
			}
			else if (To is ConstStringType) return new ConstExpressionNode(To, Signed.ToString(), Code);
			else if (To is EnumType)
			{
				var ValType = (To as EnumType).TypeOfValues;
				if (ValType is SignedType) return new ConstExpressionNode(To, Signed, Code);
				else if (ValType is UnsignedType) return new ConstExpressionNode(To, (ulong)Signed, Code);
			}

			return null;
		}

		private ConstExpressionNode ConvertEnum(Type To)
		{
			if (To is NumberType)
			{
				var ValType = (Type as EnumType).TypeOfValues;
				if (ValType is UnsignedType) return new ConstExpressionNode(To, Unsigned, Code);
				else if (ValType is SignedType) return new ConstExpressionNode(To, Signed, Code);
			}

			return null;
		}

		public static ConstExpressionNode DoOps(IdContainer Container, OpExpressionNode OpNode)
		{
			var Ch = OpNode.Children;
			var Op = OpNode.Operator;
			var Dst = Ch[0] as ConstExpressionNode;
			var Src = Ch.Count > 0 ? Ch[1] as ConstExpressionNode : null;

			if (Op == Operator.Condition)
			{
				if (Dst.Bool) return Ch[1] as ConstExpressionNode;
				else return Ch[2] as ConstExpressionNode;
			}

			if (Dst.Type is SignedType) return SignedOps(Container, OpNode, Dst, Src);
			else if (Dst.Type is UnsignedType) return UnsignedOps(Container, OpNode, Dst, Src);
			else if (Dst.Type is FloatType) return FloatOps(Container, OpNode, Dst, Src);
			else if (Dst.Type is BoolType) return BoolOps(OpNode, Dst, Src);
			else if (Dst.Type is ConstStringType) return ConstStrOps(Container, OpNode, Dst, Src);
			else if (Dst.Type is EnumType) return EnumOps(Container, OpNode, Dst, Src);
			else throw new NotImplementedException();
		}

		private static ConstExpressionNode EnumOps(IdContainer Container, OpExpressionNode OpNode, ConstExpressionNode Dst, ConstExpressionNode Src)
		{
			var Op = OpNode.Operator;
			if (Op == Operator.Equal) return Expressions.GetBoolValue(Container, Dst.Unsigned == Src.Unsigned, OpNode.Code);
			else if (Op == Operator.Nonequal) return Expressions.GetBoolValue(Container, Dst.Unsigned != Src.Unsigned, OpNode.Code);

			else if (Op == Operator.BitwiseAnd) return new ConstExpressionNode(Dst.Type, Dst.Unsigned & Src.Unsigned, OpNode.Code);
			else if (Op == Operator.BitwiseOr) return new ConstExpressionNode(Dst.Type, Dst.Unsigned | Src.Unsigned, OpNode.Code);
			else if (Op == Operator.BitwiseXor) return new ConstExpressionNode(Dst.Type, (Dst.Unsigned | Src.Unsigned) & ~(Dst.Unsigned & Src.Unsigned), OpNode.Code);
			else throw new NotImplementedException();
		}

		private static ConstExpressionNode ConstStrOps(IdContainer Container, OpExpressionNode OpNode, ConstExpressionNode Dst, ConstExpressionNode Src)
		{
			var Op = OpNode.Operator;
			if (Op == Operator.Add) return new ConstExpressionNode(Dst.Type, Dst.String + Src.String, OpNode.Code);
			else if (Op == Operator.Equal) return Expressions.GetBoolValue(Container, Dst.String == Src.String, OpNode.Code);
			else if (Op == Operator.Nonequal) return Expressions.GetBoolValue(Container, Dst.String != Src.String, OpNode.Code);
			else throw new NotImplementedException();
		}

		private static ConstExpressionNode BoolOps(OpExpressionNode OpNode, ConstExpressionNode Dst, ConstExpressionNode Src)
		{
			var Op = OpNode.Operator;
			var BType = Dst.Type as BoolType;
			if (Op == Operator.Equal) return Expressions.GetBoolValue(BType, Dst.Bool == Src.Bool, OpNode.Code);
			else if (Op == Operator.Nonequal) return Expressions.GetBoolValue(BType, Dst.Bool != Src.Bool, OpNode.Code);

			else if (Op == Operator.Add) return Expressions.GetBoolValue(BType, Dst.Bool && Src.Bool, OpNode.Code);
			else if (Op == Operator.Or) return Expressions.GetBoolValue(BType, Dst.Bool || Src.Bool, OpNode.Code);
			else if (Op == Operator.Xor) return Expressions.GetBoolValue(BType, (Dst.Bool || Src.Bool) && !(Dst.Bool && Src.Bool), OpNode.Code);

			else if (Op == Operator.Not) return new ConstExpressionNode(Dst.Type, !Dst.Bool, OpNode.Code);
			else throw new NotImplementedException();
		}

		private static ConstExpressionNode FloatOps(IdContainer Container, OpExpressionNode OpNode, ConstExpressionNode Dst, ConstExpressionNode Src)
		{
			var Op = OpNode.Operator;
			if (Op == Operator.Add) return new ConstExpressionNode(Dst.Type, Dst.Double + Src.Double, OpNode.Code);
			else if (Op == Operator.Subract) return new ConstExpressionNode(Dst.Type, Dst.Double - Src.Double, OpNode.Code);
			else if (Op == Operator.Multiply) return new ConstExpressionNode(Dst.Type, Dst.Double * Src.Double, OpNode.Code);
			else if (Op == Operator.Divide) return new ConstExpressionNode(Dst.Type, Dst.Double / Src.Double, OpNode.Code);
			else if (Op == Operator.Modolus) return new ConstExpressionNode(Dst.Type, Dst.Double % Src.Double, OpNode.Code);

			else if (Op == Operator.Equal) return Expressions.GetBoolValue(Container, Dst.Double == Src.Double, OpNode.Code);
			else if (Op == Operator.Nonequal) return Expressions.GetBoolValue(Container, Dst.Double != Src.Double, OpNode.Code);
			else if (Op == Operator.Greater) return Expressions.GetBoolValue(Container, Dst.Double > Src.Double, OpNode.Code);
			else if (Op == Operator.GreaterEqual) return Expressions.GetBoolValue(Container, Dst.Double >= Src.Double, OpNode.Code);
			else if (Op == Operator.Less) return Expressions.GetBoolValue(Container, Dst.Double < Src.Double, OpNode.Code);
			else if (Op == Operator.LessEqual) return Expressions.GetBoolValue(Container, Dst.Double <= Src.Double, OpNode.Code);

			else if (Op == Operator.Neg) return new ConstExpressionNode(Dst.Type, -Dst.Double, OpNode.Code);
			else if (Op == Operator.Abs) return new ConstExpressionNode(Dst.Type, Dst.Double < 0 ? -Dst.Double : Dst.Double, OpNode.Code);

			else if (Op == Operator.Sqrt) return new ConstExpressionNode(Dst.Type, Math.Sqrt(Dst.Double), OpNode.Code);
			else if (Op == Operator.Sin) return new ConstExpressionNode(Dst.Type, Math.Sin(Dst.Double), OpNode.Code);
			else if (Op == Operator.Cos) return new ConstExpressionNode(Dst.Type, Math.Cos(Dst.Double), OpNode.Code);
			else if (Op == Operator.Tan) return new ConstExpressionNode(Dst.Type, Math.Tan(Dst.Double), OpNode.Code);
			else if (Op == Operator.ATan) return new ConstExpressionNode(Dst.Type, Math.Atan(Dst.Double), OpNode.Code);
			else if (Op == Operator.ATan2) return new ConstExpressionNode(Dst.Type, Math.Atan2(Dst.Double, Src.CDouble), OpNode.Code);
			else if (Op == Operator.ASin) return new ConstExpressionNode(Dst.Type, Math.Asin(Dst.Double), OpNode.Code);
			else if (Op == Operator.ACos) return new ConstExpressionNode(Dst.Type, Math.Acos(Dst.Double), OpNode.Code);
			else throw new NotImplementedException();
		}

		private static ConstExpressionNode UnsignedOps(IdContainer Container, OpExpressionNode OpNode, ConstExpressionNode Dst, ConstExpressionNode Src)
		{
			var Op = OpNode.Operator;
			if (Op == Operator.Add) return new ConstExpressionNode(Dst.Type, Dst.Unsigned + Src.Unsigned, OpNode.Code);
			else if (Op == Operator.Subract) return new ConstExpressionNode(Dst.Type, Dst.Unsigned - Src.Unsigned, OpNode.Code);
			else if (Op == Operator.Multiply) return new ConstExpressionNode(Dst.Type, Dst.Unsigned * Src.Unsigned, OpNode.Code);
			else if (Op == Operator.Divide) return new ConstExpressionNode(Dst.Type, Dst.Unsigned / Src.Unsigned, OpNode.Code);
			else if (Op == Operator.Modolus) return new ConstExpressionNode(Dst.Type, Dst.Unsigned % Src.Unsigned, OpNode.Code);
			else if (Op == Operator.ShiftLeft) return new ConstExpressionNode(Dst.Type, Dst.Unsigned * Helper.Pow(Src.Unsigned, 2), OpNode.Code);
			else if (Op == Operator.ShiftRight) return new ConstExpressionNode(Dst.Type, Dst.Unsigned / Helper.Pow(Src.Unsigned, 2), OpNode.Code);

			else if (Op == Operator.Equal) return Expressions.GetBoolValue(Container, Dst.Unsigned == Src.Unsigned, OpNode.Code);
			else if (Op == Operator.Nonequal) return Expressions.GetBoolValue(Container, Dst.Unsigned != Src.Unsigned, OpNode.Code);
			else if (Op == Operator.Greater) return Expressions.GetBoolValue(Container, Dst.Unsigned > Src.Unsigned, OpNode.Code);
			else if (Op == Operator.GreaterEqual) return Expressions.GetBoolValue(Container, Dst.Unsigned >= Src.Unsigned, OpNode.Code);
			else if (Op == Operator.Less) return Expressions.GetBoolValue(Container, Dst.Unsigned < Src.Unsigned, OpNode.Code);
			else if (Op == Operator.LessEqual) return Expressions.GetBoolValue(Container, Dst.Unsigned <= Src.Unsigned, OpNode.Code);

			else if (Op == Operator.Complement) return new ConstExpressionNode(Dst.Type, ~Dst.Unsigned, OpNode.Code);

			else if (Op == Operator.BitwiseAnd) return new ConstExpressionNode(Dst.Type, Dst.Unsigned & Src.Unsigned, OpNode.Code);
			else if (Op == Operator.BitwiseOr) return new ConstExpressionNode(Dst.Type, Dst.Unsigned | Src.Unsigned, OpNode.Code);
			else if (Op == Operator.BitwiseXor) return new ConstExpressionNode(Dst.Type, (Dst.Unsigned | Src.Unsigned) & ~(Dst.Unsigned & Src.Unsigned), OpNode.Code);

			else if (Op == Operator.Sqrt) return Expressions.GetFloatValue(Container, Math.Sqrt(Dst.CDouble), OpNode.Code);
			else if (Op == Operator.Sin) return Expressions.GetFloatValue(Container, Math.Sin(Dst.CDouble), OpNode.Code);
			else if (Op == Operator.Cos) return Expressions.GetFloatValue(Container, Math.Cos(Dst.CDouble), OpNode.Code);
			else if (Op == Operator.Tan) return Expressions.GetFloatValue(Container, Math.Tan(Dst.CDouble), OpNode.Code);
			else if (Op == Operator.ATan) return Expressions.GetFloatValue(Container, Math.Atan(Dst.CDouble), OpNode.Code);
			else if (Op == Operator.ATan2) return Expressions.GetFloatValue(Container, Math.Atan2(Dst.CDouble, Src.CDouble), OpNode.Code);
			else if (Op == Operator.ASin) return Expressions.GetFloatValue(Container, Math.Asin(Dst.CDouble), OpNode.Code);
			else if (Op == Operator.ACos) return Expressions.GetFloatValue(Container, Math.Acos(Dst.CDouble), OpNode.Code);
			else throw new NotImplementedException();
		}

		private static ConstExpressionNode SignedOps(IdContainer Container, OpExpressionNode OpNode, ConstExpressionNode Dst, ConstExpressionNode Src)
		{
			var Op = OpNode.Operator;
			if (Op == Operator.Add) return new ConstExpressionNode(Dst.Type, Dst.Signed + Src.Signed, OpNode.Code);
			else if (Op == Operator.Subract) return new ConstExpressionNode(Dst.Type, Dst.Signed - Src.Signed, OpNode.Code);
			else if (Op == Operator.Multiply) return new ConstExpressionNode(Dst.Type, Dst.Signed * Src.Signed, OpNode.Code);
			else if (Op == Operator.Divide) return new ConstExpressionNode(Dst.Type, Dst.Signed / Src.Signed, OpNode.Code);
			else if (Op == Operator.Modolus) return new ConstExpressionNode(Dst.Type, Dst.Signed % Src.Signed, OpNode.Code);
			else if (Op == Operator.ShiftLeft) return new ConstExpressionNode(Dst.Type, Dst.Signed * Helper.Pow(Src.Signed, 2), OpNode.Code);
			else if (Op == Operator.ShiftRight) return new ConstExpressionNode(Dst.Type, Dst.Signed / Helper.Pow(Src.Signed, 2), OpNode.Code);

			else if (Op == Operator.Equal) return Expressions.GetBoolValue(Container, Dst.Signed == Src.Signed, OpNode.Code);
			else if (Op == Operator.Nonequal) return Expressions.GetBoolValue(Container, Dst.Signed != Src.Signed, OpNode.Code);
			else if (Op == Operator.Greater) return Expressions.GetBoolValue(Container, Dst.Signed > Src.Signed, OpNode.Code);
			else if (Op == Operator.GreaterEqual) return Expressions.GetBoolValue(Container, Dst.Signed >= Src.Signed, OpNode.Code);
			else if (Op == Operator.Less) return Expressions.GetBoolValue(Container, Dst.Signed < Src.Signed, OpNode.Code);
			else if (Op == Operator.LessEqual) return Expressions.GetBoolValue(Container, Dst.Signed <= Src.Signed, OpNode.Code);

			else if (Op == Operator.Complement) return new ConstExpressionNode(Dst.Type, ~Dst.Signed, OpNode.Code);
			else if (Op == Operator.Neg) return new ConstExpressionNode(Dst.Type, -Dst.Signed, OpNode.Code);

			else if (Op == Operator.BitwiseAnd) return new ConstExpressionNode(Dst.Type, Dst.Signed & Src.Signed, OpNode.Code);
			else if (Op == Operator.BitwiseOr) return new ConstExpressionNode(Dst.Type, Dst.Signed | Src.Signed, OpNode.Code);
			else if (Op == Operator.BitwiseXor) return new ConstExpressionNode(Dst.Type, (Dst.Signed | Src.Signed) & ~(Dst.Signed & Src.Signed), OpNode.Code);
			
			else if (Op == Operator.Abs) return new ConstExpressionNode(Dst.Type, Dst.Signed < 0 ? -Dst.Signed : Dst.Signed, OpNode.Code);
			else if (Op == Operator.Sqrt) return Expressions.GetFloatValue(Container, Math.Sqrt(Dst.CDouble), OpNode.Code);

			else if (Op == Operator.Sin) return Expressions.GetFloatValue(Container, Math.Sin(Dst.CDouble), OpNode.Code);
			else if (Op == Operator.Cos) return Expressions.GetFloatValue(Container, Math.Cos(Dst.CDouble), OpNode.Code);
			else if (Op == Operator.Tan) return Expressions.GetFloatValue(Container, Math.Tan(Dst.CDouble), OpNode.Code);
			else if (Op == Operator.ATan) return Expressions.GetFloatValue(Container, Math.Atan(Dst.CDouble), OpNode.Code);
			else if (Op == Operator.ATan2) return Expressions.GetFloatValue(Container, Math.Atan2(Dst.CDouble, Src.CDouble), OpNode.Code);
			else if (Op == Operator.ASin) return Expressions.GetFloatValue(Container, Math.Asin(Dst.CDouble), OpNode.Code);
			else if (Op == Operator.ACos) return Expressions.GetFloatValue(Container, Math.Acos(Dst.CDouble), OpNode.Code);
			else throw new NotImplementedException();
		}
	}

}

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