Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 40 additions & 0 deletions Converters/TimeSpanTypeConverter.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
using System;
using System.ComponentModel;
using System.Globalization;

namespace HiSystems.Interpreter.Converters {
public class TimeSpanTypeConverter : TypeConverter
{
public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceType)
{
return sourceType == typeof(System.TimeSpan);
}

public override bool CanConvertTo(ITypeDescriptorContext context, Type destinationType)
{
return destinationType == typeof(TimeSpan) || destinationType == typeof(Literal); ;
}

public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, object value)
{
var date = default(TimeSpan);
if (value is System.TimeSpan)
{
date = (System.TimeSpan)value;
}

return new TimeSpan(date);
}

public override object ConvertTo(ITypeDescriptorContext context, CultureInfo culture, object value, Type destinationType)
{
var date = value as TimeSpan;
if (date != null)
{
return (System.TimeSpan)date;
}

return default(System.TimeSpan);
}
}
}
4 changes: 4 additions & 0 deletions Engine/Engine.cs
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,7 @@ private class ReservedWord
new OperatorAndPrecedence() { Operation = new EqualToOperator(), Precedence = 3 },
new OperatorAndPrecedence() { Operation = new NotEqualToOperator(), Precedence = 3 },
new OperatorAndPrecedence() { Operation = new AndOperator(), Precedence = 2 },
new OperatorAndPrecedence() { Operation = new XorOperator(), Precedence = 2 },
new OperatorAndPrecedence() { Operation = new OrOperator(), Precedence = 1 }
};

Expand Down Expand Up @@ -309,6 +310,9 @@ private List<TranslatedToken> TranslateTokens(List<Token> tokens, List<Variable>
case TokenType.DateTime:
translatedTokens.Add(new ConstructToken(new DateTime(System.DateTime.Parse(token.Value))));
break;
case TokenType.TimeSpan:
translatedTokens.Add(new ConstructToken(new TimeSpan(System.TimeSpan.Parse(token.Value))));
break;
case TokenType.Other:
var operationForToken = allOperators
.Select(item => item.Operation)
Expand Down
4 changes: 4 additions & 0 deletions Engine/Token.cs
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,10 @@ internal enum TokenType
/// </summary>
DateTime,

/// <summary>
/// Value is surrounded by ` characters.
/// </summary>
TimeSpan,
/// <summary>
/// Any other token that is not one of the other token types specified in this enum.
/// Usually a special character such as '*' or '^'.
Expand Down
24 changes: 21 additions & 3 deletions Engine/Tokenizer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,8 @@ public static List<Token> Parse(string expression)
const char RightParenthesis = ')';
const char Comma = ',';
const char NumericNegative = '-';
const char DateTimeDelimiter = '#';
const char DateTimeDelimiter = '#';
const char TimeSpanDelimiter = '`';

var whitespaceCharacters = new[] { ' ', '\t' };
var numericCharacters = new[] { '.', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9' };
Expand All @@ -46,7 +47,8 @@ public static List<Token> Parse(string expression)
var textDelimiters = new[] { '\"', '\'' };
bool isNumericNegative = false;
bool parsingText = false;
bool parsingDateTime = false;
bool parsingDateTime = false;
bool parsingTimeSpan = false;

var tokens = new List<Token>();
var currentTokenType = TokenType.Other;
Expand Down Expand Up @@ -97,7 +99,23 @@ public static List<Token> Parse(string expression)
}
else
characterString = character.ToString();
}
}
else if (character == TimeSpanDelimiter || parsingTimeSpan)
{
if (!parsingTimeSpan) // started parsing
{
characterTokenType = TokenType.TimeSpan;
characterString = String.Empty; // consume character
parsingTimeSpan = true;
}
else if (character == TimeSpanDelimiter) // finished parsing
{
characterString = String.Empty; // consume character
parsingTimeSpan = false;
}
else
characterString = character.ToString();
}
else if (whitespaceCharacters.Contains(character))
{
characterTokenType = TokenType.Whitespace;
Expand Down
195 changes: 99 additions & 96 deletions HiSystems.Interpreter.csproj
Original file line number Diff line number Diff line change
@@ -1,103 +1,106 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="4.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
<ProductVersion>8.0.30703</ProductVersion>
<SchemaVersion>2.0</SchemaVersion>
<ProjectGuid>{65D25F1E-BD7D-4B5E-8457-DD8A4813DD30}</ProjectGuid>
<OutputType>Library</OutputType>
<AppDesignerFolder>Properties</AppDesignerFolder>
<RootNamespace>HiSystems.Interpreter</RootNamespace>
<AssemblyName>HiSystems.Interpreter</AssemblyName>
<TargetFrameworkVersion>v3.5</TargetFrameworkVersion>
<FileAlignment>512</FileAlignment>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<DebugSymbols>true</DebugSymbols>
<DebugType>full</DebugType>
<Optimize>false</Optimize>
<OutputPath>bin\Debug\</OutputPath>
<DefineConstants>DEBUG;TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<DebugType>pdbonly</DebugType>
<Optimize>true</Optimize>
<OutputPath>bin\Release\</OutputPath>
<DefineConstants>TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<ItemGroup>
<Reference Include="System" />
<Reference Include="System.Core" />
<Reference Include="System.Xml.Linq" />
<Reference Include="System.Data.DataSetExtensions" />
<Reference Include="System.Data" />
<Reference Include="System.Xml" />
</ItemGroup>
<ItemGroup>
<Compile Include="AssemblyInfo.cs" />
<Compile Include="Constructs\FunctionOperation.cs" />
<Compile Include="Constructs\IConstruct.cs" />
<Compile Include="Constructs\Operation.cs" />
<Compile Include="Constructs\Variable.cs" />
<Compile Include="Converters\BooleanTypeConverter.cs" />
<Compile Include="Converters\DateTimeTypeConverter.cs" />
<Compile Include="Converters\NumberTypeConverter.cs" />
<Compile Include="Converters\TextTypeConverter.cs" />
<Compile Include="Engine\Engine.cs" />
<Compile Include="Engine\Expression.cs" />
<Compile Include="Engine\ExpressionParsed.cs" />
<Compile Include="Engine\ExpressionParseOnDemand.cs" />
<Compile Include="Engine\Token.cs" />
<Compile Include="Engine\Tokenizer.cs" />
<Compile Include="Functions\ArrayFunction.cs" />
<Compile Include="Functions\Average.cs" />
<Compile Include="Functions\Format.cs" />
<Compile Include="Functions\Function.cs" />
<Compile Include="Functions\If.cs" />
<Compile Include="Functions\Len.cs" />
<Compile Include="Functions\Max.cs" />
<Compile Include="Functions\Min.cs" />
<Compile Include="Functions\Sum.cs" />
<Compile Include="Functions\Today.cs" />
<Compile Include="Library\PeekableEnumerator.cs" />
<Compile Include="Literals\Array.cs" />
<Compile Include="Literals\Boolean.cs" />
<Compile Include="Literals\DateTime.cs" />
<Compile Include="Literals\Literal.cs" />
<Compile Include="Literals\Number.cs" />
<Compile Include="Literals\Text.cs" />
<Compile Include="Operators\AddOperator.cs" />
<Compile Include="Operators\AndOperator.cs" />
<Compile Include="Operators\DivideOperator.cs" />
<Compile Include="Operators\EqualToOperator.cs" />
<Compile Include="Operators\GreaterThanOperator.cs" />
<Compile Include="Operators\GreaterThanOrEqualToOperator.cs" />
<Compile Include="Operators\LessThanOperator.cs" />
<Compile Include="Operators\LessThanOrEqualToOperator.cs" />
<Compile Include="Operators\ModulusOperator.cs" />
<Compile Include="Operators\MultiplyOperator.cs" />
<Compile Include="Operators\NotEqualToOperator.cs" />
<Compile Include="Operators\Operator.cs" />
<Compile Include="Operators\OrOperator.cs" />
<Compile Include="Operators\SubtractOperator.cs" />
</ItemGroup>
<ItemGroup>
<Folder Include="Properties\" />
</ItemGroup>
<ItemGroup>
<None Include="readme.md" />
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="4.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
<ProductVersion>8.0.30703</ProductVersion>
<SchemaVersion>2.0</SchemaVersion>
<ProjectGuid>{65D25F1E-BD7D-4B5E-8457-DD8A4813DD30}</ProjectGuid>
<OutputType>Library</OutputType>
<AppDesignerFolder>Properties</AppDesignerFolder>
<RootNamespace>HiSystems.Interpreter</RootNamespace>
<AssemblyName>HiSystems.Interpreter</AssemblyName>
<TargetFrameworkVersion>v3.5</TargetFrameworkVersion>
<FileAlignment>512</FileAlignment>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<DebugSymbols>true</DebugSymbols>
<DebugType>full</DebugType>
<Optimize>false</Optimize>
<OutputPath>bin\Debug\</OutputPath>
<DefineConstants>DEBUG;TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<DebugType>pdbonly</DebugType>
<Optimize>true</Optimize>
<OutputPath>bin\Release\</OutputPath>
<DefineConstants>TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<ItemGroup>
<Reference Include="System" />
<Reference Include="System.Core" />
<Reference Include="System.Xml.Linq" />
<Reference Include="System.Data.DataSetExtensions" />
<Reference Include="System.Data" />
<Reference Include="System.Xml" />
</ItemGroup>
<ItemGroup>
<Compile Include="AssemblyInfo.cs" />
<Compile Include="Constructs\FunctionOperation.cs" />
<Compile Include="Constructs\IConstruct.cs" />
<Compile Include="Constructs\Operation.cs" />
<Compile Include="Constructs\Variable.cs" />
<Compile Include="Converters\BooleanTypeConverter.cs" />
<Compile Include="Converters\DateTimeTypeConverter.cs" />
<Compile Include="Converters\NumberTypeConverter.cs" />
<Compile Include="Converters\TextTypeConverter.cs" />
<Compile Include="Converters\TimeSpanTypeConverter.cs" />
<Compile Include="Engine\Engine.cs" />
<Compile Include="Engine\Expression.cs" />
<Compile Include="Engine\ExpressionParsed.cs" />
<Compile Include="Engine\ExpressionParseOnDemand.cs" />
<Compile Include="Engine\Token.cs" />
<Compile Include="Engine\Tokenizer.cs" />
<Compile Include="Functions\ArrayFunction.cs" />
<Compile Include="Functions\Average.cs" />
<Compile Include="Functions\Format.cs" />
<Compile Include="Functions\Function.cs" />
<Compile Include="Functions\If.cs" />
<Compile Include="Functions\Len.cs" />
<Compile Include="Functions\Max.cs" />
<Compile Include="Functions\Min.cs" />
<Compile Include="Functions\Sum.cs" />
<Compile Include="Functions\Today.cs" />
<Compile Include="Library\PeekableEnumerator.cs" />
<Compile Include="Literals\Array.cs" />
<Compile Include="Literals\Boolean.cs" />
<Compile Include="Literals\DateTime.cs" />
<Compile Include="Literals\Literal.cs" />
<Compile Include="Literals\Number.cs" />
<Compile Include="Literals\Text.cs" />
<Compile Include="Literals\TimeSpan.cs" />
<Compile Include="Operators\AddOperator.cs" />
<Compile Include="Operators\AndOperator.cs" />
<Compile Include="Operators\DivideOperator.cs" />
<Compile Include="Operators\EqualToOperator.cs" />
<Compile Include="Operators\GreaterThanOperator.cs" />
<Compile Include="Operators\GreaterThanOrEqualToOperator.cs" />
<Compile Include="Operators\LessThanOperator.cs" />
<Compile Include="Operators\LessThanOrEqualToOperator.cs" />
<Compile Include="Operators\ModulusOperator.cs" />
<Compile Include="Operators\MultiplyOperator.cs" />
<Compile Include="Operators\NotEqualToOperator.cs" />
<Compile Include="Operators\Operator.cs" />
<Compile Include="Operators\OrOperator.cs" />
<Compile Include="Operators\SubtractOperator.cs" />
<Compile Include="Operators\XorOperator.cs" />
</ItemGroup>
<ItemGroup>
<Folder Include="Properties\" />
</ItemGroup>
<ItemGroup>
<None Include="readme.md" />
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
Other similar extension points exist, see Microsoft.Common.targets.
<Target Name="BeforeBuild">
</Target>
<Target Name="AfterBuild">
</Target>
-->
-->
</Project>
4 changes: 4 additions & 0 deletions Literals/Boolean.cs
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,10 @@ public static implicit operator Boolean(bool value)
return value1.value | value2.value;
}

public static Boolean operator ^(Boolean value1, Boolean value2)
{
return value1.value ^ value2.value;
}
public static Boolean operator!(Boolean value)
{
return new Boolean(!value.value);
Expand Down
12 changes: 6 additions & 6 deletions Literals/DateTime.cs
Original file line number Diff line number Diff line change
Expand Up @@ -69,19 +69,19 @@ public static implicit operator DateTime(System.DateTime value)
return !AreEqual(value1, value2);
}

public static DateTime operator+(DateTime date, Number days)
public static DateTime operator+(DateTime date, TimeSpan timeSpan)
{
return new DateTime(date.value.AddDays((double)days));
return new DateTime(date.value.Add(timeSpan));
}

public static DateTime operator-(DateTime date, Number days)
public static DateTime operator-(DateTime date, TimeSpan timeSpan)
{
return new DateTime(date.value.AddDays(-(double)days));
return new DateTime(date.value.Subtract(timeSpan));
}

public static Number operator-(DateTime date1, DateTime date2)
public static TimeSpan operator-(DateTime date1, DateTime date2)
{
return new Number(Convert.ToDecimal((date1.value - date2.value).TotalDays));
return new TimeSpan(date1.value - date2.value);
}

public static Boolean operator>(DateTime value1, DateTime value2)
Expand Down
Loading