Angel \”Java\” Lopez on Blog

July 16, 2014

RubySharp, implementing Ruby in C# (3)

Filed under: .NET, C Sharp, Open Source Projects, Programming Languages, Ruby, RubySharp — ajlopez @ 4:43 pm

Previous Post

In RubySharp, we can define new functions (methods of the current object), and invoke them. There are some built-in functions in C#:

Every function should implement the interface:

public interface IFunction
{
    object Apply(DynamicObject self, Context context, IList<object> values);
}

On apply, each function receives the object (self), the context for variables (locals, arguments, closure….), and a list of already evaluated arguments.

An example, the implemention of puts:

public class PutsFunction : IFunction
{
    private TextWriter writer;

    public PutsFunction(TextWriter writer)
    {
        this.writer = writer;
    }

    public object Apply(DynamicObject self, Context context, IList<object> values)
    {
        foreach (var value in values)
            this.writer.WriteLine(value);

        return null;
    }
}

It receives the object to which it is a method, the context, and a list of arguments. Each of the arguments was evaluated. The implementation simply sends the arguments to a TextWriter, one argument per line. The TextWriter is provided when the function object is created (in the Machine object, that represents the current running environment). This injected facilites the test of the function, example:

[TestMethod]
public void PutsTwoIntegers()
{
    StringWriter writer = new StringWriter();
    PutsFunction function = new PutsFunction(writer);

    Assert.IsNull(function.Apply(null, null, new object[] { 123, 456 }));

    Assert.AreEqual("123\r\n456\r\n", writer.ToString());
}

The above code was born using the TDD workflow.

Let’s see another built-in function, require:

public class RequireFunction : IFunction
{
    private Machine machine;

    public RequireFunction(Machine machine)
    {
        this.machine = machine;
    }

    public object Apply(DynamicObject self, Context context, IList<object> values)
    {
        string filename = (string)values[0];
        return this.machine.RequireFile(filename);
    }
}

This time, the job of loading a file is delegated to the Machin object, injected in the constructor.

And finally, let’s review the code of a defined function (defined in RubySharp):

public class DefinedFunction : IFunction
{
    private IExpression body;
    private IList<string> parameters;
    private Context context;

    public DefinedFunction(IExpression body, IList<string> parameters, Context context)
    {
        this.body = body;
        this.context = context;
        this.parameters = parameters;
    }

    public object Apply(DynamicObject self, Context context, IList<object> values)
    {
        Context newcontext = new Context(self, this.context);

        int k = 0;
        int cv = values.Count;

        foreach (var parameter in this.parameters) 
        {
            newcontext.SetLocalValue(parameter, values[k]);
            k++;
        }

        return this.body.Evaluate(newcontext);
    }
}

In this case, a new contexts is built, containing the new arguments to the defined function, but with the original closure, injected in the constructor. The context provided in the Apply is not used.

Next topics: some command implementations, the Machine object, the REPL, etc…

Stay tuned!

Angel “Java” Lopez

http://www.ajlopez.com

http://twitter.com/ajlopez

July 13, 2014

SharpBus in C# (1) First Steps

Filed under: .NET, C Sharp, Open Source Projects, SharpBus, Test-Driven Development — ajlopez @ 11:54 am

Last month I started to write SharpBus, a simple message processor inspired by ideas from Mule Java project. See:

http://en.wikipedia.org/wiki/Mule_(software)

http://www.mulesoft.org/what-mule-esb

As usual, I’m using TDD (Test-Driven Development), implementing small use cases and API consumption, in the simplest possible way. In fact, the project is a personal exercise of TDD. I hope that some may be useful beyond that. For now, it is an example that I want to share my workflow.

You can see the progress in:

https://github.com/ajlopez/SharpBus

The main ideas to implement are:

- There are Messages containing Payloads and Properties (dictionary of name / value)

- There Flows that process messages

- A Flow is composed of several elements

The elements can be chained in the flow, and can be:

- A Transformer, taking a payload or complete message, and returns a transformed version

- A Processor, taking a payload or complete message, operate on them, maybe accessing the business domain, but without transformation

- Input, a payload/message producer

- Output, a payload/message consumer, at the end od the Flow process

- A Rourter, given a payload/message, it decides with Branch to follow in the Flow. Each Branch is identified by a name.

There will also be items that can issue new posts than the current, and can issue them to different flows in the system. The message producers should be able to run threads (Threads) in principle. And there will be a set of threads (Pool of Threads) attending the incoming messages and deriving them to the respective flows.

A flow can take a message and return a response (Request / Response), or it can take a message, process it without returning an answer.

Some items need complete Message and other items operate over the Payload.

The elements in the world of Mule / Java are objects. Notably, in C #, the simplest way to implement is using lambdas. So, following TDD, the early testing and deployment has come out that way. You can see in some simple tests:

https://github.com/ajlopez/SharpBus/blob/master/Src/SharpBus.Core.Tests/FlowTests.cs

As an example of send a Payload and receive an answer, using an empty Flow:

var flow = Flow.Create();

Assert.AreEqual(1, flow.Send(1));

To send a payload and transform it:

var flow = Flow.Create()
    .Transform(x => ((int)x) + 1);

Assert.AreEqual(2, flow.Send(1));

The .Transform Transform expects a Func<object, object> which may be provided at the time with a lambda. In a classic Java 7 Mule, we should provide an object that implements an interface having a method to transform the Payload. I have to think whether it is worth having a typed Transform <T> (receiving T and returns T), or Transform <T, S> (receiving and returns T S).

I decided to build the flow by code. In the Mule environment Spring is used to set up the flow using XML. I think it is time to explore this other way, build the flow by code, and put it as the "first way", ie, as the way on which everything else could be mounted. If anyone ever needed a DSL (Domain-Specific Language) for creating flows could be implemented this way by having well designed code.

Following TDD, I still have not a case of using a Message (a complete message with content (Payload) and properties (Properties, a name / value) dictionary. All of the above is to process Payload directly.

Upcoming topics: add Message Payload with the output of messages, routing, assembling branches, etc. ..

Stay tuned!

Angel “Java” Lopez

http://www.ajlopez.com

http://twitter.com/ajlopez

July 12, 2014

Mass Programming Language (5) Variables Scope

Filed under: .NET, C Sharp, Mass, Open Source Projects, Programming Languages — ajlopez @ 6:18 pm

Previos Post

I finally started a new implementation of variables and their scope in programming language Mass. My idea is:

- Have local variables

- No need to explicitly declare

- Access available "outside" if necessary

The basic idea is:

- Any variable assignment involves the local level

That is, if I assign

a = 3

in a function, this refers to a local variable in the function. No matter if there is a variable external to the function

See for example

https://github.com/ajlopez/Mass/blob/master/Src/Mass.Core.Tests/MachineFiles/Local.ms

# file variable
a = 1

define foo()
    a = 2 # local variable
end

foo()

The first variable a is a variable of the file in process (the module). The variable assigned in the foo function is local to that function, and it is different from the outside variable.

But if we do not assign the variable, and only consulted it, its value is returned. example:

https://github.com/ajlopez/Mass/blob/master/Src/Mass.Core.Tests/MachineFiles/Scope.ms

# file variable
a = 1

define foo()
   a # access file variable
end

foo()

In this case, the function foo returns the value to "outside", since this variable has not been assigned in the function when evaluating the return value.

How to edit an "external” variable, that is, a non-local variablel? Well, this is a topic for an upcoming post. The idea: to access the value of a non-local variable, it must be implied, without declaring anything extra. BUT modifying its value must should be explicit: we put something to explicitly declare "we are changing the value of a non-local variable".

Stay tuned!

Angel “Java” Lopez

http://www.ajlopez.com

http://twitter.com/ajlopez

GrammGen In C# (2) First Rules

Filed under: .NET, C Sharp, GrammGen, Open Source Projects — ajlopez @ 10:30 am

Previous Post

Let’s see today how a parser is built with GrammGen using code. The idea is to define using GrammGen how to build a tree with expressions detected in the text under process. The text is written in the programming language that we want to implement. The expression tree is created using the GrammGen definition, given by code.

I presented in the previous post for an example of a console program, which employs GrammGen. A parser is defined using rules. The rules indicate:

- How to process text characters
- How then form text elements, such as numbers, names, operators
- How to go from putting together such elements, expressions, commands that define our language to implement.

In the sample of calculator

https://github.com/ajlopez/GrammGen/tree/master/Samples/Calculator

the rules are defined by the parser:

https://github.com/ajlopez/GrammGen/blob/master/Samples/Calculator/Calculator/ExpressionParser.cs

The first rules:

private static Rule[] rules = new Rule[]
{
   Rule.Or(' ', '\t', '\r', '\n').Skip(),
   Rule.Or('+', '-').Generate("Oper0"),
   Rule.Or('*', '/').Generate("Oper1"),
// ...


The idea is to get an Expression. The first rule defines that anyone (Rule.Or) of space, tab, carriage return, new line items are ignored (. Skip ()). You can put more than one rule of this type, for example, to set comments to be ignored from the input text.

The second rule defines both + (the plus sign) and – (minus sign) produce a called terminal Oper0. They differ from * (the token) and / (divide sign) because we want to take precedence among operators.

There is a rule to construct integers:

 

Rule.Get("0-9").OneOrMore().Generate("Integer", MakeIntegerConstantExpression), 

The Rule.Get allows you to specify a range of characters, and then, with the fluent interface defined, you can attach the .OneOrMore () method, which specifies that the rule can be applied once or several times. When you can not apply more, it ends generating a node called "Integer". But in GrammGen a node, in addition to a name, may have an associated object. Al. A second parameter can be passed to Generate method, with a function that creates the associated object.

MakeIntegerConstantExpression job is:

- Get the object that was formed with the rule, in this case, a concatenated string with all digits were found.

- Transform it to integer

- Create a ConstantExpression with the intever as constant value.

The class ConstantExpression is not from GrammGen. It is defined by us. We have the freedom to define the objects we want to attach to each node of the GrammGen generated tree.

The code of MakeIntegerConstantExpression is:


   private static object MakeIntegerConstantExpression(object obj)
   {
       int value = int.Parse((string)obj);

       return new ConstantExpression(value);
   }

In the next posts we will see how the nodes of binary operations are formed, how operator precedence is implemented, how to manage left recursion, and evaluation of the generated expressions.

Stay tuned!

Angel “Java” Lopez

http://www.ajlopez.com

http://twitter.com/ajlopez

July 10, 2014

End Of Iteration 2014w27

Filed under: .NET, AjGenesis, C Sharp, Code Generation, Iteration, JavaScript, NodeJs — ajlopez @ 2:33 pm

Previous Post

Code Generation with AjGenesisNode

I updated and published new versions of my projects:

https://github.com/ajlopez/AjGenesisNode
https://github.com/ajlopez/AjGenesisNode-Entity
https://github.com/ajlopez/AjGenesisNode-Model
https://github.com/ajlopez/AjGenesisNode-Sinatra
https://github.com/ajlopez/AjGenesisNode-Express

Now, there are new functions to manage the model directory in AjGenesis. That directory contains the JSON files that represent the model, ie entities, project settings, db settings, etc. It resides in “./ajgenesis/models”.

The code generation for Sinatra and Express was improved. Instead of create a project and then, generate a “build” subdirectory in project directory, now the code generation acts over the current project directory. This way is more straighforward, and more aligned with other projects that generates code: there is no “build” subdirectory.

I added the preserve of code regions by name. If a generated file has a code region delimited with special text, that region is preserved even when the file is regenerated.

DylanSharp

My Dylan language interpreted in C#

https://github.com/ajlopez/DylanSharp

I added more binary operations. I should add type support, but in the current use cases, the untyped evaluation is OK.

SharpBus

My Mule-like simple service bus in C#

https://github.com/ajlopez/SharpBus

I added message transformers, message processors, instead of play payload transformers and processors. And I added a Router object. So, the implementation has/will have:

- Lambdas that operates on payload (processors, transformers, outputs, routers)
- Lambdas that operatos on message (processors, transformers, outputs, routers)
- Objects that operates on payload (processors, transformers, outputs, routers)
- Objects that operates on messages (processors, transformers, outputs, routers)

A payload is an untyped object. A message has a payload and additional properties (by name/value).

JavaScript Samples

I wrote my first AngularJS samples

https://github.com/ajlopez/JavaScriptSamples/tree/master/Angular

Dynamic Site in Express 4

Based on JSON definitions

https://github.com/ajlopez/ExpressSamples/tree/master/DynSite4

I added the support of https://github.com/ajlopez/LoadJsons to load JSON files that have references to other JSON files. The site is a prototype based on pages defined in JSON files. So you can easy change the JSON data to alter the prototyped site. Next steps: add lists based on MongoDB collections, with views. Then, code generation for differents targets (Express 4? Laravel?)

Others

I changed https://github.com/ajlopez/SharpGo with minor improvements. I also worked on three non-public projects. More fun is coming.

Stay tuned!

Angel “Java” Lopez
http://www.ajlopez.com
http://twitter.com/ajlopez

June 27, 2014

End Of Iteration 2014w25

Previous Post
Next Post

Code Generation with AjGenesis

I published a new version of

https://github.com/ajlopez/AjGenesisNode

My code generation tool, based on Node.js. Now, it can preserve files in generate tasks (if you want one of your files doesn’t overwritten). But the key change was to put models under current project directory, ajgenesis/models folder. In this way, I can put all the AjGenesis-related stuff in only one directory related to current project. So, I refactored and republished the modules and examples of:

https://github.com/ajlopez/AjGenesisNode-Express
https://github.com/ajlopez/AjGenesisNode-Entity
https://github.com/ajlopez/AjGenesisNode-Hello

Next steps: refactor https://github.com/ajlopez/AjGenesisNode-Model to use the new directory, and refactor/complete the generation process in Express, Sinatra, Laravel, Django projects.

SharpBus

I worked on

https://github.com/ajlopez/SharpBus

implemented a simplified Mule-like message flow in C#, using TDD (Test-Driven Development). Now I have: input, output, transform, processors, routers, branches in flow. It was nice to see all in place, using “baby steps”, and the powerful lambdas of C#. I implemented transformer objects too. Next steps: instead of simply process a payload, start to process a complete message.

SimpleCurry

After attending a JavaScript meetup at Buenos Aires, I wrote a simple JavaScript curryfing function library:

https://github.com/ajlopez/SimpleCurry

Since then, I started to add some additional pieces, like partial application, and composition of functions.

SimpleStates

Thanks to @fabiomaulo, I met state machine implementations in C#. So, I wrote my own version in JavaScript:

https://github.com/ajlopez/SimpleStates

It has a fluent interface to define states, with triggers and actions. Next steps: add global triggers, and hierarchical states.

Others

I added some functions to https://github.com/ajlopez/SharpMongo core and REPL. I worked on four private projects, too. More fun is comming.

Stay tuned!

Angel “Java” Lopez
http://www.ajlopez.com
http://twitter.com/ajlopez

June 20, 2014

End Of Iteration 2014w24

Filed under: .NET, C Sharp, Iteration, Mass, Open Source Projects, SharpBus, SharpGo, SharpMongo — ajlopez @ 6:27 pm

Previous Post
Next Post

SharpGo

My Go Programming Language interpreter in C#

https://github.com/ajlopez/SharpGo

I added typed nodes to Abstract Syntax Tree, and nodes that generate expressions instead of commands. The types will help me to determine if an expression is valid or not, according to Go type specifications.

Mass

The Mass programming language interpreter

https://github.com/ajlopez/Mass

I reimplemented binary arithmetic operations (and string concatenation) without using ObOps, but directly using compiled lambda expressions. Next steps: removing ObOps dependency from compare operations. I think that a compiled lambda is a better outcome for such expression.

SharpMongo

My in-process, in-memory implementation in C# of a document database with a MongoDB-like interface:

https://github.com/ajlopez/SharpMongo

I fixed source analysis issuses, and added .count method to collections and to collection objects in REPL.

SharpBus

I started

https://github.com/ajlopez/SharpBus

A simple message bus implemented in C#, inspired by Mule. Notably, the flow of message can be defined using lambdas (something missing in Mule Java up to version 7). Using TDD, using fluent interface, consuming lamdbas, is the simplest way I found to implement a message bus.

Others

I worked fixing scripts in https://github.com/ajlopez/ajlopezsite. My parser “generator” https://github.com/ajlopez/GrammGen was  converted to VisualStudio 2010I worked on four non-public projects. More fun is coming.

Stay tuned!

Angel “Java” Lopez
http://www.ajlopez.com
http://twitter.com/ajlopez

February 21, 2014

Owin: Links And Resources (2)

Filed under: .NET, C Sharp, Links, Owin — ajlopez @ 7:24 pm

Previous Post

Byte Rot: OWIN and Katana challenges: blues of a library developer
http://byterot.blogspot.com.ar/2013/08/OWIN-Katana-challenges-blues-library-developer-aspnetwebapi-nancyfx.html

damianh (Damian Hickey)
https://github.com/damianh

owin Archives – StrathWeb
http://www.strathweb.com/category/owin/

Getting Started With OWIN and the Katana Stack – Tugberk Ugurlu’s Blog
http://www.tugberkugurlu.com/archive/getting-started-with-owin-and-the-katana-stack

Owin.Dependencies: An IoC Container Adapter Into OWIN Pipeline – Tugberk Ugurlu’s Blog
http://www.tugberkugurlu.com/archive/owin-dependencies–an-ioc-container-adapter-into-owin-pipeline

Introducing SaasKit – Multi-tenancy made easy – Ben Foster
http://ben.onfabrik.com/posts/saaskit-multi-tenancy-made-easy

Are we gOWINg in the right direction? | Roysvork
http://roysvork.wordpress.com/2014/02/18/are-we-gowing-in-the-right-direction/

The Liara Framework by Prasanna V. Loganathar
http://prasannavl.github.io/Liara/

davidfowl/OwinHttpClient
https://github.com/davidfowl/OwinHttpClient

prabirshrestha/simple-owin
https://github.com/prabirshrestha/simple-owin

OWIN Middleware in the IIS Integrated Pipeline : The Official Microsoft ASP.NET Site
http://www.asp.net/aspnet/overview/owin-and-katana/owin-middleware-in-the-iis-integrated-pipeline

OWIN and Katana : The Official Microsoft ASP.NET Site
http://www.asp.net/aspnet/overview/owin-and-katana

Fix and OWIN and Simple.Web – codeface
http://blog.markrendle.net/2013/10/01/fix-and-owin-and-simple-web/

fracture
https://github.com/fractureio

ASP.NET MVC 5 Fundamentals – Online Training Course for Developers
http://pluralsight.com/training/Courses/TableOfContents/aspdotnet-mvc5-fundamentals

Writing OWIN middleware
http://odetocode.com/blogs/scott/archive/2013/11/11/writing-owin-middleware.aspx

ASP.NET – Getting Started with the Katana Project
http://msdn.microsoft.com/en-us/magazine/dn451439.aspx

Understanding OWIN Forms authentication in MVC 5 – .NET Web Development and Tools Blog – Site Home – MSDN Blogs
http://blogs.msdn.com/b/webdev/archive/2013/07/03/understanding-owin-forms-authentication-in-mvc-5.aspx

My Links
http://delicious.com/ajlopez/owin

Keep tuned!

Angel “Java” Lopez
http://www.ajlopez.com
http://twitter.com/ajlopez

February 20, 2014

Owin: Links and Resources (1)

Filed under: .NET, C Sharp, Links, Owin — ajlopez @ 7:37 pm

Next Post

OWIN — Open Web Interface for .NET
http://owin.org/

Getting up to speed with Katana – Jon Galloway
http://weblogs.asp.net/jgalloway/archive/2013/05/30/getting-up-to-speed-with-katana.aspx

Tomek on Software: Access MS SQL from a node.js application using OWIN, in-process CLR hosting, .NET, and ADO.NET
http://tomasz.janczuk.org/2013/02/access-ms-sql-from-nodejs-application.html

Tomek on Software: CPU bound workers for node.js applications using in-process .NET and OWIN
http://tomasz.janczuk.org/2013/02/cpu-bound-workers-for-nodejs.html

tjanczuk/owin · GitHub
https://github.com/tjanczuk/owin

Tomek on Software: Hosting .NET code in node.js applications using OWIN
http://tomasz.janczuk.org/2013/02/hosting-net-code-in-nodejs-applications.html

gsscoder/owinhttplistener · GitHub
https://github.com/gsscoder/owinhttplistener

Herding Code 123: Andreas Håkansson and Steven Robbins on NancyFx
http://herdingcode.com/?p=350

Kayak, a C# HTTP server.
http://kayakhttp.com/

OWIN — Open Web Server Interface for .NET, v1.0 Draft 5
http://owin.org/spec.html#Overview

Hanselminutes Podcast 264 – This is not your father’s WCF – All about the WebAPI with Glenn Block – Scott Hanselman
http://www.hanselman.com/blog/HanselminutesPodcast264ThisIsNotYourFathersWCFAllAboutTheWebAPIWithGlennBlock.aspx

Getting fancy with Nancy – Introduction – TheCodeJunkie
http://thecodejunkie.com/getting-fancy-with-nancy-introduction

CodePaste.NET – Processor for supporting uri extensions which map to variant representations
http://codepaste.net/j8spid

My Links
http://delicious.com/ajlopez/owin

February 19, 2014

End Of Iteration 2014w07

Filed under: .NET, C Sharp, Iteration, JavaScript, NodeJs, Open Source Projects, Scala, ScalaSharp — ajlopez @ 7:15 pm

Previous Post
Next Post

A lot of interesting topics and code.

MeteorSamples

I started to explore Meteor:

https://github.com/ajlopez/MeteorSamples

You can see one of my samples deployed at:

http://mycompany.meteor.com/

A simple CRUD (WIP) application. Next steps: code generation using AjGenesis for Node.js, mobile look, and wrap up as a PhoneGap application.

I also wrote posts:

Programming Meteor (1) First Steps
Meteor: Links And Resources (1)

ScalaSharp

My Scala interpreter in C#:

https://github.com/ajlopez/ScalaSharp#

I started to switch from a tree of expression to a tree of nodes (a classical AST). In this way, I could implement type checking, compilation, etc… visiting the node tree. There are differences between an expression and a node. A node could be a name, but that name, in Scala, could be a val, a var, or a def. I have not the full context to decide what kind of expression to invoke. So, I need a previous step. A node is the first parser output. A tree node could be visited, performing type checking and then producing the correct expression.

SimpleReact

Inspired by Meteor .autorun method, I wrote a react library in JavaScript

https://github.com/ajlopez/SimpleReact

As usual, simple code, using TDD workflow.

SimpleScraper

I continue adding features to

https://github.com/ajlopez/SimpleScraper

A simple JavaScript library to process an HTML string. I should add more features, and write a sample, scraping an e-commerce site.

Others

A minor addition in ClojSharp https://github.com/ajlopez/ClojSharp using recur in a function

Completing the model in https://github.com/ajlopez/AjGenesisNode-Express supporting that an entity references another entity.

I worked on two non-public projects, too. More fun is coming.

Keep tuned!

Angel “Java” Lopez
http://www.ajlopez.com
http://twitter.com/ajlopez

« Newer PostsOlder Posts »

The Shocking Blue Green Theme. Blog at WordPress.com.

Follow

Get every new post delivered to your Inbox.

Join 66 other followers