Permalink
Browse files

IdFunction, httpserver.py (WIP), fix if parser with indent and wo/else

  • Loading branch information...
ajlopez committed Nov 16, 2012
1 parent 5adef83 commit e33f1d0b32c1068018193557ad8419457fc8e5bf
@@ -10,14 +10,20 @@
using PythonSharp.Compiler;
using PythonSharp.Expressions;
using PythonSharp.Utilities;
+ using System.IO;
public class Program
{
public static void Main(string[] args)
{
+ PythonSharp.Machine machine = new PythonSharp.Machine();
+
+ if (args != null && args.Length > 0)
+ if (ProcessFiles(args, machine))
+ return;
+
PrintIntro();
- PythonSharp.Machine machine = new PythonSharp.Machine();
Parser parser = new Parser(System.Console.In);
while (true)
@@ -53,5 +59,23 @@ private static void PrintIntro()
System.Console.Write(">>> ");
System.Console.Out.Flush();
}
+
+ private static bool ProcessFiles(string[] args, Machine machine)
+ {
+ bool hasfiles = false;
+
+ foreach (var arg in args)
+ {
+ if (!arg.EndsWith(".py"))
+ continue;
+
+ hasfiles = true;
+ Parser parser = new Parser(new StreamReader(arg));
+ ICommand command = parser.CompileCommandList();
+ command.Execute(machine.Environment);
+ }
+
+ return hasfiles;
+ }
}
}
@@ -9,7 +9,7 @@
<OutputType>Exe</OutputType>
<AppDesignerFolder>Properties</AppDesignerFolder>
<RootNamespace>PythonSharp.Console</RootNamespace>
- <AssemblyName>PythonSharp.Console</AssemblyName>
+ <AssemblyName>pythonsh</AssemblyName>
<TargetFrameworkVersion>v3.5</TargetFrameworkVersion>
<FileAlignment>512</FileAlignment>
</PropertyGroup>
@@ -64,6 +64,11 @@
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
</ItemGroup>
+ <ItemGroup>
+ <Content Include="examples\httpserver.py">
+ <CopyToOutputDirectory>Always</CopyToOutputDirectory>
+ </Content>
+ </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.
@@ -0,0 +1,33 @@
+
+from System import Array, Byte
+from System.Net import HttpListener
+from System.IO import Path, FileStream, FileMode
+
+root = "c:/apache-tomcat-6.0.18/webapps/docs"
+
+bytes = Array.CreateInstance(Byte,1024)
+
+listener = HttpListener()
+listener.Prefixes.Add("http://*:8000/")
+
+def process(context):
+ filename = context.Request.Url.AbsolutePath
+ print(filename)
+ if not filename:
+ filename = "index.html"
+ filename = Path.Combine(root, filename)
+ print(filename)
+ input = FileStream(filename, FileMode.Open)
+ bytes = Array.CreateInstance(Byte, 1024 * 16)
+ nbytes = input.Read(bytes, 0, bytes.Length)
+ while nbytes>0:
+ context.Response.OutputStream.Write(bytes, 0, nbytes)
+ nbytes = input.Read(bytes, 0, bytes.Length)
+ input.Close()
+ context.Response.OutputStream.Close()
+
+listener.Start()
+
+while True:
+ process(listener.GetContext())
+
@@ -0,0 +1,4 @@
+def positive(n):
+ if n>0:
+ return True
+ return False
@@ -0,0 +1,33 @@
+
+from System import Array, Byte
+from System.Net import HttpListener
+from System.IO import Path, FileStream, FileMode
+
+root = "c:/apache-tomcat-6.0.18/webapps/docs"
+
+bytes = Array.CreateInstance(Byte,1024)
+
+listener = HttpListener()
+listener.Prefixes.Add("http://*:8000/")
+
+def process(context):
+ filename = context.Request.Url.AbsolutePath
+ print(filename)
+ if not filename:
+ filename = "index.html"
+ filename = Path.Combine(root, filename)
+ print(filename)
+ input = FileStream(filename, FileMode.Open)
+ bytes = Array.CreateInstance(Byte, 1024 * 16)
+ nbytes = input.Read(bytes, 0, bytes.Length)
+ while nbytes>0:
+ context.Response.OutputStream.Write(bytes, 0, nbytes)
+ nbytes = input.Read(bytes, 0, bytes.Length)
+ input.Close()
+ context.Response.OutputStream.Close()
+
+listener.Start()
+
+while True:
+ process(listener.GetContext())
+
@@ -83,5 +83,23 @@ public void EvaluateImportFrom()
Assert.AreEqual(1, machine.Environment.GetValue("one"));
Assert.AreEqual(2, machine.Environment.GetValue("two"));
}
+
+ [TestMethod]
+ [DeploymentItem("Examples/defif.py")]
+ public void CompileDefIf()
+ {
+ Parser parser = new Parser(new StreamReader("defif.py"));
+
+ ICommand command = parser.CompileCommandList();
+ }
+
+ [TestMethod]
+ [DeploymentItem("Examples/httpserver.py")]
+ public void CompileHttpServer()
+ {
+ Parser parser = new Parser(new StreamReader("httpserver.py"));
+
+ ICommand command = parser.CompileCommandList();
+ }
}
}
@@ -0,0 +1,91 @@
+namespace PythonSharp.Tests.Functions
+{
+ using System;
+ using System.Collections;
+ using System.Collections.Generic;
+ using System.Linq;
+ using System.Text;
+ using Microsoft.VisualStudio.TestTools.UnitTesting;
+ using PythonSharp.Exceptions;
+ using PythonSharp.Functions;
+ using PythonSharp.Language;
+ using PythonSharp.Tests.Classes;
+
+ [TestClass]
+ public class IdFunctionTests
+ {
+ private IFunction id;
+
+ [TestInitialize]
+ public void Setup()
+ {
+ this.id = new IdFunction();
+ }
+
+ [TestMethod]
+ public void IdString()
+ {
+ string text = "spam";
+ Assert.AreEqual(text.GetHashCode(), this.id.Apply(null, new object[] { text }, null));
+ }
+
+ [TestMethod]
+ public void IdNull()
+ {
+ Assert.AreEqual(0, this.id.Apply(null, new object[] { null }, null));
+ }
+
+ [TestMethod]
+ public void IdObject()
+ {
+ var person = new Person();
+ Assert.AreEqual(person.GetHashCode(), this.id.Apply(null, new object[] { person }, null));
+ }
+
+ [TestMethod]
+ public void RaiseWhenNullAsArguments()
+ {
+ try
+ {
+ this.id.Apply(null, null, null);
+ Assert.Fail("Exception expected");
+ }
+ catch (Exception ex)
+ {
+ Assert.IsInstanceOfType(ex, typeof(TypeError));
+ Assert.AreEqual("id() takes exactly one argument (0 given)", ex.Message);
+ }
+ }
+
+ [TestMethod]
+ public void RaiseWhenZeroArguments()
+ {
+ try
+ {
+ this.id.Apply(null, new object[] { }, null);
+ Assert.Fail("Exception expected");
+ }
+ catch (Exception ex)
+ {
+ Assert.IsInstanceOfType(ex, typeof(TypeError));
+ Assert.AreEqual("id() takes exactly one argument (0 given)", ex.Message);
+ }
+ }
+
+ [TestMethod]
+ public void RaiseWhenTwoArguments()
+ {
+ try
+ {
+ this.id.Apply(null, new object[] { 1, 2 }, null);
+ Assert.Fail("Exception expected");
+ }
+ catch (Exception ex)
+ {
+ Assert.IsInstanceOfType(ex, typeof(TypeError));
+ Assert.AreEqual("id() takes exactly one argument (2 given)", ex.Message);
+ }
+ }
+ }
+}
+
@@ -45,6 +45,8 @@ public void BuiltinFunctions()
Assert.IsInstanceOfType(machine.Environment.GetValue("dir"), typeof(DirFunction));
Assert.IsNotNull(machine.Environment.GetValue("exit"));
Assert.IsInstanceOfType(machine.Environment.GetValue("exit"), typeof(ExitFunction));
+ Assert.IsNotNull(machine.Environment.GetValue("id"));
+ Assert.IsInstanceOfType(machine.Environment.GetValue("id"), typeof(IdFunction));
var globals = machine.Environment.GetValue("globals");
Assert.IsNotNull(globals);
@@ -71,6 +71,7 @@
<Compile Include="Expressions\SlicedExpressionTests.cs" />
<Compile Include="Expressions\SliceExpressionTests.cs" />
<Compile Include="Functions\ContextFunctionTests.cs" />
+ <Compile Include="Functions\IdFunctionTests.cs" />
<Compile Include="Functions\ExitFunctionTests.cs" />
<Compile Include="Functions\DirFunctionTests.cs" />
<Compile Include="Functions\ExecFunctionTests.cs" />
@@ -170,6 +171,14 @@
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
</ItemGroup>
+ <ItemGroup>
+ <None Include="Examples\defif.py">
+ <CopyToOutputDirectory>Always</CopyToOutputDirectory>
+ </None>
+ <Content Include="Examples\httpserver.py">
+ <CopyToOutputDirectory>Always</CopyToOutputDirectory>
+ </Content>
+ </ItemGroup>
<Import Project="$(MSBuildBinPath)\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.
@@ -501,16 +501,14 @@ private ICommand CompileIfCommand()
int indent = this.lexer.NextIndent();
- if (indent != this.indent)
- {
- this.lexer.PushIndent(indent);
- }
- else if (this.TryCompile(TokenType.Name, "else"))
+ if (indent == this.indent && this.TryCompile(TokenType.Name, "else"))
{
ICommand elsecommand = this.CompileSuite();
return new IfCommand(condition, thencommand, elsecommand);
}
+ else
+ this.lexer.PushIndent(indent);
return new IfCommand(condition, thencommand);
}
@@ -0,0 +1,28 @@
+namespace PythonSharp.Functions
+{
+ using System;
+ using System.Collections;
+ using System.Collections.Generic;
+ using System.Linq;
+ using System.Text;
+ using PythonSharp.Exceptions;
+ using PythonSharp.Language;
+
+ public class IdFunction : IFunction
+ {
+ public object Apply(IContext context, IList<object> arguments, IDictionary<string, object> namedArguments)
+ {
+ int nargs = arguments == null ? 0 : arguments.Count;
+
+ if (nargs != 1)
+ throw new TypeError(string.Format("id() takes exactly one argument ({0} given)", nargs));
+
+ object argument = arguments[0];
+
+ if (argument == null)
+ return 0;
+
+ return argument.GetHashCode();
+ }
+ }
+}
@@ -22,6 +22,7 @@ public Machine()
this.environment.SetValue("exec", new ExecFunction());
this.environment.SetValue("dir", new DirFunction());
this.environment.SetValue("exit", new ExitFunction());
+ this.environment.SetValue("id", new IdFunction());
this.environment.SetValue("locals", new ContextFunction("locals", false));
this.environment.SetValue("globals", new ContextFunction("globals", true));
this.environment.SetValue("__machine__", this);
@@ -85,6 +85,7 @@
<Compile Include="Expressions\NotExpression.cs" />
<Compile Include="Expressions\ParameterExpression.cs" />
<Compile Include="Functions\ContextFunction.cs" />
+ <Compile Include="Functions\IdFunction.cs" />
<Compile Include="Functions\ExitFunction.cs" />
<Compile Include="Functions\DirFunction.cs" />
<Compile Include="Functions\ExecFunction.cs" />
Oops, something went wrong.

0 comments on commit e33f1d0

Please sign in to comment.