Skip to content

Commit

Permalink
basic formula solving
Browse files Browse the repository at this point in the history
  • Loading branch information
ElemarJR committed Oct 26, 2015
1 parent e654814 commit 1be325a
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 3 deletions.
15 changes: 14 additions & 1 deletion demo/bellevue/Bellevue.Parser/parser.fs
@@ -1,6 +1,10 @@

namespace Bellevue.Parser

type Tokens =
| Literal of string
| Formula of string

module private Util =
let toString chars =
System.String(chars |> Array.ofList)
Expand All @@ -26,13 +30,22 @@ module private Util =
let rec parse acc chars = seq {
let emitLiteral() = seq {
if acc <> [] then
yield acc |> List.rev |> toString
yield acc |> List.rev |> toString |> Literal
}

let emitFormula body = seq {
if body <> [] then
yield body |> toString |> Formula
}

match chars with
| Bracketed ['@'; '*' ] ['*'; '@' ] (body, chars) ->
yield! emitLiteral()
yield! parse [] chars
| Bracketed ['@'; '{' ] ['}'] (body, chars) ->
yield! emitLiteral()
yield! emitFormula body
yield! parse [] chars
| h::t ->
yield! parse (h::acc) t
| [] ->
Expand Down
19 changes: 18 additions & 1 deletion demo/bellevue/Bellevue/Program.cs
@@ -1,4 +1,5 @@
using System;
using System.Globalization;
using System.IO;

using FluentIL;
Expand Down Expand Up @@ -44,12 +45,28 @@ static void Main(string[] args)
var body = main.Returns(typeof (void));
foreach (var block in TextParser.Parse(File.ReadAllText(input)))
{
body.Write(block);
if (block.IsLiteral)
{
body.Write(((Tokens.Literal) block).Item);
}
else if (block.IsFormula)
{
var formula = ((Tokens.Formula) block).Item;
// TODO: Which version of write should it use?!
body
.Parse(formula)
.Write<int>();
}
}
body.Ret();

assembly.SetEntryPoint(main);
assembly.Save();
}

static string Number()
{
return (3.1415).ToString(CultureInfo.InvariantCulture);
}
}
}
2 changes: 1 addition & 1 deletion demo/bellevue/Bellevue/sample.txt
Expand Up @@ -2,6 +2,6 @@ Hello World! @*this is a comment*@

This source text @*this is a comment*@will be compiled into an executable.

Yeap. It works!
Yeap. It works! @{2+2/2}

Bye!
8 changes: 8 additions & 0 deletions src/FluentIL/Emitters/DynamicMethodBody.cs
Expand Up @@ -38,6 +38,14 @@ public DynamicMethodBody Write(string message)
return Ldstr(message).Call(minfo);
}

public DynamicMethodBody Write<T>()
{
var minfo = typeof(Console).GetMethod(
"Write",
new[] { typeof(T) });
return Call(minfo);
}

public object Invoke(params object[] args)
{
return _methodInfo.AsDynamicMethod.Invoke(null, args);
Expand Down

0 comments on commit 1be325a

Please sign in to comment.