Skip to content

Commit

Permalink
Preliminary Regexp support
Browse files Browse the repository at this point in the history
  • Loading branch information
haileys committed Feb 12, 2011
1 parent 3661cdf commit 6caef6a
Show file tree
Hide file tree
Showing 4 changed files with 166 additions and 9 deletions.
26 changes: 18 additions & 8 deletions Fructose/Fructose.csproj
@@ -1,4 +1,4 @@
<?xml version="1.0" encoding="utf-8"?>
<?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>
Expand All @@ -24,6 +24,7 @@
<DefineConstants>DEBUG;TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
<Commandlineparameters>tmp.rb</Commandlineparameters>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|x86' ">
<PlatformTarget>x86</PlatformTarget>
Expand All @@ -34,17 +35,25 @@
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<PropertyGroup>
<StartupObject />
</PropertyGroup>
<ItemGroup>
<Reference Include="System" />
<Reference Include="System.Core" />
<Reference Include="IronRuby">
<Private>True</Private>
<Reference Include="IronRuby, Version=1.1.1.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35">
<SpecificVersion>False</SpecificVersion>
<HintPath>..\lib\IronRuby.dll</HintPath>
</Reference>
<Reference Include="IronRuby.Libraries, Version=1.1.1.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35">
<SpecificVersion>False</SpecificVersion>
<HintPath>..\lib\IronRuby.Libraries.dll</HintPath>
</Reference>
<Reference Include="Microsoft.Dynamic, Version=1.1.0.10, Culture=neutral, PublicKeyToken=31bf3856ad364e35">
<SpecificVersion>False</SpecificVersion>
<HintPath>..\lib\Microsoft.Dynamic.dll</HintPath>
</Reference>
<Reference Include="Microsoft.Scripting, Version=1.1.0.10, Culture=neutral, PublicKeyToken=31bf3856ad364e35">
<SpecificVersion>False</SpecificVersion>
<HintPath>..\lib\Microsoft.Scripting.dll</HintPath>
</Reference>
<Reference Include="Microsoft.Scripting" />
<Reference Include="Microsoft.Dynamic" />
</ItemGroup>
<ItemGroup>
<Compile Include="Compiler\AstNodeGenerator.cs" />
Expand Down Expand Up @@ -74,6 +83,7 @@
<Compile Include="AssemblyInfo.cs" />
<Compile Include="Transformer\Transformer.cs" />
<Compile Include="Util.cs" />
<Compile Include="Compiler\Generators\Regex.cs" />
</ItemGroup>
<ItemGroup>
<Folder Include="Properties\" />
Expand Down
1 change: 1 addition & 0 deletions Fructose/Program.cs
Expand Up @@ -44,6 +44,7 @@ static void Usage()
Usage: fructose [( -o output-file | --stdout )] ( - | input-file )
");
Environment.Exit(1);
}

static void Fatal(string message, params object[] args)
Expand Down
2 changes: 1 addition & 1 deletion README.md
Expand Up @@ -17,7 +17,7 @@ It is composed of two parts - Fructose itself, which is written in C#, and libfr
<tr><td>Object (including Kernel)</td><td><b>Done</b></td></tr>
<tr><td>Hash</td><td><b>Done</b></td></tr>
<tr><td>Exceptions/Errors</td><td><b>Done</b></td></tr>
<tr><td>Regexp</td><td></td></tr>
<tr><td>Regexp</td><td><b>Subset Done</b></td></tr>
<tr><td>Match</td><td></td></tr>
<tr><td>Proc</td><td><b>Done</b></td></tr>
<tr><td>Random</td><td><b>Done</b></td></tr>
Expand Down
146 changes: 146 additions & 0 deletions libfructose/libfructose.php
Expand Up @@ -536,6 +536,152 @@ public static function SF_rand($max = NULL)
return F_Number::__from_number((mt_rand() / mt_getrandmax()) * $m);
}
}
class F_Regexp extends F_Object
{
public static function __from_string($str)
{
$r = new F_Regexp;
$r->__REGEXP = $str;
return $r;
}
public function __operator_eq($block, $operand)
{
return F_TrueClass::__from_bool($this->__REGEXP === $operand->__REGEXP);
}
public function __operator_stricteq($block, $operand)
{
return $this->__operator_match(NULL, $operand);
}
public function __operator_match($block, $operand)
{
if(preg_match($this->__REGEXP, $operand->F_to_s(NULL)->__STRING, $matches, PREG_OFFSET_CAPTURE) === 0)
return new F_NilClass;

return F_Number::__from_number($matches[0][1]);
}
public function F_casefold_QUES_($block)
{
return F_TrueClass::__from_bool(preg_match('/\/[a-z]*i[a-z]*$/', $this->__REGEXP));
}
public function F_to_s($block)
{
return F_String::__from_string($this->__REGEXP);
}
public function F_source($block)
{
preg_match('/^\/(.*)\/[a-z]*$/', $this->__REGEXP, $matches);
return F_String::__from_string($matches[0]);
}
public function F_match($block, $str)
{
if(preg_match($this->__REGEXP, $str->F_to_s(NULL)->__STRING, $matches, PREG_OFFSET_CAPTURE) === 0)
return new F_NilClass;
return F_MatchData::__from_array($matches, $this);
}
}
class F_MatchData extends F_Object
{
public static function __from_array($matches, $regexp)
{
$md = new F_MatchData;
$md->__MATCHES = $matches;
$md->__REGEXP = $regexp;
return $md;
}
public function __operator_arrayget($block, $idx)
{
$count = 0;
foreach($this->__MATCHES as $k=>$v)
if(is_int($k))
$count++;

if(is_a($idx, 'F_Number'))
{
$index = $idx->__NUMBER;
if($index < 0)
$index += $count;
if($index < 0)
return new F_NilClass;
if($index >= $count)
return new F_NilClass;
return F_String::__from_string($this->__MATCHES[$index][0]);
}
$index = $idx->F_to_s(NULL)->__STRING;
if(!isset($this->__MATCHES[$index]))
return new F_NilClass;
return F_String::__from_string($this->__MATCHES[$index][0]);
}
public function F_begin($block, $idx)
{
$count = 0;
foreach($this->__MATCHES as $k=>$v)
if(is_int($k))
$count++;
if(is_a($idx, 'F_Number'))
{
$index = $idx->__NUMBER;
if($index < 0)
$index += $count;
if($index < 0)
return new F_NilClass;
if($index >= $count)
return new F_NilClass;
return F_Number::__from_number($this->__MATCHES[$index][1]);
}
$index = $idx->F_to_s(NULL)->__STRING;
if(!isset($this->__MATCHES[$index]))
return new F_NilClass;
return F_Number::__from_number($this->__MATCHES[$index][1]);
}
public function F_captures($block)
{
$caps = array_map(create_function('$a','return F_String::__from_string($a[0]);'), $this->__MATCHES);
array_shift($caps);
return F_Array::__from_array($caps);
}
public function F_end($block, $idx)
{
$count = 0;
foreach($this->__MATCHES as $k=>$v)
if(is_int($k))
$count++;
if(is_a($idx, 'F_Number'))
{
$index = $idx->__NUMBER;
if($index < 0)
$index += $count;
if($index < 0)
return new F_NilClass;
if($index >= $count)
return new F_NilClass;
return F_Number::__from_number($this->__MATCHES[$index][1] + strlen($this->__MATCHES[$index][0]));
}
$index = $idx->F_to_s(NULL)->__STRING;
if(!isset($this->__MATCHES[$index]))
return new F_NilClass;
return F_Number::__from_number($this->__MATCHES[$index][1] + strlen($this->__MATCHES[$index][0]));
}
public function F_size($block)
{
$count = 0;
foreach($this->__MATCHES as $k=>$v)
if(is_int($k))
$count++;
return F_Number::__from_number($count);
}
public function F_regexp($block)
{
return $this->__REGEXP;
}
public function F_to_a($block)
{
return $this->F_captures(NULL);
}
public function F_to_s($block)
{
return F_String::__from_string($this->__MATCHES[0][0]);
}
}
class F_Enumerable extends F_Object
{
public static $_states = array();
Expand Down

0 comments on commit 6caef6a

Please sign in to comment.